From b80e1744be10d48bb0153165955f17b95ea527f8 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Fri, 14 Mar 2025 11:18:17 +1100 Subject: [PATCH] Snap to reroute when connecting links (#778) Add basic snap to reroute on hover; reuses existing slot visual. --- src/LGraphCanvas.ts | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index 1a1a58f60..6fcfcce1d 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -2485,7 +2485,8 @@ export class LGraphCanvas implements ConnectionColorContext { if (this.set_canvas_dirty_on_mouse_event) this.dirty_canvas = true - if (!this.graph) return + const { graph, resizingGroup, linkConnector } = this + if (!graph) return LGraphCanvas.active_canvas = this this.adjustMouseEvent(e) @@ -2524,12 +2525,11 @@ export class LGraphCanvas implements ConnectionColorContext { /** See {@link state}.{@link LGraphCanvasState.hoveringOver hoveringOver} */ let underPointer = CanvasItem.Nothing // get node over - const node = this.graph.getNodeOnPos( + const node = graph.getNodeOnPos( e.canvasX, e.canvasY, this.visible_nodes, ) - const { resizingGroup, linkConnector } = this const dragRect = this.dragging_rectangle if (dragRect) { @@ -2667,6 +2667,21 @@ export class LGraphCanvas implements ConnectionColorContext { underPointer |= CanvasItem.ResizeSe } } else { + const reroute = graph.getRerouteOnPos(e.canvasX, e.canvasY) + if (reroute) { + underPointer |= CanvasItem.Reroute + if (linkConnector.isConnecting) { + const firstSlot = linkConnector.renderLinks[0]?.fromSlot + const rerouteType = reroute.firstLink?.type ?? reroute.firstFloatingLink?.type + + if (firstSlot && rerouteType != null && LiteGraph.isValidConnection(rerouteType, firstSlot.type)) { + this._highlight_pos = reroute.pos + } + } + } else { + this._highlight_pos &&= undefined + } + // Not over a node const segment = this.#getLinkCentreOnPos(e) if (this.over_link_center !== segment) { @@ -2676,7 +2691,7 @@ export class LGraphCanvas implements ConnectionColorContext { } if (this.canvas) { - const group = this.graph.getGroupOnPos(e.canvasX, e.canvasY) + const group = graph.getGroupOnPos(e.canvasX, e.canvasY) if ( group && !e.ctrlKey &&