Skip to content

Commit daa76ef

Browse files
committed
[refactor] Create internal module for litegraph types
- Extract internal litegraph types to LiteGraphInternal.ts - Remove circular dependencies between modules - Maintain backward compatibility with existing exports - Keep singleton pattern intact
1 parent 6317469 commit daa76ef

29 files changed

+527
-388
lines changed

src/lib/litegraph/src/ContextMenu.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { LiteGraphInternal } from './LiteGraphInternal'
12
import type {
23
ContextMenuDivElement,
34
IContextMenuOptions,
45
IContextMenuValue
56
} from './interfaces'
6-
import { LiteGraph } from './litegraph'
77

88
// TODO: Replace this pattern with something more modern.
99
export interface ContextMenu<TValue = unknown> {
@@ -182,7 +182,7 @@ export class ContextMenu<TValue = unknown> {
182182
root.style.left = `${left}px`
183183
root.style.top = `${top}px`
184184

185-
if (LiteGraph.context_menu_scaling && options.scale) {
185+
if (LiteGraphInternal.context_menu_scaling && options.scale) {
186186
root.style.transform = `scale(${Math.round(options.scale * 4) * 0.25})`
187187
}
188188
}
@@ -355,7 +355,7 @@ export class ContextMenu<TValue = unknown> {
355355
) {
356356
ContextMenu.trigger(
357357
this.parentMenu.root,
358-
`${LiteGraph.pointerevents_method}leave`,
358+
`${LiteGraphInternal.pointerevents_method}leave`,
359359
e
360360
)
361361
}

src/lib/litegraph/src/LGraph.ts

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { LGraphCanvas } from './LGraphCanvas'
1414
import { LGraphGroup } from './LGraphGroup'
1515
import { LGraphNode, type NodeId } from './LGraphNode'
1616
import { LLink, type LinkId } from './LLink'
17+
import { LiteGraphInternal } from './LiteGraphInternal'
1718
import { MapProxyHandler } from './MapProxyHandler'
1819
import { Reroute, type RerouteId } from './Reroute'
1920
import { CustomEventTarget } from './infrastructure/CustomEventTarget'
@@ -34,7 +35,7 @@ import type {
3435
Positionable,
3536
Size
3637
} from './interfaces'
37-
import { LiteGraph, SubgraphNode } from './litegraph'
38+
import { SubgraphNode } from './litegraph'
3839
import {
3940
alignOutsideContainer,
4041
alignToContainer,
@@ -274,7 +275,7 @@ export class LGraph
274275
* @param o data from previous serialization [optional]
275276
*/
276277
constructor(o?: ISerialisedGraph | SerialisableGraph) {
277-
if (LiteGraph.debug) console.log('Graph created')
278+
if (LiteGraphInternal.debug) console.log('Graph created')
278279

279280
/** @see MapProxyHandler */
280281
const links = this._links
@@ -424,7 +425,7 @@ export class LGraph
424425
this.sendEventToAllNodes('onStart')
425426

426427
// launch
427-
this.starttime = LiteGraph.getTime()
428+
this.starttime = LiteGraphInternal.getTime()
428429
this.last_update_time = this.starttime
429430
interval ||= 0
430431

@@ -486,7 +487,7 @@ export class LGraph
486487
runStep(num: number, do_not_catch_errors: boolean, limit?: number): void {
487488
num = num || 1
488489

489-
const start = LiteGraph.getTime()
490+
const start = LiteGraphInternal.getTime()
490491
this.globaltime = 0.001 * (start - this.starttime)
491492

492493
const nodes = this._nodes_executable || this._nodes
@@ -530,14 +531,15 @@ export class LGraph
530531
this.errors_in_execution = false
531532
} catch (error) {
532533
this.errors_in_execution = true
533-
if (LiteGraph.throw_errors) throw error
534+
if (LiteGraphInternal.throw_errors) throw error
534535

535-
if (LiteGraph.debug) console.log('Error during execution:', error)
536+
if (LiteGraphInternal.debug)
537+
console.log('Error during execution:', error)
536538
this.stop()
537539
}
538540
}
539541

540-
const now = LiteGraph.getTime()
542+
const now = LiteGraphInternal.getTime()
541543
let elapsed = now - start
542544
if (elapsed == 0) elapsed = 1
543545

@@ -662,7 +664,7 @@ export class LGraph
662664
L.push(M[i])
663665
}
664666

665-
if (L.length != this._nodes.length && LiteGraph.debug)
667+
if (L.length != this._nodes.length && LiteGraphInternal.debug)
666668
console.warn('something went wrong, nodes missing')
667669

668670
/** Ensure type is set */
@@ -718,16 +720,21 @@ export class LGraph
718720
if (!column) continue
719721

720722
let max_size = 100
721-
let y = margin + LiteGraph.NODE_TITLE_HEIGHT
723+
let y = margin + LiteGraphInternal.NODE_TITLE_HEIGHT
722724
for (const node of column) {
723-
node.pos[0] = layout == LiteGraph.VERTICAL_LAYOUT ? y : x
724-
node.pos[1] = layout == LiteGraph.VERTICAL_LAYOUT ? x : y
725-
const max_size_index = layout == LiteGraph.VERTICAL_LAYOUT ? 1 : 0
725+
node.pos[0] = layout == LiteGraphInternal.VERTICAL_LAYOUT ? y : x
726+
node.pos[1] = layout == LiteGraphInternal.VERTICAL_LAYOUT ? x : y
727+
const max_size_index =
728+
layout == LiteGraphInternal.VERTICAL_LAYOUT ? 1 : 0
726729
if (node.size[max_size_index] > max_size) {
727730
max_size = node.size[max_size_index]
728731
}
729-
const node_size_index = layout == LiteGraph.VERTICAL_LAYOUT ? 0 : 1
730-
y += node.size[node_size_index] + margin + LiteGraph.NODE_TITLE_HEIGHT
732+
const node_size_index =
733+
layout == LiteGraphInternal.VERTICAL_LAYOUT ? 0 : 1
734+
y +=
735+
node.size[node_size_index] +
736+
margin +
737+
LiteGraphInternal.NODE_TITLE_HEIGHT
731738
}
732739
x += max_size + margin
733740
}
@@ -831,7 +838,7 @@ export class LGraph
831838
const { state } = this
832839

833840
// Ensure created items are snapped
834-
if (LiteGraph.alwaysSnapToGrid) {
841+
if (LiteGraphInternal.alwaysSnapToGrid) {
835842
const snapTo = this.getSnapToGridSize()
836843
if (snapTo) node.snapToGrid(snapTo)
837844
}
@@ -856,16 +863,18 @@ export class LGraph
856863
console.warn(
857864
'LiteGraph: there is already a node with this ID, changing it'
858865
)
859-
node.id = LiteGraph.use_uuids ? LiteGraph.uuidv4() : ++state.lastNodeId
866+
node.id = LiteGraphInternal.use_uuids
867+
? LiteGraphInternal.uuidv4()
868+
: ++state.lastNodeId
860869
}
861870

862-
if (this._nodes.length >= LiteGraph.MAX_NUMBER_OF_NODES) {
871+
if (this._nodes.length >= LiteGraphInternal.MAX_NUMBER_OF_NODES) {
863872
throw 'LiteGraph: max number of nodes in a graph reached'
864873
}
865874

866875
// give him an id
867-
if (LiteGraph.use_uuids) {
868-
if (node.id == null || node.id == -1) node.id = LiteGraph.uuidv4()
876+
if (LiteGraphInternal.use_uuids) {
877+
if (node.id == null || node.id == -1) node.id = LiteGraphInternal.uuidv4()
869878
} else {
870879
if (node.id == null || node.id == -1) {
871880
node.id = ++state.lastNodeId
@@ -1128,9 +1137,9 @@ export class LGraph
11281137
/**
11291138
* Snaps the provided items to a grid.
11301139
*
1131-
* Item positions are reounded to the nearest multiple of {@link LiteGraph.CANVAS_GRID_SIZE}.
1140+
* Item positions are reounded to the nearest multiple of {@link LiteGraphInternal.CANVAS_GRID_SIZE}.
11321141
*
1133-
* When {@link LiteGraph.alwaysSnapToGrid} is enabled
1142+
* When {@link LiteGraphInternal.alwaysSnapToGrid} is enabled
11341143
* and the grid size is falsy, a default of 1 is used.
11351144
* @param items The items to be snapped to the grid
11361145
* @todo Currently only snaps nodes.
@@ -1150,9 +1159,9 @@ export class LGraph
11501159
*/
11511160
getSnapToGridSize(): number {
11521161
// Default to 1 when always snapping
1153-
return LiteGraph.alwaysSnapToGrid
1154-
? LiteGraph.CANVAS_GRID_SIZE || 1
1155-
: LiteGraph.CANVAS_GRID_SIZE
1162+
return LiteGraphInternal.alwaysSnapToGrid
1163+
? LiteGraphInternal.CANVAS_GRID_SIZE || 1
1164+
: LiteGraphInternal.CANVAS_GRID_SIZE
11561165
}
11571166

11581167
/**
@@ -1164,11 +1173,11 @@ export class LGraph
11641173
checkNodeTypes() {
11651174
const { _nodes } = this
11661175
for (const [i, node] of _nodes.entries()) {
1167-
const ctor = LiteGraph.registered_node_types[node.type]
1176+
const ctor = LiteGraphInternal.registered_node_types[node.type]
11681177
if (node.constructor == ctor) continue
11691178

11701179
console.log('node being replaced by newer version:', node.type)
1171-
const newnode = LiteGraph.createNode(node.type)
1180+
const newnode = LiteGraphInternal.createNode(node.type)
11721181
if (!newnode) continue
11731182
_nodes[i] = newnode
11741183
newnode.configure(node.serialize())
@@ -1229,7 +1238,7 @@ export class LGraph
12291238

12301239
/* Called when something visually changed (not the graph!) */
12311240
change(): void {
1232-
if (LiteGraph.debug) {
1241+
if (LiteGraphInternal.debug) {
12331242
console.log('Graph changed')
12341243
}
12351244
this.canvasAction((c) => c.setDirty(true, true))
@@ -1579,9 +1588,13 @@ export class LGraph
15791588
})
15801589

15811590
// Create subgraph node object
1582-
const subgraphNode = LiteGraph.createNode(subgraph.id, subgraph.name, {
1583-
outputs: structuredClone(outputs)
1584-
})
1591+
const subgraphNode = LiteGraphInternal.createNode(
1592+
subgraph.id,
1593+
subgraph.name,
1594+
{
1595+
outputs: structuredClone(outputs)
1596+
}
1597+
)
15851598
if (!subgraphNode) throw new Error('Failed to create subgraph node')
15861599
for (let i = 0; i < inputs.length; i++) {
15871600
Object.assign(subgraphNode.inputs[i], inputs[i])
@@ -1598,7 +1611,7 @@ export class LGraph
15981611
)
15991612

16001613
//Correct for title height. It's included in bounding box, but not _posSize
1601-
subgraphNode.pos[1] += LiteGraph.NODE_TITLE_HEIGHT / 2
1614+
subgraphNode.pos[1] += LiteGraphInternal.NODE_TITLE_HEIGHT / 2
16021615

16031616
// Add the subgraph node to the graph
16041617
this.add(subgraphNode)
@@ -1719,7 +1732,10 @@ export class LGraph
17191732
const movedNodes = multiClone(subgraphNode.subgraph.nodes)
17201733
const nodeIdMap = new Map<NodeId, NodeId>()
17211734
for (const n_info of movedNodes) {
1722-
const node = LiteGraph.createNode(String(n_info.type), n_info.title)
1735+
const node = LiteGraphInternal.createNode(
1736+
String(n_info.type),
1737+
n_info.title
1738+
)
17231739
if (!node) {
17241740
throw new Error('Node not found')
17251741
}
@@ -2027,7 +2043,7 @@ export class LGraph
20272043
definitions,
20282044
config,
20292045
extra,
2030-
version: LiteGraph.VERSION
2046+
version: LiteGraphInternal.VERSION
20312047
}
20322048
}
20332049

@@ -2052,7 +2068,7 @@ export class LGraph
20522068
const { id, revision, config, state } = this
20532069

20542070
const nodeList =
2055-
!LiteGraph.use_uuids && options?.sortNodes
2071+
!LiteGraphInternal.use_uuids && options?.sortNodes
20562072
? // @ts-expect-error If LiteGraph.use_uuids is false, ids are numbers.
20572073
[...this._nodes].sort((a, b) => a.id - b.id)
20582074
: this._nodes
@@ -2072,7 +2088,8 @@ export class LGraph
20722088

20732089
// Save scale and offset
20742090
const extra = { ...this.extra }
2075-
if (LiteGraph.saveViewportWithGraph) extra.ds = this.#getDragAndScale()
2091+
if (LiteGraphInternal.saveViewportWithGraph)
2092+
extra.ds = this.#getDragAndScale()
20762093
if (!extra.ds) delete extra.ds
20772094

20782095
const data: ReturnType<typeof this.asSerialisable> = {
@@ -2230,9 +2247,12 @@ export class LGraph
22302247
if (nodesData) {
22312248
for (const n_info of nodesData) {
22322249
// stored info
2233-
let node = LiteGraph.createNode(String(n_info.type), n_info.title)
2250+
let node = LiteGraphInternal.createNode(
2251+
String(n_info.type),
2252+
n_info.title
2253+
)
22342254
if (!node) {
2235-
if (LiteGraph.debug)
2255+
if (LiteGraphInternal.debug)
22362256
console.log('Node not found or has errors:', n_info.type)
22372257

22382258
// in case of error we create a replacement node to avoid losing info
@@ -2284,7 +2304,7 @@ export class LGraph
22842304
if (groupData) {
22852305
for (const data of groupData) {
22862306
// TODO: Search/remove these global object refs
2287-
const group = new LiteGraph.LGraphGroup()
2307+
const group = new LiteGraphInternal.LGraphGroup()
22882308
group.configure(data)
22892309
this.add(group)
22902310
}

0 commit comments

Comments
 (0)