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";
|
import { registerShortcut } from "../lib/shortcuts";
|
||||||
|
|
||||||
export let keyCombination: string;
|
export let keyCombination: string;
|
||||||
|
export let event: "keydown" | "keyup" | undefined = undefined;
|
||||||
|
|
||||||
const dispatch = createEventDispatcher();
|
const dispatch = createEventDispatcher();
|
||||||
|
|
||||||
onMount(() =>
|
onMount(() =>
|
||||||
registerShortcut((event: KeyboardEvent) => {
|
registerShortcut(
|
||||||
|
(event: KeyboardEvent) => {
|
||||||
preventDefault(event);
|
preventDefault(event);
|
||||||
dispatch("action", { originalEvent: event });
|
dispatch("action", { originalEvent: event });
|
||||||
}, keyCombination),
|
},
|
||||||
|
keyCombination,
|
||||||
|
{ event },
|
||||||
|
),
|
||||||
);
|
);
|
||||||
</script>
|
</script>
|
||||||
|
@ -126,7 +126,7 @@ if (isApplePlatform()) {
|
|||||||
|
|
||||||
export function preventBuiltinShortcuts(editable: HTMLElement): void {
|
export function preventBuiltinShortcuts(editable: HTMLElement): void {
|
||||||
for (const keyCombination of ["Control+B", "Control+U", "Control+I"]) {
|
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;
|
off = !off;
|
||||||
}
|
}
|
||||||
|
|
||||||
function shortcut(element: HTMLElement): void {
|
function shortcut(target: HTMLElement): () => void {
|
||||||
registerShortcut(toggle, keyCombination, element);
|
return registerShortcut(toggle, keyCombination, { target });
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => editorField.element.then(shortcut));
|
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 {
|
function shortcut(target: HTMLElement): () => void {
|
||||||
registerShortcut(toggle, keyCombination, element);
|
return registerShortcut(toggle, keyCombination, { target });
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => editorField.element.then(shortcut));
|
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}
|
{@html ellipseIcon}
|
||||||
</IconButton>
|
</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(
|
export function registerShortcut(
|
||||||
callback: (event: KeyboardEvent) => void,
|
callback: (event: KeyboardEvent) => void,
|
||||||
keyCombinationString: string,
|
keyCombinationString: string,
|
||||||
target: EventTarget | Document = document,
|
restParams: Partial<RegisterShortcutRestParams> = defaultRegisterShortcutRestParams,
|
||||||
): () => void {
|
): () => void {
|
||||||
|
const {
|
||||||
|
target = defaultRegisterShortcutRestParams.target,
|
||||||
|
event = defaultRegisterShortcutRestParams.event,
|
||||||
|
} = restParams;
|
||||||
|
|
||||||
const [check, ...restChecks] =
|
const [check, ...restChecks] =
|
||||||
splitKeyCombinationString(keyCombinationString).map(keyCombinationToCheck);
|
splitKeyCombinationString(keyCombinationString).map(keyCombinationToCheck);
|
||||||
|
|
||||||
@ -156,7 +173,7 @@ export function registerShortcut(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return on(target, "keydown", handler);
|
return on(target, event, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
registerPackage("anki/shortcuts", {
|
registerPackage("anki/shortcuts", {
|
||||||
|
@ -1,26 +1,23 @@
|
|||||||
// 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 type { RegisterShortcutRestParams } from "../lib/shortcuts";
|
||||||
import { registerShortcut } from "../lib/shortcuts";
|
import { registerShortcut } from "../lib/shortcuts";
|
||||||
|
|
||||||
|
interface ShortcutParams {
|
||||||
|
action: (event: KeyboardEvent) => void;
|
||||||
|
keyCombination: string;
|
||||||
|
params?: RegisterShortcutRestParams;
|
||||||
|
}
|
||||||
|
|
||||||
export function shortcut(
|
export function shortcut(
|
||||||
_node: Node,
|
_node: Node,
|
||||||
{
|
{ action, keyCombination, params }: ShortcutParams,
|
||||||
action,
|
|
||||||
keyCombination,
|
|
||||||
target,
|
|
||||||
}: {
|
|
||||||
action: (event: KeyboardEvent) => void;
|
|
||||||
keyCombination: string;
|
|
||||||
target?: EventTarget;
|
|
||||||
},
|
|
||||||
): { destroy: () => void } {
|
): { destroy: () => void } {
|
||||||
const deregister = registerShortcut(action, keyCombination, target ?? document);
|
const deregister = registerShortcut(action, keyCombination, params);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
destroy() {
|
destroy: deregister,
|
||||||
deregister();
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user