feat(stacking): simplify zIndex and treeLevel behaviour

This commit is contained in:
Christopher Möller
2021-11-04 12:42:14 +01:00
parent 79e46d90ad
commit 1b390e103d
6 changed files with 24 additions and 27 deletions
+5 -7
View File
@@ -28,7 +28,7 @@ const initialNodes: Node[] = [
data: { label: 'Node 4' }, data: { label: 'Node 4' },
position: { x: 100, y: 200 }, position: { x: 100, y: 200 },
className: 'light', className: 'light',
style: { backgroundColor: 'rgba(255, 0, 0, 1)' }, style: { backgroundColor: 'rgba(255, 0, 0, 0.8)' },
width: 600, width: 600,
height: 300, height: 300,
}, },
@@ -42,12 +42,12 @@ const initialNodes: Node[] = [
{ {
id: '4b', id: '4b',
data: { label: 'Node 4b' }, data: { label: 'Node 4b' },
position: { x: 80, y: 80 }, position: { x: 150, y: 50 },
className: 'light', className: 'light',
style: { backgroundColor: 'rgba(255, 255, 0, 0.2)' }, style: { backgroundColor: 'rgba(255, 0, 255, 0.8)' },
height: 300,
width: 300,
parentNode: '4', parentNode: '4',
height: 200,
width: 350,
}, },
{ {
id: '4b1', id: '4b1',
@@ -68,10 +68,8 @@ const initialNodes: Node[] = [
const initialEdges: Edge[] = [ const initialEdges: Edge[] = [
{ id: 'e1-2', source: '1', target: '2', animated: true }, { id: 'e1-2', source: '1', target: '2', animated: true },
{ id: 'e1-3', source: '1', target: '3' }, { id: 'e1-3', source: '1', target: '3' },
{ id: 'e2-4a', source: '2', target: '4a', animated: true },
{ id: 'e3-4', source: '3', target: '4' }, { id: 'e3-4', source: '3', target: '4' },
{ id: 'e3-4b', source: '3', target: '4b' }, { id: 'e3-4b', source: '3', target: '4b' },
{ id: 'e3-4b2', source: '3', target: '4b2' },
{ id: 'e4a-4b1', source: '4a', target: '4b1' }, { id: 'e4a-4b1', source: '4a', target: '4b1' },
{ id: 'e4a-4b2', source: '4a', target: '4b2' }, { id: 'e4a-4b2', source: '4a', target: '4b2' },
{ id: 'e4b1-4b2', source: '4b1', target: '4b2' }, { id: 'e4b1-4b2', source: '4b1', target: '4b2' },
+9 -11
View File
@@ -1,19 +1,19 @@
import React, { useEffect, useState, CSSProperties } from 'react'; import React, { useEffect, useState, CSSProperties } from 'react';
import { useStore } from '../../store';
import { getBezierPath } from '../Edges/BezierEdge'; import { getBezierPath } from '../Edges/BezierEdge';
import { getSmoothStepPath } from '../Edges/SmoothStepEdge'; import { getSmoothStepPath } from '../Edges/SmoothStepEdge';
import { import {
ElementId, ElementId,
Node, NodeLookupItem,
Transform, Transform,
HandleElement, HandleElement,
Position, Position,
ConnectionLineType, ConnectionLineType,
ConnectionLineComponent, ConnectionLineComponent,
HandleType, HandleType,
ReactFlowState, Node,
} from '../../types'; } from '../../types';
import useNodeLookup from '../../hooks/useNodeLookup';
interface ConnectionLineProps { interface ConnectionLineProps {
connectionNodeId: ElementId; connectionNodeId: ElementId;
connectionHandleId: ElementId | null; connectionHandleId: ElementId | null;
@@ -27,8 +27,6 @@ interface ConnectionLineProps {
CustomConnectionLineComponent?: ConnectionLineComponent; CustomConnectionLineComponent?: ConnectionLineComponent;
} }
const nodesSelector = (s: ReactFlowState) => s.nodes;
export default ({ export default ({
connectionNodeId, connectionNodeId,
connectionHandleId, connectionHandleId,
@@ -41,13 +39,13 @@ export default ({
isConnectable, isConnectable,
CustomConnectionLineComponent, CustomConnectionLineComponent,
}: ConnectionLineProps) => { }: ConnectionLineProps) => {
const nodes = useStore(nodesSelector); const nodeLookup = useNodeLookup();
const [sourceNode, setSourceNode] = useState<Node | null>(null); const [sourceNode, setSourceNode] = useState<NodeLookupItem | null>(null);
const nodeId = connectionNodeId; const nodeId = connectionNodeId;
const handleId = connectionHandleId; const handleId = connectionHandleId;
useEffect(() => { useEffect(() => {
const nextSourceNode = nodes.find((n) => n.id === nodeId) || null; const nextSourceNode = nodeLookup.get(nodeId) || null;
setSourceNode(nextSourceNode); setSourceNode(nextSourceNode);
}, []); }, []);
@@ -60,8 +58,8 @@ export default ({
: sourceNode.handleBounds[connectionHandleType]![0]; : sourceNode.handleBounds[connectionHandleType]![0];
const sourceHandleX = sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : sourceNode.width! / 2; const sourceHandleX = sourceHandle ? sourceHandle.x + sourceHandle.width / 2 : sourceNode.width! / 2;
const sourceHandleY = sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.height!; const sourceHandleY = sourceHandle ? sourceHandle.y + sourceHandle.height / 2 : sourceNode.height!;
const sourceX = sourceNode.position.x + sourceHandleX; const sourceX = sourceNode.position!.x + sourceHandleX;
const sourceY = sourceNode.position.y + sourceHandleY; const sourceY = sourceNode.position!.y + sourceHandleY;
const targetX = (connectionPositionX - transform[0]) / transform[2]; const targetX = (connectionPositionX - transform[0]) / transform[2];
const targetY = (connectionPositionY - transform[1]) / transform[2]; const targetY = (connectionPositionY - transform[1]) / transform[2];
@@ -81,7 +79,7 @@ export default ({
targetPosition={targetPosition} targetPosition={targetPosition}
connectionLineType={connectionLineType} connectionLineType={connectionLineType}
connectionLineStyle={connectionLineStyle} connectionLineStyle={connectionLineStyle}
sourceNode={sourceNode} sourceNode={sourceNode as Node}
sourceHandle={sourceHandle} sourceHandle={sourceHandle}
/> />
</g> </g>
+1 -1
View File
@@ -247,7 +247,7 @@ const EdgeRenderer = (props: EdgeRendererProps) => {
return ( return (
<> <>
{edgeTree.map(({ level, edges, isMaxLevel }) => ( {edgeTree.map(({ level, edges, isMaxLevel }) => (
<svg key={level} style={{ zIndex: 6 + level }} width={width} height={height} className="react-flow__edges"> <svg key={level} style={{ zIndex: level }} width={width} height={height} className="react-flow__edges">
{isMaxLevel && <MarkerDefinitions defaultColor={defaultMarkerColor} />} {isMaxLevel && <MarkerDefinitions defaultColor={defaultMarkerColor} />}
<g> <g>
{edges.map((edge: Edge) => { {edges.map((edge: Edge) => {
+1 -1
View File
@@ -116,7 +116,7 @@ function Node({
isConnectable={isConnectable} isConnectable={isConnectable}
resizeObserver={resizeObserver} resizeObserver={resizeObserver}
dragHandle={node.dragHandle} dragHandle={node.dragHandle}
zIndex={6 + treeLevel} zIndex={treeLevel}
isParentNode={isParentNode} isParentNode={isParentNode}
/> />
); );
+7 -5
View File
@@ -136,7 +136,7 @@ const createStore = () =>
height: node.height || null, height: node.height || null,
position: node.position, position: node.position,
positionAbsolute: node.position, positionAbsolute: node.position,
treeLevel: 0, treeLevel: node.zIndex || 0,
}; };
if (node.parentNode) { if (node.parentNode) {
lookupNode.parentNode = node.parentNode; lookupNode.parentNode = node.parentNode;
@@ -149,19 +149,21 @@ const createStore = () =>
.forEach((node) => { .forEach((node) => {
const positionAbsoluteAndTreeLevel = getAbsolutePositionAndTreeLevel(node, nodeLookup, { const positionAbsoluteAndTreeLevel = getAbsolutePositionAndTreeLevel(node, nodeLookup, {
...node.position, ...node.position,
treeLevel: 0, treeLevel: node.zIndex || 0,
}); });
nodeLookup.set(node.parentNode!, { ...nodeLookup.get(node.parentNode!), isParentNode: true }); nodeLookup.set(node.parentNode!, { ...nodeLookup.get(node.parentNode!), isParentNode: true });
if (positionAbsoluteAndTreeLevel) { if (positionAbsoluteAndTreeLevel) {
const { treeLevel, x, y } = positionAbsoluteAndTreeLevel;
nodeLookup.set(node.id, { nodeLookup.set(node.id, {
...nodeLookup.get(node.id), ...nodeLookup.get(node.id),
positionAbsolute: { positionAbsolute: {
x: positionAbsoluteAndTreeLevel.x, x,
y: positionAbsoluteAndTreeLevel.y, y,
}, },
treeLevel: positionAbsoluteAndTreeLevel.treeLevel, treeLevel,
}); });
} }
}); });
+1 -2
View File
@@ -82,9 +82,8 @@ export interface Node<T = any> {
dragHandle?: string; dragHandle?: string;
width?: number | null; width?: number | null;
height?: number | null; height?: number | null;
handleBounds?: NodeHandleBounds;
parentNode?: ElementId; parentNode?: ElementId;
childNodes?: Node[]; zIndex?: number;
} }
export enum ArrowHeadType { export enum ArrowHeadType {