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

Fix Shift selection marquee and ImageNode resize translation issues

Browse files
src/features/canvas/components/nodes/ImageNode.jsx CHANGED
@@ -1,20 +1,17 @@
1
  import React, { useState, useEffect, useRef } from 'react';
2
- import { NodeResizer, 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';
6
 
7
- const ImageNode = ({ id, data, selected }) => {
8
  const { t } = useTranslation();
9
  const updateNodeInternals = useUpdateNodeInternals();
10
  const [imageUrl, setImageUrl] = useState(data.url || '');
11
  const [loading, setLoading] = useState(false);
12
-
13
- // Local state for smooth resizing without lag
14
- const [localSize, setLocalSize] = useState({
15
- width: data.width || 300,
16
- height: data.height || 'auto'
17
- });
18
 
19
  // Load large data from backend on mount if not loaded
20
  useEffect(() => {
@@ -42,28 +39,66 @@ const ImageNode = ({ id, data, selected }) => {
42
  }
43
  }, [data.url]);
44
 
45
- // Re-calculate handles when data dimensions update (instead of on every pixel movement to eliminate lag)
46
  useEffect(() => {
47
  updateNodeInternals(id);
48
  }, [id, data.width, data.height, updateNodeInternals]);
49
 
50
- const activeDirectionRef = useRef('');
51
- const imageRatioRef = useRef(1);
 
 
 
 
52
 
53
- const handleResizeStart = (e) => {
54
- const classList = e.target.classList;
55
- let direction = '';
56
- if (classList.contains('top-left')) direction = 'top-left';
57
- else if (classList.contains('top-right')) direction = 'top-right';
58
- else if (classList.contains('bottom-left')) direction = 'bottom-left';
59
- else if (classList.contains('bottom-right')) direction = 'bottom-right';
60
- else if (classList.contains('top')) direction = 'top';
61
- else if (classList.contains('bottom')) direction = 'bottom';
62
- else if (classList.contains('left')) direction = 'left';
63
- else if (classList.contains('right')) direction = 'right';
64
-
65
- activeDirectionRef.current = direction;
66
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
 
68
  const handleImageLoad = (e) => {
69
  const { naturalWidth, naturalHeight } = e.target;
@@ -75,7 +110,6 @@ const ImageNode = ({ id, data, selected }) => {
75
  const initialWidth = data.width || 300;
76
  const calculatedHeight = Math.round(initialWidth / ratio);
77
 
78
- setLocalSize({ width: initialWidth, height: calculatedHeight });
79
  if (data.updateNodeData) {
80
  data.updateNodeData(id, { width: initialWidth, height: calculatedHeight });
81
  }
@@ -83,71 +117,20 @@ const ImageNode = ({ id, data, selected }) => {
83
  }
84
  };
85
 
86
- // Sync aspect ratio ref if size is changed externally
87
- useEffect(() => {
88
- if (data.width && data.height && data.height !== 'auto') {
89
- imageRatioRef.current = data.width / data.height;
90
- }
91
- }, [data.width, data.height]);
92
-
93
- useEffect(() => {
94
- if (!selected) {
95
- setLocalSize({
96
- width: data.width || 300,
97
- height: data.height || 'auto'
98
- });
99
- }
100
- }, [data.width, data.height, selected]);
101
-
102
- const handleResize = (e, params) => {
103
- const shiftPressed = e.sourceEvent && e.sourceEvent.shiftKey;
104
- const direction = activeDirectionRef.current;
105
-
106
- // Shift locks aspect ratio for side handles, otherwise we stretch
107
- if (shiftPressed && imageRatioRef.current) {
108
- const ratio = imageRatioRef.current;
109
- let newWidth = params.width;
110
- let newHeight = params.height;
111
-
112
- if (direction === 'left' || direction === 'right') {
113
- newHeight = Math.round(newWidth / ratio);
114
- } else if (direction === 'top' || direction === 'bottom') {
115
- newWidth = Math.round(newHeight * ratio);
116
- } else {
117
- newHeight = Math.round(newWidth / ratio);
118
- }
119
-
120
- setLocalSize({ width: newWidth, height: newHeight });
121
- } else {
122
- setLocalSize({ width: params.width, height: params.height });
123
  }
124
  };
125
 
126
- const handleResizeEnd = (e, params) => {
127
- const shiftPressed = e.sourceEvent && e.sourceEvent.shiftKey;
128
- const direction = activeDirectionRef.current;
129
-
130
- if (shiftPressed && imageRatioRef.current) {
131
- const ratio = imageRatioRef.current;
132
- let newWidth = params.width;
133
- let newHeight = params.height;
134
-
135
- if (direction === 'left' || direction === 'right') {
136
- newHeight = Math.round(newWidth / ratio);
137
- } else if (direction === 'top' || direction === 'bottom') {
138
- newWidth = Math.round(newHeight * ratio);
139
- } else {
140
- newHeight = Math.round(newWidth / ratio);
141
- }
142
-
143
- if (data.updateNodeData) {
144
- data.updateNodeData(id, { width: newWidth, height: newHeight });
145
- }
146
- } else {
147
- if (data.updateNodeData) {
148
- data.updateNodeData(id, { width: params.width, height: params.height });
149
- }
150
- }
151
  };
152
 
153
  return (
@@ -156,38 +139,30 @@ const ImageNode = ({ id, data, selected }) => {
156
  selected ? 'ring-2 ring-primary/40 shadow-xl' : ''
157
  }`}
158
  style={{
159
- width: localSize.width,
160
- height: localSize.height,
161
  minWidth: 50,
162
  minHeight: 50,
163
  }}
164
  >
165
- {/* Corner Resizer: keepAspectRatio = true */}
166
- <NodeResizer
167
- isVisible={selected}
168
- minWidth={50}
169
- minHeight={50}
170
- keepAspectRatio={true}
171
- directions={['top-left', 'top-right', 'bottom-left', 'bottom-right']}
172
- onResizeStart={handleResizeStart}
173
- lineClassName="!border-primary !border-2 !border-dashed opacity-60"
174
- handleClassName="!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"
175
- onResize={handleResize}
176
- onResizeEnd={handleResizeEnd}
177
- />
178
- {/* Side Resizer: keepAspectRatio = false */}
179
- <NodeResizer
180
- isVisible={selected}
181
- minWidth={50}
182
- minHeight={50}
183
- keepAspectRatio={false}
184
- directions={['top', 'right', 'bottom', 'left']}
185
- onResizeStart={handleResizeStart}
186
- lineClassName="!border-primary !border-2 !border-dashed opacity-60"
187
- handleClassName="!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"
188
- onResize={handleResize}
189
- onResizeEnd={handleResizeEnd}
190
- />
191
 
192
  {loading ? (
193
  <div className="w-full h-full min-h-[100px] bg-secondary/5 flex flex-col items-center justify-center gap-2 p-4 text-center border-2 border-dashed border-primary/10">
 
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';
6
 
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
17
  useEffect(() => {
 
39
  }
40
  }, [data.url]);
41
 
42
+ // Re-calculate handles when data dimensions update
43
  useEffect(() => {
44
  updateNodeInternals(id);
45
  }, [id, data.width, data.height, updateNodeInternals]);
46
 
47
+ // Sync aspect ratio ref if size is changed
48
+ useEffect(() => {
49
+ if (data.width && data.height && data.height !== 'auto') {
50
+ imageRatioRef.current = data.width / data.height;
51
+ }
52
+ }, [data.width, data.height]);
53
 
54
+ // Listen to Shift key events globally to toggle aspect ratio constraint
55
+ useEffect(() => {
56
+ const handleKeyDown = (e) => {
57
+ if (e.key === 'Shift') {
58
+ setIsShiftPressed(true);
59
+ }
60
+ };
61
+ const handleKeyUp = (e) => {
62
+ if (e.key === 'Shift') {
63
+ setIsShiftPressed(false);
64
+ }
65
+ };
66
+ const handleBlur = () => {
67
+ setIsShiftPressed(false);
68
+ };
69
+
70
+ window.addEventListener('keydown', handleKeyDown);
71
+ window.addEventListener('keyup', handleKeyUp);
72
+ window.addEventListener('blur', handleBlur);
73
+ return () => {
74
+ window.removeEventListener('keydown', handleKeyDown);
75
+ window.removeEventListener('keyup', handleKeyUp);
76
+ window.removeEventListener('blur', handleBlur);
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;
 
110
  const initialWidth = data.width || 300;
111
  const calculatedHeight = Math.round(initialWidth / ratio);
112
 
 
113
  if (data.updateNodeData) {
114
  data.updateNodeData(id, { width: initialWidth, height: calculatedHeight });
115
  }
 
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 (
 
139
  selected ? 'ring-2 ring-primary/40 shadow-xl' : ''
140
  }`}
141
  style={{
142
+ width: currentWidth,
143
+ height: currentHeight,
144
  minWidth: 50,
145
  minHeight: 50,
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 ? (
168
  <div className="w-full h-full min-h-[100px] bg-secondary/5 flex flex-col items-center justify-center gap-2 p-4 text-center border-2 border-dashed border-primary/10">
src/pages/CanvasPage.jsx CHANGED
@@ -2995,7 +2995,7 @@ const CanvasInner = () => {
2995
  nodesConnectable={!drawingMode && permission !== 'Read'}
2996
  panOnDrag={!drawingMode}
2997
  selectionOnDrag={!drawingMode}
2998
- selectionKeyCode={['Control', 'Meta']}
2999
  nodeTypes={nodeTypes}
3000
  edgeTypes={edgeTypes}
3001
  connectionLineType={ConnectionLineType.SmoothStep}
 
2995
  nodesConnectable={!drawingMode && permission !== 'Read'}
2996
  panOnDrag={!drawingMode}
2997
  selectionOnDrag={!drawingMode}
2998
+ selectionKeyCode="Shift"
2999
  nodeTypes={nodeTypes}
3000
  edgeTypes={edgeTypes}
3001
  connectionLineType={ConnectionLineType.SmoothStep}