refactor(inrernals): use one prop for all internals
This commit is contained in:
@@ -6,7 +6,7 @@ import { getBezierPath } from '../Edges/BezierEdge';
|
|||||||
import { getSmoothStepPath } from '../Edges/SmoothStepEdge';
|
import { getSmoothStepPath } from '../Edges/SmoothStepEdge';
|
||||||
import { ConnectionLineType, ConnectionLineComponent, HandleType, Node, ReactFlowState, Position } from '../../types';
|
import { ConnectionLineType, ConnectionLineComponent, HandleType, Node, ReactFlowState, Position } from '../../types';
|
||||||
import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge';
|
import { getSimpleBezierPath } from '../Edges/SimpleBezierEdge';
|
||||||
import { handleBoundsSymbol } from '../../utils';
|
import { internalsSymbol } from '../../utils';
|
||||||
|
|
||||||
interface ConnectionLineProps {
|
interface ConnectionLineProps {
|
||||||
connectionNodeId: string;
|
connectionNodeId: string;
|
||||||
@@ -38,7 +38,7 @@ export default ({
|
|||||||
|
|
||||||
const { nodeInternals, transform } = useStore(selector, shallow);
|
const { nodeInternals, transform } = useStore(selector, shallow);
|
||||||
const fromNode = useRef<Node | undefined>(nodeInternals.get(nodeId));
|
const fromNode = useRef<Node | undefined>(nodeInternals.get(nodeId));
|
||||||
const fromHandleBounds = fromNode.current?.[handleBoundsSymbol];
|
const fromHandleBounds = fromNode.current?.[internalsSymbol].handleBounds;
|
||||||
|
|
||||||
if (!fromNode.current || !isConnectable || !fromHandleBounds?.[connectionHandleType]) {
|
if (!fromNode.current || !isConnectable || !fromHandleBounds?.[connectionHandleType]) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ import {
|
|||||||
Transform,
|
Transform,
|
||||||
XYPosition,
|
XYPosition,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
import { handleBoundsSymbol, rectToBox } from '../../utils';
|
import { internalsSymbol, rectToBox } from '../../utils';
|
||||||
|
|
||||||
export type CreateEdgeTypes = (edgeTypes: EdgeTypes) => EdgeTypesWrapped;
|
export type CreateEdgeTypes = (edgeTypes: EdgeTypes) => EdgeTypesWrapped;
|
||||||
|
|
||||||
@@ -168,7 +168,7 @@ export function isEdgeVisible({
|
|||||||
|
|
||||||
export function getNodeData(nodeInternals: NodeInternals, nodeId: string): [Rect, NodeHandleBounds | null, boolean] {
|
export function getNodeData(nodeInternals: NodeInternals, nodeId: string): [Rect, NodeHandleBounds | null, boolean] {
|
||||||
const node = nodeInternals.get(nodeId);
|
const node = nodeInternals.get(nodeId);
|
||||||
const handleBounds = node?.[handleBoundsSymbol] || null;
|
const handleBounds = node?.[internalsSymbol].handleBounds || null;
|
||||||
|
|
||||||
const isInvalid =
|
const isInvalid =
|
||||||
!node ||
|
!node ||
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
ReactFlowState,
|
ReactFlowState,
|
||||||
WrapNodeProps,
|
WrapNodeProps,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
import { isParentSymbol, zSymbol } from '../../utils';
|
import { internalsSymbol } from '../../utils';
|
||||||
|
|
||||||
interface NodeRendererProps {
|
interface NodeRendererProps {
|
||||||
nodeTypes: NodeTypesWrapped;
|
nodeTypes: NodeTypesWrapped;
|
||||||
@@ -118,8 +118,8 @@ const NodeRenderer = (props: NodeRendererProps) => {
|
|||||||
isConnectable={isConnectable}
|
isConnectable={isConnectable}
|
||||||
resizeObserver={resizeObserver}
|
resizeObserver={resizeObserver}
|
||||||
dragHandle={node.dragHandle}
|
dragHandle={node.dragHandle}
|
||||||
zIndex={node[zSymbol] ?? 0}
|
zIndex={node[internalsSymbol].z ?? 0}
|
||||||
isParent={!!node[isParentSymbol]}
|
isParent={!!node[internalsSymbol].isParent}
|
||||||
noDragClassName={props.noDragClassName}
|
noDragClassName={props.noDragClassName}
|
||||||
noPanClassName={props.noPanClassName}
|
noPanClassName={props.noPanClassName}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { useCallback } from 'react';
|
|||||||
import { useStore } from '../store';
|
import { useStore } from '../store';
|
||||||
import { isEdgeVisible } from '../container/EdgeRenderer/utils';
|
import { isEdgeVisible } from '../container/EdgeRenderer/utils';
|
||||||
import { ReactFlowState, NodeInternals, Edge } from '../types';
|
import { ReactFlowState, NodeInternals, Edge } from '../types';
|
||||||
import { isNumeric, zSymbol } from '../utils';
|
import { internalsSymbol, isNumeric } from '../utils';
|
||||||
|
|
||||||
const defaultEdgeTree = [{ level: 0, isMaxLevel: true, edges: [] }];
|
const defaultEdgeTree = [{ level: 0, isMaxLevel: true, edges: [] }];
|
||||||
|
|
||||||
@@ -17,7 +17,10 @@ function groupEdgesByZLevel(edges: Edge[], nodeInternals: NodeInternals, elevate
|
|||||||
if (elevateEdgesOnSelect) {
|
if (elevateEdgesOnSelect) {
|
||||||
z = hasZIndex
|
z = hasZIndex
|
||||||
? edge.zIndex!
|
? edge.zIndex!
|
||||||
: Math.max(nodeInternals.get(edge.source)?.[zSymbol] || 0, nodeInternals.get(edge.target)?.[zSymbol] || 0);
|
: Math.max(
|
||||||
|
nodeInternals.get(edge.source)?.[internalsSymbol].z || 0,
|
||||||
|
nodeInternals.get(edge.target)?.[internalsSymbol].z || 0
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tree[z]) {
|
if (tree[z]) {
|
||||||
|
|||||||
@@ -32,6 +32,7 @@ export {
|
|||||||
} from './utils/graph';
|
} from './utils/graph';
|
||||||
export { applyNodeChanges, applyEdgeChanges } from './utils/changes';
|
export { applyNodeChanges, applyEdgeChanges } from './utils/changes';
|
||||||
export { getMarkerEnd, getCenter as getEdgeCenter } from './components/Edges/utils';
|
export { getMarkerEnd, getCenter as getEdgeCenter } from './components/Edges/utils';
|
||||||
|
export { internalsSymbol } from './utils';
|
||||||
|
|
||||||
export { default as useReactFlow } from './hooks/useReactFlow';
|
export { default as useReactFlow } from './hooks/useReactFlow';
|
||||||
export { default as useUpdateNodeInternals } from './hooks/useUpdateNodeInternals';
|
export { default as useUpdateNodeInternals } from './hooks/useUpdateNodeInternals';
|
||||||
|
|||||||
+5
-2
@@ -1,7 +1,7 @@
|
|||||||
import create from 'zustand';
|
import create from 'zustand';
|
||||||
import createContext from 'zustand/context';
|
import createContext from 'zustand/context';
|
||||||
|
|
||||||
import { clampPosition, getDimensions, handleBoundsSymbol } from '../utils';
|
import { clampPosition, getDimensions, internalsSymbol } from '../utils';
|
||||||
import { applyNodeChanges } from '../utils/changes';
|
import { applyNodeChanges } from '../utils/changes';
|
||||||
import {
|
import {
|
||||||
ReactFlowState,
|
ReactFlowState,
|
||||||
@@ -60,7 +60,10 @@ const createStore = () =>
|
|||||||
const handleBounds = getHandleBounds(update.nodeElement, transform[2]);
|
const handleBounds = getHandleBounds(update.nodeElement, transform[2]);
|
||||||
nodeInternals.set(node.id, {
|
nodeInternals.set(node.id, {
|
||||||
...node,
|
...node,
|
||||||
[handleBoundsSymbol]: handleBounds,
|
[internalsSymbol]: {
|
||||||
|
...node[internalsSymbol],
|
||||||
|
handleBounds,
|
||||||
|
},
|
||||||
...dimensions,
|
...dimensions,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+10
-12
@@ -1,7 +1,7 @@
|
|||||||
import { zoomIdentity } from 'd3-zoom';
|
import { zoomIdentity } from 'd3-zoom';
|
||||||
import { GetState, SetState } from 'zustand';
|
import { GetState, SetState } from 'zustand';
|
||||||
|
|
||||||
import { handleBoundsSymbol, isNumeric, isParentSymbol, zSymbol } from '../utils';
|
import { internalsSymbol, isNumeric } from '../utils';
|
||||||
import { getD3Transition, getRectOfNodes, getTransformForBounds } from '../utils/graph';
|
import { getD3Transition, getRectOfNodes, getTransformForBounds } from '../utils/graph';
|
||||||
import {
|
import {
|
||||||
Edge,
|
Edge,
|
||||||
@@ -30,7 +30,7 @@ function calculateXYZPosition(
|
|||||||
return calculateXYZPosition(parentNode, nodeInternals, parentNodes, {
|
return calculateXYZPosition(parentNode, nodeInternals, parentNodes, {
|
||||||
x: (result.x ?? 0) + (parentNode.position?.x ?? 0),
|
x: (result.x ?? 0) + (parentNode.position?.x ?? 0),
|
||||||
y: (result.y ?? 0) + (parentNode.position?.y ?? 0),
|
y: (result.y ?? 0) + (parentNode.position?.y ?? 0),
|
||||||
z: (parentNode[zSymbol] ?? 0) > (result.z ?? 0) ? parentNode[zSymbol] ?? 0 : result.z ?? 0,
|
z: (parentNode[internalsSymbol].z ?? 0) > (result.z ?? 0) ? parentNode[internalsSymbol].z ?? 0 : result.z ?? 0,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,14 +57,12 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
|
|||||||
parentNodes[node.parentNode] = true;
|
parentNodes[node.parentNode] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(internals, handleBoundsSymbol, {
|
Object.defineProperty(internals, internalsSymbol, {
|
||||||
enumerable: false,
|
enumerable: false,
|
||||||
value: currInternals?.[handleBoundsSymbol],
|
value: {
|
||||||
});
|
handleBounds: currInternals?.[internalsSymbol].handleBounds,
|
||||||
|
z,
|
||||||
Object.defineProperty(internals, zSymbol, {
|
},
|
||||||
enumerable: false,
|
|
||||||
value: z,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
nextNodeInternals.set(node.id, internals);
|
nextNodeInternals.set(node.id, internals);
|
||||||
@@ -78,7 +76,7 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
|
|||||||
if (node.parentNode || parentNodes[node.id]) {
|
if (node.parentNode || parentNodes[node.id]) {
|
||||||
const { x, y, z } = calculateXYZPosition(node, nextNodeInternals, parentNodes, {
|
const { x, y, z } = calculateXYZPosition(node, nextNodeInternals, parentNodes, {
|
||||||
...node.position,
|
...node.position,
|
||||||
z: node[zSymbol] ?? 0,
|
z: node[internalsSymbol].z ?? 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
node.positionAbsolute = {
|
node.positionAbsolute = {
|
||||||
@@ -86,10 +84,10 @@ export function createNodeInternals(nodes: Node[], nodeInternals: NodeInternals)
|
|||||||
y,
|
y,
|
||||||
};
|
};
|
||||||
|
|
||||||
node[zSymbol] = z;
|
node[internalsSymbol].z = z;
|
||||||
|
|
||||||
if (parentNodes[node.id]) {
|
if (parentNodes[node.id]) {
|
||||||
node[isParentSymbol] = true;
|
node[internalsSymbol].isParent = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
+6
-4
@@ -2,7 +2,7 @@ import { CSSProperties, MouseEvent as ReactMouseEvent } from 'react';
|
|||||||
|
|
||||||
import { XYPosition, Position, CoordinateExtent } from './utils';
|
import { XYPosition, Position, CoordinateExtent } from './utils';
|
||||||
import { HandleElement } from './handles';
|
import { HandleElement } from './handles';
|
||||||
import { handleBoundsSymbol, isParentSymbol, zSymbol } from '../utils';
|
import { internalsSymbol } from '../utils';
|
||||||
|
|
||||||
// interface for the user node items
|
// interface for the user node items
|
||||||
export interface Node<T = any> {
|
export interface Node<T = any> {
|
||||||
@@ -29,9 +29,11 @@ export interface Node<T = any> {
|
|||||||
positionAbsolute?: XYPosition;
|
positionAbsolute?: XYPosition;
|
||||||
|
|
||||||
// only used internally
|
// only used internally
|
||||||
[zSymbol]?: number;
|
[internalsSymbol]: {
|
||||||
[handleBoundsSymbol]?: NodeHandleBounds;
|
z?: number;
|
||||||
[isParentSymbol]?: boolean;
|
handleBounds?: NodeHandleBounds;
|
||||||
|
isParent?: boolean;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// props that get passed to a custom node
|
// props that get passed to a custom node
|
||||||
|
|||||||
+1
-3
@@ -41,6 +41,4 @@ export const getBoundsofRects = (rect1: Rect, rect2: Rect): Rect =>
|
|||||||
|
|
||||||
export const isNumeric = (n: any): n is number => !isNaN(n) && isFinite(n);
|
export const isNumeric = (n: any): n is number => !isNaN(n) && isFinite(n);
|
||||||
|
|
||||||
export const handleBoundsSymbol = Symbol('handleBounds');
|
export const internalsSymbol = Symbol('internals');
|
||||||
export const zSymbol = Symbol('z');
|
|
||||||
export const isParentSymbol = Symbol('isParent');
|
|
||||||
|
|||||||
Reference in New Issue
Block a user