Spaces:
Running

File size: 14,497 Bytes
4c980ad
b4d6f7f
e0fff03
9c4e6fe
e0fff03
9c4e6fe
14b17c5
9c4e6fe
4555b70
b4d6f7f
e0fff03
 
14b17c5
f2546a6
14b17c5
9bcc2ab
e0fff03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14b17c5
4555b70
 
4fe0fa4
 
14b17c5
 
 
 
 
 
4fe0fa4
14b17c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78167bc
 
 
 
4c980ad
78167bc
 
 
 
 
 
 
 
 
 
 
 
b4d6f7f
 
 
 
 
 
 
f2546a6
 
b4d6f7f
 
 
 
 
 
 
 
 
 
 
f2546a6
 
 
 
 
 
 
b4d6f7f
 
 
 
 
 
 
 
 
 
 
f2546a6
 
 
b4d6f7f
f2546a6
 
 
 
b4d6f7f
f2546a6
 
 
b4d6f7f
f2546a6
 
 
 
b4d6f7f
 
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
 
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
 
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
 
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
 
 
 
f2546a6
b4d6f7f
f2546a6
 
b4d6f7f
f2546a6
b4d6f7f
f2546a6
 
b4d6f7f
f2546a6
 
b4d6f7f
f2546a6
 
 
b4d6f7f
f2546a6
 
 
b4d6f7f
f2546a6
 
 
 
b4d6f7f
 
 
f2546a6
 
 
 
 
 
 
 
 
 
 
 
 
b4d6f7f
 
 
 
 
f2546a6
 
 
 
b4d6f7f
 
 
 
 
 
f2546a6
 
 
 
 
 
 
 
b4d6f7f
f2546a6
 
b4d6f7f
 
 
 
4c980ad
 
f2546a6
 
 
14b17c5
f2546a6
b4d6f7f
4c980ad
9c4e6fe
 
f2546a6
bcbd0a4
9c4e6fe
 
f2546a6
 
bcbd0a4
 
9c4e6fe
 
14b17c5
b4d6f7f
14b17c5
b4d6f7f
14b17c5
b4d6f7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14b17c5
9c4e6fe
e0fff03
 
 
 
 
 
 
 
9c4e6fe
 
 
78167bc
4fe0fa4
9c4e6fe
bcbd0a4
 
 
 
 
9c4e6fe
 
 
 
bcbd0a4
 
 
9c4e6fe
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import React, { useState, useEffect, useRef } from 'react';
import { Handle, Position, useUpdateNodeInternals, useReactFlow } from '@xyflow/react';
import { Image as ImageIcon, Loader2 } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import axios from 'axios';

