Spaces:
Running
Running
Commit ·
f2546a6
1
Parent(s): b4d6f7f
Optimize ImageNode resizing: use direct DOM updates and set node size to 100% when selected/resizing to make it responsive on all 8 handles without lag
Browse files10 unmodified lines11121314151684 unmodified lines1011021031041051065 unmodified lines1121131141151161172 unmodified lines12012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028110 unmodified linesconst [imageUrl, setImageUrl] = useState(data.url || '');const [loading, setLoading] = useState(false);const [isShiftPressed, setIsShiftPressed] = useState(false);const imageRatioRef = useRef(1);// Load large data from backend on mount if not loaded84 unmodified linesconst node = getNode(id);if (!node) return;const startX = e.clientX;const startY = e.clientY;const startPos = { ...node.position };5 unmodified linesconst ratio = imageRatioRef.current || (startSize.width / startSize.height) || 1;const zoom = getZoom();const handlePointerMove = (moveEvent) => {const dx = (moveEvent.clientX - startX) / zoom;const dy = (moveEvent.clientY - startY) / zoom;2 unmodified linesconst isCorner = ['top-left', 'top-right', 'bottom-left', 'bottom-right'].includes(direction);const shouldLockRatio = isCorner || shiftPressed;let newWidth = startSize.width;let newHeight = startSize.height;let newX = startPos.x;let newY = startPos.y;if (shouldLockRatio) {// --- PROPORTIONAL RESIZING ---if (direction === 'right') {newWidth = Math.max(50, startSize.width + dx);newHeight = Math.round(newWidth / ratio);newY = startPos.y;} else if (direction === 'left') {newWidth = Math.max(50, startSize.width - dx);newHeight = Math.round(newWidth / ratio);newX = startPos.x + (startSize.width - newWidth);newY = startPos.y;} else if (direction === 'bottom') {newHeight = Math.max(50, startSize.height + dy);newWidth = Math.round(newHeight * ratio);newX = startPos.x;} else if (direction === 'top') {newHeight = Math.max(50, startSize.height - dy);newWidth = Math.round(newHeight * ratio);newY = startPos.y + (startSize.height - newHeight);newX = startPos.x;} else if (direction === 'bottom-right') {if (Math.abs(dx) > Math.abs(dy)) {newWidth = Math.max(50, startSize.width + dx);newHeight = Math.round(newWidth / ratio);} else {newHeight = Math.max(50, startSize.height + dy);newWidth = Math.round(newHeight * ratio);}newX = startPos.x;newY = startPos.y;} else if (direction === 'bottom-left') {if (Math.abs(dx) > Math.abs(dy)) {newWidth = Math.max(50, startSize.width - dx);newHeight = Math.round(newWidth / ratio);} else {newHeight = Math.max(50, startSize.height + dy);newWidth = Math.round(newHeight * ratio);}newX = startPos.x + (startSize.width - newWidth);newY = startPos.y;} else if (direction === 'top-right') {if (Math.abs(dx) > Math.abs(dy)) {newWidth = Math.max(50, startSize.width + dx);newHeight = Math.round(newWidth / ratio);} else {newHeight = Math.max(50, startSize.height - dy);newWidth = Math.round(newHeight * ratio);}newX = startPos.x;newY = startPos.y + (startSize.height - newHeight);} else if (direction === 'top-left') {if (Math.abs(dx) > Math.abs(dy)) {newWidth = Math.max(50, startSize.width - dx);newHeight = Math.round(newWidth / ratio);} else {newHeight = Math.max(50, startSize.height - dy);newWidth = Math.round(newHeight * ratio);}newX = startPos.x + (startSize.width - newWidth);newY = startPos.y + (startSize.height - newHeight);}} else {// --- DISTORTION RESIZING ---if (direction === 'right') {newWidth = Math.max(50, startSize.width + dx);} else if (direction === 'left') {newWidth = Math.max(50, startSize.width - dx);newX = startPos.x + (startSize.width - newWidth);} else if (direction === 'bottom') {newHeight = Math.max(50, startSize.height + dy);} else if (direction === 'top') {newHeight = Math.max(50, startSize.height - dy);newY = startPos.y + (startSize.height - newHeight);} else if (direction === 'bottom-right') {newWidth = Math.max(50, startSize.width + dx);newHeight = Math.max(50, startSize.height + dy);} else if (direction === 'bottom-left') {newWidth = Math.max(50, startSize.width - dx);newHeight = Math.max(50, startSize.height + dy);newX = startPos.x + (startSize.width - newWidth);} else if (direction === 'top-right') {newWidth = Math.max(50, startSize.width + dx);newHeight = Math.max(50, startSize.height - dy);newY = startPos.y + (startSize.height - newHeight);} else if (direction === 'top-left') {newWidth = Math.max(50, startSize.width - dx);newHeight = Math.max(50, startSize.height - dy);newX = startPos.x + (startSize.width - newWidth);newY = startPos.y + (startSize.height - newHeight);}}// Update the node size and position in React Flow's store in real-timesetNodes((nds) =>nds.map((n) => {if (n.id === id) {return {...n,position: { x: newX, y: newY },width: newWidth,height: newHeight,style: { ...n.style, width: newWidth, height: newHeight }};}return n;}));};const handlePointerUp = () => {window.removeEventListener('pointermove', handlePointerMove);window.removeEventListener('pointerup', handlePointerUp);// Get the final node bounds from the store to save themconst latestNode = getNode(id);if (latestNode) {const finalWidth = latestNode.width || latestNode.style?.width || 300;const finalHeight = latestNode.height || latestNode.style?.height || 200;const finalX = latestNode.position.x;const finalY = latestNode.position.y;// Save new dimensions and position to database, history and broadcastif (data.updateNodeData) {data.updateNodeData(id, {width: finalWidth,height: finalHeight,x: finalX,y: finalY});}}};window.addEventListener('pointermove', handlePointerMove);window.addEventListener('pointerup', handlePointerUp);};const currentWidth = width || data.width || 300;const currentHeight = height || data.height || 'auto';// Common handle style & classconst 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 (<divclassName={`relative group transition-all ${selected ? 'ring-2 ring-primary/40 shadow-xl' : ''}`}style={{width: currentWidth,height: currentHeight,minWidth: 50,minHeight: 50,}}10 unmodified lines1112131415161784 unmodified lines1021031041051061071081095 unmodified lines1151161171181191201211221231241251261272 unmodified lines13013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628710 unmodified linesconst [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 loaded84 unmodified linesconst node = getNode(id);if (!node) return;setIsResizing(true);const startX = e.clientX;const startY = e.clientY;const startPos = { ...node.position };5 unmodified linesconst 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;2 unmodified linesconst 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 draggingif (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 storesetNodes((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 broadcastif (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 (<divclassName={`relative group ${selected ? 'ring-2 ring-primary/40 shadow-xl' : ''}`}style={{width: styleWidth,height: styleHeight,minWidth: 50,minHeight: 50,}}
src/features/canvas/components/nodes/ImageNode.jsx+100-94