Line data Source code
1 : import { computed, onScopeDispose, shallowReactive, watch } from 'vue'
2 :
3 : import { toSelectableKey } from '@/core/selection/selectionState'
4 : import { isSelectedIn } from '@/core/selection/selectionStore'
5 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
6 : import { registerNodeEmphasisQuery } from '@/lib/litegraph/src/nodeEmphasis'
7 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
8 : import type { GraphScope } from '@comfyorg/graph-model/graphScopeId'
9 : import { graphScopeOf } from '@comfyorg/graph-model/graphScopeId'
10 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
11 :
12 2355 : const referenceSources = shallowReactive(new Set<() => NodeId | undefined>())
13 :
14 2355 : const referencedNodeIds = computed(() => {
15 2356 : const ids = new Set<NodeId>()
16 2356 : for (const source of referenceSources) {
17 1 : const id = source()
18 1 : if (id !== undefined) ids.add(id)
19 : }
20 2356 : return ids
21 : })
22 :
23 : /**
24 : * Emphasizes the node `source` names, without selecting it, for as long as
25 : * the calling scope lives. The caller stays the owner of the reference.
26 : */
27 1 : export function useNodeEmphasisSource(source: () => NodeId | undefined): void {
28 1 : referenceSources.add(source)
29 1 : onScopeDispose(() => referenceSources.delete(source))
30 : }
31 :
32 : /**
33 : * Whether a node of the graph `scope` is drawn emphasized: selected there,
34 : * or named by an emphasis source.
35 : */
36 143047 : export function isNodeEmphasized(
37 : scope: GraphScope | undefined,
38 : nodeId: NodeId
39 : ): boolean {
40 143047 : return (
41 : isSelectedIn(scope, toSelectableKey('node', nodeId)) ||
42 : referencedNodeIds.value.has(nodeId)
43 : )
44 : }
45 :
46 132680 : function isCanvasNodeEmphasized(node: LGraphNode): boolean {
47 : // A node outside any graph keeps its own selection flag.
48 132680 : return node.graph
49 : ? isNodeEmphasized(graphScopeOf(node.graph), node.id)
50 : : node.selected
51 : }
52 :
53 : /**
54 : * Installs {@link isNodeEmphasized} as the legacy canvas's emphasis query
55 : * and redraws that canvas when the referenced nodes change.
56 : */
57 2355 : export function useNodeEmphasis(): void {
58 2355 : const canvasStore = useCanvasStore()
59 2355 : onScopeDispose(registerNodeEmphasisQuery(isCanvasNodeEmphasized))
60 2355 : watch(referencedNodeIds, () => canvasStore.canvas?.setDirty(true, false))
61 : }
|