@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,363 @@
|
|
|
1
|
+
import { useRef, useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import { useReactFlow } from '@xyflow/react';
|
|
3
|
+
import { DrawingMode, EditMode } from '../typings.js';
|
|
4
|
+
|
|
5
|
+
// Utility function to constrain point to straight lines (horizontal, vertical, 45-degree)
|
|
6
|
+
const constrainToStraightLine = (currentPoint, previousPoint)=>{
|
|
7
|
+
const dx = currentPoint.x - previousPoint.x;
|
|
8
|
+
const dy = currentPoint.y - previousPoint.y;
|
|
9
|
+
// Calculate angle in degrees
|
|
10
|
+
const angle = Math.atan2(dy, dx) * (180 / Math.PI);
|
|
11
|
+
// Normalize angle to 0-360 range
|
|
12
|
+
const normalizedAngle = (angle % 360 + 360) % 360;
|
|
13
|
+
// Snap to nearest cardinal/diagonal direction (every 45 degrees)
|
|
14
|
+
const snapAngle = Math.round(normalizedAngle / 45) * 45;
|
|
15
|
+
// Calculate distance from previous point
|
|
16
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
17
|
+
// Convert back to radians and calculate constrained position
|
|
18
|
+
const constrainedAngle = snapAngle * Math.PI / 180;
|
|
19
|
+
return {
|
|
20
|
+
x: previousPoint.x + Math.cos(constrainedAngle) * distance,
|
|
21
|
+
y: previousPoint.y + Math.sin(constrainedAngle) * distance
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
const usePenDrawing = ({ editMode, drawingMode, onCreatePath })=>{
|
|
25
|
+
const { screenToFlowPosition } = useReactFlow();
|
|
26
|
+
const containerRef = useRef(null);
|
|
27
|
+
const previousModeRef = useRef(drawingMode);
|
|
28
|
+
const previousEditModeRef = useRef(editMode);
|
|
29
|
+
const [drawingState, setDrawingState] = useState({
|
|
30
|
+
isDrawing: false,
|
|
31
|
+
points: [],
|
|
32
|
+
screenPoints: [],
|
|
33
|
+
currentMousePos: null,
|
|
34
|
+
lastClickTime: 0,
|
|
35
|
+
isShiftPressed: false
|
|
36
|
+
});
|
|
37
|
+
const handleClick = useCallback((event)=>{
|
|
38
|
+
if (drawingMode !== DrawingMode.PEN || editMode !== EditMode.LAYER) return;
|
|
39
|
+
// IMMEDIATELY stop event propagation to prevent React Flow pane click
|
|
40
|
+
event.stopPropagation();
|
|
41
|
+
event.preventDefault();
|
|
42
|
+
event.stopImmediatePropagation();
|
|
43
|
+
// Debug log for shift key behavior
|
|
44
|
+
if (event.shiftKey) {
|
|
45
|
+
console.log('🖱️ Pen tool click with Shift key pressed - event stopped', {
|
|
46
|
+
isDrawing: drawingState.isDrawing,
|
|
47
|
+
pointsCount: drawingState.points.length,
|
|
48
|
+
lastClickTime: drawingState.lastClickTime,
|
|
49
|
+
currentTime: Date.now()
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
const currentTime = Date.now();
|
|
53
|
+
const wrapper = containerRef.current;
|
|
54
|
+
if (!wrapper) return;
|
|
55
|
+
const rect = wrapper.getBoundingClientRect();
|
|
56
|
+
const screenX = event.clientX - rect.left;
|
|
57
|
+
const screenY = event.clientY - rect.top;
|
|
58
|
+
const basePosition = screenToFlowPosition({
|
|
59
|
+
x: event.clientX,
|
|
60
|
+
y: event.clientY
|
|
61
|
+
});
|
|
62
|
+
const baseScreenPos = {
|
|
63
|
+
x: screenX,
|
|
64
|
+
y: screenY
|
|
65
|
+
};
|
|
66
|
+
// Apply straight line constraint if Shift is pressed and we have a previous point
|
|
67
|
+
const { position, constrainedScreenPos } = (()=>{
|
|
68
|
+
if (!event.shiftKey || drawingState.points.length === 0) {
|
|
69
|
+
return {
|
|
70
|
+
position: basePosition,
|
|
71
|
+
constrainedScreenPos: baseScreenPos
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const previousPoint = drawingState.points[drawingState.points.length - 1];
|
|
75
|
+
const constrainedPosition = constrainToStraightLine(basePosition, previousPoint);
|
|
76
|
+
// Also constrain the screen position for consistent preview
|
|
77
|
+
const constrainedScreen = drawingState.screenPoints.length > 0 ? (()=>{
|
|
78
|
+
const previousScreenPoint = drawingState.screenPoints[drawingState.screenPoints.length - 1];
|
|
79
|
+
return constrainToStraightLine(baseScreenPos, previousScreenPoint);
|
|
80
|
+
})() : baseScreenPos;
|
|
81
|
+
return {
|
|
82
|
+
position: constrainedPosition,
|
|
83
|
+
constrainedScreenPos: constrainedScreen
|
|
84
|
+
};
|
|
85
|
+
})();
|
|
86
|
+
// Check for double-click (within 300ms)
|
|
87
|
+
const isDoubleClick = currentTime - drawingState.lastClickTime < 300;
|
|
88
|
+
// Check if clicking on the first point to close the path (only if we have at least 3 points)
|
|
89
|
+
// IMPORTANT: Use original screen position, not constrained position, to avoid accidental closing when using Shift
|
|
90
|
+
const isClickingFirstPoint = (()=>{
|
|
91
|
+
if (!drawingState.isDrawing || drawingState.points.length < 3) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
const firstPoint = drawingState.screenPoints[0];
|
|
95
|
+
const distance = Math.sqrt(Math.pow(screenX - firstPoint.x, 2) + Math.pow(screenY - firstPoint.y, 2));
|
|
96
|
+
// Consider it a click on the first point if within 10 pixels
|
|
97
|
+
return distance <= 10;
|
|
98
|
+
})();
|
|
99
|
+
if (isClickingFirstPoint) {
|
|
100
|
+
console.log('🔵 Pen tool: Closing path by clicking first point', {
|
|
101
|
+
shiftKey: event.shiftKey,
|
|
102
|
+
pointsCount: drawingState.points.length
|
|
103
|
+
});
|
|
104
|
+
// Close the path by adding the first point at the end
|
|
105
|
+
const closedPath = [
|
|
106
|
+
...drawingState.points,
|
|
107
|
+
drawingState.points[0]
|
|
108
|
+
];
|
|
109
|
+
onCreatePath(closedPath);
|
|
110
|
+
// Reset drawing state
|
|
111
|
+
setDrawingState({
|
|
112
|
+
isDrawing: false,
|
|
113
|
+
points: [],
|
|
114
|
+
screenPoints: [],
|
|
115
|
+
currentMousePos: null,
|
|
116
|
+
lastClickTime: 0,
|
|
117
|
+
isShiftPressed: false
|
|
118
|
+
});
|
|
119
|
+
} else if (isDoubleClick && drawingState.isDrawing) {
|
|
120
|
+
console.log('🔵 Pen tool: Completing path by double-click', {
|
|
121
|
+
shiftKey: event.shiftKey,
|
|
122
|
+
pointsCount: drawingState.points.length,
|
|
123
|
+
timeDiff: currentTime - drawingState.lastClickTime
|
|
124
|
+
});
|
|
125
|
+
// Complete and close the path if we have at least 3 points
|
|
126
|
+
if (drawingState.points.length >= 3) {
|
|
127
|
+
// Close the path by adding the first point at the end
|
|
128
|
+
const closedPath = [
|
|
129
|
+
...drawingState.points,
|
|
130
|
+
drawingState.points[0]
|
|
131
|
+
];
|
|
132
|
+
onCreatePath(closedPath);
|
|
133
|
+
} else if (drawingState.points.length >= 2) {
|
|
134
|
+
// If only 2 points, create an open path
|
|
135
|
+
onCreatePath(drawingState.points);
|
|
136
|
+
}
|
|
137
|
+
// Reset drawing state
|
|
138
|
+
setDrawingState({
|
|
139
|
+
isDrawing: false,
|
|
140
|
+
points: [],
|
|
141
|
+
screenPoints: [],
|
|
142
|
+
currentMousePos: null,
|
|
143
|
+
lastClickTime: 0,
|
|
144
|
+
isShiftPressed: false
|
|
145
|
+
});
|
|
146
|
+
} else {
|
|
147
|
+
// Regular click - add point
|
|
148
|
+
setDrawingState((prev)=>({
|
|
149
|
+
...prev,
|
|
150
|
+
isDrawing: true,
|
|
151
|
+
points: [
|
|
152
|
+
...prev.points,
|
|
153
|
+
position
|
|
154
|
+
],
|
|
155
|
+
screenPoints: [
|
|
156
|
+
...prev.screenPoints,
|
|
157
|
+
constrainedScreenPos
|
|
158
|
+
],
|
|
159
|
+
lastClickTime: currentTime,
|
|
160
|
+
isShiftPressed: event.shiftKey
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
}, [
|
|
164
|
+
drawingMode,
|
|
165
|
+
editMode,
|
|
166
|
+
screenToFlowPosition,
|
|
167
|
+
drawingState.lastClickTime,
|
|
168
|
+
drawingState.isDrawing,
|
|
169
|
+
drawingState.points,
|
|
170
|
+
drawingState.screenPoints,
|
|
171
|
+
onCreatePath
|
|
172
|
+
]);
|
|
173
|
+
const handleMouseMove = useCallback((event)=>{
|
|
174
|
+
if (drawingMode !== DrawingMode.PEN || editMode !== EditMode.LAYER) return;
|
|
175
|
+
const wrapper = containerRef.current;
|
|
176
|
+
if (!wrapper) return;
|
|
177
|
+
const rect = wrapper.getBoundingClientRect();
|
|
178
|
+
const baseScreenX = event.clientX - rect.left;
|
|
179
|
+
const baseScreenY = event.clientY - rect.top;
|
|
180
|
+
// Apply constraint to mouse position if Shift is pressed and we're drawing
|
|
181
|
+
const { screenX, screenY } = (()=>{
|
|
182
|
+
if (!event.shiftKey || drawingState.screenPoints.length === 0) {
|
|
183
|
+
return {
|
|
184
|
+
screenX: baseScreenX,
|
|
185
|
+
screenY: baseScreenY
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
const previousScreenPoint = drawingState.screenPoints[drawingState.screenPoints.length - 1];
|
|
189
|
+
const constrainedPos = constrainToStraightLine({
|
|
190
|
+
x: baseScreenX,
|
|
191
|
+
y: baseScreenY
|
|
192
|
+
}, previousScreenPoint);
|
|
193
|
+
return {
|
|
194
|
+
screenX: constrainedPos.x,
|
|
195
|
+
screenY: constrainedPos.y
|
|
196
|
+
};
|
|
197
|
+
})();
|
|
198
|
+
setDrawingState((prev)=>({
|
|
199
|
+
...prev,
|
|
200
|
+
currentMousePos: {
|
|
201
|
+
x: screenX,
|
|
202
|
+
y: screenY
|
|
203
|
+
},
|
|
204
|
+
isShiftPressed: event.shiftKey
|
|
205
|
+
}));
|
|
206
|
+
}, [
|
|
207
|
+
drawingMode,
|
|
208
|
+
editMode,
|
|
209
|
+
drawingState.screenPoints
|
|
210
|
+
]);
|
|
211
|
+
const handleKeyDown = useCallback((event)=>{
|
|
212
|
+
if (drawingMode !== DrawingMode.PEN || editMode !== EditMode.LAYER || !drawingState.isDrawing) return;
|
|
213
|
+
// Escape key cancels current path
|
|
214
|
+
if (event.key === 'Escape') {
|
|
215
|
+
setDrawingState({
|
|
216
|
+
isDrawing: false,
|
|
217
|
+
points: [],
|
|
218
|
+
screenPoints: [],
|
|
219
|
+
currentMousePos: null,
|
|
220
|
+
lastClickTime: 0,
|
|
221
|
+
isShiftPressed: false
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
// Enter key completes and closes the path
|
|
225
|
+
if (event.key === 'Enter' && drawingState.points.length >= 2) {
|
|
226
|
+
if (drawingState.points.length >= 3) {
|
|
227
|
+
// Close the path by adding the first point at the end
|
|
228
|
+
const closedPath = [
|
|
229
|
+
...drawingState.points,
|
|
230
|
+
drawingState.points[0]
|
|
231
|
+
];
|
|
232
|
+
onCreatePath(closedPath);
|
|
233
|
+
} else {
|
|
234
|
+
// If only 2 points, create an open path
|
|
235
|
+
onCreatePath(drawingState.points);
|
|
236
|
+
}
|
|
237
|
+
setDrawingState({
|
|
238
|
+
isDrawing: false,
|
|
239
|
+
points: [],
|
|
240
|
+
screenPoints: [],
|
|
241
|
+
currentMousePos: null,
|
|
242
|
+
lastClickTime: 0,
|
|
243
|
+
isShiftPressed: false
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}, [
|
|
247
|
+
drawingMode,
|
|
248
|
+
editMode,
|
|
249
|
+
drawingState.isDrawing,
|
|
250
|
+
drawingState.points,
|
|
251
|
+
onCreatePath
|
|
252
|
+
]);
|
|
253
|
+
// Force complete current path (for auto-close on pane click)
|
|
254
|
+
const forceComplete = useCallback(()=>{
|
|
255
|
+
console.log('🔴 forceComplete called', {
|
|
256
|
+
isDrawing: drawingState.isDrawing,
|
|
257
|
+
pointsLength: drawingState.points.length,
|
|
258
|
+
willClose: drawingState.points.length >= 3
|
|
259
|
+
});
|
|
260
|
+
if (!drawingState.isDrawing || drawingState.points.length < 2) return;
|
|
261
|
+
// Auto-close the path if we have 3+ points, otherwise create open path
|
|
262
|
+
if (drawingState.points.length >= 3) {
|
|
263
|
+
console.log('🔴 Force completing with closed path');
|
|
264
|
+
const closedPath = [
|
|
265
|
+
...drawingState.points,
|
|
266
|
+
drawingState.points[0]
|
|
267
|
+
];
|
|
268
|
+
onCreatePath(closedPath);
|
|
269
|
+
} else {
|
|
270
|
+
console.log('🔴 Force completing with open path');
|
|
271
|
+
onCreatePath(drawingState.points);
|
|
272
|
+
}
|
|
273
|
+
// Reset drawing state
|
|
274
|
+
setDrawingState({
|
|
275
|
+
isDrawing: false,
|
|
276
|
+
points: [],
|
|
277
|
+
screenPoints: [],
|
|
278
|
+
currentMousePos: null,
|
|
279
|
+
lastClickTime: 0,
|
|
280
|
+
isShiftPressed: false
|
|
281
|
+
});
|
|
282
|
+
}, [
|
|
283
|
+
drawingState.isDrawing,
|
|
284
|
+
drawingState.points,
|
|
285
|
+
onCreatePath
|
|
286
|
+
]);
|
|
287
|
+
// Reset drawing state when switching away from pen mode or LAYER mode
|
|
288
|
+
useEffect(()=>{
|
|
289
|
+
const modeChanged = previousModeRef.current !== drawingMode;
|
|
290
|
+
const editModeChanged = previousEditModeRef.current !== editMode;
|
|
291
|
+
if (modeChanged || editModeChanged) {
|
|
292
|
+
setDrawingState((prev)=>{
|
|
293
|
+
// Should we force complete before resetting?
|
|
294
|
+
const shouldForceComplete = // Switching away from PEN mode
|
|
295
|
+
(previousModeRef.current === DrawingMode.PEN && drawingMode !== DrawingMode.PEN || // Switching away from LAYER mode
|
|
296
|
+
previousEditModeRef.current === EditMode.LAYER && editMode !== EditMode.LAYER) && prev.isDrawing && prev.points.length >= 2;
|
|
297
|
+
if (shouldForceComplete) {
|
|
298
|
+
// Force complete the current path before switching
|
|
299
|
+
if (prev.points.length >= 3) {
|
|
300
|
+
const closedPath = [
|
|
301
|
+
...prev.points,
|
|
302
|
+
prev.points[0]
|
|
303
|
+
];
|
|
304
|
+
onCreatePath(closedPath);
|
|
305
|
+
} else {
|
|
306
|
+
onCreatePath(prev.points);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
// Reset drawing state
|
|
310
|
+
return {
|
|
311
|
+
isDrawing: false,
|
|
312
|
+
points: [],
|
|
313
|
+
screenPoints: [],
|
|
314
|
+
currentMousePos: null,
|
|
315
|
+
lastClickTime: 0,
|
|
316
|
+
isShiftPressed: false
|
|
317
|
+
};
|
|
318
|
+
});
|
|
319
|
+
// Update refs
|
|
320
|
+
previousModeRef.current = drawingMode;
|
|
321
|
+
previousEditModeRef.current = editMode;
|
|
322
|
+
}
|
|
323
|
+
}, [
|
|
324
|
+
drawingMode,
|
|
325
|
+
editMode,
|
|
326
|
+
onCreatePath
|
|
327
|
+
]);
|
|
328
|
+
// Event listeners setup
|
|
329
|
+
useEffect(()=>{
|
|
330
|
+
const wrapper = containerRef.current;
|
|
331
|
+
if (!wrapper) return;
|
|
332
|
+
// Use capture phase to intercept clicks before React Flow processes them
|
|
333
|
+
wrapper.addEventListener('click', handleClick, true);
|
|
334
|
+
document.addEventListener('mousemove', handleMouseMove);
|
|
335
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
336
|
+
return ()=>{
|
|
337
|
+
wrapper.removeEventListener('click', handleClick, true);
|
|
338
|
+
document.removeEventListener('mousemove', handleMouseMove);
|
|
339
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
340
|
+
};
|
|
341
|
+
}, [
|
|
342
|
+
handleClick,
|
|
343
|
+
handleMouseMove,
|
|
344
|
+
handleKeyDown
|
|
345
|
+
]);
|
|
346
|
+
// Calculate preview path including current mouse position
|
|
347
|
+
const previewPath = drawingState.isDrawing && drawingState.screenPoints.length > 0 ? drawingState.currentMousePos ? [
|
|
348
|
+
...drawingState.screenPoints,
|
|
349
|
+
drawingState.currentMousePos
|
|
350
|
+
] : drawingState.screenPoints : null;
|
|
351
|
+
return {
|
|
352
|
+
containerRef,
|
|
353
|
+
isDrawing: drawingState.isDrawing,
|
|
354
|
+
previewPath,
|
|
355
|
+
currentPoints: drawingState.screenPoints,
|
|
356
|
+
// 提供起始點資訊,用於顯示閉合提示
|
|
357
|
+
firstPoint: drawingState.screenPoints.length > 0 ? drawingState.screenPoints[0] : null,
|
|
358
|
+
canClose: drawingState.isDrawing && drawingState.screenPoints.length >= 3,
|
|
359
|
+
forceComplete
|
|
360
|
+
};
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
export { usePenDrawing };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { EditMode, DrawingMode } from '../typings';
|
|
3
|
+
interface UseRectangleDrawingProps {
|
|
4
|
+
editMode: EditMode;
|
|
5
|
+
drawingMode: DrawingMode;
|
|
6
|
+
onCreateRectangle: (startX: number, startY: number, endX: number, endY: number) => void;
|
|
7
|
+
}
|
|
8
|
+
interface UseRectangleDrawingReturn {
|
|
9
|
+
containerRef: React.RefObject<HTMLDivElement | null>;
|
|
10
|
+
isDrawing: boolean;
|
|
11
|
+
previewRect: {
|
|
12
|
+
x: number;
|
|
13
|
+
y: number;
|
|
14
|
+
width: number;
|
|
15
|
+
height: number;
|
|
16
|
+
} | null;
|
|
17
|
+
}
|
|
18
|
+
export declare const useRectangleDrawing: ({ editMode, drawingMode, onCreateRectangle, }: UseRectangleDrawingProps) => UseRectangleDrawingReturn;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { useRef, useState, useCallback, useEffect } from 'react';
|
|
2
|
+
import { useReactFlow } from '@xyflow/react';
|
|
3
|
+
import { DrawingMode, EditMode } from '../typings.js';
|
|
4
|
+
import { MIN_RECTANGLE_SIZE } from '../constants/index.js';
|
|
5
|
+
|
|
6
|
+
const useRectangleDrawing = ({ editMode, drawingMode, onCreateRectangle })=>{
|
|
7
|
+
const { screenToFlowPosition } = useReactFlow();
|
|
8
|
+
const containerRef = useRef(null);
|
|
9
|
+
const [drawingState, setDrawingState] = useState({
|
|
10
|
+
isDrawing: false,
|
|
11
|
+
startPos: {
|
|
12
|
+
x: 0,
|
|
13
|
+
y: 0
|
|
14
|
+
},
|
|
15
|
+
currentPos: {
|
|
16
|
+
x: 0,
|
|
17
|
+
y: 0
|
|
18
|
+
},
|
|
19
|
+
startScreenPos: {
|
|
20
|
+
x: 0,
|
|
21
|
+
y: 0
|
|
22
|
+
},
|
|
23
|
+
currentScreenPos: {
|
|
24
|
+
x: 0,
|
|
25
|
+
y: 0
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
const handleMouseDown = useCallback((event)=>{
|
|
29
|
+
if (drawingMode !== DrawingMode.RECTANGLE || editMode !== EditMode.LAYER) return;
|
|
30
|
+
const wrapper = containerRef.current;
|
|
31
|
+
if (!wrapper) return;
|
|
32
|
+
const rect = wrapper.getBoundingClientRect();
|
|
33
|
+
const screenX = event.clientX - rect.left;
|
|
34
|
+
const screenY = event.clientY - rect.top;
|
|
35
|
+
const position = screenToFlowPosition({
|
|
36
|
+
x: event.clientX,
|
|
37
|
+
y: event.clientY
|
|
38
|
+
});
|
|
39
|
+
setDrawingState({
|
|
40
|
+
isDrawing: true,
|
|
41
|
+
startPos: position,
|
|
42
|
+
currentPos: position,
|
|
43
|
+
startScreenPos: {
|
|
44
|
+
x: screenX,
|
|
45
|
+
y: screenY
|
|
46
|
+
},
|
|
47
|
+
currentScreenPos: {
|
|
48
|
+
x: screenX,
|
|
49
|
+
y: screenY
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
}, [
|
|
53
|
+
drawingMode,
|
|
54
|
+
editMode,
|
|
55
|
+
screenToFlowPosition
|
|
56
|
+
]);
|
|
57
|
+
const handleMouseMove = useCallback((event)=>{
|
|
58
|
+
if (!drawingState.isDrawing || drawingMode !== DrawingMode.RECTANGLE) return;
|
|
59
|
+
const wrapper = containerRef.current;
|
|
60
|
+
if (!wrapper) return;
|
|
61
|
+
const rect = wrapper.getBoundingClientRect();
|
|
62
|
+
const screenX = event.clientX - rect.left;
|
|
63
|
+
const screenY = event.clientY - rect.top;
|
|
64
|
+
const position = screenToFlowPosition({
|
|
65
|
+
x: event.clientX,
|
|
66
|
+
y: event.clientY
|
|
67
|
+
});
|
|
68
|
+
setDrawingState((prev)=>({
|
|
69
|
+
...prev,
|
|
70
|
+
currentPos: position,
|
|
71
|
+
currentScreenPos: {
|
|
72
|
+
x: screenX,
|
|
73
|
+
y: screenY
|
|
74
|
+
}
|
|
75
|
+
}));
|
|
76
|
+
}, [
|
|
77
|
+
drawingState.isDrawing,
|
|
78
|
+
drawingMode,
|
|
79
|
+
screenToFlowPosition
|
|
80
|
+
]);
|
|
81
|
+
const handleMouseUp = useCallback((event)=>{
|
|
82
|
+
if (!drawingState.isDrawing || drawingMode !== DrawingMode.RECTANGLE) return;
|
|
83
|
+
const wrapper = containerRef.current;
|
|
84
|
+
if (!wrapper) return;
|
|
85
|
+
const endPosition = screenToFlowPosition({
|
|
86
|
+
x: event.clientX,
|
|
87
|
+
y: event.clientY
|
|
88
|
+
});
|
|
89
|
+
const width = Math.abs(endPosition.x - drawingState.startPos.x);
|
|
90
|
+
const height = Math.abs(endPosition.y - drawingState.startPos.y);
|
|
91
|
+
if (width > MIN_RECTANGLE_SIZE && height > MIN_RECTANGLE_SIZE) {
|
|
92
|
+
onCreateRectangle(drawingState.startPos.x, drawingState.startPos.y, endPosition.x, endPosition.y);
|
|
93
|
+
}
|
|
94
|
+
setDrawingState((prev)=>({
|
|
95
|
+
...prev,
|
|
96
|
+
isDrawing: false
|
|
97
|
+
}));
|
|
98
|
+
}, [
|
|
99
|
+
drawingState.isDrawing,
|
|
100
|
+
drawingState.startPos,
|
|
101
|
+
drawingMode,
|
|
102
|
+
screenToFlowPosition,
|
|
103
|
+
onCreateRectangle
|
|
104
|
+
]);
|
|
105
|
+
// Event listeners setup
|
|
106
|
+
useEffect(()=>{
|
|
107
|
+
const wrapper = containerRef.current;
|
|
108
|
+
if (!wrapper) return;
|
|
109
|
+
wrapper.addEventListener('mousedown', handleMouseDown);
|
|
110
|
+
document.addEventListener('mousemove', handleMouseMove);
|
|
111
|
+
document.addEventListener('mouseup', handleMouseUp);
|
|
112
|
+
return ()=>{
|
|
113
|
+
wrapper.removeEventListener('mousedown', handleMouseDown);
|
|
114
|
+
document.removeEventListener('mousemove', handleMouseMove);
|
|
115
|
+
document.removeEventListener('mouseup', handleMouseUp);
|
|
116
|
+
};
|
|
117
|
+
}, [
|
|
118
|
+
handleMouseDown,
|
|
119
|
+
handleMouseMove,
|
|
120
|
+
handleMouseUp
|
|
121
|
+
]);
|
|
122
|
+
// Calculate preview rectangle for drawing using screen coordinates
|
|
123
|
+
const previewRect = drawingState.isDrawing ? {
|
|
124
|
+
x: Math.min(drawingState.startScreenPos.x, drawingState.currentScreenPos.x),
|
|
125
|
+
y: Math.min(drawingState.startScreenPos.y, drawingState.currentScreenPos.y),
|
|
126
|
+
width: Math.abs(drawingState.currentScreenPos.x - drawingState.startScreenPos.x),
|
|
127
|
+
height: Math.abs(drawingState.currentScreenPos.y - drawingState.startScreenPos.y)
|
|
128
|
+
} : null;
|
|
129
|
+
return {
|
|
130
|
+
containerRef,
|
|
131
|
+
isDrawing: drawingState.isDrawing,
|
|
132
|
+
previewRect
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
export { useRectangleDrawing };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
interface UseTextEditingProps {
|
|
3
|
+
id: string;
|
|
4
|
+
label: string;
|
|
5
|
+
isEditable: boolean;
|
|
6
|
+
onTextEditComplete?: (id: string, oldText: string, newText: string) => void;
|
|
7
|
+
}
|
|
8
|
+
interface UseTextEditingReturn {
|
|
9
|
+
isEditing: boolean;
|
|
10
|
+
editingText: string;
|
|
11
|
+
inputRef: React.RefObject<HTMLInputElement | null>;
|
|
12
|
+
setEditingText: React.Dispatch<React.SetStateAction<string>>;
|
|
13
|
+
handleDoubleClick: (event: React.MouseEvent) => void;
|
|
14
|
+
handleKeyDown: (event: React.KeyboardEvent) => void;
|
|
15
|
+
handleBlur: () => void;
|
|
16
|
+
updateNodeData: (updates: Record<string, unknown>) => void;
|
|
17
|
+
}
|
|
18
|
+
export declare const useTextEditing: ({ id, label, isEditable, onTextEditComplete, }: UseTextEditingProps) => UseTextEditingReturn;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { useRef, useState, useEffect, useCallback } from 'react';
|
|
2
|
+
import { useReactFlow, useUpdateNodeInternals } from '@xyflow/react';
|
|
3
|
+
|
|
4
|
+
const useTextEditing = ({ id, label, isEditable, onTextEditComplete })=>{
|
|
5
|
+
const { setNodes } = useReactFlow();
|
|
6
|
+
const updateNodeInternals = useUpdateNodeInternals();
|
|
7
|
+
const inputRef = useRef(null);
|
|
8
|
+
const [isEditing, setIsEditing] = useState(false);
|
|
9
|
+
const [editingText, setEditingText] = useState(label);
|
|
10
|
+
// Sync state with node data changes
|
|
11
|
+
useEffect(()=>{
|
|
12
|
+
if (!isEditing) {
|
|
13
|
+
setEditingText(label);
|
|
14
|
+
}
|
|
15
|
+
}, [
|
|
16
|
+
label,
|
|
17
|
+
isEditing
|
|
18
|
+
]);
|
|
19
|
+
const updateNodeData = useCallback((updates)=>{
|
|
20
|
+
console.log('📝 updateNodeData 調用 (會觸發 React Flow 資料變更):', {
|
|
21
|
+
id,
|
|
22
|
+
updates
|
|
23
|
+
});
|
|
24
|
+
setNodes((nodes)=>nodes.map((node)=>node.id === id ? {
|
|
25
|
+
...node,
|
|
26
|
+
data: {
|
|
27
|
+
...node.data,
|
|
28
|
+
...updates
|
|
29
|
+
}
|
|
30
|
+
} : node));
|
|
31
|
+
updateNodeInternals(id);
|
|
32
|
+
}, [
|
|
33
|
+
id,
|
|
34
|
+
setNodes,
|
|
35
|
+
updateNodeInternals
|
|
36
|
+
]);
|
|
37
|
+
// Handle double click to start editing
|
|
38
|
+
const handleDoubleClick = useCallback((event)=>{
|
|
39
|
+
event.stopPropagation();
|
|
40
|
+
if (isEditable) {
|
|
41
|
+
setIsEditing(true);
|
|
42
|
+
setEditingText(label);
|
|
43
|
+
}
|
|
44
|
+
}, [
|
|
45
|
+
isEditable,
|
|
46
|
+
label
|
|
47
|
+
]);
|
|
48
|
+
// Handle saving the edited text
|
|
49
|
+
const handleSaveText = useCallback(()=>{
|
|
50
|
+
// 只有在文字真的有變更時才進行更新
|
|
51
|
+
if (label === editingText) {
|
|
52
|
+
setIsEditing(false);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const oldText = label;
|
|
56
|
+
const newText = editingText;
|
|
57
|
+
console.log('💾 保存文字編輯 (手動觸發歷史記錄):', {
|
|
58
|
+
id,
|
|
59
|
+
oldText,
|
|
60
|
+
newText
|
|
61
|
+
});
|
|
62
|
+
setIsEditing(false);
|
|
63
|
+
updateNodeData({
|
|
64
|
+
label: editingText
|
|
65
|
+
});
|
|
66
|
+
// 通知父組件文字編輯完成(這會觸發歷史記錄)
|
|
67
|
+
if (onTextEditComplete) {
|
|
68
|
+
// 延遲執行以確保 updateNodeData 完成
|
|
69
|
+
setTimeout(()=>{
|
|
70
|
+
onTextEditComplete(id, oldText, newText);
|
|
71
|
+
}, 10);
|
|
72
|
+
}
|
|
73
|
+
}, [
|
|
74
|
+
editingText,
|
|
75
|
+
updateNodeData,
|
|
76
|
+
id,
|
|
77
|
+
label,
|
|
78
|
+
onTextEditComplete
|
|
79
|
+
]);
|
|
80
|
+
// Handle input key events
|
|
81
|
+
const handleKeyDown = useCallback((event)=>{
|
|
82
|
+
if (event.key === 'Enter') {
|
|
83
|
+
handleSaveText();
|
|
84
|
+
} else if (event.key === 'Escape') {
|
|
85
|
+
setIsEditing(false);
|
|
86
|
+
setEditingText(label);
|
|
87
|
+
}
|
|
88
|
+
}, [
|
|
89
|
+
handleSaveText,
|
|
90
|
+
label
|
|
91
|
+
]);
|
|
92
|
+
// Auto-focus input when editing starts
|
|
93
|
+
useEffect(()=>{
|
|
94
|
+
if (isEditing && inputRef.current) {
|
|
95
|
+
inputRef.current.focus();
|
|
96
|
+
inputRef.current.select();
|
|
97
|
+
}
|
|
98
|
+
}, [
|
|
99
|
+
isEditing
|
|
100
|
+
]);
|
|
101
|
+
// Handle clicking outside to save
|
|
102
|
+
useEffect(()=>{
|
|
103
|
+
const handleClickOutside = (event)=>{
|
|
104
|
+
if (isEditing && inputRef.current) {
|
|
105
|
+
const target = event.target;
|
|
106
|
+
// 檢查點擊是否在 input 元素外部
|
|
107
|
+
if (!inputRef.current.contains(target)) {
|
|
108
|
+
console.log('🖱️ 點擊外部區域,保存文字編輯:', {
|
|
109
|
+
id,
|
|
110
|
+
editingText
|
|
111
|
+
});
|
|
112
|
+
handleSaveText();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
if (isEditing) {
|
|
117
|
+
// 使用 'click' 事件而不是 'mousedown' 以確保更好的兼容性
|
|
118
|
+
document.addEventListener('click', handleClickOutside, true);
|
|
119
|
+
return ()=>{
|
|
120
|
+
document.removeEventListener('click', handleClickOutside, true);
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}, [
|
|
124
|
+
isEditing,
|
|
125
|
+
handleSaveText,
|
|
126
|
+
id,
|
|
127
|
+
editingText
|
|
128
|
+
]);
|
|
129
|
+
// Handle input blur event (when input loses focus)
|
|
130
|
+
const handleBlur = useCallback(()=>{
|
|
131
|
+
console.log('🔄 Input blur 事件,保存文字編輯:', {
|
|
132
|
+
id,
|
|
133
|
+
editingText
|
|
134
|
+
});
|
|
135
|
+
handleSaveText();
|
|
136
|
+
}, [
|
|
137
|
+
handleSaveText,
|
|
138
|
+
id,
|
|
139
|
+
editingText
|
|
140
|
+
]);
|
|
141
|
+
return {
|
|
142
|
+
isEditing,
|
|
143
|
+
editingText,
|
|
144
|
+
inputRef,
|
|
145
|
+
setEditingText,
|
|
146
|
+
handleDoubleClick,
|
|
147
|
+
handleKeyDown,
|
|
148
|
+
handleBlur,
|
|
149
|
+
updateNodeData
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
export { useTextEditing };
|