Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/lib/litegraph/src/ContextMenu.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { LiteGraphInternal } from './LiteGraphInternal'
import type {
ContextMenuDivElement,
IContextMenuOptions,
IContextMenuValue
} from './interfaces'
import { LiteGraph } from './litegraph'

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

if (LiteGraph.context_menu_scaling && options.scale) {
if (LiteGraphInternal.context_menu_scaling && options.scale) {
root.style.transform = `scale(${Math.round(options.scale * 4) * 0.25})`
}
}
Expand Down Expand Up @@ -355,7 +355,7 @@ export class ContextMenu<TValue = unknown> {
) {
ContextMenu.trigger(
this.parentMenu.root,
`${LiteGraph.pointerevents_method}leave`,
`${LiteGraphInternal.pointerevents_method}leave`,
e
)
}
Expand Down
96 changes: 58 additions & 38 deletions src/lib/litegraph/src/LGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { LGraphCanvas } from './LGraphCanvas'
import { LGraphGroup } from './LGraphGroup'
import { LGraphNode, type NodeId } from './LGraphNode'
import { LLink, type LinkId } from './LLink'
import { LiteGraphInternal } from './LiteGraphInternal'
import { MapProxyHandler } from './MapProxyHandler'
import { Reroute, type RerouteId } from './Reroute'
import { CustomEventTarget } from './infrastructure/CustomEventTarget'
Expand All @@ -34,7 +35,7 @@ import type {
Positionable,
Size
} from './interfaces'
import { LiteGraph, SubgraphNode } from './litegraph'
import { SubgraphNode } from './litegraph'
import {
alignOutsideContainer,
alignToContainer,
Expand Down Expand Up @@ -274,7 +275,7 @@ export class LGraph
* @param o data from previous serialization [optional]
*/
constructor(o?: ISerialisedGraph | SerialisableGraph) {
if (LiteGraph.debug) console.log('Graph created')
if (LiteGraphInternal.debug) console.log('Graph created')

/** @see MapProxyHandler */
const links = this._links
Expand Down Expand Up @@ -424,7 +425,7 @@ export class LGraph
this.sendEventToAllNodes('onStart')

// launch
this.starttime = LiteGraph.getTime()
this.starttime = LiteGraphInternal.getTime()
this.last_update_time = this.starttime
interval ||= 0

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

const start = LiteGraph.getTime()
const start = LiteGraphInternal.getTime()
this.globaltime = 0.001 * (start - this.starttime)

const nodes = this._nodes_executable || this._nodes
Expand Down Expand Up @@ -530,14 +531,15 @@ export class LGraph
this.errors_in_execution = false
} catch (error) {
this.errors_in_execution = true
if (LiteGraph.throw_errors) throw error
if (LiteGraphInternal.throw_errors) throw error

if (LiteGraph.debug) console.log('Error during execution:', error)
if (LiteGraphInternal.debug)
console.log('Error during execution:', error)
this.stop()
}
}

const now = LiteGraph.getTime()
const now = LiteGraphInternal.getTime()
let elapsed = now - start
if (elapsed == 0) elapsed = 1

Expand Down Expand Up @@ -662,7 +664,7 @@ export class LGraph
L.push(M[i])
}

if (L.length != this._nodes.length && LiteGraph.debug)
if (L.length != this._nodes.length && LiteGraphInternal.debug)
console.warn('something went wrong, nodes missing')

/** Ensure type is set */
Expand Down Expand Up @@ -718,16 +720,21 @@ export class LGraph
if (!column) continue

let max_size = 100
let y = margin + LiteGraph.NODE_TITLE_HEIGHT
let y = margin + LiteGraphInternal.NODE_TITLE_HEIGHT
for (const node of column) {
node.pos[0] = layout == LiteGraph.VERTICAL_LAYOUT ? y : x
node.pos[1] = layout == LiteGraph.VERTICAL_LAYOUT ? x : y
const max_size_index = layout == LiteGraph.VERTICAL_LAYOUT ? 1 : 0
node.pos[0] = layout == LiteGraphInternal.VERTICAL_LAYOUT ? y : x
node.pos[1] = layout == LiteGraphInternal.VERTICAL_LAYOUT ? x : y
const max_size_index =
layout == LiteGraphInternal.VERTICAL_LAYOUT ? 1 : 0
if (node.size[max_size_index] > max_size) {
max_size = node.size[max_size_index]
}
const node_size_index = layout == LiteGraph.VERTICAL_LAYOUT ? 0 : 1
y += node.size[node_size_index] + margin + LiteGraph.NODE_TITLE_HEIGHT
const node_size_index =
layout == LiteGraphInternal.VERTICAL_LAYOUT ? 0 : 1
y +=
node.size[node_size_index] +
margin +
LiteGraphInternal.NODE_TITLE_HEIGHT
}
x += max_size + margin
}
Expand Down Expand Up @@ -831,7 +838,7 @@ export class LGraph
const { state } = this

// Ensure created items are snapped
if (LiteGraph.alwaysSnapToGrid) {
if (LiteGraphInternal.alwaysSnapToGrid) {
const snapTo = this.getSnapToGridSize()
if (snapTo) node.snapToGrid(snapTo)
}
Expand All @@ -856,16 +863,18 @@ export class LGraph
console.warn(
'LiteGraph: there is already a node with this ID, changing it'
)
node.id = LiteGraph.use_uuids ? LiteGraph.uuidv4() : ++state.lastNodeId
node.id = LiteGraphInternal.use_uuids
? LiteGraphInternal.uuidv4()
: ++state.lastNodeId
}

