anki/ts/lib/shortcuts.ts

146 lines
4.3 KiB
TypeScript
Raw Normal View History

2021-04-22 16:49:30 +02:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import * as tr from "./i18n";
export type Modifier = "Control" | "Alt" | "Shift" | "Meta";
const modifiers: Modifier[] = ["Control", "Alt", "Shift", "Meta"];
2021-04-21 23:59:50 +02:00
function isApplePlatform(): boolean {
return (
window.navigator.platform.startsWith("Mac") ||
window.navigator.platform.startsWith("iP")
);
}
// how modifiers are mapped
const platformModifiers = isApplePlatform()
? ["Meta", "Alt", "Shift", "Control"]
: ["Control", "Alt", "Shift", "OS"];
function modifiersToPlatformString(modifiers: string[]): string {
const displayModifiers = isApplePlatform()
? ["^", "⌥", "⇧", "⌘"]
: [`${tr.keyboardCtrl()}+`, "Alt+", `${tr.keyboardShift()}+`, "Win+"];
let result = "";
for (const modifier of modifiers) {
result += displayModifiers[platformModifiers.indexOf(modifier)];
}
return result;
}
const alphabeticPrefix = "Key";
const numericPrefix = "Digit";
const keyToCharacterMap = {
Backslash: "\\",
Backquote: "`",
BracketLeft: "[",
BrackerRight: "]",
Quote: "'",
Semicolon: ";",
Minus: "-",
Equal: "=",
Comma: ",",
Period: ".",
Slash: "/",
};
function keyToPlatformString(key: string): string {
if (key.startsWith(alphabeticPrefix)) {
return key.slice(alphabeticPrefix.length);
} else if (key.startsWith(numericPrefix)) {
return key.slice(numericPrefix.length);
} else if (Object.prototype.hasOwnProperty.call(keyToCharacterMap, key)) {
return keyToCharacterMap[key];
} else {
return key;
}
}
2021-04-22 15:24:27 +02:00
function toPlatformString(modifiersAndKey: string[]): string {
return `${modifiersToPlatformString(
modifiersAndKey.slice(0, -1)
)}${keyToPlatformString(modifiersAndKey[modifiersAndKey.length - 1])}`;
}
export function getPlatformString(keyCombination: string[][]): string {
return keyCombination.map(toPlatformString).join(", ");
}
2021-04-21 23:59:50 +02:00
function checkKey(event: KeyboardEvent, key: string): boolean {
return event.key === key;
2021-04-21 23:59:50 +02:00
}
function checkModifiers(
event: KeyboardEvent,
optionalModifiers: Modifier[],
activeModifiers: string[]
): boolean {
2021-04-21 23:59:50 +02:00
return modifiers.reduce(
(matches: boolean, modifier: string, currentIndex: number): boolean =>
matches &&
(optionalModifiers.includes(modifier as Modifier) ||
event.getModifierState(platformModifiers[currentIndex]) ===
activeModifiers.includes(modifier)),
2021-04-21 23:59:50 +02:00
true
);
}
function check(
event: KeyboardEvent,
optionalModifiers: Modifier[],
modifiersAndKey: string[]
): boolean {
2021-04-21 23:59:50 +02:00
return (
checkKey(event, modifiersAndKey[modifiersAndKey.length - 1]) &&
checkModifiers(event, optionalModifiers, modifiersAndKey.slice(0, -1))
2021-04-21 23:59:50 +02:00
);
}
function innerShortcut(
lastEvent: KeyboardEvent,
callback: (event: KeyboardEvent) => void,
optionalModifiers: Modifier[],
...keyCombination: string[][]
2021-04-21 23:59:50 +02:00
): void {
2021-04-22 15:24:27 +02:00
let interval: number;
if (keyCombination.length === 0) {
2021-04-21 23:59:50 +02:00
callback(lastEvent);
} else {
const [nextKey, ...restKeys] = keyCombination;
2021-04-21 23:59:50 +02:00
const handler = (event: KeyboardEvent): void => {
if (check(event, optionalModifiers, nextKey)) {
innerShortcut(event, callback, optionalModifiers, ...restKeys);
2021-04-22 15:24:27 +02:00
clearTimeout(interval);
} else if (event.location === 0) {
// Any non-modifier key will cancel the shortcut sequence
document.removeEventListener("keydown", handler);
2021-04-21 23:59:50 +02:00
}
};
document.addEventListener("keydown", handler, { once: true });
}
}
export function registerShortcut(
2021-04-21 23:59:50 +02:00
callback: (event: KeyboardEvent) => void,
keyCombination: string[][],
optionalModifiers: Modifier[] = []
2021-04-21 23:59:50 +02:00
): () => void {
const [firstKey, ...restKeys] = keyCombination;
2021-04-21 23:59:50 +02:00
const handler = (event: KeyboardEvent): void => {
if (check(event, optionalModifiers, firstKey)) {
innerShortcut(event, callback, optionalModifiers, ...restKeys);
2021-04-21 23:59:50 +02:00
}
};
document.addEventListener("keydown", handler);
return (): void => document.removeEventListener("keydown", handler);
}