Line data Source code
1 : import { useStepper } from '@vueuse/core'
2 : import { v4 as uuidv4 } from 'uuid'
3 : import { computed, ref } from 'vue'
4 :
5 : import type {
6 : ComfyHubPublishFormData,
7 : ExampleImage
8 : } from '@/platform/workflow/sharing/types/comfyHubTypes'
9 : import type { PublishPrefill } from '@/platform/workflow/sharing/types/shareTypes'
10 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
11 : import { normalizeTags } from '@/platform/workflow/sharing/utils/normalizeTags'
12 :
13 30 : const PUBLISH_STEPS = [
14 : 'describe',
15 : 'examples',
16 : 'finish',
17 : 'profileCreation'
18 : ] as const
19 :
20 : export type ComfyHubPublishStep = (typeof PUBLISH_STEPS)[number]
21 :
22 : // TODO: Migrate to a Pinia store alongside the profile gate singleton
23 30 : const cachedPrefills = new Map<string, PublishPrefill>()
24 :
25 30 : function createDefaultFormData(): ComfyHubPublishFormData {
26 : const workflowStore = useWorkflowStore()
27 30 : return {
28 : name: workflowStore.activeWorkflow?.filename ?? '',
29 : description: '',
30 : tags: [],
31 : models: [],
32 : customNodes: [],
33 : thumbnailType: 'image',
34 : thumbnailFile: null,
35 : thumbnailUrl: null,
36 : existingThumbnailType: null,
37 : comparisonBeforeFile: null,
38 : comparisonAfterFile: null,
39 : comparisonAfterUrl: null,
40 : exampleImages: [],
41 : tutorialUrl: '',
42 : metadata: {}
43 : }
44 : }
45 :
46 0 : function createExampleImagesFromUrls(urls: string[]): ExampleImage[] {
47 0 : return urls.map((url) => ({ id: uuidv4(), url }))
48 : }
49 :
50 2 : function extractPrefillFromFormData(
51 : formData: ComfyHubPublishFormData
52 : ): PublishPrefill {
53 2 : return {
54 : name: formData.name || undefined,
55 : description: formData.description || undefined,
56 : tags: formData.tags.length > 0 ? normalizeTags(formData.tags) : undefined,
57 : models: formData.models.length > 0 ? formData.models : undefined,
58 : customNodes:
59 : formData.customNodes.length > 0 ? formData.customNodes : undefined,
60 : thumbnailType: formData.thumbnailType,
61 : thumbnailUrl: formData.thumbnailUrl ?? undefined,
62 : thumbnailComparisonUrl: formData.comparisonAfterUrl ?? undefined,
63 : sampleImageUrls: formData.exampleImages
64 : .map((img) => img.url)
65 : .filter((url) => !url.startsWith('blob:')),
66 : tutorialUrl: formData.tutorialUrl || undefined,
67 : metadata:
68 : Object.keys(formData.metadata).length > 0 ? formData.metadata : undefined
69 : }
70 : }
71 :
72 2 : export function cachePublishPrefill(
73 : workflowPath: string,
74 : formData: ComfyHubPublishFormData
75 : ) {
76 2 : cachedPrefills.set(workflowPath, extractPrefillFromFormData(formData))
77 : }
78 :
79 30 : export function getCachedPrefill(workflowPath: string): PublishPrefill | null {
80 30 : return cachedPrefills.get(workflowPath) ?? null
81 : }
82 :
83 30 : export function useComfyHubPublishWizard() {
84 30 : const stepper = useStepper([...PUBLISH_STEPS])
85 30 : const formData = ref<ComfyHubPublishFormData>(createDefaultFormData())
86 :
87 30 : const canGoNext = computed(() => {
88 0 : if (stepper.isCurrent('describe')) {
89 0 : return formData.value.name.trim().length > 0
90 : }
91 0 : return true
92 : })
93 :
94 30 : const isLastStep = computed(() => stepper.isCurrent('finish'))
95 30 : const isProfileCreationStep = computed(() =>
96 : stepper.isCurrent('profileCreation')
97 : )
98 :
99 1 : function openProfileCreationStep() {
100 1 : stepper.goTo('profileCreation')
101 : }
102 :
103 0 : function closeProfileCreationStep() {
104 0 : stepper.goTo('finish')
105 : }
106 :
107 0 : function applyPrefill(prefill: PublishPrefill) {
108 0 : const defaults = createDefaultFormData()
109 0 : const current = formData.value
110 0 : const hasThumbnail = !!(current.thumbnailFile || current.thumbnailUrl)
111 0 : const hasComparisonAfter = !!(
112 : current.comparisonAfterFile || current.comparisonAfterUrl
113 : )
114 0 : const restoredThumbnailUrl = hasThumbnail
115 : ? current.thumbnailUrl
116 : : (prefill.thumbnailUrl ?? current.thumbnailUrl)
117 0 : formData.value = {
118 : ...current,
119 : name:
120 : current.name === defaults.name
121 : ? (prefill.name ?? current.name)
122 : : current.name,
123 : description:
124 : current.description === defaults.description
125 : ? (prefill.description ?? current.description)
126 : : current.description,
127 : tags:
128 : current.tags.length === 0 && prefill.tags?.length
129 : ? prefill.tags
130 : : current.tags,
131 : models:
132 : current.models.length === 0 && prefill.models?.length
133 : ? prefill.models
134 : : current.models,
135 : customNodes:
136 : current.customNodes.length === 0 && prefill.customNodes?.length
137 : ? prefill.customNodes
138 : : current.customNodes,
139 : thumbnailType:
140 : current.thumbnailType === defaults.thumbnailType
141 : ? (prefill.thumbnailType ?? current.thumbnailType)
142 : : current.thumbnailType,
143 : thumbnailUrl: restoredThumbnailUrl,
144 : existingThumbnailType:
145 : restoredThumbnailUrl && !current.thumbnailFile
146 : ? (prefill.thumbnailType ?? current.existingThumbnailType)
147 : : current.existingThumbnailType,
148 : comparisonAfterUrl: hasComparisonAfter
149 : ? current.comparisonAfterUrl
150 : : (prefill.thumbnailComparisonUrl ?? current.comparisonAfterUrl),
151 : exampleImages:
152 : current.exampleImages.length === 0 && prefill.sampleImageUrls?.length
153 : ? createExampleImagesFromUrls(prefill.sampleImageUrls)
154 : : current.exampleImages,
155 : tutorialUrl:
156 : current.tutorialUrl === defaults.tutorialUrl
157 : ? (prefill.tutorialUrl ?? current.tutorialUrl)
158 : : current.tutorialUrl,
159 : metadata:
160 : Object.keys(current.metadata).length === 0 && prefill.metadata
161 : ? prefill.metadata
162 : : current.metadata
163 : }
164 : }
165 :
166 30 : return {
167 : currentStep: stepper.current,
168 : formData,
169 : canGoNext,
170 : isFirstStep: stepper.isFirst,
171 : isLastStep,
172 : isProfileCreationStep,
173 : goToStep: stepper.goTo,
174 : goNext: stepper.goToNext,
175 : goBack: stepper.goToPrevious,
176 : openProfileCreationStep,
177 : closeProfileCreationStep,
178 : applyPrefill
179 : }
180 : }
|