mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
Add getPlatformString for making shortcuts to platform string
This commit is contained in:
parent
2109b67caf
commit
e95e78da9c
3 changed files with 89 additions and 19 deletions
2
ftl/core/keyboard.ftl
Normal file
2
ftl/core/keyboard.ftl
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
keyboard-ctrl = Ctrl
|
||||||
|
keyboard-shift = Shift
|
|
@ -7,13 +7,23 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
import type { ToolbarItem } from "./types";
|
import type { ToolbarItem } from "./types";
|
||||||
|
|
||||||
import { onDestroy } from "svelte";
|
import { onDestroy } from "svelte";
|
||||||
import { registerShortcut } from "anki/shortcuts";
|
import { registerShortcut, getPlatformString } from "anki/shortcuts";
|
||||||
|
|
||||||
export let button: ToolbarItem;
|
export let button: ToolbarItem;
|
||||||
export let shortcuts: string[];
|
export let shortcuts: string[];
|
||||||
|
|
||||||
function extend({ ...rest }: DynamicSvelteComponent): DynamicSvelteComponent {
|
function extend({
|
||||||
|
tooltip,
|
||||||
|
...rest
|
||||||
|
}: DynamicSvelteComponent): DynamicSvelteComponent {
|
||||||
|
const platformShortcut = getPlatformString(shortcuts[0]);
|
||||||
|
|
||||||
|
if (tooltip) {
|
||||||
|
tooltip = `${tooltip} (${platformShortcut})`;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
tooltip,
|
||||||
...rest,
|
...rest,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,69 @@
|
||||||
|
import * as tr from "./i18n";
|
||||||
|
|
||||||
const modifiers = ["Control", "Alt", "Shift", "Meta"];
|
const modifiers = ["Control", "Alt", "Shift", "Meta"];
|
||||||
|
|
||||||
|
// how modifiers are mapped
|
||||||
const platformModifiers =
|
const platformModifiers =
|
||||||
navigator.platform === "MacIntel" ? ["Meta", "Alt", "Shift", "Control"] : modifiers;
|
navigator.platform === "MacIntel"
|
||||||
|
? ["Meta", "Alt", "Shift", "Control"]
|
||||||
|
: ["Control", "Alt", "Shift", "OS"];
|
||||||
|
|
||||||
|
function splitKeyCombinationString(keyCombinationString: string): string[][] {
|
||||||
|
return keyCombinationString.split(", ").map((segment) => segment.split("+"));
|
||||||
|
}
|
||||||
|
|
||||||
|
function modifiersToPlatformString(modifiers: string[]): string {
|
||||||
|
const displayModifiers =
|
||||||
|
navigator.platform === "MacIntel"
|
||||||
|
? ["^", "⌥", "⇧", "⌘"]
|
||||||
|
: [`${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 {
|
||||||
|
return key.startsWith(alphabeticPrefix)
|
||||||
|
? key.slice(alphabeticPrefix.length)
|
||||||
|
: key.startsWith(numericPrefix)
|
||||||
|
? key.slice(numericPrefix.length)
|
||||||
|
: Object.prototype.hasOwnProperty.call(keyToCharacterMap, key)
|
||||||
|
? keyToCharacterMap[key]
|
||||||
|
: key;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toPlatformString(modifiersAndKey: string[]) {
|
||||||
|
return `${modifiersToPlatformString(
|
||||||
|
modifiersAndKey.slice(0, -1)
|
||||||
|
)}${keyToPlatformString(modifiersAndKey[modifiersAndKey.length - 1])}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPlatformString(keyCombinationString: string): string {
|
||||||
|
return splitKeyCombinationString(keyCombinationString)
|
||||||
|
.map(toPlatformString)
|
||||||
|
.join(", ");
|
||||||
|
}
|
||||||
|
|
||||||
function checkKey(event: KeyboardEvent, key: string): boolean {
|
function checkKey(event: KeyboardEvent, key: string): boolean {
|
||||||
return event.code === key;
|
return event.code === key;
|
||||||
|
@ -24,32 +86,28 @@ function check(event: KeyboardEvent, modifiersAndKey: string[]): boolean {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function normalizeShortcutString(shortcutString: string): string[][] {
|
|
||||||
return shortcutString.split(", ").map((segment) => segment.split("+"));
|
|
||||||
}
|
|
||||||
|
|
||||||
const shortcutTimeoutMs = 350;
|
const shortcutTimeoutMs = 350;
|
||||||
|
|
||||||
function innerShortcut(
|
function innerShortcut(
|
||||||
lastEvent: KeyboardEvent,
|
lastEvent: KeyboardEvent,
|
||||||
callback: (event: KeyboardEvent) => void,
|
callback: (event: KeyboardEvent) => void,
|
||||||
...shortcuts: string[][]
|
...keyCombination: string[][]
|
||||||
): void {
|
): void {
|
||||||
if (shortcuts.length === 0) {
|
if (keyCombination.length === 0) {
|
||||||
callback(lastEvent);
|
callback(lastEvent);
|
||||||
} else {
|
} else {
|
||||||
const [nextShortcut, ...restShortcuts] = shortcuts;
|
const [nextKey, ...restKeys] = keyCombination;
|
||||||
|
|
||||||
let ivl: number;
|
let ivl: number;
|
||||||
|
|
||||||
const handler = (event: KeyboardEvent): void => {
|
const handler = (event: KeyboardEvent): void => {
|
||||||
if (check(event, nextShortcut)) {
|
if (check(event, nextKey)) {
|
||||||
innerShortcut(event, callback, ...restShortcuts);
|
innerShortcut(event, callback, ...restKeys);
|
||||||
clearInterval(ivl);
|
clearTimeout(ivl);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ivl = setInterval(
|
ivl = setTimeout(
|
||||||
(): void => document.removeEventListener("keydown", handler),
|
(): void => document.removeEventListener("keydown", handler),
|
||||||
shortcutTimeoutMs
|
shortcutTimeoutMs
|
||||||
);
|
);
|
||||||
|
@ -60,14 +118,14 @@ function innerShortcut(
|
||||||
|
|
||||||
export function registerShortcut(
|
export function registerShortcut(
|
||||||
callback: (event: KeyboardEvent) => void,
|
callback: (event: KeyboardEvent) => void,
|
||||||
shortcutString: string
|
keyCombinationString: string
|
||||||
): () => void {
|
): () => void {
|
||||||
const shortcuts = normalizeShortcutString(shortcutString);
|
const keyCombination = splitKeyCombinationString(keyCombinationString);
|
||||||
const [firstShortcut, ...restShortcuts] = shortcuts;
|
const [firstKey, ...restKeys] = keyCombination;
|
||||||
|
|
||||||
const handler = (event: KeyboardEvent): void => {
|
const handler = (event: KeyboardEvent): void => {
|
||||||
if (check(event, firstShortcut)) {
|
if (check(event, firstKey)) {
|
||||||
innerShortcut(event, callback, ...restShortcuts);
|
innerShortcut(event, callback, ...restKeys);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue