mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
Fix inverted Ctrl+right/left handling in RTL fields (again) (#2191)
This commit is contained in:
parent
063623af3c
commit
434287a50a
3 changed files with 39 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue