danylokhodus commited on
Commit
b4d6f7f
·
1 Parent(s): 14b17c5

Completely rewrite ImageNode scaling with pure pointer-event mathematical calculations and direct React Flow store updates

Browse files
src/features/canvas/components/nodes/ImageNode.jsx CHANGED
@@ -1,5 +1,5 @@
1
  import React, { useState, useEffect, useRef } from 'react';
2
- import { NodeResizeControl, Handle, Position, useUpdateNodeInternals } from '@xyflow/react';
3
  import { Image as ImageIcon, Loader2 } from 'lucide-react';
4
  import { useTranslation } from 'react-i18next';
5
  import axios from 'axios';
@@ -7,10 +7,10 @@ import axios from 'axios';
7
  const ImageNode = ({ id, data, selected, width, height }) => {
8
  const { t } = useTranslation();
9
  const updateNodeInternals = useUpdateNodeInternals();
 
10
  const [imageUrl, setImageUrl] = useState(data.url || '');
11
  const [loading, setLoading] = useState(false);
12
  const [isShiftPressed, setIsShiftPressed] = useState(false);
13
- const containerRef = useRef(null);
14
  const imageRatioRef = useRef(1);
15
 
16
  // Load large data from backend on mount if not loaded
@@ -77,29 +77,6 @@ const ImageNode = ({ id, data, selected, width, height }) => {
77
  };
78
  }, []);
79
 
