[API] Add LinkConnector - replaces connecting_links (#726)

### LinkConnector

Replaces the minimal-change effort of `connecting_links` with a more
reliable implementation.

- Subscribable, event-based API
- Uses browser-standard `e.preventDefault()` to cancel `before-*` events
- Uses standard `state` POJO - can be proxied without issue
- Structures code and separates concerns out of `LGraphCanvas`
- Link creation calls can now be made from anywhere, without the need
for a rewrite
- New canvas sub-components now live in `src/canvas/`

### Rendering

- Skips link segments by setting a `_dragging` bool flag on the LLink or
Reroute
- Moves some previously nested code to event listeners, configured in
the `LGraphCanvas` constructor

### Deprecation

`LGraphCanvas.connecting_links` is now deprecated and will later be
removed.

Until it is removed, to prevent breaking extensions it will continue to
be set and cleared by a legacy callback. The contents of this property
are ignored; code search revealed no exentsions actually modifying the
array.
This commit is contained in:
filtered
2025-03-09 10:48:45 +11:00
committed by GitHub
parent 8e59fbfaa2
commit c0bfe5489f
12 changed files with 950 additions and 330 deletions

View File

@@ -1,4 +1,4 @@
import type { CanvasColour, ConnectingLink, Dictionary, INodeInputSlot, INodeOutputSlot, INodeSlot, ISlotType, IWidgetInputSlot, Point } from "./interfaces"
import type { CanvasColour, Dictionary, INodeInputSlot, INodeOutputSlot, INodeSlot, ISlotType, IWidgetInputSlot, Point } from "./interfaces"
import type { LinkId } from "./LLink"
import type { IWidget } from "./types/widgets"
@@ -83,9 +83,9 @@ export abstract class NodeSlot implements INodeSlot {
/**
* Whether this slot is a valid target for a dragging link.
* @param link The link to check against.
* @param fromSlot The slot that the link is being connected from.
*/
abstract isValidTarget(link: ConnectingLink | undefined): boolean
abstract isValidTarget(fromSlot: INodeInputSlot | INodeOutputSlot): boolean
/**
* The label to display in the UI.
@@ -270,10 +270,8 @@ export class NodeInputSlot extends NodeSlot implements INodeInputSlot {
return this.link != null
}
override isValidTarget(link: ConnectingLink | undefined): boolean {
if (!link) return true
return !!link.output && LiteGraph.isValidConnection(this.type, link.output.type)
override isValidTarget(fromSlot: INodeInputSlot | INodeOutputSlot): boolean {
return "links" in fromSlot && LiteGraph.isValidConnection(this.type, fromSlot.type)
}
override draw(ctx: CanvasRenderingContext2D, options: Omit<IDrawOptions, "doStroke" | "labelPosition">) {
@@ -306,10 +304,8 @@ export class NodeOutputSlot extends NodeSlot implements INodeOutputSlot {
this.slot_index = slot.slot_index
}
override isValidTarget(link: ConnectingLink | undefined): boolean {
if (!link) return true
return !!link.input && LiteGraph.isValidConnection(this.type, link.input.type)
override isValidTarget(fromSlot: INodeInputSlot | INodeOutputSlot): boolean {
return "link" in fromSlot && LiteGraph.isValidConnection(this.type, fromSlot.type)
}
override isConnected(): boolean {