refactor(nodes): rename parentNode to parentId

This commit is contained in:
moklick
2024-04-09 13:26:54 +02:00
parent 4570b2494b
commit 3e59ab94f0
10 changed files with 41 additions and 39 deletions
@@ -137,7 +137,7 @@ const initialNodes: Node[] = [
label: 'Child with extent: parent', label: 'Child with extent: parent',
}, },
position: { x: 50, y: 50 }, position: { x: 50, y: 50 },
parentNode: '5', parentId: '5',
extent: 'parent', extent: 'parent',
width: 50, width: 50,
height: 100, height: 100,
@@ -148,7 +148,7 @@ const initialNodes: Node[] = [
type: 'defaultResizer', type: 'defaultResizer',
data: { label: 'Child with expandParent' }, data: { label: 'Child with expandParent' },
position: { x: 150, y: 100 }, position: { x: 150, y: 100 },
parentNode: '5', parentId: '5',
expandParent: true, expandParent: true,
style: { ...nodeStyle }, style: { ...nodeStyle },
}, },
@@ -157,7 +157,7 @@ const initialNodes: Node[] = [
type: 'defaultResizer', type: 'defaultResizer',
data: { label: 'Child with expandParent & keepAspectRatio', keepAspectRatio: true }, data: { label: 'Child with expandParent & keepAspectRatio', keepAspectRatio: true },
position: { x: 25, y: 200 }, position: { x: 25, y: 200 },
parentNode: '5', parentId: '5',
expandParent: true, expandParent: true,
style: { ...nodeStyle }, style: { ...nodeStyle },
}, },
@@ -50,7 +50,7 @@ const initialNodes: Node[] = [
data: { label: 'Node 4a' }, data: { label: 'Node 4a' },
position: { x: 15, y: 15 }, position: { x: 15, y: 15 },
className: 'light', className: 'light',
parentNode: '4', parentId: '4',
origin: [0.5, 0.5], origin: [0.5, 0.5],
extent: [ extent: [
@@ -68,21 +68,21 @@ const initialNodes: Node[] = [
height: 200, height: 200,
width: 300, width: 300,
}, },
parentNode: '4', parentId: '4',
}, },
{ {
id: '4b1', id: '4b1',
data: { label: 'Node 4b1' }, data: { label: 'Node 4b1' },
position: { x: 40, y: 20 }, position: { x: 40, y: 20 },
className: 'light', className: 'light',
parentNode: '4b', parentId: '4b',
}, },
{ {
id: '4b2', id: '4b2',
data: { label: 'Node 4b2' }, data: { label: 'Node 4b2' },
position: { x: 20, y: 100 }, position: { x: 20, y: 100 },
className: 'light', className: 'light',
parentNode: '4b', parentId: '4b',
}, },
{ {
id: '5', id: '5',
@@ -98,7 +98,7 @@ const initialNodes: Node[] = [
data: { label: 'Node 5a' }, data: { label: 'Node 5a' },
position: { x: 0, y: 0 }, position: { x: 0, y: 0 },
className: 'light', className: 'light',
parentNode: '5', parentId: '5',
extent: 'parent', extent: 'parent',
}, },
{ {
@@ -106,7 +106,7 @@ const initialNodes: Node[] = [
data: { label: 'Node 5b' }, data: { label: 'Node 5b' },
position: { x: 225, y: 50 }, position: { x: 225, y: 50 },
className: 'light', className: 'light',
parentNode: '5', parentId: '5',
expandParent: true, expandParent: true,
}, },
{ {
@@ -160,7 +160,7 @@ const Subflow = () => {
const updatePos = () => { const updatePos = () => {
setNodes((nds) => { setNodes((nds) => {
return nds.map((n) => { return nds.map((n) => {
if (!n.parentNode) { if (!n.parentId) {
return { return {
...n, ...n,
position: { position: {
@@ -194,7 +194,7 @@ const Subflow = () => {
return nds.map((n) => { return nds.map((n) => {
return { return {
...n, ...n,
hidden: !!n.parentNode && !n.hidden, hidden: !!n.parentId && !n.hidden,
}; };
}); });
}); });
@@ -113,7 +113,7 @@
type: 'defaultResizer', type: 'defaultResizer',
data: { label: 'Child with extent parent' }, data: { label: 'Child with extent parent' },
position: { x: 50, y: 50 }, position: { x: 50, y: 50 },
parentNode: '5', parentId: '5',
extent: 'parent', extent: 'parent',
style: nodeStyle style: nodeStyle
}, },
@@ -122,7 +122,7 @@
type: 'defaultResizer', type: 'defaultResizer',
data: { label: 'Child' }, data: { label: 'Child' },
position: { x: 100, y: 100 }, position: { x: 100, y: 100 },
parentNode: '5', parentId: '5',
style: nodeStyle style: nodeStyle
} }
]); ]);
@@ -36,7 +36,7 @@
id: '4a', id: '4a',
data: { label: 'Node 4a' }, data: { label: 'Node 4a' },
position: { x: 15, y: 15 }, position: { x: 15, y: 15 },
parentNode: '4', parentId: '4',
extent: [ extent: [
[0, 0], [0, 0],
[100, 100] [100, 100]
@@ -47,19 +47,19 @@
data: { label: 'Node 4b' }, data: { label: 'Node 4b' },
position: { x: 100, y: 60 }, position: { x: 100, y: 60 },
style: 'width: 300px; height: 200px;', style: 'width: 300px; height: 200px;',
parentNode: '4' parentId: '4'
}, },
{ {
id: '4b1', id: '4b1',
data: { label: 'Node 4b1' }, data: { label: 'Node 4b1' },
position: { x: 40, y: 20 }, position: { x: 40, y: 20 },
parentNode: '4b' parentId: '4b'
}, },
{ {
id: '4b2', id: '4b2',
data: { label: 'Node 4b2' }, data: { label: 'Node 4b2' },
position: { x: 20, y: 100 }, position: { x: 20, y: 100 },
parentNode: '4b' parentId: '4b'
}, },
{ {
id: '5', id: '5',
@@ -73,14 +73,14 @@
id: '5a', id: '5a',
data: { label: 'Node 5a' }, data: { label: 'Node 5a' },
position: { x: 0, y: 0 }, position: { x: 0, y: 0 },
parentNode: '5', parentId: '5',
extent: 'parent' extent: 'parent'
}, },
{ {
id: '5b', id: '5b',
data: { label: 'Node 5b' }, data: { label: 'Node 5b' },
position: { x: 225, y: 50 }, position: { x: 225, y: 50 },
parentNode: '5', parentId: '5',
expandParent: true expandParent: true
}, },
{ {
+1
View File
@@ -120,6 +120,7 @@ const createRFStore = ({
const triggerChangeNodes: InternalNode[] = []; const triggerChangeNodes: InternalNode[] = [];
const changes: NodeChange[] = nodeDragItems.map((node) => { const changes: NodeChange[] = nodeDragItems.map((node) => {
// @todo add expandParent to drag item so that we can get rid of the look up here
const internalNode = nodeLookup.get(node.id); const internalNode = nodeLookup.get(node.id);
const change: NodeChange = { const change: NodeChange = {
id: node.id, id: node.id,
+2 -2
View File
@@ -43,7 +43,7 @@ export type NodeBase<
initialWidth?: number; initialWidth?: number;
initialHeight?: number; initialHeight?: number;
/** Parent node id, used for creating sub-flows */ /** Parent node id, used for creating sub-flows */
parentNode?: string; parentId?: string;
zIndex?: number; zIndex?: number;
/** Boundary a node can be moved in /** Boundary a node can be moved in
* @example 'parent' or [[0, 0], [100, 100]] * @example 'parent' or [[0, 0], [100, 100]]
@@ -129,7 +129,7 @@ export type NodeDragItem = {
positionAbsolute: XYPosition; positionAbsolute: XYPosition;
}; };
extent?: 'parent' | CoordinateExtent; extent?: 'parent' | CoordinateExtent;
parentNode?: string; parentId?: string;
dragging?: boolean; dragging?: boolean;
origin?: NodeOrigin; origin?: NodeOrigin;
expandParent?: boolean; expandParent?: boolean;
+2 -2
View File
@@ -358,7 +358,7 @@ export function calculateNodePosition<NodeType extends NodeBase>({
onError?: OnError; onError?: OnError;
}): { position: XYPosition; positionAbsolute: XYPosition } { }): { position: XYPosition; positionAbsolute: XYPosition } {
const node = nodeLookup.get(nodeId)!; const node = nodeLookup.get(nodeId)!;
const parentNode = node.parentNode ? nodeLookup.get(node.parentNode) : undefined; const parentNode = node.parentId ? nodeLookup.get(node.parentId) : undefined;
const { x: parentX, y: parentY } = parentNode const { x: parentX, y: parentY } = parentNode
? getNodePositionWithOrigin(parentNode, parentNode.origin || nodeOrigin).positionAbsolute ? getNodePositionWithOrigin(parentNode, parentNode.origin || nodeOrigin).positionAbsolute
: { x: 0, y: 0 }; : { x: 0, y: 0 };
@@ -440,7 +440,7 @@ export async function getElementsToRemove<NodeType extends NodeBase = NodeBase,
} }
const isIncluded = nodeIds.includes(node.id); const isIncluded = nodeIds.includes(node.id);
const parentHit = !isIncluded && node.parentNode && matchingNodes.find((n) => n.id === node.parentNode); const parentHit = !isIncluded && node.parentId && matchingNodes.find((n) => n.id === node.parentId);
if (isIncluded || parentHit) { if (isIncluded || parentHit) {
matchingNodes.push(node); matchingNodes.push(node);
+11 -10
View File
@@ -31,13 +31,13 @@ export function updateAbsolutePositions<NodeType extends NodeBase>(
const selectedNodeZ: number = options?.elevateNodesOnSelect ? 1000 : 0; const selectedNodeZ: number = options?.elevateNodesOnSelect ? 1000 : 0;
for (const [id, node] of nodeLookup) { for (const [id, node] of nodeLookup) {
const parentId = node.parentNode; const parentId = node.parentId;
if (parentId && !nodeLookup.has(parentId)) { if (parentId && !nodeLookup.has(parentId)) {
throw new Error(`Parent node ${parentId} not found`); throw new Error(`Parent node ${parentId} not found`);
} }
if (node.parentNode || node.internals.isParent || parentNodeIds?.has(id)) { if (parentId || node.internals.isParent || parentNodeIds?.has(id)) {
const parentNode = parentId ? nodeLookup.get(parentId) : null; const parentNode = parentId ? nodeLookup.get(parentId) : null;
const { x, y, z } = calculateXYZPosition( const { x, y, z } = calculateXYZPosition(
node, node,
@@ -87,8 +87,8 @@ export function adoptUserNodes<NodeType extends NodeBase>(
nodes.forEach((userNode) => { nodes.forEach((userNode) => {
const currentStoreNode = tmpLookup.get(userNode.id); const currentStoreNode = tmpLookup.get(userNode.id);
if (userNode.parentNode) { if (userNode.parentId) {
parentNodeIds.add(userNode.parentNode); parentNodeIds.add(userNode.parentId);
} }
if (userNode === currentStoreNode?.internals.userNode) { if (userNode === currentStoreNode?.internals.userNode) {
@@ -123,11 +123,11 @@ function calculateXYZPosition<NodeType extends NodeBase>(
result: XYZPosition, result: XYZPosition,
nodeOrigin: NodeOrigin = [0, 0] nodeOrigin: NodeOrigin = [0, 0]
): XYZPosition { ): XYZPosition {
if (!node.parentNode) { if (!node.parentId) {
return result; return result;
} }
const parentNode = nodeLookup.get(node.parentNode)!; const parentNode = nodeLookup.get(node.parentId)!;
const { position: parentNodePosition } = getNodePositionWithOrigin(parentNode, parentNode?.origin || nodeOrigin); const { position: parentNodePosition } = getNodePositionWithOrigin(parentNode, parentNode?.origin || nodeOrigin);
return calculateXYZPosition( return calculateXYZPosition(
@@ -147,13 +147,14 @@ export function handleParentExpand(nodes: InternalNodeBase[], nodeLookup: NodeLo
const chilNodeRects = new Map<string, Rect>(); const chilNodeRects = new Map<string, Rect>();
nodes.forEach((node) => { nodes.forEach((node) => {
if (node.expandParent && node.parentNode) { const parentId = node.parentId;
const parentNode = nodeLookup.get(node.parentNode); if (node.expandParent && parentId) {
const parentNode = nodeLookup.get(parentId);
if (parentNode) { if (parentNode) {
const parentRect = chilNodeRects.get(node.parentNode) || nodeToRect(parentNode, node.origin); const parentRect = chilNodeRects.get(parentId) || nodeToRect(parentNode, node.origin);
const expandedRect = getBoundsOfRects(parentRect, nodeToRect(node, node.origin)); const expandedRect = getBoundsOfRects(parentRect, nodeToRect(node, node.origin));
chilNodeRects.set(node.parentNode, expandedRect); chilNodeRects.set(parentId, expandedRect);
} }
} }
}); });
+4 -4
View File
@@ -5,11 +5,11 @@ export function wrapSelectionDragFunc(selectionFunc?: (event: MouseEvent, nodes:
} }
export function isParentSelected<NodeType extends NodeBase>(node: NodeType, nodeLookup: NodeLookup): boolean { export function isParentSelected<NodeType extends NodeBase>(node: NodeType, nodeLookup: NodeLookup): boolean {
if (!node.parentNode) { if (!node.parentId) {
return false; return false;
} }
const parentNode = nodeLookup.get(node.parentNode); const parentNode = nodeLookup.get(node.parentId);
if (!parentNode) { if (!parentNode) {
return false; return false;
@@ -46,7 +46,7 @@ export function getDragItems<NodeType extends NodeBase>(
for (const [id, node] of nodeLookup) { for (const [id, node] of nodeLookup) {
if ( if (
(node.selected || node.id === nodeId) && (node.selected || node.id === nodeId) &&
(!node.parentNode || !isParentSelected(node, nodeLookup)) && (!node.parentId || !isParentSelected(node, nodeLookup)) &&
(node.draggable || (nodesDraggable && typeof node.draggable === 'undefined')) (node.draggable || (nodesDraggable && typeof node.draggable === 'undefined'))
) { ) {
const internalNode = nodeLookup.get(id)!; const internalNode = nodeLookup.get(id)!;
@@ -59,7 +59,7 @@ export function getDragItems<NodeType extends NodeBase>(
y: mousePos.y - (internalNode.internals.positionAbsolute?.y ?? 0), y: mousePos.y - (internalNode.internals.positionAbsolute?.y ?? 0),
}, },
extent: internalNode.extent, extent: internalNode.extent,
parentNode: internalNode.parentNode, parentId: internalNode.parentId,
origin: internalNode.origin, origin: internalNode.origin,
expandParent: internalNode.expandParent, expandParent: internalNode.expandParent,
internals: { internals: {
+2 -2
View File
@@ -136,7 +136,7 @@ export function XYResizer({ domNode, nodeId, getStoreItems, onChange }: XYResize
parentNode = undefined; parentNode = undefined;
if (node.extent === 'parent' || node.expandParent) { if (node.extent === 'parent' || node.expandParent) {
parentNode = nodeLookup.get(node.parentNode!); parentNode = nodeLookup.get(node.parentId!);
if (parentNode && node.extent === 'parent') { if (parentNode && node.extent === 'parent') {
parentExtent = nodeToParentExtent(parentNode); parentExtent = nodeToParentExtent(parentNode);
} }
@@ -148,7 +148,7 @@ export function XYResizer({ domNode, nodeId, getStoreItems, onChange }: XYResize
childExtent = undefined; childExtent = undefined;
for (const [childId, child] of nodeLookup) { for (const [childId, child] of nodeLookup) {
if (child.parentNode === nodeId) { if (child.parentId === nodeId) {
childNodes.push({ childNodes.push({
id: childId, id: childId,
position: { ...child.position }, position: { ...child.position },