import type { Changeset } from '.'; import { EdgeBase, NodeBase } from '../types'; import type { EdgeChange, ElementChangeType, NodeChange } from './types'; export function applyNodeChanges( nodes: NodeType[], nodeChanges: Changeset> ): NodeType[] { return applyChanges(nodes, nodeChanges, applyNodeChange); } export function applyEdgeChanges( edges: EdgeType[], edgeChanges: Changeset> ): EdgeType[] { return applyChanges(edges, edgeChanges, applyEdgeChange); } function applyChanges>( previousElements: ElementType[], changes: Changeset, applyChange: (element: ElementType, change: ChangeType) => void ): ElementType[] { const newElements: ElementType[] = []; const addedElements = changes.getNewElements(); const addedIds = new Set(addedElements.map((element) => element.id)); let isPreviousElement: boolean | undefined; for (const elements of [previousElements, addedElements]) { isPreviousElement = !isPreviousElement; elementsLoop: for (const element of elements) { // when we loop over the addedElements we skip elements that already exist if (!isPreviousElement && !addedIds.has(element.id)) { continue; } const elementChanges = changes.getForElement(element.id); if (!elementChanges.length) { newElements.push(element); addedIds.delete(element.id); continue; } let updatedElement = { ...element }; for (const change of elementChanges) { switch (change.type) { case 'remove': //remove takes prescedent addedIds.delete(element.id); continue elementsLoop; case 'replace': updatedElement = { ...change.item }; continue; default: applyChange(updatedElement, change); } } newElements.push(updatedElement); addedIds.delete(element.id); } } return newElements; } function applyNodeChange(node: NodeType, change: NodeChange) { switch (change.type) { case 'select': { node.selected = change.selected; break; } case 'position': { if (typeof change.position !== 'undefined') { node.position = change.position; } if (typeof change.dragging !== 'undefined') { node.dragging = change.dragging; } break; } case 'dimensions': { if (typeof change.dimensions !== 'undefined') { node.measured = { ...change.dimensions, }; if (change.setAttributes === true || change.setAttributes === 'width') { node.width = change.dimensions.width; } if (change.setAttributes === true || change.setAttributes === 'height') { node.height = change.dimensions.height; } } if (typeof change.resizing === 'boolean') { node.resizing = change.resizing; } break; } case 'update': { Object.assign(node, change.item); break; } case 'data': { node.data = change.replace ? change.data : { ...node.data, ...change.data }; break; } default: return; } } export function applyEdgeChange(edge: EdgeType, change: EdgeChange) { switch (change.type) { case 'select': { edge.selected = change.selected; break; } case 'update': { Object.assign(edge, change.item); break; } case 'data': { edge.data = change.replace ? change.data : { ...edge.data, ...change.data }; break; } default: return; } }