From 93d3578106facb43d3f92d6b6441422f8dafe404 Mon Sep 17 00:00:00 2001 From: Alexander Brown <448862+DrJKL@users.noreply.github.com> Date: Tue, 27 Jan 2026 00:32:07 -0800 Subject: [PATCH] refactor: extract litegraph types to dedicated types directory - Create src/lib/litegraph/src/types/ with pure type definitions - Extract geometry, id, and slot base types - Create type-only barrel for importing without runtime deps Amp-Thread-ID: https://ampcode.com/threads/T-019bfe73-6a29-7638-8160-8de515af8707 Co-authored-by: Amp --- src/lib/litegraph/src/LGraphNode.ts | 7 ++-- src/lib/litegraph/src/LLink.ts | 9 +++-- src/lib/litegraph/src/Reroute.ts | 8 ++-- src/lib/litegraph/src/interfaces.ts | 50 ++++++++++++------------- src/lib/litegraph/src/types/geometry.ts | 29 ++++++++++++++ src/lib/litegraph/src/types/ids.ts | 8 ++++ src/lib/litegraph/src/types/index.ts | 14 +++++++ src/lib/litegraph/src/types/slots.ts | 37 ++++++++++++++++++ 8 files changed, 125 insertions(+), 37 deletions(-) create mode 100644 src/lib/litegraph/src/types/geometry.ts create mode 100644 src/lib/litegraph/src/types/ids.ts create mode 100644 src/lib/litegraph/src/types/index.ts create mode 100644 src/lib/litegraph/src/types/slots.ts diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 1ff503cdc..c5f98e054 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -18,7 +18,7 @@ import { LGraphButton } from './LGraphButton' import type { LGraphButtonOptions } from './LGraphButton' import { LGraphCanvas } from './LGraphCanvas' import { LLink } from './LLink' -import type { Reroute, RerouteId } from './Reroute' +import type { Reroute } from './Reroute' import { getNodeInputOnPos, getNodeOutputOnPos } from './canvas/measureSlots' import type { IDrawBoundingOptions } from './draw' import { NullGraphError } from './infrastructure/NullGraphError' @@ -73,6 +73,7 @@ import { RenderShape, TitleMode } from './types/globalEnums' +import type { NodeId, RerouteId } from './types/ids' import type { ISerialisedNode, SubgraphIO } from './types/serialisation' import type { IBaseWidget, @@ -88,9 +89,9 @@ import { BaseWidget } from './widgets/BaseWidget' import { toConcreteWidget } from './widgets/widgetMap' import type { WidgetTypeMap } from './widgets/widgetMap' -// #region Types +export type { NodeId } from './types/ids' -export type NodeId = number | string +// #region Types export type NodeProperty = string | number | boolean | object diff --git a/src/lib/litegraph/src/LLink.ts b/src/lib/litegraph/src/LLink.ts index 6ca8832a1..e93db30c9 100644 --- a/src/lib/litegraph/src/LLink.ts +++ b/src/lib/litegraph/src/LLink.ts @@ -7,8 +7,8 @@ import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations' import { LayoutSource } from '@/renderer/core/layout/types' -import type { LGraphNode, NodeId } from './LGraphNode' -import type { Reroute, RerouteId } from './Reroute' +import type { LGraphNode } from './LGraphNode' +import type { Reroute } from './Reroute' import type { CanvasColour, INodeInputSlot, @@ -19,11 +19,12 @@ import type { Point, ReadonlyLinkNetwork } from './interfaces' +import type { LinkId, NodeId, RerouteId } from './types/ids' import type { Serialisable, SerialisableLLink } from './types/serialisation' -const layoutMutations = useLayoutMutations() +export type { LinkId } from './types/ids' -export type LinkId = number +const layoutMutations = useLayoutMutations() export type SerialisedLLinkArray = [ id: LinkId, diff --git a/src/lib/litegraph/src/Reroute.ts b/src/lib/litegraph/src/Reroute.ts index 6436df532..3732b13a2 100644 --- a/src/lib/litegraph/src/Reroute.ts +++ b/src/lib/litegraph/src/Reroute.ts @@ -2,9 +2,8 @@ import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMuta import { LayoutSource } from '@/renderer/core/layout/types' import { LGraphBadge } from './LGraphBadge' -import type { LGraphNode, NodeId } from './LGraphNode' +import type { LGraphNode } from './LGraphNode' import { LLink } from './LLink' -import type { LinkId } from './LLink' import type { CanvasColour, INodeInputSlot, @@ -17,11 +16,12 @@ import type { ReadonlyLinkNetwork } from './interfaces' import { distance, isPointInRect } from './measure' +import type { LinkId, NodeId, RerouteId } from './types/ids' import type { Serialisable, SerialisableReroute } from './types/serialisation' -const layoutMutations = useLayoutMutations() +export type { RerouteId } from './types/ids' -export type RerouteId = number +const layoutMutations = useLayoutMutations() /** The input or output slot that an incomplete reroute link is connected to. */ export interface FloatingRerouteSlot { diff --git a/src/lib/litegraph/src/interfaces.ts b/src/lib/litegraph/src/interfaces.ts index 9df7339af..a21eb8ef3 100644 --- a/src/lib/litegraph/src/interfaces.ts +++ b/src/lib/litegraph/src/interfaces.ts @@ -3,15 +3,36 @@ import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events' import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets' import type { ContextMenu } from './ContextMenu' -import type { LGraphNode, NodeId } from './LGraphNode' -import type { LLink, LinkId } from './LLink' -import type { Reroute, RerouteId } from './Reroute' +import type { LGraphNode } from './LGraphNode' +import type { LLink } from './LLink' +import type { Reroute } from './Reroute' import type { SubgraphInput } from './subgraph/SubgraphInput' import type { SubgraphInputNode } from './subgraph/SubgraphInputNode' import type { SubgraphOutputNode } from './subgraph/SubgraphOutputNode' import type { LinkDirection, RenderShape } from './types/globalEnums' +import type { + CanvasColour, + LinkId, + NodeId, + Point, + ReadOnlyRect, + RerouteId, + Size +} from './types/index' import type { IBaseWidget } from './types/widgets' +export type { + CanvasColour, + LinkId, + NodeId, + Point, + ReadOnlyRect, + ReadOnlyTypedArray, + Rect, + RerouteId, + Size +} from './types/index' + export type Dictionary = { [key: string]: T } /** Allows all properties to be null. The same as `Partial`, but adds null instead of undefined. */ @@ -46,8 +67,6 @@ export type SharedIntersection = { [P in keyof T2 as P extends keyof T1 ? P : never]: T2[P] } -export type CanvasColour = string | CanvasGradient | CanvasPattern - /** * Any object that has a {@link boundingRect}. */ @@ -226,27 +245,6 @@ export interface IFoundSlot extends IInputOrOutput { link_pos: Point } -/** A point represented as `[x, y]` co-ordinates */ -export type Point = [x: number, y: number] - -/** A size represented as `[width, height]` */ -export type Size = [width: number, height: number] - -/** A rectangle starting at top-left coordinates `[x, y, width, height]` */ -export type Rect = - | [x: number, y: number, width: number, height: number] - | Float64Array - -/** A rectangle starting at top-left coordinates `[x, y, width, height]` that will not be modified */ -export type ReadOnlyRect = - | readonly [x: number, y: number, width: number, height: number] - | ReadOnlyTypedArray - -export type ReadOnlyTypedArray = Omit< - Readonly, - 'fill' | 'copyWithin' | 'reverse' | 'set' | 'sort' | 'subarray' -> - /** Union of property names that are of type Match */ type KeysOfType = Exclude< { [P in keyof T]: T[P] extends Match ? P : never }[keyof T], diff --git a/src/lib/litegraph/src/types/geometry.ts b/src/lib/litegraph/src/types/geometry.ts new file mode 100644 index 000000000..b3eaf2d4f --- /dev/null +++ b/src/lib/litegraph/src/types/geometry.ts @@ -0,0 +1,29 @@ +/** A point represented as `[x, y]` co-ordinates */ +export type Point = [x: number, y: number] + +/** A size represented as `[width, height]` */ +export type Size = [width: number, height: number] + +/** A rectangle starting at top-left coordinates `[x, y, width, height]` */ +export type Rect = + | [x: number, y: number, width: number, height: number] + | Float64Array + +/** A rectangle starting at top-left coordinates `[x, y, width, height]` that will not be modified */ +export type ReadOnlyRect = + | readonly [x: number, y: number, width: number, height: number] + | ReadOnlyTypedArray + +export type ReadOnlyTypedArray = Omit< + Readonly, + 'fill' | 'copyWithin' | 'reverse' | 'set' | 'sort' | 'subarray' +> + +/** A 2D vector as `[x, y]` */ +export type Vector2 = [x: number, y: number] + +/** A 4D vector as `[x, y, z, w]` */ +export type Vector4 = [x: number, y: number, z: number, w: number] + +/** Margin values as `[top, right, bottom, left]` */ +export type Margin = [top: number, right: number, bottom: number, left: number] diff --git a/src/lib/litegraph/src/types/ids.ts b/src/lib/litegraph/src/types/ids.ts new file mode 100644 index 000000000..acc3275d0 --- /dev/null +++ b/src/lib/litegraph/src/types/ids.ts @@ -0,0 +1,8 @@ +/** Unique identifier for a node in the graph */ +export type NodeId = number | string + +/** Unique identifier for a link between nodes */ +export type LinkId = number + +/** Unique identifier for a reroute point on a link */ +export type RerouteId = number diff --git a/src/lib/litegraph/src/types/index.ts b/src/lib/litegraph/src/types/index.ts new file mode 100644 index 000000000..a86e8d8a1 --- /dev/null +++ b/src/lib/litegraph/src/types/index.ts @@ -0,0 +1,14 @@ +export type { + Margin, + Point, + ReadOnlyRect, + ReadOnlyTypedArray, + Rect, + Size, + Vector2, + Vector4 +} from './geometry' + +export type { LinkId, NodeId, RerouteId } from './ids' + +export type { CanvasColour, INodeSlotBase, ISlotType } from './slots' diff --git a/src/lib/litegraph/src/types/slots.ts b/src/lib/litegraph/src/types/slots.ts new file mode 100644 index 000000000..66abc2c98 --- /dev/null +++ b/src/lib/litegraph/src/types/slots.ts @@ -0,0 +1,37 @@ +import type { LinkDirection, RenderShape } from './globalEnums' +import type { Point, ReadOnlyRect } from './geometry' + +/** Union type for slot connection types - can be a string name or a numeric type code */ +export type ISlotType = string | number + +/** Colour type for canvas elements */ +export type CanvasColour = string | CanvasGradient | CanvasPattern + +/** + * Base interface for node slots (inputs and outputs). + * Contains common properties shared between input and output slots. + */ +export interface INodeSlotBase { + /** The unique name of the slot */ + name: string + /** The type of the slot, used for connection compatibility */ + type: ISlotType + /** Direction of the link connection */ + dir?: LinkDirection + /** Whether the slot can be removed */ + removable?: boolean + /** Visual shape of the slot */ + shape?: RenderShape + /** Color when disconnected */ + color_off?: CanvasColour + /** Color when connected */ + color_on?: CanvasColour + /** Whether the slot is locked from modifications */ + locked?: boolean + /** Whether the slot name is locked from changes */ + nameLocked?: boolean + /** Position of the slot relative to the node */ + pos?: Point + /** Bounding rectangle of the slot for hit detection */ + boundingRect: ReadOnlyRect +}