diff --git a/frontend/src/components/views/Graph.svelte b/frontend/src/components/views/Graph.svelte index b23701af..8268514a 100644 --- a/frontend/src/components/views/Graph.svelte +++ b/frontend/src/components/views/Graph.svelte @@ -40,6 +40,9 @@ let searchTerm = ""; let nodeListLocation: { x: number; y: number } | undefined = undefined; + let inputs: SVGSVGElement[][] = []; + let outputs: SVGSVGElement[][] = []; + $: watchNodes($nodeGraph.nodes); $: gridSpacing = calculateGridSpacing(transform.scale); @@ -126,17 +129,21 @@ } async function watchNodes(nodes: FrontendNode[]) { + nodes.forEach((_, index) => { + if (!inputs[index]) inputs[index] = []; + if (!outputs[index]) outputs[index] = []; + }); + selected = selected.filter((id) => nodes.find((node) => node.id === id)); await refreshLinks(); } - function resolveLink(link: FrontendNodeLink, containerBounds: HTMLDivElement): { nodeOutput: SVGSVGElement | undefined; nodeInput: SVGSVGElement | undefined } { + function resolveLink(link: FrontendNodeLink): { nodeOutput: SVGSVGElement | undefined; nodeInput: SVGSVGElement | undefined } { const outputIndex = Number(link.linkStartOutputIndex); const inputIndex = Number(link.linkEndInputIndex); - const nodeOutputConnectors = containerBounds.querySelectorAll(`[data-node="${String(link.linkStart)}"] [data-port="output"]`) || undefined; - - const nodeInputConnectors = containerBounds.querySelectorAll(`[data-node="${String(link.linkEnd)}"] [data-port="input"]`) || undefined; + const nodeOutputConnectors = outputs[$nodeGraph.nodes.findIndex((node) => node.id === link.linkStart)]; + const nodeInputConnectors = inputs[$nodeGraph.nodes.findIndex((node) => node.id === link.linkEnd)] || undefined; const nodeOutput = nodeOutputConnectors?.[outputIndex] as SVGSVGElement | undefined; const nodeInput = nodeInputConnectors?.[inputIndex] as SVGSVGElement | undefined; @@ -146,12 +153,9 @@ async function refreshLinks() { await tick(); - if (!nodesContainer) return; - const theNodesContainer = nodesContainer; - const links = $nodeGraph.links; nodeLinkPaths = links.flatMap((link, index) => { - const { nodeInput, nodeOutput } = resolveLink(link, theNodesContainer); + const { nodeInput, nodeOutput } = resolveLink(link); if (!nodeInput || !nodeOutput) return []; if (disconnecting?.linkIndex === index) return []; const linkStart = $nodeGraph.nodes.find((node) => node.id === link.linkStart)?.displayName === "Layer"; @@ -463,7 +467,7 @@ // Find the link that the node has been dragged on top of const link = $nodeGraph.links.find((link): boolean => { - const { nodeInput, nodeOutput } = resolveLink(link, theNodesContainer); + const { nodeInput, nodeOutput } = resolveLink(link); if (!nodeInput || !nodeOutput) return false; const wireCurveLocations = buildWirePathLocations(nodeOutput.getBoundingClientRect(), nodeInput.getBoundingClientRect(), false, false); @@ -661,7 +665,7 @@