@alulore/kumiko 0.16.0 → 0.17.0
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/shortcuts/bindings.d.ts +44 -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 +25 -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 +11 -0
- package/dist/shortcuts/index.js +11 -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 +43 -0
- package/dist/shortcuts/notes.d.ts +28 -0
- package/dist/shortcuts/notes.js +27 -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 +84 -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 +88 -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,151 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The pure history logic: entries, merging, the limit and moving steps between the undo and redo
|
|
3
|
+
* sides. No timers, no React, no DOM, so every rule can be unit-tested on its own.
|
|
4
|
+
*/
|
|
5
|
+
export const DEFAULT_LIMIT = 100;
|
|
6
|
+
export const DEFAULT_MERGE_WINDOW = 1200;
|
|
7
|
+
export const EMPTY_HISTORY = { past: [], future: [], sealed: false };
|
|
8
|
+
export const NEWER_FIRST = "Newer steps depend on this one. Undo them first.";
|
|
9
|
+
export const OLDER_FIRST = "Older steps come first. Redo them first.";
|
|
10
|
+
/* A copy of the list, last item first. */
|
|
11
|
+
export function reversed(items) {
|
|
12
|
+
return items.reduceRight((result, item) => {
|
|
13
|
+
result.push(item);
|
|
14
|
+
return result;
|
|
15
|
+
}, []);
|
|
16
|
+
}
|
|
17
|
+
function capped(entries, limit) {
|
|
18
|
+
const allowed = Math.max(1, limit);
|
|
19
|
+
return entries.length > allowed ? entries.slice(entries.length - allowed) : entries;
|
|
20
|
+
}
|
|
21
|
+
function joins(last, entry, history, windowMs) {
|
|
22
|
+
return (!history.sealed &&
|
|
23
|
+
entry.mergeKey !== undefined &&
|
|
24
|
+
last.mergeKey === entry.mergeKey &&
|
|
25
|
+
last.place === entry.place &&
|
|
26
|
+
entry.at - last.at <= windowMs);
|
|
27
|
+
}
|
|
28
|
+
function visible(entry, place) {
|
|
29
|
+
return place === undefined || entry.place === undefined || entry.place === place;
|
|
30
|
+
}
|
|
31
|
+
/* The steps a place can see. Without a place, all of them. */
|
|
32
|
+
export function inPlace(entries, place) {
|
|
33
|
+
return entries.filter((entry) => visible(entry, place));
|
|
34
|
+
}
|
|
35
|
+
function futureAfter(future, place, own) {
|
|
36
|
+
return own ? future.filter((entry) => !visible(entry, place)) : [];
|
|
37
|
+
}
|
|
38
|
+
/*
|
|
39
|
+
* Adds a step and drops the redo side (only the steps its place can see, with `ownFuture`). A
|
|
40
|
+
* step that joins the last one keeps the first undo and the first id, and takes the latest redo,
|
|
41
|
+
* label and target.
|
|
42
|
+
*/
|
|
43
|
+
export function remember(history, entry, options) {
|
|
44
|
+
const stamped = { ...entry, id: options.id, at: options.now };
|
|
45
|
+
const future = futureAfter(history.future, entry.place, options.ownFuture);
|
|
46
|
+
const last = history.past.at(-1);
|
|
47
|
+
if (last && joins(last, stamped, history, options.windowMs)) {
|
|
48
|
+
const merged = {
|
|
49
|
+
...last,
|
|
50
|
+
label: stamped.label,
|
|
51
|
+
redo: stamped.redo,
|
|
52
|
+
target: stamped.target ?? last.target,
|
|
53
|
+
at: stamped.at
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
history: { past: [...history.past.slice(0, -1), merged], future, sealed: false },
|
|
57
|
+
entry: merged
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
history: {
|
|
62
|
+
past: capped([...history.past, stamped], options.limit),
|
|
63
|
+
future,
|
|
64
|
+
sealed: false
|
|
65
|
+
},
|
|
66
|
+
entry: stamped
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/* Closes the last step, so the next one starts fresh even with the same merge key. */
|
|
70
|
+
export function seal(history) {
|
|
71
|
+
return history.sealed ? history : { ...history, sealed: true };
|
|
72
|
+
}
|
|
73
|
+
export function nextUndo(history, place) {
|
|
74
|
+
return inPlace(history.past, place).at(-1) ?? null;
|
|
75
|
+
}
|
|
76
|
+
export function nextRedo(history, place) {
|
|
77
|
+
return inPlace(history.future, place).at(-1) ?? null;
|
|
78
|
+
}
|
|
79
|
+
/* Moves a step from the undo side to the redo side. */
|
|
80
|
+
export function markUndone(history, id) {
|
|
81
|
+
const entry = history.past.find((item) => item.id === id);
|
|
82
|
+
if (!entry) {
|
|
83
|
+
return history;
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
past: history.past.filter((item) => item.id !== id),
|
|
87
|
+
future: [...history.future, entry],
|
|
88
|
+
sealed: true
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/* Moves a step from the redo side back to the undo side, as the newest step. */
|
|
92
|
+
export function markRedone(history, id) {
|
|
93
|
+
const entry = history.future.find((item) => item.id === id);
|
|
94
|
+
if (!entry) {
|
|
95
|
+
return history;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
past: [...history.past, entry],
|
|
99
|
+
future: history.future.filter((item) => item.id !== id),
|
|
100
|
+
sealed: true
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
/* Drops a step from the undo side for good, as an out-of-order undo does. */
|
|
104
|
+
export function forget(history, id) {
|
|
105
|
+
return { ...history, past: history.past.filter((item) => item.id !== id), sealed: true };
|
|
106
|
+
}
|
|
107
|
+
/* Puts a dropped step back as the newest one, without touching the redo side. */
|
|
108
|
+
export function reinstate(history, entry) {
|
|
109
|
+
return { ...history, past: [...history.past, entry], sealed: true };
|
|
110
|
+
}
|
|
111
|
+
export function isOnTop(history, id, place) {
|
|
112
|
+
return nextUndo(history, place)?.id === id;
|
|
113
|
+
}
|
|
114
|
+
/* Why a step can't be undone on its own right now, or null when it can. */
|
|
115
|
+
export function blockedUndo(history, id) {
|
|
116
|
+
const index = history.past.findIndex((item) => item.id === id);
|
|
117
|
+
const entry = history.past[index];
|
|
118
|
+
if (!entry?.orderKey) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
const newer = history.past.slice(index + 1);
|
|
122
|
+
return newer.some((item) => item.orderKey === entry.orderKey) ? NEWER_FIRST : null;
|
|
123
|
+
}
|
|
124
|
+
/* Why a step can't be redone on its own right now, or null when it can. */
|
|
125
|
+
export function blockedRedo(history, id) {
|
|
126
|
+
const index = history.future.findIndex((item) => item.id === id);
|
|
127
|
+
const entry = history.future[index];
|
|
128
|
+
if (!entry?.orderKey) {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
const first = history.future.slice(index + 1);
|
|
132
|
+
return first.some((item) => item.orderKey === entry.orderKey) ? OLDER_FIRST : null;
|
|
133
|
+
}
|
|
134
|
+
/*
|
|
135
|
+
* The steps to undo to get back to just after step `id` (or to the very start for null), newest
|
|
136
|
+
* first. With `include`, the step itself is undone too.
|
|
137
|
+
*/
|
|
138
|
+
export function stepsBack(history, id, include, place) {
|
|
139
|
+
const seen = inPlace(history.past, place);
|
|
140
|
+
const index = id === null ? -1 : seen.findIndex((item) => item.id === id);
|
|
141
|
+
if (id !== null && index === -1) {
|
|
142
|
+
return [];
|
|
143
|
+
}
|
|
144
|
+
return reversed(seen.slice(include && id !== null ? index : index + 1));
|
|
145
|
+
}
|
|
146
|
+
/* The steps to redo to get forward to step `id`, oldest first. */
|
|
147
|
+
export function stepsForward(history, id, place) {
|
|
148
|
+
const seen = reversed(inPlace(history.future, place));
|
|
149
|
+
const index = seen.findIndex((item) => item.id === id);
|
|
150
|
+
return index === -1 ? [] : seen.slice(0, index + 1);
|
|
151
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { DEFAULT_LIMIT, DEFAULT_MERGE_WINDOW, type History, type NewUndoEntry, type UndoEntry } from "./history";
|
|
2
|
+
export { joinHistory, useUndoAdapter, type UndoAdapter } from "./undo-adapter";
|
|
3
|
+
export { feedbackHandlers, showUndoToast, type FeedbackActions, type UndoToastTarget } from "./undo-feedback";
|
|
4
|
+
export { isTypingTarget, undoFieldKeys, useUndoKeys, type UndoKeysOptions } from "./undo-keys";
|
|
5
|
+
export { DEFAULT_OPTIONS, type BusyMode, type Direction, type FeedbackMode, type KeyReach, type PlaceMode, type RevealMode, type RunInfo, type ToastUndoMode, type UndoHandlers, type UndoOptions } from "./undo-options";
|
|
6
|
+
export { UndoButtons, UndoPanel, type PanelMode, type UndoPanelProps } from "./undo-panel";
|
|
7
|
+
export { UndoProvider, useUndo, type UndoApi, type UndoProviderProps } from "./undo-provider";
|
|
8
|
+
export { reveal, TARGET_ATTRIBUTE } from "./undo-reveal";
|
|
9
|
+
export { createUndoStore, UndoStore, type UndoDeps, type UndoScope, type UndoSnapshot } from "./undo-store";
|
|
10
|
+
export { useUndoValue, type UndoHistory, type UndoValue, type UndoValueOptions } from "./use-undo-value";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { DEFAULT_LIMIT, DEFAULT_MERGE_WINDOW } from "./history";
|
|
2
|
+
export { joinHistory, useUndoAdapter } from "./undo-adapter";
|
|
3
|
+
export { feedbackHandlers, showUndoToast } from "./undo-feedback";
|
|
4
|
+
export { isTypingTarget, undoFieldKeys, useUndoKeys } from "./undo-keys";
|
|
5
|
+
export { DEFAULT_OPTIONS } from "./undo-options";
|
|
6
|
+
export { UndoButtons, UndoPanel } from "./undo-panel";
|
|
7
|
+
export { UndoProvider, useUndo } from "./undo-provider";
|
|
8
|
+
export { reveal, TARGET_ATTRIBUTE } from "./undo-reveal";
|
|
9
|
+
export { createUndoStore, UndoStore } from "./undo-store";
|
|
10
|
+
export { useUndoValue } from "./use-undo-value";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { NewUndoEntry } from "./history";
|
|
2
|
+
/**
|
|
3
|
+
* What Kumiko needs from an editor that keeps its own undo, like a collaborative text editor.
|
|
4
|
+
* It is structural, so Yjs's UndoManager (or anything else) fits behind a few lines of glue and
|
|
5
|
+
* Kumiko never depends on it.
|
|
6
|
+
*/
|
|
7
|
+
export interface UndoAdapter {
|
|
8
|
+
/** Undoes the editor's latest step. For a collaborative editor, only this person's own. */
|
|
9
|
+
undo: () => void;
|
|
10
|
+
redo: () => void;
|
|
11
|
+
canUndo: () => boolean;
|
|
12
|
+
canRedo: () => boolean;
|
|
13
|
+
/** Calls back whenever the editor's undo or redo side changes. Returns an unsubscribe. */
|
|
14
|
+
onChange: (listener: () => void) => () => void;
|
|
15
|
+
/** Calls back when a new step of its own was made (not an undo or redo). Returns an unsubscribe. */
|
|
16
|
+
onStep?: (listener: () => void) => () => void;
|
|
17
|
+
}
|
|
18
|
+
/** The adapter's buttons state, kept current. */
|
|
19
|
+
export declare function useUndoAdapter(adapter: UndoAdapter): {
|
|
20
|
+
canUndo: boolean;
|
|
21
|
+
canRedo: boolean;
|
|
22
|
+
undo: () => void;
|
|
23
|
+
redo: () => void;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Folds an editor's own steps into the page's history: every new step the editor makes is
|
|
27
|
+
* remembered as one entry that calls the editor's undo and redo. Returns a function that stops.
|
|
28
|
+
*/
|
|
29
|
+
export declare function joinHistory(history: {
|
|
30
|
+
remember: (entry: NewUndoEntry) => unknown;
|
|
31
|
+
}, adapter: UndoAdapter, options: {
|
|
32
|
+
label: string;
|
|
33
|
+
place?: string;
|
|
34
|
+
target?: string;
|
|
35
|
+
}): () => void;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { useSyncExternalStore } from "react";
|
|
2
|
+
let adapters = 0;
|
|
3
|
+
const adapterKeys = new WeakMap();
|
|
4
|
+
function keyOf(adapter) {
|
|
5
|
+
const known = adapterKeys.get(adapter);
|
|
6
|
+
if (known) {
|
|
7
|
+
return known;
|
|
8
|
+
}
|
|
9
|
+
adapters += 1;
|
|
10
|
+
const key = `adapter-${adapters}`;
|
|
11
|
+
adapterKeys.set(adapter, key);
|
|
12
|
+
return key;
|
|
13
|
+
}
|
|
14
|
+
/** The adapter's buttons state, kept current. */
|
|
15
|
+
export function useUndoAdapter(adapter) {
|
|
16
|
+
const canUndo = useSyncExternalStore(adapter.onChange, adapter.canUndo);
|
|
17
|
+
const canRedo = useSyncExternalStore(adapter.onChange, adapter.canRedo);
|
|
18
|
+
return { canUndo, canRedo, undo: adapter.undo, redo: adapter.redo };
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Folds an editor's own steps into the page's history: every new step the editor makes is
|
|
22
|
+
* remembered as one entry that calls the editor's undo and redo. Returns a function that stops.
|
|
23
|
+
*/
|
|
24
|
+
export function joinHistory(history, adapter, options) {
|
|
25
|
+
return (adapter.onStep?.(() => {
|
|
26
|
+
history.remember({
|
|
27
|
+
orderKey: keyOf(adapter),
|
|
28
|
+
label: options.label,
|
|
29
|
+
place: options.place,
|
|
30
|
+
target: options.target,
|
|
31
|
+
undo: () => adapter.undo(),
|
|
32
|
+
redo: () => adapter.redo()
|
|
33
|
+
});
|
|
34
|
+
}) ?? (() => undefined));
|
|
35
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { UndoEntry } from "./history";
|
|
2
|
+
import type { FeedbackMode, RunInfo, ToastUndoMode, UndoHandlers } from "./undo-options";
|
|
3
|
+
/** What a toast's button can ask for: reverse the step it names, even after other steps ran. */
|
|
4
|
+
export interface FeedbackActions {
|
|
5
|
+
reverseUndo: (entry: UndoEntry, info: RunInfo) => unknown;
|
|
6
|
+
reverseRedo: (entry: UndoEntry) => unknown;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Handlers that tell the person what happened. "toast" shows "Undone: Move card", "toast-reverse"
|
|
10
|
+
* adds a button that reverses that step, and "quiet" shows nothing. Screen readers hear every
|
|
11
|
+
* mode: a toast announces itself, and "quiet" announces by hand. A failed or refused step always
|
|
12
|
+
* shows a toast.
|
|
13
|
+
*/
|
|
14
|
+
export declare function feedbackHandlers(actions: FeedbackActions, mode: FeedbackMode): UndoHandlers;
|
|
15
|
+
/** What `showUndoToast` needs from the history. `useUndo()` has all of it. */
|
|
16
|
+
export interface UndoToastTarget {
|
|
17
|
+
undo: () => unknown;
|
|
18
|
+
undoEntry: (id: string) => unknown;
|
|
19
|
+
rewindTo: (id: string | null, include?: boolean) => unknown;
|
|
20
|
+
isLatest: (id: string) => boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Shows a toast with an Undo button for a step that just happened, like a delete. What the
|
|
24
|
+
* button does when newer steps exist depends on the mode: undo just that step and drop it from
|
|
25
|
+
* the history ("step"), work only while it is still the latest step ("latest"), or rewind
|
|
26
|
+
* everything back to it ("rewind").
|
|
27
|
+
*/
|
|
28
|
+
export declare function showUndoToast(target: UndoToastTarget, mode: ToastUndoMode, message: string, entry: UndoEntry): void;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { announce } from "@ui-sdk/core";
|
|
2
|
+
import { toast } from "../toast";
|
|
3
|
+
const TOAST_MS = 2600;
|
|
4
|
+
const DELETE_TOAST_MS = 6000;
|
|
5
|
+
function describe(verb, entry, info) {
|
|
6
|
+
return info.steps > 1 ? `${verb} ${info.steps} steps` : `${verb}: ${entry.label}`;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Handlers that tell the person what happened. "toast" shows "Undone: Move card", "toast-reverse"
|
|
10
|
+
* adds a button that reverses that step, and "quiet" shows nothing. Screen readers hear every
|
|
11
|
+
* mode: a toast announces itself, and "quiet" announces by hand. A failed or refused step always
|
|
12
|
+
* shows a toast.
|
|
13
|
+
*/
|
|
14
|
+
export function feedbackHandlers(actions, mode) {
|
|
15
|
+
const say = (text, info, reverse, action) => {
|
|
16
|
+
if (mode === "quiet") {
|
|
17
|
+
announce(text);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const button = mode === "toast-reverse" && info.steps === 1;
|
|
21
|
+
toast(text, {
|
|
22
|
+
duration: TOAST_MS,
|
|
23
|
+
action: button ? { label: reverse, onAction: () => void action() } : undefined
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
onUndo: (entry, info) => {
|
|
28
|
+
if (info.last) {
|
|
29
|
+
say(describe("Undone", entry, info), info, "Redo", () => actions.reverseUndo(entry, info));
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
onRedo: (entry, info) => {
|
|
33
|
+
if (info.last) {
|
|
34
|
+
say(describe("Redone", entry, info), info, "Undo", () => actions.reverseRedo(entry));
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
onError: (_error, entry, direction) => {
|
|
38
|
+
const text = `Couldn't ${direction} ${entry.label}. Try again?`;
|
|
39
|
+
toast.error(text);
|
|
40
|
+
},
|
|
41
|
+
onBlocked: (_entry, reason) => {
|
|
42
|
+
toast.info(reason);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Shows a toast with an Undo button for a step that just happened, like a delete. What the
|
|
48
|
+
* button does when newer steps exist depends on the mode: undo just that step and drop it from
|
|
49
|
+
* the history ("step"), work only while it is still the latest step ("latest"), or rewind
|
|
50
|
+
* everything back to it ("rewind").
|
|
51
|
+
*/
|
|
52
|
+
export function showUndoToast(target, mode, message, entry) {
|
|
53
|
+
const onAction = () => {
|
|
54
|
+
if (mode === "rewind") {
|
|
55
|
+
void target.rewindTo(entry.id, true);
|
|
56
|
+
}
|
|
57
|
+
else if (mode === "step") {
|
|
58
|
+
void target.undoEntry(entry.id);
|
|
59
|
+
}
|
|
60
|
+
else if (target.isLatest(entry.id)) {
|
|
61
|
+
void target.undo();
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
const text = "Newer steps came after this one.";
|
|
65
|
+
toast.info(text);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
toast(message, { duration: DELETE_TOAST_MS, action: { label: "Undo", onAction } });
|
|
69
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type RefObject } from "react";
|
|
2
|
+
import type { KeyReach } from "./undo-options";
|
|
3
|
+
export interface UndoKeysOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Where the keys work: anywhere in the window, or only while focus is inside `area`.
|
|
6
|
+
* @defaultValue "window"
|
|
7
|
+
*/
|
|
8
|
+
reach?: KeyReach;
|
|
9
|
+
area?: RefObject<Element | null>;
|
|
10
|
+
blocked?: () => boolean;
|
|
11
|
+
/**
|
|
12
|
+
* Turns the keys off.
|
|
13
|
+
* @defaultValue true
|
|
14
|
+
*/
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** Whether a target is somewhere the browser has its own undo. */
|
|
18
|
+
export declare function isTypingTarget(target: EventTarget | null): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Registers mod+z, mod+shift+z and mod+y through ui-sdk's shortcuts. They stay quiet in text
|
|
21
|
+
* fields (the browser's own undo keeps working there), while a dialog or menu is open, while a
|
|
22
|
+
* drag runs, and, when reach is "area", while focus is outside the area.
|
|
23
|
+
*/
|
|
24
|
+
export declare function useUndoKeys(actions: {
|
|
25
|
+
undo: () => unknown;
|
|
26
|
+
redo: () => unknown;
|
|
27
|
+
}, options: UndoKeysOptions): void;
|
|
28
|
+
/**
|
|
29
|
+
* A key handler for a text field that joins the history: mod+z and redo reach the history
|
|
30
|
+
* instead of the browser's own undo.
|
|
31
|
+
*/
|
|
32
|
+
export declare function undoFieldKeys(event: KeyboardEvent | React.KeyboardEvent, undo: () => void, redo: () => void): void;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { matchesShortcut } from "@ui-sdk/core";
|
|
2
|
+
import { useShortcut } from "@ui-sdk/react/shortcuts";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
/** Whether a target is somewhere the browser has its own undo. */
|
|
5
|
+
export function isTypingTarget(target) {
|
|
6
|
+
return (target instanceof HTMLElement &&
|
|
7
|
+
(target.isContentEditable ||
|
|
8
|
+
target.tagName === "INPUT" ||
|
|
9
|
+
target.tagName === "TEXTAREA" ||
|
|
10
|
+
target.tagName === "SELECT"));
|
|
11
|
+
}
|
|
12
|
+
/* A dialog, an alert dialog or a menu is open, and has the keyboard. */
|
|
13
|
+
function surfaceOpen() {
|
|
14
|
+
return document.querySelector('[role="dialog"], [role="alertdialog"], [role="menu"]') !== null;
|
|
15
|
+
}
|
|
16
|
+
/* The area last touched, so keys keep working after a click leaves focus on the page body. */
|
|
17
|
+
let lastArea = null;
|
|
18
|
+
function allowed(options) {
|
|
19
|
+
const active = document.activeElement;
|
|
20
|
+
if (options.enabled === false || isTypingTarget(active) || surfaceOpen()) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
if (options.blocked?.() === true) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (options.reach === "area") {
|
|
27
|
+
const area = options.area?.current;
|
|
28
|
+
if (!area) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return area.contains(active) || (active === document.body && lastArea === area);
|
|
32
|
+
}
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Registers mod+z, mod+shift+z and mod+y through ui-sdk's shortcuts. They stay quiet in text
|
|
37
|
+
* fields (the browser's own undo keeps working there), while a dialog or menu is open, while a
|
|
38
|
+
* drag runs, and, when reach is "area", while focus is outside the area.
|
|
39
|
+
*/
|
|
40
|
+
export function useUndoKeys(actions, options) {
|
|
41
|
+
const latest = useRef(options);
|
|
42
|
+
latest.current = options;
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
const note = (event) => {
|
|
45
|
+
const area = latest.current.area?.current;
|
|
46
|
+
if (area && event.target instanceof Node && area.contains(event.target)) {
|
|
47
|
+
lastArea = area;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
document.addEventListener("pointerdown", note, true);
|
|
51
|
+
document.addEventListener("focusin", note, true);
|
|
52
|
+
return () => {
|
|
53
|
+
document.removeEventListener("pointerdown", note, true);
|
|
54
|
+
document.removeEventListener("focusin", note, true);
|
|
55
|
+
};
|
|
56
|
+
}, []);
|
|
57
|
+
const when = () => allowed(latest.current);
|
|
58
|
+
useShortcut("mod+z", () => void actions.undo(), { when, description: "Undo" });
|
|
59
|
+
useShortcut("mod+shift+z", () => void actions.redo(), { when, description: "Redo" });
|
|
60
|
+
useShortcut("mod+y", () => void actions.redo(), { when, description: "Redo" });
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A key handler for a text field that joins the history: mod+z and redo reach the history
|
|
64
|
+
* instead of the browser's own undo.
|
|
65
|
+
*/
|
|
66
|
+
export function undoFieldKeys(event, undo, redo) {
|
|
67
|
+
const native = "nativeEvent" in event ? event.nativeEvent : event;
|
|
68
|
+
if (matchesShortcut(native, "mod+z")) {
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
undo();
|
|
71
|
+
}
|
|
72
|
+
else if (matchesShortcut(native, "mod+shift+z") || matchesShortcut(native, "mod+y")) {
|
|
73
|
+
event.preventDefault();
|
|
74
|
+
redo();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { UndoEntry } from "./history";
|
|
2
|
+
export type PlaceMode = "jump" | "own" | "stay";
|
|
3
|
+
export type BusyMode = "ignore" | "queue";
|
|
4
|
+
export type RevealMode = "stay" | "focus" | "flash";
|
|
5
|
+
export type FeedbackMode = "toast" | "toast-reverse" | "quiet";
|
|
6
|
+
export type ToastUndoMode = "step" | "latest" | "rewind";
|
|
7
|
+
export type KeyReach = "window" | "area";
|
|
8
|
+
/** The settings that shape how the history behaves. Read live, so changing one never resets it. */
|
|
9
|
+
export interface UndoOptions {
|
|
10
|
+
/**
|
|
11
|
+
* How long after a step a step with the same merge key still joins it. Infinity joins until
|
|
12
|
+
* the history is sealed.
|
|
13
|
+
* @defaultValue 1200
|
|
14
|
+
*/
|
|
15
|
+
mergeWindow: number;
|
|
16
|
+
/**
|
|
17
|
+
* How many steps are kept. Infinity keeps all.
|
|
18
|
+
* @defaultValue 100
|
|
19
|
+
*/
|
|
20
|
+
limit: number;
|
|
21
|
+
/**
|
|
22
|
+
* Pressing undo while one runs: dropped, or run afterwards in order.
|
|
23
|
+
* @defaultValue "ignore"
|
|
24
|
+
*/
|
|
25
|
+
busy: BusyMode;
|
|
26
|
+
/**
|
|
27
|
+
* A step from another place: jump there first, keep one history per place, or undo in place.
|
|
28
|
+
* @defaultValue "jump"
|
|
29
|
+
*/
|
|
30
|
+
place: PlaceMode;
|
|
31
|
+
}
|
|
32
|
+
export declare const DEFAULT_OPTIONS: UndoOptions;
|
|
33
|
+
/** Which direction a step ran. */
|
|
34
|
+
export type Direction = "undo" | "redo";
|
|
35
|
+
/** Tells a handler where a step sits in a run of several, like a rewind. */
|
|
36
|
+
export interface RunInfo {
|
|
37
|
+
/** How many steps the run has. */
|
|
38
|
+
steps: number;
|
|
39
|
+
/** Whether this is the run's last step. */
|
|
40
|
+
last: boolean;
|
|
41
|
+
/** Whether the step was undone out of order, so it left the history instead of moving to the redo side. */
|
|
42
|
+
dropped: boolean;
|
|
43
|
+
}
|
|
44
|
+
/** What an app can listen to. All are optional. */
|
|
45
|
+
export interface UndoHandlers {
|
|
46
|
+
onUndo?: (entry: UndoEntry, info: RunInfo) => void;
|
|
47
|
+
onRedo?: (entry: UndoEntry, info: RunInfo) => void;
|
|
48
|
+
onError?: (error: unknown, entry: UndoEntry, direction: Direction) => void;
|
|
49
|
+
onBlocked?: (entry: UndoEntry, reason: string) => void;
|
|
50
|
+
onPlace?: (place: string, entry: UndoEntry) => unknown;
|
|
51
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export type PanelMode = "rewind" | "steps" | "buttons";
|
|
2
|
+
export interface UndoPanelProps {
|
|
3
|
+
/**
|
|
4
|
+
* How the list works: click a step to rewind to it, a button on each step, or no list.
|
|
5
|
+
* @defaultValue "rewind"
|
|
6
|
+
*/
|
|
7
|
+
mode?: PanelMode;
|
|
8
|
+
/**
|
|
9
|
+
* The heading above the list.
|
|
10
|
+
* @defaultValue "History"
|
|
11
|
+
*/
|
|
12
|
+
title?: string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Undo and Redo buttons. With `named`, the next step's label shows beside them and in the
|
|
16
|
+
* tooltips. While a step runs they lock (`aria-disabled`, taps ignored, `data-busy` on the root),
|
|
17
|
+
* and a spinner shows if the step takes longer than `--kumiko-undo-spinner-delay`.
|
|
18
|
+
*/
|
|
19
|
+
export declare function UndoButtons({ named }: {
|
|
20
|
+
named?: boolean;
|
|
21
|
+
}): React.JSX.Element;
|
|
22
|
+
/** The history as a list of labelled steps. Its controls lock while a step runs. */
|
|
23
|
+
export declare function UndoPanel({ mode, title }: UndoPanelProps): React.JSX.Element | null;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useShortcutHint } from "@ui-sdk/react/shortcuts";
|
|
3
|
+
import { reversed } from "./history";
|
|
4
|
+
import { useUndo } from "./undo-provider";
|
|
5
|
+
/* Taps are ignored while a step runs. aria-disabled keeps focus on the button. */
|
|
6
|
+
const lockedProps = (off) => ({
|
|
7
|
+
"aria-disabled": off ? "true" : undefined
|
|
8
|
+
});
|
|
9
|
+
function Spinner() {
|
|
10
|
+
return _jsx("span", { "data-kumiko-undo": "spinner", "aria-hidden": "true" });
|
|
11
|
+
}
|
|
12
|
+
function Control(props) {
|
|
13
|
+
const { label, off, busy, keys, step, named, onTap } = props;
|
|
14
|
+
const title = named && step ? `${label}: ${step.label}` : label;
|
|
15
|
+
return (_jsx("button", { type: "button", ...lockedProps(off), "aria-busy": busy, "aria-keyshortcuts": keys.ariaKeyshortcuts, title: `${title} (${keys.text})`, onClick: () => {
|
|
16
|
+
if (!off) {
|
|
17
|
+
onTap();
|
|
18
|
+
}
|
|
19
|
+
}, children: label }));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Undo and Redo buttons. With `named`, the next step's label shows beside them and in the
|
|
23
|
+
* tooltips. While a step runs they lock (`aria-disabled`, taps ignored, `data-busy` on the root),
|
|
24
|
+
* and a spinner shows if the step takes longer than `--kumiko-undo-spinner-delay`.
|
|
25
|
+
*/
|
|
26
|
+
export function UndoButtons({ named = false }) {
|
|
27
|
+
const { undo, redo, canUndo, canRedo, entries, future, busy, locked } = useUndo();
|
|
28
|
+
const undoKeys = useShortcutHint("mod+z");
|
|
29
|
+
const redoKeys = useShortcutHint("mod+shift+z");
|
|
30
|
+
const next = entries.at(-1);
|
|
31
|
+
return (_jsxs("span", { "data-kumiko-undo": "buttons", "data-busy": busy ? "" : undefined, children: [named ? (_jsx("span", { "data-kumiko-undo": "next", children: next ? `Next undo: ${next.label}` : "Nothing to undo" })) : null, _jsx(Control, { label: "Undo", off: !canUndo || locked, busy: busy, keys: undoKeys, step: next, named: named, onTap: () => void undo() }), _jsx(Control, { label: "Redo", off: !canRedo || locked, busy: busy, keys: redoKeys, step: future.at(-1), named: named, onTap: () => void redo() }), busy ? _jsx(Spinner, {}) : null] }));
|
|
32
|
+
}
|
|
33
|
+
function Step({ entry, mode, state }) {
|
|
34
|
+
const { rewindTo, undoEntry, locked } = useUndo();
|
|
35
|
+
const run = (action) => {
|
|
36
|
+
if (!locked) {
|
|
37
|
+
void action();
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
if (mode === "rewind") {
|
|
41
|
+
return (_jsx("li", { "data-kumiko-undo-step": state, children: _jsx("button", { type: "button", ...lockedProps(locked), onClick: () => run(() => rewindTo(entry.id)), children: entry.label }) }));
|
|
42
|
+
}
|
|
43
|
+
return (_jsxs("li", { "data-kumiko-undo-step": state, children: [_jsx("span", { children: entry.label }), state === "undone" ? null : (_jsx("button", { type: "button", ...lockedProps(locked), onClick: () => run(() => undoEntry(entry.id)), children: "Undo" }))] }));
|
|
44
|
+
}
|
|
45
|
+
/** The history as a list of labelled steps. Its controls lock while a step runs. */
|
|
46
|
+
export function UndoPanel({ mode = "rewind", title = "History" }) {
|
|
47
|
+
const { entries, future, rewindTo, busy, locked } = useUndo();
|
|
48
|
+
if (mode === "buttons") {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return (_jsxs("section", { "data-kumiko-undo": "panel", "aria-label": title, "data-busy": busy ? "" : undefined, children: [_jsx("h3", { children: title }), _jsxs("ol", { children: [mode === "rewind" ? (_jsx("li", { children: _jsx("button", { type: "button", ...lockedProps(locked), onClick: () => {
|
|
52
|
+
if (!locked) {
|
|
53
|
+
void rewindTo(null);
|
|
54
|
+
}
|
|
55
|
+
}, children: "Start" }) })) : null, entries.map((entry, index) => (_jsx(Step, { entry: entry, mode: mode, state: index === entries.length - 1 ? "latest" : "past" }, entry.id))), reversed(future).map((entry) => (_jsx(Step, { entry: entry, mode: mode, state: "undone" }, entry.id)))] }), busy ? _jsx(Spinner, {}) : null] }));
|
|
56
|
+
}
|