@open-document/core 0.6.0 → 0.8.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.
Files changed (36) hide show
  1. package/dist/{build-BvIHtePO.js → build-CFL7nhz0.js} +1 -1
  2. package/dist/{check-BqEdVH7G.js → check-pEQh4jJE.js} +3 -3
  3. package/dist/cli/bin.js +6 -6
  4. package/dist/{config-B7vFf9qZ.js → config-4nz5NnxW.js} +593 -137
  5. package/dist/{context-BaUE_aqU.js → context-BH8C-dSW.js} +1 -1
  6. package/dist/{dev-DXG-Dg-y.js → dev-BCSUczXW.js} +1 -1
  7. package/dist/{export-CT1Axnjv.js → export-BQ-GvMsb.js} +3 -3
  8. package/dist/{import-C8ZC4R6T.js → import-Daat3L2J.js} +2 -2
  9. package/dist/index.d.ts +21 -3
  10. package/dist/index.js +40 -9
  11. package/dist/ops/index.d.ts +47 -3
  12. package/dist/ops/index.js +3 -3
  13. package/dist/{ops-ByclxTfk.js → ops-DQfckiyp.js} +2 -45
  14. package/dist/{preview-CdmWbaHy.js → preview-CwCrw-i3.js} +1 -1
  15. package/dist/vite/index.js +1 -1
  16. package/package.json +1 -1
  17. package/src/app/components/data-table.tsx +17 -2
  18. package/src/app/components/design-panel/design-panel.tsx +149 -274
  19. package/src/app/components/design-panel/design-provider.tsx +35 -15
  20. package/src/app/components/footnote.tsx +27 -10
  21. package/src/app/components/history-provider.tsx +125 -0
  22. package/src/app/components/inspector/inspector.tsx +1149 -208
  23. package/src/app/components/numbering.tsx +13 -1
  24. package/src/app/components/panel/edit-save-card.tsx +112 -0
  25. package/src/app/components/panel/fields.tsx +247 -0
  26. package/src/app/components/panel/panel-shell.tsx +71 -0
  27. package/src/app/components/panel/save-card.tsx +146 -0
  28. package/src/app/lib/footnotes.ts +6 -3
  29. package/src/app/lib/href.ts +14 -0
  30. package/src/app/lib/inspector/inline-edit.test.ts +244 -0
  31. package/src/app/lib/inspector/inline-edit.ts +770 -0
  32. package/src/app/lib/source-loc.ts +15 -0
  33. package/src/app/lib/view-mode.test.ts +46 -0
  34. package/src/app/lib/view-mode.ts +76 -0
  35. package/src/app/routes/doc.tsx +206 -48
  36. 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
+ }