Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
2 : import { iconForMediaType } from '@/platform/assets/utils/mediaIconUtil'
3 : import { appendCloudResParam } from '@/platform/distribution/cloudPreviewUtil'
4 : import { api } from '@/scripts/api'
5 : import { app } from '@/scripts/app'
6 : import type { useWidgetValueStore } from '@/stores/widgetValueStore'
7 : import { parseWidgetId } from '@comfyorg/graph-model/widgetId'
8 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
9 : import {
10 : extractWidgetStringValue,
11 : parseImageWidgetValue
12 : } from '@/utils/imageUtil'
13 :
14 : type LoaderMediaType = 'image' | 'video' | 'audio'
15 :
16 : export interface LoaderDropIndicator {
17 : iconClass: string
18 : mediaUrl: string | undefined
19 : mediaType: LoaderMediaType
20 : label: string | undefined
21 : onClick: () => void
22 : onMaskEdit: (() => void) | undefined
23 : }
24 :
25 : interface LoaderMediaConfig {
26 : /** Name of the widget holding the selected filename. */
27 : widgetName: string
28 : mediaType: LoaderMediaType
29 : labelKey: string
30 : }
31 :
32 : /**
33 : * Native loader node types that upload/select a single media file. App mode
34 : * only renders widgets, so these types are hard-coded here to get the same
35 : * inline preview as the normal editor. See FE-1344 for the long-term plan of
36 : * making the preview part of the selection widget itself.
37 : */
38 2355 : const LOADER_MEDIA_CONFIG: Partial<Record<string, LoaderMediaConfig>> = {
39 : LoadImage: {
40 : widgetName: 'image',
41 : mediaType: 'image',
42 : labelKey: 'linearMode.dragAndDropImage'
43 : },
44 : LoadVideo: {
45 : widgetName: 'file',
46 : mediaType: 'video',
47 : labelKey: 'linearMode.dragAndDropVideo'
48 : },
49 : LoadAudio: {
50 : widgetName: 'audio',
51 : mediaType: 'audio',
52 : labelKey: 'linearMode.dragAndDropAudio'
53 : }
54 : }
55 :
56 12 : function buildMediaUrl(
57 : mediaType: LoaderMediaType,
58 : filename: string,
59 : subfolder: string,
60 : type: string,
61 : previewRevision?: (filename: string) => number
62 : ) {
63 12 : if (!filename) return undefined
64 :
65 11 : const params = new URLSearchParams({ filename, subfolder, type })
66 11 : appendCloudResParam(params, filename)
67 12 : const revision = previewRevision?.(filename)
68 12 : if (revision) params.set('template_input_revision', String(revision))
69 12 : const previewParam = mediaType === 'image' ? app.getPreviewFormatParam() : ''
70 12 : return api.apiURL(`/view?${params}${previewParam}`)
71 : }
72 :
73 : /**
74 : * Builds the App mode drop-zone preview for native loader nodes
75 : * (`LoadImage`, `LoadVideo`, `LoadAudio`). Returns `undefined` for any other
76 : * node type.
77 : */
78 131 : export function getLoaderDropIndicator(
79 : node: LGraphNode,
80 : id: WidgetId,
81 : options: {
82 : mobile: boolean
83 : label: (key: string) => string
84 : onMaskEdit: (node: LGraphNode) => void
85 : widgetValueStore: Pick<ReturnType<typeof useWidgetValueStore>, 'getWidget'>
86 : previewRevision?: (filename: string) => number
87 : }
88 : ): LoaderDropIndicator | undefined {
89 131 : const config = LOADER_MEDIA_CONFIG[node.type]
90 131 : if (!config) return undefined
91 27 : if (parseWidgetId(id).name !== config.widgetName) return undefined
92 :
93 131 : const stringValue = extractWidgetStringValue(
94 : options.widgetValueStore.getWidget(id)?.value
95 : )
96 131 : const { filename, subfolder, type } = stringValue
97 : ? parseImageWidgetValue(stringValue)
98 : : { filename: '', subfolder: '', type: 'input' }
99 :
100 131 : const mediaUrl = buildMediaUrl(
101 : config.mediaType,
102 : filename,
103 : subfolder,
104 : type,
105 : options.previewRevision
106 : )
107 :
108 131 : return {
109 : iconClass: iconForMediaType(config.mediaType),
110 : mediaUrl,
111 : mediaType: config.mediaType,
112 : label: options.mobile ? undefined : options.label(config.labelKey),
113 : onClick: () =>
114 : node.widgets?.find((w) => w.name === 'upload')?.callback?.(undefined),
115 : onMaskEdit:
116 : config.mediaType === 'image' && mediaUrl
117 : ? () => options.onMaskEdit(node)
118 : : undefined
119 : }
120 : }
|