From f7a0a92f3a4aa001c5dedb0f7b02031a301c975d Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Mon, 21 Apr 2025 01:02:54 +1000 Subject: [PATCH] [CodeHealth] Standardise code (#951) Refactors & standardises code. --- src/LGraphNode.ts | 13 ++++++------- src/LiteGraphGlobal.ts | 20 +++++++++++--------- src/NodeSlot.ts | 10 ++++------ src/interfaces.ts | 6 +++++- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/LGraphNode.ts b/src/LGraphNode.ts index feba139b55..9c10e1307d 100644 --- a/src/LGraphNode.ts +++ b/src/LGraphNode.ts @@ -1472,7 +1472,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable { */ addInput(name: string, type: ISlotType, extra_info?: Partial): INodeInputSlot { type = type || 0 - const input: INodeInputSlot = new NodeInputSlot({ name: name, type: type, link: null }) + const input = new NodeInputSlot({ name: name, type: type, link: null }) if (extra_info) Object.assign(input, extra_info) this.inputs ||= [] @@ -2878,10 +2878,9 @@ export class LGraphNode implements Positionable, IPinnable, IColorable { * @internal The inputs that are not positioned with absolute coordinates. */ get #defaultVerticalInputs() { - return this.inputs.filter((slot: INodeInputSlot) => !( - slot.pos || - (this.widgets?.length && isWidgetInputSlot(slot)) - )) + return this.inputs.filter( + slot => !slot.pos && !(this.widgets?.length && isWidgetInputSlot(slot)), + ) } /** @@ -3489,7 +3488,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable { slots.push(layoutElement) } - return slots.length ? createBounds(slots, /** padding= */ 0) : null + return slots.length ? createBounds(slots, 0) : null } #getMouseOverSlot(slot: INodeSlot): INodeSlot | null { @@ -3658,7 +3657,7 @@ export class LGraphNode implements Positionable, IPinnable, IColorable { for (const [i, slot] of this.inputs.entries()) { if (!isWidgetInputSlot(slot)) continue - slotByWidgetName.set(slot.widget?.name, { ...slot, index: i }) + slotByWidgetName.set(slot.widget.name, { ...slot, index: i }) } if (!slotByWidgetName.size) return diff --git a/src/LiteGraphGlobal.ts b/src/LiteGraphGlobal.ts index bdc1deefb7..328e62a504 100644 --- a/src/LiteGraphGlobal.ts +++ b/src/LiteGraphGlobal.ts @@ -364,20 +364,22 @@ export class LiteGraphGlobal { for (let slotType of allTypes) { if (slotType === "") slotType = "*" - const registerTo = out - ? "registered_slot_out_types" - : "registered_slot_in_types" - if (this[registerTo][slotType] === undefined) - this[registerTo][slotType] = { nodes: [] } - if (!this[registerTo][slotType].nodes.includes(class_type)) - this[registerTo][slotType].nodes.push(class_type) + const register = out + ? this.registered_slot_out_types + : this.registered_slot_in_types + register[slotType] ??= { nodes: [] } + + const { nodes } = register[slotType] + if (!nodes.includes(class_type)) nodes.push(class_type) // check if is a new type const types = out ? this.slot_types_out : this.slot_types_in - if (!types.includes(slotType.toLowerCase())) { - types.push(slotType.toLowerCase()) + const type = slotType.toLowerCase() + + if (!types.includes(type)) { + types.push(type) types.sort() } } diff --git a/src/NodeSlot.ts b/src/NodeSlot.ts index 20c7cf7d55..ac3eda8e8c 100644 --- a/src/NodeSlot.ts +++ b/src/NodeSlot.ts @@ -139,18 +139,16 @@ export abstract class NodeSlot implements INodeSlot { draw( ctx: CanvasRenderingContext2D, - options: IDrawOptions, - ) { - const { + { pos, colorContext, labelColor = "#AAA", labelPosition = LabelPosition.Right, lowQuality = false, highlight = false, - } = options - let { doStroke = false } = options - + doStroke = false, + }: IDrawOptions, + ) { // Save the current fillStyle and strokeStyle const originalFillStyle = ctx.fillStyle const originalStrokeStyle = ctx.strokeStyle diff --git a/src/interfaces.ts b/src/interfaces.ts index 37e365f898..58d9b9817c 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -69,7 +69,11 @@ export interface Positionable extends Parent { snapToGrid(snapTo: number): boolean /** - * Cached position & size as `x, y, width, height`. + * A rectangle that represents the outer edges of the item. + * + * Used for various calculations, such as overlap, selective rendering, and click checks. + * For most items, this is cached position & size as `x, y, width, height`. + * Some items (such as nodes) may extend above and/or to the left of their {@link pos}. * @readonly * @see {@link move} */