Line data Source code
1 : import { useEventListener } from '@vueuse/core'
2 : import { computed, shallowRef, triggerRef } from 'vue'
3 :
4 : import { promotedInputWidgets } from '@/core/graph/subgraph/promotedInputWidget'
5 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
6 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
7 : import type { InputWidgetConfig } from '@/platform/workflow/management/stores/comfyWorkflow'
8 : import { app } from '@/scripts/app'
9 : import { useAppModeStore } from '@/stores/appModeStore'
10 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
11 : import { isWidgetId, parseWidgetId } from '@comfyorg/graph-model/widgetId'
12 :
13 : export type ResolvedSelection =
14 : | {
15 : status: 'resolved'
16 : widgetId: WidgetId
17 : node: LGraphNode
18 : widget: IBaseWidget
19 : displayName: string
20 : config?: InputWidgetConfig
21 : }
22 : | {
23 : status: 'unknown'
24 : widgetId: WidgetId
25 : displayName: string
26 : config?: InputWidgetConfig
27 : }
28 :
29 157 : export function useResolvedSelectedInputs() {
30 157 : const appModeStore = useAppModeStore()
31 157 : const rootGraph = app.rootGraph
32 :
33 157 : const graphNodes = shallowRef<LGraphNode[]>([...rootGraph.nodes])
34 157 : const refreshGraphNodes = () => (graphNodes.value = [...rootGraph.nodes])
35 157 : useEventListener(() => rootGraph.events, 'configured', refreshGraphNodes)
36 157 : useEventListener(
37 : () => rootGraph.events,
38 : 'convert-to-subgraph',
39 : refreshGraphNodes
40 : )
41 157 : useEventListener(
42 : () => rootGraph.events,
43 : 'subgraph-created',
44 : refreshGraphNodes
45 : )
46 157 : useEventListener(
47 : () => rootGraph.events,
48 : 'node:slot-label:changed',
49 : () => triggerRef(graphNodes)
50 : )
51 :
52 157 : return computed<ResolvedSelection[]>(() => {
53 206 : void graphNodes.value
54 206 : return appModeStore.selectedInputs.flatMap(
55 : ([widgetId, displayName, config]): ResolvedSelection[] => {
56 236 : if (!isWidgetId(widgetId)) return []
57 236 : const { nodeId, name } = parseWidgetId(widgetId)
58 236 : const node = rootGraph.getNodeById(nodeId)
59 236 : const widgets = node?.isSubgraphNode()
60 : ? promotedInputWidgets(node)
61 : : node?.widgets
62 236 : const widget =
63 : widgets?.find((w) => w.widgetId === widgetId) ??
64 : widgets?.find((w) => w.name === name)
65 236 : if (!node || !widget) {
66 4 : return [{ status: 'unknown', widgetId, displayName, config }]
67 : }
68 232 : return [
69 : { status: 'resolved', widgetId, node, widget, displayName, config }
70 : ]
71 : }
72 : )
73 : })
74 : }
|