@open-document/core 0.6.0 → 0.7.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/{build-BvIHtePO.js → build-CFL7nhz0.js} +1 -1
- package/dist/{check-BqEdVH7G.js → check-pEQh4jJE.js} +3 -3
- package/dist/cli/bin.js +6 -6
- package/dist/{config-B7vFf9qZ.js → config-4nz5NnxW.js} +593 -137
- package/dist/{context-BaUE_aqU.js → context-BH8C-dSW.js} +1 -1
- package/dist/{dev-DXG-Dg-y.js → dev-BCSUczXW.js} +1 -1
- package/dist/{export-CT1Axnjv.js → export-BQ-GvMsb.js} +3 -3
- package/dist/{import-C8ZC4R6T.js → import-Daat3L2J.js} +2 -2
- package/dist/index.d.ts +21 -3
- package/dist/index.js +40 -9
- package/dist/ops/index.d.ts +47 -3
- package/dist/ops/index.js +3 -3
- package/dist/{ops-ByclxTfk.js → ops-DQfckiyp.js} +2 -45
- package/dist/{preview-CdmWbaHy.js → preview-CwCrw-i3.js} +1 -1
- package/dist/vite/index.js +1 -1
- package/package.json +1 -1
- package/src/app/components/data-table.tsx +17 -2
- package/src/app/components/design-panel/design-panel.tsx +149 -274
- package/src/app/components/design-panel/design-provider.tsx +35 -15
- package/src/app/components/footnote.tsx +27 -10
- package/src/app/components/history-provider.tsx +125 -0
- package/src/app/components/inspector/inspector.tsx +1145 -208
- package/src/app/components/numbering.tsx +13 -1
- package/src/app/components/panel/edit-save-card.tsx +112 -0
- package/src/app/components/panel/fields.tsx +247 -0
- package/src/app/components/panel/panel-shell.tsx +71 -0
- package/src/app/components/panel/save-card.tsx +146 -0
- package/src/app/lib/footnotes.ts +6 -3
- package/src/app/lib/href.ts +14 -0
- package/src/app/lib/inspector/inline-edit.test.ts +244 -0
- package/src/app/lib/inspector/inline-edit.ts +770 -0
- package/src/app/lib/source-loc.ts +15 -0
- package/src/app/routes/doc.tsx +109 -39
- package/src/app/styles.css +41 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useMemo,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from 'react';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* One undoable change in the document view: a design tweak, or a finished
|
|
13
|
+
* session of typing on the page. Undo and redo are closures over whatever the
|
|
14
|
+
* change touched, so the history knows nothing about either kind.
|
|
15
|
+
*/
|
|
16
|
+
export type HistoryEntry = {
|
|
17
|
+
undo: () => void;
|
|
18
|
+
redo: () => void;
|
|
19
|
+
/** Changes with the same key inside the coalesce window merge into one step. */
|
|
20
|
+
coalesceKey?: string;
|
|
21
|
+
ts: number;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type HistoryCtx = {
|
|
25
|
+
canUndo: boolean;
|
|
26
|
+
canRedo: boolean;
|
|
27
|
+
record: (entry: Omit<HistoryEntry, 'ts'>) => void;
|
|
28
|
+
undo: () => void;
|
|
29
|
+
redo: () => void;
|
|
30
|
+
clear: () => void;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Long enough to fold a slider drag into one step, short enough that two
|
|
34
|
+
// deliberate changes to the same field stay two.
|
|
35
|
+
const COALESCE_WINDOW_MS = 500;
|
|
36
|
+
|
|
37
|
+
const Ctx = createContext<HistoryCtx | null>(null);
|
|
38
|
+
|
|
39
|
+
export function useHistory(): HistoryCtx {
|
|
40
|
+
const value = useContext(Ctx);
|
|
41
|
+
if (!value) throw new Error('useHistory must be used inside <HistoryProvider>');
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function HistoryProvider({ children }: { children: ReactNode }) {
|
|
46
|
+
const stacksRef = useRef<{ past: HistoryEntry[]; future: HistoryEntry[] }>({
|
|
47
|
+
past: [],
|
|
48
|
+
future: [],
|
|
49
|
+
});
|
|
50
|
+
const [availability, setAvailability] = useState({ canUndo: false, canRedo: false });
|
|
51
|
+
// Set while an entry's own undo/redo runs, so the state change it makes is
|
|
52
|
+
// not recorded again as a new step.
|
|
53
|
+
const suppressedRef = useRef(false);
|
|
54
|
+
|
|
55
|
+
const sync = useCallback(() => {
|
|
56
|
+
const canUndo = stacksRef.current.past.length > 0;
|
|
57
|
+
const canRedo = stacksRef.current.future.length > 0;
|
|
58
|
+
setAvailability((previous) =>
|
|
59
|
+
previous.canUndo === canUndo && previous.canRedo === canRedo
|
|
60
|
+
? previous
|
|
61
|
+
: { canUndo, canRedo },
|
|
62
|
+
);
|
|
63
|
+
}, []);
|
|
64
|
+
|
|
65
|
+
const record = useCallback(
|
|
66
|
+
(entry: Omit<HistoryEntry, 'ts'>) => {
|
|
67
|
+
if (suppressedRef.current) return;
|
|
68
|
+
const ts = Date.now();
|
|
69
|
+
const { past } = stacksRef.current;
|
|
70
|
+
const top = past.at(-1);
|
|
71
|
+
if (
|
|
72
|
+
top &&
|
|
73
|
+
entry.coalesceKey !== undefined &&
|
|
74
|
+
top.coalesceKey === entry.coalesceKey &&
|
|
75
|
+
ts - top.ts < COALESCE_WINDOW_MS
|
|
76
|
+
) {
|
|
77
|
+
const merged: HistoryEntry = { ...entry, undo: top.undo, ts };
|
|
78
|
+
stacksRef.current = { past: [...past.slice(0, -1), merged], future: [] };
|
|
79
|
+
} else {
|
|
80
|
+
stacksRef.current = { past: [...past, { ...entry, ts }], future: [] };
|
|
81
|
+
}
|
|
82
|
+
sync();
|
|
83
|
+
},
|
|
84
|
+
[sync],
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const step = useCallback(
|
|
88
|
+
(forward: boolean) => {
|
|
89
|
+
if (suppressedRef.current) return;
|
|
90
|
+
const previous = stacksRef.current;
|
|
91
|
+
const top = forward ? previous.future.at(-1) : previous.past.at(-1);
|
|
92
|
+
if (!top) return;
|
|
93
|
+
stacksRef.current = forward
|
|
94
|
+
? { past: [...previous.past, top], future: previous.future.slice(0, -1) }
|
|
95
|
+
: { past: previous.past.slice(0, -1), future: [...previous.future, top] };
|
|
96
|
+
suppressedRef.current = true;
|
|
97
|
+
try {
|
|
98
|
+
if (forward) top.redo();
|
|
99
|
+
else top.undo();
|
|
100
|
+
} catch (error) {
|
|
101
|
+
stacksRef.current = previous;
|
|
102
|
+
throw error;
|
|
103
|
+
} finally {
|
|
104
|
+
suppressedRef.current = false;
|
|
105
|
+
sync();
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
[sync],
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
const undo = useCallback(() => step(false), [step]);
|
|
112
|
+
const redo = useCallback(() => step(true), [step]);
|
|
113
|
+
|
|
114
|
+
const clear = useCallback(() => {
|
|
115
|
+
stacksRef.current = { past: [], future: [] };
|
|
116
|
+
sync();
|
|
117
|
+
}, [sync]);
|
|
118
|
+
|
|
119
|
+
const value = useMemo<HistoryCtx>(
|
|
120
|
+
() => ({ ...availability, record, undo, redo, clear }),
|
|
121
|
+
[availability, record, undo, redo, clear],
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
return <Ctx.Provider value={value}>{children}</Ctx.Provider>;
|
|
125
|
+
}
|