From 5197e3b7795e4f72bf56f220c3d5dc4f0b9ca7ad Mon Sep 17 00:00:00 2001 From: Henrik Giesel Date: Thu, 22 Apr 2021 17:52:27 +0200 Subject: [PATCH] Prefer optionalModifiers over register multiple shortcuts --- .../{WithShortcuts.d.ts => WithShortcut.d.ts} | 5 ++- ...thShortcuts.svelte => WithShortcut.svelte} | 21 +++++----- ts/editor-toolbar/dynamicComponents.ts | 8 ++-- ts/editor/cloze.ts | 19 +++++----- ts/editor/color.ts | 10 ++--- ts/editor/formatInline.ts | 26 ++++++------- ts/editor/notetype.ts | 6 +-- ts/editor/template.ts | 38 +++++++++---------- ts/lib/shortcuts.ts | 2 +- 9 files changed, 70 insertions(+), 65 deletions(-) rename ts/editor-toolbar/{WithShortcuts.d.ts => WithShortcut.d.ts} (68%) rename ts/editor-toolbar/{WithShortcuts.svelte => WithShortcut.svelte} (61%) diff --git a/ts/editor-toolbar/WithShortcuts.d.ts b/ts/editor-toolbar/WithShortcut.d.ts similarity index 68% rename from ts/editor-toolbar/WithShortcuts.d.ts rename to ts/editor-toolbar/WithShortcut.d.ts index 2b8a19a86..ec8b54535 100644 --- a/ts/editor-toolbar/WithShortcuts.d.ts +++ b/ts/editor-toolbar/WithShortcut.d.ts @@ -2,7 +2,8 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import type { ToolbarItem } from "./types"; -export interface WithShortcutsProps { +export interface WithShortcutProps { button: ToolbarItem; - shortcuts: string[]; + shortcut: string; + optionalModifiers: string[]; } diff --git a/ts/editor-toolbar/WithShortcuts.svelte b/ts/editor-toolbar/WithShortcut.svelte similarity index 61% rename from ts/editor-toolbar/WithShortcuts.svelte rename to ts/editor-toolbar/WithShortcut.svelte index 09e15aba8..8295c4fae 100644 --- a/ts/editor-toolbar/WithShortcuts.svelte +++ b/ts/editor-toolbar/WithShortcut.svelte @@ -7,13 +7,14 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import type { ToolbarItem } from "./types"; import { onDestroy } from "svelte"; - import { registerShortcut, getPlatformString } from "anki/shortcuts"; + import { Modifier, registerShortcut, getPlatformString } from "anki/shortcuts"; export let button: ToolbarItem; - export let shortcuts: string[]; + export let shortcut: string; + export let optionalModifiers: Modifier[]; function extend({ ...rest }: DynamicSvelteComponent): DynamicSvelteComponent { - const shortcutLabel = getPlatformString(shortcuts[0]); + const shortcutLabel = getPlatformString(shortcut); return { shortcutLabel, @@ -21,19 +22,21 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html }; } - let deregisters: (() => void)[]; + let deregister: () => void; function createShortcut({ detail }: CustomEvent): void { const mounted: HTMLButtonElement = detail.button; - deregisters = shortcuts.map((shortcut: string): (() => void) => - registerShortcut((event) => { - mounted.dispatchEvent(new MouseEvent("click")); + deregister = registerShortcut( + (event: KeyboardEvent) => { + mounted.dispatchEvent(new KeyboardEvent("click", event)); event.preventDefault(); - }, shortcut) + }, + shortcut, + optionalModifiers ); } - onDestroy(() => deregisters.forEach((dereg: () => void): void => dereg())); + onDestroy(() => deregister()); (WithDropdownMenu); -export const withShortcuts = dynamicComponent( - WithShortcuts +export const withShortcut = dynamicComponent( + WithShortcut ); diff --git a/ts/editor/cloze.ts b/ts/editor/cloze.ts index 8434edcd6..32856f297 100644 --- a/ts/editor/cloze.ts +++ b/ts/editor/cloze.ts @@ -1,11 +1,11 @@ // Copyright: Ankitects Pty Ltd and contributors // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html -import type WithShortcuts from "editor-toolbar/WithShortcuts.svelte"; -import type { WithShortcutsProps } from "editor-toolbar/WithShortcuts"; +import type WithShortcut from "editor-toolbar/WithShortcut.svelte"; +import type { WithShortcutProps } from "editor-toolbar/WithShortcut"; import type { DynamicSvelteComponent } from "sveltelib/dynamicComponent"; import * as tr from "anki/i18n"; -import { iconButton, withShortcuts } from "editor-toolbar/dynamicComponents"; +import { iconButton, withShortcut } from "editor-toolbar/dynamicComponents"; import bracketsIcon from "./code-brackets.svg"; @@ -35,16 +35,17 @@ function getCurrentHighestCloze(increment: boolean): number { return Math.max(1, highest); } -function onCloze(event: MouseEvent): void { - const highestCloze = getCurrentHighestCloze(!event.altKey); +function onCloze(event: KeyboardEvent | MouseEvent): void { + const highestCloze = getCurrentHighestCloze(!event.getModifierState("Alt")); wrap(`{{c${highestCloze}::`, "}}"); } -export function getClozeButton(): DynamicSvelteComponent & - WithShortcutsProps { - return withShortcuts({ +export function getClozeButton(): DynamicSvelteComponent & + WithShortcutProps { + return withShortcut({ id: "cloze", - shortcuts: ["Control+Shift+KeyC", "Control+Alt+Shift+KeyC"], + shortcut: "Control+Shift+KeyC", + optionalModifiers: ["Alt"], button: iconButton({ icon: bracketsIcon, onClick: onCloze, diff --git a/ts/editor/color.ts b/ts/editor/color.ts index b41345b06..8278047aa 100644 --- a/ts/editor/color.ts +++ b/ts/editor/color.ts @@ -8,7 +8,7 @@ import { iconButton, colorPicker, buttonGroup, - withShortcuts, + withShortcut, } from "editor-toolbar/dynamicComponents"; import * as tr from "anki/i18n"; @@ -31,8 +31,8 @@ function wrapWithForecolor(color: string): void { export function getColorGroup(): DynamicSvelteComponent & ButtonGroupProps { - const forecolorButton = withShortcuts({ - shortcuts: ["F7"], + const forecolorButton = withShortcut({ + shortcut: "F7", button: iconButton({ icon: squareFillIcon, className: "forecolor", @@ -41,8 +41,8 @@ export function getColorGroup(): DynamicSvelteComponent & }), }); - const colorpickerButton = withShortcuts({ - shortcuts: ["F8"], + const colorpickerButton = withShortcut({ + shortcut: "F8", button: colorPicker({ onChange: ({ currentTarget }) => setForegroundColor((currentTarget as HTMLInputElement).value), diff --git a/ts/editor/formatInline.ts b/ts/editor/formatInline.ts index 4dd131e42..af565365e 100644 --- a/ts/editor/formatInline.ts +++ b/ts/editor/formatInline.ts @@ -9,7 +9,7 @@ import { commandIconButton, iconButton, buttonGroup, - withShortcuts, + withShortcut, } from "editor-toolbar/dynamicComponents"; import boldIcon from "./type-bold.svg"; @@ -21,8 +21,8 @@ import eraserIcon from "./eraser.svg"; export function getFormatInlineGroup(): DynamicSvelteComponent & ButtonGroupProps { - const boldButton = withShortcuts({ - shortcuts: ["Control+KeyB"], + const boldButton = withShortcut({ + shortcut: "Control+KeyB", button: commandIconButton({ icon: boldIcon, tooltip: tr.editingBoldText(), @@ -30,8 +30,8 @@ export function getFormatInlineGroup(): DynamicSvelteComponent & @@ -21,8 +21,8 @@ export function getNotetypeGroup(): DynamicSvelteComponent & tooltip: tr.editingCustomizeFields(), }); - const cardsButton = withShortcuts({ - shortcuts: ["Control+KeyL"], + const cardsButton = withShortcut({ + shortcut: "Control+KeyL", button: labelButton({ onClick: () => bridgeCommand("cards"), disables: false, diff --git a/ts/editor/template.ts b/ts/editor/template.ts index d1189dd76..35d77322f 100644 --- a/ts/editor/template.ts +++ b/ts/editor/template.ts @@ -13,7 +13,7 @@ import { dropdownMenu, dropdownItem, buttonGroup, - withShortcuts, + withShortcut, } from "editor-toolbar/dynamicComponents"; import * as tr from "anki/i18n"; @@ -42,8 +42,8 @@ const mathjaxMenuId = "mathjaxMenu"; export function getTemplateGroup(): DynamicSvelteComponent & ButtonGroupProps { - const attachmentButton = withShortcuts({ - shortcuts: ["F3"], + const attachmentButton = withShortcut({ + shortcut: "F3", button: iconButton({ icon: paperclipIcon, onClick: onAttachment, @@ -51,8 +51,8 @@ export function getTemplateGroup(): DynamicSvelteComponent & }), }); - const recordButton = withShortcuts({ - shortcuts: ["F5"], + const recordButton = withShortcut({ + shortcut: "F5", button: iconButton({ icon: micIcon, onClick: onRecord, @@ -69,8 +69,8 @@ export function getTemplateGroup(): DynamicSvelteComponent & menuId: mathjaxMenuId, }); - const htmlButton = withShortcuts({ - shortcuts: ["Control+Shift+KeyX"], + const htmlButton = withShortcut({ + shortcut: "Control+Shift+KeyX", button: iconButton({ icon: xmlIcon, onClick: onHtmlEdit, @@ -93,22 +93,22 @@ export function getTemplateGroup(): DynamicSvelteComponent & export function getTemplateMenus(): (DynamicSvelteComponent & DropdownMenuProps)[] { const mathjaxMenuItems = [ - withShortcuts({ - shortcuts: ["Control+KeyM, KeyM"], + withShortcut({ + shortcut: "Control+KeyM, KeyM", button: dropdownItem({ onClick: () => wrap("\\(", "\\)"), label: tr.editingMathjaxInline(), }), }), - withShortcuts({ - shortcuts: ["Control+KeyM, KeyE"], + withShortcut({ + shortcut: "Control+KeyM, KeyE", button: dropdownItem({ onClick: () => wrap("\\[", "\\]"), label: tr.editingMathjaxBlock(), }), }), - withShortcuts({ - shortcuts: ["Control+KeyM, KeyC"], + withShortcut({ + shortcut: "Control+KeyM, KeyC", button: dropdownItem({ onClick: () => wrap("\\(\\ce{", "}\\)"), label: tr.editingMathjaxChemistry(), @@ -117,22 +117,22 @@ export function getTemplateMenus(): (DynamicSvelteComponent ]; const latexMenuItems = [ - withShortcuts({ - shortcuts: ["Control+KeyT, KeyT"], + withShortcut({ + shortcut: "Control+KeyT, KeyT", button: dropdownItem({ onClick: () => wrap("[latex]", "[/latex]"), label: tr.editingLatex(), }), }), - withShortcuts({ - shortcuts: ["Control+KeyT, KeyE"], + withShortcut({ + shortcut: "Control+KeyT, KeyE", button: dropdownItem({ onClick: () => wrap("[$]", "[/$]"), label: tr.editingLatexEquation(), }), }), - withShortcuts({ - shortcuts: ["Control+KeyT, KeyM"], + withShortcut({ + shortcut: "Control+KeyT, KeyM", button: dropdownItem({ onClick: () => wrap("[$$]", "[/$$]"), label: tr.editingLatexMathEnv(), diff --git a/ts/lib/shortcuts.ts b/ts/lib/shortcuts.ts index bf3d7e191..0a7312e12 100644 --- a/ts/lib/shortcuts.ts +++ b/ts/lib/shortcuts.ts @@ -2,7 +2,7 @@ // License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html import * as tr from "./i18n"; -type Modifier = "Control" | "Alt" | "Shift" | "Meta"; +export type Modifier = "Control" | "Alt" | "Shift" | "Meta"; const modifiers: Modifier[] = ["Control", "Alt", "Shift", "Meta"];