Fix corruption of selected link highlights (#897)

Occurred when deselecting one side of a link when both sides were
selected.
This commit is contained in:
filtered
2025-04-07 00:30:22 +10:00
committed by GitHub
parent 7360e09172
commit 6cafeeff19
4 changed files with 37 additions and 3 deletions

View File

@@ -919,7 +919,7 @@ export class LGraph implements LinkNetwork, Serialisable<SerialisableGraph> {
/**
* 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

View File

@@ -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]
}
}

View File

@@ -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<SerialisableLLink> {
?.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<ReadonlyLinkNetwork, "getNodeById" | "links">, 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<ReadonlyLinkNetwork, "getNodeById" | "links">, 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]

View File

@@ -114,7 +114,7 @@ export interface ReadonlyLinkNetwork {
readonly links: ReadonlyMap<LinkId, LLink>
readonly reroutes: ReadonlyMap<RerouteId, Reroute>
readonly floatingLinks: ReadonlyMap<LinkId, LLink>
getNodeById(id: NodeId): LGraphNode | null
getNodeById(id: NodeId | null | undefined): LGraphNode | null
getReroute(parentId: null | undefined): undefined
getReroute(parentId: RerouteId | null | undefined): Reroute | undefined
}