diff --git a/.fallow-baselines/dead-code.json b/.fallow-baselines/dead-code.json index 65b24e4bcf7..59f0274cf05 100644 --- a/.fallow-baselines/dead-code.json +++ b/.fallow-baselines/dead-code.json @@ -27,26 +27,6 @@ "src/composables/sidebarTabs/useJobHistorySidebarTab.ts->src/stores/workspace/sidebarTabStore.ts", "src/composables/usePaste.ts->src/scripts/app.ts", "src/extensions/core/load3d/Load3dUtils.ts->src/scripts/app.ts", - "src/lib/litegraph/src/ContextMenu.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/ContextMenu.ts->src/lib/litegraph/src/LiteGraphGlobal.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/DragAndScale.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/DragAndScale.ts->src/lib/litegraph/src/LGraphCanvas.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/DragAndScale.ts->src/lib/litegraph/src/LiteGraphGlobal.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/LGraph.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/LGraphCanvas.ts->src/lib/litegraph/src/LGraphGroup.ts", - "src/lib/litegraph/src/LGraphCanvas.ts->src/lib/litegraph/src/LGraphNode.ts", - "src/lib/litegraph/src/LGraphCanvas.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/LGraphGroup.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/LGraphNode.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/LGraphNode.ts->src/lib/litegraph/src/utils/collections.ts", - "src/lib/litegraph/src/Reroute.ts->src/lib/litegraph/src/litegraph.ts", - "src/lib/litegraph/src/litegraph.ts->src/lib/litegraph/src/node/NodeInputSlot.ts", - "src/lib/litegraph/src/litegraph.ts->src/lib/litegraph/src/node/NodeOutputSlot.ts", - "src/lib/litegraph/src/litegraph.ts->src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts", - "src/lib/litegraph/src/litegraph.ts->src/lib/litegraph/src/subgraph/SubgraphNode.ts", - "src/lib/litegraph/src/litegraph.ts->src/lib/litegraph/src/subgraph/subgraphUtils.ts", - "src/lib/litegraph/src/litegraph.ts->src/lib/litegraph/src/widgets/LegacyWidget.ts", - "src/lib/litegraph/src/node/NodeInputSlot.ts->src/lib/litegraph/src/node/NodeSlot.ts", "src/platform/cloud/subscription/composables/useSubscriptionDialog.ts->src/services/dialogService.ts", "src/platform/missingMedia/missingMediaStore.ts->src/scripts/app.ts", "src/platform/missingModel/missingModelStore.ts->src/scripts/app.ts", @@ -602,14 +582,27 @@ "unresolved_imports": ["index.html:./materialdesignicons.min.css"], "unlisted_dependencies": [], "duplicate_exports": [ + "BaseLGraph|src/lib/litegraph/src/LGraph.ts|src/lib/litegraph/src/types/entityRegistry.ts", "Bounds|src/renderer/core/layout/types.ts|src/renderer/core/spatial/QuadTree.ts", "ComfyDialog|src/scripts/ui.ts|src/scripts/ui/dialog.ts", + "ContextMenu|src/lib/litegraph/src/ContextMenu.ts|src/lib/litegraph/src/ContextMenu.ts|src/lib/litegraph/src/types/entityRegistry.ts", "Direction|src/lib/litegraph/src/interfaces.ts|src/renderer/core/canvas/pathRenderer.ts", + "GraphOrSubgraph|src/lib/litegraph/src/LGraph.ts|src/lib/litegraph/src/types/entityRegistry.ts", + "IGraphGroupFlags|src/lib/litegraph/src/LGraphGroup.ts|src/lib/litegraph/src/types/entityRegistry.ts", "JobListItem|src/composables/queue/useJobList.ts|src/platform/remote/comfyui/jobs/jobTypes.ts", + "LGraphCanvas|src/lib/litegraph/src/LGraphCanvas.ts|src/lib/litegraph/src/LGraphCanvas.ts|src/lib/litegraph/src/types/entityRegistry.ts", + "LGraphConfig|src/lib/litegraph/src/LGraph.ts|src/lib/litegraph/src/types/entityRegistry.ts", + "LGraphExtra|src/lib/litegraph/src/LGraph.ts|src/lib/litegraph/src/types/entityRegistry.ts", + "LGraphNode|src/lib/litegraph/src/LGraphNode.ts|src/lib/litegraph/src/LGraphNode.ts|src/lib/litegraph/src/types/entityRegistry.ts", "ModelFile|src/platform/assets/schemas/assetSchema.ts|src/platform/workflow/validation/schemas/workflowSchema.ts", "PendingInvite|src/platform/workspace/api/workspaceApi.ts|src/platform/workspace/stores/teamWorkspaceStore.ts", "Point|src/lib/litegraph/src/interfaces.ts|src/renderer/core/canvas/pathRenderer.ts|src/renderer/core/layout/types.ts", + "RenderLink|src/lib/litegraph/src/canvas/RenderLink.ts|src/lib/litegraph/src/types/entityRegistry.ts", + "ResolvedConnection|src/lib/litegraph/src/LLink.ts|src/lib/litegraph/src/types/entityRegistry.ts", + "SerialisedLLinkArray|src/lib/litegraph/src/LLink.ts|src/lib/litegraph/src/types/entityRegistry.ts", "Size|src/lib/litegraph/src/interfaces.ts|src/renderer/core/layout/types.ts", + "SlotTypeDefaultNodeOpts|src/lib/litegraph/src/LiteGraphGlobal.ts|src/lib/litegraph/src/types/entityRegistry.ts", + "SubgraphId|src/lib/litegraph/src/LGraph.ts|src/lib/litegraph/src/types/entityRegistry.ts", "User|src/schemas/apiSchema.ts|src/stores/userStore.ts", "WidgetValue|src/types/simplifiedWidget.ts|src/utils/widgetUtil.ts", "WorkspaceWithRole|src/platform/workspace/api/workspaceApi.ts|src/platform/workspace/workspaceTypes.ts", diff --git a/docs/adr/TYPES-LITEGRAPH-0044-exact-entity-types-without-import-cycles.md b/docs/adr/TYPES-LITEGRAPH-0044-exact-entity-types-without-import-cycles.md new file mode 100644 index 00000000000..6293bace018 --- /dev/null +++ b/docs/adr/TYPES-LITEGRAPH-0044-exact-entity-types-without-import-cycles.md @@ -0,0 +1,52 @@ +# ADR-TYPES-LITEGRAPH-0044: Exact entity types without import cycles + +Date: 2026-10-11 + +## Status + +Proposed + +## Context + +Litegraph's runtime imports are acyclic, but its public types are mutually +recursive. A node refers to its graph, and a graph refers to its nodes. +Importing these types from their implementation modules recreates the module +cycles. Replacing a concrete class with a public-member-only interface is not +compatible: TypeScript uses private and protected members when checking class +assignability, and extensions subclass these classes. + +## Decision + +Use same-name interfaces in `types/entityRegistry.ts`. Each implementation +augments its interface to extend the exact concrete type through a local alias. +Keep union, tuple, and primitive aliases in the merged +`LitegraphEntityRegistry` interface because an interface cannot preserve those +shapes by extending them. Existing public exports retain their names. + +This removes module-import cycles, not recursive type relationships or runtime +coupling. It does not add entity members, change layout mutation ownership, or +replace the dedicated stores required by ADR-ECS-0008 and ADR-CRDT-LAYOUT-0003. +All type providers must remain reachable from the public litegraph barrel's +TypeScript import graph. Runtime providers must remain statically reachable +from `main.ts` without type-only or dynamic-import edges. + +API Extractor does not reliably preserve local module augmentations. The types +build converts the leaf declarations into type re-exports of each provider's +exact alias, exports those aliases, and removes the internal augmentations +before bundling. The build derives the mapping from the source augmentations +rather than maintaining a second registry. The published package contains +ordinary declarations and does not require consumers to load our providers. + +## Alternatives and consequences + +- A structural contract layer would lose nominal compatibility or duplicate + hundreds of class members. It belongs to a separate extension API redesign. +- Merging the core class modules would combine unrelated responsibilities and + thousands of lines merely to reduce a graph metric. +- An indexed registry for every type works, but same-name interfaces avoid + indexed lookups for the class and object types that support inheritance. +- Source navigation now passes through the leaf interface. A partial source + program must include its providers; importing the supported barrel does so. +- Declaration publishing has an additional transformation. A compiler and + API Extractor integration test checks nominal, generic, and union types in + an isolated consumer. Source type assertions check the graph/node contracts. diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json index 5f91ff2f6a0..1e2f92b00c8 100644 --- a/oxlint-suppressions.json +++ b/oxlint-suppressions.json @@ -579,7 +579,7 @@ }, "src/lib/litegraph/src/litegraph.ts": { "comfy/no-reexports": { - "count": 54 + "count": 55 } }, "src/lib/litegraph/src/litegraphInstance.ts": { @@ -620,6 +620,16 @@ "count": 1 } }, + "src/lib/litegraph/src/subgraph/subgraphUtils.ts": { + "comfy/no-reexports": { + "count": 1 + } + }, + "src/lib/litegraph/src/types/widgets.ts": { + "comfy/no-reexports": { + "count": 1 + } + }, "src/lib/litegraph/src/utils/arrange.ts": { "comfy/no-positional-boolean-params": { "count": 1 @@ -1431,6 +1441,16 @@ "count": 36 } }, + "src/types/nodeState.ts": { + "comfy/no-reexports": { + "count": 1 + } + }, + "src/types/simplifiedWidget.ts": { + "comfy/no-reexports": { + "count": 1 + } + }, "src/types/slotId.test.ts": { "comfy/prefer-array-containing": { "count": 1 diff --git a/package.json b/package.json index dad59bb4200..b9dcea1f486 100644 --- a/package.json +++ b/package.json @@ -161,6 +161,7 @@ "@comfyorg/website": "workspace:*", "@eslint/js": "catalog:", "@intlify/eslint-plugin-vue-i18n": "catalog:", + "@microsoft/api-extractor": "catalog:", "@pinia/testing": "catalog:", "@playwright/test": "catalog:", "@sentry/vite-plugin": "catalog:", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ba5bbcc6146..372f195f791 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -72,6 +72,9 @@ catalogs: '@lucide/vue': specifier: ^1.17.0 version: 1.24.0 + '@microsoft/api-extractor': + specifier: 7.58.9 + version: 7.58.9 '@pinia/testing': specifier: ^2.0.1 version: 2.0.1 @@ -705,6 +708,9 @@ importers: '@intlify/eslint-plugin-vue-i18n': specifier: 'catalog:' version: 4.5.1(eslint@10.10.0(jiti@2.7.0))(jsonc-eslint-parser@3.1.0)(vue-eslint-parser@10.4.1(eslint@10.10.0(jiti@2.7.0)))(yaml-eslint-parser@2.1.0) + '@microsoft/api-extractor': + specifier: 'catalog:' + version: 7.58.9(@types/node@25.9.5) '@pinia/testing': specifier: 'catalog:' version: 2.0.1(pinia@4.0.2(@vue/devtools-api@8.1.5)(typescript@6.0.3)(vue@3.5.42(typescript@6.0.3))) @@ -11821,7 +11827,6 @@ snapshots: '@rushstack/node-core-library': 5.23.1(@types/node@25.9.5) transitivePeerDependencies: - '@types/node' - optional: true '@microsoft/api-extractor@7.58.9(@types/node@25.9.5)': dependencies: @@ -11840,7 +11845,6 @@ snapshots: typescript: 5.9.3 transitivePeerDependencies: - '@types/node' - optional: true '@microsoft/tsdoc-config@0.18.1': dependencies: @@ -11848,10 +11852,8 @@ snapshots: ajv: 8.20.0 jju: 1.4.0 resolve: 1.22.12 - optional: true - '@microsoft/tsdoc@0.16.0': - optional: true + '@microsoft/tsdoc@0.16.0': {} '@mixpanel/rrdom@2.0.0-alpha.18.5': dependencies: @@ -12434,18 +12436,15 @@ snapshots: semver: 7.7.4 optionalDependencies: '@types/node': 25.9.5 - optional: true '@rushstack/problem-matcher@0.2.1(@types/node@25.9.5)': optionalDependencies: '@types/node': 25.9.5 - optional: true '@rushstack/rig-package@0.7.3': dependencies: jju: 1.4.0 resolve: 1.22.12 - optional: true '@rushstack/terminal@0.24.0(@types/node@25.9.5)': dependencies: @@ -12454,7 +12453,6 @@ snapshots: supports-color: 8.1.1 optionalDependencies: '@types/node': 25.9.5 - optional: true '@rushstack/ts-command-line@5.3.10(@types/node@25.9.5)': dependencies: @@ -12464,7 +12462,6 @@ snapshots: string-argv: 0.3.2 transitivePeerDependencies: - '@types/node' - optional: true '@segment/analytics.js-video-plugins@0.2.1': dependencies: @@ -13052,8 +13049,7 @@ snapshots: tslib: 2.8.1 optional: true - '@types/argparse@1.0.38': - optional: true + '@types/argparse@1.0.38': {} '@types/aria-query@5.0.4': {} @@ -13952,7 +13948,6 @@ snapshots: argparse@1.0.10: dependencies: sprintf-js: 1.0.3 - optional: true argparse@2.0.1: {} @@ -15669,8 +15664,7 @@ snapshots: import-fresh@4.0.0: {} - import-lazy@4.0.0: - optional: true + import-lazy@4.0.0: {} import-meta-resolve@4.2.0: {} @@ -15881,8 +15875,7 @@ snapshots: jiti@2.7.0: {} - jju@1.4.0: - optional: true + jju@1.4.0: {} joi@18.2.3: dependencies: @@ -18065,8 +18058,7 @@ snapshots: speakingurl@14.0.1: {} - sprintf-js@1.0.3: - optional: true + sprintf-js@1.0.3: {} sqids@0.3.0: {} @@ -18252,7 +18244,6 @@ snapshots: supports-color@8.1.1: dependencies: has-flag: 4.0.0 - optional: true supports-hyperlinks@4.5.0: dependencies: diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index dcf388ac377..380074a23e5 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -165,6 +165,7 @@ catalog: '@comfyorg/comfy-multi-player': 0.3.10 typescript-native-bridge: 6.0.3-bridge.17.tsgo.7.0.2 dependency-cruiser: ^18.5.0 + '@microsoft/api-extractor': 7.58.9 cleanupUnusedCatalogs: true diff --git a/scripts/litegraphDeclarations.test.ts b/scripts/litegraphDeclarations.test.ts new file mode 100644 index 00000000000..9d32ce31552 --- /dev/null +++ b/scripts/litegraphDeclarations.test.ts @@ -0,0 +1,155 @@ +import { Extractor, ExtractorConfig } from '@microsoft/api-extractor' +import { + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync +} from 'node:fs' +import { tmpdir } from 'node:os' +import { dirname, join } from 'node:path' +import ts from 'typescript' +import { expect, it, onTestFinished } from 'vitest' + +import { createLitegraphDeclarationTransform } from './litegraphDeclarations' + +function writeFiles(root: string, files: Record) { + for (const [file, text] of Object.entries(files)) { + mkdirSync(dirname(join(root, file)), { recursive: true }) + writeFileSync(join(root, file), text) + } +} + +it('publishes exact nominal, generic, and union types without augmentation dependencies', () => { + const root = mkdtempSync(join(tmpdir(), 'litegraph-declarations-')) + onTestFinished(() => rmSync(root, { recursive: true, force: true })) + writeFiles(root, { + 'package.json': JSON.stringify({ name: 'type-consumer', version: '1.0.0' }), + 'types/entityRegistry.ts': ` + export interface Graph {} + export interface LitegraphEntityRegistry {} + export type Choice = LitegraphEntityRegistry['Choice'] + export interface Box<_T = unknown> {} + `, + 'Graph.ts': ` + import { Node } from './Node' + export class Graph { + private identity = 1 + nodes: Node[] = [] + add(node: Node): this { node.graph = this; return this } + } + type RegisteredGraph = Graph + type RegisteredChoice = 'first' | 'second' + declare module './types/entityRegistry' { + interface Graph extends RegisteredGraph {} + interface LitegraphEntityRegistry { Choice: RegisteredChoice } + } + `, + 'Node.ts': ` + import type { Graph, Box, Choice } from './types/entityRegistry' + export class Node { + graph: Graph | null = null + box?: Box + choice?: Choice + } + `, + 'Box.ts': ` + export class Box { + private identity = 2 + constructor(public value: T) {} + } + type RegisteredBox = Box + declare module './types/entityRegistry' { + interface Box<_T = unknown> extends RegisteredBox<_T> {} + } + `, + 'index.ts': `export { Graph } from './Graph' + export { Node } from './Node' + export { Box } from './Box'` + }) + const transform = createLitegraphDeclarationTransform(root) + const options: ts.CompilerOptions = { + target: ts.ScriptTarget.ESNext, + module: ts.ModuleKind.ESNext, + moduleResolution: ts.ModuleResolutionKind.Bundler, + strict: true, + skipLibCheck: true, + types: [], + declaration: true, + emitDeclarationOnly: true, + outDir: join(root, 'out') + } + const program = ts.createProgram([join(root, 'index.ts')], options) + expect( + ts + .getPreEmitDiagnostics(program) + .map((diagnostic) => diagnostic.messageText) + ).toEqual([]) + program.emit(undefined, (file, text) => { + writeFiles(dirname(file), { + [file.slice(dirname(file).length + 1)]: + transform(file, text)?.content ?? text + }) + }) + const entry = join(root, 'out/index.d.ts') + const result = Extractor.invoke( + ExtractorConfig.prepare({ + configObject: { + projectFolder: root, + mainEntryPointFilePath: entry, + compiler: { + overrideTsconfig: { + compilerOptions: { types: [], skipLibCheck: true }, + files: [entry] + } + }, + apiReport: { enabled: false }, + docModel: { enabled: false }, + tsdocMetadata: { enabled: false }, + dtsRollup: { + enabled: true, + untrimmedFilePath: join(root, 'bundle.d.ts') + } + }, + configObjectFullPath: join(root, 'api-extractor.json'), + packageJsonFullPath: join(root, 'package.json') + }), + { + localBuild: true, + messageCallback: (message) => { + message.handled = true + } + } + ) + expect(result.succeeded).toBe(true) + const bundle = readFileSync(join(root, 'bundle.d.ts'), 'utf8') + expect(bundle).not.toContain('entityRegistry') + rmSync(root, { recursive: true, force: true }) + writeFiles(root, { + 'bundle.d.ts': bundle, + 'consumer.ts': ` + import { Graph, Node, Box } from './bundle' + type Equal = (() => T extends A ? 1 : 2) extends (() => T extends B ? 1 : 2) ? true : false + type Check = T + export type GraphIdentity = Check> + export type BoxIdentity = Check | undefined>> + export type UnionIdentity = Check> + export type Nominality = Check> + const graph = new Graph() + const node = new Node() + node.graph = graph + export const concrete: Graph = node.graph + export const generic: Box = new Box('value') + ` + }) + const consumer = ts.createProgram([join(root, 'consumer.ts')], { + ...options, + skipLibCheck: false, + noEmit: true + }) + expect( + ts + .getPreEmitDiagnostics(consumer) + .map((diagnostic) => diagnostic.messageText) + ).toEqual([]) +}, 30_000) diff --git a/scripts/litegraphDeclarations.ts b/scripts/litegraphDeclarations.ts new file mode 100644 index 00000000000..c35866a521a --- /dev/null +++ b/scripts/litegraphDeclarations.ts @@ -0,0 +1,127 @@ +import { readdirSync, readFileSync } from 'node:fs' +import { relative, resolve } from 'node:path' +import ts from 'typescript' + +interface Registration { + file: string + names: { name: string; alias: string }[] +} + +function registryAugmentation(node: ts.Node): node is ts.ModuleDeclaration & { + body: ts.ModuleBlock +} { + return ( + ts.isModuleDeclaration(node) && + ts.isStringLiteral(node.name) && + node.name.text.endsWith('/entityRegistry') && + node.body !== undefined && + ts.isModuleBlock(node.body) + ) +} + +export function createLitegraphDeclarationTransform(root: string) { + const registrations: Registration[] = readdirSync(root, { + recursive: true, + encoding: 'utf8' + }).flatMap((file) => { + if (!file.endsWith('.ts')) return [] + const content = readFileSync(resolve(root, file), 'utf8') + if (!content.includes('declare module')) return [] + const source = ts.createSourceFile( + file, + content, + ts.ScriptTarget.Latest, + true + ) + const names = source.statements + .filter(registryAugmentation) + .flatMap((node) => + node.body.statements.flatMap((statement) => { + if (!ts.isInterfaceDeclaration(statement)) return [] + if (statement.name.text === 'LitegraphEntityRegistry') { + return statement.members.flatMap((member) => + ts.isPropertySignature(member) && member.type + ? [ + { + name: member.name.getText(source), + alias: member.type.getText(source) + } + ] + : [] + ) + } + return ( + statement.heritageClauses?.flatMap((clause) => + clause.types.map((type) => ({ + name: statement.name.text, + alias: type.expression.getText(source) + })) + ) ?? [] + ) + }) + ) + return names.length ? [{ file: file.replaceAll('\\', '/'), names }] : [] + }) + + return ( + filePath: string, + content: string + ): { content: string } | undefined => { + const outputPath = filePath.replaceAll('\\', '/') + if (outputPath.endsWith('/types/entityRegistry.d.ts')) { + return { + content: registrations + .map(({ file, names }) => { + const owner = relative('types', file) + .replaceAll('\\', '/') + .replace(/\.ts$/, '') + return `export type { ${names.map(({ name, alias }) => `${alias} as ${name}`).join(', ')} } from '${owner.startsWith('.') ? owner : './' + owner}'` + }) + .join('\n') + } + } + const registration = registrations.find(({ file }) => + outputPath.endsWith('/' + file.replace(/\.ts$/, '.d.ts')) + ) + if (!registration) return undefined + const source = ts.createSourceFile( + filePath, + content, + ts.ScriptTarget.Latest, + true + ) + const aliases = new Set(registration.names.map(({ alias }) => alias)) + const edits = source.statements.flatMap((statement) => { + if (registryAugmentation(statement)) { + return [ + { start: statement.getFullStart(), end: statement.getEnd(), text: '' } + ] + } + if ( + ts.isTypeAliasDeclaration(statement) && + aliases.has(statement.name.text) && + !statement.modifiers?.some( + (modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword + ) + ) { + return [ + { + start: statement.getStart(source), + end: statement.getStart(source), + text: 'export ' + } + ] + } + return [] + }) + return { + content: edits + .reverse() + .reduce( + (text, edit) => + text.slice(0, edit.start) + edit.text + text.slice(edit.end), + content + ) + } + } +} diff --git a/src/core/graph/nodeShell/nodeShellLifecycle.ts b/src/core/graph/nodeShell/nodeShellLifecycle.ts index bbc7f77eb0d..db276c7b134 100644 --- a/src/core/graph/nodeShell/nodeShellLifecycle.ts +++ b/src/core/graph/nodeShell/nodeShellLifecycle.ts @@ -1,3 +1,8 @@ +import type { + LGraph, + LGraphNode, + Subgraph +} from '@/lib/litegraph/src/types/entityRegistry' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { WidgetRefusalReport } from '@/lib/litegraph/src/node/widgetsView' import { refuseAmbiguousNodeWidgets } from '@/lib/litegraph/src/node/widgetsView' @@ -10,10 +15,8 @@ import type { RefusedWidget } from '@/types/widgetId' import { registerNodeState, unregisterNodeState } from './nodeShellState' -import type { LGraph } from '@/lib/litegraph/src/LGraph' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { NodeId } from '@/types/nodeId' -import type { Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph' + import type { UUID } from '@/utils/uuid' const REFUSAL_TEARDOWN_ERROR_TYPE = 'failure_tearing_down_refused_widget' diff --git a/src/core/graph/nodeShell/nodeShellState.ts b/src/core/graph/nodeShell/nodeShellState.ts index 3c78cf62284..28872eb2ec4 100644 --- a/src/core/graph/nodeShell/nodeShellState.ts +++ b/src/core/graph/nodeShell/nodeShellState.ts @@ -1,3 +1,7 @@ +import type { + LGraph, + LGraphNode +} from '@/lib/litegraph/src/types/entityRegistry' import { shallowReactive } from 'vue' import { assert } from '@/base/assert' @@ -15,8 +19,7 @@ import type { INodeInputSlot, INodeOutputSlot } from '@/lib/litegraph/src/types/slots' -import type { LGraph } from '@/lib/litegraph/src/LGraph' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' + import type { TitleMode } from '@/lib/litegraph/src/types/globalEnums' import type { NodeState } from '@/types/nodeState' diff --git a/src/core/graph/subgraph/adoptPromotedWidgetValue.ts b/src/core/graph/subgraph/adoptPromotedWidgetValue.ts index 957339754be..cc668ff3921 100644 --- a/src/core/graph/subgraph/adoptPromotedWidgetValue.ts +++ b/src/core/graph/subgraph/adoptPromotedWidgetValue.ts @@ -1,5 +1,6 @@ +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' import type { INodeInputSlot } from '@/lib/litegraph/src/types/slots' -import type { LGraphNode } from '@/lib/litegraph/src/litegraph' + import { isWidgetValue } from '@/lib/litegraph/src/types/widgets' import { useWidgetValueStore } from '@/stores/widgetValueStore' diff --git a/src/core/graph/subgraph/promotedWidgetTypes.ts b/src/core/graph/subgraph/promotedWidgetTypes.ts index 9bf9c0ba2bf..b5d63a8648a 100644 --- a/src/core/graph/subgraph/promotedWidgetTypes.ts +++ b/src/core/graph/subgraph/promotedWidgetTypes.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/litegraph' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { NodeId } from '@/types/nodeId' import type { NodeExecutionId } from '@/types/nodeIdentification' diff --git a/src/core/graph/subgraph/resolveConcretePromotedWidget.ts b/src/core/graph/subgraph/resolveConcretePromotedWidget.ts index 4563c670ec0..28c36ecf1bd 100644 --- a/src/core/graph/subgraph/resolveConcretePromotedWidget.ts +++ b/src/core/graph/subgraph/resolveConcretePromotedWidget.ts @@ -1,11 +1,14 @@ +import type { + LGraphNode, + SubgraphNode, + SubgraphInput +} from '@/lib/litegraph/src/types/entityRegistry' import type { PromotedWidgetExecutionSource, ResolvedPromotedWidget } from '@/core/graph/subgraph/promotedWidgetTypes' import { resolveSubgraphInputTarget } from '@/core/graph/subgraph/resolveSubgraphInputTarget' -import type { LGraphNode } from '@/lib/litegraph/src/litegraph' -import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' + import { LGraphEventMode } from '@/lib/litegraph/src/types/globalEnums' import type { NodeExecutionId } from '@/types/nodeIdentification' import { createNodeExecutionId } from '@/types/nodeIdentification' diff --git a/src/core/graph/subgraph/resolveSubgraphInputLink.ts b/src/core/graph/subgraph/resolveSubgraphInputLink.ts index 4f95bf694e0..bd73f153594 100644 --- a/src/core/graph/subgraph/resolveSubgraphInputLink.ts +++ b/src/core/graph/subgraph/resolveSubgraphInputLink.ts @@ -1,5 +1,5 @@ +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' import type { INodeInputSlot } from '@/lib/litegraph/src/types/slots' -import type { LGraphNode } from '@/lib/litegraph/src/litegraph' type SubgraphInputLinkContext = { inputNode: LGraphNode diff --git a/src/core/graph/subgraph/resolveSubgraphInputTarget.ts b/src/core/graph/subgraph/resolveSubgraphInputTarget.ts index b79b789059b..64031c52dea 100644 --- a/src/core/graph/subgraph/resolveSubgraphInputTarget.ts +++ b/src/core/graph/subgraph/resolveSubgraphInputTarget.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/litegraph' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { NodeId } from '@/types/nodeId' import { resolveSubgraphInputLink } from './resolveSubgraphInputLink' diff --git a/src/core/schemas/parseNodePropertyArray.ts b/src/core/schemas/parseNodePropertyArray.ts index f9c2aae3da4..aae821604fd 100644 --- a/src/core/schemas/parseNodePropertyArray.ts +++ b/src/core/schemas/parseNodePropertyArray.ts @@ -1,7 +1,7 @@ import type { z } from 'zod' import { fromZodError } from 'zod-validation-error' -import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' +import type { NodeProperty } from '@/types/nodeProperty' /** * @param property - Raw node property value. diff --git a/src/core/schemas/previewExposureSchema.ts b/src/core/schemas/previewExposureSchema.ts index 1dc50456010..0aeccf47eb3 100644 --- a/src/core/schemas/previewExposureSchema.ts +++ b/src/core/schemas/previewExposureSchema.ts @@ -1,6 +1,6 @@ import { z } from 'zod' -import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' +import type { NodeProperty } from '@/types/nodeProperty' import { toNodeId } from '@/types/nodeId' import { parseNodePropertyArray } from './parseNodePropertyArray' diff --git a/src/core/schemas/promotionSchema.ts b/src/core/schemas/promotionSchema.ts index 7907f36784a..ca49b2b8517 100644 --- a/src/core/schemas/promotionSchema.ts +++ b/src/core/schemas/promotionSchema.ts @@ -1,6 +1,6 @@ import { z } from 'zod' -import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' +import type { NodeProperty } from '@/types/nodeProperty' import { parseNodePropertyArray } from './parseNodePropertyArray' diff --git a/src/core/schemas/proxyWidgetQuarantineSchema.ts b/src/core/schemas/proxyWidgetQuarantineSchema.ts index 47cab8e21be..bb9d834edd6 100644 --- a/src/core/schemas/proxyWidgetQuarantineSchema.ts +++ b/src/core/schemas/proxyWidgetQuarantineSchema.ts @@ -1,7 +1,7 @@ import { z } from 'zod' -import type { NodeProperty } from '@/lib/litegraph/src/LGraphNode' -import type { TWidgetValue } from '@/lib/litegraph/src/types/widgets' +import type { NodeProperty } from '@/types/nodeProperty' +import type { WidgetValue } from '@/types/widgetValue' import { parseNodePropertyArray } from './parseNodePropertyArray' import { serializedProxyWidgetTupleSchema } from './promotionSchema' @@ -32,7 +32,7 @@ const proxyWidgetErrorQuarantinePropertySchema = z.array( export type ProxyWidgetErrorQuarantineEntry = Omit< z.infer, 'hostValue' -> & { hostValue?: TWidgetValue } +> & { hostValue?: WidgetValue } export function parseProxyWidgetErrorQuarantine( property: NodeProperty | undefined diff --git a/src/lib/litegraph/src/ContextMenu.ts b/src/lib/litegraph/src/ContextMenu.ts index c2cb9f9e0f9..d1d28134881 100644 --- a/src/lib/litegraph/src/ContextMenu.ts +++ b/src/lib/litegraph/src/ContextMenu.ts @@ -5,7 +5,7 @@ import type { IContextMenuOptions, IContextMenuValue } from './types/contextMenu' -import { LiteGraph } from './litegraph' +import { litegraph } from './litegraphInstance' const ALLOWED_TAGS = ['span', 'b', 'i', 'em', 'strong'] const ALLOWED_STYLE_PROPS = new Set([ @@ -215,7 +215,7 @@ export class ContextMenu { root.style.left = `${left}px` root.style.top = `${top}px` - if (LiteGraph.context_menu_scaling && options.scale) { + if (litegraph().context_menu_scaling && options.scale) { root.style.transform = `scale(${Math.round(options.scale * 4) * 0.25})` } } @@ -384,7 +384,7 @@ export class ContextMenu { } else if (!ContextMenu.isCursorOverElement(e, this.parentMenu.root)) { ContextMenu.trigger( this.parentMenu.root, - `${LiteGraph.pointerevents_method}leave`, + `${litegraph().pointerevents_method}leave`, e ) } @@ -436,3 +436,11 @@ export class ContextMenu { return false } } + +type RegisteredContextMenu = ContextMenu + +declare module './types/entityRegistry' { + interface ContextMenu< + _TValue = unknown + > extends RegisteredContextMenu<_TValue> {} +} diff --git a/src/lib/litegraph/src/CurveEditor.ts b/src/lib/litegraph/src/CurveEditor.ts index b774b8e4972..04d157be003 100644 --- a/src/lib/litegraph/src/CurveEditor.ts +++ b/src/lib/litegraph/src/CurveEditor.ts @@ -1,7 +1,8 @@ +import type { LGraphCanvas } from '@/lib/litegraph/src/types/entityRegistry' import { clamp } from 'es-toolkit/compat' import type { Point, Rect } from './interfaces' -import type { LGraphCanvas } from './litegraph' + import { distance } from './measure' // used by some widgets to render a curve editor diff --git a/src/lib/litegraph/src/DragAndScale.ts b/src/lib/litegraph/src/DragAndScale.ts index 695a2e70f0c..ec9cb8251f2 100644 --- a/src/lib/litegraph/src/DragAndScale.ts +++ b/src/lib/litegraph/src/DragAndScale.ts @@ -1,7 +1,8 @@ import { readBrowserDpr } from '@/renderer/core/canvas/canvasViewport' import type { Point, ReadOnlyRect, Rect } from './interfaces' -import { EaseFunction, Rectangle } from './litegraph' +import { EaseFunction } from './types/globalEnums' +import { Rectangle } from './infrastructure/Rectangle' export interface DragAndScaleState { /** diff --git a/src/lib/litegraph/src/LGraph.ts b/src/lib/litegraph/src/LGraph.ts index b2b3fb9d359..96c04c3f703 100644 --- a/src/lib/litegraph/src/LGraph.ts +++ b/src/lib/litegraph/src/LGraph.ts @@ -1,3 +1,4 @@ +import type { LGraphCanvas } from '@/lib/litegraph/src/types/entityRegistry' import { toString } from 'es-toolkit/compat' import { shallowRef, toRaw } from 'vue' @@ -109,7 +110,8 @@ import { } from './utils/namedValuesShadowDiffTelemetry' import type { DragAndScaleState } from './DragAndScale' -import { LGraphCanvas } from './LGraphCanvas' + +import { canvasClass } from './canvas/canvasClassAccess' import { Rectangle } from './infrastructure/Rectangle' import { LGraphGroup } from './LGraphGroup' import { LGraphNode } from './LGraphNode' @@ -121,7 +123,7 @@ import { unregisterLinkTopology } from './LLink' import { LinkMap } from './LinkMap' -import type { LinkId } from './LLink' +import type { LinkId } from '@/types/linkId' import { MapProxyHandler } from './MapProxyHandler' import { registerRerouteChain, @@ -129,7 +131,7 @@ import { unregisterAllRerouteChains, unregisterRerouteChain } from './Reroute' -import type { RerouteId } from './Reroute' +import type { RerouteId } from '@/types/rerouteId' import { CustomEventTarget } from './infrastructure/CustomEventTarget' import type { LGraphEventMap } from './infrastructure/LGraphEventMap' import type { SubgraphEventMap } from './infrastructure/SubgraphEventMap' @@ -146,7 +148,8 @@ import type { } from './interfaces' import type { INodeInputSlot, INodeOutputSlot } from './types/slots' import type { LinkNetwork } from './types/linkNetwork' -import { LiteGraph, SubgraphNode } from './litegraph' +import { LiteGraph } from './litegraphInstance' +import { SubgraphNode } from './subgraph/SubgraphNode' import { alignOutsideContainer, alignToContainer, @@ -158,6 +161,7 @@ import { SubgraphInput } from './subgraph/SubgraphInput' import { SubgraphInputNode } from './subgraph/SubgraphInputNode' import { SubgraphOutput } from './subgraph/SubgraphOutput' import { SubgraphOutputNode } from './subgraph/SubgraphOutputNode' +import { provideSubgraphGuard } from './subgraph/subgraphTypeGuards' import { captureUnpackedTargetInput, findUnavailableSubgraphNodeType, @@ -941,7 +945,7 @@ export class LGraph * Attach Canvas to this graph */ attachCanvas(canvas: LGraphCanvas): void { - if (!(canvas instanceof LGraphCanvas)) { + if (!(canvas instanceof canvasClass)) { throw new TypeError('attachCanvas expects an LGraphCanvas instance') } @@ -3806,3 +3810,25 @@ export class Subgraph } } } + +provideSubgraphGuard((value): value is Subgraph => value instanceof Subgraph) + +type RegisteredLGraphConfig = LGraphConfig +type RegisteredLGraphExtra = LGraphExtra +type RegisteredLGraph = LGraph +type RegisteredSubgraph = Subgraph +type RegisteredSubgraphId = SubgraphId +type RegisteredGraphOrSubgraph = GraphOrSubgraph +type RegisteredBaseLGraph = BaseLGraph + +declare module './types/entityRegistry' { + interface LitegraphEntityRegistry { + SubgraphId: RegisteredSubgraphId + GraphOrSubgraph: RegisteredGraphOrSubgraph + } + interface LGraphConfig extends RegisteredLGraphConfig {} + interface LGraphExtra extends RegisteredLGraphExtra {} + interface LGraph extends RegisteredLGraph {} + interface Subgraph extends RegisteredSubgraph {} + interface BaseLGraph extends RegisteredBaseLGraph {} +} diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 7c050d79f2f..2e4d9fb543e 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -1,6 +1,14 @@ +import type { + ContextMenu, + LGraph, + SubgraphId, + SlotTypeDefaultNodeOpts, + Subgraph +} from '@/lib/litegraph/src/types/entityRegistry' import { default as DOMPurify } from 'dompurify' import { cloneDeep, toString } from 'es-toolkit/compat' import { toValue } from 'vue' +import { provideCanvasClass } from './canvas/canvasClassAccess' import { isMiddleButtonEvent } from '@/base/pointerUtils' import { transferLinkPresentation } from '@/core/graph/transferLinkPresentation' @@ -62,23 +70,23 @@ import { CANVAS_CLIPBOARD_ID_KEY, CANVAS_CLIPBOARD_KEY } from './canvas/clipboardStorage' -import type { ContextMenu } from './ContextMenu' + import { createCursorCache } from './cursorCache' import { DragAndScale } from './DragAndScale' import type { AnimationOptions } from './DragAndScale' -import type { LGraph, SubgraphId } from './LGraph' + import { LGraphGroup } from './LGraphGroup' -import type { SlotTypeDefaultNodeOpts } from './LiteGraphGlobal' + import { LGraphNode } from './LGraphNode' -import type { NodeProperty } from './LGraphNode' +import type { NodeProperty } from '@/types/nodeProperty' import { detachSerialisedLinks } from './linkDeduplication' import { parseNodeId, serializeNodeId } from '@/types/nodeId' import type { NodeId, SerializedNodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from './LLink' import { inputHasLink, nodeLinkIds, outputLinks } from './node/slotLinks' -import type { LinkId } from './LLink' +import type { LinkId } from '@/types/linkId' import { Reroute } from './Reroute' -import type { RerouteId } from './Reroute' +import type { RerouteId } from '@/types/rerouteId' import type { CanvasInteractionModeReader } from './canvas/CanvasInteractionMode' import { LinkConnector } from './canvas/LinkConnector' import { findRerouteAtPoint } from './canvas/hitTesting' @@ -156,7 +164,7 @@ import type { PanelWidgetCallback, PanelWidgetOptions } from './types/panel' -import { LiteGraph } from './litegraph' +import { LiteGraph } from './litegraphInstance' import { containsRect, createBounds, @@ -168,7 +176,7 @@ import { snapPoint } from './measure' import { NodeInputSlot } from './node/NodeInputSlot' -import type { Subgraph } from './subgraph/Subgraph' + import { SubgraphIONodeBase } from './subgraph/SubgraphIONodeBase' import { SubgraphInputNode } from './subgraph/SubgraphInputNode' import { SubgraphNode } from './subgraph/SubgraphNode' @@ -9097,3 +9105,11 @@ defineDeprecatedProperty( 'applyNodePositions', 'LGraphCanvas.repositionNodesVueMode is deprecated. Use applyNodePositions instead.' ) + +provideCanvasClass(LGraphCanvas) + +type RegisteredLGraphCanvas = LGraphCanvas + +declare module './types/entityRegistry' { + interface LGraphCanvas extends RegisteredLGraphCanvas {} +} diff --git a/src/lib/litegraph/src/LGraphGroup.ts b/src/lib/litegraph/src/LGraphGroup.ts index 680dbcc6c11..d1d3e3bda64 100644 --- a/src/lib/litegraph/src/LGraphGroup.ts +++ b/src/lib/litegraph/src/LGraphGroup.ts @@ -1,3 +1,7 @@ +import type { + LGraph, + LGraphCanvas +} from '@/lib/litegraph/src/types/entityRegistry' import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError' import { setGroupBoundsLayout } from '@/renderer/core/layout/operations/graphLayoutAttachment' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' @@ -9,8 +13,7 @@ import type { GroupId } from '@/types/groupId' import { toGroupId } from '@/types/groupId' import { hexToRgb, luminance, readableTextColor } from '@/utils/colorUtil' -import type { LGraph } from './LGraph' -import { LGraphCanvas } from './LGraphCanvas' +import { canvasClass } from './canvas/canvasClassAccess' import { LGraphNode } from './LGraphNode' import { strokeShape } from './draw' import { createMutationView } from './infrastructure/createMutationView' @@ -24,7 +27,8 @@ import type { Size } from './interfaces' import type { IContextMenuValue } from './types/contextMenu' -import { LiteGraph, Rectangle } from './litegraph' +import { LiteGraph } from './litegraphInstance' +import { Rectangle } from './infrastructure/Rectangle' import { containsCentre, containsRect, @@ -123,7 +127,7 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable { // TODO: Object instantiation pattern requires too much boilerplate and null checking. ID should be passed in via constructor. this.id = toGroupId(id ?? -1) this.title = title || 'Group' - const { pale_blue } = LGraphCanvas.node_colors + const { pale_blue } = canvasClass.node_colors this.color = pale_blue.groupcolor } @@ -139,7 +143,7 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable { /** @inheritdoc {@link IColorable.getColorOption} */ getColorOption(): ColorOption | null { return ( - Object.values(LGraphCanvas.node_colors).find( + Object.values(canvasClass.node_colors).find( (colorOption) => colorOption.groupcolor === this.color ) ?? null ) @@ -459,19 +463,19 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable { } }, null, - { content: 'Title', callback: LGraphCanvas.onShowPropertyEditor }, + { content: 'Title', callback: canvasClass.onShowPropertyEditor }, { content: 'Color', has_submenu: true, - callback: LGraphCanvas.onMenuNodeColors + callback: canvasClass.onMenuNodeColors }, { content: 'Font size', property: 'font_size', - callback: LGraphCanvas.onShowPropertyEditor + callback: canvasClass.onShowPropertyEditor }, null, - { content: 'Remove', callback: LGraphCanvas.onMenuNodeRemove } + { content: 'Remove', callback: canvasClass.onMenuNodeRemove } ] } @@ -497,3 +501,11 @@ export class LGraphGroup implements Positionable, IPinnable, IColorable { } setDirtyCanvas = LGraphNode.prototype.setDirtyCanvas } + +type RegisteredLGraphGroup = LGraphGroup +type RegisteredIGraphGroupFlags = IGraphGroupFlags + +declare module './types/entityRegistry' { + interface LGraphGroup extends RegisteredLGraphGroup {} + interface IGraphGroupFlags extends RegisteredIGraphGroupFlags {} +} diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 04a6099f663..a85254f680a 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -1,3 +1,13 @@ +import type { + LGraph, + Subgraph, + LGraphCanvas, + Reroute, + SubgraphNode, + SubgraphInputNode, + SubgraphOutputNode +} from '@/lib/litegraph/src/types/entityRegistry' +import type { LGraphNodeConstructor } from '@/lib/litegraph/src/types/nodeConstructor' import { toValue } from 'vue' import { @@ -30,7 +40,8 @@ import type { GraphScope } from '@/types/graphScopeId' import { linkIdReservations, mintLinkId } from './idAllocation' import { UNASSIGNED_NODE_ID, toNodeId, serializeNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' -import type { NodeProperty, NodeState } from '@/types/nodeState' +import type { NodeState } from '@/types/nodeState' +import type { NodeProperty } from '@/types/nodeProperty' import { deriveWidgetVisibility, isWidgetVisibleOnSurface, @@ -51,12 +62,13 @@ import { } from '@/lib/litegraph/src/constants' import { cachedMeasureText } from '@/lib/litegraph/src/utils/textMeasureCache' import type { DragAndScale } from './DragAndScale' -import type { LGraph } from './LGraph' + import { LGraphBadge } from './LGraphBadge' import { badgeDrawObjects, badgeRows } from './nodeBadgeDraw' import { LGraphButton } from './LGraphButton' import type { LGraphButtonOptions } from './LGraphButton' -import { LGraphCanvas } from './LGraphCanvas' + +import { canvasClass } from './canvas/canvasClassAccess' import { realignGroupWidgetChildLinks } from './linkDeduplication' import { LLink, replaceLinkTopology, slotFloatingLinks } from './LLink' import { @@ -88,7 +100,7 @@ import { runExtensionSerializeHook } from './extensionPersistence' import { anchorRerouteChain } from './Reroute' -import type { Reroute, RerouteId } from './Reroute' +import type { RerouteId } from '@/types/rerouteId' import { getNodeInputOnPos, getNodeOutputOnPos } from './canvas/measureSlots' import type { IDrawBoundingOptions } from './draw' import { emitNodeFieldWrite } from './graphIntents' @@ -120,8 +132,9 @@ import type { INodeSlotContextItem } from './types/slots' import type { Panel } from './types/panel' -import { LiteGraph, Subgraph } from './litegraph' -import type { LGraphNodeConstructor, SubgraphNode } from './litegraph' +import { LiteGraph } from './litegraphInstance' +import { isSubgraph } from './subgraph/subgraphTypeGuards' + import { createBounds, isInRect, @@ -136,8 +149,7 @@ import { isWidgetInputSlot, outputAsSerialisable } from './node/slotUtils' -import type { SubgraphInputNode } from './subgraph/SubgraphInputNode' -import type { SubgraphOutputNode } from './subgraph/SubgraphOutputNode' + import type { NodeLike } from './types/NodeLike' import type { CanvasPointerEvent } from './types/events' import { @@ -153,7 +165,7 @@ import type { TWidgetType, TWidgetValue } from './types/widgets' -import { findFreeSlotOfType } from './utils/collections' +import { findFreeSlotOfType, provideGraphNodeGuard } from './utils/collections' import { warnDeprecated } from './utils/feedback' import { computeLegacyWidgetShadow, @@ -659,7 +671,7 @@ export class LGraphNode /** @inheritdoc {@link IColorable.getColorOption} */ getColorOption(): ColorOption | null { return ( - Object.values(LGraphCanvas.node_colors).find( + Object.values(canvasClass.node_colors).find( (colorOption) => colorOption.color === this.color && colorOption.bgcolor === this.bgcolor @@ -2169,7 +2181,7 @@ export class LGraphNode function compute_text_size(text: string | undefined, fontStyle: string) { const value = text ?? '' return ( - LGraphCanvas._measureText?.(value, fontStyle) ?? + canvasClass._measureText?.(value, fontStyle) ?? font_size * value.length * 0.6 ) } @@ -3440,10 +3452,7 @@ export class LGraphNode } disconnected = true - if ( - link_info.target_id === SUBGRAPH_OUTPUT_ID && - graph instanceof Subgraph - ) { + if (link_info.target_id === SUBGRAPH_OUTPUT_ID && isSubgraph(graph)) { const targetSlot = legacyArrayItem( graph.outputNode.slots, link_info.target_slot @@ -4619,3 +4628,11 @@ export class LGraphNode ctx.fillStyle = originalFillStyle } } + +provideGraphNodeGuard((item): item is LGraphNode => item instanceof LGraphNode) + +type RegisteredLGraphNode = LGraphNode + +declare module './types/entityRegistry' { + interface LGraphNode extends RegisteredLGraphNode {} +} diff --git a/src/lib/litegraph/src/LLink.ts b/src/lib/litegraph/src/LLink.ts index 5ffb8ce952a..40bc93dce7a 100644 --- a/src/lib/litegraph/src/LLink.ts +++ b/src/lib/litegraph/src/LLink.ts @@ -1,9 +1,15 @@ +import type { + SubgraphInput, + SubgraphOutput, + LGraph, + LGraphNode, + Reroute +} from '@/lib/litegraph/src/types/entityRegistry' import { SUBGRAPH_INPUT_ID, SUBGRAPH_OUTPUT_ID } from '@/lib/litegraph/src/constants' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' + import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { useLinkStore } from '@/stores/linkStore' @@ -18,10 +24,9 @@ import type { EndpointPatch } from '@/stores/linkStore' import type { LinkId } from '@/types/linkId' import type { LinkTopology } from '@/types/linkTopology' import type { RerouteId } from '@/types/rerouteId' -import type { LGraph } from './LGraph' -import type { LGraphNode } from './LGraphNode' + import type { NodeId, SerializedNodeId } from '@/types/nodeId' -import type { Reroute } from './Reroute' + import type { CanvasColour, ISlotType, LinkSegment, Point } from './interfaces' import type { INodeInputSlot, INodeOutputSlot } from './types/slots' import type { LinkNetwork, ReadonlyLinkNetwork } from './types/linkNetwork' @@ -727,3 +732,15 @@ export function unregisterAllLinkTopologies( unregisterLinkTopology(link) } } + +type RegisteredLLink = LLink +type RegisteredSerialisedLLinkArray = SerialisedLLinkArray +type RegisteredResolvedConnection = ResolvedConnection + +declare module './types/entityRegistry' { + interface LitegraphEntityRegistry { + SerialisedLLinkArray: RegisteredSerialisedLLinkArray + ResolvedConnection: RegisteredResolvedConnection + } + interface LLink extends RegisteredLLink {} +} diff --git a/src/lib/litegraph/src/LinkMap.ts b/src/lib/litegraph/src/LinkMap.ts index 518a1ff444d..862ba34b6bc 100644 --- a/src/lib/litegraph/src/LinkMap.ts +++ b/src/lib/litegraph/src/LinkMap.ts @@ -1,8 +1,7 @@ +import type { LLink } from '@/lib/litegraph/src/types/entityRegistry' import type { GraphScope } from '@/types/graphScopeId' import type { LinkId } from '@/types/linkId' -import type { LLink } from './LLink' - export class LinkMap extends Map { private cachedRevision = -1 private cachedLinks = new Map() diff --git a/src/lib/litegraph/src/LiteGraphGlobal.ts b/src/lib/litegraph/src/LiteGraphGlobal.ts index 41adc63fdf8..123b30fc8b6 100644 --- a/src/lib/litegraph/src/LiteGraphGlobal.ts +++ b/src/lib/litegraph/src/LiteGraphGlobal.ts @@ -5,7 +5,7 @@ import { LGraph } from './LGraph' import { LGraphCanvas } from './LGraphCanvas' import { LGraphGroup } from './LGraphGroup' import { LGraphNode } from './LGraphNode' -import type { NodeProperty } from './LGraphNode' +import type { NodeProperty } from '@/types/nodeProperty' import { LLink } from './LLink' import { Reroute } from './Reroute' import { InputIndicators } from './canvas/InputIndicators' @@ -1024,3 +1024,11 @@ export class LiteGraphGlobal { } } } + +type RegisteredSlotTypeDefaultNodeOpts = SlotTypeDefaultNodeOpts +type RegisteredLiteGraphGlobal = LiteGraphGlobal + +declare module './types/entityRegistry' { + interface SlotTypeDefaultNodeOpts extends RegisteredSlotTypeDefaultNodeOpts {} + interface LiteGraphGlobal extends RegisteredLiteGraphGlobal {} +} diff --git a/src/lib/litegraph/src/Reroute.ts b/src/lib/litegraph/src/Reroute.ts index 840aebf9daa..34420abe54b 100644 --- a/src/lib/litegraph/src/Reroute.ts +++ b/src/lib/litegraph/src/Reroute.ts @@ -1,3 +1,7 @@ +import type { + LGraph, + LGraphNode +} from '@/lib/litegraph/src/types/entityRegistry' import { moveRerouteLayout } from '@/renderer/core/layout/operations/graphLayoutAttachment' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { isSelectedIn, setSelectedIn } from '@/core/selection/selectionStore' @@ -29,10 +33,9 @@ function hasPointLength(value: unknown): boolean { } import { LGraphBadge } from './LGraphBadge' -import type { LGraph } from './LGraph' -import type { LGraphNode } from './LGraphNode' + import { LLink } from './LLink' -import type { LinkId } from './LLink' +import type { LinkId } from '@/types/linkId' import { createMutationView } from './infrastructure/createMutationView' import type { CanvasColour, @@ -43,7 +46,7 @@ import type { } from './interfaces' import type { INodeInputSlot, INodeOutputSlot } from './types/slots' import type { LinkNetwork, ReadonlyLinkNetwork } from './types/linkNetwork' -import { LiteGraph } from './litegraph' +import { litegraph } from './litegraphInstance' import { distance, isPointInRect } from './measure' import type { Serialisable, SerialisableReroute } from './types/serialisation' @@ -472,7 +475,7 @@ export class Reroute snapToGrid(snapTo: number): boolean { if (!snapTo) return false - const offsetY = LiteGraph.NODE_SLOT_HEIGHT * 0.7 + const offsetY = litegraph().NODE_SLOT_HEIGHT * 0.7 const { x, y } = this.storedPosition const snappedX = snapTo * Math.round(x / snapTo) const snappedY = snapTo * Math.round((y - offsetY) / snapTo) + offsetY @@ -899,3 +902,9 @@ export function unregisterAllRerouteChains( ): void { for (const reroute of graph.reroutes.values()) unregisterRerouteChain(reroute) } + +type RegisteredReroute = Reroute + +declare module './types/entityRegistry' { + interface Reroute extends RegisteredReroute {} +} diff --git a/src/lib/litegraph/src/canvas/FloatingRenderLink.ts b/src/lib/litegraph/src/canvas/FloatingRenderLink.ts index 2144713c9dd..74ba296e4bc 100644 --- a/src/lib/litegraph/src/canvas/FloatingRenderLink.ts +++ b/src/lib/litegraph/src/canvas/FloatingRenderLink.ts @@ -1,8 +1,14 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { + LGraphNode, + LLink, + Reroute, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + import { UNASSIGNED_NODE_ID } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import { SUBGRAPH_INPUT_ID, SUBGRAPH_OUTPUT_ID @@ -15,8 +21,7 @@ import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' + import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' import type { RenderLink } from './RenderLink' @@ -220,3 +225,9 @@ export class FloatingRenderLink implements RenderLink { events.dispatch('output-moved', this) } } + +type RegisteredFloatingRenderLink = FloatingRenderLink + +declare module '../types/entityRegistry' { + interface FloatingRenderLink extends RegisteredFloatingRenderLink {} +} diff --git a/src/lib/litegraph/src/canvas/InputIndicators.ts b/src/lib/litegraph/src/canvas/InputIndicators.ts index f684a8a047b..a52978a09a1 100644 --- a/src/lib/litegraph/src/canvas/InputIndicators.ts +++ b/src/lib/litegraph/src/canvas/InputIndicators.ts @@ -1,5 +1,5 @@ +import type { LGraphCanvas } from '@/lib/litegraph/src/types/entityRegistry' import { isMiddleButtonHeld } from '@/base/pointerUtils' -import type { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas' /** * A class that can be added to the render cycle to show pointer / keyboard status symbols. diff --git a/src/lib/litegraph/src/canvas/LinkConnector.ts b/src/lib/litegraph/src/canvas/LinkConnector.ts index 9b245978cd6..54edc079308 100644 --- a/src/lib/litegraph/src/canvas/LinkConnector.ts +++ b/src/lib/litegraph/src/canvas/LinkConnector.ts @@ -1,12 +1,17 @@ +import type { + LGraphNode, + Reroute, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' import { remove } from 'es-toolkit' import { transferLinkPresentation } from '@/core/graph/transferLinkPresentation' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { graphScopeOf } from '@/types/graphScopeId' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import { LLink, slotFloatingLinks } from '@/lib/litegraph/src/LLink' import { inputLinkId, outputLinks } from '@/lib/litegraph/src/node/slotLinks' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import { SUBGRAPH_INPUT_ID, SUBGRAPH_OUTPUT_ID @@ -26,9 +31,9 @@ import type { import { EmptySubgraphInput } from '@/lib/litegraph/src/subgraph/EmptySubgraphInput' import { EmptySubgraphOutput } from '@/lib/litegraph/src/subgraph/EmptySubgraphOutput' import { Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' + import { SubgraphInputNode } from '@/lib/litegraph/src/subgraph/SubgraphInputNode' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' + import { SubgraphOutputNode } from '@/lib/litegraph/src/subgraph/SubgraphOutputNode' import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events' import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' @@ -1243,3 +1248,9 @@ function canConnectInputLinkToReroute( } return true } + +type RegisteredLinkConnector = LinkConnector + +declare module '../types/entityRegistry' { + interface LinkConnector extends RegisteredLinkConnector {} +} diff --git a/src/lib/litegraph/src/canvas/MovingInputLink.ts b/src/lib/litegraph/src/canvas/MovingInputLink.ts index f7e8ec0ba32..0604ddcd72c 100644 --- a/src/lib/litegraph/src/canvas/MovingInputLink.ts +++ b/src/lib/litegraph/src/canvas/MovingInputLink.ts @@ -1,8 +1,13 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { + LGraphNode, + LLink, + Reroute, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + import { transferLinkPresentation } from '@/core/graph/transferLinkPresentation' import { graphScopeOf } from '@/types/graphScopeId' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import type { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { LinkConnectorEventMap } from '@/lib/litegraph/src/infrastructure/LinkConnectorEventMap' import type { Point, SlotIndex } from '@/lib/litegraph/src/interfaces' @@ -11,7 +16,7 @@ import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' + import type { NodeLike } from '@/lib/litegraph/src/types/NodeLike' import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' import type { SubgraphIO } from '@/lib/litegraph/src/types/serialisation' @@ -152,3 +157,9 @@ export class MovingInputLink extends MovingLinkBase { return this.inputNode.disconnectInput(this.inputIndex, true) } } + +type RegisteredMovingInputLink = MovingInputLink + +declare module '../types/entityRegistry' { + interface MovingInputLink extends RegisteredMovingInputLink {} +} diff --git a/src/lib/litegraph/src/canvas/MovingLinkBase.ts b/src/lib/litegraph/src/canvas/MovingLinkBase.ts index 535a28e6c3c..3ed904fab04 100644 --- a/src/lib/litegraph/src/canvas/MovingLinkBase.ts +++ b/src/lib/litegraph/src/canvas/MovingLinkBase.ts @@ -1,10 +1,16 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { + LGraphNode, + LLink, + Reroute, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + import type { NodeId } from '@/types/nodeId' import type { LinkPresentation } from '@/types/linkPresentation' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { graphScopeOf } from '@/types/graphScopeId' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import type { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { LinkConnectorEventMap } from '@/lib/litegraph/src/infrastructure/LinkConnectorEventMap' import type { Point, SlotIndex } from '@/lib/litegraph/src/interfaces' @@ -13,8 +19,7 @@ import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' + import type { NodeLike } from '@/lib/litegraph/src/types/NodeLike' import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' diff --git a/src/lib/litegraph/src/canvas/MovingOutputLink.ts b/src/lib/litegraph/src/canvas/MovingOutputLink.ts index bb140b10c8d..46b764bf07c 100644 --- a/src/lib/litegraph/src/canvas/MovingOutputLink.ts +++ b/src/lib/litegraph/src/canvas/MovingOutputLink.ts @@ -1,8 +1,13 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { + LGraphNode, + LLink, + Reroute, + SubgraphInput +} from '@/lib/litegraph/src/types/entityRegistry' + import { transferLinkPresentation } from '@/core/graph/transferLinkPresentation' import { graphScopeOf } from '@/types/graphScopeId' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import type { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { LinkConnectorEventMap } from '@/lib/litegraph/src/infrastructure/LinkConnectorEventMap' import type { Point, SlotIndex } from '@/lib/litegraph/src/interfaces' @@ -11,7 +16,7 @@ import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' + import type { NodeLike } from '@/lib/litegraph/src/types/NodeLike' import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' import type { SubgraphIO } from '@/lib/litegraph/src/types/serialisation' @@ -153,3 +158,9 @@ export class MovingOutputLink extends MovingLinkBase { return this.outputNode.disconnectOutput(this.outputIndex, this.inputNode) } } + +type RegisteredMovingOutputLink = MovingOutputLink + +declare module '../types/entityRegistry' { + interface MovingOutputLink extends RegisteredMovingOutputLink {} +} diff --git a/src/lib/litegraph/src/canvas/RenderLink.ts b/src/lib/litegraph/src/canvas/RenderLink.ts index 310f30bdb56..3a68d8034b1 100644 --- a/src/lib/litegraph/src/canvas/RenderLink.ts +++ b/src/lib/litegraph/src/canvas/RenderLink.ts @@ -1,17 +1,21 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { + LGraphNode, + LLink, + Reroute, + SubgraphIONode, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + import type { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { LinkConnectorEventMap } from '@/lib/litegraph/src/infrastructure/LinkConnectorEventMap' import type { Point, SlotIndex } from '@/lib/litegraph/src/interfaces' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' import type { INodeInputSlot, - INodeOutputSlot, - LLink, - Reroute -} from '@/lib/litegraph/src/litegraph' -import type { SubgraphIONodeBase } from '@/lib/litegraph/src/subgraph/SubgraphIONodeBase' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' + INodeOutputSlot +} from '@/lib/litegraph/src/types/slots' + import type { NodeLike } from '@/lib/litegraph/src/types/NodeLike' import type { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' @@ -28,7 +32,7 @@ export interface RenderLink { /** The network that the link belongs to. */ readonly network: LinkNetwork /** The node that the link is being connected from. */ - readonly node: LGraphNode | SubgraphIONodeBase + readonly node: LGraphNode | SubgraphIONode /** The slot that the link is being connected from. */ readonly fromSlot: | INodeOutputSlot @@ -93,3 +97,9 @@ export interface RenderLink { events: CustomEventTarget ): void } + +type RegisteredRenderLink = RenderLink + +declare module '../types/entityRegistry' { + interface RenderLink extends RegisteredRenderLink {} +} diff --git a/src/lib/litegraph/src/canvas/SelectedItemsView.ts b/src/lib/litegraph/src/canvas/SelectedItemsView.ts index eca6e619731..4eb5c96b750 100644 --- a/src/lib/litegraph/src/canvas/SelectedItemsView.ts +++ b/src/lib/litegraph/src/canvas/SelectedItemsView.ts @@ -1,4 +1,5 @@ -import type { LGraph } from '../LGraph' +import type { LGraph } from '@/lib/litegraph/src/types/entityRegistry' + import { clearGraphSelection, updateGraphSelection diff --git a/src/lib/litegraph/src/canvas/ToInputFromIoNodeLink.ts b/src/lib/litegraph/src/canvas/ToInputFromIoNodeLink.ts index 37a2d6b3ac2..2885e46672d 100644 --- a/src/lib/litegraph/src/canvas/ToInputFromIoNodeLink.ts +++ b/src/lib/litegraph/src/canvas/ToInputFromIoNodeLink.ts @@ -1,17 +1,21 @@ +import type { + LGraphNode, + LLink, + Reroute, + SubgraphInput, + SubgraphInputNode +} from '@/lib/litegraph/src/types/entityRegistry' import { transferLinkPresentation } from '@/core/graph/transferLinkPresentation' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { graphScopeOf } from '@/types/graphScopeId' import type { LinkPresentation } from '@/types/linkPresentation' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import type { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { LinkConnectorEventMap } from '@/lib/litegraph/src/infrastructure/LinkConnectorEventMap' import type { Point, SlotIndex } from '@/lib/litegraph/src/interfaces' import type { INodeInputSlot } from '@/lib/litegraph/src/types/slots' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphInputNode } from '@/lib/litegraph/src/subgraph/SubgraphInputNode' + import type { NodeLike } from '@/lib/litegraph/src/types/NodeLike' import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' @@ -182,3 +186,9 @@ export class ToInputFromIoNodeLink implements RenderLink { return true } } + +type RegisteredToInputFromIoNodeLink = ToInputFromIoNodeLink + +declare module '../types/entityRegistry' { + interface ToInputFromIoNodeLink extends RegisteredToInputFromIoNodeLink {} +} diff --git a/src/lib/litegraph/src/canvas/ToInputRenderLink.ts b/src/lib/litegraph/src/canvas/ToInputRenderLink.ts index ccbe3f6b34a..ffc85663f58 100644 --- a/src/lib/litegraph/src/canvas/ToInputRenderLink.ts +++ b/src/lib/litegraph/src/canvas/ToInputRenderLink.ts @@ -1,6 +1,10 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' +import type { + LGraphNode, + LLink, + Reroute, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + import type { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { LinkConnectorEventMap } from '@/lib/litegraph/src/infrastructure/LinkConnectorEventMap' import type { Point, SlotIndex } from '@/lib/litegraph/src/interfaces' @@ -9,7 +13,7 @@ import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' + import type { NodeLike } from '@/lib/litegraph/src/types/NodeLike' import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' @@ -138,3 +142,9 @@ export class ToInputRenderLink implements RenderLink { throw new Error('ToInputRenderLink cannot connect to an output.') } } + +type RegisteredToInputRenderLink = ToInputRenderLink + +declare module '../types/entityRegistry' { + interface ToInputRenderLink extends RegisteredToInputRenderLink {} +} diff --git a/src/lib/litegraph/src/canvas/ToOutputFromIoNodeLink.ts b/src/lib/litegraph/src/canvas/ToOutputFromIoNodeLink.ts index 79b896e6e01..aa3b2c540c8 100644 --- a/src/lib/litegraph/src/canvas/ToOutputFromIoNodeLink.ts +++ b/src/lib/litegraph/src/canvas/ToOutputFromIoNodeLink.ts @@ -1,17 +1,21 @@ +import type { + LGraphNode, + LLink, + Reroute, + SubgraphOutput, + SubgraphOutputNode +} from '@/lib/litegraph/src/types/entityRegistry' import { transferLinkPresentation } from '@/core/graph/transferLinkPresentation' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { graphScopeOf } from '@/types/graphScopeId' import type { LinkPresentation } from '@/types/linkPresentation' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import type { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { LinkConnectorEventMap } from '@/lib/litegraph/src/infrastructure/LinkConnectorEventMap' import type { Point, SlotIndex } from '@/lib/litegraph/src/interfaces' import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' -import type { SubgraphOutputNode } from '@/lib/litegraph/src/subgraph/SubgraphOutputNode' + import type { NodeLike } from '@/lib/litegraph/src/types/NodeLike' import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' import type { SubgraphIO } from '@/lib/litegraph/src/types/serialisation' diff --git a/src/lib/litegraph/src/canvas/ToOutputFromRerouteLink.ts b/src/lib/litegraph/src/canvas/ToOutputFromRerouteLink.ts index a2194985f2d..b116c976381 100644 --- a/src/lib/litegraph/src/canvas/ToOutputFromRerouteLink.ts +++ b/src/lib/litegraph/src/canvas/ToOutputFromRerouteLink.ts @@ -1,15 +1,18 @@ +import type { + LGraphNode, + Reroute, + LinkConnector +} from '@/lib/litegraph/src/types/entityRegistry' import { transferLinkPresentation } from '@/core/graph/transferLinkPresentation' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' import { graphScopeOf } from '@/types/graphScopeId' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import type { INodeInputSlot, - INodeOutputSlot, - LinkNetwork -} from '@/lib/litegraph/src/litegraph' + INodeOutputSlot +} from '@/lib/litegraph/src/types/slots' +import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { LinkConnector } from './LinkConnector' import { ToInputRenderLink } from './ToInputRenderLink' import { ToOutputRenderLink } from './ToOutputRenderLink' diff --git a/src/lib/litegraph/src/canvas/ToOutputRenderLink.ts b/src/lib/litegraph/src/canvas/ToOutputRenderLink.ts index af184826f76..af831ab8ca4 100644 --- a/src/lib/litegraph/src/canvas/ToOutputRenderLink.ts +++ b/src/lib/litegraph/src/canvas/ToOutputRenderLink.ts @@ -1,5 +1,9 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { Reroute } from '@/lib/litegraph/src/Reroute' +import type { + LGraphNode, + Reroute, + SubgraphInput +} from '@/lib/litegraph/src/types/entityRegistry' + import type { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { LinkConnectorEventMap } from '@/lib/litegraph/src/infrastructure/LinkConnectorEventMap' import type { Point, SlotIndex } from '@/lib/litegraph/src/interfaces' @@ -8,7 +12,7 @@ import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' import type { LinkNetwork } from '@/lib/litegraph/src/types/linkNetwork' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' + import type { NodeLike } from '@/lib/litegraph/src/types/NodeLike' import { LinkDirection } from '@/lib/litegraph/src/types/globalEnums' import type { SubgraphIO } from '@/lib/litegraph/src/types/serialisation' diff --git a/src/lib/litegraph/src/canvas/canvasClassAccess.ts b/src/lib/litegraph/src/canvas/canvasClassAccess.ts new file mode 100644 index 00000000000..6d5bd29e622 --- /dev/null +++ b/src/lib/litegraph/src/canvas/canvasClassAccess.ts @@ -0,0 +1,37 @@ +import type { + ContextMenu, + LGraphNode +} from '@/lib/litegraph/src/types/entityRegistry' + +import type { ColorOption } from '../interfaces' +import type { + IContextMenuOptions, + IContextMenuValue +} from '../types/contextMenu' + +interface CanvasClassAccess { + [Symbol.hasInstance](value: unknown): boolean + node_colors: Record + _measureText?: (text: string, fontStyle?: string) => number + onShowPropertyEditor( + item: { property?: 'title' | 'font_size' }, + options: IContextMenuOptions, + event: MouseEvent, + menu: ContextMenu, + node: { title: string; font_size?: number } + ): void + onMenuNodeColors( + value: IContextMenuValue, + options: IContextMenuOptions, + event: MouseEvent, + menu: ContextMenu, + node: LGraphNode + ): boolean + onMenuNodeRemove(): void +} + +export let canvasClass: CanvasClassAccess + +export function provideCanvasClass(value: CanvasClassAccess): void { + canvasClass = value +} diff --git a/src/lib/litegraph/src/canvas/getCanvasContextMenuTarget.ts b/src/lib/litegraph/src/canvas/getCanvasContextMenuTarget.ts index f6c43198c62..42b5620bbd9 100644 --- a/src/lib/litegraph/src/canvas/getCanvasContextMenuTarget.ts +++ b/src/lib/litegraph/src/canvas/getCanvasContextMenuTarget.ts @@ -1,5 +1,8 @@ -import type { LGraphCanvas } from '../LGraphCanvas' -import type { LGraphGroup } from '../LGraphGroup' +import type { + LGraphCanvas, + LGraphGroup +} from '@/lib/litegraph/src/types/entityRegistry' + import { LLink } from '../LLink' import { Reroute } from '../Reroute' import type { LinkSegment } from '../interfaces' diff --git a/src/lib/litegraph/src/canvas/hitTesting.ts b/src/lib/litegraph/src/canvas/hitTesting.ts index fc0a2fd3c32..58295066f7e 100644 --- a/src/lib/litegraph/src/canvas/hitTesting.ts +++ b/src/lib/litegraph/src/canvas/hitTesting.ts @@ -1,8 +1,11 @@ +import type { + LGraph, + LGraphCanvas +} from '@/lib/litegraph/src/types/entityRegistry' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' -import type { LGraph } from '../LGraph' import type { LinkSegment } from '../interfaces' -import type { LGraphCanvas } from '../LGraphCanvas' + import { Reroute } from '../Reroute' /** diff --git a/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts b/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts index c7d5a803edb..0549720c895 100644 --- a/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts +++ b/src/lib/litegraph/src/canvas/linkBadgeRenderer.ts @@ -1,5 +1,5 @@ -import type { LGraph } from '../LGraph' -import type { LLink } from '../LLink' +import type { LGraph, LLink } from '@/lib/litegraph/src/types/entityRegistry' + import type { HiddenLinkBadge } from './linkBadges' import { graphScopeOf } from '@/types/graphScopeId' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' diff --git a/src/lib/litegraph/src/canvas/linkBadges.ts b/src/lib/litegraph/src/canvas/linkBadges.ts index 64acb99da8f..9c83ff02bd2 100644 --- a/src/lib/litegraph/src/canvas/linkBadges.ts +++ b/src/lib/litegraph/src/canvas/linkBadges.ts @@ -1,8 +1,9 @@ +import type { LLink } from '@/lib/litegraph/src/types/entityRegistry' import { textOnColor } from '@/utils/colorUtil' import type { HasBoundingRect, Point, ReadOnlyRect, Rect } from '../interfaces' import { LGraphBadge } from '../LGraphBadge' -import type { LLink } from '../LLink' + import type { LinkId } from '@/types/linkId' import type { LinkPresentation } from '@/types/linkPresentation' import { compareNodeIds } from '@/types/nodeId' diff --git a/src/lib/litegraph/src/canvas/linkGeometry.ts b/src/lib/litegraph/src/canvas/linkGeometry.ts index 11bea2b37a5..78699b2c7bd 100644 --- a/src/lib/litegraph/src/canvas/linkGeometry.ts +++ b/src/lib/litegraph/src/canvas/linkGeometry.ts @@ -1,7 +1,7 @@ -import type { LGraph } from '../LGraph' -import type { LLink } from '../LLink' +import type { LGraph, LLink } from '@/lib/litegraph/src/types/entityRegistry' + import type { Point } from '../interfaces' -import { LiteGraph } from '../litegraph' +import { litegraph } from '../litegraphInstance' import { inputLinkId } from '../node/slotLinks' import { getSlotPosition } from '@/renderer/core/canvas/litegraph/slotCalculations' @@ -16,7 +16,7 @@ export function getLinkEndpointPositions( return } - const endPos = LiteGraph.vueNodesMode + const endPos = litegraph().vueNodesMode ? getSlotPosition(inputNode, link.target_slot, true) : inputNode.getInputPos(link.target_slot) return [subgraphInput.pos, endPos] @@ -28,7 +28,7 @@ export function getLinkEndpointPositions( return } - const startPos = LiteGraph.vueNodesMode + const startPos = litegraph().vueNodesMode ? getSlotPosition(outputNode, link.origin_slot, false) : outputNode.getOutputPos(link.origin_slot) return [startPos, subgraphOutput.pos] @@ -42,10 +42,10 @@ export function getLinkEndpointPositions( return if (!outputNode || !output) return - const endPos: Point = LiteGraph.vueNodesMode + const endPos: Point = litegraph().vueNodesMode ? getSlotPosition(inputNode, link.target_slot, true) : inputNode.getInputPos(link.target_slot) - const startPos: Point = LiteGraph.vueNodesMode + const startPos: Point = litegraph().vueNodesMode ? getSlotPosition(outputNode, link.origin_slot, false) : outputNode.getOutputPos(link.origin_slot) diff --git a/src/lib/litegraph/src/canvas/linkVisibility.ts b/src/lib/litegraph/src/canvas/linkVisibility.ts index 9ea08d7d27e..5791dd8e660 100644 --- a/src/lib/litegraph/src/canvas/linkVisibility.ts +++ b/src/lib/litegraph/src/canvas/linkVisibility.ts @@ -1,3 +1,8 @@ +import type { + LGraph, + LLink, + Reroute +} from '@/lib/litegraph/src/types/entityRegistry' import { st, t } from '@/i18n' import type { IContextMenuValue } from '../types/contextMenu' import { useLinkPresentationStore } from '@/stores/linkPresentationStore' @@ -5,10 +10,7 @@ import { graphScopeOf } from '@/types/graphScopeId' import type { GraphScope } from '@/types/graphScopeId' import type { LinkId } from '@/types/linkId' -import type { LGraph } from '../LGraph' import { Subgraph } from '../subgraph/Subgraph' -import type { LLink } from '../LLink' -import type { Reroute } from '../Reroute' import type { CanvasPointerEvent } from '../types/events' export interface LinkMutationHost { diff --git a/src/lib/litegraph/src/canvas/measureSlots.ts b/src/lib/litegraph/src/canvas/measureSlots.ts index d5761c37885..7306c40f8cd 100644 --- a/src/lib/litegraph/src/canvas/measureSlots.ts +++ b/src/lib/litegraph/src/canvas/measureSlots.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { Point } from '@/lib/litegraph/src/interfaces' import type { INodeInputSlot, diff --git a/src/lib/litegraph/src/canvas/resolvePointerTarget.ts b/src/lib/litegraph/src/canvas/resolvePointerTarget.ts index 009b35f08ce..0fe2b5d3e24 100644 --- a/src/lib/litegraph/src/canvas/resolvePointerTarget.ts +++ b/src/lib/litegraph/src/canvas/resolvePointerTarget.ts @@ -1,13 +1,17 @@ -import type { LGraph } from '../LGraph' -import type { LGraphCanvas } from '../LGraphCanvas' -import type { LGraphGroup } from '../LGraphGroup' -import type { LGraphNode } from '../LGraphNode' -import type { LLink } from '../LLink' -import type { Reroute } from '../Reroute' +import type { + LGraph, + LGraphCanvas, + LGraphGroup, + LGraphNode, + LLink, + Reroute, + SubgraphInputNode, + SubgraphOutputNode +} from '@/lib/litegraph/src/types/entityRegistry' + import type { LinkSegment, Point, Positionable } from '../interfaces' import { isInRectangle } from '../measure' -import type { SubgraphInputNode } from '../subgraph/SubgraphInputNode' -import type { SubgraphOutputNode } from '../subgraph/SubgraphOutputNode' + import { LinkMarkerShape, LinkRenderType } from '../types/globalEnums' import type { CanvasPointerEvent } from '../types/events' import { diff --git a/src/lib/litegraph/src/graphIntents.ts b/src/lib/litegraph/src/graphIntents.ts index 9e25114f08a..320813fc01d 100644 --- a/src/lib/litegraph/src/graphIntents.ts +++ b/src/lib/litegraph/src/graphIntents.ts @@ -1,3 +1,8 @@ +import type { + LGraph, + LGraphNode, + LLink +} from '@/lib/litegraph/src/types/entityRegistry' /** * Graph mutations announced as commands from the graph-API funnel they run * through (`LGraph.add/remove/_addLink/_removeLink/clear`, the widget value @@ -12,10 +17,6 @@ import { reportError } from '@/platform/telemetry/reportError' import type { LinkId } from '@/types/linkId' import type { NodeId } from '@/types/nodeId' -import type { LGraph } from './LGraph' -import type { LGraphNode } from './LGraphNode' -import type { LLink } from './LLink' - export type GraphIntentSource = 'local' | 'agent-remote' | 'load' /** diff --git a/src/lib/litegraph/src/idAllocation.ts b/src/lib/litegraph/src/idAllocation.ts index c1c4cbe122e..afa7a53e5d0 100644 --- a/src/lib/litegraph/src/idAllocation.ts +++ b/src/lib/litegraph/src/idAllocation.ts @@ -1,4 +1,5 @@ -import type { LGraph } from './LGraph' +import type { LGraph } from '@/lib/litegraph/src/types/entityRegistry' + import type { ISerialisedGroup, ISerialisedNode, diff --git a/src/lib/litegraph/src/infrastructure/LGraphCanvasEventMap.ts b/src/lib/litegraph/src/infrastructure/LGraphCanvasEventMap.ts index 5775f7525d9..b67dd1ca177 100644 --- a/src/lib/litegraph/src/infrastructure/LGraphCanvasEventMap.ts +++ b/src/lib/litegraph/src/infrastructure/LGraphCanvasEventMap.ts @@ -1,11 +1,16 @@ -import type { LGraph } from '@/lib/litegraph/src/LGraph' +import type { + LGraph, + LGraphGroup, + LGraphNode, + Subgraph, + SubgraphNode +} from '@/lib/litegraph/src/types/entityRegistry' + import type { LGraphButton } from '@/lib/litegraph/src/LGraphButton' -import type { LGraphGroup } from '@/lib/litegraph/src/LGraphGroup' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' + import type { SerializedNodeId } from '@/types/nodeId' import type { ConnectingLink } from '@/lib/litegraph/src/types/slots' -import type { Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph' -import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' + import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events' export interface LGraphCanvasEventMap { diff --git a/src/lib/litegraph/src/infrastructure/LGraphEventMap.ts b/src/lib/litegraph/src/infrastructure/LGraphEventMap.ts index 62fe2aae93d..c0f6447aec8 100644 --- a/src/lib/litegraph/src/infrastructure/LGraphEventMap.ts +++ b/src/lib/litegraph/src/infrastructure/LGraphEventMap.ts @@ -1,9 +1,15 @@ -import type { LGraph } from '@/lib/litegraph/src/LGraph' +import type { + LGraph, + LGraphNode, + LLink, + ResolvedConnection, + Subgraph +} from '@/lib/litegraph/src/types/entityRegistry' + import type { SerializedNodeId } from '@/types/nodeId' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink, ResolvedConnection } from '@/lib/litegraph/src/LLink' + import type { ReadOnlyRect } from '@/lib/litegraph/src/interfaces' -import type { Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph' + import type { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums' import type { ExportedSubgraph, diff --git a/src/lib/litegraph/src/infrastructure/LinkConnectorEventMap.ts b/src/lib/litegraph/src/infrastructure/LinkConnectorEventMap.ts index 837c131dedc..c305919ca95 100644 --- a/src/lib/litegraph/src/infrastructure/LinkConnectorEventMap.ts +++ b/src/lib/litegraph/src/infrastructure/LinkConnectorEventMap.ts @@ -1,14 +1,17 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' -import type { FloatingRenderLink } from '@/lib/litegraph/src/canvas/FloatingRenderLink' -import type { MovingInputLink } from '@/lib/litegraph/src/canvas/MovingInputLink' -import type { MovingOutputLink } from '@/lib/litegraph/src/canvas/MovingOutputLink' -import type { RenderLink } from '@/lib/litegraph/src/canvas/RenderLink' -import type { ToInputFromIoNodeLink } from '@/lib/litegraph/src/canvas/ToInputFromIoNodeLink' -import type { ToInputRenderLink } from '@/lib/litegraph/src/canvas/ToInputRenderLink' -import type { SubgraphInputNode } from '@/lib/litegraph/src/subgraph/SubgraphInputNode' -import type { SubgraphOutputNode } from '@/lib/litegraph/src/subgraph/SubgraphOutputNode' +import type { + LGraphNode, + LLink, + Reroute, + FloatingRenderLink, + MovingInputLink, + MovingOutputLink, + ToInputFromIoNodeLink, + ToInputRenderLink, + SubgraphInputNode, + SubgraphOutputNode, + RenderLink +} from '@/lib/litegraph/src/types/entityRegistry' + import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events' import type { IWidget } from '@/lib/litegraph/src/types/widgets' diff --git a/src/lib/litegraph/src/infrastructure/SubgraphEventMap.ts b/src/lib/litegraph/src/infrastructure/SubgraphEventMap.ts index bb74f39f345..8bf6eb189dd 100644 --- a/src/lib/litegraph/src/infrastructure/SubgraphEventMap.ts +++ b/src/lib/litegraph/src/infrastructure/SubgraphEventMap.ts @@ -1,7 +1,10 @@ -import type { Subgraph } from '@/lib/litegraph/src/subgraph/Subgraph' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' +import type { + Subgraph, + SubgraphInput, + SubgraphNode, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { LGraphEventMap } from './LGraphEventMap' diff --git a/src/lib/litegraph/src/infrastructure/SubgraphInputEventMap.ts b/src/lib/litegraph/src/infrastructure/SubgraphInputEventMap.ts index 3bf43110511..0112f081b98 100644 --- a/src/lib/litegraph/src/infrastructure/SubgraphInputEventMap.ts +++ b/src/lib/litegraph/src/infrastructure/SubgraphInputEventMap.ts @@ -1,6 +1,10 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { INodeInputSlot } from '@/lib/litegraph/src/litegraph' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' +import type { + LGraphNode, + SubgraphInput +} from '@/lib/litegraph/src/types/entityRegistry' + +import type { INodeInputSlot } from '@/lib/litegraph/src/types/slots' + import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import type { LGraphEventMap } from './LGraphEventMap' diff --git a/src/lib/litegraph/src/linkDeduplication.ts b/src/lib/litegraph/src/linkDeduplication.ts index be0359220f7..af531ac38bc 100644 --- a/src/lib/litegraph/src/linkDeduplication.ts +++ b/src/lib/litegraph/src/linkDeduplication.ts @@ -1,3 +1,9 @@ +import type { + LGraph, + LGraphNode, + LLink, + SerialisedLLinkArray +} from '@/lib/litegraph/src/types/entityRegistry' import { useTelemetry } from '@/platform/telemetry' import { useLinkStore } from '@/stores/linkStore' import { graphScopeOf } from '@/types/graphScopeId' @@ -6,9 +12,8 @@ import { parseLinkId, toLinkId } from '@/types/linkId' import { toNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' import cloneDeep from 'es-toolkit/compat/cloneDeep' -import type { LGraph } from './LGraph' -import type { LGraphNode } from './LGraphNode' -import type { LinkId, LLink, SerialisedLLinkArray } from './LLink' + +import type { LinkId } from '@/types/linkId' import type { ExportedSubgraph, ISerialisedGraph, diff --git a/src/lib/litegraph/src/litegraph.ts b/src/lib/litegraph/src/litegraph.ts index 1d63166ace6..66186f34b06 100644 --- a/src/lib/litegraph/src/litegraph.ts +++ b/src/lib/litegraph/src/litegraph.ts @@ -1,13 +1,15 @@ -import type { ContextMenu } from './ContextMenu' -import type { LGraphNode } from './LGraphNode' +import type { + ContextMenu, + LGraphNode +} from '@/lib/litegraph/src/types/entityRegistry' + import { LiteGraphGlobal } from './LiteGraphGlobal' -import type { Point, Size } from './interfaces' +import type { Point } from './interfaces' import type { ConnectingLink } from './types/slots' import type { IContextMenuOptions } from './types/contextMenu' import { registerLiteGraphInstance } from './litegraphInstance' import { loadPolyfills } from './polyfills' import type { CanvasEventDetail } from './types/events' -import type { RenderShape, TitleMode } from './types/globalEnums' // Must remain above LiteGraphGlobal (circular dependency due to abstract factory behaviour in `configure`) export { Subgraph } from './subgraph/Subgraph' @@ -53,29 +55,6 @@ export interface LinkReleaseContextExtended { export interface LiteGraphCanvasEvent extends CustomEvent {} -export interface LGraphNodeConstructor { - new (title: string, type?: string): T - - title?: string - desc?: string - priority?: number - type?: string // TODO: to be, or not to be--that is the question - size?: Size - min_height?: number - slot_start_y?: number - widgets_info?: Record - collapsable?: boolean - color?: string - bgcolor?: string - shape?: RenderShape - title_mode?: TitleMode - title_color?: string - title_text_color?: string - keepAllLinksOnBypass: boolean - resizeHandleSize?: number - resizeEdgeSize?: number -} - // End backwards compat export type { CanvasInteractionModeReader } from './canvas/CanvasInteractionMode' @@ -180,3 +159,10 @@ export { MovingInputLink } from './canvas/MovingInputLink' export { ToInputRenderLink } from './canvas/ToInputRenderLink' export { LiteGraphGlobal } from './LiteGraphGlobal' export type { CreateNodeOptions } from './LiteGraphGlobal' + +type RegisteredLinkReleaseContextExtended = LinkReleaseContextExtended + +declare module './types/entityRegistry' { + interface LinkReleaseContextExtended extends RegisteredLinkReleaseContextExtended {} +} +export type { LGraphNodeConstructor } from './types/nodeConstructor' diff --git a/src/lib/litegraph/src/litegraphInstance.ts b/src/lib/litegraph/src/litegraphInstance.ts index 11f3cbc2143..0812e1a04cd 100644 --- a/src/lib/litegraph/src/litegraphInstance.ts +++ b/src/lib/litegraph/src/litegraphInstance.ts @@ -1,4 +1,4 @@ -import type { LiteGraphGlobal } from './LiteGraphGlobal' +import type { LiteGraphGlobal } from '@/lib/litegraph/src/types/entityRegistry' declare global { var __COMFY_LITEGRAPH_INSTANCE__: LiteGraphGlobal | undefined @@ -17,9 +17,11 @@ declare global { * The barrel constructs the singleton and calls {@link registerLiteGraphInstance}. */ let instance: LiteGraphGlobal | null = null +export let LiteGraph: LiteGraphGlobal export function registerLiteGraphInstance(value: LiteGraphGlobal): void { instance = value + LiteGraph = value globalThis.__COMFY_LITEGRAPH_INSTANCE__ = value } diff --git a/src/lib/litegraph/src/node/NodeInputSlot.ts b/src/lib/litegraph/src/node/NodeInputSlot.ts index af0e58c3c05..e0761f22dde 100644 --- a/src/lib/litegraph/src/node/NodeInputSlot.ts +++ b/src/lib/litegraph/src/node/NodeInputSlot.ts @@ -1,19 +1,27 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink, LinkId } from '@/lib/litegraph/src/LLink' +import type { + LGraphNode, + LLink, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + +import type { LinkId } from '@/types/linkId' import { LabelPosition } from '@/lib/litegraph/src/draw' import type { OptionalProps, Point } from '@/lib/litegraph/src/interfaces' import type { INodeInputSlot, INodeOutputSlot } from '@/lib/litegraph/src/types/slots' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' -import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' +import { + NodeSlot, + provideNodeInputSlotGuard +} from '@/lib/litegraph/src/node/NodeSlot' import { inputHasLink, inputLink } from '@/lib/litegraph/src/node/slotLinks' import { warnDeprecated } from '@/lib/litegraph/src/utils/feedback' import type { IDrawOptions } from '@/lib/litegraph/src/node/NodeSlot' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' -import { isSubgraphInput } from '@/lib/litegraph/src/subgraph/subgraphUtils' + +import { isSubgraphInput } from '@/lib/litegraph/src/subgraph/subgraphTypeGuards' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' export class NodeInputSlot extends NodeSlot implements INodeInputSlot { @@ -130,3 +138,5 @@ function linkOf(slot: NodeInputSlot): LLink | undefined { const { graph } = slot.node return graph ? inputLink(graph, slot.node.id, indexOf(slot)) : undefined } + +provideNodeInputSlotGuard((slot) => slot instanceof NodeInputSlot) diff --git a/src/lib/litegraph/src/node/NodeOutputSlot.ts b/src/lib/litegraph/src/node/NodeOutputSlot.ts index 23dfdb4a608..5526e06a6bc 100644 --- a/src/lib/litegraph/src/node/NodeOutputSlot.ts +++ b/src/lib/litegraph/src/node/NodeOutputSlot.ts @@ -1,19 +1,24 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink, LinkId } from '@/lib/litegraph/src/LLink' +import type { + LGraphNode, + LLink, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + +import type { LinkId } from '@/types/linkId' import { LabelPosition } from '@/lib/litegraph/src/draw' import type { OptionalProps, Point } from '@/lib/litegraph/src/interfaces' import type { INodeInputSlot, INodeOutputSlot } from '@/lib/litegraph/src/types/slots' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' import { createArrayMutationView } from '@/lib/litegraph/src/infrastructure/createMutationView' import { NodeSlot } from '@/lib/litegraph/src/node/NodeSlot' import type { IDrawOptions } from '@/lib/litegraph/src/node/NodeSlot' import { outputHasLinks, outputLinks } from '@/lib/litegraph/src/node/slotLinks' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' -import { isSubgraphOutput } from '@/lib/litegraph/src/subgraph/subgraphUtils' + +import { isSubgraphOutput } from '@/lib/litegraph/src/subgraph/subgraphTypeGuards' import { warnDeprecated } from '@/lib/litegraph/src/utils/feedback' export class NodeOutputSlot extends NodeSlot implements INodeOutputSlot { diff --git a/src/lib/litegraph/src/node/NodeSlot.ts b/src/lib/litegraph/src/node/NodeSlot.ts index 1400099ef01..334aa4eb976 100644 --- a/src/lib/litegraph/src/node/NodeSlot.ts +++ b/src/lib/litegraph/src/node/NodeSlot.ts @@ -1,7 +1,12 @@ +import type { + LGraphNode, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' import { shallowReactive } from 'vue' import { MAX_MULTITYPE_SLICES } from '@/constants/slotColors' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' + import { LabelPosition, SlotShape, SlotType } from '@/lib/litegraph/src/draw' import type { CanvasColour, @@ -15,18 +20,25 @@ import type { INodeOutputSlot, ISubgraphInput } from '@/lib/litegraph/src/types/slots' -import { LiteGraph, Rectangle } from '@/lib/litegraph/src/litegraph' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' +import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle' import { getCentre } from '@/lib/litegraph/src/measure' -import type { SubgraphInput } from '@/lib/litegraph/src/subgraph/SubgraphInput' -import type { SubgraphOutput } from '@/lib/litegraph/src/subgraph/SubgraphOutput' + import { LinkDirection, RenderShape } from '@/lib/litegraph/src/types/globalEnums' -import { NodeInputSlot } from './NodeInputSlot' import { SlotBase } from './SlotBase' +let isNodeInputSlot: (slot: NodeSlot) => boolean + +export function provideNodeInputSlotGuard( + guard: (slot: NodeSlot) => boolean +): void { + isNodeInputSlot = guard +} + export interface IDrawOptions { colorContext: DefaultConnectionColors labelPosition?: LabelPosition @@ -292,7 +304,7 @@ export abstract class NodeSlot extends SlotBase implements INodeSlot { ctx.rect(x - 7 + 0.5, y - 4, 14, 8) } else if (this.shape === RenderShape.ARROW) { // Adjust arrow direction based on whether this is an input or output slot - const isInput = this instanceof NodeInputSlot + const isInput = isNodeInputSlot(this) if (isInput) { ctx.moveTo(x + 8, y) ctx.lineTo(x - 4, y - 4) diff --git a/src/lib/litegraph/src/node/slotDescriptorView.ts b/src/lib/litegraph/src/node/slotDescriptorView.ts index c20bef00f56..e22c79ac0c3 100644 --- a/src/lib/litegraph/src/node/slotDescriptorView.ts +++ b/src/lib/litegraph/src/node/slotDescriptorView.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { INodeInputSlot, INodeOutputSlot diff --git a/src/lib/litegraph/src/node/slotLinks.ts b/src/lib/litegraph/src/node/slotLinks.ts index f66910c8b85..7b85d671c8d 100644 --- a/src/lib/litegraph/src/node/slotLinks.ts +++ b/src/lib/litegraph/src/node/slotLinks.ts @@ -1,3 +1,8 @@ +import type { + LGraph, + LGraphNode, + LLink +} from '@/lib/litegraph/src/types/entityRegistry' import { useLinkStore } from '@/stores/linkStore' import type { EndpointUpdateError } from '@/stores/linkStore' import { graphScopeOf } from '@/types/graphScopeId' @@ -5,11 +10,9 @@ import type { LinkId } from '@/types/linkId' import type { NodeId } from '@/types/nodeId' import type { RerouteId } from '@/types/rerouteId' -import type { LGraph } from '../LGraph' -import type { LGraphNode } from '../LGraphNode' import type { INodeInputSlot } from '../types/slots' import { slotFloatingLinks } from '../LLink' -import type { LLink } from '../LLink' + import { NodeSlotType } from '../types/globalEnums' /** * Store-backed reads over the links attached to a node's slots. diff --git a/src/lib/litegraph/src/node/slotUtils.ts b/src/lib/litegraph/src/node/slotUtils.ts index 6e28e80c765..169fa592c91 100644 --- a/src/lib/litegraph/src/node/slotUtils.ts +++ b/src/lib/litegraph/src/node/slotUtils.ts @@ -1,11 +1,12 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LinkId } from '@/lib/litegraph/src/LLink' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + +import type { LinkId } from '@/types/linkId' import type { SharedIntersection } from '@/lib/litegraph/src/interfaces' -import type { IWidgetInputSlot } from '@/lib/litegraph/src/types/slots' import type { + IWidgetInputSlot, INodeInputSlot, INodeOutputSlot -} from '@/lib/litegraph/src/litegraph' +} from '@/lib/litegraph/src/types/slots' import { inputLinkId, outputLinkIds } from '@/lib/litegraph/src/node/slotLinks' import type { ISerialisableNodeInput, diff --git a/src/lib/litegraph/src/node/widgetsView.ts b/src/lib/litegraph/src/node/widgetsView.ts index 1fff0a7e20b..c468eced3da 100644 --- a/src/lib/litegraph/src/node/widgetsView.ts +++ b/src/lib/litegraph/src/node/widgetsView.ts @@ -1,6 +1,6 @@ +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' import { shallowReactive } from 'vue' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { toConcreteWidget } from '@/lib/litegraph/src/widgets/widgetMap' import { useWidgetValueStore } from '@/stores/widgetValueStore' diff --git a/src/lib/litegraph/src/nodeBadgeDraw.ts b/src/lib/litegraph/src/nodeBadgeDraw.ts index d65167af9ff..0e4c0b18b8f 100644 --- a/src/lib/litegraph/src/nodeBadgeDraw.ts +++ b/src/lib/litegraph/src/nodeBadgeDraw.ts @@ -1,10 +1,10 @@ +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' import { truncate } from 'es-toolkit/compat' import type { BadgeData, CoreBadgeData } from '@/types/badgeData' import { LGraphBadge } from './LGraphBadge' import type { LGraphIconOptions } from './LGraphIcon' -import type { LGraphNode } from './LGraphNode' const CORE_TEXT_LIMIT = 31 diff --git a/src/lib/litegraph/src/remintLinkRemap.ts b/src/lib/litegraph/src/remintLinkRemap.ts index 04f5c942470..581f9417afe 100644 --- a/src/lib/litegraph/src/remintLinkRemap.ts +++ b/src/lib/litegraph/src/remintLinkRemap.ts @@ -1,6 +1,6 @@ +import type { LLink } from '@/lib/litegraph/src/types/entityRegistry' import { UNASSIGNED_NODE_ID, toNodeId } from '@/types/nodeId' -import type { LLink } from './LLink' import type { ISerialisedNode } from './types/serialisation' import type { EndpointPatch } from '@/stores/linkStore' import type { NodeId } from '@/types/nodeId' diff --git a/src/lib/litegraph/src/strings.ts b/src/lib/litegraph/src/strings.ts index 2ce4c237894..c24db6b665f 100644 --- a/src/lib/litegraph/src/strings.ts +++ b/src/lib/litegraph/src/strings.ts @@ -1,4 +1,4 @@ -import type { ISlotType } from './litegraph' +import type { ISlotType } from './interfaces' export function parseSlotTypes(type: ISlotType): string[] { return type == '' || type == '0' diff --git a/src/lib/litegraph/src/subgraph/EmptySubgraphInput.ts b/src/lib/litegraph/src/subgraph/EmptySubgraphInput.ts index 8bdf44d22d1..f030df08f88 100644 --- a/src/lib/litegraph/src/subgraph/EmptySubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/EmptySubgraphInput.ts @@ -1,13 +1,16 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { RerouteId } from '@/lib/litegraph/src/Reroute' +import type { + LGraphNode, + LLink, + SubgraphInputNode +} from '@/lib/litegraph/src/types/entityRegistry' + +import type { RerouteId } from '@/types/rerouteId' import type { Point } from '@/lib/litegraph/src/interfaces' import type { INodeInputSlot } from '@/lib/litegraph/src/types/slots' import { nextUniqueName } from '@/lib/litegraph/src/strings' import { zeroUuid } from '@/utils/uuid' import { SubgraphInput } from './SubgraphInput' -import type { SubgraphInputNode } from './SubgraphInputNode' /** * A virtual slot that simply creates a new input slot when connected to. diff --git a/src/lib/litegraph/src/subgraph/EmptySubgraphOutput.ts b/src/lib/litegraph/src/subgraph/EmptySubgraphOutput.ts index 62cf76b737d..584d81a8dda 100644 --- a/src/lib/litegraph/src/subgraph/EmptySubgraphOutput.ts +++ b/src/lib/litegraph/src/subgraph/EmptySubgraphOutput.ts @@ -1,13 +1,16 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { RerouteId } from '@/lib/litegraph/src/Reroute' +import type { + LGraphNode, + LLink, + SubgraphOutputNode +} from '@/lib/litegraph/src/types/entityRegistry' + +import type { RerouteId } from '@/types/rerouteId' import type { Point } from '@/lib/litegraph/src/interfaces' import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' import { nextUniqueName } from '@/lib/litegraph/src/strings' import { zeroUuid } from '@/utils/uuid' import { SubgraphOutput } from './SubgraphOutput' -import type { SubgraphOutputNode } from './SubgraphOutputNode' /** * A virtual slot that simply creates a new output slot when connected to. diff --git a/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts b/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts index 11f41ca578e..164a96c77f1 100644 --- a/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts +++ b/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts @@ -1,18 +1,21 @@ +import type { + LGraph, + LGraphNode, + Subgraph, + SubgraphNode +} from '@/lib/litegraph/src/types/entityRegistry' import { inputLinkId } from '@/lib/litegraph/src/node/slotLinks' -import type { LGraph } from '@/lib/litegraph/src/LGraph' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' + import type { SerializedNodeId } from '@/types/nodeId' -import type { LinkId } from '@/lib/litegraph/src/LLink' +import type { LinkId } from '@/types/linkId' import { InvalidLinkError } from '@/lib/litegraph/src/infrastructure/InvalidLinkError' import { useWidgetValueStore } from '@/stores/widgetValueStore' import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError' import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError' import { SlotIndexError } from '@/lib/litegraph/src/infrastructure/SlotIndexError' import type { ISlotType } from '@/lib/litegraph/src/interfaces' -import { LGraphEventMode, LiteGraph } from '@/lib/litegraph/src/litegraph' - -import type { Subgraph } from './Subgraph' -import type { SubgraphNode } from './SubgraphNode' +import { LGraphEventMode } from '@/lib/litegraph/src/types/globalEnums' +import { litegraph } from '@/lib/litegraph/src/litegraphInstance' export type ExecutionId = string @@ -366,8 +369,8 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { // Prefer input with the same slot ID if ( oppositeInput && - LiteGraph.isValidConnection(oppositeInput.type, outputType) && - LiteGraph.isValidConnection(oppositeInput.type, type) + litegraph().isValidConnection(oppositeInput.type, outputType) && + litegraph().isValidConnection(oppositeInput.type, type) ) { return slot } @@ -379,8 +382,8 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { // Find first matching slot - prefer exact type return inputs.findIndex( (input) => - LiteGraph.isValidConnection(input.type, outputType) && - LiteGraph.isValidConnection(input.type, type) + litegraph().isValidConnection(input.type, outputType) && + litegraph().isValidConnection(input.type, type) ) } diff --git a/src/lib/litegraph/src/subgraph/SubgraphIONodeBase.ts b/src/lib/litegraph/src/subgraph/SubgraphIONodeBase.ts index 95eb6ff2851..30d25d42f1b 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphIONodeBase.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphIONodeBase.ts @@ -1,27 +1,33 @@ +import type { + LinkConnector, + Subgraph, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' import { toSelectableKey } from '@/core/selection/selectionState' import { isSelectedIn, setSelectedIn } from '@/core/selection/selectionStore' import { graphScopeOf } from '@/types/graphScopeId' import { serializeNodeId } from '@/types/nodeId' import type { NodeId } from '@/types/nodeId' -import type { LinkConnector } from '@/lib/litegraph/src/canvas/LinkConnector' + import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle' import type { DefaultConnectionColors, Point, - Positionable + Positionable, + CanvasColour } from '@/lib/litegraph/src/interfaces' -import type { Hoverable } from '@/lib/litegraph/src/types/events' +import type { + Hoverable, + CanvasPointerEvent +} from '@/lib/litegraph/src/types/events' import type { INodeInputSlot, INodeOutputSlot } from '@/lib/litegraph/src/types/slots' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' -import type { - CanvasColour, - CanvasPointer, - CanvasPointerEvent, - IContextMenuValue -} from '@/lib/litegraph/src/litegraph' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' +import type { CanvasPointer } from '@/lib/litegraph/src/CanvasPointer' +import type { IContextMenuValue } from '@/lib/litegraph/src/types/contextMenu' import { snapPoint } from '@/lib/litegraph/src/measure' import { CanvasItem } from '@/lib/litegraph/src/types/globalEnums' import type { @@ -31,9 +37,6 @@ import type { import type { EmptySubgraphInput } from './EmptySubgraphInput' import type { EmptySubgraphOutput } from './EmptySubgraphOutput' -import type { Subgraph } from './Subgraph' -import type { SubgraphInput } from './SubgraphInput' -import type { SubgraphOutput } from './SubgraphOutput' export abstract class SubgraphIONodeBase< TSlot extends SubgraphInput | SubgraphOutput @@ -398,3 +401,11 @@ export abstract class SubgraphIONodeBase< } } } + +type RegisteredSubgraphIONode = SubgraphIONodeBase< + SubgraphInput | SubgraphOutput +> + +declare module '../types/entityRegistry' { + interface SubgraphIONode extends RegisteredSubgraphIONode {} +} diff --git a/src/lib/litegraph/src/subgraph/SubgraphInput.ts b/src/lib/litegraph/src/subgraph/SubgraphInput.ts index 2b8974aae09..cae03cbbc86 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInput.ts @@ -1,9 +1,14 @@ +import type { + LGraphNode, + SubgraphInputNode, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' import { inputLink } from '@/lib/litegraph/src/node/slotLinks' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' + import { LLink, replaceLinkTopology } from '@/lib/litegraph/src/LLink' import { linkIdReservations, mintLinkId } from '../idAllocation' import { anchorRerouteChain } from '@/lib/litegraph/src/Reroute' -import type { RerouteId } from '@/lib/litegraph/src/Reroute' +import type { RerouteId } from '@/types/rerouteId' import { CustomEventTarget } from '@/lib/litegraph/src/infrastructure/CustomEventTarget' import type { SubgraphInputEventMap } from '@/lib/litegraph/src/infrastructure/SubgraphInputEventMap' import type { Point, ReadOnlyRect } from '@/lib/litegraph/src/interfaces' @@ -11,14 +16,12 @@ import type { INodeInputSlot, INodeOutputSlot } from '@/lib/litegraph/src/types/slots' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' -import type { SubgraphInputNode } from './SubgraphInputNode' -import type { SubgraphOutput } from './SubgraphOutput' import { SubgraphSlot } from './SubgraphSlotBase' -import { isNodeSlot, isSubgraphOutput } from './subgraphUtils' +import { isNodeSlot, isSubgraphOutput } from './subgraphTypeGuards' /** * An input "slot" from a parent graph into a subgraph. @@ -251,3 +254,9 @@ export class SubgraphInput extends SubgraphSlot { return false } } + +type RegisteredSubgraphInput = SubgraphInput + +declare module '../types/entityRegistry' { + interface SubgraphInput extends RegisteredSubgraphInput {} +} diff --git a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts index 61fd158fc65..33a7b1490c9 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphInputNode.ts @@ -1,10 +1,16 @@ +import type { + LGraphNode, + LinkConnector, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' import type { CanvasPointer } from '@/lib/litegraph/src/CanvasPointer' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' + import type { NodeId } from '@/types/nodeId' import { LLink, slotFloatingLinks } from '@/lib/litegraph/src/LLink' import { linkIdReservations, mintLinkId } from '../idAllocation' -import type { RerouteId } from '@/lib/litegraph/src/Reroute' -import type { LinkConnector } from '@/lib/litegraph/src/canvas/LinkConnector' +import type { RerouteId } from '@/types/rerouteId' + import { SUBGRAPH_INPUT_ID } from '@/lib/litegraph/src/constants' import type { DefaultConnectionColors, @@ -23,8 +29,8 @@ import { nextUniqueName } from '@/lib/litegraph/src/strings' import { EmptySubgraphInput } from './EmptySubgraphInput' import { SubgraphIONodeBase } from './SubgraphIONodeBase' -import type { SubgraphInput } from './SubgraphInput' -import type { SubgraphOutput } from './SubgraphOutput' + +import { provideSubgraphInputGuard } from './subgraphTypeGuards' export class SubgraphInputNode extends SubgraphIONodeBase @@ -288,3 +294,20 @@ export class SubgraphInputNode this.drawSlots(ctx, colorContext, fromSlot, editorAlpha) } } + +function isSubgraphInput(slot: unknown): slot is SubgraphInput { + return ( + slot != null && + typeof slot === 'object' && + 'parent' in slot && + slot.parent instanceof SubgraphInputNode + ) +} + +provideSubgraphInputGuard(isSubgraphInput) + +type RegisteredSubgraphInputNode = SubgraphInputNode + +declare module '../types/entityRegistry' { + interface SubgraphInputNode extends RegisteredSubgraphInputNode {} +} diff --git a/src/lib/litegraph/src/subgraph/SubgraphNode.ts b/src/lib/litegraph/src/subgraph/SubgraphNode.ts index 37ee451cf6c..373783059c7 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphNode.ts @@ -1,25 +1,34 @@ +import type { + BaseLGraph, + LGraph, + SubgraphId, + LGraphCanvas, + ResolvedConnection, + GraphOrSubgraph, + Subgraph, + SubgraphInput +} from '@/lib/litegraph/src/types/entityRegistry' import cloneDeep from 'es-toolkit/compat/cloneDeep' -import type { BaseLGraph, LGraph, SubgraphId } from '@/lib/litegraph/src/LGraph' + import type { LGraphButton } from '@/lib/litegraph/src/LGraphButton' -import type { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas' + import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { DrawTitleBoxOptions } from '@/lib/litegraph/src/LGraphNode' import { LLink } from '@/lib/litegraph/src/LLink' -import type { ResolvedConnection } from '@/lib/litegraph/src/LLink' + import { NullGraphError } from '@/lib/litegraph/src/infrastructure/NullGraphError' import { RecursionError } from '@/lib/litegraph/src/infrastructure/RecursionError' -import type { IWidgetLocator } from '@/lib/litegraph/src/interfaces' -import type { ISubgraphInput } from '@/lib/litegraph/src/types/slots' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' -import type { INodeInputSlot, ISlotType } from '@/lib/litegraph/src/litegraph' +import type { IWidgetLocator, ISlotType } from '@/lib/litegraph/src/interfaces' +import type { + ISubgraphInput, + INodeInputSlot +} from '@/lib/litegraph/src/types/slots' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' import { NodeInputSlot } from '@/lib/litegraph/src/node/NodeInputSlot' import { NodeOutputSlot } from '@/lib/litegraph/src/node/NodeOutputSlot' import { UNASSIGNED_NODE_ID, toNodeId } from '@/types/nodeId' import type { SerializedNodeId } from '@/types/nodeId' -import type { - GraphOrSubgraph, - Subgraph -} from '@/lib/litegraph/src/subgraph/Subgraph' + import type { ExportedSubgraphInstance, ISerialisedNode @@ -53,7 +62,7 @@ import { deriveWidgetVisibility } from '@/types/widgetVisibility' import { ExecutableNodeDTO } from './ExecutableNodeDTO' import type { ExecutableLGraphNode, ExecutionId } from './ExecutableNodeDTO' import { createPromotedWidgetStoreProjection } from './promotedWidgetStoreProjection' -import type { SubgraphInput } from './SubgraphInput' + import { createBitmapCache } from './svgBitmapCache' const workflowSvg = new Image() @@ -1052,3 +1061,9 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { return shapes.every((shape) => shape === shapes[0]) ? shapes[0] : undefined } } + +type RegisteredSubgraphNode = SubgraphNode + +declare module '../types/entityRegistry' { + interface SubgraphNode extends RegisteredSubgraphNode {} +} diff --git a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts index d0691b0ab87..460baa33826 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphOutput.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphOutput.ts @@ -1,20 +1,23 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { + LGraphNode, + SubgraphInput, + SubgraphOutputNode +} from '@/lib/litegraph/src/types/entityRegistry' + import { LLink, replaceLinkTopology } from '@/lib/litegraph/src/LLink' import { linkIdReservations, mintLinkId } from '../idAllocation' import { anchorRerouteChain } from '@/lib/litegraph/src/Reroute' -import type { RerouteId } from '@/lib/litegraph/src/Reroute' +import type { RerouteId } from '@/types/rerouteId' import type { Point, ReadOnlyRect } from '@/lib/litegraph/src/interfaces' import type { INodeInputSlot, INodeOutputSlot } from '@/lib/litegraph/src/types/slots' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' import { NodeSlotType } from '@/lib/litegraph/src/types/globalEnums' -import type { SubgraphInput } from './SubgraphInput' -import type { SubgraphOutputNode } from './SubgraphOutputNode' import { SubgraphSlot } from './SubgraphSlotBase' -import { isNodeSlot, isSubgraphInput } from './subgraphUtils' +import { isNodeSlot, isSubgraphInput } from './subgraphTypeGuards' /** * An output "slot" from a subgraph to a parent graph. @@ -156,3 +159,9 @@ export class SubgraphOutput extends SubgraphSlot { this.linkIds.length = 0 } } + +type RegisteredSubgraphOutput = SubgraphOutput + +declare module '../types/entityRegistry' { + interface SubgraphOutput extends RegisteredSubgraphOutput {} +} diff --git a/src/lib/litegraph/src/subgraph/SubgraphOutputNode.ts b/src/lib/litegraph/src/subgraph/SubgraphOutputNode.ts index 0ef1c4f7bd0..799ac23ea53 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphOutputNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphOutputNode.ts @@ -1,9 +1,16 @@ +import type { + LGraphNode, + LLink, + LinkConnector, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' import type { CanvasPointer } from '@/lib/litegraph/src/CanvasPointer' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' + import type { NodeId } from '@/types/nodeId' -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { RerouteId } from '@/lib/litegraph/src/Reroute' -import type { LinkConnector } from '@/lib/litegraph/src/canvas/LinkConnector' + +import type { RerouteId } from '@/types/rerouteId' + import { SUBGRAPH_OUTPUT_ID } from '@/lib/litegraph/src/constants' import type { DefaultConnectionColors, @@ -21,8 +28,8 @@ import { findFreeSlotOfType } from '@/lib/litegraph/src/utils/collections' import { EmptySubgraphOutput } from './EmptySubgraphOutput' import { SubgraphIONodeBase } from './SubgraphIONodeBase' -import type { SubgraphInput } from './SubgraphInput' -import type { SubgraphOutput } from './SubgraphOutput' + +import { provideSubgraphOutputGuard } from './subgraphTypeGuards' export class SubgraphOutputNode extends SubgraphIONodeBase @@ -161,3 +168,20 @@ export class SubgraphOutputNode this.drawSlots(ctx, colorContext, fromSlot, editorAlpha) } } + +function isSubgraphOutput(slot: unknown): slot is SubgraphOutput { + return ( + slot != null && + typeof slot === 'object' && + 'parent' in slot && + slot.parent instanceof SubgraphOutputNode + ) +} + +provideSubgraphOutputGuard(isSubgraphOutput) + +type RegisteredSubgraphOutputNode = SubgraphOutputNode + +declare module '../types/entityRegistry' { + interface SubgraphOutputNode extends RegisteredSubgraphOutputNode {} +} diff --git a/src/lib/litegraph/src/subgraph/SubgraphSlotBase.ts b/src/lib/litegraph/src/subgraph/SubgraphSlotBase.ts index d79aa964562..de7effe75bf 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphSlotBase.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphSlotBase.ts @@ -1,7 +1,15 @@ -import { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LLink, LinkId } from '@/lib/litegraph/src/LLink' -import type { RerouteId } from '@/lib/litegraph/src/Reroute' +import type { + LGraphNode, + LLink, + SubgraphInput, + SubgraphInputNode, + SubgraphOutput, + SubgraphOutputNode +} from '@/lib/litegraph/src/types/entityRegistry' +import { canvasClass } from '@/lib/litegraph/src/canvas/canvasClassAccess' + +import type { LinkId } from '@/types/linkId' +import type { RerouteId } from '@/types/rerouteId' import { SlotShape } from '@/lib/litegraph/src/draw' import { ConstrainedSize } from '@/lib/litegraph/src/infrastructure/ConstrainedSize' import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle' @@ -15,7 +23,7 @@ import type { INodeInputSlot, INodeOutputSlot } from '@/lib/litegraph/src/types/slots' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' import { SlotBase } from '@/lib/litegraph/src/node/SlotBase' import type { CanvasPointerEvent, @@ -27,11 +35,6 @@ import type { } from '@/lib/litegraph/src/types/serialisation' import type { UUID } from '@/utils/uuid' -import type { SubgraphInput } from './SubgraphInput' -import type { SubgraphInputNode } from './SubgraphInputNode' -import type { SubgraphOutput } from './SubgraphOutput' -import type { SubgraphOutputNode } from './SubgraphOutputNode' - interface SubgraphSlotDrawOptions { ctx: CanvasRenderingContext2D colorContext: DefaultConnectionColors @@ -136,7 +139,7 @@ export abstract class SubgraphSlot } measure(): Readonly { - const width = LGraphCanvas._measureText?.(this.displayName) ?? 0 + const width = canvasClass._measureText?.(this.displayName) ?? 0 const { defaultHeight } = SubgraphSlot this.measurement.setValues(width + defaultHeight, defaultHeight) diff --git a/src/lib/litegraph/src/subgraph/promotedWidgetStoreProjection.ts b/src/lib/litegraph/src/subgraph/promotedWidgetStoreProjection.ts index bda29121361..058a825a0b5 100644 --- a/src/lib/litegraph/src/subgraph/promotedWidgetStoreProjection.ts +++ b/src/lib/litegraph/src/subgraph/promotedWidgetStoreProjection.ts @@ -1,4 +1,4 @@ -import type { INodeInputSlot } from '@/lib/litegraph/src/litegraph' +import type { INodeInputSlot } from '@/lib/litegraph/src/types/slots' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { WidgetId } from '@/types/widgetId' diff --git a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts index 7479eaadbd2..5c2eccebe67 100644 --- a/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts +++ b/src/lib/litegraph/src/subgraph/subgraphDeduplication.ts @@ -19,7 +19,7 @@ import { toNodeId } from '@/types/nodeId' import type { NodeId, SerializedNodeId } from '@/types/nodeId' import { toLinkId } from '@/types/linkId' import { toRerouteId } from '@/types/rerouteId' -import type { NodeProperty } from '@/types/nodeState' +import type { NodeProperty } from '@/types/nodeProperty' import { createUuidv4 } from '@/utils/uuid' import type { ExportedSubgraph, diff --git a/src/lib/litegraph/src/subgraph/subgraphTypeGuards.ts b/src/lib/litegraph/src/subgraph/subgraphTypeGuards.ts new file mode 100644 index 00000000000..79aa2c0e281 --- /dev/null +++ b/src/lib/litegraph/src/subgraph/subgraphTypeGuards.ts @@ -0,0 +1,35 @@ +import type { + Subgraph, + SubgraphInput, + SubgraphOutput +} from '@/lib/litegraph/src/types/entityRegistry' + +import type { INodeInputSlot, INodeOutputSlot } from '../types/slots' + +export let isSubgraph: (value: unknown) => value is Subgraph +export let isSubgraphInput: (slot: unknown) => slot is SubgraphInput +export let isSubgraphOutput: (slot: unknown) => slot is SubgraphOutput + +export function provideSubgraphGuard(guard: typeof isSubgraph): void { + isSubgraph = guard +} + +export function provideSubgraphInputGuard(guard: typeof isSubgraphInput): void { + isSubgraphInput = guard +} + +export function provideSubgraphOutputGuard( + guard: typeof isSubgraphOutput +): void { + isSubgraphOutput = guard +} + +export function isNodeSlot( + slot: unknown +): slot is INodeInputSlot | INodeOutputSlot { + return ( + slot != null && + typeof slot === 'object' && + ('link' in slot || 'links' in slot) + ) +} diff --git a/src/lib/litegraph/src/subgraph/subgraphUtils.ts b/src/lib/litegraph/src/subgraph/subgraphUtils.ts index e405ca6ebf6..7087e222717 100644 --- a/src/lib/litegraph/src/subgraph/subgraphUtils.ts +++ b/src/lib/litegraph/src/subgraph/subgraphUtils.ts @@ -1,9 +1,18 @@ +import type { + LGraph, + SubgraphId, + ResolvedConnection, + GraphOrSubgraph, + Subgraph, + SubgraphInput, + SubgraphNode +} from '@/lib/litegraph/src/types/entityRegistry' import { isEqual } from 'es-toolkit' -import type { LGraph, SubgraphId } from '@/lib/litegraph/src/LGraph' + import { LGraphGroup } from '@/lib/litegraph/src/LGraphGroup' import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import { LLink, slotFloatingLinks } from '@/lib/litegraph/src/LLink' -import type { ResolvedConnection } from '@/lib/litegraph/src/LLink' + import { captureInputLayout, inputLinkId, @@ -11,18 +20,16 @@ import { replaceNodeInputs } from '@/lib/litegraph/src/node/slotLinks' import { Reroute } from '@/lib/litegraph/src/Reroute' -import type { RerouteId } from '@/lib/litegraph/src/Reroute' +import type { RerouteId } from '@/types/rerouteId' import { toRerouteId } from '@/types/rerouteId' import { SUBGRAPH_INPUT_ID, SUBGRAPH_OUTPUT_ID } from '@/lib/litegraph/src/constants' import type { Positionable } from '@/lib/litegraph/src/interfaces' -import type { - INodeInputSlot, - INodeOutputSlot -} from '@/lib/litegraph/src/types/slots' -import { LiteGraph, createUuidv4 } from '@/lib/litegraph/src/litegraph' +import type { INodeOutputSlot } from '@/lib/litegraph/src/types/slots' +import { LiteGraph } from '@/lib/litegraph/src/litegraphInstance' +import { createUuidv4 } from '@/utils/uuid' import { nextUniqueName } from '@/lib/litegraph/src/strings' import { UNASSIGNED_NODE_ID } from '@/types/nodeId' import type { @@ -31,11 +38,8 @@ import type { SubgraphIO } from '@/lib/litegraph/src/types/serialisation' -import type { GraphOrSubgraph, Subgraph } from './Subgraph' -import type { SubgraphInput } from './SubgraphInput' import { SubgraphInputNode } from './SubgraphInputNode' -import type { SubgraphNode } from './SubgraphNode' -import type { SubgraphOutput } from './SubgraphOutput' + import { SubgraphOutputNode } from './SubgraphOutputNode' interface FilteredItems { @@ -652,45 +656,4 @@ export function reorderSubgraphInputs( } } -/** - * Type guard to check if a slot is a SubgraphInput. - * @param slot The slot to check - * @returns true if the slot is a SubgraphInput - */ -export function isSubgraphInput(slot: unknown): slot is SubgraphInput { - return ( - slot != null && - typeof slot === 'object' && - 'parent' in slot && - slot.parent instanceof SubgraphInputNode - ) -} - -/** - * Type guard to check if a slot is a SubgraphOutput. - * @param slot The slot to check - * @returns true if the slot is a SubgraphOutput - */ -export function isSubgraphOutput(slot: unknown): slot is SubgraphOutput { - return ( - slot != null && - typeof slot === 'object' && - 'parent' in slot && - slot.parent instanceof SubgraphOutputNode - ) -} - -/** - * Type guard to check if a slot is a regular node slot (INodeInputSlot or INodeOutputSlot). - * @param slot The slot to check - * @returns true if the slot is a regular node slot - */ -export function isNodeSlot( - slot: unknown -): slot is INodeInputSlot | INodeOutputSlot { - return ( - slot != null && - typeof slot === 'object' && - ('link' in slot || 'links' in slot) - ) -} +export { isSubgraphInput, isSubgraphOutput } from './subgraphTypeGuards' diff --git a/src/lib/litegraph/src/subgraph/unpackSubgraph.ts b/src/lib/litegraph/src/subgraph/unpackSubgraph.ts index 535e41469e5..addb45e1e2b 100644 --- a/src/lib/litegraph/src/subgraph/unpackSubgraph.ts +++ b/src/lib/litegraph/src/subgraph/unpackSubgraph.ts @@ -1,3 +1,4 @@ +import type { LGraph } from '@/lib/litegraph/src/types/entityRegistry' import type { UUID } from '@/utils/uuid' import { createUuidv4 } from '@/utils/uuid' import type { NodeId } from '@/types/nodeId' @@ -5,9 +6,9 @@ import { toNodeId } from '@/types/nodeId' import type { Point } from '../interfaces' import type { INodeInputSlot } from '../types/slots' -import type { LGraph } from '../LGraph' + import { LGraphNode } from '../LGraphNode' -import { LiteGraph } from '../litegraph' +import { LiteGraph } from '../litegraphInstance' import type { ISerialisedNode } from '../types/serialisation' export type UnpackedTargetInput = diff --git a/src/lib/litegraph/src/types/contextMenu.ts b/src/lib/litegraph/src/types/contextMenu.ts index b48539dfc21..bd3302d2797 100644 --- a/src/lib/litegraph/src/types/contextMenu.ts +++ b/src/lib/litegraph/src/types/contextMenu.ts @@ -1,5 +1,8 @@ -import type { ContextMenu } from '../ContextMenu' -import type { LGraphNode } from '../LGraphNode' +import type { + ContextMenu, + LGraphNode +} from '@/lib/litegraph/src/types/entityRegistry' + import type { IFoundSlot } from './slots' interface IContextMenuBase { @@ -58,7 +61,7 @@ export interface IContextMenuValue< interface IContextMenuSubmenu< TValue = unknown > extends IContextMenuOptions { - options: ConstructorParameters>[0] + options: ConstructorParameters['constructor']>[0] } export interface ContextMenuDivElement< diff --git a/src/lib/litegraph/src/types/entityRegistry.test.ts b/src/lib/litegraph/src/types/entityRegistry.test.ts new file mode 100644 index 00000000000..603fb886256 --- /dev/null +++ b/src/lib/litegraph/src/types/entityRegistry.test.ts @@ -0,0 +1,35 @@ +import { expectTypeOf, it } from 'vitest' + +import type { NodeId } from '@/types/nodeId' + +import type { BaseLGraph } from '../LGraph' +import type { + ContextMenu, + LGraph, + LGraphNode, + LGraphNodeConstructor, + Subgraph +} from '../litegraph' +import type { GraphOrSubgraph } from '../subgraph/Subgraph' +import type * as Entities from './entityRegistry' +import type { LGraphNodeConstructor as NodeConstructor } from './nodeConstructor' +import type { NodeBindable } from './widgets' + +it('preserves concrete graph, node, constructor, and generic menu contracts', () => { + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf() + expectTypeOf< + Entities.LitegraphEntityRegistry['GraphOrSubgraph'] + >().toEqualTypeOf() + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf<{ + setNodeId(nodeId: NodeId): void + }>() + expectTypeOf().toEqualTypeOf() + expectTypeOf().toEqualTypeOf() + expectTypeOf>().toEqualTypeOf< + ContextMenu + >() + expectTypeOf().toEqualTypeOf() +}) diff --git a/src/lib/litegraph/src/types/entityRegistry.ts b/src/lib/litegraph/src/types/entityRegistry.ts new file mode 100644 index 00000000000..330f20b1ae0 --- /dev/null +++ b/src/lib/litegraph/src/types/entityRegistry.ts @@ -0,0 +1,34 @@ +export interface LitegraphEntityRegistry {} +export interface ContextMenu<_TValue = unknown> {} +export interface LGraphNode {} +export interface LGraph {} +export interface Subgraph {} +export interface LGraphCanvas {} +export interface SubgraphNode {} +export interface SubgraphInput {} +export interface SubgraphOutput {} +export interface SubgraphInputNode {} +export interface SubgraphOutputNode {} +export interface LLink {} +export interface Reroute {} +export interface LGraphGroup {} +export interface LiteGraphGlobal {} +export interface LinkConnector {} +export interface FloatingRenderLink {} +export interface MovingInputLink {} +export interface MovingOutputLink {} +export interface ToInputFromIoNodeLink {} +export interface ToInputRenderLink {} +export type GraphOrSubgraph = LitegraphEntityRegistry['GraphOrSubgraph'] +export interface LGraphConfig {} +export interface LGraphExtra {} +export type SubgraphId = LitegraphEntityRegistry['SubgraphId'] +export interface BaseLGraph {} +export interface IGraphGroupFlags {} +export type SerialisedLLinkArray = + LitegraphEntityRegistry['SerialisedLLinkArray'] +export type ResolvedConnection = LitegraphEntityRegistry['ResolvedConnection'] +export interface SlotTypeDefaultNodeOpts {} +export interface LinkReleaseContextExtended {} +export interface RenderLink {} +export interface SubgraphIONode {} diff --git a/src/lib/litegraph/src/types/events.ts b/src/lib/litegraph/src/types/events.ts index d8ca2f2f1aa..f6ff160abd0 100644 --- a/src/lib/litegraph/src/types/events.ts +++ b/src/lib/litegraph/src/types/events.ts @@ -1,11 +1,14 @@ +import type { + LGraphGroup, + LGraphNode, + LinkReleaseContextExtended +} from '@/lib/litegraph/src/types/entityRegistry' /** * Event interfaces for event extension */ -import type { LGraphGroup } from '../LGraphGroup' + import type { Rectangle } from '../infrastructure/Rectangle' import type { HasBoundingRect, Point } from '../interfaces' -import type { LGraphNode } from '../LGraphNode' -import type { LinkReleaseContextExtended } from '../litegraph' /** For Canvas*Event - adds graph space co-ordinates (property names are shipped) */ interface ICanvasPosition { diff --git a/src/lib/litegraph/src/types/linkNetwork.ts b/src/lib/litegraph/src/types/linkNetwork.ts index 4e0636f06e8..087270da52c 100644 --- a/src/lib/litegraph/src/types/linkNetwork.ts +++ b/src/lib/litegraph/src/types/linkNetwork.ts @@ -1,14 +1,15 @@ +import type { + LGraphNode, + LLink, + Reroute, + SubgraphInputNode, + SubgraphOutputNode +} from '@/lib/litegraph/src/types/entityRegistry' import type { LinkId } from '@/types/linkId' import type { NodeId } from '@/types/nodeId' import type { RerouteId } from '@/types/rerouteId' import type { UUID } from '@/utils/uuid' -import type { LGraphNode } from '../LGraphNode' -import type { LLink } from '../LLink' -import type { Reroute } from '../Reroute' -import type { SubgraphInputNode } from '../subgraph/SubgraphInputNode' -import type { SubgraphOutputNode } from '../subgraph/SubgraphOutputNode' - export interface ReadonlyLinkNetwork { readonly rootGraph: { readonly id: UUID } readonly links: ReadonlyMap diff --git a/src/lib/litegraph/src/types/nodeConstructor.ts b/src/lib/litegraph/src/types/nodeConstructor.ts new file mode 100644 index 00000000000..72321a30a8f --- /dev/null +++ b/src/lib/litegraph/src/types/nodeConstructor.ts @@ -0,0 +1,26 @@ +import type { LGraphNode } from './entityRegistry' +import type { Size } from '../interfaces' +import type { RenderShape, TitleMode } from './globalEnums' + +export interface LGraphNodeConstructor { + new (title: string, type?: string): T + + title?: string + desc?: string + priority?: number + type?: string // TODO: to be, or not to be--that is the question + size?: Size + min_height?: number + slot_start_y?: number + widgets_info?: Record + collapsable?: boolean + color?: string + bgcolor?: string + shape?: RenderShape + title_mode?: TitleMode + title_color?: string + title_text_color?: string + keepAllLinksOnBypass: boolean + resizeHandleSize?: number + resizeEdgeSize?: number +} diff --git a/src/lib/litegraph/src/types/panel.ts b/src/lib/litegraph/src/types/panel.ts index 10ae3876333..73cde7ce9ce 100644 --- a/src/lib/litegraph/src/types/panel.ts +++ b/src/lib/litegraph/src/types/panel.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '../LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { IContextMenuValue } from './contextMenu' import type { TWidgetValue } from './widgets' diff --git a/src/lib/litegraph/src/types/serialisation.ts b/src/lib/litegraph/src/types/serialisation.ts index 8c4a4cab7bf..1b5312cf0eb 100644 --- a/src/lib/litegraph/src/types/serialisation.ts +++ b/src/lib/litegraph/src/types/serialisation.ts @@ -1,12 +1,17 @@ +import type { + LGraphConfig, + LGraphExtra, + SubgraphId, + IGraphGroupFlags, + SerialisedLLinkArray +} from '@/lib/litegraph/src/types/entityRegistry' import type { UUID } from '@/utils/uuid' import type { LinkPresentation } from '@/types/linkPresentation' -import type { LGraphConfig, LGraphExtra, SubgraphId } from '../LGraph' -import type { IGraphGroupFlags } from '../LGraphGroup' -import type { NodeProperty } from '@/types/nodeState' +import type { NodeProperty } from '@/types/nodeProperty' import type { SerializedNodeId } from '@/types/nodeId' -import type { SerialisedLLinkArray } from '../LLink' -import type { FloatingRerouteSlot } from '../Reroute' + +import type { FloatingRerouteSlot } from '@/types/rerouteChain' import type { Dictionary, INodeFlags, @@ -16,7 +21,7 @@ import type { Size } from '../interfaces' import type { INodeInputSlot, INodeOutputSlot } from './slots' -import type { LiteGraph } from '../litegraph' +import type { LiteGraphGlobal } from './entityRegistry' import type { RenderShape } from './globalEnums' import type { TWidgetValue } from './widgets' @@ -142,7 +147,7 @@ export interface ISerialisedGraph extends BaseExportedGraph { links: SerialisedLLinkArray[] floatingLinks?: SerialisableLLink[] groups: ISerialisedGroup[] - version: typeof LiteGraph.VERSION + version: LiteGraphGlobal['VERSION'] extra?: LGraphExtra } diff --git a/src/lib/litegraph/src/types/slots.ts b/src/lib/litegraph/src/types/slots.ts index b890b6a0092..3f988f6cbcc 100644 --- a/src/lib/litegraph/src/types/slots.ts +++ b/src/lib/litegraph/src/types/slots.ts @@ -1,12 +1,15 @@ +import type { + LGraphNode, + LLink, + SubgraphInput +} from '@/lib/litegraph/src/types/entityRegistry' import type { LinkId } from '@/types/linkId' import type { RerouteId } from '@/types/rerouteId' import type { SlotIndex } from '@/types/slotId' import type { WidgetId } from '@/types/widgetId' -import type { LGraphNode } from '../LGraphNode' -import type { LLink } from '../LLink' import type { IWidgetLocator, ISlotType, INodeSlot, Point } from '../interfaces' -import type { SubgraphInput } from '../subgraph/SubgraphInput' + import type { LinkDirection } from './globalEnums' import type { IBaseWidget } from './widgets' diff --git a/src/lib/litegraph/src/types/widgets.ts b/src/lib/litegraph/src/types/widgets.ts index e33ea150b5b..fb29414e849 100644 --- a/src/lib/litegraph/src/types/widgets.ts +++ b/src/lib/litegraph/src/types/widgets.ts @@ -1,10 +1,13 @@ +import type { + LGraphCanvas, + LGraphNode +} from '@/lib/litegraph/src/types/entityRegistry' import type { Bounds } from '@/renderer/core/layout/types' import type { CompositorWidgetValue } from '@/renderer/extensions/compositor/components/types' import type { CurveData } from '@/components/curve/types' import type { BoundingBox } from '@/types/boundingBoxes' import type { LightInfoEntry } from '@/types/lightInfo' -import type { NodeId } from '@/types/nodeId' -import type { WidgetValue } from '@/types/simplifiedWidget' +import type { WidgetValue } from '@/types/widgetValue' import type { WidgetId } from '@/types/widgetId' import type { WidgetSurfaces, @@ -19,12 +22,10 @@ import type { RequiredProps, Size } from '../interfaces' -import type { CanvasPointer, LGraphCanvas, LGraphNode } from '../litegraph' +import type { CanvasPointer } from '@/lib/litegraph/src/CanvasPointer' import type { CanvasPointerEvent } from './events' -export interface NodeBindable { - setNodeId(nodeId: NodeId): void -} +export type { NodeBindable } from '../utils/type' export interface IWidgetOptions { on?: string diff --git a/src/lib/litegraph/src/utils/arrange.ts b/src/lib/litegraph/src/utils/arrange.ts index 712d7d7bd0a..01accef4ab7 100644 --- a/src/lib/litegraph/src/utils/arrange.ts +++ b/src/lib/litegraph/src/utils/arrange.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '../LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { Direction } from '../interfaces' export interface NewNodePosition { diff --git a/src/lib/litegraph/src/utils/collections.ts b/src/lib/litegraph/src/utils/collections.ts index 4f443ccb2aa..fa2e385a301 100644 --- a/src/lib/litegraph/src/utils/collections.ts +++ b/src/lib/litegraph/src/utils/collections.ts @@ -1,8 +1,15 @@ -import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import { parseSlotTypes } from '@/lib/litegraph/src/strings' import type { ISlotType, Positionable } from '../interfaces' +let isGraphNode: (item: Positionable) => item is LGraphNode + +export function provideGraphNodeGuard(guard: typeof isGraphNode): void { + isGraphNode = guard +} + /** * Creates a flat set of all positionable items by recursively iterating through all child items. * @@ -57,7 +64,7 @@ export function findFirstNode( items: Iterable ): LGraphNode | undefined { for (const item of items) { - if (item instanceof LGraphNode) return item + if (isGraphNode(item)) return item } } diff --git a/src/lib/litegraph/src/utils/feedback.ts b/src/lib/litegraph/src/utils/feedback.ts index 8215735a439..88e798a903c 100644 --- a/src/lib/litegraph/src/utils/feedback.ts +++ b/src/lib/litegraph/src/utils/feedback.ts @@ -1,4 +1,4 @@ -import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import { litegraph } from '@/lib/litegraph/src/litegraphInstance' /** Guard against unbound allocation. */ const UNIQUE_MESSAGE_LIMIT = 10_000 @@ -11,7 +11,7 @@ const sentWarnings: Set = new Set() * @param source A reference object to include alongside the message, e.g. `this`. */ export function warnDeprecated(message: string, source?: object): void { - if (!LiteGraph.alwaysRepeatWarnings) { + if (!litegraph().alwaysRepeatWarnings) { // Do not repeat if (sentWarnings.has(message)) return @@ -21,7 +21,7 @@ export function warnDeprecated(message: string, source?: object): void { sentWarnings.add(message) } - for (const callback of LiteGraph.onDeprecationWarning) { + for (const callback of litegraph().onDeprecationWarning) { callback(message, source) } } diff --git a/src/lib/litegraph/src/utils/linkColors.ts b/src/lib/litegraph/src/utils/linkColors.ts index e6e966133d6..b2f745d5753 100644 --- a/src/lib/litegraph/src/utils/linkColors.ts +++ b/src/lib/litegraph/src/utils/linkColors.ts @@ -1,5 +1,5 @@ import type { CanvasColour, ISlotType } from '../interfaces' -import { LiteGraph } from '../litegraph' +import { litegraph } from '../litegraphInstance' /** * Resolve the colour used while rendering or previewing a connection of a given slot type. @@ -7,7 +7,7 @@ import { LiteGraph } from '../litegraph' export function resolveConnectingLinkColor( type: ISlotType | undefined ): CanvasColour { - return type === LiteGraph.EVENT - ? LiteGraph.EVENT_LINK_COLOR - : LiteGraph.CONNECTING_LINK_COLOR + return type === litegraph().EVENT + ? litegraph().EVENT_LINK_COLOR + : litegraph().CONNECTING_LINK_COLOR } diff --git a/src/lib/litegraph/src/utils/namedValuesShadowDiffTelemetry.ts b/src/lib/litegraph/src/utils/namedValuesShadowDiffTelemetry.ts index d7a3fc97f6c..8b3fa6ace2f 100644 --- a/src/lib/litegraph/src/utils/namedValuesShadowDiffTelemetry.ts +++ b/src/lib/litegraph/src/utils/namedValuesShadowDiffTelemetry.ts @@ -1,7 +1,7 @@ +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' import { getCnrIdFromNode } from '@/platform/nodeReplacement/cnrIdUtil' import { useTelemetry } from '@/platform/telemetry' -import type { LGraphNode } from '../LGraphNode' import type { NamedValuesShadowDiffResult } from './namedValuesShadowDiff' const SHADOW_DIFF_SUMMARY_SAMPLE_RATE = 0.05 diff --git a/src/lib/litegraph/src/utils/type.ts b/src/lib/litegraph/src/utils/type.ts index ccf9801bb63..604617a2240 100644 --- a/src/lib/litegraph/src/utils/type.ts +++ b/src/lib/litegraph/src/utils/type.ts @@ -1,7 +1,11 @@ import { without } from 'es-toolkit' import type { IColorable, ISlotType } from '@/lib/litegraph/src/interfaces' -import type { NodeBindable } from '@/lib/litegraph/src/types/widgets' +import type { NodeId } from '@/types/nodeId' + +export interface NodeBindable { + setNodeId(nodeId: NodeId): void +} /** * Converts a plain object to a class instance if it is not already an instance of the class. diff --git a/src/lib/litegraph/src/utils/widget.ts b/src/lib/litegraph/src/utils/widget.ts index ba6e8aaede7..a64c3a6c3f8 100644 --- a/src/lib/litegraph/src/utils/widget.ts +++ b/src/lib/litegraph/src/utils/widget.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { IBaseWidget, IWidgetOptions diff --git a/src/lib/litegraph/src/widgets/AssetWidget.ts b/src/lib/litegraph/src/widgets/AssetWidget.ts index b01520b3a52..533eda92828 100644 --- a/src/lib/litegraph/src/widgets/AssetWidget.ts +++ b/src/lib/litegraph/src/widgets/AssetWidget.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { IAssetWidget } from '@/lib/litegraph/src/types/widgets' import { BaseWidget, extensionValue } from './BaseWidget' diff --git a/src/lib/litegraph/src/widgets/BaseWidget.ts b/src/lib/litegraph/src/widgets/BaseWidget.ts index 5ed3e6430f8..ae9fc0d1801 100644 --- a/src/lib/litegraph/src/widgets/BaseWidget.ts +++ b/src/lib/litegraph/src/widgets/BaseWidget.ts @@ -1,21 +1,18 @@ +import type { + LGraphCanvas, + LGraphNode +} from '@/lib/litegraph/src/types/entityRegistry' import { t } from '@/i18n' import { drawTextInArea } from '@/lib/litegraph/src/draw' import { cachedMeasureText } from '@/lib/litegraph/src/utils/textMeasureCache' import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle' -import type { Point } from '@/lib/litegraph/src/interfaces' +import type { Point, Size } from '@/lib/litegraph/src/interfaces' import type { NodeId } from '@/types/nodeId' -import type { - CanvasPointer, - LGraphCanvas, - LGraphNode, - Size -} from '@/lib/litegraph/src/litegraph' +import type { CanvasPointer } from '@/lib/litegraph/src/CanvasPointer' import { litegraph } from '@/lib/litegraph/src/litegraphInstance' import type { CanvasPointerEvent } from '@/lib/litegraph/src/types/events' -import type { - IBaseWidget, - NodeBindable -} from '@/lib/litegraph/src/types/widgets' +import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' +import type { NodeBindable } from '@/lib/litegraph/src/utils/type' import { deriveWidgetRenderState } from '@/lib/litegraph/src/utils/widget' import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { WidgetId } from '@/types/widgetId' diff --git a/src/lib/litegraph/src/widgets/ButtonWidget.ts b/src/lib/litegraph/src/widgets/ButtonWidget.ts index 8da6d90c23f..1117f14b5a0 100644 --- a/src/lib/litegraph/src/widgets/ButtonWidget.ts +++ b/src/lib/litegraph/src/widgets/ButtonWidget.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { IButtonWidget } from '@/lib/litegraph/src/types/widgets' import { BaseWidget, extensionValue } from './BaseWidget' diff --git a/src/lib/litegraph/src/widgets/ComboWidget.ts b/src/lib/litegraph/src/widgets/ComboWidget.ts index 8d16ea2ba73..a3999e8e3da 100644 --- a/src/lib/litegraph/src/widgets/ComboWidget.ts +++ b/src/lib/litegraph/src/widgets/ComboWidget.ts @@ -1,8 +1,9 @@ +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' import { clamp } from 'es-toolkit/compat' import type { IContextMenuValue } from '@/lib/litegraph/src/types/contextMenu' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' + +import { litegraph } from '@/lib/litegraph/src/litegraphInstance' import type { ComboWidgetValues, IComboWidget, @@ -169,7 +170,7 @@ export class ComboWidget } } } - const menu = new LiteGraph.ContextMenu([], menuOptions) + const menu = new (litegraph().ContextMenu)([], menuOptions) const getOptionLabel = this.options.getOptionLabel for (const value of values_list) { @@ -187,7 +188,7 @@ export class ComboWidget // Show dropdown menu when user clicks on widget label const text_values = values != values_list ? Object.values(values) : values - new LiteGraph.ContextMenu(text_values, { + new (litegraph().ContextMenu)(text_values, { scale: Math.max(1, canvas.ds.scale), event: e, className: 'dark', diff --git a/src/lib/litegraph/src/widgets/LegacyWidget.ts b/src/lib/litegraph/src/widgets/LegacyWidget.ts index 1c48bb3255d..f759fa1dd7c 100644 --- a/src/lib/litegraph/src/widgets/LegacyWidget.ts +++ b/src/lib/litegraph/src/widgets/LegacyWidget.ts @@ -1,5 +1,6 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + +import { litegraph } from '@/lib/litegraph/src/litegraphInstance' import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets' import { BaseWidget } from './BaseWidget' @@ -28,7 +29,7 @@ export class LegacyWidget ctx: CanvasRenderingContext2D, options: DrawWidgetOptions ) { - const H = LiteGraph.NODE_WIDGET_HEIGHT + const H = litegraph().NODE_WIDGET_HEIGHT this.draw?.(ctx, this.node, options.width, this.y, H, !!options.showText) } diff --git a/src/lib/litegraph/src/widgets/TextWidget.ts b/src/lib/litegraph/src/widgets/TextWidget.ts index 0e0c37d4c33..5bdb056b8ef 100644 --- a/src/lib/litegraph/src/widgets/TextWidget.ts +++ b/src/lib/litegraph/src/widgets/TextWidget.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { IStringWidget } from '@/lib/litegraph/src/types/widgets' import { BaseWidget, extensionValue } from './BaseWidget' diff --git a/src/lib/litegraph/src/widgets/widgetMap.ts b/src/lib/litegraph/src/widgets/widgetMap.ts index 736cbfefcf6..bcf26ce7b4e 100644 --- a/src/lib/litegraph/src/widgets/widgetMap.ts +++ b/src/lib/litegraph/src/widgets/widgetMap.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import type { IAssetWidget, IBaseWidget, diff --git a/src/platform/nodeReplacement/cnrIdUtil.ts b/src/platform/nodeReplacement/cnrIdUtil.ts index 165944ccd31..c1730d0f6ab 100644 --- a/src/platform/nodeReplacement/cnrIdUtil.ts +++ b/src/platform/nodeReplacement/cnrIdUtil.ts @@ -1,4 +1,4 @@ -import type { LGraphNode } from '@/lib/litegraph/src/litegraph' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' /** * Extracts the custom node registry ID (cnr_id or aux_id) from a raw diff --git a/src/renderer/core/canvas/links/linkDropOrchestrator.test.ts b/src/renderer/core/canvas/links/linkDropOrchestrator.test.ts index 969ab09c40b..fa01fdd924e 100644 --- a/src/renderer/core/canvas/links/linkDropOrchestrator.test.ts +++ b/src/renderer/core/canvas/links/linkDropOrchestrator.test.ts @@ -1,8 +1,7 @@ import { fromPartial } from '@total-typescript/shoehorn' import { beforeEach, describe, expect, it, vi } from 'vitest' -import { LGraph } from '@/lib/litegraph/src/LGraph' -import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import type { LinkConnectorAdapter } from '@/renderer/core/canvas/links/linkConnectorAdapter' import { useSlotLinkDragUIState } from '@/renderer/core/canvas/links/slotLinkDragUIState' import { getSlotKey } from '@/renderer/core/layout/slots/slotIdentifier' diff --git a/src/renderer/core/canvas/litegraph/arrangeForLegacyRender.ts b/src/renderer/core/canvas/litegraph/arrangeForLegacyRender.ts index 519e0c21544..2318f3276dc 100644 --- a/src/renderer/core/canvas/litegraph/arrangeForLegacyRender.ts +++ b/src/renderer/core/canvas/litegraph/arrangeForLegacyRender.ts @@ -1,6 +1,9 @@ +import type { + LGraph, + LGraphNode +} from '@/lib/litegraph/src/types/entityRegistry' import log from 'loglevel' -import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import { compareNodeIds } from '@/types/nodeId' diff --git a/src/renderer/core/canvas/litegraph/litegraphLinkAdapter.ts b/src/renderer/core/canvas/litegraph/litegraphLinkAdapter.ts index 8f39ab67f76..6309e39a0e7 100644 --- a/src/renderer/core/canvas/litegraph/litegraphLinkAdapter.ts +++ b/src/renderer/core/canvas/litegraph/litegraphLinkAdapter.ts @@ -1,3 +1,4 @@ +import type { LLink, Reroute } from '@/lib/litegraph/src/types/entityRegistry' /** * Litegraph Link Adapter * @@ -6,10 +7,9 @@ * rendering data that can be consumed by the PathRenderer. * Maintains backward compatibility with existing litegraph integration. */ -import type { LLink } from '@/lib/litegraph/src/LLink' -import type { Reroute } from '@/lib/litegraph/src/Reroute' + import type { CanvasColour, Point } from '@/lib/litegraph/src/interfaces' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import { litegraph } from '@/lib/litegraph/src/litegraphInstance' import { LinkDirection, LinkMarkerShape, @@ -118,7 +118,7 @@ export class LitegraphLinkAdapter { disabled: context.disabledPattern }, animation: { - time: LiteGraph.getTime() * 0.001 + time: litegraph().getTime() * 0.001 }, scale: context.scale, highlightedIds: new Set(Array.from(context.highlightedLinks).map(String)) diff --git a/src/renderer/core/canvas/litegraph/selectionAdapter.ts b/src/renderer/core/canvas/litegraph/selectionAdapter.ts index 68bbe231244..dc0a207e1cd 100644 --- a/src/renderer/core/canvas/litegraph/selectionAdapter.ts +++ b/src/renderer/core/canvas/litegraph/selectionAdapter.ts @@ -1,4 +1,10 @@ import type { ClickSelectionIntent } from '@/core/selection/selectionIntent' +import type { + LGraph, + LGraphCanvas, + SubgraphInputNode, + SubgraphOutputNode +} from '@/lib/litegraph/src/types/entityRegistry' import type { SelectableKey, SelectionCommand @@ -7,18 +13,14 @@ import { parseSelectableKey, toSelectableKey } from '@/core/selection/selectionState' -import type { LGraph } from '@/lib/litegraph/src/LGraph' -import type { LGraphCanvas } from '@/lib/litegraph/src/LGraphCanvas' + import type { Positionable } from '@/lib/litegraph/src/interfaces' -import { - LGraphGroup, - LGraphNode, - Reroute, - Subgraph -} from '@/lib/litegraph/src/litegraph' +import { LGraphGroup } from '@/lib/litegraph/src/LGraphGroup' +import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import { Reroute } from '@/lib/litegraph/src/Reroute' import { SubgraphIONodeBase } from '@/lib/litegraph/src/subgraph/SubgraphIONodeBase' -import type { SubgraphInputNode } from '@/lib/litegraph/src/subgraph/SubgraphInputNode' -import type { SubgraphOutputNode } from '@/lib/litegraph/src/subgraph/SubgraphOutputNode' +import { isSubgraph } from '@/lib/litegraph/src/subgraph/subgraphTypeGuards' + import { useSelectionStore } from '@/core/selection/selectionStore' import { graphScopeOf } from '@/types/graphScopeId' import { toNodeId } from '@/types/nodeId' @@ -53,7 +55,7 @@ export function resolveSelectable( case 'reroute': return graph.getReroute(toRerouteId(Number(id))) case 'io': - if (!(graph instanceof Subgraph)) return undefined + if (!isSubgraph(graph)) return undefined return [graph.inputNode, graph.outputNode].find( (ioNode) => String(ioNode.id) === id ) diff --git a/src/renderer/core/canvas/litegraph/slotCalculations.test.ts b/src/renderer/core/canvas/litegraph/slotCalculations.test.ts index 969aedf10d3..55c4d14202d 100644 --- a/src/renderer/core/canvas/litegraph/slotCalculations.test.ts +++ b/src/renderer/core/canvas/litegraph/slotCalculations.test.ts @@ -1,17 +1,7 @@ import { fromPartial } from '@total-typescript/shoehorn' -import { beforeEach, describe, expect, it, vi } from 'vitest' - -const mockLiteGraph = vi.hoisted(() => ({ - NODE_TITLE_HEIGHT: 30, - NODE_SLOT_HEIGHT: 20, - NODE_COLLAPSED_WIDTH: 80, - vueNodesMode: false -})) - -vi.mock(import('@/lib/litegraph/src/litegraph'), () => ({ - LiteGraph: mockLiteGraph -})) +import { beforeEach, describe, expect, it } from 'vitest' +import { LiteGraph } from '@/lib/litegraph/src/litegraph' import type { LGraph } from '@/lib/litegraph/src/LGraph' import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' import type { @@ -31,8 +21,15 @@ import { } from './slotCalculations' import type { SlotPositionContext } from './slotCalculations' -const SLOT_HEIGHT = mockLiteGraph.NODE_SLOT_HEIGHT -const TITLE_HEIGHT = mockLiteGraph.NODE_TITLE_HEIGHT +const SLOT_HEIGHT = 20 +const TITLE_HEIGHT = 30 + +beforeEach(() => { + const previousMode = LiteGraph.vueNodesMode + return () => { + LiteGraph.vueNodesMode = previousMode + } +}) function makeContext( overrides: Partial = {} @@ -79,7 +76,7 @@ function makeNode( flags: { collapsed: overrides.collapsed ?? false }, title_mode: overrides.titleMode ?? TitleMode.NORMAL_TITLE, type: overrides.type ?? 'TestNode', - _collapsed_width: mockLiteGraph.NODE_COLLAPSED_WIDTH, + _collapsed_width: 80, constructor: { slot_start_y: undefined }, inputs: overrides.inputs ?? [], outputs: overrides.outputs ?? [], @@ -180,7 +177,7 @@ describe('calculateInputSlotPosFromSlot', () => { describe('getSlotPosition — legacy fallback (vueNodesMode disabled)', () => { beforeEach(() => { - mockLiteGraph.vueNodesMode = false + LiteGraph.vueNodesMode = false }) it('calculates input slot position from node.pos', () => { @@ -211,7 +208,7 @@ describe('getSlotPosition — legacy fallback (vueNodesMode disabled)', () => { const output = makeOutput() const node = makeNode({ outputs: [output], collapsed: true }) const [x, y] = getSlotPosition(node, 0, false) - expect(x).toBe(100 + mockLiteGraph.NODE_COLLAPSED_WIDTH) + expect(x).toBe(180) expect(y).toBe(200 - TITLE_HEIGHT * 0.5) }) @@ -225,7 +222,7 @@ describe('getSlotPosition — legacy fallback (vueNodesMode disabled)', () => { describe('Vue slot geometry', () => { beforeEach(() => { - mockLiteGraph.vueNodesMode = true + LiteGraph.vueNodesMode = true layoutStore.resetForTests() }) diff --git a/src/renderer/core/canvas/litegraph/slotCalculations.ts b/src/renderer/core/canvas/litegraph/slotCalculations.ts index 7bf77fbb6fc..2e713f19289 100644 --- a/src/renderer/core/canvas/litegraph/slotCalculations.ts +++ b/src/renderer/core/canvas/litegraph/slotCalculations.ts @@ -1,3 +1,7 @@ +import type { + LGraphNode, + LGraph +} from '@/lib/litegraph/src/types/entityRegistry' /** * Slot Position Calculations * @@ -5,14 +9,13 @@ * This allows both litegraph nodes and the layout system to use the same * calculation logic while providing their own position data. */ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { LGraph } from '@/lib/litegraph/src/LGraph' + import type { Point } from '@/lib/litegraph/src/interfaces' import type { INodeInputSlot, INodeOutputSlot } from '@/lib/litegraph/src/types/slots' -import { LiteGraph } from '@/lib/litegraph/src/litegraph' +import { litegraph } from '@/lib/litegraph/src/litegraphInstance' import { isWidgetInputSlot } from '@/lib/litegraph/src/node/slotUtils' import { TitleMode } from '@/lib/litegraph/src/types/globalEnums' import { nodesInRenderOrder } from '@/renderer/core/canvas/litegraph/arrangeForLegacyRender' @@ -76,7 +79,7 @@ export function calculateInputSlotPosFromSlot( // Handle collapsed nodes if (collapsed) { - const halfTitle = LiteGraph.NODE_TITLE_HEIGHT * 0.5 + const halfTitle = litegraph().NODE_TITLE_HEIGHT * 0.5 return [nodeX, nodeY - halfTitle] } @@ -85,7 +88,7 @@ export function calculateInputSlotPosFromSlot( if (pos) return [nodeX + pos[0], nodeY + pos[1]] // Default vertical slots - const offsetX = LiteGraph.NODE_SLOT_HEIGHT * 0.5 + const offsetX = litegraph().NODE_SLOT_HEIGHT * 0.5 const nodeOffsetY = context.slotStartY || 0 let slotIndex = -1 const inputIndex = context.inputs.indexOf(input) @@ -99,7 +102,7 @@ export function calculateInputSlotPosFromSlot( } } } - const slotY = (slotIndex + 0.7) * LiteGraph.NODE_SLOT_HEIGHT + const slotY = (slotIndex + 0.7) * litegraph().NODE_SLOT_HEIGHT return [nodeX + offsetX, nodeY + slotY + nodeOffsetY] } @@ -119,8 +122,8 @@ function calculateOutputSlotPos( // Handle collapsed nodes if (collapsed) { - const width = collapsedWidth || LiteGraph.NODE_COLLAPSED_WIDTH - const halfTitle = LiteGraph.NODE_TITLE_HEIGHT * 0.5 + const width = collapsedWidth || litegraph().NODE_COLLAPSED_WIDTH + const halfTitle = litegraph().NODE_TITLE_HEIGHT * 0.5 return [nodeX + width, nodeY - halfTitle] } @@ -132,11 +135,11 @@ function calculateOutputSlotPos( if (outputPos) return [nodeX + outputPos[0], nodeY + outputPos[1]] // Default vertical slots - const offsetX = LiteGraph.NODE_SLOT_HEIGHT * 0.5 + const offsetX = litegraph().NODE_SLOT_HEIGHT * 0.5 const nodeOffsetY = context.slotStartY || 0 const defaultVerticalOutputs = getDefaultVerticalOutputs(context) const slotIndex = defaultVerticalOutputs.indexOf(outputSlot) - const slotY = (slotIndex + 0.7) * LiteGraph.NODE_SLOT_HEIGHT + const slotY = (slotIndex + 0.7) * litegraph().NODE_SLOT_HEIGHT // TODO: Why +1? return [nodeX + nodeWidth + 1 - offsetX, nodeY + slotY + nodeOffsetY] @@ -154,7 +157,7 @@ export function getSlotPosition( slotIndex: number, isInput: boolean ): Point { - if (LiteGraph.vueNodesMode) + if (litegraph().vueNodesMode) return getVueSlotPosition(node, slotIndex, isInput) const context: SlotPositionContext = { @@ -185,7 +188,7 @@ function getVueSlotPosition( if (offset) return [nodeX + offset.x, nodeY + offset.y] const nodeWidth = node.flags.collapsed - ? (node._collapsed_width ?? LiteGraph.NODE_COLLAPSED_WIDTH) + ? (node._collapsed_width ?? litegraph().NODE_COLLAPSED_WIDTH) : node.renderingSize[0] return calculateVueSlotPosition( node, @@ -223,7 +226,7 @@ function calculateVueSlotPosition( if (node.flags.collapsed) { return [ isInput ? nodeX : nodeX + nodeWidth, - nodeY - LiteGraph.NODE_TITLE_HEIGHT * 0.5 + nodeY - litegraph().NODE_TITLE_HEIGHT * 0.5 ] } @@ -265,7 +268,7 @@ function getWidgetSlotY( const widget = input._widget ?? node.widgets?.find((candidate) => candidate.name === input.widget.name) - return widget && widget.y + LiteGraph.NODE_SLOT_HEIGHT * 0.5 + return widget && widget.y + litegraph().NODE_SLOT_HEIGHT * 0.5 } function calculateVueOutputSlotPosition( @@ -288,13 +291,13 @@ function calculateVueOutputSlotPosition( function getVueSlotY(node: LGraphNode, slotIndex: number): number { return ( (slotIndex + (node.type === 'Reroute' ? 0.5 : 0.7)) * - LiteGraph.NODE_SLOT_HEIGHT + litegraph().NODE_SLOT_HEIGHT ) } function getVueNodeContentY(node: LGraphNode, nodeY: number): number { return node.title_mode === TitleMode.NO_TITLE - ? nodeY - LiteGraph.NODE_TITLE_HEIGHT + ? nodeY - litegraph().NODE_TITLE_HEIGHT : nodeY } @@ -320,7 +323,7 @@ function createSlotLayout( isInput: boolean, [x, y]: Point ): SlotLayout { - const size = LiteGraph.NODE_SLOT_HEIGHT + const size = litegraph().NODE_SLOT_HEIGHT const half = size * 0.5 return { nodeId: node.id, diff --git a/src/renderer/core/layout/operations/graphLayoutAttachment.ts b/src/renderer/core/layout/operations/graphLayoutAttachment.ts index f1e0f757d18..da30dc73d5e 100644 --- a/src/renderer/core/layout/operations/graphLayoutAttachment.ts +++ b/src/renderer/core/layout/operations/graphLayoutAttachment.ts @@ -1,7 +1,10 @@ -import type { LGraph } from '@/lib/litegraph/src/LGraph' -import type { LGraphGroup } from '@/lib/litegraph/src/LGraphGroup' -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' -import type { Reroute } from '@/lib/litegraph/src/Reroute' +import type { + LGraph, + LGraphGroup, + LGraphNode, + Reroute +} from '@/lib/litegraph/src/types/entityRegistry' + import { createMutationView } from '@/lib/litegraph/src/infrastructure/createMutationView' import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle' import type { diff --git a/src/renderer/core/layout/operations/layoutMutations.ts b/src/renderer/core/layout/operations/layoutMutations.ts index 08d1878aa24..452a73d423c 100644 --- a/src/renderer/core/layout/operations/layoutMutations.ts +++ b/src/renderer/core/layout/operations/layoutMutations.ts @@ -1,6 +1,7 @@ +import type { LGraph } from '@/lib/litegraph/src/types/entityRegistry' import type { NodeId } from '@/types/nodeId' import type { UUID } from '@/utils/uuid' -import type { LGraph } from '@/lib/litegraph/src/litegraph' + import { layoutStore } from '@/renderer/core/layout/store/layoutStore' import type { LayoutSource, Point } from '@/renderer/core/layout/types' diff --git a/src/stores/clearNodeOwnedStoreState.ts b/src/stores/clearNodeOwnedStoreState.ts index 241ed46d970..e10f7728c19 100644 --- a/src/stores/clearNodeOwnedStoreState.ts +++ b/src/stores/clearNodeOwnedStoreState.ts @@ -1,4 +1,5 @@ -import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode' +import type { LGraphNode } from '@/lib/litegraph/src/types/entityRegistry' + import { createNodeLocatorId } from '@/types/nodeIdentification' import { parseNodeId } from '@/types/nodeId' diff --git a/src/stores/graphMetadataStore.ts b/src/stores/graphMetadataStore.ts index 774031f67c5..e25a9d0a2b9 100644 --- a/src/stores/graphMetadataStore.ts +++ b/src/stores/graphMetadataStore.ts @@ -1,7 +1,10 @@ +import type { + LGraphConfig, + LGraphExtra +} from '@/lib/litegraph/src/types/entityRegistry' import { defineStore } from 'pinia' import { shallowReactive } from 'vue' -import type { LGraphConfig, LGraphExtra } from '@/lib/litegraph/src/LGraph' import type { UUID } from '@/utils/uuid' interface GraphMetadata { diff --git a/src/stores/previewExposureStore.ts b/src/stores/previewExposureStore.ts index aa3fa6dee9b..ec432b9b11e 100644 --- a/src/stores/previewExposureStore.ts +++ b/src/stores/previewExposureStore.ts @@ -1,3 +1,4 @@ +import type { SubgraphNode } from '@/lib/litegraph/src/types/entityRegistry' import { defineStore } from 'pinia' import { ref } from 'vue' @@ -8,7 +9,7 @@ import type { import { resolvePreviewExposureChain } from '@/core/graph/subgraph/preview/previewExposureChain' import type { PromotedWidgetSource } from '@/core/graph/subgraph/promotedWidgetTypes' import type { PreviewExposure } from '@/core/schemas/previewExposureSchema' -import type { SubgraphNode } from '@/lib/litegraph/src/litegraph' + import { nextUniqueName } from '@/lib/litegraph/src/strings' import { createNodeLocatorId } from '@/types/nodeIdentification' import type { NodeLocatorId } from '@/types/nodeIdentification' diff --git a/src/stores/widgetValueStore.ts b/src/stores/widgetValueStore.ts index c839ee9f7a3..e26cecc5218 100644 --- a/src/stores/widgetValueStore.ts +++ b/src/stores/widgetValueStore.ts @@ -11,7 +11,7 @@ import { widgetId as createWidgetId } from '@/types/widgetId' import type { WidgetId } from '@/types/widgetId' -import type { WidgetValue } from '@/types/simplifiedWidget' +import type { WidgetValue } from '@/types/widgetValue' import type { WidgetState, WidgetStateInit } from '@/types/widgetState' import { applyLegacyHiddenWrite, diff --git a/src/types/nodeProperty.ts b/src/types/nodeProperty.ts new file mode 100644 index 00000000000..6b42243a629 --- /dev/null +++ b/src/types/nodeProperty.ts @@ -0,0 +1 @@ +export type NodeProperty = string | number | boolean | object | null diff --git a/src/types/nodeState.ts b/src/types/nodeState.ts index 5e9f2805370..1be431777bf 100644 --- a/src/types/nodeState.ts +++ b/src/types/nodeState.ts @@ -10,9 +10,10 @@ import type { } from '@/lib/litegraph/src/types/globalEnums' import type { ISerialisedNode } from '@/lib/litegraph/src/types/serialisation' import type { NodeId } from '@/types/nodeId' +import type { NodeProperty } from '@/types/nodeProperty' import type { UUID } from '@/utils/uuid' -export type NodeProperty = string | number | boolean | object | null +export type { NodeProperty } from '@/types/nodeProperty' /** * The fields the renderer draws. Selection, execution, errors, geometry, widget diff --git a/src/types/simplifiedWidget.ts b/src/types/simplifiedWidget.ts index 0f34c5a08f7..f6cbbed6909 100644 --- a/src/types/simplifiedWidget.ts +++ b/src/types/simplifiedWidget.ts @@ -10,6 +10,9 @@ import type { import { IS_CONTROL_WIDGET } from '@/core/graph/widgets/controlWidgetMarker' import type { NodeId } from '@/types/nodeId' import type { NodeLocatorId } from '@/types/nodeIdentification' +import type { WidgetValue } from '@/types/widgetValue' + +export type { WidgetValue } from '@/types/widgetValue' export const CONTROL_OPTIONS = [ 'fixed', @@ -23,9 +26,6 @@ export function isControlOption(value: unknown): value is ControlOptions { return CONTROL_OPTIONS.some((option) => option === value) } -/** Valid types for widget values */ -export type WidgetValue = string | number | boolean | object | undefined | null - function normalizeControlOption(val: WidgetValue): ControlOptions { if (isControlOption(val)) return val return 'randomize' diff --git a/src/types/widgetState.ts b/src/types/widgetState.ts index 04e0d010599..50417df94f1 100644 --- a/src/types/widgetState.ts +++ b/src/types/widgetState.ts @@ -3,7 +3,7 @@ import type { IWidgetOptions } from '@/lib/litegraph/src/types/widgets' import type { NodeId } from '@/types/nodeId' -import type { WidgetValue } from '@/types/simplifiedWidget' +import type { WidgetValue } from '@/types/widgetValue' export interface WidgetState< TValue = WidgetValue, diff --git a/src/types/widgetValue.ts b/src/types/widgetValue.ts new file mode 100644 index 00000000000..593279f69bc --- /dev/null +++ b/src/types/widgetValue.ts @@ -0,0 +1 @@ +export type WidgetValue = string | number | boolean | object | undefined | null diff --git a/src/utils/graphTraversalUtil.ts b/src/utils/graphTraversalUtil.ts index 556a04cc690..408b1017ead 100644 --- a/src/utils/graphTraversalUtil.ts +++ b/src/utils/graphTraversalUtil.ts @@ -3,7 +3,8 @@ import type { LGraphNode, Subgraph, SubgraphNode -} from '@/lib/litegraph/src/litegraph' +} from '@/lib/litegraph/src/types/entityRegistry' + import { LGraphEventMode } from '@/lib/litegraph/src/types/globalEnums' import type { NodeExecutionId, NodeLocatorId } from '@/types/nodeIdentification' import { diff --git a/src/utils/typeGuardUtil.ts b/src/utils/typeGuardUtil.ts index 9d456c3b033..c16e68b48db 100644 --- a/src/utils/typeGuardUtil.ts +++ b/src/utils/typeGuardUtil.ts @@ -1,9 +1,9 @@ import type { - INodeSlot, LGraph, LGraphNode, Subgraph -} from '@/lib/litegraph/src/litegraph' +} from '@/lib/litegraph/src/types/entityRegistry' +import type { INodeSlot } from '@/lib/litegraph/src/interfaces' import type { ResultItemType } from '@/schemas/resultItemTypeSchema' /** diff --git a/vite.types.config.mts b/vite.types.config.mts index 531708909c3..81434eb2ad1 100644 --- a/vite.types.config.mts +++ b/vite.types.config.mts @@ -2,6 +2,8 @@ import { resolve } from 'path' import { defineConfig } from 'vite' import dts from 'vite-plugin-dts' +import { createLitegraphDeclarationTransform } from './scripts/litegraphDeclarations' + export default defineConfig({ build: { lib: { @@ -17,8 +19,12 @@ export default defineConfig({ plugins: [ dts({ copyDtsFiles: true, - rollupTypes: true, - tsconfigPath: 'tsconfig.types.json' + bundleTypes: true, + processor: 'vue', + tsconfigPath: 'tsconfig.types.json', + beforeWriteFile: createLitegraphDeclarationTransform( + resolve(__dirname, 'src/lib/litegraph/src') + ) }) ] })