[Refactor] Use strict mode in Add Node menus (#677)

- Improves TS types
- Adds type guards / null checks
This commit is contained in:
filtered
2025-03-03 01:17:25 +11:00
committed by GitHub
parent 77b274f27f
commit 972527de70

View File

@@ -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<ICreateNodeOptions, ICreateNodeOptions>({
createDefaultNodeForSlot(optPass: ICreateDefaultNodeOptions): boolean {
type DefaultOptions = ICreateDefaultNodeOptions & {
posAdd: Point
posSizeFix: Point
}
const opts = Object.assign<DefaultOptions, ICreateDefaultNodeOptions>({
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<ICreateNodeOptions & { e: MouseEvent }>): void {
const opts = Object.assign<ICreateNodeOptions, ICreateNodeOptions>({
const opts = Object.assign<ICreateNodeOptions & HasShowSearchCallback, ICreateNodeOptions>({
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<ICreateNodeOptions, ICreateNodeOptions>(opts, {
position: [opts.e.canvasX, opts.e.canvasY],
const customProps = {
position: [opts.e?.canvasX ?? 0, opts.e?.canvasY ?? 0],
nodeType: v,
afterRerouteId,
}))
} satisfies Partial<ICreateDefaultNodeOptions>
const options = Object.assign(opts, customProps)
that.createDefaultNodeForSlot(options)
break
}
}