Allow using keyup with registerShortcut (#1755)

This commit is contained in:
Henrik Giesel 2022-03-29 01:28:44 +02:00 committed by GitHub
parent f1488b5983
commit 7977a0dc1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 25 deletions

View File

@ -9,13 +9,18 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import { registerShortcut } from "../lib/shortcuts";
export let keyCombination: string;
export let event: "keydown" | "keyup" | undefined = undefined;
const dispatch = createEventDispatcher();
onMount(() =>
registerShortcut((event: KeyboardEvent) => {
preventDefault(event);
dispatch("action", { originalEvent: event });
}, keyCombination),
registerShortcut(
(event: KeyboardEvent) => {
preventDefault(event);
dispatch("action", { originalEvent: event });
},
keyCombination,
{ event },
),
);
</script>

View File

@ -126,7 +126,7 @@ if (isApplePlatform()) {
export function preventBuiltinShortcuts(editable: HTMLElement): void {
for (const keyCombination of ["Control+B", "Control+U", "Control+I"]) {
registerShortcut(preventDefault, keyCombination, editable);
registerShortcut(preventDefault, keyCombination, { target: editable });
}
}

View File

@ -22,8 +22,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
off = !off;
}
function shortcut(element: HTMLElement): void {
registerShortcut(toggle, keyCombination, element);
function shortcut(target: HTMLElement): () => void {
return registerShortcut(toggle, keyCombination, { target });
}
onMount(() => editorField.element.then(shortcut));

View File

@ -27,8 +27,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
});
}
function shortcut(element: HTMLElement): void {
registerShortcut(toggle, keyCombination, element);
function shortcut(target: HTMLElement): () => void {
return registerShortcut(toggle, keyCombination, { target });
}
onMount(() => editorField.element.then(shortcut));

View File

@ -63,4 +63,8 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
{@html ellipseIcon}
</IconButton>
<Shortcut {keyCombination} on:action={(event) => onCloze(event.detail.originalEvent)} />
<Shortcut
{keyCombination}
event="keyup"
on:action={(event) => onCloze(event.detail.originalEvent)}
/>

View File

@ -142,11 +142,28 @@ function innerShortcut(
}
}
export interface RegisterShortcutRestParams {
target: EventTarget;
/// There might be no good reason to use `keyup` other
/// than to circumvent Qt bugs
event: "keydown" | "keyup";
}
const defaultRegisterShortcutRestParams = {
target: document,
event: "keydown" as const,
};
export function registerShortcut(
callback: (event: KeyboardEvent) => void,
keyCombinationString: string,
target: EventTarget | Document = document,
restParams: Partial<RegisterShortcutRestParams> = defaultRegisterShortcutRestParams,
): () => void {
const {
target = defaultRegisterShortcutRestParams.target,
event = defaultRegisterShortcutRestParams.event,
} = restParams;
const [check, ...restChecks] =
splitKeyCombinationString(keyCombinationString).map(keyCombinationToCheck);
@ -156,7 +173,7 @@ export function registerShortcut(
}
}
return on(target, "keydown", handler);
return on(target, event, handler);
}
registerPackage("anki/shortcuts", {

View File

@ -1,26 +1,23 @@
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import type { RegisterShortcutRestParams } from "../lib/shortcuts";
import { registerShortcut } from "../lib/shortcuts";
interface ShortcutParams {
action: (event: KeyboardEvent) => void;
keyCombination: string;
params?: RegisterShortcutRestParams;
}
export function shortcut(
_node: Node,
{
action,
keyCombination,
target,
}: {
action: (event: KeyboardEvent) => void;
keyCombination: string;
target?: EventTarget;
},
{ action, keyCombination, params }: ShortcutParams,
): { destroy: () => void } {
const deregister = registerShortcut(action, keyCombination, target ?? document);
const deregister = registerShortcut(action, keyCombination, params);
return {
destroy() {
deregister();
},
destroy: deregister,
};
}