@xnetjs/react 0.0.2 → 0.1.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/README.md +206 -2
- package/dist/chunk-6VOICQZ3.js +760 -0
- package/dist/chunk-7OQXRHEQ.js +4649 -0
- package/dist/chunk-EJ5RW5GI.js +93 -0
- package/dist/chunk-IHTMVTTE.js +1108 -0
- package/dist/chunk-JCOFKBOB.js +11 -0
- package/dist/chunk-KSHTDZ2V.js +893 -0
- package/dist/chunk-QHNYQVUM.js +989 -0
- package/dist/context-CFu9i136.d.ts +392 -0
- package/dist/core.d.ts +372 -0
- package/dist/core.js +29 -0
- package/dist/database.d.ts +383 -0
- package/dist/database.js +19 -0
- package/dist/experimental-B2FrBnkV.d.ts +1584 -0
- package/dist/experimental.d.ts +15 -0
- package/dist/experimental.js +248 -0
- package/dist/index.d.ts +601 -2700
- package/dist/index.js +4782 -4427
- package/dist/{instrumentation-Cn94kn8-.d.ts → instrumentation-CpIuG2y5.d.ts} +32 -1
- package/dist/internal.d.ts +32 -22
- package/dist/internal.js +16 -4
- package/dist/telemetry-context-B7r6H1KW.d.ts +35 -0
- package/dist/useNodeStore-DTCSBF51.d.ts +28 -0
- package/dist/useQuery-D7ajycrc.d.ts +302 -0
- package/package.json +28 -10
- package/dist/chunk-CWHCGYDW.js +0 -2238
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useNodeStore
|
|
3
|
+
} from "./chunk-IHTMVTTE.js";
|
|
4
|
+
|
|
5
|
+
// src/hooks/useUndoScope.ts
|
|
6
|
+
import { UndoManager } from "@xnetjs/history";
|
|
7
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
8
|
+
function useUndoScope(nodeIds, opts) {
|
|
9
|
+
const { store } = useNodeStore();
|
|
10
|
+
const [undoCount, setUndoCount] = useState(0);
|
|
11
|
+
const [redoCount, setRedoCount] = useState(0);
|
|
12
|
+
const managerRef = useRef(null);
|
|
13
|
+
const nodeIdsRef = useRef(nodeIds);
|
|
14
|
+
nodeIdsRef.current = nodeIds;
|
|
15
|
+
const normalizedOptions = useMemo(
|
|
16
|
+
() => opts.options ? {
|
|
17
|
+
localOnly: opts.options.localOnly,
|
|
18
|
+
maxStackSize: opts.options.maxStackSize,
|
|
19
|
+
mergeInterval: opts.options.mergeInterval
|
|
20
|
+
} : void 0,
|
|
21
|
+
[opts.options?.localOnly, opts.options?.maxStackSize, opts.options?.mergeInterval]
|
|
22
|
+
);
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
if (!store || !opts.localDID) {
|
|
25
|
+
managerRef.current = null;
|
|
26
|
+
setUndoCount(0);
|
|
27
|
+
setRedoCount(0);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const manager = new UndoManager(store, opts.localDID, normalizedOptions);
|
|
31
|
+
manager.start();
|
|
32
|
+
managerRef.current = manager;
|
|
33
|
+
return () => {
|
|
34
|
+
manager.stop();
|
|
35
|
+
managerRef.current = null;
|
|
36
|
+
};
|
|
37
|
+
}, [store, opts.localDID, normalizedOptions]);
|
|
38
|
+
const syncCounts = useCallback(() => {
|
|
39
|
+
const manager = managerRef.current;
|
|
40
|
+
const scope = nodeIdsRef.current;
|
|
41
|
+
if (!manager || scope.length === 0) {
|
|
42
|
+
setUndoCount(0);
|
|
43
|
+
setRedoCount(0);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
setUndoCount(scope.reduce((count, nodeId) => count + manager.getUndoCount(nodeId), 0));
|
|
47
|
+
setRedoCount(scope.reduce((count, nodeId) => count + manager.getRedoCount(nodeId), 0));
|
|
48
|
+
}, []);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
syncCounts();
|
|
51
|
+
}, [nodeIds, syncCounts]);
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
if (!store) return;
|
|
54
|
+
const unsubscribe = store.subscribe((event) => {
|
|
55
|
+
if (nodeIdsRef.current.includes(event.change.payload.nodeId)) {
|
|
56
|
+
syncCounts();
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
return unsubscribe;
|
|
60
|
+
}, [store, syncCounts]);
|
|
61
|
+
const undo = useCallback(async () => {
|
|
62
|
+
if (!managerRef.current) return false;
|
|
63
|
+
const result = await managerRef.current.undoLatest(nodeIdsRef.current);
|
|
64
|
+
syncCounts();
|
|
65
|
+
return result;
|
|
66
|
+
}, [syncCounts]);
|
|
67
|
+
const redo = useCallback(async () => {
|
|
68
|
+
if (!managerRef.current) return false;
|
|
69
|
+
const result = await managerRef.current.redoLatest(nodeIdsRef.current);
|
|
70
|
+
syncCounts();
|
|
71
|
+
return result;
|
|
72
|
+
}, [syncCounts]);
|
|
73
|
+
const clear = useCallback(() => {
|
|
74
|
+
if (!managerRef.current) return;
|
|
75
|
+
for (const nodeId of nodeIdsRef.current) {
|
|
76
|
+
managerRef.current.clear(nodeId);
|
|
77
|
+
}
|
|
78
|
+
syncCounts();
|
|
79
|
+
}, [syncCounts]);
|
|
80
|
+
return {
|
|
81
|
+
undo,
|
|
82
|
+
redo,
|
|
83
|
+
canUndo: undoCount > 0,
|
|
84
|
+
canRedo: redoCount > 0,
|
|
85
|
+
undoCount,
|
|
86
|
+
redoCount,
|
|
87
|
+
clear
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export {
|
|
92
|
+
useUndoScope
|
|
93
|
+
};
|