@alulore/kumiko 0.15.1 → 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/media-viewer/icons.d.ts +4 -0
- package/dist/media-viewer/icons.js +18 -0
- package/dist/media-viewer/index.d.ts +4 -0
- package/dist/media-viewer/index.js +3 -0
- package/dist/media-viewer/media-slide.d.ts +14 -0
- package/dist/media-viewer/media-slide.js +67 -0
- package/dist/media-viewer/media-viewer.css +611 -0
- package/dist/media-viewer/media-viewer.d.ts +17 -0
- package/dist/media-viewer/media-viewer.js +241 -0
- package/dist/media-viewer/pager-drag.d.ts +10 -0
- package/dist/media-viewer/pager-drag.js +53 -0
- package/dist/media-viewer/types.d.ts +58 -0
- package/dist/media-viewer/types.js +17 -0
- package/dist/media-viewer/video-controls.d.ts +3 -0
- package/dist/media-viewer/video-controls.js +62 -0
- package/dist/media-viewer/view-gesture.d.ts +74 -0
- package/dist/media-viewer/view-gesture.js +465 -0
- package/dist/media-viewer/viewer-chrome.d.ts +13 -0
- package/dist/media-viewer/viewer-chrome.js +35 -0
- package/dist/media-viewer/viewer-motion.d.ts +9 -0
- package/dist/media-viewer/viewer-motion.js +126 -0
- package/dist/media-viewer/zoom-pan.d.ts +35 -0
- package/dist/media-viewer/zoom-pan.js +102 -0
- 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/titlebar/index.d.ts +2 -0
- package/dist/titlebar/index.js +2 -0
- package/dist/titlebar/titlebar-looks.d.ts +53 -0
- package/dist/titlebar/titlebar-looks.js +26 -0
- package/dist/titlebar/titlebar.css +561 -0
- package/dist/titlebar/titlebar.d.ts +54 -0
- package/dist/titlebar/titlebar.js +68 -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 +42 -1
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { type ReactNode, type RefObject } from "react";
|
|
2
|
+
import type { NewUndoEntry, UndoEntry } from "./history";
|
|
3
|
+
import type { FeedbackMode, KeyReach, RevealMode, UndoHandlers, UndoOptions } from "./undo-options";
|
|
4
|
+
import { type UndoScope, type UndoStore } from "./undo-store";
|
|
5
|
+
/** What `useUndo` gives a component. */
|
|
6
|
+
export interface UndoApi {
|
|
7
|
+
store: UndoStore;
|
|
8
|
+
/** What this provider asks the store as: its place and its handlers. */
|
|
9
|
+
scope: UndoScope;
|
|
10
|
+
/** The steps this place can undo, oldest first. */
|
|
11
|
+
entries: readonly UndoEntry[];
|
|
12
|
+
/** The steps this place can redo, most recently undone last. */
|
|
13
|
+
future: readonly UndoEntry[];
|
|
14
|
+
canUndo: boolean;
|
|
15
|
+
canRedo: boolean;
|
|
16
|
+
/** Whether an undo or redo is running or waiting, anywhere on the store. */
|
|
17
|
+
busy: boolean;
|
|
18
|
+
/** Whether undo and redo controls should ignore taps: a step runs and extra taps are dropped. */
|
|
19
|
+
locked: boolean;
|
|
20
|
+
place: string | undefined;
|
|
21
|
+
/** Stores a step and returns the stored entry. */
|
|
22
|
+
remember: (entry: NewUndoEntry) => UndoEntry;
|
|
23
|
+
undo: () => Promise<void>;
|
|
24
|
+
redo: () => Promise<void>;
|
|
25
|
+
undoEntry: (id: string) => Promise<void>;
|
|
26
|
+
redoEntry: (id: string) => Promise<void>;
|
|
27
|
+
rewindTo: (id: string | null, include?: boolean) => Promise<void>;
|
|
28
|
+
isLatest: (id: string) => boolean;
|
|
29
|
+
seal: () => void;
|
|
30
|
+
}
|
|
31
|
+
export interface UndoProviderProps extends UndoHandlers {
|
|
32
|
+
/**
|
|
33
|
+
* A store to share between providers. Without one the provider makes its own. A shared store
|
|
34
|
+
* keeps its own options: set them with `createUndoStore` or `store.setOptions`.
|
|
35
|
+
* @defaultValue undefined, so the provider makes its own
|
|
36
|
+
*/
|
|
37
|
+
store?: UndoStore;
|
|
38
|
+
/**
|
|
39
|
+
* Merge window, limit, busy and place behaviour, for the store this provider makes. Changing
|
|
40
|
+
* them keeps the history.
|
|
41
|
+
* @defaultValue the defaults of `UndoOptions`
|
|
42
|
+
*/
|
|
43
|
+
options?: Partial<UndoOptions>;
|
|
44
|
+
/**
|
|
45
|
+
* The place on screen (a board, a document), for step places and the "own" place mode.
|
|
46
|
+
* @defaultValue undefined, so no place
|
|
47
|
+
*/
|
|
48
|
+
place?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Whether the shortcuts are on.
|
|
51
|
+
* @defaultValue true
|
|
52
|
+
*/
|
|
53
|
+
keys?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Where the shortcuts work: anywhere in the window, or only inside `area`.
|
|
56
|
+
* @defaultValue "window"
|
|
57
|
+
*/
|
|
58
|
+
keyReach?: KeyReach;
|
|
59
|
+
/**
|
|
60
|
+
* The area the shortcuts reach when `keyReach` is "area".
|
|
61
|
+
* @defaultValue undefined
|
|
62
|
+
*/
|
|
63
|
+
area?: RefObject<Element | null>;
|
|
64
|
+
/**
|
|
65
|
+
* Return true while a drag runs, so the shortcuts stay quiet.
|
|
66
|
+
* @defaultValue undefined, so never
|
|
67
|
+
*/
|
|
68
|
+
blocked?: () => boolean;
|
|
69
|
+
/**
|
|
70
|
+
* How the changed item shows after undo or redo.
|
|
71
|
+
* @defaultValue "flash"
|
|
72
|
+
*/
|
|
73
|
+
reveal?: RevealMode;
|
|
74
|
+
/**
|
|
75
|
+
* Toasts after undo and redo. Screen readers are always told.
|
|
76
|
+
* @defaultValue "toast"
|
|
77
|
+
*/
|
|
78
|
+
feedback?: FeedbackMode;
|
|
79
|
+
children: ReactNode;
|
|
80
|
+
}
|
|
81
|
+
/** Everything a component needs to remember, undo and show the history. */
|
|
82
|
+
export declare function useUndo(): UndoApi;
|
|
83
|
+
/**
|
|
84
|
+
* Holds one history for everything inside it, and wires the keys, the reveal and the feedback.
|
|
85
|
+
* `useUndo` reads it. Providers given the same `store` share one timeline, and each hears only
|
|
86
|
+
* about what it asked for.
|
|
87
|
+
*/
|
|
88
|
+
export declare function UndoProvider(props: UndoProviderProps): React.JSX.Element;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { createContext, useContext, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { feedbackHandlers } from "./undo-feedback";
|
|
4
|
+
import { useUndoKeys } from "./undo-keys";
|
|
5
|
+
import { reveal } from "./undo-reveal";
|
|
6
|
+
import { createUndoStore, viewOf } from "./undo-store";
|
|
7
|
+
const UndoContext = createContext(null);
|
|
8
|
+
function bind({ store, scope, place }, snapshot) {
|
|
9
|
+
const { entries, future } = viewOf(snapshot, place);
|
|
10
|
+
return {
|
|
11
|
+
store,
|
|
12
|
+
scope,
|
|
13
|
+
entries,
|
|
14
|
+
future,
|
|
15
|
+
canUndo: entries.length > 0,
|
|
16
|
+
canRedo: future.length > 0,
|
|
17
|
+
busy: snapshot.busy,
|
|
18
|
+
locked: snapshot.busy && snapshot.busyMode === "ignore",
|
|
19
|
+
place,
|
|
20
|
+
remember: store.remember,
|
|
21
|
+
undo: () => store.undo(scope),
|
|
22
|
+
redo: () => store.redo(scope),
|
|
23
|
+
undoEntry: (id) => store.undoEntry(id, scope),
|
|
24
|
+
redoEntry: (id) => store.redoEntry(id, scope),
|
|
25
|
+
rewindTo: (id, include) => store.rewindTo(id, include, scope),
|
|
26
|
+
isLatest: (id) => store.isLatest(id, scope),
|
|
27
|
+
seal: store.seal
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
/** Everything a component needs to remember, undo and show the history. */
|
|
31
|
+
export function useUndo() {
|
|
32
|
+
const context = useContext(UndoContext);
|
|
33
|
+
if (!context) {
|
|
34
|
+
throw new Error("useUndo needs an UndoProvider above it.");
|
|
35
|
+
}
|
|
36
|
+
const snapshot = useSyncExternalStore(context.store.subscribe, context.store.getSnapshot);
|
|
37
|
+
return useMemo(() => bind(context, snapshot), [context, snapshot]);
|
|
38
|
+
}
|
|
39
|
+
/* The handlers this provider gives the store: the app's, the reveal and the feedback. */
|
|
40
|
+
function useHandlers(store, scope, props) {
|
|
41
|
+
const latest = useRef(props);
|
|
42
|
+
latest.current = props;
|
|
43
|
+
const [handlers] = useState(() => {
|
|
44
|
+
const feedback = () => feedbackHandlers({
|
|
45
|
+
reverseUndo: (entry, info) => info.dropped
|
|
46
|
+
? store.reinstate(entry, scope)
|
|
47
|
+
: store.redoEntry(entry.id, scope),
|
|
48
|
+
reverseRedo: (entry) => store.undoEntry(entry.id, scope)
|
|
49
|
+
}, latest.current.feedback ?? "toast");
|
|
50
|
+
const show = (entry) => {
|
|
51
|
+
void reveal(entry, latest.current.reveal ?? "flash");
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
onUndo: (entry, info) => {
|
|
55
|
+
latest.current.onUndo?.(entry, info);
|
|
56
|
+
show(entry);
|
|
57
|
+
feedback().onUndo?.(entry, info);
|
|
58
|
+
},
|
|
59
|
+
onRedo: (entry, info) => {
|
|
60
|
+
latest.current.onRedo?.(entry, info);
|
|
61
|
+
show(entry);
|
|
62
|
+
feedback().onRedo?.(entry, info);
|
|
63
|
+
},
|
|
64
|
+
onError: (error, entry, direction) => {
|
|
65
|
+
latest.current.onError?.(error, entry, direction);
|
|
66
|
+
feedback().onError?.(error, entry, direction);
|
|
67
|
+
},
|
|
68
|
+
onBlocked: (entry, reason) => {
|
|
69
|
+
latest.current.onBlocked?.(entry, reason);
|
|
70
|
+
feedback().onBlocked?.(entry, reason);
|
|
71
|
+
},
|
|
72
|
+
onPlace: (place, entry) => latest.current.onPlace?.(place, entry)
|
|
73
|
+
};
|
|
74
|
+
});
|
|
75
|
+
return handlers;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Holds one history for everything inside it, and wires the keys, the reveal and the feedback.
|
|
79
|
+
* `useUndo` reads it. Providers given the same `store` share one timeline, and each hears only
|
|
80
|
+
* about what it asked for.
|
|
81
|
+
*/
|
|
82
|
+
export function UndoProvider(props) {
|
|
83
|
+
const [own] = useState(() => createUndoStore());
|
|
84
|
+
const store = props.store ?? own;
|
|
85
|
+
const [scope] = useState({});
|
|
86
|
+
scope.handlers = useHandlers(store, scope, props);
|
|
87
|
+
const { place } = props;
|
|
88
|
+
useLayoutEffect(() => {
|
|
89
|
+
scope.place = place;
|
|
90
|
+
}, [scope, place]);
|
|
91
|
+
useLayoutEffect(() => {
|
|
92
|
+
if (!props.store) {
|
|
93
|
+
store.setOptions(props.options ?? {});
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
useUndoKeys({ undo: () => store.undo(scope), redo: () => store.redo(scope) }, {
|
|
97
|
+
enabled: props.keys ?? true,
|
|
98
|
+
reach: props.keyReach,
|
|
99
|
+
area: props.area,
|
|
100
|
+
blocked: props.blocked
|
|
101
|
+
});
|
|
102
|
+
const value = useMemo(() => ({ store, scope, place }), [store, scope, place]);
|
|
103
|
+
return _jsx(UndoContext.Provider, { value: value, children: props.children });
|
|
104
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { UndoEntry } from "./history";
|
|
2
|
+
import type { RevealMode } from "./undo-options";
|
|
3
|
+
export declare const TARGET_ATTRIBUTE = "data-kumiko-undo-target";
|
|
4
|
+
/**
|
|
5
|
+
* Shows what a step changed once the app has rendered it: moves focus to the item, flashes it, or
|
|
6
|
+
* leaves everything alone. The item is found by the entry's `target`, through
|
|
7
|
+
* `data-kumiko-undo-target`.
|
|
8
|
+
*/
|
|
9
|
+
export declare function reveal(entry: UndoEntry, mode: RevealMode): Promise<void>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/* The attribute an app puts on the element a step changed. Its value is the entry's `target`. */
|
|
2
|
+
export const TARGET_ATTRIBUTE = "data-kumiko-undo-target";
|
|
3
|
+
const FLASH_ATTRIBUTE = "data-kumiko-undo-flash";
|
|
4
|
+
function twoFrames() {
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
requestAnimationFrame(() => {
|
|
7
|
+
requestAnimationFrame(() => resolve());
|
|
8
|
+
});
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function findTarget(target) {
|
|
12
|
+
const found = document.querySelector(`[${TARGET_ATTRIBUTE}="${CSS.escape(target)}"]`);
|
|
13
|
+
return found instanceof HTMLElement ? found : null;
|
|
14
|
+
}
|
|
15
|
+
/* Lights the item up with the flash animation, and clears it once the animation has ended. */
|
|
16
|
+
async function flash(element) {
|
|
17
|
+
element.removeAttribute(FLASH_ATTRIBUTE);
|
|
18
|
+
/* Reading the layout restarts the animation when the same item flashes twice in a row. */
|
|
19
|
+
void element.offsetWidth;
|
|
20
|
+
element.setAttribute(FLASH_ATTRIBUTE, "");
|
|
21
|
+
await Promise.allSettled(element.getAnimations().map((animation) => animation.finished));
|
|
22
|
+
element.removeAttribute(FLASH_ATTRIBUTE);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Shows what a step changed once the app has rendered it: moves focus to the item, flashes it, or
|
|
26
|
+
* leaves everything alone. The item is found by the entry's `target`, through
|
|
27
|
+
* `data-kumiko-undo-target`.
|
|
28
|
+
*/
|
|
29
|
+
export async function reveal(entry, mode) {
|
|
30
|
+
if (mode === "stay" || entry.target === undefined) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
await twoFrames();
|
|
34
|
+
const element = findTarget(entry.target);
|
|
35
|
+
if (!element) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (mode === "focus") {
|
|
39
|
+
element.focus({ preventScroll: true });
|
|
40
|
+
element.scrollIntoView({ block: "nearest" });
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
await flash(element);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { type NewUndoEntry, type UndoEntry } from "./history";
|
|
2
|
+
import { type BusyMode, type PlaceMode, type UndoHandlers, type UndoOptions } from "./undo-options";
|
|
3
|
+
/** What a component reads from the store. A new object whenever anything changes. */
|
|
4
|
+
export interface UndoSnapshot {
|
|
5
|
+
/** Every step that can be undone, oldest first, across all places. */
|
|
6
|
+
past: readonly UndoEntry[];
|
|
7
|
+
/** Every step that can be redone, most recently undone last. */
|
|
8
|
+
future: readonly UndoEntry[];
|
|
9
|
+
/** Whether an undo or redo is running or waiting. */
|
|
10
|
+
busy: boolean;
|
|
11
|
+
placeMode: PlaceMode;
|
|
12
|
+
busyMode: BusyMode;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Who is asking: the place on screen, and the handlers to tell. Each provider has its own, so two
|
|
16
|
+
* providers on one store each hear only about what they asked for.
|
|
17
|
+
*/
|
|
18
|
+
export interface UndoScope {
|
|
19
|
+
place?: string;
|
|
20
|
+
handlers?: UndoHandlers;
|
|
21
|
+
}
|
|
22
|
+
/** What the store needs from outside, so tests can run it without a browser. */
|
|
23
|
+
export interface UndoDeps {
|
|
24
|
+
now: () => number;
|
|
25
|
+
frame: () => Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A command history that lives outside React: remember steps, undo and redo them (async, one at a
|
|
29
|
+
* time), and listen. `UndoProvider` wraps one, and two providers can share one store.
|
|
30
|
+
*/
|
|
31
|
+
export declare class UndoStore {
|
|
32
|
+
private history;
|
|
33
|
+
private options;
|
|
34
|
+
private pending;
|
|
35
|
+
private tail;
|
|
36
|
+
private counter;
|
|
37
|
+
private readonly listeners;
|
|
38
|
+
private readonly handlers;
|
|
39
|
+
private snapshot;
|
|
40
|
+
private readonly deps;
|
|
41
|
+
constructor(deps?: UndoDeps);
|
|
42
|
+
private build;
|
|
43
|
+
private publish;
|
|
44
|
+
private scopePlace;
|
|
45
|
+
readonly getSnapshot: () => UndoSnapshot;
|
|
46
|
+
readonly subscribe: (listener: () => void) => (() => void);
|
|
47
|
+
/** Adds handlers that hear about every undo, redo, error and block on the store. */
|
|
48
|
+
readonly addHandlers: (handlers: UndoHandlers) => (() => void);
|
|
49
|
+
/** Changes the settings. Anything left out goes back to its default, and the history stays. */
|
|
50
|
+
readonly setOptions: (options: Partial<UndoOptions>) => void;
|
|
51
|
+
/** Stores a step and returns the stored entry (the one it merged into, when it joined). */
|
|
52
|
+
readonly remember: (entry: NewUndoEntry) => UndoEntry;
|
|
53
|
+
/** Closes the last step: the next one starts fresh even with the same merge key. */
|
|
54
|
+
readonly seal: () => void;
|
|
55
|
+
/** Whether a step is the latest one the scope can see. */
|
|
56
|
+
readonly isLatest: (id: string, scope?: UndoScope) => boolean;
|
|
57
|
+
/** Undoes the latest step the scope can see. */
|
|
58
|
+
readonly undo: (scope?: UndoScope) => Promise<void>;
|
|
59
|
+
/** Redoes the latest undone step the scope can see. */
|
|
60
|
+
readonly redo: (scope?: UndoScope) => Promise<void>;
|
|
61
|
+
/**
|
|
62
|
+
* Undoes one step wherever it sits. The latest step goes to the redo side as usual. A step
|
|
63
|
+
* with newer ones above it leaves the history, so the key can't run it a second time. A step
|
|
64
|
+
* that only works in order is refused while a newer one of its kind is done.
|
|
65
|
+
*/
|
|
66
|
+
readonly undoEntry: (id: string, scope?: UndoScope) => Promise<void>;
|
|
67
|
+
/** Redoes one step from the redo side, wherever it sits. It comes back as the newest step. */
|
|
68
|
+
readonly redoEntry: (id: string, scope?: UndoScope) => Promise<void>;
|
|
69
|
+
/** Does a step that left the history again, and puts it back as the newest step. */
|
|
70
|
+
readonly reinstate: (entry: UndoEntry, scope?: UndoScope) => Promise<void>;
|
|
71
|
+
/**
|
|
72
|
+
* Goes back to just after step `id`, or to the very start for null, undoing every newer step
|
|
73
|
+
* (the step itself too with `include`). For a step on the redo side, redoes up to it.
|
|
74
|
+
*/
|
|
75
|
+
readonly rewindTo: (id: string | null, include?: boolean, scope?: UndoScope) => Promise<void>;
|
|
76
|
+
private enqueue;
|
|
77
|
+
private tell;
|
|
78
|
+
private run;
|
|
79
|
+
private runFrom;
|
|
80
|
+
private step;
|
|
81
|
+
private goTo;
|
|
82
|
+
private settle;
|
|
83
|
+
}
|
|
84
|
+
/** The steps a place sees: all of them, or in own mode only its own. */
|
|
85
|
+
export declare function viewOf(snapshot: UndoSnapshot, place: string | undefined): {
|
|
86
|
+
entries: readonly UndoEntry[];
|
|
87
|
+
future: readonly UndoEntry[];
|
|
88
|
+
};
|
|
89
|
+
export declare function createUndoStore(options?: Partial<UndoOptions>, deps?: UndoDeps): UndoStore;
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { EMPTY_HISTORY, blockedRedo, blockedUndo, forget, inPlace, isOnTop, markRedone, markUndone, nextRedo, nextUndo, reinstate, remember, seal, stepsBack, stepsForward } from "./history";
|
|
2
|
+
import { DEFAULT_OPTIONS } from "./undo-options";
|
|
3
|
+
const browserDeps = {
|
|
4
|
+
now: () => Date.now(),
|
|
5
|
+
frame: () => new Promise((resolve) => {
|
|
6
|
+
requestAnimationFrame(() => resolve());
|
|
7
|
+
})
|
|
8
|
+
};
|
|
9
|
+
function sameOptions(a, b) {
|
|
10
|
+
return (a.mergeWindow === b.mergeWindow &&
|
|
11
|
+
a.limit === b.limit &&
|
|
12
|
+
a.busy === b.busy &&
|
|
13
|
+
a.place === b.place);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A command history that lives outside React: remember steps, undo and redo them (async, one at a
|
|
17
|
+
* time), and listen. `UndoProvider` wraps one, and two providers can share one store.
|
|
18
|
+
*/
|
|
19
|
+
export class UndoStore {
|
|
20
|
+
history = EMPTY_HISTORY;
|
|
21
|
+
options = DEFAULT_OPTIONS;
|
|
22
|
+
pending = 0;
|
|
23
|
+
tail = Promise.resolve();
|
|
24
|
+
counter = 0;
|
|
25
|
+
listeners = new Set();
|
|
26
|
+
handlers = new Set();
|
|
27
|
+
snapshot = this.build();
|
|
28
|
+
deps;
|
|
29
|
+
constructor(deps = browserDeps) {
|
|
30
|
+
this.deps = deps;
|
|
31
|
+
}
|
|
32
|
+
build() {
|
|
33
|
+
return {
|
|
34
|
+
past: this.history.past,
|
|
35
|
+
future: this.history.future,
|
|
36
|
+
busy: this.pending > 0,
|
|
37
|
+
placeMode: this.options.place,
|
|
38
|
+
busyMode: this.options.busy
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
publish() {
|
|
42
|
+
this.snapshot = this.build();
|
|
43
|
+
for (const listener of this.listeners) {
|
|
44
|
+
listener();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/* In own mode a place sees only its own steps. */
|
|
48
|
+
scopePlace(scope) {
|
|
49
|
+
return this.options.place === "own" ? scope.place : undefined;
|
|
50
|
+
}
|
|
51
|
+
getSnapshot = () => this.snapshot;
|
|
52
|
+
subscribe = (listener) => {
|
|
53
|
+
this.listeners.add(listener);
|
|
54
|
+
return () => {
|
|
55
|
+
this.listeners.delete(listener);
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
/** Adds handlers that hear about every undo, redo, error and block on the store. */
|
|
59
|
+
addHandlers = (handlers) => {
|
|
60
|
+
this.handlers.add(handlers);
|
|
61
|
+
return () => {
|
|
62
|
+
this.handlers.delete(handlers);
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
/** Changes the settings. Anything left out goes back to its default, and the history stays. */
|
|
66
|
+
setOptions = (options) => {
|
|
67
|
+
const next = { ...DEFAULT_OPTIONS, ...options };
|
|
68
|
+
if (!sameOptions(next, this.options)) {
|
|
69
|
+
this.options = next;
|
|
70
|
+
this.publish();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
/** Stores a step and returns the stored entry (the one it merged into, when it joined). */
|
|
74
|
+
remember = (entry) => {
|
|
75
|
+
this.counter += 1;
|
|
76
|
+
const result = remember(this.history, entry, {
|
|
77
|
+
limit: this.options.limit,
|
|
78
|
+
windowMs: this.options.mergeWindow,
|
|
79
|
+
now: this.deps.now(),
|
|
80
|
+
id: `undo-${this.counter}`,
|
|
81
|
+
ownFuture: this.options.place === "own"
|
|
82
|
+
});
|
|
83
|
+
this.history = result.history;
|
|
84
|
+
this.publish();
|
|
85
|
+
return result.entry;
|
|
86
|
+
};
|
|
87
|
+
/** Closes the last step: the next one starts fresh even with the same merge key. */
|
|
88
|
+
seal = () => {
|
|
89
|
+
this.history = seal(this.history);
|
|
90
|
+
};
|
|
91
|
+
/** Whether a step is the latest one the scope can see. */
|
|
92
|
+
isLatest = (id, scope = {}) => isOnTop(this.history, id, this.scopePlace(scope));
|
|
93
|
+
/** Undoes the latest step the scope can see. */
|
|
94
|
+
undo = (scope = {}) => this.enqueue(async () => {
|
|
95
|
+
const entry = nextUndo(this.history, this.scopePlace(scope));
|
|
96
|
+
if (entry) {
|
|
97
|
+
await this.run([entry], "undo", "move", scope);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
/** Redoes the latest undone step the scope can see. */
|
|
101
|
+
redo = (scope = {}) => this.enqueue(async () => {
|
|
102
|
+
const entry = nextRedo(this.history, this.scopePlace(scope));
|
|
103
|
+
if (entry) {
|
|
104
|
+
await this.run([entry], "redo", "move", scope);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
/**
|
|
108
|
+
* Undoes one step wherever it sits. The latest step goes to the redo side as usual. A step
|
|
109
|
+
* with newer ones above it leaves the history, so the key can't run it a second time. A step
|
|
110
|
+
* that only works in order is refused while a newer one of its kind is done.
|
|
111
|
+
*/
|
|
112
|
+
undoEntry = (id, scope = {}) => this.enqueue(async () => {
|
|
113
|
+
const entry = this.history.past.find((item) => item.id === id);
|
|
114
|
+
if (!entry) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const reason = blockedUndo(this.history, id);
|
|
118
|
+
if (reason) {
|
|
119
|
+
this.tell(scope, (handler) => handler.onBlocked?.(entry, reason));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const top = isOnTop(this.history, id, this.scopePlace(scope));
|
|
123
|
+
await this.run([entry], "undo", top ? "move" : "drop", scope);
|
|
124
|
+
});
|
|
125
|
+
/** Redoes one step from the redo side, wherever it sits. It comes back as the newest step. */
|
|
126
|
+
redoEntry = (id, scope = {}) => this.enqueue(async () => {
|
|
127
|
+
const entry = this.history.future.find((item) => item.id === id);
|
|
128
|
+
if (!entry) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const reason = blockedRedo(this.history, id);
|
|
132
|
+
if (reason) {
|
|
133
|
+
this.tell(scope, (handler) => handler.onBlocked?.(entry, reason));
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
await this.run([entry], "redo", "move", scope);
|
|
137
|
+
});
|
|
138
|
+
/** Does a step that left the history again, and puts it back as the newest step. */
|
|
139
|
+
reinstate = (entry, scope = {}) => this.enqueue(() => this.run([entry], "redo", "reinstate", scope));
|
|
140
|
+
/**
|
|
141
|
+
* Goes back to just after step `id`, or to the very start for null, undoing every newer step
|
|
142
|
+
* (the step itself too with `include`). For a step on the redo side, redoes up to it.
|
|
143
|
+
*/
|
|
144
|
+
rewindTo = (id, include = false, scope = {}) => this.enqueue(async () => {
|
|
145
|
+
const place = this.scopePlace(scope);
|
|
146
|
+
const forward = id === null ? [] : stepsForward(this.history, id, place);
|
|
147
|
+
if (forward.length > 0) {
|
|
148
|
+
await this.run(forward, "redo", "move", scope);
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
await this.run(stepsBack(this.history, id, include, place), "undo", "move", scope);
|
|
152
|
+
});
|
|
153
|
+
enqueue(job) {
|
|
154
|
+
if (this.pending > 0 && this.options.busy === "ignore") {
|
|
155
|
+
return Promise.resolve();
|
|
156
|
+
}
|
|
157
|
+
this.pending += 1;
|
|
158
|
+
this.publish();
|
|
159
|
+
const done = this.tail.then(job).finally(() => {
|
|
160
|
+
this.pending -= 1;
|
|
161
|
+
this.publish();
|
|
162
|
+
});
|
|
163
|
+
this.tail = done.catch(() => undefined);
|
|
164
|
+
return done;
|
|
165
|
+
}
|
|
166
|
+
tell(scope, call) {
|
|
167
|
+
if (scope.handlers) {
|
|
168
|
+
call(scope.handlers);
|
|
169
|
+
}
|
|
170
|
+
for (const handlers of this.handlers) {
|
|
171
|
+
call(handlers);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/* Runs steps one by one and stops at the first failure. */
|
|
175
|
+
async run(entries, direction, settle, scope) {
|
|
176
|
+
await this.runFrom(entries, 0, direction, settle, scope);
|
|
177
|
+
}
|
|
178
|
+
async runFrom(entries, index, direction, settle, scope) {
|
|
179
|
+
const entry = entries[index];
|
|
180
|
+
if (!entry) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const info = {
|
|
184
|
+
steps: entries.length,
|
|
185
|
+
last: index === entries.length - 1,
|
|
186
|
+
dropped: settle === "drop"
|
|
187
|
+
};
|
|
188
|
+
if (await this.step(entry, direction, settle, info, scope)) {
|
|
189
|
+
await this.runFrom(entries, index + 1, direction, settle, scope);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async step(entry, direction, settle, info, scope) {
|
|
193
|
+
try {
|
|
194
|
+
const away = entry.place !== undefined && entry.place !== scope.place;
|
|
195
|
+
if (this.options.place === "jump" && away) {
|
|
196
|
+
await this.goTo(entry, scope);
|
|
197
|
+
}
|
|
198
|
+
await (direction === "undo" ? entry.undo() : entry.redo());
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
this.tell(scope, (handler) => handler.onError?.(error, entry, direction));
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
this.settle(entry, direction, settle);
|
|
205
|
+
this.tell(scope, (handler) => direction === "undo" ? handler.onUndo?.(entry, info) : handler.onRedo?.(entry, info));
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
async goTo(entry, scope) {
|
|
209
|
+
const place = entry.place;
|
|
210
|
+
if (place === undefined) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const waits = [];
|
|
214
|
+
this.tell(scope, (handler) => waits.push(handler.onPlace?.(place, entry)));
|
|
215
|
+
await Promise.all(waits);
|
|
216
|
+
await this.deps.frame();
|
|
217
|
+
}
|
|
218
|
+
settle(entry, direction, settle) {
|
|
219
|
+
if (settle === "reinstate") {
|
|
220
|
+
this.history = reinstate(this.history, entry);
|
|
221
|
+
}
|
|
222
|
+
else if (direction === "redo") {
|
|
223
|
+
this.history = markRedone(this.history, entry.id);
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
this.history =
|
|
227
|
+
settle === "drop"
|
|
228
|
+
? forget(this.history, entry.id)
|
|
229
|
+
: markUndone(this.history, entry.id);
|
|
230
|
+
}
|
|
231
|
+
this.publish();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/** The steps a place sees: all of them, or in own mode only its own. */
|
|
235
|
+
export function viewOf(snapshot, place) {
|
|
236
|
+
const scope = snapshot.placeMode === "own" ? place : undefined;
|
|
237
|
+
return { entries: inPlace(snapshot.past, scope), future: inPlace(snapshot.future, scope) };
|
|
238
|
+
}
|
|
239
|
+
export function createUndoStore(options = {}, deps) {
|
|
240
|
+
const store = new UndoStore(deps);
|
|
241
|
+
store.setOptions(options);
|
|
242
|
+
return store;
|
|
243
|
+
}
|