Line data Source code
1 : import type { LGraph } from '@/lib/litegraph/src/litegraph'
2 : import { calculateNodeBounds } from '@/renderer/core/spatial/boundsCalculator'
3 : import { useLinkStore } from '@/stores/linkStore'
4 : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
5 : import { createNodeLocatorId } from '@comfyorg/graph-model/nodeIdentification'
6 :
7 : import type { SizedExtents as MinimapBounds } from '@comfyorg/shared-frontend-utils/geometry'
8 : import type {
9 : MinimapGroupData,
10 : MinimapLinkData,
11 : MinimapNodeData,
12 : MinimapNodeProgressStates
13 : } from '@/renderer/extensions/minimap/types'
14 :
15 2356 : export class MinimapDataSource {
16 : constructor(
17 19474 : private readonly graph: LGraph | null,
18 19474 : private readonly nodeProgressStates: MinimapNodeProgressStates = {}
19 : ) {}
20 :
21 : private nodes: MinimapNodeData[] | null = null
22 :
23 : getNodes(): MinimapNodeData[] {
24 25041 : this.nodes ??= this.buildNodes()
25 25041 : return this.nodes
26 : }
27 :
28 : private buildNodes(): MinimapNodeData[] {
29 17112 : const graph = this.graph
30 17112 : if (!graph) return []
31 :
32 17112 : const nodeProgressStates = this.nodeProgressStates
33 17112 : const containingSubgraphId = graph.isRootGraph ? null : graph.id
34 :
35 17112 : return graph._nodes.map((node) => {
36 69475 : const [width, height] = node.renderingSize
37 69475 : const locatorId = createNodeLocatorId(containingSubgraphId, node.id)
38 69475 : const executionState = Object.hasOwn(nodeProgressStates, locatorId)
39 : ? nodeProgressStates[locatorId].state
40 : : null
41 :
42 69475 : return {
43 : id: node.id,
44 : x: node.pos[0],
45 : y: node.pos[1],
46 : width,
47 : height,
48 : bgcolor: node.bgcolor,
49 : mode: node.mode,
50 : hasErrors: node.has_errors,
51 : executionState
52 : }
53 : })
54 : }
55 :
56 : getLinks(): MinimapLinkData[] {
57 7929 : const graph = this.graph
58 7929 : if (!graph) return []
59 :
60 7929 : const linkStore = useLinkStore()
61 7929 : const scope = graphScopeOf(graph)
62 7929 : const links: MinimapLinkData[] = []
63 7929 : const nodeMap = new Map(this.getNodes().map((node) => [node.id, node]))
64 :
65 7929 : for (const link of linkStore.graphTopologies(scope)) {
66 52631 : const sourceNode = nodeMap.get(link.originNodeId)
67 52631 : const targetNode = nodeMap.get(link.targetNodeId)
68 52631 : if (!sourceNode || !targetNode) continue
69 :
70 31943 : links.push({
71 : sourceNode,
72 : targetNode,
73 : sourceSlot: link.originSlot,
74 : targetSlot: link.targetSlot
75 : })
76 : }
77 :
78 7929 : return links
79 : }
80 :
81 : getGroups(): MinimapGroupData[] {
82 9330 : return (
83 : this.graph?._groups.map((group) => ({
84 : x: group.pos[0],
85 : y: group.pos[1],
86 : width: group.size[0],
87 : height: group.size[1],
88 : color: group.color
89 : })) ?? []
90 : )
91 : }
92 :
93 : getBounds(): MinimapBounds {
94 7782 : return (
95 : calculateNodeBounds(
96 : this.getNodes().map((node) => ({
97 : pos: [node.x, node.y],
98 : size: [node.width, node.height]
99 : }))
100 : ) ?? {
101 : minX: 0,
102 : minY: 0,
103 : maxX: 100,
104 : maxY: 100,
105 : width: 100,
106 : height: 100
107 : }
108 : )
109 : }
110 :
111 : getNodeCount(): number {
112 19474 : return this.graph?._nodes.length ?? 0
113 : }
114 :
115 : hasData(): boolean {
116 19474 : return this.getNodeCount() > 0
117 : }
118 : }
|