if (this._nodes.length >= LiteGraph.MAX_NUMBER_OF_NODES) {
if (this._nodes.length >= LiteGraphInternal.MAX_NUMBER_OF_NODES) {
throw 'LiteGraph: max number of nodes in a graph reached'
}

// give him an id
if (LiteGraph.use_uuids) {
if (node.id == null || node.id == -1) node.id = LiteGraph.uuidv4()
if (LiteGraphInternal.use_uuids) {
if (node.id == null || node.id == -1) node.id = LiteGraphInternal.uuidv4()
} else {
if (node.id == null || node.id == -1) {
node.id = ++state.lastNodeId
Expand Down Expand Up @@ -1128,9 +1137,9 @@ export class LGraph
/**
* Snaps the provided items to a grid.
*
* Item positions are reounded to the nearest multiple of {@link LiteGraph.CANVAS_GRID_SIZE}.
* Item positions are reounded to the nearest multiple of {@link LiteGraphInternal.CANVAS_GRID_SIZE}.
*
* When {@link LiteGraph.alwaysSnapToGrid} is enabled
* When {@link LiteGraphInternal.alwaysSnapToGrid} is enabled
* and the grid size is falsy, a default of 1 is used.
* @param items The items to be snapped to the grid
* @todo Currently only snaps nodes.
Expand All @@ -1150,9 +1159,9 @@ export class LGraph
*/
getSnapToGridSize(): number {
// Default to 1 when always snapping
return LiteGraph.alwaysSnapToGrid
? LiteGraph.CANVAS_GRID_SIZE || 1
: LiteGraph.CANVAS_GRID_SIZE
return LiteGraphInternal.alwaysSnapToGrid
? LiteGraphInternal.CANVAS_GRID_SIZE || 1
: LiteGraphInternal.CANVAS_GRID_SIZE
}

/**
Expand All @@ -1164,11 +1173,11 @@ export class LGraph
checkNodeTypes() {
const { _nodes } = this
for (const [i, node] of _nodes.entries()) {
const ctor = LiteGraph.registered_node_types[node.type]
const ctor = LiteGraphInternal.registered_node_types[node.type]
if (node.constructor == ctor) continue

console.log('node being replaced by newer version:', node.type)
const newnode = LiteGraph.createNode(node.type)
const newnode = LiteGraphInternal.createNode(node.type)
if (!newnode) continue
_nodes[i] = newnode
newnode.configure(node.serialize())
Expand Down Expand Up @@ -1229,7 +1238,7 @@ export class LGraph

/* Called when something visually changed (not the graph!) */
change(): void {
if (LiteGraph.debug) {
if (LiteGraphInternal.debug) {
console.log('Graph changed')
}
this.canvasAction((c) => c.setDirty(true, true))
Expand Down Expand Up @@ -1579,9 +1588,13 @@ export class LGraph
})

// Create subgraph node object
const subgraphNode = LiteGraph.createNode(subgraph.id, subgraph.name, {
outputs: structuredClone(outputs)
})
const subgraphNode = LiteGraphInternal.createNode(
subgraph.id,
subgraph.name,
{
outputs: structuredClone(outputs)
}
)
if (!subgraphNode) throw new Error('Failed to create subgraph node')
for (let i = 0; i < inputs.length; i++) {
Object.assign(subgraphNode.inputs[i], inputs[i])
Expand All @@ -1598,7 +1611,7 @@ export class LGraph
)

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

// Add the subgraph node to the graph
this.add(subgraphNode)
Expand Down Expand Up @@ -1719,7 +1732,10 @@ export class LGraph
const movedNodes = multiClone(subgraphNode.subgraph.nodes)
const nodeIdMap = new Map<NodeId, NodeId>()
for (const n_info of movedNodes) {
const node = LiteGraph.createNode(String(n_info.type), n_info.title)
const node = LiteGraphInternal.createNode(
String(n_info.type),
n_info.title
)
if (!node) {
throw new Error('Node not found')
}
Expand Down Expand Up @@ -2027,7 +2043,7 @@ export class LGraph
definitions,
config,
extra,
version: LiteGraph.VERSION
version: LiteGraphInternal.VERSION
}
}

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

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

// Save scale and offset
const extra = { ...this.extra }
if (LiteGraph.saveViewportWithGraph) extra.ds = this.#getDragAndScale()
if (LiteGraphInternal.saveViewportWithGraph)
extra.ds = this.#getDragAndScale()
if (!extra.ds) delete extra.ds

const data: ReturnType<typeof this.asSerialisable> = {
Expand Down Expand Up @@ -2230,9 +2247,12 @@ export class LGraph
if (nodesData) {
for (const n_info of nodesData) {
// stored info
let node = LiteGraph.createNode(String(n_info.type), n_info.title)
let node = LiteGraphInternal.createNode(
String(n_info.type),
n_info.title
)
if (!node) {
if (LiteGraph.debug)
if (LiteGraphInternal.debug)
console.log('Node not found or has errors:', n_info.type)

// in case of error we create a replacement node to avoid losing info
Expand Down Expand Up @@ -2284,7 +2304,7 @@ export class LGraph
if (groupData) {
for (const data of groupData) {
// TODO: Search/remove these global object refs
const group = new LiteGraph.LGraphGroup()
const group = new LiteGraphInternal.LGraphGroup()
group.configure(data)
this.add(group)
}
Expand Down
Loading