From 450cb90e67b752ec2d9c94dea3e087b130437a5d Mon Sep 17 00:00:00 2001 From: moklick Date: Wed, 20 Oct 2021 19:26:22 +0200 Subject: [PATCH] refactor(nested-nodes): zIndex --- src/components/Nodes/wrapNode.tsx | 3 +- src/container/NodeRenderer/index.tsx | 206 ++++++++++++++------------- src/types/index.ts | 1 + 3 files changed, 107 insertions(+), 103 deletions(-) diff --git a/src/components/Nodes/wrapNode.tsx b/src/components/Nodes/wrapNode.tsx index 6da0f95c..a0b30cd9 100644 --- a/src/components/Nodes/wrapNode.tsx +++ b/src/components/Nodes/wrapNode.tsx @@ -48,6 +48,7 @@ export default (NodeComponent: ComponentType) => { isDragging, resizeObserver, dragHandle, + zIndex, }: WrapNodeProps) => { const { addSelectedElements, @@ -63,7 +64,7 @@ export default (NodeComponent: ComponentType) => { const nodeStyle: CSSProperties = useMemo( () => ({ - zIndex: isSelected ? 10 : 3, + zIndex: isSelected ? zIndex + 1 : zIndex, transform: `translate(${xPos}px,${yPos}px)`, pointerEvents: isSelectable || isDraggable || onClick || onMouseEnter || onMouseMove || onMouseLeave ? 'all' : 'none', diff --git a/src/container/NodeRenderer/index.tsx b/src/container/NodeRenderer/index.tsx index f362f63a..16e3e037 100644 --- a/src/container/NodeRenderer/index.tsx +++ b/src/container/NodeRenderer/index.tsx @@ -39,111 +39,112 @@ interface NodesProps extends NodeRendererProps { nodesDraggable: boolean; nodesConnectable: boolean; elementsSelectable: boolean; + recursionDepth: number; } -const Nodes = memo( - ({ - nodes = [], - isDraggable, - resizeObserver, - scale, - snapToGrid, - snapGrid, - nodesDraggable, - nodesConnectable, - elementsSelectable, - ...props - }: NodesProps) => { +function Nodes({ + nodes = [], + isDraggable, + resizeObserver, + scale, + snapToGrid, + snapGrid, + nodesDraggable, + nodesConnectable, + elementsSelectable, + recursionDepth, + ...props +}: NodesProps): any { + return nodes.map((node) => { + const nodeType = node.type || 'default'; + + if (!props.nodeTypes[nodeType]) { + console.warn(`Node type "${nodeType}" not found. Using fallback type "default".`); + } + + const NodeComponent = (props.nodeTypes[nodeType] || props.nodeTypes.default) as ComponentType; + const isNodeDraggable = + typeof isDraggable !== 'undefined' + ? isDraggable + : !!(node.draggable || (nodesDraggable && typeof node.draggable === 'undefined')); + const isSelectable = !!(node.selectable || (elementsSelectable && typeof node.selectable === 'undefined')); + const isConnectable = !!(node.connectable || (nodesConnectable && typeof node.connectable === 'undefined')); + const isInitialized = + node.width !== null && + node.height !== null && + typeof node.width !== 'undefined' && + typeof node.height !== 'undefined'; + let childRect; + + if (node.childNodes) { + childRect = getRectOfNodes(node.childNodes); + node.position = node.isDragging + ? node.position + : { x: Math.round(childRect.x) - 10, y: Math.round(childRect.y) - 10 }; + node.style = { + ...node.style, + width: Math.round(childRect.width) + 20, + height: Math.round(childRect.height) + 20, + boxSizing: 'border-box', + }; + } + return ( - <> - {nodes.map((node) => { - const nodeType = node.type || 'default'; - - if (!props.nodeTypes[nodeType]) { - console.warn(`Node type "${nodeType}" not found. Using fallback type "default".`); - } - - const NodeComponent = (props.nodeTypes[nodeType] || props.nodeTypes.default) as ComponentType; - const isNodeDraggable = - typeof isDraggable !== 'undefined' - ? isDraggable - : !!(node.draggable || (nodesDraggable && typeof node.draggable === 'undefined')); - const isSelectable = !!(node.selectable || (elementsSelectable && typeof node.selectable === 'undefined')); - const isConnectable = !!(node.connectable || (nodesConnectable && typeof node.connectable === 'undefined')); - const isInitialized = - node.width !== null && - node.height !== null && - typeof node.width !== 'undefined' && - typeof node.height !== 'undefined'; - - if (node.childNodes) { - const childRect = getRectOfNodes(node.childNodes); - node.position = node.isDragging - ? node.position - : { x: Math.round(childRect.x) - 10, y: Math.round(childRect.y) - 10 }; - node.style = { - ...node.style, - width: Math.round(childRect.width) + 20, - height: Math.round(childRect.height) + 20, - boxSizing: 'border-box', - }; - } - - return ( - - - {node.childNodes && ( - - )} - - ); - })} - + + + {node.childNodes && ( + + )} + ); - } -); + }); +} + +const MemoizedNodes = memo(Nodes); const NodeRenderer = (props: NodeRendererProps) => { const { @@ -191,7 +192,7 @@ const NodeRenderer = (props: NodeRendererProps) => { return (
- { resizeObserver={resizeObserver} elementsSelectable={elementsSelectable} scale={transform[2]} + recursionDepth={0} {...props} />
diff --git a/src/types/index.ts b/src/types/index.ts index 817657dd..b3133c0a 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -300,6 +300,7 @@ export interface WrapNodeProps { isDragging?: boolean; resizeObserver: ResizeObserver | null; dragHandle?: string; + zIndex: number; } export type FitViewParams = {