@rytass/wms-map-react-components 0.1.9 → 0.2.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/components/breadcrumb/breadcrumb-edit-modal.cjs +92 -0
- package/components/breadcrumb/breadcrumb-edit-modal.d.cts +9 -0
- package/components/breadcrumb/breadcrumb-edit-modal.js +1 -0
- package/components/breadcrumb/breadcrumb-edit-modal.module.scss.cjs +5 -0
- package/components/breadcrumb/breadcrumb.cjs +171 -0
- package/components/breadcrumb/breadcrumb.d.cts +11 -0
- package/components/breadcrumb/breadcrumb.d.ts +1 -1
- package/components/breadcrumb/breadcrumb.js +1 -0
- package/components/breadcrumb/breadcrumb.module.scss.cjs +5 -0
- package/components/canvas/react-flow-canvas.cjs +321 -0
- package/components/canvas/react-flow-canvas.d.cts +38 -0
- package/components/canvas/react-flow-canvas.d.ts +1 -1
- package/components/canvas/react-flow-canvas.js +1 -0
- package/components/canvas/react-flow-canvas.module.scss.cjs +5 -0
- package/components/modal/wms-map-content.cjs +808 -0
- package/components/modal/wms-map-content.d.cts +4 -0
- package/components/modal/wms-map-content.d.ts +1 -1
- package/components/modal/wms-map-content.js +1 -0
- package/components/modal/wms-map-modal-container.cjs +130 -0
- package/components/modal/wms-map-modal-container.d.cts +5 -0
- package/components/modal/wms-map-modal-container.d.ts +1 -1
- package/components/modal/wms-map-modal-container.js +1 -0
- package/components/modal/wms-map-modal-container.module.scss.cjs +5 -0
- package/components/nodes/base-node.d.cts +12 -0
- package/components/nodes/base-node.d.ts +1 -1
- package/components/nodes/image-node.cjs +236 -0
- package/components/nodes/image-node.d.cts +17 -0
- package/components/nodes/image-node.d.ts +1 -1
- package/components/nodes/image-node.js +1 -0
- package/components/nodes/image-node.module.scss.cjs +5 -0
- package/components/nodes/path-node.cjs +553 -0
- package/components/nodes/path-node.d.cts +19 -0
- package/components/nodes/path-node.d.ts +1 -1
- package/components/nodes/path-node.js +1 -0
- package/components/nodes/path-node.module.scss.cjs +5 -0
- package/components/nodes/rectangle-node.cjs +239 -0
- package/components/nodes/rectangle-node.d.cts +11 -0
- package/components/nodes/rectangle-node.d.ts +1 -1
- package/components/nodes/rectangle-node.js +1 -0
- package/components/nodes/rectangle-node.module.scss.cjs +5 -0
- package/components/ui/context-menu.cjs +171 -0
- package/components/ui/context-menu.d.cts +25 -0
- package/components/ui/context-menu.js +1 -0
- package/components/ui/context-menu.module.scss.cjs +5 -0
- package/components/ui/toolbar.cjs +286 -0
- package/components/ui/toolbar.d.cts +23 -0
- package/components/ui/toolbar.d.ts +1 -1
- package/components/ui/toolbar.js +1 -0
- package/components/ui/toolbar.module.scss.cjs +5 -0
- package/components/ui/view-mode-toolbar.cjs +44 -0
- package/components/ui/view-mode-toolbar.d.cts +9 -0
- package/components/ui/view-mode-toolbar.d.ts +1 -1
- package/components/ui/view-mode-toolbar.js +1 -0
- package/components/ui/view-mode-toolbar.module.scss.cjs +5 -0
- package/constants/index.cjs +54 -0
- package/constants/index.d.cts +45 -0
- package/constants/index.d.ts +2 -2
- package/constants/text-mappings.cjs +46 -0
- package/constants/text-mappings.d.cts +181 -0
- package/hooks/use-context-menu.cjs +231 -0
- package/hooks/use-context-menu.d.cts +37 -0
- package/hooks/use-context-menu.d.ts +1 -1
- package/hooks/use-context-menu.js +1 -0
- package/hooks/use-direct-state-history.cjs +217 -0
- package/hooks/use-direct-state-history.d.cts +58 -0
- package/hooks/use-direct-state-history.d.ts +2 -2
- package/hooks/use-direct-state-history.js +1 -0
- package/hooks/use-pen-drawing.cjs +366 -0
- package/hooks/use-pen-drawing.d.cts +30 -0
- package/hooks/use-pen-drawing.d.ts +1 -1
- package/hooks/use-pen-drawing.js +1 -0
- package/hooks/use-rectangle-drawing.cjs +139 -0
- package/hooks/use-rectangle-drawing.d.cts +19 -0
- package/hooks/use-rectangle-drawing.d.ts +1 -1
- package/hooks/use-rectangle-drawing.js +1 -0
- package/hooks/use-text-editing.cjs +156 -0
- package/hooks/use-text-editing.d.cts +19 -0
- package/hooks/use-text-editing.js +1 -0
- package/icons/index.cjs +233 -0
- package/icons/index.d.cts +16 -0
- package/index.cjs +26 -0
- package/index.d.cts +5 -0
- package/index.d.ts +5 -5
- package/package.json +68 -3
- package/types/callback.types.d.cts +7 -0
- package/types/callback.types.d.ts +1 -1
- package/types/canvas.types.d.cts +35 -0
- package/types/canvas.types.d.ts +1 -1
- package/types/component-props.types.d.cts +45 -0
- package/types/component-props.types.d.ts +2 -2
- package/types/history.types.d.cts +24 -0
- package/types/history.types.d.ts +2 -2
- package/types/index.d.cts +5 -0
- package/types/index.d.ts +5 -5
- package/types/react-flow.types.d.cts +30 -0
- package/typings.cjs +44 -0
- package/typings.d.cts +136 -0
- package/utils/api-data-transform.cjs +221 -0
- package/utils/api-data-transform.d.cts +33 -0
- package/utils/api-data-transform.d.ts +1 -1
- package/utils/color-utils.cjs +58 -0
- package/utils/color-utils.d.cts +17 -0
- package/utils/debug-logger.cjs +45 -0
- package/utils/debug-logger.d.cts +33 -0
- package/utils/map-data-transform.cjs +360 -0
- package/utils/map-data-transform.d.cts +18 -0
- package/utils/map-data-transform.d.ts +1 -1
- package/utils/node-operations.cjs +88 -0
- package/utils/node-operations.d.cts +26 -0
- package/utils/node-utils.cjs +40 -0
- package/utils/node-utils.d.cts +10 -0
- package/index.cjs.css +0 -1
- package/index.cjs.js +0 -5128
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { EditMode } from '../typings.cjs';
|
|
2
|
+
import { FlowNode, FlowEdge } from '../types/react-flow.types.cjs';
|
|
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 {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { EditMode } from '../typings';
|
|
2
|
-
import { FlowNode, FlowEdge } from '../types/react-flow.types';
|
|
1
|
+
import { EditMode } from '../typings.js';
|
|
2
|
+
import { FlowNode, FlowEdge } from '../types/react-flow.types.js';
|
|
3
3
|
interface HistoryState {
|
|
4
4
|
nodes: FlowNode[];
|
|
5
5
|
edges: FlowEdge[];
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var react = require('@xyflow/react');
|
|
6
|
+
var typings = require('../typings.cjs');
|
|
7
|
+
|
|
8
|
+
// Utility function to constrain point to straight lines (horizontal, vertical, 45-degree)
|
|
9
|
+
const constrainToStraightLine = (currentPoint, previousPoint)=>{
|
|
10
|
+
const dx = currentPoint.x - previousPoint.x;
|
|
11
|
+
const dy = currentPoint.y - previousPoint.y;
|
|
12
|
+
// Calculate angle in degrees
|
|
13
|
+
const angle = Math.atan2(dy, dx) * (180 / Math.PI);
|
|
14
|
+
// Normalize angle to 0-360 range
|
|
15
|
+
const normalizedAngle = (angle % 360 + 360) % 360;
|
|
16
|
+
// Snap to nearest cardinal/diagonal direction (every 45 degrees)
|
|
17
|
+
const snapAngle = Math.round(normalizedAngle / 45) * 45;
|
|
18
|
+
// Calculate distance from previous point
|
|
19
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
20
|
+
// Convert back to radians and calculate constrained position
|
|
21
|
+
const constrainedAngle = snapAngle * Math.PI / 180;
|
|
22
|
+
return {
|
|
23
|
+
x: previousPoint.x + Math.cos(constrainedAngle) * distance,
|
|
24
|
+
y: previousPoint.y + Math.sin(constrainedAngle) * distance
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
const usePenDrawing = ({ editMode, drawingMode, onCreatePath })=>{
|
|
28
|
+
const { screenToFlowPosition } = react.useReactFlow();
|
|
29
|
+
const containerRef = React.useRef(null);
|
|
30
|
+
const previousModeRef = React.useRef(drawingMode);
|
|
31
|
+
const previousEditModeRef = React.useRef(editMode);
|
|
32
|
+
const [drawingState, setDrawingState] = React.useState({
|
|
33
|
+
isDrawing: false,
|
|
34
|
+
points: [],
|
|
35
|
+
screenPoints: [],
|
|
36
|
+
currentMousePos: null,
|
|
37
|
+
lastClickTime: 0,
|
|
38
|
+
isShiftPressed: false
|
|
39
|
+
});
|
|
40
|
+
const handleClick = React.useCallback((event)=>{
|
|
41
|
+
if (drawingMode !== typings.DrawingMode.PEN || editMode !== typings.EditMode.LAYER) return;
|
|
42
|
+
// IMMEDIATELY stop event propagation to prevent React Flow pane click
|
|
43
|
+
event.stopPropagation();
|
|
44
|
+
event.preventDefault();
|
|
45
|
+
event.stopImmediatePropagation();
|
|
46
|
+
// Debug log for shift key behavior
|
|
47
|
+
if (event.shiftKey) {
|
|
48
|
+
console.log('🖱️ Pen tool click with Shift key pressed - event stopped', {
|
|
49
|
+
isDrawing: drawingState.isDrawing,
|
|
50
|
+
pointsCount: drawingState.points.length,
|
|
51
|
+
lastClickTime: drawingState.lastClickTime,
|
|
52
|
+
currentTime: Date.now()
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const currentTime = Date.now();
|
|
56
|
+
const wrapper = containerRef.current;
|
|
57
|
+
if (!wrapper) return;
|
|
58
|
+
const rect = wrapper.getBoundingClientRect();
|
|
59
|
+
const screenX = event.clientX - rect.left;
|
|
60
|
+
const screenY = event.clientY - rect.top;
|
|
61
|
+
const basePosition = screenToFlowPosition({
|
|
62
|
+
x: event.clientX,
|
|
63
|
+
y: event.clientY
|
|
64
|
+
});
|
|
65
|
+
const baseScreenPos = {
|
|
66
|
+
x: screenX,
|
|
67
|
+
y: screenY
|
|
68
|
+
};
|
|
69
|
+
// Apply straight line constraint if Shift is pressed and we have a previous point
|
|
70
|
+
const { position, constrainedScreenPos } = (()=>{
|
|
71
|
+
if (!event.shiftKey || drawingState.points.length === 0) {
|
|
72
|
+
return {
|
|
73
|
+
position: basePosition,
|
|
74
|
+
constrainedScreenPos: baseScreenPos
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
const previousPoint = drawingState.points[drawingState.points.length - 1];
|
|
78
|
+
const constrainedPosition = constrainToStraightLine(basePosition, previousPoint);
|
|
79
|
+
// Also constrain the screen position for consistent preview
|
|
80
|
+
const constrainedScreen = drawingState.screenPoints.length > 0 ? (()=>{
|
|
81
|
+
const previousScreenPoint = drawingState.screenPoints[drawingState.screenPoints.length - 1];
|
|
82
|
+
return constrainToStraightLine(baseScreenPos, previousScreenPoint);
|
|
83
|
+
})() : baseScreenPos;
|
|
84
|
+
return {
|
|
85
|
+
position: constrainedPosition,
|
|
86
|
+
constrainedScreenPos: constrainedScreen
|
|
87
|
+
};
|
|
88
|
+
})();
|
|
89
|
+
// Check for double-click (within 300ms)
|
|
90
|
+
const isDoubleClick = currentTime - drawingState.lastClickTime < 300;
|
|
91
|
+
// Check if clicking on the first point to close the path (only if we have at least 3 points)
|
|
92
|
+
// IMPORTANT: Use original screen position, not constrained position, to avoid accidental closing when using Shift
|
|
93
|
+
const isClickingFirstPoint = (()=>{
|
|
94
|
+
if (!drawingState.isDrawing || drawingState.points.length < 3) {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const firstPoint = drawingState.screenPoints[0];
|
|
98
|
+
const distance = Math.sqrt(Math.pow(screenX - firstPoint.x, 2) + Math.pow(screenY - firstPoint.y, 2));
|
|
99
|
+
// Consider it a click on the first point if within 10 pixels
|
|
100
|
+
return distance <= 10;
|
|
101
|
+
})();
|
|
102
|
+
if (isClickingFirstPoint) {
|
|
103
|
+
console.log('🔵 Pen tool: Closing path by clicking first point', {
|
|
104
|
+
shiftKey: event.shiftKey,
|
|
105
|
+
pointsCount: drawingState.points.length
|
|
106
|
+
});
|
|
107
|
+
// Close the path by adding the first point at the end
|
|
108
|
+
const closedPath = [
|
|
109
|
+
...drawingState.points,
|
|
110
|
+
drawingState.points[0]
|
|
111
|
+
];
|
|
112
|
+
onCreatePath(closedPath);
|
|
113
|
+
// Reset drawing state
|
|
114
|
+
setDrawingState({
|
|
115
|
+
isDrawing: false,
|
|
116
|
+
points: [],
|
|
117
|
+
screenPoints: [],
|
|
118
|
+
currentMousePos: null,
|
|
119
|
+
lastClickTime: 0,
|
|
120
|
+
isShiftPressed: false
|
|
121
|
+
});
|
|
122
|
+
} else if (isDoubleClick && drawingState.isDrawing) {
|
|
123
|
+
console.log('🔵 Pen tool: Completing path by double-click', {
|
|
124
|
+
shiftKey: event.shiftKey,
|
|
125
|
+
pointsCount: drawingState.points.length,
|
|
126
|
+
timeDiff: currentTime - drawingState.lastClickTime
|
|
127
|
+
});
|
|
128
|
+
// Complete and close the path if we have at least 3 points
|
|
129
|
+
if (drawingState.points.length >= 3) {
|
|
130
|
+
// Close the path by adding the first point at the end
|
|
131
|
+
const closedPath = [
|
|
132
|
+
...drawingState.points,
|
|
133
|
+
drawingState.points[0]
|
|
134
|
+
];
|
|
135
|
+
onCreatePath(closedPath);
|
|
136
|
+
} else if (drawingState.points.length >= 2) {
|
|
137
|
+
// If only 2 points, create an open path
|
|
138
|
+
onCreatePath(drawingState.points);
|
|
139
|
+
}
|
|
140
|
+
// Reset drawing state
|
|
141
|
+
setDrawingState({
|
|
142
|
+
isDrawing: false,
|
|
143
|
+
points: [],
|
|
144
|
+
screenPoints: [],
|
|
145
|
+
currentMousePos: null,
|
|
146
|
+
lastClickTime: 0,
|
|
147
|
+
isShiftPressed: false
|
|
148
|
+
});
|
|
149
|
+
} else {
|
|
150
|
+
// Regular click - add point
|
|
151
|
+
setDrawingState((prev)=>({
|
|
152
|
+
...prev,
|
|
153
|
+
isDrawing: true,
|
|
154
|
+
points: [
|
|
155
|
+
...prev.points,
|
|
156
|
+
position
|
|
157
|
+
],
|
|
158
|
+
screenPoints: [
|
|
159
|
+
...prev.screenPoints,
|
|
160
|
+
constrainedScreenPos
|
|
161
|
+
],
|
|
162
|
+
lastClickTime: currentTime,
|
|
163
|
+
isShiftPressed: event.shiftKey
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
}, [
|
|
167
|
+
drawingMode,
|
|
168
|
+
editMode,
|
|
169
|
+
screenToFlowPosition,
|
|
170
|
+
drawingState.lastClickTime,
|
|
171
|
+
drawingState.isDrawing,
|
|
172
|
+
drawingState.points,
|
|
173
|
+
drawingState.screenPoints,
|
|
174
|
+
onCreatePath
|
|
175
|
+
]);
|
|
176
|
+
const handleMouseMove = React.useCallback((event)=>{
|
|
177
|
+
if (drawingMode !== typings.DrawingMode.PEN || editMode !== typings.EditMode.LAYER) return;
|
|
178
|
+
const wrapper = containerRef.current;
|
|
179
|
+
if (!wrapper) return;
|
|
180
|
+
const rect = wrapper.getBoundingClientRect();
|
|
181
|
+
const baseScreenX = event.clientX - rect.left;
|
|
182
|
+
const baseScreenY = event.clientY - rect.top;
|
|
183
|
+
// Apply constraint to mouse position if Shift is pressed and we're drawing
|
|
184
|
+
const { screenX, screenY } = (()=>{
|
|
185
|
+
if (!event.shiftKey || drawingState.screenPoints.length === 0) {
|
|
186
|
+
return {
|
|
187
|
+
screenX: baseScreenX,
|
|
188
|
+
screenY: baseScreenY
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
const previousScreenPoint = drawingState.screenPoints[drawingState.screenPoints.length - 1];
|
|
192
|
+
const constrainedPos = constrainToStraightLine({
|
|
193
|
+
x: baseScreenX,
|
|
194
|
+
y: baseScreenY
|
|
195
|
+
}, previousScreenPoint);
|
|
196
|
+
return {
|
|
197
|
+
screenX: constrainedPos.x,
|
|
198
|
+
screenY: constrainedPos.y
|
|
199
|
+
};
|
|
200
|
+
})();
|
|
201
|
+
setDrawingState((prev)=>({
|
|
202
|
+
...prev,
|
|
203
|
+
currentMousePos: {
|
|
204
|
+
x: screenX,
|
|
205
|
+
y: screenY
|
|
206
|
+
},
|
|
207
|
+
isShiftPressed: event.shiftKey
|
|
208
|
+
}));
|
|
209
|
+
}, [
|
|
210
|
+
drawingMode,
|
|
211
|
+
editMode,
|
|
212
|
+
drawingState.screenPoints
|
|
213
|
+
]);
|
|
214
|
+
const handleKeyDown = React.useCallback((event)=>{
|
|
215
|
+
if (drawingMode !== typings.DrawingMode.PEN || editMode !== typings.EditMode.LAYER || !drawingState.isDrawing) return;
|
|
216
|
+
// Escape key cancels current path
|
|
217
|
+
if (event.key === 'Escape') {
|
|
218
|
+
setDrawingState({
|
|
219
|
+
isDrawing: false,
|
|
220
|
+
points: [],
|
|
221
|
+
screenPoints: [],
|
|
222
|
+
currentMousePos: null,
|
|
223
|
+
lastClickTime: 0,
|
|
224
|
+
isShiftPressed: false
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
// Enter key completes and closes the path
|
|
228
|
+
if (event.key === 'Enter' && drawingState.points.length >= 2) {
|
|
229
|
+
if (drawingState.points.length >= 3) {
|
|
230
|
+
// Close the path by adding the first point at the end
|
|
231
|
+
const closedPath = [
|
|
232
|
+
...drawingState.points,
|
|
233
|
+
drawingState.points[0]
|
|
234
|
+
];
|
|
235
|
+
onCreatePath(closedPath);
|
|
236
|
+
} else {
|
|
237
|
+
// If only 2 points, create an open path
|
|
238
|
+
onCreatePath(drawingState.points);
|
|
239
|
+
}
|
|
240
|
+
setDrawingState({
|
|
241
|
+
isDrawing: false,
|
|
242
|
+
points: [],
|
|
243
|
+
screenPoints: [],
|
|
244
|
+
currentMousePos: null,
|
|
245
|
+
lastClickTime: 0,
|
|
246
|
+
isShiftPressed: false
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
}, [
|
|
250
|
+
drawingMode,
|
|
251
|
+
editMode,
|
|
252
|
+
drawingState.isDrawing,
|
|
253
|
+
drawingState.points,
|
|
254
|
+
onCreatePath
|
|
255
|
+
]);
|
|
256
|
+
// Force complete current path (for auto-close on pane click)
|
|
257
|
+
const forceComplete = React.useCallback(()=>{
|
|
258
|
+
console.log('🔴 forceComplete called', {
|
|
259
|
+
isDrawing: drawingState.isDrawing,
|
|
260
|
+
pointsLength: drawingState.points.length,
|
|
261
|
+
willClose: drawingState.points.length >= 3
|
|
262
|
+
});
|
|
263
|
+
if (!drawingState.isDrawing || drawingState.points.length < 2) return;
|
|
264
|
+
// Auto-close the path if we have 3+ points, otherwise create open path
|
|
265
|
+
if (drawingState.points.length >= 3) {
|
|
266
|
+
console.log('🔴 Force completing with closed path');
|
|
267
|
+
const closedPath = [
|
|
268
|
+
...drawingState.points,
|
|
269
|
+
drawingState.points[0]
|
|
270
|
+
];
|
|
271
|
+
onCreatePath(closedPath);
|
|
272
|
+
} else {
|
|
273
|
+
console.log('🔴 Force completing with open path');
|
|
274
|
+
onCreatePath(drawingState.points);
|
|
275
|
+
}
|
|
276
|
+
// Reset drawing state
|
|
277
|
+
setDrawingState({
|
|
278
|
+
isDrawing: false,
|
|
279
|
+
points: [],
|
|
280
|
+
screenPoints: [],
|
|
281
|
+
currentMousePos: null,
|
|
282
|
+
lastClickTime: 0,
|
|
283
|
+
isShiftPressed: false
|
|
284
|
+
});
|
|
285
|
+
}, [
|
|
286
|
+
drawingState.isDrawing,
|
|
287
|
+
drawingState.points,
|
|
288
|
+
onCreatePath
|
|
289
|
+
]);
|
|
290
|
+
// Reset drawing state when switching away from pen mode or LAYER mode
|
|
291
|
+
React.useEffect(()=>{
|
|
292
|
+
const modeChanged = previousModeRef.current !== drawingMode;
|
|
293
|
+
const editModeChanged = previousEditModeRef.current !== editMode;
|
|
294
|
+
if (modeChanged || editModeChanged) {
|
|
295
|
+
setDrawingState((prev)=>{
|
|
296
|
+
// Should we force complete before resetting?
|
|
297
|
+
const shouldForceComplete = // Switching away from PEN mode
|
|
298
|
+
(previousModeRef.current === typings.DrawingMode.PEN && drawingMode !== typings.DrawingMode.PEN || // Switching away from LAYER mode
|
|
299
|
+
previousEditModeRef.current === typings.EditMode.LAYER && editMode !== typings.EditMode.LAYER) && prev.isDrawing && prev.points.length >= 2;
|
|
300
|
+
if (shouldForceComplete) {
|
|
301
|
+
// Force complete the current path before switching
|
|
302
|
+
if (prev.points.length >= 3) {
|
|
303
|
+
const closedPath = [
|
|
304
|
+
...prev.points,
|
|
305
|
+
prev.points[0]
|
|
306
|
+
];
|
|
307
|
+
onCreatePath(closedPath);
|
|
308
|
+
} else {
|
|
309
|
+
onCreatePath(prev.points);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
// Reset drawing state
|
|
313
|
+
return {
|
|
314
|
+
isDrawing: false,
|
|
315
|
+
points: [],
|
|
316
|
+
screenPoints: [],
|
|
317
|
+
currentMousePos: null,
|
|
318
|
+
lastClickTime: 0,
|
|
319
|
+
isShiftPressed: false
|
|
320
|
+
};
|
|
321
|
+
});
|
|
322
|
+
// Update refs
|
|
323
|
+
previousModeRef.current = drawingMode;
|
|
324
|
+
previousEditModeRef.current = editMode;
|
|
325
|
+
}
|
|
326
|
+
}, [
|
|
327
|
+
drawingMode,
|
|
328
|
+
editMode,
|
|
329
|
+
onCreatePath
|
|
330
|
+
]);
|
|
331
|
+
// Event listeners setup
|
|
332
|
+
React.useEffect(()=>{
|
|
333
|
+
const wrapper = containerRef.current;
|
|
334
|
+
if (!wrapper) return;
|
|
335
|
+
// Use capture phase to intercept clicks before React Flow processes them
|
|
336
|
+
wrapper.addEventListener('click', handleClick, true);
|
|
337
|
+
document.addEventListener('mousemove', handleMouseMove);
|
|
338
|
+
document.addEventListener('keydown', handleKeyDown);
|
|
339
|
+
return ()=>{
|
|
340
|
+
wrapper.removeEventListener('click', handleClick, true);
|
|
341
|
+
document.removeEventListener('mousemove', handleMouseMove);
|
|
342
|
+
document.removeEventListener('keydown', handleKeyDown);
|
|
343
|
+
};
|
|
344
|
+
}, [
|
|
345
|
+
handleClick,
|
|
346
|
+
handleMouseMove,
|
|
347
|
+
handleKeyDown
|
|
348
|
+
]);
|
|
349
|
+
// Calculate preview path including current mouse position
|
|
350
|
+
const previewPath = drawingState.isDrawing && drawingState.screenPoints.length > 0 ? drawingState.currentMousePos ? [
|
|
351
|
+
...drawingState.screenPoints,
|
|
352
|
+
drawingState.currentMousePos
|
|
353
|
+
] : drawingState.screenPoints : null;
|
|
354
|
+
return {
|
|
355
|
+
containerRef,
|
|
356
|
+
isDrawing: drawingState.isDrawing,
|
|
357
|
+
previewPath,
|
|
358
|
+
currentPoints: drawingState.screenPoints,
|
|
359
|
+
// 提供起始點資訊,用於顯示閉合提示
|
|
360
|
+
firstPoint: drawingState.screenPoints.length > 0 ? drawingState.screenPoints[0] : null,
|
|
361
|
+
canClose: drawingState.isDrawing && drawingState.screenPoints.length >= 3,
|
|
362
|
+
forceComplete
|
|
363
|
+
};
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
exports.usePenDrawing = usePenDrawing;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { EditMode, DrawingMode } from '../typings.cjs';
|
|
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 {};
|
package/hooks/use-pen-drawing.js
CHANGED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var React = require('react');
|
|
5
|
+
var react = require('@xyflow/react');
|
|
6
|
+
var typings = require('../typings.cjs');
|
|
7
|
+
var index = require('../constants/index.cjs');
|
|
8
|
+
|
|
9
|
+
const useRectangleDrawing = ({ editMode, drawingMode, onCreateRectangle })=>{
|
|
10
|
+
const { screenToFlowPosition } = react.useReactFlow();
|
|
11
|
+
const containerRef = React.useRef(null);
|
|
12
|
+
const [drawingState, setDrawingState] = React.useState({
|
|
13
|
+
isDrawing: false,
|
|
14
|
+
startPos: {
|
|
15
|
+
x: 0,
|
|
16
|
+
y: 0
|
|
17
|
+
},
|
|
18
|
+
currentPos: {
|
|
19
|
+
x: 0,
|
|
20
|
+
y: 0
|
|
21
|
+
},
|
|
22
|
+
startScreenPos: {
|
|
23
|
+
x: 0,
|
|
24
|
+
y: 0
|
|
25
|
+
},
|
|
26
|
+
currentScreenPos: {
|
|
27
|
+
x: 0,
|
|
28
|
+
y: 0
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const handleMouseDown = React.useCallback((event)=>{
|
|
32
|
+
if (drawingMode !== typings.DrawingMode.RECTANGLE || editMode !== typings.EditMode.LAYER) return;
|
|
33
|
+
const wrapper = containerRef.current;
|
|
34
|
+
if (!wrapper) return;
|
|
35
|
+
const rect = wrapper.getBoundingClientRect();
|
|
36
|
+
const screenX = event.clientX - rect.left;
|
|
37
|
+
const screenY = event.clientY - rect.top;
|
|
38
|
+
const position = screenToFlowPosition({
|
|
39
|
+
x: event.clientX,
|
|
40
|
+
y: event.clientY
|
|
41
|
+
});
|
|
42
|
+
setDrawingState({
|
|
43
|
+
isDrawing: true,
|
|
44
|
+
startPos: position,
|
|
45
|
+
currentPos: position,
|
|
46
|
+
startScreenPos: {
|
|
47
|
+
x: screenX,
|
|
48
|
+
y: screenY
|
|
49
|
+
},
|
|
50
|
+
currentScreenPos: {
|
|
51
|
+
x: screenX,
|
|
52
|
+
y: screenY
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}, [
|
|
56
|
+
drawingMode,
|
|
57
|
+
editMode,
|
|
58
|
+
screenToFlowPosition
|
|
59
|
+
]);
|
|
60
|
+
const handleMouseMove = React.useCallback((event)=>{
|
|
61
|
+
if (!drawingState.isDrawing || drawingMode !== typings.DrawingMode.RECTANGLE) return;
|
|
62
|
+
const wrapper = containerRef.current;
|
|
63
|
+
if (!wrapper) return;
|
|
64
|
+
const rect = wrapper.getBoundingClientRect();
|
|
65
|
+
const screenX = event.clientX - rect.left;
|
|
66
|
+
const screenY = event.clientY - rect.top;
|
|
67
|
+
const position = screenToFlowPosition({
|
|
68
|
+
x: event.clientX,
|
|
69
|
+
y: event.clientY
|
|
70
|
+
});
|
|
71
|
+
setDrawingState((prev)=>({
|
|
72
|
+
...prev,
|
|
73
|
+
currentPos: position,
|
|
74
|
+
currentScreenPos: {
|
|
75
|
+
x: screenX,
|
|
76
|
+
y: screenY
|
|
77
|
+
}
|
|
78
|
+
}));
|
|
79
|
+
}, [
|
|
80
|
+
drawingState.isDrawing,
|
|
81
|
+
drawingMode,
|
|
82
|
+
screenToFlowPosition
|
|
83
|
+
]);
|
|
84
|
+
const handleMouseUp = React.useCallback((event)=>{
|
|
85
|
+
if (!drawingState.isDrawing || drawingMode !== typings.DrawingMode.RECTANGLE) return;
|
|
86
|
+
const wrapper = containerRef.current;
|
|
87
|
+
if (!wrapper) return;
|
|
88
|
+
const endPosition = screenToFlowPosition({
|
|
89
|
+
x: event.clientX,
|
|
90
|
+
y: event.clientY
|
|
91
|
+
});
|
|
92
|
+
const width = Math.abs(endPosition.x - drawingState.startPos.x);
|
|
93
|
+
const height = Math.abs(endPosition.y - drawingState.startPos.y);
|
|
94
|
+
if (width > index.MIN_RECTANGLE_SIZE && height > index.MIN_RECTANGLE_SIZE) {
|
|
95
|
+
onCreateRectangle(drawingState.startPos.x, drawingState.startPos.y, endPosition.x, endPosition.y);
|
|
96
|
+
}
|
|
97
|
+
setDrawingState((prev)=>({
|
|
98
|
+
...prev,
|
|
99
|
+
isDrawing: false
|
|
100
|
+
}));
|
|
101
|
+
}, [
|
|
102
|
+
drawingState.isDrawing,
|
|
103
|
+
drawingState.startPos,
|
|
104
|
+
drawingMode,
|
|
105
|
+
screenToFlowPosition,
|
|
106
|
+
onCreateRectangle
|
|
107
|
+
]);
|
|
108
|
+
// Event listeners setup
|
|
109
|
+
React.useEffect(()=>{
|
|
110
|
+
const wrapper = containerRef.current;
|
|
111
|
+
if (!wrapper) return;
|
|
112
|
+
wrapper.addEventListener('mousedown', handleMouseDown);
|
|
113
|
+
document.addEventListener('mousemove', handleMouseMove);
|
|
114
|
+
document.addEventListener('mouseup', handleMouseUp);
|
|
115
|
+
return ()=>{
|
|
116
|
+
wrapper.removeEventListener('mousedown', handleMouseDown);
|
|
117
|
+
document.removeEventListener('mousemove', handleMouseMove);
|
|
118
|
+
document.removeEventListener('mouseup', handleMouseUp);
|
|
119
|
+
};
|
|
120
|
+
}, [
|
|
121
|
+
handleMouseDown,
|
|
122
|
+
handleMouseMove,
|
|
123
|
+
handleMouseUp
|
|
124
|
+
]);
|
|
125
|
+
// Calculate preview rectangle for drawing using screen coordinates
|
|
126
|
+
const previewRect = drawingState.isDrawing ? {
|
|
127
|
+
x: Math.min(drawingState.startScreenPos.x, drawingState.currentScreenPos.x),
|
|
128
|
+
y: Math.min(drawingState.startScreenPos.y, drawingState.currentScreenPos.y),
|
|
129
|
+
width: Math.abs(drawingState.currentScreenPos.x - drawingState.startScreenPos.x),
|
|
130
|
+
height: Math.abs(drawingState.currentScreenPos.y - drawingState.startScreenPos.y)
|
|
131
|
+
} : null;
|
|
132
|
+
return {
|
|
133
|
+
containerRef,
|
|
134
|
+
isDrawing: drawingState.isDrawing,
|
|
135
|
+
previewRect
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
exports.useRectangleDrawing = useRectangleDrawing;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { EditMode, DrawingMode } from '../typings.cjs';
|
|
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 {};
|