mirror of
https://github.com/ankitects/anki.git
synced 2025-09-21 15:32:23 -04:00
Create lib/keys.ts
This commit is contained in:
parent
5c2911c053
commit
4601ebb347
2 changed files with 59 additions and 50 deletions
|
@ -0,0 +1,56 @@
|
||||||
|
// 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";
|
||||||
|
import { isApplePlatform } from "./platform";
|
||||||
|
|
||||||
|
export type Modifier = "Control" | "Alt" | "Shift" | "Meta";
|
||||||
|
|
||||||
|
// how modifiers are mapped
|
||||||
|
const allModifiers: Modifier[] = ["Control", "Alt", "Shift", "Meta"];
|
||||||
|
|
||||||
|
const platformModifiers = isApplePlatform()
|
||||||
|
? ["Meta", "Alt", "Shift", "Control"]
|
||||||
|
: ["Control", "Alt", "Shift", "OS"];
|
||||||
|
|
||||||
|
|
||||||
|
export const checkModifiers =
|
||||||
|
(required: Modifier[], optional: Modifier[] = []) =>
|
||||||
|
(event: KeyboardEvent): boolean => {
|
||||||
|
return allModifiers.reduce(
|
||||||
|
(
|
||||||
|
matches: boolean,
|
||||||
|
currentModifier: Modifier,
|
||||||
|
currentIndex: number
|
||||||
|
): boolean =>
|
||||||
|
matches &&
|
||||||
|
(optional.includes(currentModifier as Modifier) ||
|
||||||
|
event.getModifierState(platformModifiers[currentIndex]) ===
|
||||||
|
required.includes(currentModifier)),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const hasModifier = (modifier: Modifier) => (event: KeyboardEvent): boolean => event.getModifierState(platformModifiers[allModifiers.indexOf(modifier)]);
|
||||||
|
|
||||||
|
export function isControl(key: string): boolean {
|
||||||
|
return key === platformModifiers[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isShift(key: string): boolean {
|
||||||
|
return key === platformModifiers[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
export 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;
|
||||||
|
}
|
|
@ -1,38 +1,10 @@
|
||||||
// Copyright: Ankitects Pty Ltd and contributors
|
// Copyright: Ankitects Pty Ltd and contributors
|
||||||
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
||||||
|
|
||||||
import * as tr from "./i18n";
|
import type { Modifier } from "./keys";
|
||||||
import { isApplePlatform } from "./platform";
|
|
||||||
import { registerPackage } from "./register-package";
|
import { registerPackage } from "./register-package";
|
||||||
|
import { modifiersToPlatformString, checkModifiers } from "./keys";
|
||||||
export type Modifier = "Control" | "Alt" | "Shift" | "Meta";
|
|
||||||
|
|
||||||
// how modifiers are mapped
|
|
||||||
const platformModifiers = isApplePlatform()
|
|
||||||
? ["Meta", "Alt", "Shift", "Control"]
|
|
||||||
: ["Control", "Alt", "Shift", "OS"];
|
|
||||||
|
|
||||||
export function isControl(key: string): boolean {
|
|
||||||
return key === platformModifiers[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isShift(key: string): boolean {
|
|
||||||
return key === platformModifiers[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
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 keyCodeLookup = {
|
const keyCodeLookup = {
|
||||||
Backspace: 8,
|
Backspace: 8,
|
||||||
|
@ -90,25 +62,6 @@ function checkKey(event: KeyboardEvent, key: number): boolean {
|
||||||
return event.which === key;
|
return event.which === key;
|
||||||
}
|
}
|
||||||
|
|
||||||
const allModifiers: Modifier[] = ["Control", "Alt", "Shift", "Meta"];
|
|
||||||
|
|
||||||
const checkModifiers =
|
|
||||||
(required: Modifier[], optional: Modifier[] = []) =>
|
|
||||||
(event: KeyboardEvent): boolean => {
|
|
||||||
return allModifiers.reduce(
|
|
||||||
(
|
|
||||||
matches: boolean,
|
|
||||||
currentModifier: Modifier,
|
|
||||||
currentIndex: number
|
|
||||||
): boolean =>
|
|
||||||
matches &&
|
|
||||||
(optional.includes(currentModifier as Modifier) ||
|
|
||||||
event.getModifierState(platformModifiers[currentIndex]) ===
|
|
||||||
required.includes(currentModifier)),
|
|
||||||
true
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
function partition<T>(predicate: (t: T) => boolean, items: T[]): [T[], T[]] {
|
function partition<T>(predicate: (t: T) => boolean, items: T[]): [T[], T[]] {
|
||||||
const trueItems: T[] = [];
|
const trueItems: T[] = [];
|
||||||
const falseItems: T[] = [];
|
const falseItems: T[] = [];
|
||||||
|
|
Loading…
Reference in a new issue