@rytass/wms-map-react-components 0.1.1
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/LICENSE +21 -0
- package/README.md +77 -0
- package/components/breadcrumb/breadcrumb-edit-modal.d.ts +9 -0
- package/components/breadcrumb/breadcrumb-edit-modal.js +92 -0
- package/components/breadcrumb/breadcrumb-edit-modal.module.scss.js +3 -0
- package/components/breadcrumb/breadcrumb.d.ts +8 -0
- package/components/breadcrumb/breadcrumb.js +167 -0
- package/components/breadcrumb/breadcrumb.module.scss.js +3 -0
- package/components/canvas/react-flow-canvas.d.ts +37 -0
- package/components/canvas/react-flow-canvas.js +317 -0
- package/components/canvas/react-flow-canvas.module.scss.js +3 -0
- package/components/modal/wms-map-content.d.ts +4 -0
- package/components/modal/wms-map-content.js +753 -0
- package/components/modal/wms-map-header.d.ts +6 -0
- package/components/modal/wms-map-header.js +19 -0
- package/components/modal/wms-map-header.module.scss.js +3 -0
- package/components/modal/wms-map-modal-container.d.ts +4 -0
- package/components/modal/wms-map-modal-container.js +95 -0
- package/components/modal/wms-map-modal-container.module.scss.js +3 -0
- package/components/nodes/base-node.d.ts +12 -0
- package/components/nodes/image-node.d.ts +17 -0
- package/components/nodes/image-node.js +233 -0
- package/components/nodes/image-node.module.scss.js +3 -0
- package/components/nodes/path-node.d.ts +19 -0
- package/components/nodes/path-node.js +550 -0
- package/components/nodes/path-node.module.scss.js +3 -0
- package/components/nodes/rectangle-node.d.ts +11 -0
- package/components/nodes/rectangle-node.js +236 -0
- package/components/nodes/rectangle-node.module.scss.js +3 -0
- package/components/ui/context-menu.d.ts +25 -0
- package/components/ui/context-menu.js +168 -0
- package/components/ui/context-menu.module.scss.js +3 -0
- package/components/ui/toolbar.d.ts +23 -0
- package/components/ui/toolbar.js +278 -0
- package/components/ui/toolbar.module.scss.js +3 -0
- package/components/ui/view-mode-toolbar.d.ts +9 -0
- package/components/ui/view-mode-toolbar.js +41 -0
- package/components/ui/view-mode-toolbar.module.scss.js +3 -0
- package/constants/index.d.ts +45 -0
- package/constants/index.js +37 -0
- package/constants/text-mappings.d.ts +181 -0
- package/constants/text-mappings.js +44 -0
- package/hooks/use-context-menu.d.ts +37 -0
- package/hooks/use-context-menu.js +228 -0
- package/hooks/use-direct-state-history.d.ts +58 -0
- package/hooks/use-direct-state-history.js +214 -0
- package/hooks/use-pen-drawing.d.ts +30 -0
- package/hooks/use-pen-drawing.js +363 -0
- package/hooks/use-rectangle-drawing.d.ts +19 -0
- package/hooks/use-rectangle-drawing.js +136 -0
- package/hooks/use-text-editing.d.ts +19 -0
- package/hooks/use-text-editing.js +153 -0
- package/icons/index.d.ts +15 -0
- package/icons/index.js +209 -0
- package/index.cjs.css +1 -0
- package/index.cjs.js +5033 -0
- package/index.css +1 -0
- package/index.d.ts +5 -0
- package/index.js +4 -0
- package/package.json +40 -0
- package/types/callback.types.d.ts +7 -0
- package/types/canvas.types.d.ts +35 -0
- package/types/component-props.types.d.ts +41 -0
- package/types/history.types.d.ts +24 -0
- package/types/index.d.ts +5 -0
- package/types/react-flow.types.d.ts +30 -0
- package/typings.d.ts +140 -0
- package/typings.js +37 -0
- package/utils/api-data-transform.d.ts +33 -0
- package/utils/api-data-transform.js +218 -0
- package/utils/color-utils.d.ts +17 -0
- package/utils/color-utils.js +55 -0
- package/utils/debug-logger.d.ts +33 -0
- package/utils/debug-logger.js +41 -0
- package/utils/map-data-transform.d.ts +18 -0
- package/utils/map-data-transform.js +342 -0
- package/utils/node-operations.d.ts +26 -0
- package/utils/node-operations.js +83 -0
- package/utils/node-utils.d.ts +10 -0
- package/utils/node-utils.js +37 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Node } from '@xyflow/react';
|
|
3
|
+
import { EditMode } from '../typings';
|
|
4
|
+
interface UseContextMenuProps {
|
|
5
|
+
id: string;
|
|
6
|
+
editMode: EditMode;
|
|
7
|
+
isEditable: boolean;
|
|
8
|
+
nodeType?: 'rectangleNode' | 'pathNode' | 'imageNode';
|
|
9
|
+
}
|
|
10
|
+
interface ArrangeStates {
|
|
11
|
+
canBringToFront: boolean;
|
|
12
|
+
canBringForward: boolean;
|
|
13
|
+
canSendBackward: boolean;
|
|
14
|
+
canSendToBack: boolean;
|
|
15
|
+
}
|
|
16
|
+
interface ContextMenuState {
|
|
17
|
+
visible: boolean;
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
}
|
|
21
|
+
interface UseContextMenuReturn {
|
|
22
|
+
contextMenu: ContextMenuState;
|
|
23
|
+
handleContextMenu: (event: React.MouseEvent) => void;
|
|
24
|
+
handleCloseContextMenu: () => void;
|
|
25
|
+
handleDelete: () => void;
|
|
26
|
+
arrangeActions: {
|
|
27
|
+
onBringToFront: () => void;
|
|
28
|
+
onBringForward: () => void;
|
|
29
|
+
onSendBackward: () => void;
|
|
30
|
+
onSendToBack: () => void;
|
|
31
|
+
};
|
|
32
|
+
arrangeStates: ArrangeStates;
|
|
33
|
+
getNodes: () => Node[];
|
|
34
|
+
setNodes: (nodes: Node[] | ((nodes: Node[]) => Node[])) => void;
|
|
35
|
+
}
|
|
36
|
+
export declare const useContextMenu: ({ id, editMode, isEditable, nodeType }: UseContextMenuProps) => UseContextMenuReturn;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
import { useReactFlow } from '@xyflow/react';
|
|
3
|
+
import { EditMode } from '../typings.js';
|
|
4
|
+
|
|
5
|
+
const useContextMenu = ({ id, editMode, isEditable, nodeType })=>{
|
|
6
|
+
const { setNodes, getNodes } = useReactFlow();
|
|
7
|
+
// Calculate arrange states based on current node position
|
|
8
|
+
const getArrangeStates = useCallback(()=>{
|
|
9
|
+
const nodes = getNodes();
|
|
10
|
+
const currentNode = nodes.find((node)=>node.id === id);
|
|
11
|
+
if (!currentNode) {
|
|
12
|
+
return {
|
|
13
|
+
canBringToFront: false,
|
|
14
|
+
canBringForward: false,
|
|
15
|
+
canSendBackward: false,
|
|
16
|
+
canSendToBack: false
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const currentZ = currentNode.zIndex || 0;
|
|
20
|
+
const allZIndexes = nodes.map((n)=>n.zIndex || 0).sort((a, b)=>a - b);
|
|
21
|
+
const maxZ = Math.max(...allZIndexes);
|
|
22
|
+
const minZ = Math.min(...allZIndexes);
|
|
23
|
+
// Check if there are nodes above (higher zIndex)
|
|
24
|
+
const nodesAbove = nodes.filter((n)=>(n.zIndex || 0) > currentZ);
|
|
25
|
+
const nodesBelow = nodes.filter((n)=>(n.zIndex || 0) < currentZ);
|
|
26
|
+
return {
|
|
27
|
+
canBringToFront: currentZ < maxZ,
|
|
28
|
+
canBringForward: nodesAbove.length > 0,
|
|
29
|
+
canSendBackward: nodesBelow.length > 0,
|
|
30
|
+
canSendToBack: currentZ > minZ
|
|
31
|
+
};
|
|
32
|
+
}, [
|
|
33
|
+
id,
|
|
34
|
+
getNodes
|
|
35
|
+
]);
|
|
36
|
+
const [contextMenu, setContextMenu] = useState({
|
|
37
|
+
visible: false,
|
|
38
|
+
x: 0,
|
|
39
|
+
y: 0
|
|
40
|
+
});
|
|
41
|
+
// Handle right click for context menu
|
|
42
|
+
const handleContextMenu = useCallback((event)=>{
|
|
43
|
+
// For image nodes, show context menu in BACKGROUND mode
|
|
44
|
+
// For rectangle/path nodes, show context menu in LAYER mode
|
|
45
|
+
const shouldShowMenu = nodeType === 'imageNode' && editMode === EditMode.BACKGROUND || (nodeType === 'rectangleNode' || nodeType === 'pathNode') && editMode === EditMode.LAYER && isEditable;
|
|
46
|
+
if (!shouldShowMenu) return;
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
event.stopPropagation();
|
|
49
|
+
setContextMenu({
|
|
50
|
+
visible: true,
|
|
51
|
+
x: event.clientX,
|
|
52
|
+
y: event.clientY
|
|
53
|
+
});
|
|
54
|
+
}, [
|
|
55
|
+
isEditable,
|
|
56
|
+
editMode,
|
|
57
|
+
nodeType
|
|
58
|
+
]);
|
|
59
|
+
// Handle context menu actions
|
|
60
|
+
const handleCloseContextMenu = useCallback(()=>{
|
|
61
|
+
setContextMenu({
|
|
62
|
+
visible: false,
|
|
63
|
+
x: 0,
|
|
64
|
+
y: 0
|
|
65
|
+
});
|
|
66
|
+
}, []);
|
|
67
|
+
const handleDelete = useCallback(()=>{
|
|
68
|
+
setNodes((nodes)=>nodes.filter((node)=>node.id !== id));
|
|
69
|
+
handleCloseContextMenu();
|
|
70
|
+
}, [
|
|
71
|
+
id,
|
|
72
|
+
setNodes,
|
|
73
|
+
handleCloseContextMenu
|
|
74
|
+
]);
|
|
75
|
+
// Arrange functionality
|
|
76
|
+
const handleBringToFront = useCallback(()=>{
|
|
77
|
+
console.log('Bringing to front:', id);
|
|
78
|
+
setNodes((nodes)=>{
|
|
79
|
+
console.log('Current nodes:', nodes.map((n)=>n.id));
|
|
80
|
+
const nodeToMove = nodes.find((node)=>node.id === id);
|
|
81
|
+
if (!nodeToMove) {
|
|
82
|
+
console.log('Node not found');
|
|
83
|
+
return nodes;
|
|
84
|
+
}
|
|
85
|
+
const otherNodes = nodes.filter((node)=>node.id !== id);
|
|
86
|
+
// Calculate highest zIndex
|
|
87
|
+
const maxZIndex = Math.max(...nodes.map((n)=>n.zIndex || 0), 0);
|
|
88
|
+
const updatedNode = {
|
|
89
|
+
...nodeToMove,
|
|
90
|
+
zIndex: maxZIndex + 1
|
|
91
|
+
};
|
|
92
|
+
const newNodes = [
|
|
93
|
+
...otherNodes,
|
|
94
|
+
updatedNode
|
|
95
|
+
]; // Move to end (front) with higher zIndex
|
|
96
|
+
console.log('New nodes order:', newNodes.map((n)=>`${n.id}(z:${n.zIndex || 0})`));
|
|
97
|
+
return newNodes;
|
|
98
|
+
});
|
|
99
|
+
handleCloseContextMenu();
|
|
100
|
+
}, [
|
|
101
|
+
id,
|
|
102
|
+
setNodes,
|
|
103
|
+
handleCloseContextMenu
|
|
104
|
+
]);
|
|
105
|
+
const handleBringForward = useCallback(()=>{
|
|
106
|
+
console.log('Bringing forward:', id);
|
|
107
|
+
setNodes((nodes)=>{
|
|
108
|
+
const currentNode = nodes.find((node)=>node.id === id);
|
|
109
|
+
if (!currentNode) {
|
|
110
|
+
console.log('Node not found');
|
|
111
|
+
return nodes;
|
|
112
|
+
}
|
|
113
|
+
// Find the node immediately in front (higher zIndex)
|
|
114
|
+
const currentZ = currentNode.zIndex || 0;
|
|
115
|
+
const nodesAbove = nodes.filter((n)=>(n.zIndex || 0) > currentZ);
|
|
116
|
+
if (nodesAbove.length === 0) {
|
|
117
|
+
console.log('Already at front');
|
|
118
|
+
return nodes;
|
|
119
|
+
}
|
|
120
|
+
// Find the node with the lowest zIndex above current node
|
|
121
|
+
const nextNode = nodesAbove.reduce((min, node)=>(node.zIndex || 0) < (min.zIndex || 0) ? node : min);
|
|
122
|
+
// Swap zIndex values
|
|
123
|
+
return nodes.map((node)=>{
|
|
124
|
+
if (node.id === id) {
|
|
125
|
+
return {
|
|
126
|
+
...node,
|
|
127
|
+
zIndex: nextNode.zIndex
|
|
128
|
+
};
|
|
129
|
+
} else if (node.id === nextNode.id) {
|
|
130
|
+
return {
|
|
131
|
+
...node,
|
|
132
|
+
zIndex: currentZ
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return node;
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
handleCloseContextMenu();
|
|
139
|
+
}, [
|
|
140
|
+
id,
|
|
141
|
+
setNodes,
|
|
142
|
+
handleCloseContextMenu
|
|
143
|
+
]);
|
|
144
|
+
const handleSendBackward = useCallback(()=>{
|
|
145
|
+
console.log('Sending backward:', id);
|
|
146
|
+
setNodes((nodes)=>{
|
|
147
|
+
const currentNode = nodes.find((node)=>node.id === id);
|
|
148
|
+
if (!currentNode) {
|
|
149
|
+
console.log('Node not found');
|
|
150
|
+
return nodes;
|
|
151
|
+
}
|
|
152
|
+
// Find the node immediately behind (lower zIndex)
|
|
153
|
+
const currentZ = currentNode.zIndex || 0;
|
|
154
|
+
const nodesBelow = nodes.filter((n)=>(n.zIndex || 0) < currentZ);
|
|
155
|
+
if (nodesBelow.length === 0) {
|
|
156
|
+
console.log('Already at back');
|
|
157
|
+
return nodes;
|
|
158
|
+
}
|
|
159
|
+
// Find the node with the highest zIndex below current node
|
|
160
|
+
const prevNode = nodesBelow.reduce((max, node)=>(node.zIndex || 0) > (max.zIndex || 0) ? node : max);
|
|
161
|
+
// Swap zIndex values
|
|
162
|
+
return nodes.map((node)=>{
|
|
163
|
+
if (node.id === id) {
|
|
164
|
+
return {
|
|
165
|
+
...node,
|
|
166
|
+
zIndex: prevNode.zIndex
|
|
167
|
+
};
|
|
168
|
+
} else if (node.id === prevNode.id) {
|
|
169
|
+
return {
|
|
170
|
+
...node,
|
|
171
|
+
zIndex: currentZ
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
return node;
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
handleCloseContextMenu();
|
|
178
|
+
}, [
|
|
179
|
+
id,
|
|
180
|
+
setNodes,
|
|
181
|
+
handleCloseContextMenu
|
|
182
|
+
]);
|
|
183
|
+
const handleSendToBack = useCallback(()=>{
|
|
184
|
+
console.log('Sending to back:', id);
|
|
185
|
+
setNodes((nodes)=>{
|
|
186
|
+
const nodeToMove = nodes.find((node)=>node.id === id);
|
|
187
|
+
if (!nodeToMove) {
|
|
188
|
+
console.log('Node not found');
|
|
189
|
+
return nodes;
|
|
190
|
+
}
|
|
191
|
+
const otherNodes = nodes.filter((node)=>node.id !== id);
|
|
192
|
+
// Calculate lowest zIndex
|
|
193
|
+
const minZIndex = Math.min(...nodes.map((n)=>n.zIndex || 0), 0);
|
|
194
|
+
const updatedNode = {
|
|
195
|
+
...nodeToMove,
|
|
196
|
+
zIndex: minZIndex - 1
|
|
197
|
+
};
|
|
198
|
+
const newNodes = [
|
|
199
|
+
updatedNode,
|
|
200
|
+
...otherNodes
|
|
201
|
+
]; // Move to beginning (back) with lower zIndex
|
|
202
|
+
console.log('New nodes order:', newNodes.map((n)=>`${n.id}(z:${n.zIndex || 0})`));
|
|
203
|
+
return newNodes;
|
|
204
|
+
});
|
|
205
|
+
handleCloseContextMenu();
|
|
206
|
+
}, [
|
|
207
|
+
id,
|
|
208
|
+
setNodes,
|
|
209
|
+
handleCloseContextMenu
|
|
210
|
+
]);
|
|
211
|
+
return {
|
|
212
|
+
contextMenu,
|
|
213
|
+
handleContextMenu,
|
|
214
|
+
handleCloseContextMenu,
|
|
215
|
+
handleDelete,
|
|
216
|
+
arrangeActions: {
|
|
217
|
+
onBringToFront: handleBringToFront,
|
|
218
|
+
onBringForward: handleBringForward,
|
|
219
|
+
onSendBackward: handleSendBackward,
|
|
220
|
+
onSendToBack: handleSendToBack
|
|
221
|
+
},
|
|
222
|
+
arrangeStates: getArrangeStates(),
|
|
223
|
+
getNodes,
|
|
224
|
+
setNodes
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
export { useContextMenu };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { EditMode } from '../typings';
|
|
2
|
+
import { FlowNode, FlowEdge } from '../types/react-flow.types';
|
|
3
|
+
interface HistoryState {
|
|
4
|
+
nodes: FlowNode[];
|
|
5
|
+
edges: FlowEdge[];
|
|
6
|
+
editMode: EditMode;
|
|
7
|
+
operation: string;
|
|
8
|
+
timestamp: number;
|
|
9
|
+
}
|
|
10
|
+
interface UseDirectStateHistoryOptions {
|
|
11
|
+
maxHistorySize?: number;
|
|
12
|
+
debugMode?: boolean;
|
|
13
|
+
}
|
|
14
|
+
interface HistorySummary {
|
|
15
|
+
canUndo: boolean;
|
|
16
|
+
canRedo: boolean;
|
|
17
|
+
historyLength: number;
|
|
18
|
+
currentIndex: number;
|
|
19
|
+
currentOperation?: string;
|
|
20
|
+
operations?: Array<{
|
|
21
|
+
index: number;
|
|
22
|
+
operation: string;
|
|
23
|
+
nodes: number;
|
|
24
|
+
edges: number;
|
|
25
|
+
isCurrent: boolean;
|
|
26
|
+
}>;
|
|
27
|
+
}
|
|
28
|
+
interface UseDirectStateHistoryReturn {
|
|
29
|
+
saveState: (nodes: FlowNode[], edges: FlowEdge[], operation: string, editMode: EditMode) => void;
|
|
30
|
+
undo: () => {
|
|
31
|
+
nodes: FlowNode[];
|
|
32
|
+
edges: FlowEdge[];
|
|
33
|
+
editMode: EditMode;
|
|
34
|
+
} | null;
|
|
35
|
+
redo: () => {
|
|
36
|
+
nodes: FlowNode[];
|
|
37
|
+
edges: FlowEdge[];
|
|
38
|
+
editMode: EditMode;
|
|
39
|
+
} | null;
|
|
40
|
+
canUndo: boolean;
|
|
41
|
+
canRedo: boolean;
|
|
42
|
+
initializeHistory: (initialNodes: FlowNode[], initialEdges: FlowEdge[], editMode: EditMode) => void;
|
|
43
|
+
clearHistory: () => void;
|
|
44
|
+
getHistorySummary: () => HistorySummary;
|
|
45
|
+
history?: HistoryState[];
|
|
46
|
+
currentIndex?: number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 直接狀態歷史系統
|
|
50
|
+
*
|
|
51
|
+
* 核心概念:
|
|
52
|
+
* - 直接記錄 nodes 和 edges 的完整狀態
|
|
53
|
+
* - 每個用戶操作後保存一份狀態快照
|
|
54
|
+
* - undo/redo 就是直接設置 nodes 和 edges
|
|
55
|
+
* - 簡單、直接、可靠
|
|
56
|
+
*/
|
|
57
|
+
export declare const useDirectStateHistory: ({ maxHistorySize, debugMode, }?: UseDirectStateHistoryOptions) => UseDirectStateHistoryReturn;
|
|
58
|
+
export {};
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
import { useState, useRef, useCallback } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 直接狀態歷史系統
|
|
5
|
+
*
|
|
6
|
+
* 核心概念:
|
|
7
|
+
* - 直接記錄 nodes 和 edges 的完整狀態
|
|
8
|
+
* - 每個用戶操作後保存一份狀態快照
|
|
9
|
+
* - undo/redo 就是直接設置 nodes 和 edges
|
|
10
|
+
* - 簡單、直接、可靠
|
|
11
|
+
*/ const useDirectStateHistory = ({ maxHistorySize = 50, debugMode = false } = {})=>{
|
|
12
|
+
const [history, setHistory] = useState([]);
|
|
13
|
+
const [currentIndex, setCurrentIndex] = useState(-1);
|
|
14
|
+
const isRestoringRef = useRef(false);
|
|
15
|
+
const canUndo = currentIndex > 0;
|
|
16
|
+
const canRedo = currentIndex < history.length - 1;
|
|
17
|
+
// 初始化歷史記錄
|
|
18
|
+
const initializeHistory = useCallback((initialNodes, initialEdges, editMode)=>{
|
|
19
|
+
const initialState = {
|
|
20
|
+
nodes: JSON.parse(JSON.stringify(initialNodes)),
|
|
21
|
+
edges: JSON.parse(JSON.stringify(initialEdges)),
|
|
22
|
+
editMode,
|
|
23
|
+
operation: 'init',
|
|
24
|
+
timestamp: Date.now()
|
|
25
|
+
};
|
|
26
|
+
setHistory([
|
|
27
|
+
initialState
|
|
28
|
+
]);
|
|
29
|
+
setCurrentIndex(0);
|
|
30
|
+
if (debugMode) {
|
|
31
|
+
console.log('🎯 直接狀態歷史系統初始化:', {
|
|
32
|
+
nodes: initialNodes.length,
|
|
33
|
+
edges: initialEdges.length,
|
|
34
|
+
editMode
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}, [
|
|
38
|
+
debugMode
|
|
39
|
+
]);
|
|
40
|
+
// 保存狀態快照
|
|
41
|
+
const saveState = useCallback((nodes, edges, operation, editMode)=>{
|
|
42
|
+
if (isRestoringRef.current) {
|
|
43
|
+
if (debugMode) {
|
|
44
|
+
console.log('🚫 跳過保存 - 正在執行 undo/redo');
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const newState = {
|
|
49
|
+
nodes: JSON.parse(JSON.stringify(nodes)),
|
|
50
|
+
edges: JSON.parse(JSON.stringify(edges)),
|
|
51
|
+
editMode,
|
|
52
|
+
operation,
|
|
53
|
+
timestamp: Date.now()
|
|
54
|
+
};
|
|
55
|
+
setHistory((prevHistory)=>{
|
|
56
|
+
// 如果當前不在最後位置,截斷後續歷史
|
|
57
|
+
const truncatedHistory = currentIndex < prevHistory.length - 1 ? prevHistory.slice(0, currentIndex + 1) : prevHistory;
|
|
58
|
+
// 添加新狀態
|
|
59
|
+
const historyWithNewState = [
|
|
60
|
+
...truncatedHistory,
|
|
61
|
+
newState
|
|
62
|
+
];
|
|
63
|
+
// 限制歷史大小
|
|
64
|
+
const finalHistory = historyWithNewState.length > maxHistorySize ? (()=>{
|
|
65
|
+
const removeCount = historyWithNewState.length - maxHistorySize;
|
|
66
|
+
const trimmedHistory = historyWithNewState.slice(removeCount);
|
|
67
|
+
setCurrentIndex(trimmedHistory.length - 1);
|
|
68
|
+
return trimmedHistory;
|
|
69
|
+
})() : (()=>{
|
|
70
|
+
setCurrentIndex(historyWithNewState.length - 1);
|
|
71
|
+
return historyWithNewState;
|
|
72
|
+
})();
|
|
73
|
+
if (debugMode) {
|
|
74
|
+
console.log(`📸 保存狀態 [${operation}]:`, {
|
|
75
|
+
nodes: nodes.length,
|
|
76
|
+
edges: edges.length,
|
|
77
|
+
editMode,
|
|
78
|
+
historyIndex: finalHistory.length - 1,
|
|
79
|
+
totalHistory: finalHistory.length
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
return finalHistory;
|
|
83
|
+
});
|
|
84
|
+
}, [
|
|
85
|
+
currentIndex,
|
|
86
|
+
maxHistorySize,
|
|
87
|
+
debugMode
|
|
88
|
+
]);
|
|
89
|
+
// Undo 操作
|
|
90
|
+
const undo = useCallback(()=>{
|
|
91
|
+
if (!canUndo) {
|
|
92
|
+
if (debugMode) {
|
|
93
|
+
console.log('⚠️ 無法 undo - 已在歷史開始');
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
const targetIndex = currentIndex - 1;
|
|
98
|
+
const targetState = history[targetIndex];
|
|
99
|
+
if (!targetState) return null;
|
|
100
|
+
isRestoringRef.current = true;
|
|
101
|
+
setCurrentIndex(targetIndex);
|
|
102
|
+
if (debugMode) {
|
|
103
|
+
console.log(`↶ Undo 到 [${targetState.operation}]:`, {
|
|
104
|
+
fromIndex: currentIndex,
|
|
105
|
+
toIndex: targetIndex,
|
|
106
|
+
nodes: targetState.nodes.length,
|
|
107
|
+
edges: targetState.edges.length,
|
|
108
|
+
editMode: targetState.editMode
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
// 下一個事件循環解除恢復標記
|
|
112
|
+
setTimeout(()=>{
|
|
113
|
+
isRestoringRef.current = false;
|
|
114
|
+
}, 0);
|
|
115
|
+
return {
|
|
116
|
+
nodes: targetState.nodes,
|
|
117
|
+
edges: targetState.edges,
|
|
118
|
+
editMode: targetState.editMode
|
|
119
|
+
};
|
|
120
|
+
}, [
|
|
121
|
+
canUndo,
|
|
122
|
+
currentIndex,
|
|
123
|
+
history,
|
|
124
|
+
debugMode
|
|
125
|
+
]);
|
|
126
|
+
// Redo 操作
|
|
127
|
+
const redo = useCallback(()=>{
|
|
128
|
+
if (!canRedo) {
|
|
129
|
+
if (debugMode) {
|
|
130
|
+
console.log('⚠️ 無法 redo - 已在歷史末端');
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
const targetIndex = currentIndex + 1;
|
|
135
|
+
const targetState = history[targetIndex];
|
|
136
|
+
if (!targetState) return null;
|
|
137
|
+
isRestoringRef.current = true;
|
|
138
|
+
setCurrentIndex(targetIndex);
|
|
139
|
+
if (debugMode) {
|
|
140
|
+
console.log(`↷ Redo 到 [${targetState.operation}]:`, {
|
|
141
|
+
fromIndex: currentIndex,
|
|
142
|
+
toIndex: targetIndex,
|
|
143
|
+
nodes: targetState.nodes.length,
|
|
144
|
+
edges: targetState.edges.length,
|
|
145
|
+
editMode: targetState.editMode
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
// 下一個事件循環解除恢復標記
|
|
149
|
+
setTimeout(()=>{
|
|
150
|
+
isRestoringRef.current = false;
|
|
151
|
+
}, 0);
|
|
152
|
+
return {
|
|
153
|
+
nodes: targetState.nodes,
|
|
154
|
+
edges: targetState.edges,
|
|
155
|
+
editMode: targetState.editMode
|
|
156
|
+
};
|
|
157
|
+
}, [
|
|
158
|
+
canRedo,
|
|
159
|
+
currentIndex,
|
|
160
|
+
history,
|
|
161
|
+
debugMode
|
|
162
|
+
]);
|
|
163
|
+
// 獲取歷史摘要
|
|
164
|
+
const getHistorySummary = useCallback(()=>{
|
|
165
|
+
return {
|
|
166
|
+
canUndo,
|
|
167
|
+
canRedo,
|
|
168
|
+
historyLength: history.length,
|
|
169
|
+
currentIndex,
|
|
170
|
+
currentOperation: currentIndex >= 0 ? history[currentIndex]?.operation : undefined,
|
|
171
|
+
operations: debugMode ? history.map((h, index)=>({
|
|
172
|
+
index,
|
|
173
|
+
operation: h.operation,
|
|
174
|
+
nodes: h.nodes.length,
|
|
175
|
+
edges: h.edges.length,
|
|
176
|
+
isCurrent: index === currentIndex
|
|
177
|
+
})) : undefined
|
|
178
|
+
};
|
|
179
|
+
}, [
|
|
180
|
+
canUndo,
|
|
181
|
+
canRedo,
|
|
182
|
+
history,
|
|
183
|
+
currentIndex,
|
|
184
|
+
debugMode
|
|
185
|
+
]);
|
|
186
|
+
// 清除歷史
|
|
187
|
+
const clearHistory = useCallback(()=>{
|
|
188
|
+
setHistory([]);
|
|
189
|
+
setCurrentIndex(-1);
|
|
190
|
+
if (debugMode) {
|
|
191
|
+
console.log('🗑️ 清除歷史記錄');
|
|
192
|
+
}
|
|
193
|
+
}, [
|
|
194
|
+
debugMode
|
|
195
|
+
]);
|
|
196
|
+
return {
|
|
197
|
+
// 核心功能
|
|
198
|
+
saveState,
|
|
199
|
+
undo,
|
|
200
|
+
redo,
|
|
201
|
+
// 狀態查詢
|
|
202
|
+
canUndo,
|
|
203
|
+
canRedo,
|
|
204
|
+
// 工具函數
|
|
205
|
+
initializeHistory,
|
|
206
|
+
clearHistory,
|
|
207
|
+
getHistorySummary,
|
|
208
|
+
// 調試信息
|
|
209
|
+
history: debugMode ? history : undefined,
|
|
210
|
+
currentIndex: debugMode ? currentIndex : undefined
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
export { useDirectStateHistory };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { EditMode, DrawingMode } from '../typings';
|
|
3
|
+
interface UsePenDrawingProps {
|
|
4
|
+
editMode: EditMode;
|
|
5
|
+
drawingMode: DrawingMode;
|
|
6
|
+
onCreatePath: (points: {
|
|
7
|
+
x: number;
|
|
8
|
+
y: number;
|
|
9
|
+
}[]) => void;
|
|
10
|
+
}
|
|
11
|
+
interface UsePenDrawingReturn {
|
|
12
|
+
containerRef: React.RefObject<HTMLDivElement | null>;
|
|
13
|
+
isDrawing: boolean;
|
|
14
|
+
previewPath: {
|
|
15
|
+
x: number;
|
|
16
|
+
y: number;
|
|
17
|
+
}[] | null;
|
|
18
|
+
currentPoints: {
|
|
19
|
+
x: number;
|
|
20
|
+
y: number;
|
|
21
|
+
}[];
|
|
22
|
+
firstPoint: {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
25
|
+
} | null;
|
|
26
|
+
canClose: boolean;
|
|
27
|
+
forceComplete: () => void;
|
|
28
|
+
}
|
|
29
|
+
export declare const usePenDrawing: ({ editMode, drawingMode, onCreatePath }: UsePenDrawingProps) => UsePenDrawingReturn;
|
|
30
|
+
export {};
|