Allow using keyup with registerShortcut (#1755)
This commit is contained in:
parent
f1488b5983
commit
7977a0dc1f
@ -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) => {
|
||||
registerShortcut(
|
||||
(event: KeyboardEvent) => {
|
||||
preventDefault(event);
|
||||
dispatch("action", { originalEvent: event });
|
||||
}, keyCombination),
|
||||
},
|
||||
keyCombination,
|
||||
{ event },
|
||||
),
|
||||
);
|
||||
</script>
|
||||
|
@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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));
|
||||
|
@ -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));
|
||||
|
@ -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)}
|
||||
/>
|
||||
|
@ -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", {
|
||||
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user