@mhamz.01/easyflow-whiteboard 2.33.0 → 2.35.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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"custom-node-overlay-layer.d.ts","sourceRoot":"","sources":["../../../src/components/node/custom-node-overlay-layer.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"custom-node-overlay-layer.d.ts","sourceRoot":"","sources":["../../../src/components/node/custom-node-overlay-layer.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAM9C,MAAM,WAAW,IAAI;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,GAAG,aAAa,GAAG,MAAM,CAAC;IACxC,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACrC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED,UAAU,uBAAuB;IAC/B,KAAK,EAAE,IAAI,EAAE,CAAC;IACd,SAAS,EAAE,QAAQ,EAAE,CAAC;IACtB,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,IAAI,EAAE,KAAK,IAAI,CAAC;IACxC,iBAAiB,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,EAAE,KAAK,IAAI,CAAC;IACpD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,cAAc,CAAC,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC1C,YAAY,CAAC,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IACzE,qBAAqB,CAAC,EAAE,YAAY,EAAE,CAAC;IACvC,YAAY,CAAC,EAAE,KAAK,CAAC,SAAS,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;CAC/C;AAoBD,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,EACzC,KAAK,EACL,SAAS,EACT,aAAa,EACb,iBAAiB,EACjB,UAAc,EACd,cAA+B,EAC/B,YAAmB,EACnB,qBAA0B,EAC1B,YAAY,GACb,EAAE,uBAAuB,2CAgczB"}
|
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
-
import { useState, useEffect, useRef } from "react";
|
|
3
|
+
import { useState, useEffect, useRef, useCallback, memo } from "react";
|
|
4
4
|
import TaskNode from "./custom-node";
|
|
5
5
|
import DocumentNode from "./document-node";
|
|
6
|
+
// ─── Memoized node wrappers ───────────────────────────────────────────────────
|
|
7
|
+
// Prevents all sibling nodes re-rendering when one node's position or
|
|
8
|
+
// selection state changes. Props compared shallowly by React.memo.
|
|
9
|
+
const MemoTaskNode = memo(TaskNode);
|
|
10
|
+
const MemoDocumentNode = memo(DocumentNode);
|
|
6
11
|
// ─── Component ────────────────────────────────────────────────────────────────
|
|
7
12
|
export default function CanvasOverlayLayer({ tasks, documents, onTasksUpdate, onDocumentsUpdate, canvasZoom = 1, canvasViewport = { x: 0, y: 0 }, selectionBox = null, selectedCanvasObjects = [], fabricCanvas, }) {
|
|
8
13
|
const [localTasks, setLocalTasks] = useState(tasks);
|
|
9
14
|
const [localDocuments, setLocalDocuments] = useState(documents);
|
|
10
15
|
const [selectedIds, setSelectedIds] = useState(new Set());
|
|
11
|
-
|
|
16
|
+
// `dragging` state removed entirely — it caused a re-render on drag start
|
|
17
|
+
// which tore down/reattached the mousemove listener mid-gesture (jump bug).
|
|
18
|
+
// All drag lifecycle now lives exclusively in dragStateRef.
|
|
19
|
+
// ─── Refs ──────────────────────────────────────────────────────────────────
|
|
12
20
|
const dragStateRef = useRef({
|
|
13
21
|
isDragging: false,
|
|
14
22
|
itemIds: [],
|
|
@@ -19,73 +27,55 @@ export default function CanvasOverlayLayer({ tasks, documents, onTasksUpdate, on
|
|
|
19
27
|
});
|
|
20
28
|
const rafIdRef = useRef(null);
|
|
21
29
|
const overlayRef = useRef(null);
|
|
30
|
+
// ── Always-fresh refs — assigned synchronously in render body ─────────────
|
|
31
|
+
// Read inside stable [] effect closures. Assigned during render (not useEffect)
|
|
32
|
+
// so they hold the latest value before any event handler can fire.
|
|
22
33
|
const selectedIdsRef = useRef(selectedIds);
|
|
23
34
|
selectedIdsRef.current = selectedIds;
|
|
24
|
-
//
|
|
35
|
+
// O(1) world-position lookup — replaces O(n) getItemPosition() scans
|
|
36
|
+
const nodePositionsRef = useRef(new Map());
|
|
37
|
+
nodePositionsRef.current = new Map([
|
|
38
|
+
...localTasks.map((t) => [t.id, { x: t.x, y: t.y }]),
|
|
39
|
+
...localDocuments.map((d) => [d.id, { x: d.x, y: d.y }]),
|
|
40
|
+
]);
|
|
41
|
+
// Avoids stale prop capture of selectedCanvasObjects in handleDragStart
|
|
42
|
+
const selectedCanvasObjectsRef = useRef(selectedCanvasObjects);
|
|
43
|
+
selectedCanvasObjectsRef.current = selectedCanvasObjects;
|
|
44
|
+
// Stable callback refs — handleEnd never closes over stale prop functions
|
|
45
|
+
const onTasksUpdateRef = useRef(onTasksUpdate);
|
|
46
|
+
const onDocumentsUpdateRef = useRef(onDocumentsUpdate);
|
|
47
|
+
onTasksUpdateRef.current = onTasksUpdate;
|
|
48
|
+
onDocumentsUpdateRef.current = onDocumentsUpdate;
|
|
49
|
+
// ─── Sync props → local state ──────────────────────────────────────────────
|
|
25
50
|
useEffect(() => { setLocalTasks(tasks); }, [tasks]);
|
|
26
51
|
useEffect(() => { setLocalDocuments(documents); }, [documents]);
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const canvas = fabricCanvas?.current;
|
|
31
|
-
if (!canvas)
|
|
32
|
-
return;
|
|
33
|
-
const nativeEvent = e.nativeEvent;
|
|
34
|
-
// getScenePoint handles the transformation from screen to canvas space
|
|
35
|
-
const scenePoint = canvas.getScenePoint(nativeEvent);
|
|
36
|
-
// Viewport point is simply the mouse position relative to the canvas element
|
|
37
|
-
const rect = canvas.getElement().getBoundingClientRect();
|
|
38
|
-
const viewportPoint = {
|
|
39
|
-
x: nativeEvent.clientX - rect.left,
|
|
40
|
-
y: nativeEvent.clientY - rect.top,
|
|
41
|
-
};
|
|
42
|
-
// We cast to 'any' here because we are manually triggering an internal
|
|
43
|
-
// event bus, and Fabric's internal types for .fire() can be overly strict.
|
|
44
|
-
canvas.fire("mouse:wheel", {
|
|
45
|
-
e: nativeEvent,
|
|
46
|
-
scenePoint,
|
|
47
|
-
viewportPoint,
|
|
48
|
-
});
|
|
49
|
-
e.preventDefault();
|
|
50
|
-
e.stopPropagation();
|
|
51
|
-
}
|
|
52
|
-
};
|
|
52
|
+
// ─── Wheel forwarding to Fabric ────────────────────────────────────────────
|
|
53
|
+
// `canvasZoom` removed from deps — handler reads canvas state imperatively,
|
|
54
|
+
// no stale data risk. Registered once per canvas mount.
|
|
53
55
|
useEffect(() => {
|
|
54
56
|
const overlayEl = overlayRef.current;
|
|
55
57
|
const canvas = fabricCanvas?.current;
|
|
56
58
|
if (!overlayEl || !canvas)
|
|
57
59
|
return;
|
|
58
60
|
const handleGlobalWheel = (e) => {
|
|
59
|
-
|
|
60
|
-
// (meaning they are hovering over a Task or Document)
|
|
61
|
-
const target = e.target;
|
|
62
|
-
const isOverNode = target !== overlayEl;
|
|
61
|
+
const isOverNode = e.target !== overlayEl;
|
|
63
62
|
if ((e.ctrlKey || e.metaKey) && isOverNode) {
|
|
64
|
-
// 1. Prevent Browser Zoom immediately
|
|
65
63
|
e.preventDefault();
|
|
66
64
|
e.stopPropagation();
|
|
67
|
-
// 2. Calculate coordinates for Fabric
|
|
68
65
|
const scenePoint = canvas.getScenePoint(e);
|
|
69
66
|
const rect = canvas.getElement().getBoundingClientRect();
|
|
70
|
-
const viewportPoint = {
|
|
71
|
-
x: e.clientX - rect.left,
|
|
72
|
-
y: e.clientY - rect.top,
|
|
73
|
-
};
|
|
74
|
-
// 3. Manually fire the event into Fabric
|
|
75
67
|
canvas.fire("mouse:wheel", {
|
|
76
|
-
e
|
|
68
|
+
e,
|
|
77
69
|
scenePoint,
|
|
78
|
-
viewportPoint,
|
|
70
|
+
viewportPoint: { x: e.clientX - rect.left, y: e.clientY - rect.top },
|
|
79
71
|
});
|
|
80
72
|
}
|
|
81
73
|
};
|
|
82
|
-
// CRITICAL: { passive: false } allows us to cancel the browser's zoom
|
|
83
74
|
overlayEl.addEventListener("wheel", handleGlobalWheel, { passive: false });
|
|
84
|
-
return () =>
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
// ── Fabric → Overlay Sync (Fixes Dragging from Fabric area) ──────────────────
|
|
75
|
+
return () => overlayEl.removeEventListener("wheel", handleGlobalWheel);
|
|
76
|
+
}, [fabricCanvas]);
|
|
77
|
+
// ─── Fabric → Overlay sync + deselection ───────────────────────────────────
|
|
78
|
+
// Registered once — selectedIds always read via selectedIdsRef.
|
|
89
79
|
useEffect(() => {
|
|
90
80
|
const canvas = fabricCanvas?.current;
|
|
91
81
|
if (!canvas)
|
|
@@ -100,8 +90,7 @@ export default function CanvasOverlayLayer({ tasks, documents, onTasksUpdate, on
|
|
|
100
90
|
target._prevTop = target.top;
|
|
101
91
|
if (deltaX === 0 && deltaY === 0)
|
|
102
92
|
return;
|
|
103
|
-
|
|
104
|
-
const sel = selectedIdsRef.current;
|
|
93
|
+
const sel = selectedIdsRef.current; // always fresh
|
|
105
94
|
setLocalTasks((prev) => prev.map((t) => sel.has(t.id) ? { ...t, x: t.x + deltaX, y: t.y + deltaY } : t));
|
|
106
95
|
setLocalDocuments((prev) => prev.map((d) => sel.has(d.id) ? { ...d, x: d.x + deltaX, y: d.y + deltaY } : d));
|
|
107
96
|
};
|
|
@@ -112,158 +101,76 @@ export default function CanvasOverlayLayer({ tasks, documents, onTasksUpdate, on
|
|
|
112
101
|
target._prevTop = target.top;
|
|
113
102
|
}
|
|
114
103
|
if (!target) {
|
|
104
|
+
// Clicked empty canvas — always deselect HTML nodes
|
|
115
105
|
setSelectedIds(new Set());
|
|
116
106
|
return;
|
|
117
107
|
}
|
|
118
|
-
//
|
|
108
|
+
// Clicked a Fabric object — only deselect HTML nodes if NOT already in
|
|
109
|
+
// the active selection (preserves mixed-group drag behaviour)
|
|
119
110
|
const activeObjects = canvas.getActiveObjects();
|
|
120
|
-
|
|
121
|
-
|
|
111
|
+
if (!activeObjects.includes(target)) {
|
|
112
|
+
setSelectedIds(new Set());
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
// Clear HTML selection on fresh Fabric selection, but never mid-drag
|
|
116
|
+
const handleFabricSelection = () => {
|
|
117
|
+
if (!dragStateRef.current.isDragging) {
|
|
122
118
|
setSelectedIds(new Set());
|
|
123
119
|
}
|
|
124
120
|
};
|
|
125
121
|
canvas.on("object:moving", handleObjectMoving);
|
|
126
122
|
canvas.on("mouse:down", handleMouseDown);
|
|
123
|
+
canvas.on("selection:created", handleFabricSelection);
|
|
124
|
+
canvas.on("selection:updated", handleFabricSelection);
|
|
127
125
|
return () => {
|
|
128
126
|
canvas.off("object:moving", handleObjectMoving);
|
|
129
127
|
canvas.off("mouse:down", handleMouseDown);
|
|
128
|
+
canvas.off("selection:created", handleFabricSelection);
|
|
129
|
+
canvas.off("selection:updated", handleFabricSelection);
|
|
130
130
|
};
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
// change, creating a new closure each time. The second drag captured a stale
|
|
134
|
-
// or empty selectedIds from the closure at re-registration time.
|
|
135
|
-
}, [canvasZoom, fabricCanvas]);
|
|
136
|
-
// ── Helpers ─────────────────────────────────────────────────────────────────
|
|
137
|
-
const getItemPosition = (id) => {
|
|
138
|
-
const task = localTasks.find((t) => t.id === id);
|
|
139
|
-
if (task)
|
|
140
|
-
return { x: task.x, y: task.y };
|
|
141
|
-
const doc = localDocuments.find((d) => d.id === id);
|
|
142
|
-
if (doc)
|
|
143
|
-
return { x: doc.x, y: doc.y };
|
|
144
|
-
return undefined;
|
|
145
|
-
};
|
|
146
|
-
const isItemInSelectionBox = (x, y, width, height, box) => {
|
|
147
|
-
const itemX1 = x * canvasZoom + canvasViewport.x;
|
|
148
|
-
const itemY1 = y * canvasZoom + canvasViewport.y;
|
|
149
|
-
const itemX2 = itemX1 + width * canvasZoom;
|
|
150
|
-
const itemY2 = itemY1 + height * canvasZoom;
|
|
151
|
-
const boxX1 = Math.min(box.x1, box.x2);
|
|
152
|
-
const boxY1 = Math.min(box.y1, box.y2);
|
|
153
|
-
const boxX2 = Math.max(box.x1, box.x2);
|
|
154
|
-
const boxY2 = Math.max(box.y1, box.y2);
|
|
155
|
-
return !(boxX2 < itemX1 || boxX1 > itemX2 || boxY2 < itemY1 || boxY1 > itemY2);
|
|
156
|
-
};
|
|
157
|
-
// ── Selection box detection ──────────────────────────────────────────────────
|
|
131
|
+
}, [fabricCanvas]); // selectedIds NOT in deps — read via ref
|
|
132
|
+
// ─── Selection box hit detection ───────────────────────────────────────────
|
|
158
133
|
useEffect(() => {
|
|
159
134
|
if (!selectionBox)
|
|
160
135
|
return;
|
|
161
|
-
|
|
136
|
+
const bX1 = Math.min(selectionBox.x1, selectionBox.x2);
|
|
137
|
+
const bY1 = Math.min(selectionBox.y1, selectionBox.y2);
|
|
138
|
+
const bX2 = Math.max(selectionBox.x1, selectionBox.x2);
|
|
139
|
+
const bY2 = Math.max(selectionBox.y1, selectionBox.y2);
|
|
140
|
+
const hits = (x, y, w, h) => {
|
|
141
|
+
const x1 = x * canvasZoom + canvasViewport.x;
|
|
142
|
+
const y1 = y * canvasZoom + canvasViewport.y;
|
|
143
|
+
return !(bX2 < x1 || bX1 > x1 + w * canvasZoom || bY2 < y1 || bY1 > y1 + h * canvasZoom);
|
|
144
|
+
};
|
|
162
145
|
const newSelected = new Set();
|
|
163
|
-
for (const task of localTasks)
|
|
164
|
-
if (
|
|
146
|
+
for (const task of localTasks)
|
|
147
|
+
if (hits(task.x, task.y, 300, 140))
|
|
165
148
|
newSelected.add(task.id);
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (isItemInSelectionBox(doc.x, doc.y, 320, 160, selectionBox))
|
|
149
|
+
for (const doc of localDocuments)
|
|
150
|
+
if (hits(doc.x, doc.y, 320, 160))
|
|
169
151
|
newSelected.add(doc.id);
|
|
170
|
-
|
|
171
|
-
// ── O(n) equality check: size first (fast path), then membership ──
|
|
152
|
+
// O(n) equality — same Set ref if unchanged → no re-render
|
|
172
153
|
setSelectedIds((prev) => {
|
|
173
154
|
if (prev.size !== newSelected.size)
|
|
174
155
|
return newSelected;
|
|
175
|
-
for (const id of newSelected)
|
|
156
|
+
for (const id of newSelected)
|
|
176
157
|
if (!prev.has(id))
|
|
177
|
-
return newSelected;
|
|
178
|
-
|
|
179
|
-
return prev; // identical — return same reference, no re-render
|
|
158
|
+
return newSelected;
|
|
159
|
+
return prev;
|
|
180
160
|
});
|
|
181
161
|
}, [selectionBox, localTasks, localDocuments, canvasZoom, canvasViewport]);
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return e.touches[0];
|
|
187
|
-
return e;
|
|
188
|
-
};
|
|
189
|
-
const handleDragStart = (itemId, e) => {
|
|
190
|
-
// 1. Safety check for the Fabric instance
|
|
191
|
-
const canvas = fabricCanvas?.current;
|
|
192
|
-
if (!canvas)
|
|
193
|
-
return;
|
|
194
|
-
// 2. Normalize the event (Touch vs Mouse)
|
|
195
|
-
if (e.cancelable)
|
|
196
|
-
e.preventDefault();
|
|
197
|
-
const pointer = getPointerEvent(e);
|
|
198
|
-
// 3. Determine which items are being dragged
|
|
199
|
-
// selection update DOES NOT trigger before drag snapshot
|
|
200
|
-
let itemsToDrag;
|
|
201
|
-
if (selectedIds.has(itemId)) {
|
|
202
|
-
itemsToDrag = Array.from(selectedIds);
|
|
203
|
-
}
|
|
204
|
-
else {
|
|
205
|
-
itemsToDrag = [itemId];
|
|
206
|
-
}
|
|
207
|
-
// 4. Capture current World Transform (Zoom & Pan)
|
|
208
|
-
// We read directly from the canvas to ensure zero-frame lag
|
|
209
|
-
const vpt = canvas.viewportTransform || [1, 0, 0, 1, 0, 0];
|
|
210
|
-
const liveZoom = vpt[0];
|
|
211
|
-
const liveVpX = vpt[4];
|
|
212
|
-
const liveVpY = vpt[5];
|
|
213
|
-
// 5. Convert the Click Position from Screen Pixels to World Units
|
|
214
|
-
const clickWorldX = (pointer.clientX - liveVpX) / liveZoom;
|
|
215
|
-
const clickWorldY = (pointer.clientY - liveVpY) / liveZoom;
|
|
216
|
-
// 6. Get the clicked item's current World Position
|
|
217
|
-
const clickedPos = getItemPosition(itemId);
|
|
218
|
-
if (!clickedPos)
|
|
219
|
-
return;
|
|
220
|
-
// 7. Calculate the Offset in WORLD UNITS
|
|
221
|
-
// This is the distance from the mouse to the node's top-left in the infinite grid.
|
|
222
|
-
// This value remains constant even if you zoom during the drag.
|
|
223
|
-
const worldOffsetX = clickWorldX - clickedPos.x;
|
|
224
|
-
const worldOffsetY = clickWorldY - clickedPos.y;
|
|
225
|
-
// 8. Snapshot starting positions for all selected HTML nodes
|
|
226
|
-
const startPositions = new Map();
|
|
227
|
-
itemsToDrag.forEach((id) => {
|
|
228
|
-
const pos = getItemPosition(id);
|
|
229
|
-
if (pos)
|
|
230
|
-
startPositions.set(id, pos);
|
|
231
|
-
});
|
|
232
|
-
// 9. Snapshot starting positions for all selected Fabric objects
|
|
233
|
-
const canvasObjectsStartPos = new Map();
|
|
234
|
-
selectedCanvasObjects.forEach((obj) => {
|
|
235
|
-
canvasObjectsStartPos.set(obj, { left: obj.left || 0, top: obj.top || 0 });
|
|
236
|
-
});
|
|
237
|
-
// 10. Commit to the ref for the requestAnimationFrame loop
|
|
238
|
-
dragStateRef.current = {
|
|
239
|
-
isDragging: true,
|
|
240
|
-
itemIds: itemsToDrag,
|
|
241
|
-
startPositions,
|
|
242
|
-
canvasObjectsStartPos,
|
|
243
|
-
offsetX: worldOffsetX, // Now stored as World Units
|
|
244
|
-
offsetY: worldOffsetY, // Now stored as World Units
|
|
245
|
-
};
|
|
246
|
-
if (!selectedIds.has(itemId) && dragStateRef.current.itemIds.length === 0) {
|
|
247
|
-
setSelectedIds(new Set([itemId]));
|
|
248
|
-
}
|
|
249
|
-
// 11. Trigger UI states
|
|
250
|
-
setDragging({ itemIds: itemsToDrag });
|
|
251
|
-
document.body.style.cursor = "grabbing";
|
|
252
|
-
document.body.style.userSelect = "none";
|
|
253
|
-
document.body.style.touchAction = "none";
|
|
254
|
-
};
|
|
255
|
-
// ── Drag move (HTML Node side) ───────────────────────────────────────────────
|
|
162
|
+
// ─── Global drag listeners — registered ONCE on mount ──────────────────────
|
|
163
|
+
// Critical: must NOT be in a useEffect with [localTasks] or any state deps.
|
|
164
|
+
// Every setLocalTasks during drag would tear down + reattach mousemove → jump.
|
|
165
|
+
// All values read via refs so the [] closure is always correct.
|
|
256
166
|
useEffect(() => {
|
|
257
|
-
if (!dragging)
|
|
258
|
-
return;
|
|
259
|
-
// Inside the useEffect that watches [dragging, localTasks, localDocuments, fabricCanvas]
|
|
260
167
|
const handleMove = (e) => {
|
|
261
168
|
if (!dragStateRef.current.isDragging)
|
|
262
169
|
return;
|
|
263
170
|
if (e.cancelable)
|
|
264
171
|
e.preventDefault();
|
|
265
|
-
const pointer =
|
|
266
|
-
|
|
172
|
+
const pointer = "touches" in e && e.touches.length > 0
|
|
173
|
+
? e.touches[0] : e;
|
|
267
174
|
if (rafIdRef.current !== null)
|
|
268
175
|
cancelAnimationFrame(rafIdRef.current);
|
|
269
176
|
rafIdRef.current = requestAnimationFrame(() => {
|
|
@@ -271,59 +178,48 @@ export default function CanvasOverlayLayer({ tasks, documents, onTasksUpdate, on
|
|
|
271
178
|
const canvas = fabricCanvas?.current;
|
|
272
179
|
if (!canvas)
|
|
273
180
|
return;
|
|
274
|
-
// 2. Read the "Source of Truth" transform from the canvas
|
|
275
181
|
const vpt = canvas.viewportTransform;
|
|
276
|
-
const liveZoom = vpt[0];
|
|
277
|
-
const liveVpX = vpt[4];
|
|
278
|
-
const liveVpY = vpt[5];
|
|
279
|
-
|
|
280
|
-
const
|
|
281
|
-
const
|
|
282
|
-
// 4. Calculate where the "Anchor" node should be in World Units
|
|
283
|
-
// (Current Mouse World - Initial World Offset from Start)
|
|
284
|
-
const newWorldX = currentWorldX - offsetX;
|
|
285
|
-
const newWorldY = currentWorldY - offsetY;
|
|
286
|
-
// 5. Calculate the Movement Delta in World Units
|
|
287
|
-
// We compare where the first item started vs where it is now.
|
|
288
|
-
const firstId = itemIds[0];
|
|
289
|
-
const firstStart = startPositions.get(firstId);
|
|
182
|
+
const liveZoom = vpt[0];
|
|
183
|
+
const liveVpX = vpt[4];
|
|
184
|
+
const liveVpY = vpt[5];
|
|
185
|
+
const newWorldX = (pointer.clientX - liveVpX) / liveZoom - offsetX;
|
|
186
|
+
const newWorldY = (pointer.clientY - liveVpY) / liveZoom - offsetY;
|
|
187
|
+
const firstStart = startPositions.get(itemIds[0]);
|
|
290
188
|
if (!firstStart)
|
|
291
189
|
return;
|
|
292
190
|
const deltaX = newWorldX - firstStart.x;
|
|
293
191
|
const deltaY = newWorldY - firstStart.y;
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
} : d));
|
|
305
|
-
// 7. Sync Fabric Objects (Imperative update for performance)
|
|
192
|
+
setLocalTasks((prev) => prev.map((t) => itemIds.includes(t.id)
|
|
193
|
+
? { ...t,
|
|
194
|
+
x: (startPositions.get(t.id)?.x ?? t.x) + deltaX,
|
|
195
|
+
y: (startPositions.get(t.id)?.y ?? t.y) + deltaY }
|
|
196
|
+
: t));
|
|
197
|
+
setLocalDocuments((prev) => prev.map((d) => itemIds.includes(d.id)
|
|
198
|
+
? { ...d,
|
|
199
|
+
x: (startPositions.get(d.id)?.x ?? d.x) + deltaX,
|
|
200
|
+
y: (startPositions.get(d.id)?.y ?? d.y) + deltaY }
|
|
201
|
+
: d));
|
|
306
202
|
canvasObjectsStartPos.forEach((startPos, obj) => {
|
|
307
|
-
obj.set({
|
|
308
|
-
|
|
309
|
-
top: startPos.top + deltaY,
|
|
310
|
-
});
|
|
311
|
-
obj.setCoords(); // Required for selection/intersection accuracy
|
|
203
|
+
obj.set({ left: startPos.left + deltaX, top: startPos.top + deltaY });
|
|
204
|
+
obj.setCoords();
|
|
312
205
|
});
|
|
313
|
-
// 8. Single render call for all Fabric changes
|
|
314
206
|
canvas.requestRenderAll();
|
|
315
207
|
});
|
|
316
208
|
};
|
|
317
209
|
const handleEnd = () => {
|
|
318
|
-
if (
|
|
210
|
+
if (!dragStateRef.current.isDragging)
|
|
211
|
+
return;
|
|
212
|
+
if (rafIdRef.current !== null) {
|
|
319
213
|
cancelAnimationFrame(rafIdRef.current);
|
|
214
|
+
rafIdRef.current = null;
|
|
215
|
+
}
|
|
320
216
|
dragStateRef.current.isDragging = false;
|
|
321
|
-
setDragging(null);
|
|
322
217
|
document.body.style.cursor = "";
|
|
323
218
|
document.body.style.userSelect = "";
|
|
324
219
|
document.body.style.touchAction = "";
|
|
325
|
-
|
|
326
|
-
|
|
220
|
+
// Flush via functional updater (reads latest state) + callback ref (never stale)
|
|
221
|
+
setLocalTasks((prev) => { onTasksUpdateRef.current?.(prev); return prev; });
|
|
222
|
+
setLocalDocuments((prev) => { onDocumentsUpdateRef.current?.(prev); return prev; });
|
|
327
223
|
};
|
|
328
224
|
window.addEventListener("mousemove", handleMove, { passive: false });
|
|
329
225
|
window.addEventListener("mouseup", handleEnd);
|
|
@@ -337,9 +233,61 @@ export default function CanvasOverlayLayer({ tasks, documents, onTasksUpdate, on
|
|
|
337
233
|
window.removeEventListener("touchend", handleEnd);
|
|
338
234
|
window.removeEventListener("touchcancel", handleEnd);
|
|
339
235
|
};
|
|
340
|
-
}, [
|
|
341
|
-
//
|
|
342
|
-
|
|
236
|
+
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
237
|
+
// ─── Drag start ────────────────────────────────────────────────────────────
|
|
238
|
+
// useCallback: stable ref so MemoTaskNode/MemoDocumentNode don't re-render
|
|
239
|
+
// on unrelated parent state changes.
|
|
240
|
+
const handleDragStart = useCallback((itemId, e) => {
|
|
241
|
+
const canvas = fabricCanvas?.current;
|
|
242
|
+
if (!canvas)
|
|
243
|
+
return;
|
|
244
|
+
if (e.cancelable)
|
|
245
|
+
e.preventDefault();
|
|
246
|
+
const pointer = "touches" in e && e.touches.length > 0
|
|
247
|
+
? e.touches[0] : e;
|
|
248
|
+
// Read from ref — never stale even if setSelectedIds was called this render
|
|
249
|
+
const currentSelected = selectedIdsRef.current;
|
|
250
|
+
const itemsToDrag = currentSelected.has(itemId)
|
|
251
|
+
? Array.from(currentSelected)
|
|
252
|
+
: [itemId];
|
|
253
|
+
if (!currentSelected.has(itemId)) {
|
|
254
|
+
setSelectedIds(new Set([itemId]));
|
|
255
|
+
}
|
|
256
|
+
const vpt = canvas.viewportTransform || [1, 0, 0, 1, 0, 0];
|
|
257
|
+
const liveZoom = vpt[0];
|
|
258
|
+
const liveVpX = vpt[4];
|
|
259
|
+
const liveVpY = vpt[5];
|
|
260
|
+
// O(1) ref lookup — no stale state scan
|
|
261
|
+
const clickedPos = nodePositionsRef.current.get(itemId);
|
|
262
|
+
if (!clickedPos)
|
|
263
|
+
return;
|
|
264
|
+
const startPositions = new Map();
|
|
265
|
+
for (const id of itemsToDrag) {
|
|
266
|
+
const pos = nodePositionsRef.current.get(id);
|
|
267
|
+
if (pos)
|
|
268
|
+
startPositions.set(id, { x: pos.x, y: pos.y });
|
|
269
|
+
}
|
|
270
|
+
// Read Fabric objects from ref — not stale prop
|
|
271
|
+
const canvasObjectsStartPos = new Map();
|
|
272
|
+
for (const obj of selectedCanvasObjectsRef.current) {
|
|
273
|
+
canvasObjectsStartPos.set(obj, { left: obj.left || 0, top: obj.top || 0 });
|
|
274
|
+
}
|
|
275
|
+
// Write only to ref — zero re-render on drag start (eliminates listener churn)
|
|
276
|
+
dragStateRef.current = {
|
|
277
|
+
isDragging: true,
|
|
278
|
+
itemIds: itemsToDrag,
|
|
279
|
+
startPositions,
|
|
280
|
+
canvasObjectsStartPos,
|
|
281
|
+
offsetX: (pointer.clientX - liveVpX) / liveZoom - clickedPos.x,
|
|
282
|
+
offsetY: (pointer.clientY - liveVpY) / liveZoom - clickedPos.y,
|
|
283
|
+
};
|
|
284
|
+
document.body.style.cursor = "grabbing";
|
|
285
|
+
document.body.style.userSelect = "none";
|
|
286
|
+
document.body.style.touchAction = "none";
|
|
287
|
+
}, [fabricCanvas]);
|
|
288
|
+
// ─── Node interaction handlers ─────────────────────────────────────────────
|
|
289
|
+
// useCallback: stable refs prevent unnecessary re-renders of memoized nodes
|
|
290
|
+
const handleSelect = useCallback((id, e) => {
|
|
343
291
|
if (e?.shiftKey || e?.ctrlKey || e?.metaKey) {
|
|
344
292
|
setSelectedIds((prev) => {
|
|
345
293
|
const next = new Set(prev);
|
|
@@ -350,68 +298,65 @@ export default function CanvasOverlayLayer({ tasks, documents, onTasksUpdate, on
|
|
|
350
298
|
else {
|
|
351
299
|
setSelectedIds(new Set([id]));
|
|
352
300
|
}
|
|
353
|
-
};
|
|
354
|
-
const handleStatusChange = (taskId, newStatus) => {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
301
|
+
}, []);
|
|
302
|
+
const handleStatusChange = useCallback((taskId, newStatus) => {
|
|
303
|
+
setLocalTasks((prev) => {
|
|
304
|
+
const updated = prev.map((t) => t.id === taskId ? { ...t, status: newStatus } : t);
|
|
305
|
+
onTasksUpdateRef.current?.(updated);
|
|
306
|
+
return updated;
|
|
307
|
+
});
|
|
308
|
+
}, []);
|
|
309
|
+
// ─── Keyboard shortcuts ────────────────────────────────────────────────────
|
|
310
|
+
// selectedIds removed from deps — read via selectedIdsRef (prevents
|
|
311
|
+
// re-registering the keydown listener on every single selection change)
|
|
359
312
|
useEffect(() => {
|
|
360
313
|
const handleKeyDown = (e) => {
|
|
361
|
-
// Don't trigger if typing in input
|
|
362
314
|
if (e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement)
|
|
363
315
|
return;
|
|
364
|
-
// Select All
|
|
365
316
|
if ((e.ctrlKey || e.metaKey) && e.key === "a") {
|
|
366
317
|
e.preventDefault();
|
|
367
|
-
setSelectedIds(new Set([
|
|
318
|
+
setSelectedIds(new Set([
|
|
319
|
+
...localTasks.map((t) => t.id),
|
|
320
|
+
...localDocuments.map((d) => d.id),
|
|
321
|
+
]));
|
|
368
322
|
}
|
|
369
|
-
// Clear selection
|
|
370
323
|
if (e.key === "Escape") {
|
|
371
324
|
setSelectedIds(new Set());
|
|
372
325
|
}
|
|
373
|
-
|
|
374
|
-
if ((e.key === "Delete" || e.key === "Backspace") && selectedIds.size > 0) {
|
|
326
|
+
if ((e.key === "Delete" || e.key === "Backspace") && selectedIdsRef.current.size > 0) {
|
|
375
327
|
e.preventDefault();
|
|
376
|
-
const
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
328
|
+
const ids = selectedIdsRef.current;
|
|
329
|
+
setLocalTasks((prev) => {
|
|
330
|
+
const u = prev.filter((t) => !ids.has(t.id));
|
|
331
|
+
onTasksUpdateRef.current?.(u);
|
|
332
|
+
return u;
|
|
333
|
+
});
|
|
334
|
+
setLocalDocuments((prev) => {
|
|
335
|
+
const u = prev.filter((d) => !ids.has(d.id));
|
|
336
|
+
onDocumentsUpdateRef.current?.(u);
|
|
337
|
+
return u;
|
|
338
|
+
});
|
|
380
339
|
setSelectedIds(new Set());
|
|
381
|
-
onTasksUpdate?.(updatedTasks);
|
|
382
|
-
onDocumentsUpdate?.(updatedDocs);
|
|
383
340
|
}
|
|
384
341
|
};
|
|
385
342
|
window.addEventListener("keydown", handleKeyDown);
|
|
386
343
|
return () => window.removeEventListener("keydown", handleKeyDown);
|
|
387
|
-
}, [localTasks, localDocuments
|
|
388
|
-
//
|
|
389
|
-
const renderItem = (id, x, y, children) => {
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
// 2. Use translate3d for GPU performance
|
|
400
|
-
transform: `translate3d(${screenX}px, ${screenY}px, 0) scale(${canvasZoom})`,
|
|
401
|
-
transformOrigin: "top left",
|
|
402
|
-
// 3. THE FIX: Remove transition entirely during any viewport change
|
|
403
|
-
// Any 'ease' during zoom causes the "shaking" behavior.
|
|
404
|
-
transition: "none",
|
|
405
|
-
// 4. Optimization
|
|
406
|
-
willChange: "transform",
|
|
407
|
-
zIndex: isDragging ? 1000 : 1,
|
|
408
|
-
}, children: children }, id));
|
|
409
|
-
};
|
|
410
|
-
return (_jsx("div", { ref: overlayRef, className: "absolute inset-0 pointer-events-none", style: { zIndex: 50 }, onWheel: handleOverlayWheel, onClick: (e) => {
|
|
344
|
+
}, [localTasks, localDocuments]);
|
|
345
|
+
// ─── Render ────────────────────────────────────────────────────────────────
|
|
346
|
+
const renderItem = (id, x, y, children) => (_jsx("div", { className: "pointer-events-auto absolute", style: {
|
|
347
|
+
left: 0,
|
|
348
|
+
top: 0,
|
|
349
|
+
transform: `translate3d(${x * canvasZoom}px, ${y * canvasZoom}px, 0) scale(${canvasZoom})`,
|
|
350
|
+
transformOrigin: "top left",
|
|
351
|
+
transition: "none",
|
|
352
|
+
willChange: "transform",
|
|
353
|
+
zIndex: dragStateRef.current.itemIds.includes(id) ? 1000 : 1,
|
|
354
|
+
}, children: children }, id));
|
|
355
|
+
return (_jsx("div", { ref: overlayRef, className: "absolute inset-0 pointer-events-none", style: { zIndex: 50 }, onClick: (e) => {
|
|
411
356
|
if (e.target === e.currentTarget)
|
|
412
357
|
setSelectedIds(new Set());
|
|
413
358
|
}, children: _jsxs("div", { className: "absolute top-0 left-0 pointer-events-none", style: {
|
|
414
359
|
transform: `translate(${canvasViewport.x}px, ${canvasViewport.y}px)`,
|
|
415
360
|
transformOrigin: "top left",
|
|
416
|
-
}, children: [localTasks.map((task) => renderItem(task.id, task.x, task.y, _jsx(
|
|
361
|
+
}, children: [localTasks.map((task) => renderItem(task.id, task.x, task.y, _jsx(MemoTaskNode, { ...task, isSelected: selectedIds.has(task.id), onSelect: handleSelect, onDragStart: handleDragStart, onStatusChange: handleStatusChange, zoom: 1 }))), localDocuments.map((doc) => renderItem(doc.id, doc.x, doc.y, _jsx(MemoDocumentNode, { ...doc, isSelected: selectedIds.has(doc.id), onSelect: handleSelect, onDragStart: handleDragStart })))] }) }));
|
|
417
362
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"whiteboard-test.d.ts","sourceRoot":"","sources":["../../../src/components/whiteboard/whiteboard-test.tsx"],"names":[],"mappings":"AA4CA,MAAM,CAAC,OAAO,UAAU,gBAAgB,
|
|
1
|
+
{"version":3,"file":"whiteboard-test.d.ts","sourceRoot":"","sources":["../../../src/components/whiteboard/whiteboard-test.tsx"],"names":[],"mappings":"AA4CA,MAAM,CAAC,OAAO,UAAU,gBAAgB,4CA0QvC"}
|
|
@@ -165,8 +165,11 @@ export default function FabricWhiteboard() {
|
|
|
165
165
|
const vpt = canvas.viewportTransform;
|
|
166
166
|
if (!vpt)
|
|
167
167
|
return;
|
|
168
|
-
|
|
169
|
-
|
|
168
|
+
// vpt[0] = live zoom, vpt[4]/vpt[5] = live pan
|
|
169
|
+
// Never use canvasZoom state here — it can be 1 render behind
|
|
170
|
+
const liveZoom = vpt[0];
|
|
171
|
+
const cx = (canvas.getWidth() / 2 - vpt[4]) / liveZoom;
|
|
172
|
+
const cy = (canvas.getHeight() / 2 - vpt[5]) / liveZoom;
|
|
170
173
|
setTasks((prev) => [
|
|
171
174
|
...prev,
|
|
172
175
|
{
|
|
@@ -184,14 +187,15 @@ export default function FabricWhiteboard() {
|
|
|
184
187
|
const vpt = canvas.viewportTransform;
|
|
185
188
|
if (!vpt)
|
|
186
189
|
return;
|
|
187
|
-
const
|
|
188
|
-
const
|
|
190
|
+
const liveZoom = vpt[0];
|
|
191
|
+
const cx = (canvas.getWidth() / 2 - vpt[4]) / liveZoom;
|
|
192
|
+
const cy = (canvas.getHeight() / 2 - vpt[5]) / liveZoom;
|
|
189
193
|
setDocuments((prev) => [
|
|
190
194
|
...prev,
|
|
191
195
|
{
|
|
192
196
|
...docTemplate,
|
|
193
197
|
id: `${docTemplate.id}-${Date.now()}`,
|
|
194
|
-
x: cx -
|
|
198
|
+
x: cx - 160, // 320px wide → center offset
|
|
195
199
|
y: cy - 80,
|
|
196
200
|
},
|
|
197
201
|
]);
|