Spaces:
Running

File size: 5,592 Bytes
9c4e6fe
 
afbb4df
9c4e6fe
 
 
 
 
 
afbb4df
 
 
 
 
 
 
 
9c4e6fe
 
 
 
 
afbb4df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c4e6fe
afbb4df
9c4e6fe
afbb4df
9c4e6fe
 
afbb4df
9c4e6fe
 
 
afbb4df
9c4e6fe
afbb4df
9c4e6fe
 
afbb4df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c4e6fe
 
 
 
afbb4df
 
 
 
 
 
9c4e6fe
afbb4df
 
 
 
 
 
9c4e6fe
 
 
 
 
afbb4df
 
 
9c4e6fe
 
 
 
 
 
 
afbb4df
9c4e6fe
 
 
 
 
afbb4df
 
 
 
 
9c4e6fe
 
 
 
afbb4df
 
9c4e6fe
 
 
 
 
 
 
 
 
 
 
 
 
 
afbb4df
 
 
 
9c4e6fe
afbb4df
 
9c4e6fe
afbb4df
9c4e6fe
 
afbb4df
 
9c4e6fe
afbb4df
9c4e6fe
 
 
 
afbb4df
9c4e6fe
afbb4df
9c4e6fe
 
 
 
 
 
 
 
 
 
afbb4df
 
 
 
9c4e6fe
 
afbb4df
 
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
import React, { useRef, useEffect, useState, useCallback } from 'react';
import { NodeResizer, Handle, Position } from '@xyflow/react';
import { Pencil, Eraser, Trash2 } from 'lucide-react';
import { useTheme } from '../../../../context/ThemeContext';

const DrawNode = ({ id, data, selected }) => {
  const { isDarkMode } = useTheme();
  const canvasRef = useRef(null);
  const [isDrawing, setIsDrawing] = useState(false);
  const [tool, setTool] = useState('pencil');
  
  // Use a ref for drawing properties to avoid re-renders during active draw
  const drawState = useRef({
    ctx: null,
    lastX: 0,
    lastY: 0
  });

  const initCanvas = useCallback(() => {
    const canvas = canvasRef.current;
    if (!canvas) return;
    
    const ctx = canvas.getContext('2d');
    drawState.current.ctx = ctx;

    // Use absolute size from data or measure parent
    const rect = canvas.getBoundingClientRect();
    if (canvas.width !== rect.width || canvas.height !== rect.height) {
      // Save current content before resizing
      const tempImage = canvas.toDataURL();
      canvas.width = rect.width;
      canvas.height = rect.height;
      
      const img = new Image();
      img.onload = () => ctx.drawImage(img, 0, 0);
      img.src = tempImage || data.image;
    }

    if (data.image && !isDrawing) {
      const img = new Image();
      img.onload = () => ctx.drawImage(img, 0, 0);
      img.src = data.image;
    }
  }, [data.image, isDrawing]);

  useEffect(() => {
    initCanvas();
  }, [initCanvas, data.width, data.height]);

  const getCoordinates = (e) => {
    const canvas = canvasRef.current;
    const rect = canvas.getBoundingClientRect();
    // Support both mouse and touch
    const clientX = e.clientX || (e.touches && e.touches[0].clientX);
    const clientY = e.clientY || (e.touches && e.touches[0].clientY);
    return {
      x: clientX - rect.left,
      y: clientY - rect.top
    };
  };

  const startDrawing = (e) => {
    if (!selected) return;
    setIsDrawing(true);
    const { x, y } = getCoordinates(e);
    drawState.current.lastX = x;
    drawState.current.lastY = y;
  };

  const draw = (e) => {
    if (!isDrawing || !selected) return;
    // Prevent canvas movement while drawing
    e.preventDefault();
    e.stopPropagation();

    const { x, y } = getCoordinates(e);
    const ctx = drawState.current.ctx;

    ctx.beginPath();
    ctx.moveTo(drawState.current.lastX, drawState.current.lastY);
    ctx.lineTo(x, y);
    
    ctx.strokeStyle = tool === 'eraser' ? (isDarkMode ? '#1e1e1e' : '#ffffff') : (data.strokeColor || (isDarkMode ? '#ffffff' : '#000000'));
    ctx.lineWidth = tool === 'eraser' ? 20 : (data.lineWidth || 3);
    ctx.lineCap = 'round';
    ctx.lineJoin = 'round';
    ctx.globalCompositeOperation = tool === 'eraser' ? 'destination-out' : 'source-over';
    
    ctx.stroke();
    
    drawState.current.lastX = x;
    drawState.current.lastY = y;
  };

  const stopDrawing = () => {
    if (!isDrawing) return;
    setIsDrawing(false);
    const canvas = canvasRef.current;
    if (data.updateNodeData) {
      data.updateNodeData(id, { image: canvas.toDataURL() });
    }
  };

  const clearCanvas = (e) => {
    e.stopPropagation();
    const ctx = drawState.current.ctx;
    ctx.clearRect(0, 0, canvasRef.current.width, canvasRef.current.height);
    if (data.updateNodeData) {
      data.updateNodeData(id, { image: null });
    }
  };

  return (
    <div 
      className={`relative group transition-all rounded-xl ${
        selected ? 'ring-2 ring-primary shadow-2xl bg-surface-container-low/20' : 'hover:bg-primary/5'
      }`}
      style={{
        width: data.width || 400,
        height: data.height || 300,
      }}
    >
      <NodeResizer 
        isVisible={selected} 
        minWidth={100} 
        minHeight={100} 
        onResizeEnd={() => initCanvas()}
      />

      {selected && (
        <div 
          className="absolute -top-12 left-1/2 -translate-x-1/2 flex items-center gap-1 bg-surface-container-lowest border-2 border-primary rounded-xl px-2 py-1 shadow-lg z-[100]"
          onMouseDown={e => e.stopPropagation()} // Prevent node drag when clicking toolbar
        >
          <button 
            onClick={() => setTool('pencil')}
            className={`p-1.5 rounded-lg ${tool === 'pencil' ? 'bg-primary text-white' : 'text-primary hover:bg-surface-variant'}`}
          >
            <Pencil size={14} />
          </button>
          <button 
            onClick={() => setTool('eraser')}
            className={`p-1.5 rounded-lg ${tool === 'eraser' ? 'bg-primary text-white' : 'text-primary hover:bg-surface-variant'}`}
          >
            <Eraser size={14} />
          </button>
          <div className="w-[1px] h-4 bg-primary/20 mx-1"></div>
          <button 
            onClick={clearCanvas}
            className="p-1.5 rounded-lg text-red-500 hover:bg-red-500/10"
          >
            <Trash2 size={14} />
          </button>
        </div>
      )}

      <canvas
        ref={canvasRef}
        onMouseDown={startDrawing}
        onMouseMove={draw}
        onMouseUp={stopDrawing}
        onMouseLeave={stopDrawing}
        onTouchStart={startDrawing}
        onTouchMove={draw}
        onTouchEnd={stopDrawing}
        className={`block w-full h-full touch-none ${selected ? 'cursor-crosshair' : 'cursor-default pointer-events-none'}`}
      />

      <Handle type="target" position={Position.Top} className="!opacity-0" />
      <Handle type="source" position={Position.Bottom} className="!opacity-0" />
    </div>
  );
};

export default DrawNode;