@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
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { formatShortcut } from "@ui-sdk/react/shortcuts";
|
|
3
|
+
import { Kbd } from "../kbd";
|
|
4
|
+
import { defaultKeys, maxKeysPerAction } from "./bindings";
|
|
5
|
+
import { Capture } from "./capture";
|
|
6
|
+
import { noteKeys, sectionRows } from "./notes";
|
|
7
|
+
import { useShortcuts } from "./provider";
|
|
8
|
+
import { useRebinder } from "./rebinder";
|
|
9
|
+
/** The key as a person sees it (Ctrl+K), without the marks that keep it left to right, for a screen reader. */
|
|
10
|
+
function spoken(key) {
|
|
11
|
+
return formatShortcut(key).replaceAll(/[]/gu, "");
|
|
12
|
+
}
|
|
13
|
+
function sameKeys(action, keys) {
|
|
14
|
+
const defaults = defaultKeys(action);
|
|
15
|
+
return keys.length === defaults.length && keys.every((key, index) => key === defaults[index]);
|
|
16
|
+
}
|
|
17
|
+
function KeyChip({ action, slot, removable, rebinder }) {
|
|
18
|
+
const { keysFor } = useShortcuts();
|
|
19
|
+
const key = keysFor(action.id)[slot] ?? "";
|
|
20
|
+
const { target } = rebinder;
|
|
21
|
+
const changing = target?.id === action.id && target.slot === slot;
|
|
22
|
+
return (_jsxs("span", { className: "kumiko-shortcuts-key", "data-changing": changing ? "" : undefined, children: [_jsx(Kbd, { keys: key, listen: false }), _jsx("button", { type: "button", "aria-label": `Change ${spoken(key)} for ${action.description}`, onClick: () => rebinder.begin({ id: action.id, slot }), children: "Change" }), removable ? (_jsx("button", { type: "button", "aria-label": `Remove ${spoken(key)} from ${action.description}`, onClick: () => rebinder.remove(action.id, slot), children: "Remove" })) : null] }));
|
|
23
|
+
}
|
|
24
|
+
function KeyEditor({ action, rebinder }) {
|
|
25
|
+
const { keysFor, options } = useShortcuts();
|
|
26
|
+
const keys = keysFor(action.id);
|
|
27
|
+
const canAdd = options.multipleKeys ? keys.length < maxKeysPerAction : keys.length === 0;
|
|
28
|
+
return (_jsxs("div", { className: "kumiko-shortcuts-editor", children: [keys.map((key, slot) => (_jsx(KeyChip, { action: action, slot: slot, removable: keys.length > 1, rebinder: rebinder }, key))), keys.length === 0 ? _jsx("span", { className: "kumiko-shortcuts-none", children: "No keys" }) : null, canAdd ? (_jsx("button", { type: "button", onClick: () => rebinder.begin({ id: action.id, slot: -1 }), children: keys.length === 0 ? "Set keys" : "Add another" })) : null, options.resetEach && !sameKeys(action, keys) ? (_jsx("button", { type: "button", onClick: () => rebinder.reset(action.id), children: "Reset" })) : null] }));
|
|
29
|
+
}
|
|
30
|
+
function Row({ action, editing, rebinder }) {
|
|
31
|
+
const { keysFor, available } = useShortcuts();
|
|
32
|
+
return (_jsxs("li", { "data-unavailable": available.has(action.id) ? undefined : "", children: [_jsx("span", { className: "kumiko-shortcuts-name", children: action.description }), editing ? (_jsx(KeyEditor, { action: action, rebinder: rebinder })) : (_jsx("span", { className: "kumiko-shortcuts-keys", children: keysFor(action.id).map((key) => (_jsx(Kbd, { keys: key, listen: false }, key))) })), rebinder.target?.id === action.id ? _jsx(Capture, { rebinder: rebinder }) : null] }));
|
|
33
|
+
}
|
|
34
|
+
function NoteRow({ note }) {
|
|
35
|
+
return (_jsxs("li", { "data-note": "", children: [_jsx("span", { className: "kumiko-shortcuts-name", children: note.description }), _jsx("span", { className: "kumiko-shortcuts-keys", children: noteKeys(note).map((key) => (_jsx(Kbd, { keys: key, listen: false }, key))) })] }));
|
|
36
|
+
}
|
|
37
|
+
function EditFooter() {
|
|
38
|
+
const { resetAll, overrides, options, saveFailed } = useShortcuts();
|
|
39
|
+
return (_jsxs(_Fragment, { children: [saveFailed ? (_jsx("p", { className: "kumiko-shortcuts-message", role: "alert", children: "Your changes could not be saved. They last until the page closes." })) : null, Object.keys(overrides).length > 0 ? (_jsx("p", { children: _jsx("button", { type: "button", onClick: resetAll, children: "Reset all" }) })) : null, options.multipleKeys ? null : (_jsx("p", { className: "kumiko-shortcuts-empty", children: "Each action has one key." }))] }));
|
|
40
|
+
}
|
|
41
|
+
/** The actions grouped by section, each with its keys. With `editing`, every key can be changed. */
|
|
42
|
+
export function ShortcutsList({ editing = false, all = editing, query = "" }) {
|
|
43
|
+
const { actions, notes, available } = useShortcuts();
|
|
44
|
+
const rebinder = useRebinder();
|
|
45
|
+
const needle = query.trim().toLowerCase();
|
|
46
|
+
const sections = sectionRows(actions, notes, (action) => all || available.has(action.id), needle);
|
|
47
|
+
return (_jsxs("div", { className: "kumiko-shortcuts-list", "data-editing": editing ? "" : undefined, children: [sections.map(({ section, rows }) => (_jsxs("section", { children: [_jsx("h3", { children: section }), _jsx("ul", { children: rows.map((row) => row.kind === "note" ? (_jsx(NoteRow, { note: row.note }, `note:${row.note.description}`)) : (_jsx(Row, { action: row.action, editing: editing, rebinder: rebinder }, row.action.id))) })] }, section))), sections.length === 0 ? (_jsx("p", { className: "kumiko-shortcuts-empty", children: needle === "" ? "Nothing has a shortcut right now." : "No shortcut matches." })) : null, editing ? _jsx(EditFooter, {}) : null] }));
|
|
48
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ShortcutAction } from "./bindings";
|
|
2
|
+
/** A key listed in the help sheet for people to read. It is never registered or rebound. */
|
|
3
|
+
export interface ShortcutNote {
|
|
4
|
+
/** What the key does, shown in the help sheet. */
|
|
5
|
+
description: string;
|
|
6
|
+
/** The heading it sits under, next to the actions of the same section. */
|
|
7
|
+
section: string;
|
|
8
|
+
/** The keys, one or several, written like `"mod+k"`. */
|
|
9
|
+
keys: string | readonly string[];
|
|
10
|
+
}
|
|
11
|
+
/** One line of the help sheet: an action people can rebind, or a note they can only read. */
|
|
12
|
+
export type ShortcutRow = {
|
|
13
|
+
kind: "action";
|
|
14
|
+
action: ShortcutAction;
|
|
15
|
+
} | {
|
|
16
|
+
kind: "note";
|
|
17
|
+
note: ShortcutNote;
|
|
18
|
+
};
|
|
19
|
+
export interface ShortcutSection {
|
|
20
|
+
section: string;
|
|
21
|
+
rows: ShortcutRow[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The rows to show, grouped by section in the order sections first appear (actions, then notes). Inside a
|
|
25
|
+
* section the actions come first, then the notes. Notes are always shown, and a search still filters them by text.
|
|
26
|
+
*/
|
|
27
|
+
export declare function sectionRows(actions: readonly ShortcutAction[], notes: readonly ShortcutNote[], keepAction: (action: ShortcutAction) => boolean, needle: string): ShortcutSection[];
|
|
28
|
+
export declare function noteKeys(note: ShortcutNote): string[];
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
function matches(text, needle) {
|
|
2
|
+
return needle === "" || text.toLowerCase().includes(needle);
|
|
3
|
+
}
|
|
4
|
+
function sectionOf(row) {
|
|
5
|
+
return row.kind === "action" ? row.action.section : row.note.section;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The rows to show, grouped by section in the order sections first appear (actions, then notes). Inside a
|
|
9
|
+
* section the actions come first, then the notes. Notes are always shown, and a search still filters them by text.
|
|
10
|
+
*/
|
|
11
|
+
export function sectionRows(actions, notes, keepAction, needle) {
|
|
12
|
+
const rows = [
|
|
13
|
+
...actions
|
|
14
|
+
.filter((action) => keepAction(action) && matches(`${action.description} ${action.section}`, needle))
|
|
15
|
+
.map((action) => ({ kind: "action", action })),
|
|
16
|
+
...notes
|
|
17
|
+
.filter((note) => matches(`${note.description} ${note.section}`, needle))
|
|
18
|
+
.map((note) => ({ kind: "note", note }))
|
|
19
|
+
];
|
|
20
|
+
return [...new Set(rows.map((row) => sectionOf(row)))].map((section) => ({
|
|
21
|
+
section,
|
|
22
|
+
rows: rows.filter((row) => sectionOf(row) === section)
|
|
23
|
+
}));
|
|
24
|
+
}
|
|
25
|
+
export function noteKeys(note) {
|
|
26
|
+
return typeof note.keys === "string" ? [note.keys] : [...note.keys];
|
|
27
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type ShortcutHandler, type UseShortcutOptions } from "@ui-sdk/react/shortcuts";
|
|
2
|
+
/**
|
|
3
|
+
* Keeps the one hidden element that makes a shortcut survive modals. It is created for the first holder,
|
|
4
|
+
* bound as a persistent layer, and removed when the last holder lets go.
|
|
5
|
+
*/
|
|
6
|
+
export declare function holdOverDialogs(): () => void;
|
|
7
|
+
/** The `layer` that keeps a shortcut running over modals. Nothing while no shortcut asked for it. */
|
|
8
|
+
export declare function overDialogsLayer(): Element | undefined;
|
|
9
|
+
export type OverDialogsOptions = UseShortcutOptions & {
|
|
10
|
+
/**
|
|
11
|
+
* Whether the shortcut keeps running while a modal (a dialog or a sheet) is open. Typing in a field and
|
|
12
|
+
* `when` still apply.
|
|
13
|
+
* @defaultValue false
|
|
14
|
+
*/
|
|
15
|
+
overDialogs?: boolean;
|
|
16
|
+
};
|
|
17
|
+
/** The ui-sdk `useShortcut`, with `overDialogs` for app-wide chords that must work over a modal. */
|
|
18
|
+
export declare function useShortcut(keys: string | null | undefined, handler: ShortcutHandler, { overDialogs, ...options }?: OverDialogsOptions): void;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { bindPersistentLayer } from "@ui-sdk/core";
|
|
2
|
+
import { useShortcut as useUiShortcut } from "@ui-sdk/react/shortcuts";
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
let layer;
|
|
5
|
+
let unbind;
|
|
6
|
+
let holders = 0;
|
|
7
|
+
/**
|
|
8
|
+
* Keeps the one hidden element that makes a shortcut survive modals. It is created for the first holder,
|
|
9
|
+
* bound as a persistent layer, and removed when the last holder lets go.
|
|
10
|
+
*/
|
|
11
|
+
export function holdOverDialogs() {
|
|
12
|
+
holders += 1;
|
|
13
|
+
if (!layer) {
|
|
14
|
+
layer = document.createElement("div");
|
|
15
|
+
layer.hidden = true;
|
|
16
|
+
layer.dataset.kumikoShortcutsOverDialogs = "";
|
|
17
|
+
document.body.append(layer);
|
|
18
|
+
unbind = bindPersistentLayer(layer);
|
|
19
|
+
}
|
|
20
|
+
return () => {
|
|
21
|
+
holders -= 1;
|
|
22
|
+
if (holders === 0) {
|
|
23
|
+
unbind?.();
|
|
24
|
+
layer?.remove();
|
|
25
|
+
layer = undefined;
|
|
26
|
+
unbind = undefined;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** The `layer` that keeps a shortcut running over modals. Nothing while no shortcut asked for it. */
|
|
31
|
+
export function overDialogsLayer() {
|
|
32
|
+
return layer;
|
|
33
|
+
}
|
|
34
|
+
/** The ui-sdk `useShortcut`, with `overDialogs` for app-wide chords that must work over a modal. */
|
|
35
|
+
export function useShortcut(keys, handler, { overDialogs = false, ...options } = {}) {
|
|
36
|
+
useEffect(() => (overDialogs ? holdOverDialogs() : undefined), [overDialogs]);
|
|
37
|
+
useUiShortcut(keys, handler, overDialogs ? { ...options, layer: overDialogsLayer } : options);
|
|
38
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { type StoredStorage } from "../stored";
|
|
2
|
+
import { type Overrides, type ShortcutAction } from "./bindings";
|
|
3
|
+
import type { ShortcutNote } from "./notes";
|
|
4
|
+
export type TakenKeys = "ask" | "refuse" | "newest";
|
|
5
|
+
export type ReservedMode = "block" | "warn";
|
|
6
|
+
export type CaptureMode = "press" | "type" | "list";
|
|
7
|
+
export type HintMode = "tooltip" | "badges" | "none";
|
|
8
|
+
/** Every choice the shortcuts offer. Each one has a default, the one the family leans towards. */
|
|
9
|
+
export interface ShortcutsOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Where changed keys are saved: "local", "session", or a backend factory (see `@alulore/kumiko/stored`).
|
|
12
|
+
* @defaultValue "local"
|
|
13
|
+
*/
|
|
14
|
+
storage?: StoredStorage;
|
|
15
|
+
/**
|
|
16
|
+
* The key the changed keys are saved under.
|
|
17
|
+
* @defaultValue "kumiko.shortcuts"
|
|
18
|
+
*/
|
|
19
|
+
storageKey?: string;
|
|
20
|
+
/** Called when saving the changed keys fails (storage blocked or full). The sheet also says so. */
|
|
21
|
+
onSaveError?: () => void;
|
|
22
|
+
/**
|
|
23
|
+
* Whether page shortcuts wait while a drag (reported with `reportDrag`) or a menu is open. Modals always pause them.
|
|
24
|
+
* @defaultValue true
|
|
25
|
+
*/
|
|
26
|
+
pauseWhileBusy?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* The keys that open the help sheet. Empty leaves the button as the only way.
|
|
29
|
+
* @defaultValue ["shift+?", "mod+/"]
|
|
30
|
+
*/
|
|
31
|
+
helpKeys?: readonly string[];
|
|
32
|
+
/**
|
|
33
|
+
* Where an action's keys are shown on its control: as badges while the mod key is held (a touch app
|
|
34
|
+
* adds `ShortcutBadgesButton`), in a tooltip, or nowhere.
|
|
35
|
+
* @defaultValue "badges"
|
|
36
|
+
*/
|
|
37
|
+
hints?: HintMode;
|
|
38
|
+
/**
|
|
39
|
+
* Whether an action can hold more than one key.
|
|
40
|
+
* @defaultValue true
|
|
41
|
+
*/
|
|
42
|
+
multipleKeys?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* What happens when new keys already belong to another action: ask (swap, replace or cancel), refuse, or let the newest win.
|
|
45
|
+
* @defaultValue "ask"
|
|
46
|
+
*/
|
|
47
|
+
taken?: TakenKeys;
|
|
48
|
+
/**
|
|
49
|
+
* Whether reserved keys are blocked, or allowed after a warning.
|
|
50
|
+
* @defaultValue "block"
|
|
51
|
+
*/
|
|
52
|
+
reserved?: ReservedMode;
|
|
53
|
+
/** The reserved keys and why, in place of the defaults (mod+z, mod+c, mod+v, mod+x, mod+a, tab, escape). */
|
|
54
|
+
reservedKeys?: Readonly<Record<string, string>>;
|
|
55
|
+
/**
|
|
56
|
+
* How new keys are given: press them, type them as text, or pick from a list.
|
|
57
|
+
* @defaultValue "press"
|
|
58
|
+
*/
|
|
59
|
+
capture?: CaptureMode;
|
|
60
|
+
/**
|
|
61
|
+
* Whether each action has its own reset, besides Reset all.
|
|
62
|
+
* @defaultValue true
|
|
63
|
+
*/
|
|
64
|
+
resetEach?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Keys that work only on a focused widget and are handled there. They are listed in the help sheet for people to
|
|
67
|
+
* read, under their section, and are never registered, rebound or hidden by what can run.
|
|
68
|
+
* @defaultValue []
|
|
69
|
+
*/
|
|
70
|
+
notes?: readonly ShortcutNote[];
|
|
71
|
+
}
|
|
72
|
+
export interface ShortcutsContextValue {
|
|
73
|
+
actions: readonly ShortcutAction[];
|
|
74
|
+
overrides: Overrides;
|
|
75
|
+
options: Required<Omit<ShortcutsOptions, "storage" | "storageKey" | "onSaveError" | "notes">>;
|
|
76
|
+
/** The read-only entries of the help sheet. */
|
|
77
|
+
notes: readonly ShortcutNote[];
|
|
78
|
+
keysFor: (id: string) => string[];
|
|
79
|
+
setOverrides: (next: Overrides) => void;
|
|
80
|
+
resetAll: () => void;
|
|
81
|
+
helpOpen: boolean;
|
|
82
|
+
openHelp: () => void;
|
|
83
|
+
closeHelp: () => void;
|
|
84
|
+
/** Ids of the actions that could run when the help sheet opened. */
|
|
85
|
+
available: ReadonlySet<string>;
|
|
86
|
+
/** Whether the last attempt to save the changed keys failed. */
|
|
87
|
+
saveFailed: boolean;
|
|
88
|
+
modHeld: boolean;
|
|
89
|
+
setModHeld: (held: boolean) => void;
|
|
90
|
+
}
|
|
91
|
+
export declare function useShortcuts(): ShortcutsContextValue;
|
|
92
|
+
export type ShortcutsProviderProps = ShortcutsOptions & {
|
|
93
|
+
/** What the app can do, and the keys that do it. */
|
|
94
|
+
actions: readonly ShortcutAction[];
|
|
95
|
+
children?: React.ReactNode;
|
|
96
|
+
};
|
|
97
|
+
/** Wraps the part of the app that has shortcuts: registers every action and holds what the help sheet and hints need. */
|
|
98
|
+
export declare function ShortcutsProvider(props: ShortcutsProviderProps): React.JSX.Element;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, use, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { defineStored, useStored } from "../stored";
|
|
4
|
+
import { isOverrides, keysOf, sanitizeOverrides } from "./bindings";
|
|
5
|
+
import { defaultReservedKeys } from "./keys";
|
|
6
|
+
import { listAvailable, useModHeld, useRegistration } from "./registration";
|
|
7
|
+
const ShortcutsContext = createContext(null);
|
|
8
|
+
export function useShortcuts() {
|
|
9
|
+
const value = use(ShortcutsContext);
|
|
10
|
+
if (!value) {
|
|
11
|
+
throw new Error("Shortcuts components need a ShortcutsProvider around them.");
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
/** The saved bindings, followed live, and what to call to change them. A failed save is reported. */
|
|
16
|
+
function useSavedOverrides(props, actions) {
|
|
17
|
+
const { storage = "local", storageKey = "kumiko.shortcuts", onSaveError } = props;
|
|
18
|
+
const definition = useMemo(() => defineStored({
|
|
19
|
+
key: storageKey,
|
|
20
|
+
fallback: {},
|
|
21
|
+
validate: isOverrides,
|
|
22
|
+
storage
|
|
23
|
+
}), [storageKey, storage]);
|
|
24
|
+
const [saved, set, status, reset] = useStored(definition);
|
|
25
|
+
const overrides = useMemo(() => sanitizeOverrides(saved, actions), [saved, actions]);
|
|
26
|
+
const failure = useRef(onSaveError);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
failure.current = onSaveError;
|
|
29
|
+
});
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (status.writeFailed) {
|
|
32
|
+
failure.current?.();
|
|
33
|
+
}
|
|
34
|
+
}, [status.writeFailed]);
|
|
35
|
+
const write = useCallback((next) => (Object.keys(next).length === 0 ? reset() : set(next)), [set, reset]);
|
|
36
|
+
return [overrides, write, status.writeFailed];
|
|
37
|
+
}
|
|
38
|
+
const defaultHelpKeys = ["shift+?", "mod+/"];
|
|
39
|
+
const noNotes = [];
|
|
40
|
+
/** Fills in every option's default, and keeps the object the same while the choices are. */
|
|
41
|
+
function useResolved(props) {
|
|
42
|
+
const { pauseWhileBusy = true, helpKeys = defaultHelpKeys, hints = "badges", multipleKeys = true, taken = "ask", reserved = "block", reservedKeys = defaultReservedKeys, capture = "press", resetEach = true } = props;
|
|
43
|
+
return useMemo(() => ({
|
|
44
|
+
pauseWhileBusy,
|
|
45
|
+
helpKeys,
|
|
46
|
+
hints,
|
|
47
|
+
multipleKeys,
|
|
48
|
+
taken,
|
|
49
|
+
reserved,
|
|
50
|
+
reservedKeys,
|
|
51
|
+
capture,
|
|
52
|
+
resetEach
|
|
53
|
+
}), [
|
|
54
|
+
pauseWhileBusy,
|
|
55
|
+
helpKeys,
|
|
56
|
+
hints,
|
|
57
|
+
multipleKeys,
|
|
58
|
+
taken,
|
|
59
|
+
reserved,
|
|
60
|
+
reservedKeys,
|
|
61
|
+
capture,
|
|
62
|
+
resetEach
|
|
63
|
+
]);
|
|
64
|
+
}
|
|
65
|
+
/** The help sheet's open state. The list of what can run is taken as it opens, because a modal sheet pauses the page. */
|
|
66
|
+
function useHelpState(live) {
|
|
67
|
+
const [helpOpen, setHelpOpen] = useState(false);
|
|
68
|
+
const [available, setAvailable] = useState(new Set());
|
|
69
|
+
const openHelp = useCallback(() => {
|
|
70
|
+
setAvailable(listAvailable(live));
|
|
71
|
+
setHelpOpen(true);
|
|
72
|
+
}, [live]);
|
|
73
|
+
const closeHelp = useCallback(() => setHelpOpen(false), []);
|
|
74
|
+
return useMemo(() => ({ helpOpen, available, openHelp, closeHelp }), [helpOpen, available, openHelp, closeHelp]);
|
|
75
|
+
}
|
|
76
|
+
/** Wraps the part of the app that has shortcuts: registers every action and holds what the help sheet and hints need. */
|
|
77
|
+
export function ShortcutsProvider(props) {
|
|
78
|
+
const { actions, children, notes = noNotes } = props;
|
|
79
|
+
const options = useResolved(props);
|
|
80
|
+
const [overrides, setOverrides, saveFailed] = useSavedOverrides(props, actions);
|
|
81
|
+
const live = useRef({ ...options, actions, overrides, toggleHelp: () => { } });
|
|
82
|
+
const help = useHelpState(live);
|
|
83
|
+
const [modHeld, setModHeld] = useModHeld(options.hints === "badges");
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
live.current = {
|
|
86
|
+
...options,
|
|
87
|
+
actions,
|
|
88
|
+
overrides,
|
|
89
|
+
toggleHelp: () => (help.helpOpen ? help.closeHelp() : help.openHelp())
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
const signature = [
|
|
93
|
+
...actions.map((action) => `${action.id}:${action.description}:${action.allowInInput}:${action.overDialogs}`),
|
|
94
|
+
...options.helpKeys
|
|
95
|
+
].join("|");
|
|
96
|
+
useRegistration(live, signature);
|
|
97
|
+
const value = useMemo(() => ({
|
|
98
|
+
...help,
|
|
99
|
+
actions,
|
|
100
|
+
notes,
|
|
101
|
+
overrides,
|
|
102
|
+
options,
|
|
103
|
+
keysFor: (id) => {
|
|
104
|
+
const action = actions.find((item) => item.id === id);
|
|
105
|
+
return action
|
|
106
|
+
? keysOf(action, overrides).slice(0, options.multipleKeys ? undefined : 1)
|
|
107
|
+
: [];
|
|
108
|
+
},
|
|
109
|
+
setOverrides,
|
|
110
|
+
resetAll: () => setOverrides({}),
|
|
111
|
+
saveFailed,
|
|
112
|
+
modHeld,
|
|
113
|
+
setModHeld
|
|
114
|
+
}), [help, actions, notes, overrides, options, setOverrides, saveFailed, modHeld, setModHeld]);
|
|
115
|
+
return _jsx(ShortcutsContext, { value: value, children: children });
|
|
116
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Resolution } from "./bindings";
|
|
2
|
+
import { type Pending, type Target } from "./judge";
|
|
3
|
+
export type { Pending, Target } from "./judge";
|
|
4
|
+
export interface Rebinder {
|
|
5
|
+
target: Target | null;
|
|
6
|
+
pending: Pending | null;
|
|
7
|
+
/** Why the last keys were not taken. */
|
|
8
|
+
message: string | null;
|
|
9
|
+
begin: (target: Target) => void;
|
|
10
|
+
cancel: () => void;
|
|
11
|
+
/** Offers new keys for the current target. */
|
|
12
|
+
submit: (keys: string, allowReserved?: boolean) => void;
|
|
13
|
+
/** Starts changing a key and offers the new keys at once, for a control that rebinds without capturing. */
|
|
14
|
+
offer: (target: Target, keys: string) => void;
|
|
15
|
+
/** Answers the question about keys another action has: swap, or replace. */
|
|
16
|
+
resolve: (how: Exclude<Resolution, "plain">) => void;
|
|
17
|
+
/** Answers the warning about a reserved key: use it anyway. */
|
|
18
|
+
useAnyway: () => void;
|
|
19
|
+
remove: (id: string, slot: number) => void;
|
|
20
|
+
reset: (id: string) => void;
|
|
21
|
+
}
|
|
22
|
+
/** The state of changing one key: capture, conflicts, reserved keys, and writing the result. */
|
|
23
|
+
export declare function useRebinder(): Rebinder;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { bindKeys, resetAction, unbindKey } from "./bindings";
|
|
3
|
+
import { judge } from "./judge";
|
|
4
|
+
import { useShortcuts } from "./provider";
|
|
5
|
+
/** Carries out a verdict: bind the keys, ask the question, or say why not. */
|
|
6
|
+
function settle(verdict, at, handlers) {
|
|
7
|
+
if (verdict.kind === "unchanged") {
|
|
8
|
+
handlers.cancel();
|
|
9
|
+
}
|
|
10
|
+
else if (verdict.kind === "bind") {
|
|
11
|
+
handlers.apply(at, verdict.keys, verdict.how);
|
|
12
|
+
}
|
|
13
|
+
else if (verdict.kind === "ask") {
|
|
14
|
+
handlers.setPending(verdict.pending);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
handlers.setMessage(verdict.message);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Which key is being changed, the question waiting on an answer, and why the last keys were refused. */
|
|
21
|
+
function useChange() {
|
|
22
|
+
const [target, setTarget] = useState(null);
|
|
23
|
+
const [pending, setPending] = useState(null);
|
|
24
|
+
const [message, setMessage] = useState(null);
|
|
25
|
+
const begin = (next) => {
|
|
26
|
+
setTarget(next);
|
|
27
|
+
setPending(null);
|
|
28
|
+
setMessage(null);
|
|
29
|
+
};
|
|
30
|
+
const cancel = () => {
|
|
31
|
+
setTarget(null);
|
|
32
|
+
setPending(null);
|
|
33
|
+
setMessage(null);
|
|
34
|
+
};
|
|
35
|
+
return { target, pending, message, begin, cancel, setPending, setMessage };
|
|
36
|
+
}
|
|
37
|
+
/** The state of changing one key: capture, conflicts, reserved keys, and writing the result. */
|
|
38
|
+
export function useRebinder() {
|
|
39
|
+
const context = useShortcuts();
|
|
40
|
+
const { actions, overrides, setOverrides } = context;
|
|
41
|
+
const { target, pending, message, begin, cancel, setPending, setMessage } = useChange();
|
|
42
|
+
const apply = (at, keys, how) => {
|
|
43
|
+
if (at) {
|
|
44
|
+
setOverrides(bindKeys(actions, overrides, { ...at, keys, how }));
|
|
45
|
+
}
|
|
46
|
+
cancel();
|
|
47
|
+
};
|
|
48
|
+
const submit = (raw, allowReserved = false, at = target) => {
|
|
49
|
+
if (!at) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
settle(judge(context, at, raw, allowReserved), at, {
|
|
53
|
+
apply,
|
|
54
|
+
cancel,
|
|
55
|
+
setPending,
|
|
56
|
+
setMessage
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
target,
|
|
61
|
+
pending,
|
|
62
|
+
message,
|
|
63
|
+
begin,
|
|
64
|
+
cancel,
|
|
65
|
+
submit,
|
|
66
|
+
offer: (at, keys) => {
|
|
67
|
+
begin(at);
|
|
68
|
+
submit(keys, false, at);
|
|
69
|
+
},
|
|
70
|
+
resolve: (how) => {
|
|
71
|
+
if (pending?.kind === "taken") {
|
|
72
|
+
apply(target, pending.keys, how);
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
useAnyway: () => {
|
|
76
|
+
if (pending?.kind === "reserved") {
|
|
77
|
+
setPending(null);
|
|
78
|
+
submit(pending.keys, true);
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
remove: (id, slot) => setOverrides(unbindKey(actions, overrides, id, slot)),
|
|
82
|
+
reset: (id) => setOverrides(resetAction(overrides, id))
|
|
83
|
+
};
|
|
84
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type Overrides, type ShortcutAction } from "./bindings";
|
|
2
|
+
/** What the registered shortcuts read on every key press, kept fresh by the provider. */
|
|
3
|
+
export interface Live {
|
|
4
|
+
actions: readonly ShortcutAction[];
|
|
5
|
+
overrides: Overrides;
|
|
6
|
+
pauseWhileBusy: boolean;
|
|
7
|
+
multipleKeys: boolean;
|
|
8
|
+
helpKeys: readonly string[];
|
|
9
|
+
toggleHelp: () => void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Registers one slot per key an action can hold, through ui-sdk. The keys are read on every key
|
|
13
|
+
* press, so rebinding needs no re-registering.
|
|
14
|
+
*/
|
|
15
|
+
export declare function useRegistration(live: React.RefObject<Live>, signature: string): void;
|
|
16
|
+
/** The ids of the actions that could run right now, according to ui-sdk's list. */
|
|
17
|
+
export declare function listAvailable(live: React.RefObject<Live>): Set<string>;
|
|
18
|
+
/** Whether the mod key is held, for the badges. Off unless `enabled`. */
|
|
19
|
+
export declare function useModHeld(enabled: boolean): [boolean, (held: boolean) => void];
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { registerShortcut, shortcuts } from "@ui-sdk/react/shortcuts";
|
|
2
|
+
import { useEffect, useState } from "react";
|
|
3
|
+
import { keysOf, maxKeysPerAction } from "./bindings";
|
|
4
|
+
import { isDragging, isMenuOpen } from "./guard";
|
|
5
|
+
import { canonicalKeys, onApple } from "./keys";
|
|
6
|
+
import { holdOverDialogs, overDialogsLayer } from "./over-dialogs";
|
|
7
|
+
/** Whether page shortcuts may run: not while a drag or a menu is open, unless the app turned that off. */
|
|
8
|
+
function calm(live) {
|
|
9
|
+
return !live.current.pauseWhileBusy || (!isDragging() && !isMenuOpen());
|
|
10
|
+
}
|
|
11
|
+
function registerSlot(live, action, slot) {
|
|
12
|
+
const find = () => live.current.actions.find((item) => item.id === action.id);
|
|
13
|
+
return registerShortcut(() => {
|
|
14
|
+
const current = find();
|
|
15
|
+
return current && (slot === 0 || live.current.multipleKeys)
|
|
16
|
+
? keysOf(current, live.current.overrides)[slot]
|
|
17
|
+
: undefined;
|
|
18
|
+
}, (event) => find()?.run(event), {
|
|
19
|
+
description: action.description,
|
|
20
|
+
allowInInput: action.allowInInput ?? false,
|
|
21
|
+
layer: action.overDialogs ? overDialogsLayer : undefined,
|
|
22
|
+
when: () => calm(live) && (find()?.when?.() ?? true)
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Registers one slot per key an action can hold, through ui-sdk. The keys are read on every key
|
|
27
|
+
* press, so rebinding needs no re-registering.
|
|
28
|
+
*/
|
|
29
|
+
export function useRegistration(live, signature) {
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const offs = [];
|
|
32
|
+
if (live.current.actions.some((action) => action.overDialogs)) {
|
|
33
|
+
offs.push(holdOverDialogs());
|
|
34
|
+
}
|
|
35
|
+
for (const action of live.current.actions) {
|
|
36
|
+
for (let slot = 0; slot < maxKeysPerAction; slot += 1) {
|
|
37
|
+
offs.push(registerSlot(live, action, slot));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
for (const [slot] of live.current.helpKeys.entries()) {
|
|
41
|
+
offs.push(registerShortcut(() => live.current.helpKeys[slot], () => live.current.toggleHelp(), { description: "Show keyboard shortcuts", when: () => calm(live) }));
|
|
42
|
+
}
|
|
43
|
+
return () => {
|
|
44
|
+
for (const off of offs) {
|
|
45
|
+
off();
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}, [live, signature]);
|
|
49
|
+
}
|
|
50
|
+
/** The ids of the actions that could run right now, according to ui-sdk's list. */
|
|
51
|
+
export function listAvailable(live) {
|
|
52
|
+
const listed = shortcuts.list();
|
|
53
|
+
return new Set(live.current.actions
|
|
54
|
+
.filter((action) => listed.some((entry) => entry.description === action.description &&
|
|
55
|
+
keysOf(action, live.current.overrides).includes(canonicalKeys(entry.keys))))
|
|
56
|
+
.map((action) => action.id));
|
|
57
|
+
}
|
|
58
|
+
/** Whether the mod key is held, for the badges. Off unless `enabled`. */
|
|
59
|
+
export function useModHeld(enabled) {
|
|
60
|
+
const [held, setHeld] = useState(false);
|
|
61
|
+
useEffect(() => {
|
|
62
|
+
if (!enabled) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
const modKey = onApple() ? "Meta" : "Control";
|
|
66
|
+
const down = (event) => {
|
|
67
|
+
if (event.key === modKey) {
|
|
68
|
+
setHeld(true);
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const release = () => setHeld(false);
|
|
72
|
+
/* Only the mod key's own release hides the badges, so a key pressed while it is down changes nothing. */
|
|
73
|
+
const up = (event) => {
|
|
74
|
+
if (event.key === modKey) {
|
|
75
|
+
release();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
window.addEventListener("keydown", down);
|
|
79
|
+
window.addEventListener("keyup", up);
|
|
80
|
+
window.addEventListener("blur", release);
|
|
81
|
+
return () => {
|
|
82
|
+
window.removeEventListener("keydown", down);
|
|
83
|
+
window.removeEventListener("keyup", up);
|
|
84
|
+
window.removeEventListener("blur", release);
|
|
85
|
+
setHeld(false);
|
|
86
|
+
};
|
|
87
|
+
}, [enabled]);
|
|
88
|
+
return [enabled && held, setHeld];
|
|
89
|
+
}
|