From 972527de7098aff6dcd31b0cc356888379621958 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 3 Mar 2025 01:17:25 +1100 Subject: [PATCH] [Refactor] Use strict mode in Add Node menus (#677) - Improves TS types - Adds type guards / null checks --- src/LGraphCanvas.ts | 66 ++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 19 deletions(-) diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index c68ca3124b..c7a4c68708 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -101,23 +101,28 @@ interface ICreateNodeOptions { slotTo?: number | INodeOutputSlot | INodeInputSlot | null /** pass the event coords */ - // FIXME: Should not be optional - /** Position of new node */ - position?: Point /** Create the connection from a reroute */ afterRerouteId?: RerouteId // FIXME: Should not be optional /** choose a nodetype to add, AUTO to set at first good */ nodeType?: string + e?: CanvasMouseEvent + allow_searchbox?: boolean +} + +interface ICreateDefaultNodeOptions extends ICreateNodeOptions { + /** Position of new node */ + position: Point /** adjust x,y */ posAdd?: Point /** alpha, adjust the position x,y based on the new node size w,h */ posSizeFix?: Point - e?: CanvasMouseEvent - allow_searchbox?: boolean +} + +interface HasShowSearchCallback { /** See {@link LGraphCanvas.showSearchBox} */ - showSearchBox?: ( + showSearchBox: ( event: MouseEvent, options?: IShowSearchOptions, ) => HTMLDivElement | void @@ -5557,17 +5562,22 @@ export class LGraphCanvas implements ConnectionColorContext { } } - createDefaultNodeForSlot(optPass: ICreateNodeOptions): boolean { - const opts = Object.assign({ + createDefaultNodeForSlot(optPass: ICreateDefaultNodeOptions): boolean { + type DefaultOptions = ICreateDefaultNodeOptions & { + posAdd: Point + posSizeFix: Point + } + + const opts = Object.assign({ nodeFrom: null, slotFrom: null, nodeTo: null, slotTo: null, position: [0, 0], - nodeType: null, + nodeType: undefined, posAdd: [0, 0], posSizeFix: [0, 0], - }, optPass || {}) + }, optPass) const { afterRerouteId } = opts const isFrom = opts.nodeFrom && opts.slotFrom !== null @@ -5583,6 +5593,8 @@ export class LGraphCanvas implements ConnectionColorContext { } const nodeX = isFrom ? opts.nodeFrom : opts.nodeTo + if (!nodeX) throw new TypeError("nodeX was null when creating default node for slot.") + let slotX = isFrom ? opts.slotFrom : opts.slotTo let iSlotConn: number | false = false @@ -5592,6 +5604,11 @@ export class LGraphCanvas implements ConnectionColorContext { slotX = isFrom ? nodeX.outputs[slotX] : nodeX.inputs[slotX] break case "object": + if (slotX === null) { + console.warn("Cant get slot information", slotX) + return false + } + // ok slotX iSlotConn = isFrom ? nodeX.findOutputSlot(slotX.name) : nodeX.findInputSlot(slotX.name) break @@ -5684,8 +5701,10 @@ export class LGraphCanvas implements ConnectionColorContext { // connect the two! if (isFrom) { + if (!opts.nodeFrom) throw new TypeError("createDefaultNodeForSlot - nodeFrom was null") opts.nodeFrom.connectByType(iSlotConn, newNode, fromSlotType, { afterRerouteId }) } else { + if (!opts.nodeTo) throw new TypeError("createDefaultNodeForSlot - nodeTo was null") opts.nodeTo.connectByTypeOutput(iSlotConn, newNode, fromSlotType, { afterRerouteId }) } @@ -5703,12 +5722,12 @@ export class LGraphCanvas implements ConnectionColorContext { } showConnectionMenu(optPass: Partial): void { - const opts = Object.assign({ + const opts = Object.assign({ nodeFrom: null, slotFrom: null, nodeTo: null, slotTo: null, - e: null, + e: undefined, allow_searchbox: this.allow_searchbox, showSearchBox: this.showSearchBox, }, optPass || {}) @@ -5724,6 +5743,7 @@ export class LGraphCanvas implements ConnectionColorContext { } const nodeX = isFrom ? opts.nodeFrom : opts.nodeTo + if (!nodeX) throw new TypeError("nodeX was null when creating default node for slot.") let slotX = isFrom ? opts.slotFrom : opts.slotTo let iSlotConn: number @@ -5735,6 +5755,11 @@ export class LGraphCanvas implements ConnectionColorContext { slotX = isFrom ? nodeX.outputs[slotX] : nodeX.inputs[slotX] break case "object": + if (slotX === null) { + console.warn("Cant get slot information", slotX) + return + } + // ok slotX iSlotConn = isFrom ? nodeX.findOutputSlot(slotX.name) @@ -5786,10 +5811,12 @@ export class LGraphCanvas implements ConnectionColorContext { switch (v) { case "Add Node": LGraphCanvas.onMenuAdd(null, null, e, menu, function (node) { + if (!node) return + if (isFrom) { - opts.nodeFrom.connectByType(iSlotConn, node, fromSlotType, { afterRerouteId }) + opts.nodeFrom?.connectByType(iSlotConn, node, fromSlotType, { afterRerouteId }) } else { - opts.nodeTo.connectByTypeOutput(iSlotConn, node, fromSlotType, { afterRerouteId }) + opts.nodeTo?.connectByTypeOutput(iSlotConn, node, fromSlotType, { afterRerouteId }) } }) break @@ -5801,13 +5828,14 @@ export class LGraphCanvas implements ConnectionColorContext { } break default: { - // check for defaults nodes for this slottype - - that.createDefaultNodeForSlot(Object.assign(opts, { - position: [opts.e.canvasX, opts.e.canvasY], + const customProps = { + position: [opts.e?.canvasX ?? 0, opts.e?.canvasY ?? 0], nodeType: v, afterRerouteId, - })) + } satisfies Partial + + const options = Object.assign(opts, customProps) + that.createDefaultNodeForSlot(options) break } }