From 4601ebb347da917f2ca56e260dc68a840bb916e1 Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Tue, 29 Jun 2021 17:15:07 +0200 Subject: [PATCH] Create lib/keys.ts --- ts/lib/keys.ts | 56 +++++++++++++++++++++++++++++++++++++++++++++ ts/lib/shortcuts.ts | 53 +++--------------------------------------- 2 files changed, 59 insertions(+), 50 deletions(-) diff --git a/ts/lib/keys.ts b/ts/lib/keys.ts index e69de29bb..9e1683317 100644 --- a/ts/lib/keys.ts +++ b/ts/lib/keys.ts @@ -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; +} diff --git a/ts/lib/shortcuts.ts b/ts/lib/shortcuts.ts index 02603da28..aedadf253 100644 --- a/ts/lib/shortcuts.ts +++ b/ts/lib/shortcuts.ts @@ -1,38 +1,10 @@ // 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"; +import type { Modifier } from "./keys"; + import { registerPackage } from "./register-package"; - -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; -} +import { modifiersToPlatformString, checkModifiers } from "./keys"; const keyCodeLookup = { Backspace: 8, @@ -90,25 +62,6 @@ function checkKey(event: KeyboardEvent, key: number): boolean { 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(predicate: (t: T) => boolean, items: T[]): [T[], T[]] { const trueItems: T[] = []; const falseItems: T[] = [];