fix(nodes): re-measure nodes when dimensions are missing

This commit is contained in:
moklick
2024-02-13 17:19:32 +01:00
parent 4237775b6e
commit 04ba6e0e4e
4 changed files with 38 additions and 33 deletions
@@ -27,9 +27,9 @@ const buttonStyle: CSSProperties = {
zIndex: 4, zIndex: 4,
}; };
const CustomMiniMapNode = ({ x, y, width, height, color }: MiniMapNodeProps) => ( const CustomMiniMapNode = ({ x, y, width, height }: MiniMapNodeProps) => {
<circle cx={x} cy={y} r={Math.max(width, height) / 2} fill={color} /> return <circle cx={x} cy={y} r={Math.max(width, height) / 2} fill="#ffcc00" />;
); };
const CustomMiniMapNodeFlow = () => { const CustomMiniMapNodeFlow = () => {
const [nodes, setNodes, onNodesChange] = useNodesState<Node>([]); const [nodes, setNodes, onNodesChange] = useNodesState<Node>([]);
+20 -20
View File
@@ -90,7 +90,7 @@ const initialNodes: Node[] = [
data: { label: 'Node 5' }, data: { label: 'Node 5' },
position: { x: 650, y: 250 }, position: { x: 650, y: 250 },
className: 'light', className: 'light',
style: { width: 400, height: 150 }, style: { width: 100, height: 100 },
zIndex: 1000, zIndex: 1000,
}, },
{ {
@@ -161,9 +161,12 @@ const Subflow = () => {
setNodes((nds) => { setNodes((nds) => {
return nds.map((n) => { return nds.map((n) => {
if (!n.parentNode) { if (!n.parentNode) {
n.position = { return {
x: Math.random() * 400, ...n,
y: Math.random() * 400, position: {
x: Math.random() * 400,
y: Math.random() * 400,
},
}; };
} }
@@ -178,8 +181,10 @@ const Subflow = () => {
const toggleClassnames = () => { const toggleClassnames = () => {
setNodes((nds) => { setNodes((nds) => {
return nds.map((n) => { return nds.map((n) => {
n.className = n.className === 'light' ? 'dark' : 'light'; return {
return n; ...n,
className: n.className === 'light' ? 'dark' : 'light',
};
}); });
}); });
}; };
@@ -187,8 +192,10 @@ const Subflow = () => {
const toggleChildNodes = () => { const toggleChildNodes = () => {
setNodes((nds) => { setNodes((nds) => {
return nds.map((n) => { return nds.map((n) => {
n.hidden = !!n.parentNode && !n.hidden; return {
return n; ...n,
hidden: !!n.parentNode && !n.hidden,
};
}); });
}); });
}; };
@@ -215,19 +222,12 @@ const Subflow = () => {
<Background /> <Background />
<Panel position="top-right"> <Panel position="top-right">
<button onClick={resetTransform} style={{ marginRight: 5 }}> <button onClick={resetTransform}>reset transform</button>
reset transform <button onClick={updatePos}>change pos</button>
</button> <button onClick={toggleClassnames}>toggle classnames</button>
<button onClick={updatePos} style={{ marginRight: 5 }}> <button onClick={toggleChildNodes}>toggleChildNodes</button>
change pos
</button>
<button onClick={toggleClassnames} style={{ marginRight: 5 }}>
toggle classnames
</button>
<button style={{ marginRight: 5 }} onClick={toggleChildNodes}>
toggleChildNodes
</button>
<button onClick={logToObject}>toObject</button> <button onClick={logToObject}>toObject</button>
<button onClick={() => setNodes(initialNodes)}>setNodes</button>
</Panel> </Panel>
</ReactFlow> </ReactFlow>
); );
@@ -79,16 +79,27 @@ export function NodeWrapper({
const prevTargetPosition = useRef(node.targetPosition); const prevTargetPosition = useRef(node.targetPosition);
const prevType = useRef(nodeType); const prevType = useRef(nodeType);
const width = node.width ?? undefined;
const height = node.height ?? undefined;
const computedWidth = node.computed?.width;
const computedHeight = node.computed?.height;
const initialized = (!!computedWidth && !!computedHeight) || (!!width && !!height);
const moveSelectedNodes = useMoveSelectedNodes(); const moveSelectedNodes = useMoveSelectedNodes();
useEffect(() => { useEffect(() => {
if (nodeRef.current && !node.hidden) { if (nodeRef.current && !node.hidden) {
const currNode = nodeRef.current; const currNode = nodeRef.current;
resizeObserver?.observe(currNode);
if (!initialized) {
resizeObserver?.observe(currNode);
} else {
resizeObserver?.unobserve(currNode);
}
return () => resizeObserver?.unobserve(currNode); return () => resizeObserver?.unobserve(currNode);
} }
}, [node.hidden]); }, [node.hidden, initialized]);
useEffect(() => { useEffect(() => {
// when the user programmatically changes the source or handle position, we re-initialize the node // when the user programmatically changes the source or handle position, we re-initialize the node
@@ -123,11 +134,6 @@ export function NodeWrapper({
return null; return null;
} }
const width = node.width ?? undefined;
const height = node.height ?? undefined;
const computedWidth = node.computed?.width;
const computedHeight = node.computed?.height;
const positionAbsoluteOrigin = getPositionWithOrigin({ const positionAbsoluteOrigin = getPositionWithOrigin({
x: positionAbsoluteX, x: positionAbsoluteX,
y: positionAbsoluteY, y: positionAbsoluteY,
@@ -135,7 +141,6 @@ export function NodeWrapper({
height: computedHeight ?? height ?? 0, height: computedHeight ?? height ?? 0,
origin: node.origin || nodeOrigin, origin: node.origin || nodeOrigin,
}); });
const initialized = (!!computedWidth && !!computedHeight) || (!!width && !!height);
const hasPointerEvents = isSelectable || isDraggable || onClick || onMouseEnter || onMouseMove || onMouseLeave; const hasPointerEvents = isSelectable || isDraggable || onClick || onMouseEnter || onMouseMove || onMouseLeave;
const onMouseEnterHandler = onMouseEnter ? (event: MouseEvent) => onMouseEnter(event, { ...node }) : undefined; const onMouseEnterHandler = onMouseEnter ? (event: MouseEvent) => onMouseEnter(event, { ...node }) : undefined;
+2 -2
View File
@@ -94,8 +94,8 @@ export function adoptUserProvidedNodes<NodeType extends NodeBase>(
...n, ...n,
computed: { computed: {
positionAbsolute: n.position, positionAbsolute: n.position,
width: n.computed?.width || currentStoreNode?.computed?.width, width: n.computed?.width,
height: n.computed?.height || currentStoreNode?.computed?.height, height: n.computed?.height,
}, },
}; };
const z = (isNumeric(n.zIndex) ? n.zIndex : 0) + (n.selected ? selectedNodeZ : 0); const z = (isNumeric(n.zIndex) ? n.zIndex : 0) + (n.selected ? selectedNodeZ : 0);