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 files12345678910111213146 unmodified lines21222324252627282930313233343536373 unmodified lines4142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import React, { useState, useEffect, useRef } from 'react';import { NodeResizer, Handle, Position, useUpdateNodeInternals } from '@xyflow/react';import { useTranslation } from 'react-i18next';import { useTheme } from '../../../../context/ThemeContext';const TextNode = ({ id, data, selected }) => {const { t } = useTranslation();const { isDarkMode } = useTheme();const updateNodeInternals = useUpdateNodeInternals();const [localText, setLocalText] = useState(data.label || '');const [localWidth, setLocalWidth] = useState(data.width || 250);const textareaRef = useRef(null);// Auto-resize textarea height6 unmodified lines// Notify React Flow that height (and handles) have shiftedupdateNodeInternals(id);}}, [localText, localWidth, id, updateNodeInternals]);useEffect(() => {if (document.activeElement !== textareaRef.current) {setLocalText(data.label || '');}// Only update local width from data if we aren't currently dragging itif (!selected) {setLocalWidth(data.width || 250);}}, [data.label, data.width, selected]);const handleTextChange = (e) => {const newText = e.target.value;3 unmodified lines}};const fontSize = data.fontSize || 18;const color = data.color || (isDarkMode ? '#ffffff' : '#31363F');return (<divclassName={`relative group p-1 transition-all ${selected ? 'ring-2 ring-primary/20 rounded-md' : ''}`}style={{width: localWidth,minWidth: 50,}}><NodeResizerisVisible={selected}minWidth={50}onResize={(e, params) => {setLocalWidth(params.width);}}onResizeEnd={(e, params) => {if (data.updateNodeData) {data.updateNodeData(id, { width: params.width });}}}/><textarearef={textareaRef}value={localText}onChange={handleTextChange}placeholder={t('canvas.text_placeholder', 'Type something...')}className="w-full bg-transparent border-none outline-none resize-none overflow-hidden font-accent-note leading-tight p-0"style={{fontSize: `${fontSize}px`,color: color,textAlign: data.textAlign || 'left',cursor: selected ? 'text' : 'default'}}onKeyDown={(e) => e.stopPropagation()}onMouseDown={(e) => {if (selected) e.stopPropagation();}}/>123456789101112131415166 unmodified lines23242526272829303132333435363738394041424344454647484950513 unmodified lines555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248import React, { useState, useEffect, useRef } from 'react';import { Handle, Position, useUpdateNodeInternals, useReactFlow } from '@xyflow/react';import { useTranslation } from 'react-i18next';import { useTheme } from '../../../../context/ThemeContext';const TextNode = ({ id, data, selected, width }) => {const { t } = useTranslation();const { isDarkMode } = useTheme();const updateNodeInternals = useUpdateNodeInternals();const { getZoom, getNode, setNodes } = useReactFlow();const [localText, setLocalText] = useState(data.label || '');const [isResizing, setIsResizing] = useState(false);const [isEditing, setIsEditing] = useState(false);const textareaRef = useRef(null);// Auto-resize textarea height6 unmodified lines// Notify React Flow that height (and handles) have shiftedupdateNodeInternals(id);}}, [localText, width, data.width, id, updateNodeInternals]);// Sync if data changes from remoteuseEffect(() => {if (document.activeElement !== textareaRef.current) {setLocalText(data.label || '');}}, [data.label]);// Sync node's draggable state with editing stateuseEffect(() => {setNodes((nds) =>nds.map((n) => {if (n.id === id) {return {...n,draggable: isEditing ? false : !data.isPinned};}return n;}));}, [isEditing, id, data.isPinned, setNodes]);const handleTextChange = (e) => {const newText = e.target.value;3 unmodified lines}};const handleDoubleClick = (e) => {e.stopPropagation();setIsEditing(true);setTimeout(() => {if (textareaRef.current) {textareaRef.current.focus();textareaRef.current.select();}}, 0);};const handleBlur = () => {setIsEditing(false);if (data.updateNodeData) {data.updateNodeData(id, { label: localText });}};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 || 250,height: node.height || node.style?.height || 100};const zoom = getZoom();let finalWidth = startSize.width;let finalX = startPos.x;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;// For TextNode, we only resize widthif (['right', 'top-right', 'bottom-right', 'top', 'bottom'].includes(direction)) {// Dragging right-side / vertical handles: expand width to the rightconst delta = ['top', 'bottom'].includes(direction) ? dy : dx;finalWidth = Math.max(50, startSize.width + delta);finalX = startPos.x;} else if (['left', 'top-left', 'bottom-left'].includes(direction)) {// Dragging left-side handles: expand width to the left, shifting position xfinalWidth = Math.max(50, startSize.width - dx);finalX = startPos.x + (startSize.width - finalWidth);}if (nodeElement) {nodeElement.style.width = `${finalWidth}px`;nodeElement.style.transform = `translate(${finalX}px, ${startPos.y}px)`;}};const handlePointerUp = () => {window.removeEventListener('pointermove', handlePointerMove);window.removeEventListener('pointerup', handlePointerUp);setNodes((nds) =>nds.map((n) => {if (n.id === id) {return {...n,position: { x: finalX, y: startPos.y },width: finalWidth,style: { ...n.style, width: finalWidth }};}return n;}));if (data.updateNodeData) {data.updateNodeData(id, {width: finalWidth,x: finalX,y: startPos.y});}setIsResizing(false);};window.addEventListener('pointermove', handlePointerMove);window.addEventListener('pointerup', handlePointerUp);};const isCustomSized = width !== undefined || isResizing;const styleWidth = isCustomSized ? '100%' : (data.width || 250);const fontSize = data.fontSize || 18;const color = data.color || (isDarkMode ? '#ffffff' : '#31363F');// Common handle style & class (Removed transition-all to prevent scale animation lag)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 (<divonDoubleClick={handleDoubleClick}className={`relative group p-1 ${selected ? 'ring-2 ring-primary/40 shadow-xl rounded-md' : ''}`}style={{width: styleWidth,height: 'auto',minWidth: 50,}}>{selected && !isEditing && (<>{/* Custom Dashed Outline */}<div className="absolute inset-0 border-2 border-dashed border-primary opacity-60 rounded-lg pointer-events-none z-10" />{/* Corners */}<divstyle={{ top: '-7px', left: '-7px' }}className={`${handleClass} cursor-nwse-resize`}onPointerDown={(e) => handleResizeStart(e, 'top-left')}/><divstyle={{ top: '-7px', right: '-7px' }}className={`${handleClass} cursor-nesw-resize`}onPointerDown={(e) => handleResizeStart(e, 'top-right')}/><divstyle={{ bottom: '-7px', left: '-7px' }}className={`${handleClass} cursor-nesw-resize`}onPointerDown={(e) => handleResizeStart(e, 'bottom-left')}/><divstyle={{ bottom: '-7px', right: '-7px' }}className={`${handleClass} cursor-nwse-resize`}onPointerDown={(e) => handleResizeStart(e, 'bottom-right')}/>{/* Sides */}<divstyle={{ top: '-7px', left: '50%', marginLeft: '-7px' }}className={`${handleClass} cursor-ns-resize`}onPointerDown={(e) => handleResizeStart(e, 'top')}/><divstyle={{ bottom: '-7px', left: '50%', marginLeft: '-7px' }}className={`${handleClass} cursor-ns-resize`}onPointerDown={(e) => handleResizeStart(e, 'bottom')}/><divstyle={{ left: '-7px', top: '50%', marginTop: '-7px' }}className={`${handleClass} cursor-ew-resize`}onPointerDown={(e) => handleResizeStart(e, 'left')}/><divstyle={{ right: '-7px', top: '50%', marginTop: '-7px' }}className={`${handleClass} cursor-ew-resize`}onPointerDown={(e) => handleResizeStart(e, 'right')}/></>)}<textarearef={textareaRef}value={localText}onChange={handleTextChange}onBlur={handleBlur}readOnly={!isEditing}placeholder={t('canvas.text_placeholder', 'Type something...')}className={`w-full bg-transparent border-none outline-none resize-none overflow-hidden font-accent-note leading-tight p-0 ${isEditing ? 'pointer-events-auto' : 'pointer-events-none'}`}style={{fontSize: `${fontSize}px`,color: color,textAlign: data.textAlign || 'left',cursor: isEditing ? 'text' : 'grab'}}onKeyDown={(e) => e.stopPropagation()}onMouseDown={(e) => {if (isEditing) e.stopPropagation();}}/>
src/features/canvas/components/nodes/TextNode.jsx+188-27