const ImageNode = ({ id, data, selected, width, height }) => {
  const { t } = useTranslation();
  const updateNodeInternals = useUpdateNodeInternals();
  const { getZoom, getNode, setNodes } = useReactFlow();
  const [imageUrl, setImageUrl] = useState(data.url || '');
  const [loading, setLoading] = useState(false);
  const [isShiftPressed, setIsShiftPressed] = useState(false);
  const [isResizing, setIsResizing] = useState(false);
  const imageRatioRef = useRef(1);

  // Load large data from backend on mount if not loaded
  useEffect(() => {
    if (!imageUrl && data.hasLargeData) {
      setLoading(true);
      const API_URL = import.meta.env.VITE_API_URL;
      axios.get(`${API_URL}/TaskNodes/${id}/data`)
        .then(res => {
          if (res.data && res.data.data) {
            setImageUrl(res.data.data);
            if (data.updateNodeData) {
              data.updateNodeData(id, { url: res.data.data, isLoadedFromBackend: true });
            }
          }
        })
        .catch(err => console.error('[ImageNode] Error loading large data:', err))
        .finally(() => setLoading(false));
    }
  }, [id, imageUrl, data.hasLargeData]);

  // Sync if data changes from remote
  useEffect(() => {
    if (data.url && data.url !== imageUrl) {
      setImageUrl(data.url);
    }
  }, [data.url]);

  // Re-calculate handles when data dimensions update
  useEffect(() => {
    updateNodeInternals(id);
  }, [id, data.width, data.height, updateNodeInternals]);

  // Sync aspect ratio ref if size is changed
  useEffect(() => {
    if (data.width && data.height && data.height !== 'auto') {
      imageRatioRef.current = data.width / data.height;
    }
  }, [data.width, data.height]);

  // Listen to Shift key events globally to toggle aspect ratio constraint
  useEffect(() => {
    const handleKeyDown = (e) => {
      if (e.key === 'Shift') {
        setIsShiftPressed(true);
      }
    };
    const handleKeyUp = (e) => {
      if (e.key === 'Shift') {
        setIsShiftPressed(false);
      }
    };
    const handleBlur = () => {
      setIsShiftPressed(false);
    };

    window.addEventListener('keydown', handleKeyDown);
    window.addEventListener('keyup', handleKeyUp);
    window.addEventListener('blur', handleBlur);
    return () => {
      window.removeEventListener('keydown', handleKeyDown);
      window.removeEventListener('keyup', handleKeyUp);
      window.removeEventListener('blur', handleBlur);
    };
  }, []);

  const handleImageLoad = (e) => {
    const { naturalWidth, naturalHeight } = e.target;
    if (naturalWidth && naturalHeight) {
      const ratio = naturalWidth / naturalHeight;
      imageRatioRef.current = ratio;
      // Only set initial size if data.height is 'auto' or not set in pixels
      if (!data.height || data.height === 'auto') {
        const initialWidth = data.width || 300;
        const calculatedHeight = Math.round(initialWidth / ratio);
        
        if (data.updateNodeData) {
          data.updateNodeData(id, { width: initialWidth, height: calculatedHeight });
        }
      }
    }
  };

  const handleResizeStart = (e, direction) => {
    e.stopPropagation();
    e.preventDefault();

    const node = getNode(id);
    if (!node) return;

    setIsResizing(true);

    const startX = e.clientX;
    const startY = e.clientY;
    const startPos = { ...node.position };
    const startSize = {
      width: node.width || node.style?.width || data.width || 300,
      height: node.height || node.style?.height || data.height || 200
    };

    const ratio = imageRatioRef.current || (startSize.width / startSize.height) || 1;
    const zoom = getZoom();

    let finalWidth = startSize.width;
    let finalHeight = startSize.height;
    let finalX = startPos.x;
    let finalY = startPos.y;

    const nodeElement = document.querySelector(`.react-flow__node[data-id="${id}"]`);

    const handlePointerMove = (moveEvent) => {
      const dx = (moveEvent.clientX - startX) / zoom;
      const dy = (moveEvent.clientY - startY) / zoom;

      const shiftPressed = moveEvent.shiftKey || isShiftPressed;
      const isCorner = ['top-left', 'top-right', 'bottom-left', 'bottom-right'].includes(direction);
      const shouldLockRatio = isCorner || shiftPressed;

      if (shouldLockRatio) {
        // --- PROPORTIONAL RESIZING ---
        if (direction === 'right') {
          finalWidth = Math.max(50, startSize.width + dx);
          finalHeight = Math.round(finalWidth / ratio);
          finalY = startPos.y;
        } else if (direction === 'left') {
          finalWidth = Math.max(50, startSize.width - dx);
          finalHeight = Math.round(finalWidth / ratio);
          finalX = startPos.x + (startSize.width - finalWidth);
          finalY = startPos.y;
        } else if (direction === 'bottom') {
          finalHeight = Math.max(50, startSize.height + dy);
          finalWidth = Math.round(finalHeight * ratio);
          finalX = startPos.x;
        } else if (direction === 'top') {
          finalHeight = Math.max(50, startSize.height - dy);
          finalWidth = Math.round(finalHeight * ratio);
          finalY = startPos.y + (startSize.height - finalHeight);
          finalX = startPos.x;
        } else if (direction === 'bottom-right') {
          if (Math.abs(dx) > Math.abs(dy)) {
            finalWidth = Math.max(50, startSize.width + dx);
            finalHeight = Math.round(finalWidth / ratio);
          } else {
            finalHeight = Math.max(50, startSize.height + dy);
            finalWidth = Math.round(finalHeight * ratio);
          }
          finalX = startPos.x;
          finalY = startPos.y;
        } else if (direction === 'bottom-left') {
          if (Math.abs(dx) > Math.abs(dy)) {
            finalWidth = Math.max(50, startSize.width - dx);
            finalHeight = Math.round(finalWidth / ratio);
          } else {
            finalHeight = Math.max(50, startSize.height + dy);
            finalWidth = Math.round(finalHeight * ratio);
          }
          finalX = startPos.x + (startSize.width - finalWidth);
          finalY = startPos.y;
        } else if (direction === 'top-right') {
          if (Math.abs(dx) > Math.abs(dy)) {
            finalWidth = Math.max(50, startSize.width + dx);
            finalHeight = Math.round(finalWidth / ratio);
          } else {
            finalHeight = Math.max(50, startSize.height - dy);
            finalWidth = Math.round(finalHeight * ratio);
          }
          finalX = startPos.x;
          finalY = startPos.y + (startSize.height - finalHeight);
        } else if (direction === 'top-left') {
          if (Math.abs(dx) > Math.abs(dy)) {
            finalWidth = Math.max(50, startSize.width - dx);
            finalHeight = Math.round(finalWidth / ratio);
          } else {
            finalHeight = Math.max(50, startSize.height - dy);
            finalWidth = Math.round(finalHeight * ratio);
          }
          finalX = startPos.x + (startSize.width - finalWidth);
          finalY = startPos.y + (startSize.height - finalHeight);
        }
      } else {
        // --- DISTORTION RESIZING ---
        if (direction === 'right') {
          finalWidth = Math.max(50, startSize.width + dx);
        } else if (direction === 'left') {
          finalWidth = Math.max(50, startSize.width - dx);
          finalX = startPos.x + (startSize.width - finalWidth);
        } else if (direction === 'bottom') {
          finalHeight = Math.max(50, startSize.height + dy);
        } else if (direction === 'top') {
          finalHeight = Math.max(50, startSize.height - dy);
          finalY = startPos.y + (startSize.height - finalHeight);
        } else if (direction === 'bottom-right') {
          finalWidth = Math.max(50, startSize.width + dx);
          finalHeight = Math.max(50, startSize.height + dy);
        } else if (direction === 'bottom-left') {
          finalWidth = Math.max(50, startSize.width - dx);
          finalHeight = Math.max(50, startSize.height + dy);
          finalX = startPos.x + (startSize.width - finalWidth);
        } else if (direction === 'top-right') {
          finalWidth = Math.max(50, startSize.width + dx);
          finalHeight = Math.max(50, startSize.height - dy);
          finalY = startPos.y + (startSize.height - finalHeight);
        } else if (direction === 'top-left') {
          finalWidth = Math.max(50, startSize.width - dx);
          finalHeight = Math.max(50, startSize.height - dy);
          finalX = startPos.x + (startSize.width - finalWidth);
          finalY = startPos.y + (startSize.height - finalHeight);
        }
      }

      // Direct DOM update for 60fps buttery smooth dragging
      if (nodeElement) {
        nodeElement.style.width = `${finalWidth}px`;
        nodeElement.style.height = `${finalHeight}px`;
        nodeElement.style.transform = `translate(${finalX}px, ${finalY}px)`;
      }
    };

    const handlePointerUp = () => {
      window.removeEventListener('pointermove', handlePointerMove);
      window.removeEventListener('pointerup', handlePointerUp);

      // React state update on drop to sync react-flow store
      setNodes((nds) =>
        nds.map((n) => {
          if (n.id === id) {
            return {
              ...n,
              position: { x: finalX, y: finalY },
              width: finalWidth,
              height: finalHeight,
              style: { ...n.style, width: finalWidth, height: finalHeight }
            };
          }
          return n;
        })
      );

      // Save new dimensions and position to database, history and broadcast
      if (data.updateNodeData) {
        data.updateNodeData(id, { 
          width: finalWidth, 
          height: finalHeight,
          x: finalX,
          y: finalY
        });
      }

      setIsResizing(false);
    };

    window.addEventListener('pointermove', handlePointerMove);
    window.addEventListener('pointerup', handlePointerUp);
  };

  const isCustomSized = width !== undefined || isResizing;
  const styleWidth = isCustomSized ? '100%' : (data.width || 300);
  const styleHeight = isCustomSized ? '100%' : (data.height || 'auto');

  // Common handle style & class (Removed transition-all to prevent lag on resize animation)
  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";

  return (
    <div 
      className={`relative group ${
        selected ? 'ring-2 ring-primary/40 shadow-xl' : ''
      }`}
      style={{
        width: styleWidth,
        height: styleHeight,
        minWidth: 50,
        minHeight: 50,
      }}
    >
      {selected && (
        <>
          {/* Custom Dashed Outline */}
          <div className="absolute inset-0 border-2 border-dashed border-primary opacity-60 rounded-lg pointer-events-none z-10" />
          
          {/* Corners (Always Proportional) */}
          <div 
            style={{ top: '-7px', left: '-7px' }}
            className={`${handleClass} cursor-nwse-resize`}
            onPointerDown={(e) => handleResizeStart(e, 'top-left')}
          />
          <div 
            style={{ top: '-7px', right: '-7px' }}
            className={`${handleClass} cursor-nesw-resize`}
            onPointerDown={(e) => handleResizeStart(e, 'top-right')}
          />
          <div 
            style={{ bottom: '-7px', left: '-7px' }}
            className={`${handleClass} cursor-nesw-resize`}
            onPointerDown={(e) => handleResizeStart(e, 'bottom-left')}
          />
          <div 
            style={{ bottom: '-7px', right: '-7px' }}
            className={`${handleClass} cursor-nwse-resize`}
            onPointerDown={(e) => handleResizeStart(e, 'bottom-right')}
          />

          {/* Sides (Proportional only if Shift is held) */}
          <div 
            style={{ top: '-7px', left: '50%', marginLeft: '-7px' }}
            className={`${handleClass} cursor-ns-resize`}
            onPointerDown={(e) => handleResizeStart(e, 'top')}
          />
          <div 
            style={{ bottom: '-7px', left: '50%', marginLeft: '-7px' }}
            className={`${handleClass} cursor-ns-resize`}
            onPointerDown={(e) => handleResizeStart(e, 'bottom')}
          />
          <div 
            style={{ left: '-7px', top: '50%', marginTop: '-7px' }}
            className={`${handleClass} cursor-ew-resize`}
            onPointerDown={(e) => handleResizeStart(e, 'left')}
          />
          <div 
            style={{ right: '-7px', top: '50%', marginTop: '-7px' }}
            className={`${handleClass} cursor-ew-resize`}
            onPointerDown={(e) => handleResizeStart(e, 'right')}
          />
        </>
      )}

      {loading ? (
        <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">
          <Loader2 size={24} className="text-primary animate-spin" />
          <p className="text-[10px] font-bold text-primary/40 uppercase tracking-widest">
            {t('canvas.image_loading', 'Завантаження...')}
          </p>
        </div>
      ) : imageUrl ? (
        <img 
          src={imageUrl} 
          alt="Canvas Asset" 
          onLoad={handleImageLoad}
          className="w-full h-full object-fill pointer-events-none select-none rounded-lg"
        />
      ) : (
        <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">
          <ImageIcon size={24} className="text-primary/20" />
          <p className="text-[10px] font-bold text-primary/40 uppercase tracking-widest">
            {t('canvas.image_drop_hint', 'Drop image here')}
          </p>
        </div>
      )}

      {/* Connection points - barely visible */}
      <Handle type="target" position={Position.Top} className="!opacity-0 group-hover:!opacity-30" />
      <Handle type="source" position={Position.Bottom} className="!opacity-0 group-hover:!opacity-30" />
    </div>
  );
};

export default ImageNode;