Spaces:
Running
Running
Commit ·
14b17c5
1
Parent(s): 964ca5a
Fix Shift selection marquee and ImageNode resize translation issues
Browse filesFiles changed (2)
123456789101112131415161718192021 unmodified lines424344454647484950515253545556575859606162636465666768695 unmodified lines757677787980811 unmodified line83848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521532 unmodified lines156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193import React, { useState, useEffect, useRef } from 'react';import { NodeResizer, Handle, Position, useUpdateNodeInternals } from '@xyflow/react';import { Image as ImageIcon, Loader2 } from 'lucide-react';import { useTranslation } from 'react-i18next';import axios from 'axios';const ImageNode = ({ id, data, selected }) => {const { t } = useTranslation();const updateNodeInternals = useUpdateNodeInternals();const [imageUrl, setImageUrl] = useState(data.url || '');const [loading, setLoading] = useState(false);// Local state for smooth resizing without lagconst [localSize, setLocalSize] = useState({width: data.width || 300,height: data.height || 'auto'});// Load large data from backend on mount if not loadeduseEffect(() => {21 unmodified lines}}, [data.url]);// Re-calculate handles when data dimensions update (instead of on every pixel movement to eliminate lag)useEffect(() => {updateNodeInternals(id);}, [id, data.width, data.height, updateNodeInternals]);const activeDirectionRef = useRef('');const imageRatioRef = useRef(1);const handleResizeStart = (e) => {const classList = e.target.classList;let direction = '';if (classList.contains('top-left')) direction = 'top-left';else if (classList.contains('top-right')) direction = 'top-right';else if (classList.contains('bottom-left')) direction = 'bottom-left';else if (classList.contains('bottom-right')) direction = 'bottom-right';else if (classList.contains('top')) direction = 'top';else if (classList.contains('bottom')) direction = 'bottom';else if (classList.contains('left')) direction = 'left';else if (classList.contains('right')) direction = 'right';activeDirectionRef.current = direction;};const handleImageLoad = (e) => {const { naturalWidth, naturalHeight } = e.target;5 unmodified linesconst initialWidth = data.width || 300;const calculatedHeight = Math.round(initialWidth / ratio);setLocalSize({ width: initialWidth, height: calculatedHeight });if (data.updateNodeData) {data.updateNodeData(id, { width: initialWidth, height: calculatedHeight });}1 unmodified line}};// Sync aspect ratio ref if size is changed externallyuseEffect(() => {if (data.width && data.height && data.height !== 'auto') {imageRatioRef.current = data.width / data.height;}}, [data.width, data.height]);useEffect(() => {if (!selected) {setLocalSize({width: data.width || 300,height: data.height || 'auto'});}}, [data.width, data.height, selected]);const handleResize = (e, params) => {const shiftPressed = e.sourceEvent && e.sourceEvent.shiftKey;const direction = activeDirectionRef.current;// Shift locks aspect ratio for side handles, otherwise we stretchif (shiftPressed && imageRatioRef.current) {const ratio = imageRatioRef.current;let newWidth = params.width;let newHeight = params.height;if (direction === 'left' || direction === 'right') {newHeight = Math.round(newWidth / ratio);} else if (direction === 'top' || direction === 'bottom') {newWidth = Math.round(newHeight * ratio);} else {newHeight = Math.round(newWidth / ratio);}setLocalSize({ width: newWidth, height: newHeight });} else {setLocalSize({ width: params.width, height: params.height });}};const handleResizeEnd = (e, params) => {const shiftPressed = e.sourceEvent && e.sourceEvent.shiftKey;const direction = activeDirectionRef.current;if (shiftPressed && imageRatioRef.current) {const ratio = imageRatioRef.current;let newWidth = params.width;let newHeight = params.height;if (direction === 'left' || direction === 'right') {newHeight = Math.round(newWidth / ratio);} else if (direction === 'top' || direction === 'bottom') {newWidth = Math.round(newHeight * ratio);} else {newHeight = Math.round(newWidth / ratio);}if (data.updateNodeData) {data.updateNodeData(id, { width: newWidth, height: newHeight });}} else {if (data.updateNodeData) {data.updateNodeData(id, { width: params.width, height: params.height });}}};return (2 unmodified linesselected ? 'ring-2 ring-primary/40 shadow-xl' : ''}`}style={{width: localSize.width,height: localSize.height,minWidth: 50,minHeight: 50,}}>{/* Corner Resizer: keepAspectRatio = true */}<NodeResizerisVisible={selected}minWidth={50}minHeight={50}keepAspectRatio={true}directions={['top-left', 'top-right', 'bottom-left', 'bottom-right']}onResizeStart={handleResizeStart}lineClassName="!border-primary !border-2 !border-dashed opacity-60"handleClassName="!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"onResize={handleResize}onResizeEnd={handleResizeEnd}/>{/* Side Resizer: keepAspectRatio = false */}<NodeResizerisVisible={selected}minWidth={50}minHeight={50}keepAspectRatio={false}directions={['top', 'right', 'bottom', 'left']}onResizeStart={handleResizeStart}lineClassName="!border-primary !border-2 !border-dashed opacity-60"handleClassName="!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"onResize={handleResize}onResizeEnd={handleResizeEnd}/>{loading ? (<div className="w-full h-full min-h-[100px] bg-secondary/5 flex flex-col items-center justify-center gap-2 p-4 text-center border-2 border-dashed border-primary/10">123456789101112131415161721 unmodified lines394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031045 unmodified lines1101111121131141151 unmodified line1171181191201211221231241251261271281291301311321331341351362 unmodified lines139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168import 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';const 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 loadeduseEffect(() => {21 unmodified lines}}, [data.url]);// Re-calculate handles when data dimensions updateuseEffect(() => {updateNodeInternals(id);}, [id, data.width, data.height, updateNodeInternals]);// Sync aspect ratio ref if size is changeduseEffect(() => {if (data.width && data.height && data.height !== 'auto') {imageRatioRef.current = data.width / data.height;}}, [data.width, data.height]);// Listen to Shift key events globally to toggle aspect ratio constraintuseEffect(() => {const handleKeyDown = (e) => {if (e.key === 'Shift') {setIsShiftPressed(true);}};const handleKeyUp = (e) => {if (e.key === 'Shift') {setIsShiftPressed(false);}};const handleBlur = () => {setIsShiftPressed(false);};window.addEventListener('keydown', handleKeyDown);window.addEventListener('keyup', handleKeyUp);window.addEventListener('blur', handleBlur);return () => {window.removeEventListener('keydown', handleKeyDown);window.removeEventListener('keyup', handleKeyUp);window.removeEventListener('blur', handleBlur);};}, []);// 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;5 unmodified linesconst initialWidth = data.width || 300;const calculatedHeight = Math.round(initialWidth / ratio);if (data.updateNodeData) {data.updateNodeData(id, { width: initialWidth, height: calculatedHeight });}1 unmodified line}};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 (2 unmodified linesselected ? 'ring-2 ring-primary/40 shadow-xl' : ''}`}style={{width: currentWidth,height: currentHeight,minWidth: 50,minHeight: 50,}}>{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 ? (<div className="w-full h-full min-h-[100px] bg-secondary/5 flex flex-col items-center justify-center gap-2 p-4 text-center border-2 border-dashed border-primary/10">
2994 unmodified lines29952996299729982999300030012994 unmodified linesnodesConnectable={!drawingMode && permission !== 'Read'}panOnDrag={!drawingMode}selectionOnDrag={!drawingMode}selectionKeyCode={['Control', 'Meta']}nodeTypes={nodeTypes}edgeTypes={edgeTypes}connectionLineType={ConnectionLineType.SmoothStep}2994 unmodified lines29952996299729982999300030012994 unmodified linesnodesConnectable={!drawingMode && permission !== 'Read'}panOnDrag={!drawingMode}selectionOnDrag={!drawingMode}selectionKeyCode="Shift"nodeTypes={nodeTypes}edgeTypes={edgeTypes}connectionLineType={ConnectionLineType.SmoothStep}
src/features/canvas/components/nodes/ImageNode.jsx+91-116
src/pages/CanvasPage.jsx+1-1