@alulore/kumiko 0.16.0 → 0.17.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/media-viewer/video-controls.js +11 -1
- package/dist/shortcuts/bindings.d.ts +50 -0
- package/dist/shortcuts/bindings.js +71 -0
- package/dist/shortcuts/capture.d.ts +7 -0
- package/dist/shortcuts/capture.js +92 -0
- package/dist/shortcuts/guard.d.ts +9 -0
- package/dist/shortcuts/guard.js +48 -0
- package/dist/shortcuts/help.d.ts +29 -0
- package/dist/shortcuts/help.js +26 -0
- package/dist/shortcuts/hints.d.ts +19 -0
- package/dist/shortcuts/hints.js +33 -0
- package/dist/shortcuts/index.d.ts +12 -0
- package/dist/shortcuts/index.js +12 -0
- package/dist/shortcuts/judge.d.ts +42 -0
- package/dist/shortcuts/judge.js +34 -0
- package/dist/shortcuts/keys.d.ts +11 -0
- package/dist/shortcuts/keys.js +89 -0
- package/dist/shortcuts/list.d.ts +16 -0
- package/dist/shortcuts/list.js +48 -0
- package/dist/shortcuts/notes.d.ts +28 -0
- package/dist/shortcuts/notes.js +27 -0
- package/dist/shortcuts/over-dialogs.d.ts +18 -0
- package/dist/shortcuts/over-dialogs.js +38 -0
- package/dist/shortcuts/provider.d.ts +98 -0
- package/dist/shortcuts/provider.js +116 -0
- package/dist/shortcuts/rebinder.d.ts +23 -0
- package/dist/shortcuts/rebinder.js +84 -0
- package/dist/shortcuts/registration.d.ts +19 -0
- package/dist/shortcuts/registration.js +89 -0
- package/dist/shortcuts/shortcuts.css +171 -0
- package/dist/stored/index.d.ts +4 -0
- package/dist/stored/index.js +4 -0
- package/dist/stored/storage.d.ts +22 -0
- package/dist/stored/storage.js +10 -0
- package/dist/stored/stored.d.ts +93 -0
- package/dist/stored/stored.js +209 -0
- package/dist/stored/use-stored.d.ts +6 -0
- package/dist/stored/use-stored.js +12 -0
- package/dist/stored/validators.d.ts +6 -0
- package/dist/stored/validators.js +13 -0
- package/dist/undo/history.d.ts +62 -0
- package/dist/undo/history.js +151 -0
- package/dist/undo/index.d.ts +10 -0
- package/dist/undo/index.js +10 -0
- package/dist/undo/undo-adapter.d.ts +35 -0
- package/dist/undo/undo-adapter.js +35 -0
- package/dist/undo/undo-feedback.d.ts +28 -0
- package/dist/undo/undo-feedback.js +69 -0
- package/dist/undo/undo-keys.d.ts +32 -0
- package/dist/undo/undo-keys.js +76 -0
- package/dist/undo/undo-options.d.ts +51 -0
- package/dist/undo/undo-options.js +6 -0
- package/dist/undo/undo-panel.d.ts +23 -0
- package/dist/undo/undo-panel.js +56 -0
- package/dist/undo/undo-provider.d.ts +92 -0
- package/dist/undo/undo-provider.js +104 -0
- package/dist/undo/undo-reveal.d.ts +9 -0
- package/dist/undo/undo-reveal.js +45 -0
- package/dist/undo/undo-store.d.ts +89 -0
- package/dist/undo/undo-store.js +243 -0
- package/dist/undo/undo.css +135 -0
- package/dist/undo/use-undo-value.d.ts +43 -0
- package/dist/undo/use-undo-value.js +85 -0
- package/package.json +24 -1
|
@@ -36,12 +36,22 @@ function clock(seconds) {
|
|
|
36
36
|
const whole = Math.max(0, Math.floor(seconds));
|
|
37
37
|
return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, "0")}`;
|
|
38
38
|
}
|
|
39
|
+
/*
|
|
40
|
+
* A play() that can't finish (the video left the page, or the browser needs a tap first) leaves
|
|
41
|
+
* the clip paused, which is what the controls already show. Nothing else needs to hear of it.
|
|
42
|
+
*/
|
|
43
|
+
function keepPaused(error) {
|
|
44
|
+
if (!(error instanceof DOMException) ||
|
|
45
|
+
!["AbortError", "NotAllowedError"].includes(error.name)) {
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
39
49
|
function togglePlay(video) {
|
|
40
50
|
if (!video) {
|
|
41
51
|
return;
|
|
42
52
|
}
|
|
43
53
|
if (video.paused) {
|
|
44
|
-
|
|
54
|
+
video.play().catch(keepPaused);
|
|
45
55
|
}
|
|
46
56
|
else {
|
|
47
57
|
video.pause();
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** What an app declares: an action a person can run, and the keys that run it unless they rebind it. */
|
|
2
|
+
export interface ShortcutAction {
|
|
3
|
+
/** A stable id, the name rebinding is saved under. */
|
|
4
|
+
id: string;
|
|
5
|
+
/** What it does, shown in the help sheet. */
|
|
6
|
+
description: string;
|
|
7
|
+
/** The heading it sits under in the help sheet. */
|
|
8
|
+
section: string;
|
|
9
|
+
/** The keys that run it by default: one, or several. Written like `"mod+k"`. */
|
|
10
|
+
keys: string | readonly string[];
|
|
11
|
+
/** Runs when one of its keys is pressed, or when a control calls it. */
|
|
12
|
+
run: (event?: KeyboardEvent) => void;
|
|
13
|
+
/** Checked on every key press: the action runs only while it returns true. */
|
|
14
|
+
when?: () => boolean;
|
|
15
|
+
/** Whether it also runs while focus is in a text field. Keys with Ctrl, Cmd or Alt always do. */
|
|
16
|
+
allowInInput?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Whether it keeps running while a modal (a dialog or a sheet) is open. For app-wide chords such as a
|
|
19
|
+
* finder or settings. Typing in a field, `when` and the pause during a drag or menu still apply.
|
|
20
|
+
* @defaultValue false
|
|
21
|
+
*/
|
|
22
|
+
overDialogs?: boolean;
|
|
23
|
+
}
|
|
24
|
+
/** The keys people changed, by action id. An action that is not in here uses its defaults. */
|
|
25
|
+
export type Overrides = Readonly<Record<string, readonly string[]>>;
|
|
26
|
+
/** How many keys one action can hold. */
|
|
27
|
+
export declare const maxKeysPerAction = 3;
|
|
28
|
+
export declare function defaultKeys(action: ShortcutAction): string[];
|
|
29
|
+
export declare function keysOf(action: ShortcutAction, overrides: Overrides): string[];
|
|
30
|
+
/** Whether saved bindings have the right shape: an object of action ids to lists of key strings. */
|
|
31
|
+
export declare function isOverrides(value: unknown): value is Overrides;
|
|
32
|
+
/** Keeps what applies to these actions: known ids only, keys written one way, at most `maxKeysPerAction` each. */
|
|
33
|
+
export declare function sanitizeOverrides(saved: Overrides, actions: readonly ShortcutAction[]): Overrides;
|
|
34
|
+
/** Who has these keys now, other than `exceptId`. */
|
|
35
|
+
export declare function ownerOf(actions: readonly ShortcutAction[], overrides: Overrides, keys: string, exceptId: string): ShortcutAction | undefined;
|
|
36
|
+
export type Resolution = "plain" | "swap" | "replace";
|
|
37
|
+
export interface Change {
|
|
38
|
+
id: string;
|
|
39
|
+
/** Which of its keys changes. -1 adds a new one. */
|
|
40
|
+
slot: number;
|
|
41
|
+
keys: string;
|
|
42
|
+
how: Resolution;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Puts `keys` on one slot of an action. When another action has them, `swap` gives that action
|
|
46
|
+
* this slot's old key, and `replace` just takes them away from it.
|
|
47
|
+
*/
|
|
48
|
+
export declare function bindKeys(actions: readonly ShortcutAction[], overrides: Overrides, change: Change): Overrides;
|
|
49
|
+
export declare function unbindKey(actions: readonly ShortcutAction[], overrides: Overrides, id: string, slot: number): Overrides;
|
|
50
|
+
export declare function resetAction(overrides: Overrides, id: string): Overrides;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { canonicalKeys } from "./keys";
|
|
2
|
+
/** How many keys one action can hold. */
|
|
3
|
+
export const maxKeysPerAction = 3;
|
|
4
|
+
export function defaultKeys(action) {
|
|
5
|
+
const keys = typeof action.keys === "string" ? [action.keys] : [...action.keys];
|
|
6
|
+
return keys.map((key) => canonicalKeys(key));
|
|
7
|
+
}
|
|
8
|
+
export function keysOf(action, overrides) {
|
|
9
|
+
return [...(overrides[action.id] ?? defaultKeys(action))];
|
|
10
|
+
}
|
|
11
|
+
/** Whether saved bindings have the right shape: an object of action ids to lists of key strings. */
|
|
12
|
+
export function isOverrides(value) {
|
|
13
|
+
return (typeof value === "object" &&
|
|
14
|
+
value !== null &&
|
|
15
|
+
!Array.isArray(value) &&
|
|
16
|
+
Object.values(value).every((keys) => Array.isArray(keys) && keys.every((key) => typeof key === "string")));
|
|
17
|
+
}
|
|
18
|
+
/** Keeps what applies to these actions: known ids only, keys written one way, at most `maxKeysPerAction` each. */
|
|
19
|
+
export function sanitizeOverrides(saved, actions) {
|
|
20
|
+
const result = {};
|
|
21
|
+
for (const action of actions) {
|
|
22
|
+
const keys = Object.entries(saved).find(([id]) => id === action.id)?.[1];
|
|
23
|
+
if (keys) {
|
|
24
|
+
result[action.id] = keys.slice(0, maxKeysPerAction).map((key) => canonicalKeys(key));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
/** Who has these keys now, other than `exceptId`. */
|
|
30
|
+
export function ownerOf(actions, overrides, keys, exceptId) {
|
|
31
|
+
const wanted = canonicalKeys(keys);
|
|
32
|
+
return actions.find((action) => action.id !== exceptId && keysOf(action, overrides).includes(wanted));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Puts `keys` on one slot of an action. When another action has them, `swap` gives that action
|
|
36
|
+
* this slot's old key, and `replace` just takes them away from it.
|
|
37
|
+
*/
|
|
38
|
+
export function bindKeys(actions, overrides, change) {
|
|
39
|
+
const wanted = canonicalKeys(change.keys);
|
|
40
|
+
const mine = actions.find((action) => action.id === change.id);
|
|
41
|
+
if (!mine) {
|
|
42
|
+
return overrides;
|
|
43
|
+
}
|
|
44
|
+
const next = { ...overrides };
|
|
45
|
+
const own = keysOf(mine, overrides);
|
|
46
|
+
const previous = own[change.slot];
|
|
47
|
+
const owner = change.how === "plain" ? undefined : ownerOf(actions, overrides, wanted, change.id);
|
|
48
|
+
if (owner) {
|
|
49
|
+
const theirs = keysOf(owner, overrides);
|
|
50
|
+
const index = theirs.indexOf(wanted);
|
|
51
|
+
next[owner.id] =
|
|
52
|
+
change.how === "swap" && previous !== undefined
|
|
53
|
+
? theirs.map((key, at) => (at === index ? previous : key))
|
|
54
|
+
: theirs.filter((key) => key !== wanted);
|
|
55
|
+
}
|
|
56
|
+
next[change.id] =
|
|
57
|
+
change.slot < 0
|
|
58
|
+
? [...own, wanted]
|
|
59
|
+
: own.map((key, at) => (at === change.slot ? wanted : key));
|
|
60
|
+
return next;
|
|
61
|
+
}
|
|
62
|
+
export function unbindKey(actions, overrides, id, slot) {
|
|
63
|
+
const action = actions.find((item) => item.id === id);
|
|
64
|
+
if (!action) {
|
|
65
|
+
return overrides;
|
|
66
|
+
}
|
|
67
|
+
return { ...overrides, [id]: keysOf(action, overrides).filter((_, index) => index !== slot) };
|
|
68
|
+
}
|
|
69
|
+
export function resetAction(overrides, id) {
|
|
70
|
+
return Object.fromEntries(Object.entries(overrides).filter(([key]) => key !== id));
|
|
71
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Rebinder } from "./rebinder";
|
|
2
|
+
/** What shows under an action while its key is being changed: the capture, and the answer to a conflict. */
|
|
3
|
+
export declare function Capture({ rebinder, bare }: {
|
|
4
|
+
rebinder: Rebinder;
|
|
5
|
+
/** Shows only the questions and refusals, not the place to give keys. */
|
|
6
|
+
bare?: boolean;
|
|
7
|
+
}): React.JSX.Element;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect, useRef, useState } from "react";
|
|
3
|
+
import { canonicalKeys, describeKeys, isValidKeys, keysFromEvent } from "./keys";
|
|
4
|
+
import { useShortcuts } from "./provider";
|
|
5
|
+
/** Keys offered when capturing by picking from a list. */
|
|
6
|
+
const suggestions = [
|
|
7
|
+
"mod+j",
|
|
8
|
+
"mod+e",
|
|
9
|
+
"mod+d",
|
|
10
|
+
"mod+shift+n",
|
|
11
|
+
"mod+shift+k",
|
|
12
|
+
"mod+shift+d",
|
|
13
|
+
"mod+alt+k",
|
|
14
|
+
"mod+.",
|
|
15
|
+
"alt+n",
|
|
16
|
+
"alt+s",
|
|
17
|
+
"g",
|
|
18
|
+
"f"
|
|
19
|
+
];
|
|
20
|
+
function PressCapture({ rebinder }) {
|
|
21
|
+
const { submit, cancel } = rebinder;
|
|
22
|
+
const { options } = useShortcuts();
|
|
23
|
+
const escapeCancels = Object.keys(options.reservedKeys).some((reserved) => canonicalKeys(reserved) === "escape");
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
/* Captured before anything else hears it, so the keys being chosen never run what they are bound to. */
|
|
26
|
+
const onKey = (event) => {
|
|
27
|
+
if (event.key === "Escape" && escapeCancels) {
|
|
28
|
+
event.preventDefault();
|
|
29
|
+
event.stopPropagation();
|
|
30
|
+
cancel();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
const keys = keysFromEvent(event);
|
|
34
|
+
if (keys) {
|
|
35
|
+
event.preventDefault();
|
|
36
|
+
event.stopPropagation();
|
|
37
|
+
submit(keys);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
window.addEventListener("keydown", onKey, true);
|
|
41
|
+
return () => window.removeEventListener("keydown", onKey, true);
|
|
42
|
+
}, [submit, cancel, escapeCancels]);
|
|
43
|
+
return (_jsxs("p", { className: "kumiko-shortcuts-capture", "data-mode": "press", children: [escapeCancels ? "Press the new keys. Esc cancels." : "Press the new keys.", _jsx("button", { type: "button", onClick: cancel, children: "Cancel" })] }));
|
|
44
|
+
}
|
|
45
|
+
function TypeCapture({ rebinder }) {
|
|
46
|
+
const [text, setText] = useState("");
|
|
47
|
+
const input = useRef(null);
|
|
48
|
+
useEffect(() => input.current?.focus(), []);
|
|
49
|
+
return (_jsxs("form", { className: "kumiko-shortcuts-capture", "data-mode": "type", onSubmit: (event) => {
|
|
50
|
+
event.preventDefault();
|
|
51
|
+
if (isValidKeys(text)) {
|
|
52
|
+
rebinder.submit(text);
|
|
53
|
+
}
|
|
54
|
+
}, children: [_jsx("input", { "aria-label": "New keys, like mod+shift+k", placeholder: "mod+shift+k", ref: input, value: text, onChange: (event) => setText(event.target.value) }), _jsx("button", { type: "submit", disabled: !isValidKeys(text), children: "Use" }), _jsx("button", { type: "button", onClick: rebinder.cancel, children: "Cancel" })] }));
|
|
55
|
+
}
|
|
56
|
+
function ListCapture({ rebinder }) {
|
|
57
|
+
const [choice, setChoice] = useState("");
|
|
58
|
+
return (_jsxs("div", { className: "kumiko-shortcuts-capture", "data-mode": "list", children: [_jsxs("select", { "aria-label": "New keys", value: choice, onChange: (event) => setChoice(event.target.value), children: [_jsx("option", { value: "", children: "Pick keys\u2026" }), suggestions.map((keys) => (_jsx("option", { value: keys, children: describeKeys(keys) }, keys)))] }), _jsx("button", { type: "button", disabled: choice === "", onClick: () => rebinder.submit(choice), children: "Use" }), _jsx("button", { type: "button", onClick: rebinder.cancel, children: "Cancel" })] }));
|
|
59
|
+
}
|
|
60
|
+
function Question({ rebinder, pending }) {
|
|
61
|
+
const { actions } = useShortcuts();
|
|
62
|
+
const mine = actions.find((action) => action.id === rebinder.target?.id)?.description ?? "";
|
|
63
|
+
const keys = describeKeys(pending.keys);
|
|
64
|
+
return (_jsxs("div", { className: "kumiko-shortcuts-capture", "data-mode": pending.kind, role: "alert", children: [pending.kind === "taken" ? (_jsxs("p", { children: [keys, " already runs \u201C", pending.owner.description, "\u201D. Swap gives it the keys \u201C", mine, "\u201D had. Replace takes ", keys, " away from it."] })) : (_jsxs("p", { children: [keys, " is reserved. ", pending.reason] })), pending.kind === "taken" ? (_jsxs(_Fragment, { children: [_jsx("button", { type: "button", onClick: () => rebinder.resolve("swap"), children: "Swap" }), _jsx("button", { type: "button", onClick: () => rebinder.resolve("replace"), children: "Replace" })] })) : (_jsx("button", { type: "button", onClick: rebinder.useAnyway, children: "Use anyway" })), _jsx("button", { type: "button", onClick: rebinder.cancel, children: "Cancel" })] }));
|
|
65
|
+
}
|
|
66
|
+
/** What shows under an action while its key is being changed: the capture, and the answer to a conflict. */
|
|
67
|
+
export function Capture({ rebinder, bare = false }) {
|
|
68
|
+
const { options } = useShortcuts();
|
|
69
|
+
const { pending, message } = rebinder;
|
|
70
|
+
const captures = { press: PressCapture, type: TypeCapture, list: ListCapture };
|
|
71
|
+
const Mode = captures[options.capture];
|
|
72
|
+
let body = bare ? null : _jsx(Mode, { rebinder: rebinder });
|
|
73
|
+
if (pending) {
|
|
74
|
+
body = _jsx(Question, { rebinder: rebinder, pending: pending });
|
|
75
|
+
}
|
|
76
|
+
const wrap = useRef(null);
|
|
77
|
+
const cancel = useRef(rebinder.cancel);
|
|
78
|
+
useEffect(() => {
|
|
79
|
+
cancel.current = rebinder.cancel;
|
|
80
|
+
});
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
/* Tapping anywhere else gives up, so a touch screen never needs a key to get out. */
|
|
83
|
+
const outside = (event) => {
|
|
84
|
+
if (event.target instanceof Node && !wrap.current?.contains(event.target)) {
|
|
85
|
+
cancel.current();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
document.addEventListener("pointerdown", outside);
|
|
89
|
+
return () => document.removeEventListener("pointerdown", outside);
|
|
90
|
+
}, []);
|
|
91
|
+
return (_jsxs("div", { className: "kumiko-shortcuts-capture-wrap", ref: wrap, children: [body, message && !pending ? (_jsx("p", { className: "kumiko-shortcuts-message", role: "alert", children: message })) : null] }));
|
|
92
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Tells the shortcuts a drag has started. Returns the function that tells them it ended. */
|
|
2
|
+
export declare function reportDrag(): () => void;
|
|
3
|
+
/** Reports a drag while `active` is true, and ends it when the component goes away. */
|
|
4
|
+
export declare function useReportDrag(active: boolean): void;
|
|
5
|
+
/** Whether an app has reported a drag that is still running. */
|
|
6
|
+
export declare function isDragging(): boolean;
|
|
7
|
+
export declare function useIsDragging(): boolean;
|
|
8
|
+
/** Whether a menu is open. Menus that are not modal leave the page's shortcuts running, so this checks the page for one that is shown. */
|
|
9
|
+
export declare function isMenuOpen(): boolean;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { useEffect, useSyncExternalStore } from "react";
|
|
2
|
+
/* How many drags are running right now. Apps report them, so page shortcuts can wait. */
|
|
3
|
+
let running = 0;
|
|
4
|
+
const listeners = new Set();
|
|
5
|
+
function change(by) {
|
|
6
|
+
running = Math.max(0, running + by);
|
|
7
|
+
for (const listener of listeners) {
|
|
8
|
+
listener();
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** Tells the shortcuts a drag has started. Returns the function that tells them it ended. */
|
|
12
|
+
export function reportDrag() {
|
|
13
|
+
change(1);
|
|
14
|
+
let ended = false;
|
|
15
|
+
return () => {
|
|
16
|
+
if (!ended) {
|
|
17
|
+
ended = true;
|
|
18
|
+
change(-1);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** Reports a drag while `active` is true, and ends it when the component goes away. */
|
|
23
|
+
export function useReportDrag(active) {
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (!active) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
return reportDrag();
|
|
29
|
+
}, [active]);
|
|
30
|
+
}
|
|
31
|
+
/** Whether an app has reported a drag that is still running. */
|
|
32
|
+
export function isDragging() {
|
|
33
|
+
return running > 0;
|
|
34
|
+
}
|
|
35
|
+
export function useIsDragging() {
|
|
36
|
+
return useSyncExternalStore((listener) => {
|
|
37
|
+
listeners.add(listener);
|
|
38
|
+
return () => listeners.delete(listener);
|
|
39
|
+
}, isDragging, () => false);
|
|
40
|
+
}
|
|
41
|
+
/** Whether an element is on screen: it takes room and is not hidden. A closed menu that stays mounted does not. */
|
|
42
|
+
function isShown(element) {
|
|
43
|
+
return element.getClientRects().length > 0 && getComputedStyle(element).visibility !== "hidden";
|
|
44
|
+
}
|
|
45
|
+
/** Whether a menu is open. Menus that are not modal leave the page's shortcuts running, so this checks the page for one that is shown. */
|
|
46
|
+
export function isMenuOpen() {
|
|
47
|
+
return [...document.querySelectorAll('[role="menu"]')].some((menu) => isShown(menu));
|
|
48
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { DialogTrigger } from "../dialog";
|
|
2
|
+
export type HelpPresentation = "popover" | "dialog" | "panel";
|
|
3
|
+
/** The button attributes and `render` the sheet's trigger takes, as the other triggers do. */
|
|
4
|
+
type TriggerProps = Omit<React.ComponentProps<typeof DialogTrigger>, "children" | "className" | "controller">;
|
|
5
|
+
export type ShortcutsHelpProps = TriggerProps & {
|
|
6
|
+
/**
|
|
7
|
+
* Where the sheet appears: a dialog with search, a popover from its button, or a panel at the side.
|
|
8
|
+
* @defaultValue "dialog"
|
|
9
|
+
*/
|
|
10
|
+
presentation?: HelpPresentation;
|
|
11
|
+
/**
|
|
12
|
+
* Whether the sheet has an Edit button for changing keys. Without it, an app puts rebinding where it
|
|
13
|
+
* likes (`ShortcutsSettings`, `ShortcutsList`, or its own with `useRebinder`).
|
|
14
|
+
* @defaultValue false
|
|
15
|
+
*/
|
|
16
|
+
editable?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* What the button says.
|
|
19
|
+
* @defaultValue "Shortcuts"
|
|
20
|
+
*/
|
|
21
|
+
label?: React.ReactNode;
|
|
22
|
+
/** A class for the button. */
|
|
23
|
+
className?: string;
|
|
24
|
+
};
|
|
25
|
+
/** A button that opens the help sheet: what can run now, by section. Its keys (`helpKeys`) open it too. */
|
|
26
|
+
export declare function ShortcutsHelp({ presentation, editable, label, className, ...trigger }: ShortcutsHelpProps): React.JSX.Element;
|
|
27
|
+
/** Rebinding on its own, for apps that keep it in a settings page instead of the help sheet. */
|
|
28
|
+
export declare function ShortcutsSettings(): React.JSX.Element;
|
|
29
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { Dialog, DialogBackdrop, DialogClose, DialogContent, DialogDescription, DialogTitle, DialogTrigger } from "../dialog";
|
|
4
|
+
import { Popover, PopoverContent, PopoverDescription, PopoverTitle, PopoverTrigger } from "../popover";
|
|
5
|
+
import { ShortcutsList } from "./list";
|
|
6
|
+
import { useShortcuts } from "./provider";
|
|
7
|
+
function Body({ presentation, editable }) {
|
|
8
|
+
const [editing, setEditing] = useState(false);
|
|
9
|
+
const [query, setQuery] = useState("");
|
|
10
|
+
const { options } = useShortcuts();
|
|
11
|
+
const help = options.helpKeys;
|
|
12
|
+
return (_jsxs("div", { className: "kumiko-shortcuts-sheet", "data-presentation": presentation, children: [presentation === "dialog" ? (_jsx("input", { type: "search", "aria-label": "Search shortcuts", placeholder: "Search shortcuts", value: query, onChange: (event) => setQuery(event.target.value) })) : null, _jsx(ShortcutsList, { editing: editing, all: editing, query: query }), help.length > 0 ? (_jsxs("p", { className: "kumiko-shortcuts-foot", children: ["This sheet opens with ", help.join(" or "), "."] })) : null, editable ? (_jsx("button", { type: "button", onClick: () => setEditing((now) => !now), children: editing ? "Done" : "Edit keys" })) : null] }));
|
|
13
|
+
}
|
|
14
|
+
/** A button that opens the help sheet: what can run now, by section. Its keys (`helpKeys`) open it too. */
|
|
15
|
+
export function ShortcutsHelp({ presentation = "dialog", editable = false, label = "Shortcuts", className, ...trigger }) {
|
|
16
|
+
const { helpOpen, openHelp, closeHelp } = useShortcuts();
|
|
17
|
+
const onOpenChange = (open) => (open ? openHelp() : closeHelp());
|
|
18
|
+
if (presentation === "popover") {
|
|
19
|
+
return (_jsxs(Popover, { open: helpOpen, onOpenChange: onOpenChange, children: [_jsx(PopoverTrigger, { ...trigger, className: className, children: label }), _jsxs(PopoverContent, { align: "end", sideOffset: 8, className: "kumiko-shortcuts-popover", children: [_jsx(PopoverTitle, { children: "Keyboard shortcuts" }), _jsx(PopoverDescription, { children: "What works right now." }), _jsx(Body, { presentation: presentation, editable: editable })] })] }));
|
|
20
|
+
}
|
|
21
|
+
return (_jsxs(Dialog, { open: helpOpen, onOpenChange: onOpenChange, children: [_jsx(DialogTrigger, { ...trigger, className: className, children: label }), _jsx(DialogBackdrop, {}), _jsxs(DialogContent, { className: "kumiko-shortcuts-dialog", "data-kumiko-shortcuts": presentation, children: [_jsx(DialogTitle, { children: "Keyboard shortcuts" }), _jsx(DialogDescription, { children: "What works right now." }), _jsx(Body, { presentation: presentation, editable: editable }), _jsx(DialogClose, { className: className, children: "Close" })] })] }));
|
|
22
|
+
}
|
|
23
|
+
/** Rebinding on its own, for apps that keep it in a settings page instead of the help sheet. */
|
|
24
|
+
export function ShortcutsSettings() {
|
|
25
|
+
return (_jsx("div", { className: "kumiko-shortcuts-sheet", "data-presentation": "settings", children: _jsx(ShortcutsList, { editing: true }) }));
|
|
26
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface ShortcutHintProps {
|
|
2
|
+
/** The id of the action the control runs. */
|
|
3
|
+
action: string;
|
|
4
|
+
/** What the tooltip says before the keys. */
|
|
5
|
+
label: string;
|
|
6
|
+
/** The control. It gets `aria-keyshortcuts` in every mode. */
|
|
7
|
+
children: React.ReactElement<React.HTMLAttributes<HTMLElement>>;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Shows an action's current keys on the control that runs it, the way the provider's `hints` says:
|
|
11
|
+
* a tooltip, a badge while the mod key is held, or nothing.
|
|
12
|
+
*/
|
|
13
|
+
export declare function ShortcutHint({ action, label, children }: ShortcutHintProps): React.JSX.Element;
|
|
14
|
+
/** A button that shows the key badges on a touch screen, where there is no mod key to hold. Renders only when `hints` is "badges". */
|
|
15
|
+
export declare function ShortcutBadgesButton({ className, children }: {
|
|
16
|
+
className?: string;
|
|
17
|
+
/** What the button says while the badges are hidden. */
|
|
18
|
+
children?: React.ReactNode;
|
|
19
|
+
}): React.JSX.Element | null;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { ariaKeyshortcuts } from "@ui-sdk/react/shortcuts";
|
|
3
|
+
import { cloneElement } from "react";
|
|
4
|
+
import { Kbd } from "../kbd";
|
|
5
|
+
import { Tooltip, TooltipContent, TooltipTrigger } from "../tooltip";
|
|
6
|
+
import { useShortcuts } from "./provider";
|
|
7
|
+
/**
|
|
8
|
+
* Shows an action's current keys on the control that runs it, the way the provider's `hints` says:
|
|
9
|
+
* a tooltip, a badge while the mod key is held, or nothing.
|
|
10
|
+
*/
|
|
11
|
+
export function ShortcutHint({ action, label, children }) {
|
|
12
|
+
const { keysFor, options, modHeld } = useShortcuts();
|
|
13
|
+
const keys = keysFor(action);
|
|
14
|
+
const first = keys[0];
|
|
15
|
+
const control = cloneElement(children, {
|
|
16
|
+
"aria-keyshortcuts": keys.length > 0 ? keys.map((key) => ariaKeyshortcuts(key)).join(" ") : undefined
|
|
17
|
+
});
|
|
18
|
+
if (options.hints === "tooltip") {
|
|
19
|
+
return (_jsxs(Tooltip, { children: [_jsx(TooltipTrigger, { render: control }), _jsxs(TooltipContent, { children: [label, first ? (_jsxs(_Fragment, { children: [" ", _jsx(Kbd, { keys: first, listen: false, look: "flat" })] })) : null] })] }));
|
|
20
|
+
}
|
|
21
|
+
if (options.hints === "badges") {
|
|
22
|
+
return (_jsxs("span", { className: "kumiko-shortcuts-target", children: [control, first ? (_jsx(Kbd, { keys: first, listen: false, className: "kumiko-shortcuts-badge", "data-shown": modHeld ? "" : undefined, "aria-hidden": "true" })) : null] }));
|
|
23
|
+
}
|
|
24
|
+
return control;
|
|
25
|
+
}
|
|
26
|
+
/** A button that shows the key badges on a touch screen, where there is no mod key to hold. Renders only when `hints` is "badges". */
|
|
27
|
+
export function ShortcutBadgesButton({ className, children = "Show key badges" }) {
|
|
28
|
+
const { options, modHeld, setModHeld } = useShortcuts();
|
|
29
|
+
if (options.hints !== "badges") {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return (_jsx("button", { type: "button", className: className, "aria-pressed": modHeld, onClick: () => setModHeld(!modHeld), children: modHeld ? "Hide key badges" : children }));
|
|
33
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { type ShortcutAction, type Overrides } from "./bindings";
|
|
2
|
+
export { reportDrag, useIsDragging, useReportDrag } from "./guard";
|
|
3
|
+
export { ShortcutsHelp, ShortcutsSettings, type HelpPresentation, type ShortcutsHelpProps } from "./help";
|
|
4
|
+
export { ShortcutBadgesButton, ShortcutHint, type ShortcutHintProps } from "./hints";
|
|
5
|
+
export { defaultReservedKeys } from "./keys";
|
|
6
|
+
export { type ShortcutNote } from "./notes";
|
|
7
|
+
export { ShortcutsList, type ShortcutsListProps } from "./list";
|
|
8
|
+
export { ShortcutsProvider, useShortcuts, type CaptureMode, type HintMode, type ReservedMode, type ShortcutsContextValue, type ShortcutsOptions, type ShortcutsProviderProps, type TakenKeys } from "./provider";
|
|
9
|
+
export { ariaKeyshortcuts, formatShortcut, shortcuts, useShortcutHint, type ShortcutInfo } from "@ui-sdk/react/shortcuts";
|
|
10
|
+
export { useShortcut, type OverDialogsOptions } from "./over-dialogs";
|
|
11
|
+
export { Capture as ShortcutPrompt } from "./capture";
|
|
12
|
+
export { useRebinder, type Rebinder, type Target } from "./rebinder";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export {} from "./bindings";
|
|
2
|
+
export { reportDrag, useIsDragging, useReportDrag } from "./guard";
|
|
3
|
+
export { ShortcutsHelp, ShortcutsSettings } from "./help";
|
|
4
|
+
export { ShortcutBadgesButton, ShortcutHint } from "./hints";
|
|
5
|
+
export { defaultReservedKeys } from "./keys";
|
|
6
|
+
export {} from "./notes";
|
|
7
|
+
export { ShortcutsList } from "./list";
|
|
8
|
+
export { ShortcutsProvider, useShortcuts } from "./provider";
|
|
9
|
+
export { ariaKeyshortcuts, formatShortcut, shortcuts, useShortcutHint } from "@ui-sdk/react/shortcuts";
|
|
10
|
+
export { useShortcut } from "./over-dialogs";
|
|
11
|
+
export { Capture as ShortcutPrompt } from "./capture";
|
|
12
|
+
export { useRebinder } from "./rebinder";
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type Overrides, type Resolution, type ShortcutAction } from "./bindings";
|
|
2
|
+
/** The reserved-keys and taken-keys choices a verdict depends on. */
|
|
3
|
+
export interface JudgeOptions {
|
|
4
|
+
reserved: "block" | "warn";
|
|
5
|
+
reservedKeys: Readonly<Record<string, string>>;
|
|
6
|
+
taken: "ask" | "refuse" | "newest";
|
|
7
|
+
}
|
|
8
|
+
/** The key being changed: which action, and which of its keys. Slot -1 is a key being added. */
|
|
9
|
+
export interface Target {
|
|
10
|
+
id: string;
|
|
11
|
+
slot: number;
|
|
12
|
+
}
|
|
13
|
+
/** A question the person has to answer before the new keys are used. */
|
|
14
|
+
export type Pending = {
|
|
15
|
+
kind: "taken";
|
|
16
|
+
keys: string;
|
|
17
|
+
owner: ShortcutAction;
|
|
18
|
+
} | {
|
|
19
|
+
kind: "reserved";
|
|
20
|
+
keys: string;
|
|
21
|
+
reason: string;
|
|
22
|
+
};
|
|
23
|
+
/** What offering new keys comes to. */
|
|
24
|
+
export type Verdict = {
|
|
25
|
+
kind: "unchanged";
|
|
26
|
+
} | {
|
|
27
|
+
kind: "refused";
|
|
28
|
+
message: string;
|
|
29
|
+
} | {
|
|
30
|
+
kind: "ask";
|
|
31
|
+
pending: Pending;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "bind";
|
|
34
|
+
keys: string;
|
|
35
|
+
how: Resolution;
|
|
36
|
+
};
|
|
37
|
+
/** Decides what happens to new keys: used, refused with a reason, or a question first. */
|
|
38
|
+
export declare function judge(context: {
|
|
39
|
+
actions: readonly ShortcutAction[];
|
|
40
|
+
overrides: Overrides;
|
|
41
|
+
options: JudgeOptions;
|
|
42
|
+
}, target: Target, raw: string, allowReserved: boolean): Verdict;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { keysOf, ownerOf } from "./bindings";
|
|
2
|
+
import { canonicalKeys, describeKeys } from "./keys";
|
|
3
|
+
/** Decides what happens to new keys: used, refused with a reason, or a question first. */
|
|
4
|
+
export function judge(context, target, raw, allowReserved) {
|
|
5
|
+
const { actions, overrides, options } = context;
|
|
6
|
+
const action = actions.find((item) => item.id === target.id);
|
|
7
|
+
const keys = canonicalKeys(raw);
|
|
8
|
+
const own = action ? keysOf(action, overrides) : [];
|
|
9
|
+
if (own[target.slot] === keys) {
|
|
10
|
+
return { kind: "unchanged" };
|
|
11
|
+
}
|
|
12
|
+
if (own.includes(keys)) {
|
|
13
|
+
return { kind: "refused", message: `${describeKeys(keys)} is already one of its keys.` };
|
|
14
|
+
}
|
|
15
|
+
const reason = Object.entries(options.reservedKeys).find(([reserved]) => canonicalKeys(reserved) === keys)?.[1];
|
|
16
|
+
if (reason !== undefined && !allowReserved) {
|
|
17
|
+
return options.reserved === "block"
|
|
18
|
+
? { kind: "refused", message: `${describeKeys(keys)} is reserved. ${reason}` }
|
|
19
|
+
: { kind: "ask", pending: { kind: "reserved", keys, reason } };
|
|
20
|
+
}
|
|
21
|
+
const owner = ownerOf(actions, overrides, keys, target.id);
|
|
22
|
+
if (!owner) {
|
|
23
|
+
return { kind: "bind", keys, how: "plain" };
|
|
24
|
+
}
|
|
25
|
+
if (options.taken === "newest") {
|
|
26
|
+
return { kind: "bind", keys, how: "replace" };
|
|
27
|
+
}
|
|
28
|
+
return options.taken === "refuse"
|
|
29
|
+
? {
|
|
30
|
+
kind: "refused",
|
|
31
|
+
message: `${describeKeys(keys)} already runs “${owner.description}”.`
|
|
32
|
+
}
|
|
33
|
+
: { kind: "ask", pending: { kind: "taken", keys, owner } };
|
|
34
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Keys that are never offered to people because they already mean something everywhere, and why. */
|
|
2
|
+
export declare const defaultReservedKeys: Readonly<Record<string, string>>;
|
|
3
|
+
/** One spelling for a shortcut: lower case, modifiers first in a fixed order, so `mod+k` and `Mod + K` compare equal. */
|
|
4
|
+
export declare function canonicalKeys(keys: string): string;
|
|
5
|
+
/** Whether typed text is a usable shortcut: any modifiers, then exactly one key that is not a modifier. */
|
|
6
|
+
export declare function isValidKeys(keys: string): boolean;
|
|
7
|
+
export declare function onApple(): boolean;
|
|
8
|
+
/** The shortcut a key press spells, with `mod` for Command on Apple platforms and Control elsewhere. Null for a lone modifier. */
|
|
9
|
+
export declare function keysFromEvent(event: KeyboardEvent): string | null;
|
|
10
|
+
/** Keys as this platform writes them, for sentences that name them. */
|
|
11
|
+
export declare function describeKeys(keys: string): string;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { formatShortcut } from "@ui-sdk/react/shortcuts";
|
|
2
|
+
/* Modifiers in the order a shortcut writes them, so two spellings of one shortcut compare equal. */
|
|
3
|
+
const modifierOrder = ["mod", "meta", "control", "alt", "shift"];
|
|
4
|
+
const aliases = {
|
|
5
|
+
ctrl: "control",
|
|
6
|
+
cmd: "meta",
|
|
7
|
+
command: "meta",
|
|
8
|
+
option: "alt",
|
|
9
|
+
esc: "escape",
|
|
10
|
+
return: "enter",
|
|
11
|
+
spacebar: "space",
|
|
12
|
+
up: "arrowup",
|
|
13
|
+
down: "arrowdown",
|
|
14
|
+
left: "arrowleft",
|
|
15
|
+
right: "arrowright"
|
|
16
|
+
};
|
|
17
|
+
/** Keys that are never offered to people because they already mean something everywhere, and why. */
|
|
18
|
+
export const defaultReservedKeys = {
|
|
19
|
+
"mod+z": "Undo is on it in every text field.",
|
|
20
|
+
"mod+c": "Copy is on it.",
|
|
21
|
+
"mod+v": "Paste is on it.",
|
|
22
|
+
"mod+x": "Cut is on it.",
|
|
23
|
+
"mod+a": "Select all is on it.",
|
|
24
|
+
tab: "Tab moves focus, so it has to stay free.",
|
|
25
|
+
escape: "Escape closes things and cancels, so it has to stay free."
|
|
26
|
+
};
|
|
27
|
+
function part(raw) {
|
|
28
|
+
const lower = raw.length === 1 ? raw.toLowerCase() : raw.toLowerCase().trim();
|
|
29
|
+
return aliases[lower] ?? lower;
|
|
30
|
+
}
|
|
31
|
+
/** One spelling for a shortcut: lower case, modifiers first in a fixed order, so `mod+k` and `Mod + K` compare equal. */
|
|
32
|
+
export function canonicalKeys(keys) {
|
|
33
|
+
const parts = keys.split("+").map((raw) => part(raw));
|
|
34
|
+
const key = parts.at(-1) ?? "";
|
|
35
|
+
const modifiers = [...new Set(parts.slice(0, -1))].sort((a, b) => modifierOrder.indexOf(a) - modifierOrder.indexOf(b));
|
|
36
|
+
return [...modifiers, key].join("+");
|
|
37
|
+
}
|
|
38
|
+
/** Whether typed text is a usable shortcut: any modifiers, then exactly one key that is not a modifier. */
|
|
39
|
+
export function isValidKeys(keys) {
|
|
40
|
+
const parts = canonicalKeys(keys).split("+");
|
|
41
|
+
const key = parts.at(-1) ?? "";
|
|
42
|
+
return (key !== "" &&
|
|
43
|
+
!modifierOrder.includes(key) &&
|
|
44
|
+
parts.slice(0, -1).every((value) => modifierOrder.includes(value)));
|
|
45
|
+
}
|
|
46
|
+
export function onApple() {
|
|
47
|
+
return formatShortcut("mod").includes("⌘");
|
|
48
|
+
}
|
|
49
|
+
const ignoredKeys = new Set([
|
|
50
|
+
"Shift",
|
|
51
|
+
"Control",
|
|
52
|
+
"Alt",
|
|
53
|
+
"Meta",
|
|
54
|
+
"AltGraph",
|
|
55
|
+
"CapsLock",
|
|
56
|
+
"Dead",
|
|
57
|
+
"Unidentified"
|
|
58
|
+
]);
|
|
59
|
+
/** The shortcut a key press spells, with `mod` for Command on Apple platforms and Control elsewhere. Null for a lone modifier. */
|
|
60
|
+
export function keysFromEvent(event) {
|
|
61
|
+
if (ignoredKeys.has(event.key)) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
let key = event.key === " " ? "space" : part(event.key);
|
|
65
|
+
/* A letter or digit is named by its physical key, so Option+N on a Mac is still n. */
|
|
66
|
+
const code = /^(?:Key(?<letter>[A-Z])|Digit(?<digit>\d))$/u.exec(event.code);
|
|
67
|
+
if (code) {
|
|
68
|
+
key = (code.groups?.letter ?? code.groups?.digit ?? key).toLowerCase();
|
|
69
|
+
}
|
|
70
|
+
const apple = onApple();
|
|
71
|
+
const modifiers = [];
|
|
72
|
+
if (apple ? event.metaKey : event.ctrlKey) {
|
|
73
|
+
modifiers.push("mod");
|
|
74
|
+
}
|
|
75
|
+
if (apple ? event.ctrlKey : event.metaKey) {
|
|
76
|
+
modifiers.push(apple ? "control" : "meta");
|
|
77
|
+
}
|
|
78
|
+
if (event.altKey) {
|
|
79
|
+
modifiers.push("alt");
|
|
80
|
+
}
|
|
81
|
+
if (event.shiftKey) {
|
|
82
|
+
modifiers.push("shift");
|
|
83
|
+
}
|
|
84
|
+
return canonicalKeys([...modifiers, key].join("+"));
|
|
85
|
+
}
|
|
86
|
+
/** Keys as this platform writes them, for sentences that name them. */
|
|
87
|
+
export function describeKeys(keys) {
|
|
88
|
+
return formatShortcut(keys);
|
|
89
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface ShortcutsListProps {
|
|
2
|
+
/**
|
|
3
|
+
* Whether keys can be changed here: a Change button on every key, Add another, Reset.
|
|
4
|
+
* @defaultValue false
|
|
5
|
+
*/
|
|
6
|
+
editing?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Whether to list every action. Without it, only the actions that could run when the help sheet opened.
|
|
9
|
+
* @defaultValue true when `editing`, false otherwise
|
|
10
|
+
*/
|
|
11
|
+
all?: boolean;
|
|
12
|
+
/** Keeps only actions whose name or section contains this text. */
|
|
13
|
+
query?: string;
|
|
14
|
+
}
|
|
15
|
+
/** The actions grouped by section, each with its keys. With `editing`, every key can be changed. */
|
|
16
|
+
export declare function ShortcutsList({ editing, all, query }: ShortcutsListProps): React.JSX.Element;
|