Spaces:
Running
Running
Commit ·
23b8432
1
Parent(s): f2546a6
Update TextNode to use high-performance custom resize handles and support double-click edit mode that blocks node dragging for text selection
Browse files
src/features/canvas/components/nodes/TextNode.jsx
CHANGED
|
@@ -1,14 +1,16 @@
|
|
| 1 |
import React, { useState, useEffect, useRef } from 'react';
|
| 2 |
-
import {
|
| 3 |
import { useTranslation } from 'react-i18next';
|
| 4 |
import { useTheme } from '../../../../context/ThemeContext';
|
| 5 |
|
| 6 |
-
const TextNode = ({ id, data, selected }) => {
|
| 7 |
const { t } = useTranslation();
|
| 8 |
const { isDarkMode } = useTheme();
|
| 9 |
const updateNodeInternals = useUpdateNodeInternals();
|
|
|
|
| 10 |
const [localText, setLocalText] = useState(data.label || '');
|
| 11 |
-
const [
|
|
|
|
| 12 |
const textareaRef = useRef(null);
|
| 13 |
|
| 14 |
// Auto-resize textarea height
|
|
@@ -21,17 +23,29 @@ const TextNode = ({ id, data, selected }) => {
|
|
| 21 |
// Notify React Flow that height (and handles) have shifted
|
| 22 |
updateNodeInternals(id);
|
| 23 |
}
|
| 24 |
-
}, [localText,
|
| 25 |
|
|
|
|
| 26 |
useEffect(() => {
|
| 27 |
if (document.activeElement !== textareaRef.current) {
|
| 28 |
setLocalText(data.label || '');
|
| 29 |
}
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
const handleTextChange = (e) => {
|
| 37 |
const newText = e.target.value;
|
|
@@ -41,47 +55,194 @@ const TextNode = ({ id, data, selected }) => {
|
|
| 41 |
}
|
| 42 |
};
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
const fontSize = data.fontSize || 18;
|
| 45 |
const color = data.color || (isDarkMode ? '#ffffff' : '#31363F');
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
return (
|
| 48 |
<div
|
| 49 |
-
|
| 50 |
-
|
|
|
|
| 51 |
}`}
|
| 52 |
style={{
|
| 53 |
-
width:
|
|
|
|
| 54 |
minWidth: 50,
|
| 55 |
}}
|
| 56 |
>
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
<textarea
|
| 71 |
ref={textareaRef}
|
| 72 |
value={localText}
|
| 73 |
onChange={handleTextChange}
|
|
|
|
|
|
|
| 74 |
placeholder={t('canvas.text_placeholder', 'Type something...')}
|
| 75 |
-
className=
|
|
|
|
|
|
|
| 76 |
style={{
|
| 77 |
fontSize: `${fontSize}px`,
|
| 78 |
color: color,
|
| 79 |
textAlign: data.textAlign || 'left',
|
| 80 |
-
cursor:
|
| 81 |
}}
|
| 82 |
onKeyDown={(e) => e.stopPropagation()}
|
| 83 |
onMouseDown={(e) => {
|
| 84 |
-
if (
|
| 85 |
}}
|
| 86 |
/>
|
| 87 |
|
|
|
|
| 1 |
import React, { useState, useEffect, useRef } from 'react';
|
| 2 |
+
import { Handle, Position, useUpdateNodeInternals, useReactFlow } from '@xyflow/react';
|
| 3 |
import { useTranslation } from 'react-i18next';
|
| 4 |
import { useTheme } from '../../../../context/ThemeContext';
|
| 5 |
|
| 6 |
+
const TextNode = ({ id, data, selected, width }) => {
|
| 7 |
const { t } = useTranslation();
|
| 8 |
const { isDarkMode } = useTheme();
|
| 9 |
const updateNodeInternals = useUpdateNodeInternals();
|
| 10 |
+
const { getZoom, getNode, setNodes } = useReactFlow();
|
| 11 |
const [localText, setLocalText] = useState(data.label || '');
|
| 12 |
+
const [isResizing, setIsResizing] = useState(false);
|
| 13 |
+
const [isEditing, setIsEditing] = useState(false);
|
| 14 |
const textareaRef = useRef(null);
|
| 15 |
|
| 16 |
// Auto-resize textarea height
|
|
|
|
| 23 |
// Notify React Flow that height (and handles) have shifted
|
| 24 |
updateNodeInternals(id);
|
| 25 |
}
|
| 26 |
+
}, [localText, width, data.width, id, updateNodeInternals]);
|
| 27 |
|
| 28 |
+
// Sync if data changes from remote
|
| 29 |
useEffect(() => {
|
| 30 |
if (document.activeElement !== textareaRef.current) {
|
| 31 |
setLocalText(data.label || '');
|
| 32 |
}
|
| 33 |
+
}, [data.label]);
|
| 34 |
+
|
| 35 |
+
// Sync node's draggable state with editing state
|
| 36 |
+
useEffect(() => {
|
| 37 |
+
setNodes((nds) =>
|
| 38 |
+
nds.map((n) => {
|
| 39 |
+
if (n.id === id) {
|
| 40 |
+
return {
|
| 41 |
+
...n,
|
| 42 |
+
draggable: isEditing ? false : !data.isPinned
|
| 43 |
+
};
|
| 44 |
+
}
|
| 45 |
+
return n;
|
| 46 |
+
})
|
| 47 |
+
);
|
| 48 |
+
}, [isEditing, id, data.isPinned, setNodes]);
|
| 49 |
|
| 50 |
const handleTextChange = (e) => {
|
| 51 |
const newText = e.target.value;
|
|
|
|
| 55 |
}
|
| 56 |
};
|
| 57 |
|
| 58 |
+
const handleDoubleClick = (e) => {
|
| 59 |
+
e.stopPropagation();
|
| 60 |
+
setIsEditing(true);
|
| 61 |
+
setTimeout(() => {
|
| 62 |
+
if (textareaRef.current) {
|
| 63 |
+
textareaRef.current.focus();
|
| 64 |
+
textareaRef.current.select();
|
| 65 |
+
}
|
| 66 |
+
}, 0);
|
| 67 |
+
};
|
| 68 |
+
|
| 69 |
+
const handleBlur = () => {
|
| 70 |
+
setIsEditing(false);
|
| 71 |
+
if (data.updateNodeData) {
|
| 72 |
+
data.updateNodeData(id, { label: localText });
|
| 73 |
+
}
|
| 74 |
+
};
|
| 75 |
+
|
| 76 |
+
const handleResizeStart = (e, direction) => {
|
| 77 |
+
e.stopPropagation();
|
| 78 |
+
e.preventDefault();
|
| 79 |
+
|
| 80 |
+
const node = getNode(id);
|
| 81 |
+
if (!node) return;
|
| 82 |
+
|
| 83 |
+
setIsResizing(true);
|
| 84 |
+
|
| 85 |
+
const startX = e.clientX;
|
| 86 |
+
const startY = e.clientY;
|
| 87 |
+
const startPos = { ...node.position };
|
| 88 |
+
const startSize = {
|
| 89 |
+
width: node.width || node.style?.width || data.width || 250,
|
| 90 |
+
height: node.height || node.style?.height || 100
|
| 91 |
+
};
|
| 92 |
+
|
| 93 |
+
const zoom = getZoom();
|
| 94 |
+
|
| 95 |
+
let finalWidth = startSize.width;
|
| 96 |
+
let finalX = startPos.x;
|
| 97 |
+
|
| 98 |
+
const nodeElement = document.querySelector(`.react-flow__node[data-id="${id}"]`);
|
| 99 |
+
|
| 100 |
+
const handlePointerMove = (moveEvent) => {
|
| 101 |
+
const dx = (moveEvent.clientX - startX) / zoom;
|
| 102 |
+
const dy = (moveEvent.clientY - startY) / zoom;
|
| 103 |
+
|
| 104 |
+
// For TextNode, we only resize width
|
| 105 |
+
if (['right', 'top-right', 'bottom-right', 'top', 'bottom'].includes(direction)) {
|
| 106 |
+
// Dragging right-side / vertical handles: expand width to the right
|
| 107 |
+
const delta = ['top', 'bottom'].includes(direction) ? dy : dx;
|
| 108 |
+
finalWidth = Math.max(50, startSize.width + delta);
|
| 109 |
+
finalX = startPos.x;
|
| 110 |
+
} else if (['left', 'top-left', 'bottom-left'].includes(direction)) {
|
| 111 |
+
// Dragging left-side handles: expand width to the left, shifting position x
|
| 112 |
+
finalWidth = Math.max(50, startSize.width - dx);
|
| 113 |
+
finalX = startPos.x + (startSize.width - finalWidth);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
if (nodeElement) {
|
| 117 |
+
nodeElement.style.width = `${finalWidth}px`;
|
| 118 |
+
nodeElement.style.transform = `translate(${finalX}px, ${startPos.y}px)`;
|
| 119 |
+
}
|
| 120 |
+
};
|
| 121 |
+
|
| 122 |
+
const handlePointerUp = () => {
|
| 123 |
+
window.removeEventListener('pointermove', handlePointerMove);
|
| 124 |
+
window.removeEventListener('pointerup', handlePointerUp);
|
| 125 |
+
|
| 126 |
+
setNodes((nds) =>
|
| 127 |
+
nds.map((n) => {
|
| 128 |
+
if (n.id === id) {
|
| 129 |
+
return {
|
| 130 |
+
...n,
|
| 131 |
+
position: { x: finalX, y: startPos.y },
|
| 132 |
+
width: finalWidth,
|
| 133 |
+
style: { ...n.style, width: finalWidth }
|
| 134 |
+
};
|
| 135 |
+
}
|
| 136 |
+
return n;
|
| 137 |
+
})
|
| 138 |
+
);
|
| 139 |
+
|
| 140 |
+
if (data.updateNodeData) {
|
| 141 |
+
data.updateNodeData(id, {
|
| 142 |
+
width: finalWidth,
|
| 143 |
+
x: finalX,
|
| 144 |
+
y: startPos.y
|
| 145 |
+
});
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
setIsResizing(false);
|
| 149 |
+
};
|
| 150 |
+
|
| 151 |
+
window.addEventListener('pointermove', handlePointerMove);
|
| 152 |
+
window.addEventListener('pointerup', handlePointerUp);
|
| 153 |
+
};
|
| 154 |
+
|
| 155 |
+
const isCustomSized = width !== undefined || isResizing;
|
| 156 |
+
const styleWidth = isCustomSized ? '100%' : (data.width || 250);
|
| 157 |
+
|
| 158 |
const fontSize = data.fontSize || 18;
|
| 159 |
const color = data.color || (isDarkMode ? '#ffffff' : '#31363F');
|
| 160 |
|
| 161 |
+
// Common handle style & class (Removed transition-all to prevent scale animation lag)
|
| 162 |
+
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";
|
| 163 |
+
|
| 164 |
return (
|
| 165 |
<div
|
| 166 |
+
onDoubleClick={handleDoubleClick}
|
| 167 |
+
className={`relative group p-1 ${
|
| 168 |
+
selected ? 'ring-2 ring-primary/40 shadow-xl rounded-md' : ''
|
| 169 |
}`}
|
| 170 |
style={{
|
| 171 |
+
width: styleWidth,
|
| 172 |
+
height: 'auto',
|
| 173 |
minWidth: 50,
|
| 174 |
}}
|
| 175 |
>
|
| 176 |
+
{selected && !isEditing && (
|
| 177 |
+
<>
|
| 178 |
+
{/* Custom Dashed Outline */}
|
| 179 |
+
<div className="absolute inset-0 border-2 border-dashed border-primary opacity-60 rounded-lg pointer-events-none z-10" />
|
| 180 |
+
|
| 181 |
+
{/* Corners */}
|
| 182 |
+
<div
|
| 183 |
+
style={{ top: '-7px', left: '-7px' }}
|
| 184 |
+
className={`${handleClass} cursor-nwse-resize`}
|
| 185 |
+
onPointerDown={(e) => handleResizeStart(e, 'top-left')}
|
| 186 |
+
/>
|
| 187 |
+
<div
|
| 188 |
+
style={{ top: '-7px', right: '-7px' }}
|
| 189 |
+
className={`${handleClass} cursor-nesw-resize`}
|
| 190 |
+
onPointerDown={(e) => handleResizeStart(e, 'top-right')}
|
| 191 |
+
/>
|
| 192 |
+
<div
|
| 193 |
+
style={{ bottom: '-7px', left: '-7px' }}
|
| 194 |
+
className={`${handleClass} cursor-nesw-resize`}
|
| 195 |
+
onPointerDown={(e) => handleResizeStart(e, 'bottom-left')}
|
| 196 |
+
/>
|
| 197 |
+
<div
|
| 198 |
+
style={{ bottom: '-7px', right: '-7px' }}
|
| 199 |
+
className={`${handleClass} cursor-nwse-resize`}
|
| 200 |
+
onPointerDown={(e) => handleResizeStart(e, 'bottom-right')}
|
| 201 |
+
/>
|
| 202 |
+
|
| 203 |
+
{/* Sides */}
|
| 204 |
+
<div
|
| 205 |
+
style={{ top: '-7px', left: '50%', marginLeft: '-7px' }}
|
| 206 |
+
className={`${handleClass} cursor-ns-resize`}
|
| 207 |
+
onPointerDown={(e) => handleResizeStart(e, 'top')}
|
| 208 |
+
/>
|
| 209 |
+
<div
|
| 210 |
+
style={{ bottom: '-7px', left: '50%', marginLeft: '-7px' }}
|
| 211 |
+
className={`${handleClass} cursor-ns-resize`}
|
| 212 |
+
onPointerDown={(e) => handleResizeStart(e, 'bottom')}
|
| 213 |
+
/>
|
| 214 |
+
<div
|
| 215 |
+
style={{ left: '-7px', top: '50%', marginTop: '-7px' }}
|
| 216 |
+
className={`${handleClass} cursor-ew-resize`}
|
| 217 |
+
onPointerDown={(e) => handleResizeStart(e, 'left')}
|
| 218 |
+
/>
|
| 219 |
+
<div
|
| 220 |
+
style={{ right: '-7px', top: '50%', marginTop: '-7px' }}
|
| 221 |
+
className={`${handleClass} cursor-ew-resize`}
|
| 222 |
+
onPointerDown={(e) => handleResizeStart(e, 'right')}
|
| 223 |
+
/>
|
| 224 |
+
</>
|
| 225 |
+
)}
|
| 226 |
|
| 227 |
<textarea
|
| 228 |
ref={textareaRef}
|
| 229 |
value={localText}
|
| 230 |
onChange={handleTextChange}
|
| 231 |
+
onBlur={handleBlur}
|
| 232 |
+
readOnly={!isEditing}
|
| 233 |
placeholder={t('canvas.text_placeholder', 'Type something...')}
|
| 234 |
+
className={`w-full bg-transparent border-none outline-none resize-none overflow-hidden font-accent-note leading-tight p-0 ${
|
| 235 |
+
isEditing ? 'pointer-events-auto' : 'pointer-events-none'
|
| 236 |
+
}`}
|
| 237 |
style={{
|
| 238 |
fontSize: `${fontSize}px`,
|
| 239 |
color: color,
|
| 240 |
textAlign: data.textAlign || 'left',
|
| 241 |
+
cursor: isEditing ? 'text' : 'grab'
|
| 242 |
}}
|
| 243 |
onKeyDown={(e) => e.stopPropagation()}
|
| 244 |
onMouseDown={(e) => {
|
| 245 |
+
if (isEditing) e.stopPropagation();
|
| 246 |
}}
|
| 247 |
/>
|
| 248 |
|