From 2b572917565f3f78757f1a8d136623e20794e47d Mon Sep 17 00:00:00 2001 From: AustinMroz Date: Mon, 15 Sep 2025 14:49:10 -0700 Subject: [PATCH] Fix context menu creating nodes in wrong position (#5595) * Fix context menu creating nodes in wrong position When nodes are created from the context menu, they previously had there position set immediately after the node itself was created. Under some circumstances, this new position would be overwritten by the layout store. This is solved by setting the position before node initialization. * nit: Move size fix to named variable Also remove ternary. The elements are always numberic, so checking if a number is truthy before multiplying by 0 is a little silly. * nit: Further variable extraction --- src/lib/litegraph/src/LGraphCanvas.ts | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 9e7ef226fd..fade868c2a 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -6314,7 +6314,14 @@ export class LGraphCanvas } // that.graph.beforeChange(); - const newNode = LiteGraph.createNode(nodeNewType) + const xSizeFix = opts.posSizeFix[0] * LiteGraph.NODE_WIDTH + const ySizeFix = opts.posSizeFix[1] * LiteGraph.NODE_SLOT_HEIGHT + const nodeX = opts.position[0] + opts.posAdd[0] + xSizeFix + const nodeY = opts.position[1] + opts.posAdd[1] + ySizeFix + const pos = [nodeX, nodeY] + const newNode = LiteGraph.createNode(nodeNewType, nodeNewOpts.title, { + pos + }) if (newNode) { // if is object pass options if (nodeNewOpts) { @@ -6341,9 +6348,6 @@ export class LGraphCanvas ) } } - if (nodeNewOpts.title) { - newNode.title = nodeNewOpts.title - } if (nodeNewOpts.json) { newNode.configure(nodeNewOpts.json) } @@ -6353,14 +6357,6 @@ export class LGraphCanvas if (!this.graph) throw new NullGraphError() this.graph.add(newNode) - newNode.pos = [ - opts.position[0] + - opts.posAdd[0] + - (opts.posSizeFix[0] ? opts.posSizeFix[0] * newNode.size[0] : 0), - opts.position[1] + - opts.posAdd[1] + - (opts.posSizeFix[1] ? opts.posSizeFix[1] * newNode.size[1] : 0) - ] // Interim API - allow the link connection to be canceled. // TODO: https://github.com/Comfy-Org/litegraph.js/issues/946