Fix inverted Ctrl+right/left handling in RTL fields (again) (#2191)

This commit is contained in:
Abdo 2022-11-15 04:14:18 +03:00 committed by GitHub
parent 063623af3c
commit 434287a50a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 2 deletions

View file

@ -14,7 +14,11 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { MirrorAction } from "../sveltelib/dom-mirror";
import type { SetupInputHandlerAction } from "../sveltelib/input-handler";
import type { ContentEditableAPI } from "./content-editable";
import { preventBuiltinShortcuts, useFocusHandler } from "./content-editable";
import {
fixRTLKeyboardNav,
preventBuiltinShortcuts,
useFocusHandler,
} from "./content-editable";
export let resolve: (editable: HTMLElement) => void;
@ -40,6 +44,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
use:resolve
use:setupFocusHandling
use:preventBuiltinShortcuts
use:fixRTLKeyboardNav
use:mirrorAction={mirrorOptions}
use:inputHandlerAction={{}}
on:focus

View file

@ -5,6 +5,7 @@ import type { SelectionLocation } from "../domlib/location";
import { restoreSelection, saveSelection } from "../domlib/location";
import { placeCaretAfterContent } from "../domlib/place-caret";
import { bridgeCommand } from "../lib/bridgecommand";
import { getSelection } from "../lib/cross-browser";
import { on, preventDefault } from "../lib/events";
import { isApplePlatform } from "../lib/platform";
import { registerShortcut } from "../lib/shortcuts";
@ -138,6 +139,38 @@ export function preventBuiltinShortcuts(editable: HTMLElement): void {
}
}
declare global {
interface Selection {
modify(s: string, t: string, u: string): void;
}
}
// Fix inverted Ctrl+right/left handling in RTL fields
export function fixRTLKeyboardNav(editable: HTMLElement): void {
editable.addEventListener("keydown", (evt: KeyboardEvent) => {
if (window.getComputedStyle(editable).direction === "rtl") {
const selection = getSelection(editable)!;
let granularity = "character";
let alter = "move";
if (evt.ctrlKey) {
granularity = "word";
}
if (evt.shiftKey) {
alter = "extend";
}
if (evt.code === "ArrowRight") {
selection.modify(alter, "right", granularity);
evt.preventDefault();
return;
} else if (evt.code === "ArrowLeft") {
selection.modify(alter, "left", granularity);
evt.preventDefault();
return;
}
}
});
}
/** API */
export interface ContentEditableAPI {

View file

@ -11,7 +11,6 @@ import "../sveltelib/export-runtime";
declare global {
interface Selection {
modify(s: string, t: string, u: string): void;
addRange(r: Range): void;
removeAllRanges(): void;
getRangeAt(n: number): Range;