Line data Source code
1 : import { computed } from 'vue'
2 : import type { ComputedRef } from 'vue'
3 :
4 : import { resolveWidgetInputSource } from '@/composables/widgetInputSource'
5 : import type { LGraphNode } from '@/lib/litegraph/src/litegraph'
6 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
7 : import { savedImageUrls } from '@/renderer/extensions/vueNodes/widgets/utils/savedImageUrls'
8 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
9 :
10 19 : const BEFORE_INPUT = 'image_a'
11 19 : const AFTER_INPUT = 'image_b'
12 :
13 19 : const BEFORE_OUTPUT = 'a_images'
14 19 : const AFTER_OUTPUT = 'b_images'
15 :
16 19 : const NO_IMAGES: readonly string[] = []
17 :
18 20 : export function useImageCompareImages(
19 : node: ComputedRef<LGraphNode | null | undefined>,
20 : renderedNodeId: NodeId
21 : ): {
22 : beforeImages: ComputedRef<readonly string[]>
23 : afterImages: ComputedRef<readonly string[]>
24 : } {
25 20 : const nodeOutputStore = useNodeOutputStore()
26 :
27 76 : function upstreamImages(inputName: string): readonly string[] {
28 76 : const target = node.value
29 76 : if (!target) return NO_IMAGES
30 :
31 76 : const slot = target.inputs.findIndex((input) => input.name === inputName)
32 76 : if (slot < 0) return NO_IMAGES
33 :
34 76 : const source = resolveWidgetInputSource(target, slot, renderedNodeId)
35 76 : if (!source) return NO_IMAGES
36 :
37 76 : return nodeOutputStore.getNodeImageUrls(source) ?? NO_IMAGES
38 : }
39 :
40 76 : function savedImages(outputKey: string): readonly string[] {
41 76 : const target = node.value
42 76 : if (!target) return NO_IMAGES
43 :
44 76 : return savedImageUrls(nodeOutputStore.getNodeOutputs(target)?.[outputKey])
45 : }
46 :
47 20 : const sideImages = (inputName: string, outputKey: string) =>
48 : computed<readonly string[]>(() => {
49 76 : const upstream = upstreamImages(inputName)
50 76 : return upstream.length ? upstream : savedImages(outputKey)
51 : })
52 :
53 20 : return {
54 : beforeImages: sideImages(BEFORE_INPUT, BEFORE_OUTPUT),
55 : afterImages: sideImages(AFTER_INPUT, AFTER_OUTPUT)
56 : }
57 : }
|