Spaces:
Running
Running
Commit ·
b4d6f7f
1
Parent(s): 14b17c5
Completely rewrite ImageNode scaling with pure pointer-event mathematical calculations and direct React Flow store updates
Browse filesFiles changed (2)
123451 unmodified line7891011121314151660 unmodified lines777879808182838485868788899091929394959697989910010110210310410511 unmodified lines1171181191201211221231241251261271281291301311321331341351361378 unmodified lines146147148149150151152153154155156157158159160161162163164165166167import React, { useState, useEffect, useRef } from 'react';import { NodeResizeControl, Handle, Position, useUpdateNodeInternals } from '@xyflow/react';import { Image as ImageIcon, Loader2 } from 'lucide-react';import { useTranslation } from 'react-i18next';import axios from 'axios';1 unmodified lineconst ImageNode = ({ id, data, selected, width, height }) => {const { t } = useTranslation();const updateNodeInternals = useUpdateNodeInternals();const [imageUrl, setImageUrl] = useState(data.url || '');const [loading, setLoading] = useState(false);const [isShiftPressed, setIsShiftPressed] = useState(false);const containerRef = useRef(null);const imageRatioRef = useRef(1);// Load large data from backend on mount if not loaded60 unmodified lines};}, []);// Intercept pointer down / mouse down events on the resizer container natively// to completely block React Flow from initiating a node drag when clicking/dragging handles.useEffect(() => {const stopPropagation = (e) => {e.stopPropagation();};const el = containerRef.current;if (el) {el.addEventListener('pointerdown', stopPropagation);el.addEventListener('mousedown', stopPropagation);el.addEventListener('touchstart', stopPropagation);}return () => {if (el) {el.removeEventListener('pointerdown', stopPropagation);el.removeEventListener('mousedown', stopPropagation);el.removeEventListener('touchstart', stopPropagation);}};}, [selected]); // Re-attach when container element is rendered/unmountedconst handleImageLoad = (e) => {const { naturalWidth, naturalHeight } = e.target;if (naturalWidth && naturalHeight) {11 unmodified lines}};const handleResizeEnd = (e, params) => {if (data.updateNodeData) {data.updateNodeData(id, { width: params.width, height: params.height });}};const currentWidth = width || data.width || 300;const currentHeight = height || data.height || 'auto';const controlProps = {minWidth: 50,minHeight: 50,onResizeEnd: handleResizeEnd,className: "nodrag !pointer-events-auto !w-3.5 !h-3.5 !bg-surface-container-lowest !border-2 !border-primary !rounded-md !shadow-[2px_2px_0px_0px_var(--color-primary)] hover:!scale-110 active:!scale-95 transition-transform"};return (<div8 unmodified lines}}>{selected && (<div ref={containerRef} className="absolute inset-0 pointer-events-none z-50">{/* Custom Dashed Outline */}<div className="absolute inset-0 border-2 border-dashed border-primary opacity-60 rounded-lg" />{/* Corners (Always Keep Aspect Ratio) */}<NodeResizeControl position="top-left" keepAspectRatio={true} {...controlProps} /><NodeResizeControl position="top-right" keepAspectRatio={true} {...controlProps} /><NodeResizeControl position="bottom-left" keepAspectRatio={true} {...controlProps} /><NodeResizeControl position="bottom-right" keepAspectRatio={true} {...controlProps} />{/* Sides (Keep Aspect Ratio ONLY if Shift is pressed) */}<NodeResizeControl position="top" keepAspectRatio={isShiftPressed} {...controlProps} /><NodeResizeControl position="bottom" keepAspectRatio={isShiftPressed} {...controlProps} /><NodeResizeControl position="left" keepAspectRatio={isShiftPressed} {...controlProps} /><NodeResizeControl position="right" keepAspectRatio={isShiftPressed} {...controlProps} /></div>)}{loading ? (123451 unmodified line7891011121314151660 unmodified lines77787980818211 unmodified lines9495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712728 unmodified lines281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334import 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';1 unmodified lineconst 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 imageRatioRef = useRef(1);// Load large data from backend on mount if not loaded60 unmodified lines};}, []);const handleImageLoad = (e) => {const { naturalWidth, naturalHeight } = e.target;if (naturalWidth && naturalHeight) {11 unmodified lines}};const handleResizeStart = (e, direction) => {e.stopPropagation();e.preventDefault();const node = getNode(id);if (!node) return;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();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;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 (<div8 unmodified lines}}>{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) */}<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 (Proportional only if Shift is held) */}<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')}/></>)}{loading ? (
483 unmodified lines48448548648748848999 unmodified lines589590591592593594595483 unmodified linesdelete coupledData.isLoadedFromBackend;const updated = { ...n, data: { ...n.data, ...coupledData } };if (coupledData.isPinned !== undefined) {updated.draggable = !coupledData.isPinned;}99 unmodified lines}case 'NODE_DATA': {const { id, newData } = message.payload;setNodes(nds => nds.map(n => n.id === id ? { ...n, data: { ...n.data, ...newData } } : n));break;}case 'EDGE_ADD': {483 unmodified lines48448548648748848949049149249349449549649749849950050150299 unmodified lines602603604605606607608609610611612613614615616617618619620621622623624625483 unmodified linesdelete coupledData.isLoadedFromBackend;const updated = { ...n, data: { ...n.data, ...coupledData } };if (coupledData.width !== undefined) {updated.width = coupledData.width;updated.style = { ...updated.style, width: coupledData.width };}if (coupledData.height !== undefined) {updated.height = coupledData.height;updated.style = { ...updated.style, height: coupledData.height };}if (coupledData.x !== undefined && coupledData.y !== undefined) {updated.position = { x: coupledData.x, y: coupledData.y };}if (coupledData.isPinned !== undefined) {updated.draggable = !coupledData.isPinned;}99 unmodified lines}case 'NODE_DATA': {const { id, newData } = message.payload;setNodes(nds => nds.map(n => {if (n.id === id) {const updated = { ...n, data: { ...n.data, ...newData } };if (newData.width !== undefined) {updated.width = newData.width;updated.style = { ...updated.style, width: newData.width };}if (newData.height !== undefined) {updated.height = newData.height;updated.style = { ...updated.style, height: newData.height };}if (newData.x !== undefined && newData.y !== undefined) {updated.position = { x: newData.x, y: newData.y };}return updated;}return n;}));break;}case 'EDGE_ADD': {
src/features/canvas/components/nodes/ImageNode.jsx+216-49
src/features/canvas/hooks/useCanvasLogic.js+31-1