80
- // Intercept pointer down / mouse down events on the resizer container natively
81
- // to completely block React Flow from initiating a node drag when clicking/dragging handles.
82
- useEffect(() => {
83
- const stopPropagation = (e) => {
84
- e.stopPropagation();
85
- };
86
-
87
- const el = containerRef.current;
88
- if (el) {
89
- el.addEventListener('pointerdown', stopPropagation);
90
- el.addEventListener('mousedown', stopPropagation);
91
- el.addEventListener('touchstart', stopPropagation);
92
- }
93
-
94
- return () => {
95
- if (el) {
96
- el.removeEventListener('pointerdown', stopPropagation);
97
- el.removeEventListener('mousedown', stopPropagation);
98
- el.removeEventListener('touchstart', stopPropagation);
99
- }
100
- };
101
- }, [selected]); // Re-attach when container element is rendered/unmounted
102
-
103
  const handleImageLoad = (e) => {
104
  const { naturalWidth, naturalHeight } = e.target;
105
  if (naturalWidth && naturalHeight) {
@@ -117,21 +94,179 @@ const ImageNode = ({ id, data, selected, width, height }) => {
117
  }
118
  };
119
 
120
- const handleResizeEnd = (e, params) => {
121
- if (data.updateNodeData) {
122
- data.updateNodeData(id, { width: params.width, height: params.height });
123
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124
  };
125
 
126
  const currentWidth = width || data.width || 300;
127
  const currentHeight = height || data.height || 'auto';
128
 
129
- const controlProps = {
130
- minWidth: 50,
131
- minHeight: 50,
132
- onResizeEnd: handleResizeEnd,
133
- className: "nodrag !pointer-events-auto !w-3.5 !h-3.5 !bg-surface-container-lowest !border-2 !border-primary !rounded-md !shadow-[2px_2px_0px_0px_var(--color-primary)] hover:!scale-110 active:!scale-95 transition-transform"
134
- };
135
 
136
  return (
137
  <div
@@ -146,22 +281,54 @@ const ImageNode = ({ id, data, selected, width, height }) => {
146
  }}
147
  >
148
  {selected && (
149
- <div ref={containerRef} className="absolute inset-0 pointer-events-none z-50">
150
  {/* Custom Dashed Outline */}
151
- <div className="absolute inset-0 border-2 border-dashed border-primary opacity-60 rounded-lg" />
152
 
153
- {/* Corners (Always Keep Aspect Ratio) */}
154
- <NodeResizeControl position="top-left" keepAspectRatio={true} {...controlProps} />
155
- <NodeResizeControl position="top-right" keepAspectRatio={true} {...controlProps} />
156
- <NodeResizeControl position="bottom-left" keepAspectRatio={true} {...controlProps} />
157
- <NodeResizeControl position="bottom-right" keepAspectRatio={true} {...controlProps} />
158
-
159
- {/* Sides (Keep Aspect Ratio ONLY if Shift is pressed) */}
160
- <NodeResizeControl position="top" keepAspectRatio={isShiftPressed} {...controlProps} />
161
- <NodeResizeControl position="bottom" keepAspectRatio={isShiftPressed} {...controlProps} />
162
- <NodeResizeControl position="left" keepAspectRatio={isShiftPressed} {...controlProps} />
163
- <NodeResizeControl position="right" keepAspectRatio={isShiftPressed} {...controlProps} />
164
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
  )}
166
 
167
  {loading ? (
 
1
  import React, { useState, useEffect, useRef } from 'react';
2
+ import { Handle, Position, useUpdateNodeInternals, useReactFlow } from '@xyflow/react';
3
  import { Image as ImageIcon, Loader2 } from 'lucide-react';
4
  import { useTranslation } from 'react-i18next';
5
  import axios from 'axios';
 
7
  const ImageNode = ({ id, data, selected, width, height }) => {
8
  const { t } = useTranslation();
9
  const updateNodeInternals = useUpdateNodeInternals();
10
+ const { getZoom, getNode, setNodes } = useReactFlow();
11
  const [imageUrl, setImageUrl] = useState(data.url || '');
12
  const [loading, setLoading] = useState(false);
13
  const [isShiftPressed, setIsShiftPressed] = useState(false);
 
14
  const imageRatioRef = useRef(1);
15
 
16
  // Load large data from backend on mount if not loaded
 
77
  };
78
  }, []);
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  const handleImageLoad = (e) => {
81
  const { naturalWidth, naturalHeight } = e.target;
82
  if (naturalWidth && naturalHeight) {
 
94
  }
95
  };
96
 
97
+ const handleResizeStart = (e, direction) => {
98
+ e.stopPropagation();
99
+ e.preventDefault();
100
+
101
+ const node = getNode(id);
102
+ if (!node) return;
103
+
104
+ const startX = e.clientX;
105
+ const startY = e.clientY;
106
+ const startPos = { ...node.position };
107
+ const startSize = {
108
+ width: node.width || node.style?.width || data.width || 300,
109
+ height: node.height || node.style?.height || data.height || 200
110
+ };
111
+
112
+ const ratio = imageRatioRef.current || (startSize.width / startSize.height) || 1;
113
+ const zoom = getZoom();
114
+
115
+ const handlePointerMove = (moveEvent) => {
116
+ const dx = (moveEvent.clientX - startX) / zoom;
117
+ const dy = (moveEvent.clientY - startY) / zoom;
118
+
119
+ const shiftPressed = moveEvent.shiftKey || isShiftPressed;
120
+ const isCorner = ['top-left', 'top-right', 'bottom-left', 'bottom-right'].includes(direction);
121
+ const shouldLockRatio = isCorner || shiftPressed;
122
+
123
+ let newWidth = startSize.width;
124
+ let newHeight = startSize.height;
125
+ let newX = startPos.x;
126
+ let newY = startPos.y;
127
+
128
+ if (shouldLockRatio) {
129
+ // --- PROPORTIONAL RESIZING ---
130
+ if (direction === 'right') {
131
+ newWidth = Math.max(50, startSize.width + dx);
132
+ newHeight = Math.round(newWidth / ratio);
133
+ newY = startPos.y;
134
+ } else if (direction === 'left') {
135
+ newWidth = Math.max(50, startSize.width - dx);
136
+ newHeight = Math.round(newWidth / ratio);
137
+ newX = startPos.x + (startSize.width - newWidth);
138
+ newY = startPos.y;
139
+ } else if (direction === 'bottom') {
140
+ newHeight = Math.max(50, startSize.height + dy);
141
+ newWidth = Math.round(newHeight * ratio);
142
+ newX = startPos.x;
143
+ } else if (direction === 'top') {
144
+ newHeight = Math.max(50, startSize.height - dy);
145
+ newWidth = Math.round(newHeight * ratio);
146
+ newY = startPos.y + (startSize.height - newHeight);
147
+ newX = startPos.x;
148
+ } else if (direction === 'bottom-right') {
149
+ if (Math.abs(dx) > Math.abs(dy)) {
150
+ newWidth = Math.max(50, startSize.width + dx);
151
+ newHeight = Math.round(newWidth / ratio);
152
+ } else {
153
+ newHeight = Math.max(50, startSize.height + dy);
154
+ newWidth = Math.round(newHeight * ratio);
155
+ }
156
+ newX = startPos.x;
157
+ newY = startPos.y;
158
+ } else if (direction === 'bottom-left') {
159
+ if (Math.abs(dx) > Math.abs(dy)) {
160
+ newWidth = Math.max(50, startSize.width - dx);
161
+ newHeight = Math.round(newWidth / ratio);
162
+ } else {
163
+ newHeight = Math.max(50, startSize.height + dy);
164
+ newWidth = Math.round(newHeight * ratio);
165
+ }
166
+ newX = startPos.x + (startSize.width - newWidth);
167
+ newY = startPos.y;
168
+ } else if (direction === 'top-right') {
169
+ if (Math.abs(dx) > Math.abs(dy)) {
170
+ newWidth = Math.max(50, startSize.width + dx);
171
+ newHeight = Math.round(newWidth / ratio);
172
+ } else {
173
+ newHeight = Math.max(50, startSize.height - dy);
174
+ newWidth = Math.round(newHeight * ratio);
175
+ }
176
+ newX = startPos.x;
177
+ newY = startPos.y + (startSize.height - newHeight);
178
+ } else if (direction === 'top-left') {
179
+ if (Math.abs(dx) > Math.abs(dy)) {
180
+ newWidth = Math.max(50, startSize.width - dx);
181
+ newHeight = Math.round(newWidth / ratio);
182
+ } else {
183
+ newHeight = Math.max(50, startSize.height - dy);
184
+ newWidth = Math.round(newHeight * ratio);
185
+ }
186
+ newX = startPos.x + (startSize.width - newWidth);
187
+ newY = startPos.y + (startSize.height - newHeight);
188
+ }
189
+ } else {
190
+ // --- DISTORTION RESIZING ---
191
+ if (direction === 'right') {
192
+ newWidth = Math.max(50, startSize.width + dx);
193
+ } else if (direction === 'left') {
194
+ newWidth = Math.max(50, startSize.width - dx);
195
+ newX = startPos.x + (startSize.width - newWidth);
196
+ } else if (direction === 'bottom') {
197
+ newHeight = Math.max(50, startSize.height + dy);
198
+ } else if (direction === 'top') {
199
+ newHeight = Math.max(50, startSize.height - dy);
200
+ newY = startPos.y + (startSize.height - newHeight);
201
+ } else if (direction === 'bottom-right') {
202
+ newWidth = Math.max(50, startSize.width + dx);
203
+ newHeight = Math.max(50, startSize.height + dy);
204
+ } else if (direction === 'bottom-left') {
205
+ newWidth = Math.max(50, startSize.width - dx);
206
+ newHeight = Math.max(50, startSize.height + dy);
207
+ newX = startPos.x + (startSize.width - newWidth);
208
+ } else if (direction === 'top-right') {
209
+ newWidth = Math.max(50, startSize.width + dx);
210
+ newHeight = Math.max(50, startSize.height - dy);
211
+ newY = startPos.y + (startSize.height - newHeight);
212
+ } else if (direction === 'top-left') {
213
+ newWidth = Math.max(50, startSize.width - dx);
214
+ newHeight = Math.max(50, startSize.height - dy);
215
+ newX = startPos.x + (startSize.width - newWidth);
216
+ newY = startPos.y + (startSize.height - newHeight);
217
+ }
218
+ }
219
+
220
+ // Update the node size and position in React Flow's store in real-time
221
+ setNodes((nds) =>
222
+ nds.map((n) => {
223
+ if (n.id === id) {
224
+ return {
225
+ ...n,
226
+ position: { x: newX, y: newY },
227
+ width: newWidth,
228
+ height: newHeight,
229
+ style: { ...n.style, width: newWidth, height: newHeight }
230
+ };
231
+ }
232
+ return n;
233
+ })
234
+ );
235
+ };
236
+
237
+ const handlePointerUp = () => {
238
+ window.removeEventListener('pointermove', handlePointerMove);
239
+ window.removeEventListener('pointerup', handlePointerUp);
240
+
241
+ // Get the final node bounds from the store to save them
242
+ const latestNode = getNode(id);
243
+ if (latestNode) {
244
+ const finalWidth = latestNode.width || latestNode.style?.width || 300;
245
+ const finalHeight = latestNode.height || latestNode.style?.height || 200;
246
+ const finalX = latestNode.position.x;
247
+ const finalY = latestNode.position.y;
248
+
249
+ // Save new dimensions and position to database, history and broadcast
250
+ if (data.updateNodeData) {
251
+ data.updateNodeData(id, {
252
+ width: finalWidth,
253
+ height: finalHeight,
254
+ x: finalX,
255
+ y: finalY
256
+ });
257
+ }
258
+ }
259
+ };
260
+
261
+ window.addEventListener('pointermove', handlePointerMove);
262
+ window.addEventListener('pointerup', handlePointerUp);
263
  };
264
 
265
  const currentWidth = width || data.width || 300;
266
  const currentHeight = height || data.height || 'auto';
267
 
268
+ // Common handle style & class
269
+ const handleClass = "absolute w-3.5 h-3.5 bg-surface-container-lowest border-2 border-primary rounded-md shadow-[2px_2px_0px_0px_var(--color-primary)] z-50 hover:scale-110 active:scale-95 transition-transform select-none";
 
 
 
 
270
 
271
  return (
272
  <div
 
281
  }}
282
  >
283
  {selected && (
284
+ <>
285
  {/* Custom Dashed Outline */}
286
+ <div className="absolute inset-0 border-2 border-dashed border-primary opacity-60 rounded-lg pointer-events-none z-10" />
287
 
288
+ {/* Corners (Always Proportional) */}
289
+ <div
290
+ style={{ top: '-7px', left: '-7px' }}
291
+ className={`${handleClass} cursor-nwse-resize`}
292
+ onPointerDown={(e) => handleResizeStart(e, 'top-left')}
293
+ />
294
+ <div
295
+ style={{ top: '-7px', right: '-7px' }}
296
+ className={`${handleClass} cursor-nesw-resize`}
297
+ onPointerDown={(e) => handleResizeStart(e, 'top-right')}
298
+ />
299
+ <div
300
+ style={{ bottom: '-7px', left: '-7px' }}
301
+ className={`${handleClass} cursor-nesw-resize`}
302
+ onPointerDown={(e) => handleResizeStart(e, 'bottom-left')}
303
+ />
304
+ <div
305
+ style={{ bottom: '-7px', right: '-7px' }}
306
+ className={`${handleClass} cursor-nwse-resize`}
307
+ onPointerDown={(e) => handleResizeStart(e, 'bottom-right')}
308
+ />
309
+
310
+ {/* Sides (Proportional only if Shift is held) */}
311
+ <div
312
+ style={{ top: '-7px', left: '50%', marginLeft: '-7px' }}
313
+ className={`${handleClass} cursor-ns-resize`}
314
+ onPointerDown={(e) => handleResizeStart(e, 'top')}
315
+ />
316
+ <div
317
+ style={{ bottom: '-7px', left: '50%', marginLeft: '-7px' }}
318
+ className={`${handleClass} cursor-ns-resize`}
319
+ onPointerDown={(e) => handleResizeStart(e, 'bottom')}
320
+ />
321
+ <div
322
+ style={{ left: '-7px', top: '50%', marginTop: '-7px' }}
323
+ className={`${handleClass} cursor-ew-resize`}
324
+ onPointerDown={(e) => handleResizeStart(e, 'left')}
325
+ />
326
+ <div
327
+ style={{ right: '-7px', top: '50%', marginTop: '-7px' }}
328
+ className={`${handleClass} cursor-ew-resize`}
329
+ onPointerDown={(e) => handleResizeStart(e, 'right')}
330
+ />
331
+ </>
332
  )}
333
 
334
  {loading ? (
src/features/canvas/hooks/useCanvasLogic.js CHANGED
@@ -484,6 +484,19 @@ export const useCanvasLogic = (projectId, user) => {
484
  delete coupledData.isLoadedFromBackend;
485
 
486
  const updated = { ...n, data: { ...n.data, ...coupledData } };
 
 
 
 
 
 
 
 
 
 
 
 
 
487
  if (coupledData.isPinned !== undefined) {
488
  updated.draggable = !coupledData.isPinned;
489
  }
@@ -589,7 +602,24 @@ export const useCanvasLogic = (projectId, user) => {
589
  }
590
  case 'NODE_DATA': {
591
  const { id, newData } = message.payload;
592
- setNodes(nds => nds.map(n => n.id === id ? { ...n, data: { ...n.data, ...newData } } : n));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
  break;
594
  }
595
  case 'EDGE_ADD': {
 
484
  delete coupledData.isLoadedFromBackend;
485
 
486
  const updated = { ...n, data: { ...n.data, ...coupledData } };
487
+
488
+ if (coupledData.width !== undefined) {
489
+ updated.width = coupledData.width;
490
+ updated.style = { ...updated.style, width: coupledData.width };
491
+ }
492
+ if (coupledData.height !== undefined) {
493
+ updated.height = coupledData.height;
494
+ updated.style = { ...updated.style, height: coupledData.height };
495
+ }
496
+ if (coupledData.x !== undefined && coupledData.y !== undefined) {
497
+ updated.position = { x: coupledData.x, y: coupledData.y };
498
+ }
499
+
500
  if (coupledData.isPinned !== undefined) {
501
  updated.draggable = !coupledData.isPinned;
502
  }
 
602
  }
603
  case 'NODE_DATA': {
604
  const { id, newData } = message.payload;
605
+ setNodes(nds => nds.map(n => {
606
+ if (n.id === id) {
607
+ const updated = { ...n, data: { ...n.data, ...newData } };
608
+ if (newData.width !== undefined) {
609
+ updated.width = newData.width;
610
+ updated.style = { ...updated.style, width: newData.width };
611
+ }
612
+ if (newData.height !== undefined) {
613
+ updated.height = newData.height;
614
+ updated.style = { ...updated.style, height: newData.height };
615
+ }
616
+ if (newData.x !== undefined && newData.y !== undefined) {
617
+ updated.position = { x: newData.x, y: newData.y };
618
+ }
619
+ return updated;
620
+ }
621
+ return n;
622
+ }));
623
  break;
624
  }
625
  case 'EDGE_ADD': {