diff --git a/src/LGraph.ts b/src/LGraph.ts index 4568bd20a..5bfc67d61 100644 --- a/src/LGraph.ts +++ b/src/LGraph.ts @@ -919,7 +919,7 @@ export class LGraph implements LinkNetwork, Serialisable { /** * Returns a node by its id. */ - getNodeById(id: NodeId): LGraphNode | null { + getNodeById(id: NodeId | null | undefined): LGraphNode | null { return id != null ? this._nodes_by_id[id] : null diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index 0720d530a..4f1810cd2 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -3365,16 +3365,28 @@ export class LGraphCanvas implements ConnectionColorContext { this.onNodeDeselected?.(item) + // Should be moved to top of function, and throw if null + const { graph } = this + if (!graph) return + // Clear link highlight if (item.inputs) { for (const input of item.inputs) { if (input.link == null) continue + + const node = LLink.getOriginNode(graph, input.link) + if (node && this.selectedItems.has(node)) continue + delete this.highlighted_links[input.link] } } if (item.outputs) { for (const id of item.outputs.flatMap(x => x.links)) { if (id == null) continue + + const node = LLink.getTargetNode(graph, id) + if (node && this.selectedItems.has(node)) continue + delete this.highlighted_links[id] } } diff --git a/src/LLink.ts b/src/LLink.ts index eee314944..303709032 100644 --- a/src/LLink.ts +++ b/src/LLink.ts @@ -5,7 +5,7 @@ import type { LinkSegment, ReadonlyLinkNetwork, } from "./interfaces" -import type { NodeId } from "./LGraphNode" +import type { LGraphNode, NodeId } from "./LGraphNode" import type { Reroute, RerouteId } from "./Reroute" import type { Serialisable, SerialisableLLink } from "./types/serialisation" @@ -156,6 +156,28 @@ export class LLink implements LinkSegment, Serialisable { ?.findNextReroute(rerouteId) } + /** + * Gets the origin node of a link. + * @param network The network to search + * @param linkId The ID of the link to get the origin node of + * @returns The origin node of the link, or `undefined` if the link is not found or the origin node is not found + */ + static getOriginNode(network: Pick, linkId: LinkId): LGraphNode | undefined { + const id = network.links.get(linkId)?.origin_id + return network.getNodeById(id) ?? undefined + } + + /** + * Gets the target node of a link. + * @param network The network to search + * @param linkId The ID of the link to get the target node of + * @returns The target node of the link, or `undefined` if the link is not found or the target node is not found + */ + static getTargetNode(network: Pick, linkId: LinkId): LGraphNode | undefined { + const id = network.links.get(linkId)?.target_id + return network.getNodeById(id) ?? undefined + } + configure(o: LLink | SerialisedLLinkArray) { if (Array.isArray(o)) { this.id = o[0] diff --git a/src/interfaces.ts b/src/interfaces.ts index 8012e6763..3ef223ad0 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -114,7 +114,7 @@ export interface ReadonlyLinkNetwork { readonly links: ReadonlyMap readonly reroutes: ReadonlyMap readonly floatingLinks: ReadonlyMap - getNodeById(id: NodeId): LGraphNode | null + getNodeById(id: NodeId | null | undefined): LGraphNode | null getReroute(parentId: null | undefined): undefined getReroute(parentId: RerouteId | null | undefined): Reroute | undefined }