Line data Source code
1 16 : <template>
2 5 : <div class="flex min-h-0 flex-1 flex-col">
3 : <p class="text-sm select-none">
4 : {{
5 : $t('comfyHubPublish.examplesDescription', {
6 : total: MAX_EXAMPLES
7 : })
8 : }}
9 : </p>
10 :
11 : <div
12 : role="list"
13 : data-slot="example-grid"
14 : class="grid gap-2"
15 : style="grid-template-columns: repeat(auto-fill, 8rem)"
16 : >
17 : <!-- Upload tile (hidden when max images reached) -->
18 : <label
19 : v-if="showUploadTile"
20 : tabindex="0"
21 : role="button"
22 : :aria-label="$t('comfyHubPublish.uploadExampleImage')"
23 : class="flex aspect-square cursor-pointer flex-col items-center justify-center gap-2 rounded-lg border border-dashed text-center transition-colors hover:border-muted-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring"
24 : @dragenter.stop
25 : @dragleave.stop
26 : @dragover.prevent.stop
27 : @drop.prevent.stop="handleFileDrop"
28 : @keydown.enter.prevent="fileInputRef?.click()"
29 : @keydown.space.prevent="fileInputRef?.click()"
30 : >
31 : <input
32 : ref="fileInputRef"
33 : type="file"
34 : accept="image/*"
35 : multiple
36 : class="hidden"
37 : @change="handleFileSelect"
38 : />
39 : <i
40 : class="icon-[lucide--plus] text-muted-foreground"
41 : aria-hidden="true"
42 : />
43 : <span class="sr-only">{{
44 : $t('comfyHubPublish.uploadExampleImage')
45 : }}</span>
46 : </label>
47 :
48 : <!-- Example images (drag to reorder) -->
49 0 : <ReorderableExampleImage
50 : v-for="(image, index) in exampleImages"
51 : :key="image.id"
52 : :image="image"
53 : :index="index"
54 : :total="exampleImages.length"
55 : :instance-id="instanceId"
56 : @remove="removeImage"
57 : @move="moveImage"
58 : @insert-files="insertImagesAt"
59 : />
60 : </div>
61 : </div>
62 : </template>
63 :
64 : <script setup lang="ts">
65 : import { monitorForElements } from '@atlaskit/pragmatic-drag-and-drop/element/adapter'
66 : import { v4 as uuidv4 } from 'uuid'
67 : import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
68 :
69 : import type { ExampleImage } from '@/platform/workflow/sharing/types/comfyHubTypes'
70 : import {
71 : isFileTooLarge,
72 : MAX_IMAGE_SIZE_MB
73 : } from '@/platform/workflow/sharing/utils/validateFileSize'
74 : import ReorderableExampleImage from './ReorderableExampleImage.vue'
75 :
76 5 : const fileInputRef = ref<HTMLInputElement | null>(null)
77 :
78 30 : const MAX_EXAMPLES = 8
79 :
80 5 : const exampleImages = defineModel<ExampleImage[]>('exampleImages', {
81 : required: true
82 : })
83 :
84 5 : const showUploadTile = computed(() => exampleImages.value.length < MAX_EXAMPLES)
85 :
86 5 : const instanceId = Symbol('example-images')
87 :
88 5 : let cleanupMonitor = () => {}
89 :
90 5 : onMounted(() => {
91 5 : cleanupMonitor = monitorForElements({
92 : canMonitor: ({ source }) => source.data.instanceId === instanceId,
93 : onDrop: ({ source, location }) => {
94 0 : const destination = location.current.dropTargets[0]
95 0 : if (!destination) return
96 :
97 0 : const fromId = source.data.imageId
98 0 : const toId = destination.data.imageId
99 0 : if (typeof fromId !== 'string' || typeof toId !== 'string') return
100 :
101 0 : reorderImages(fromId, toId)
102 : }
103 : })
104 : })
105 :
106 5 : onBeforeUnmount(() => {
107 2 : cleanupMonitor()
108 : })
109 :
110 0 : function moveByIndex(fromIndex: number, toIndex: number) {
111 0 : if (fromIndex < 0 || toIndex < 0) return
112 0 : if (toIndex >= exampleImages.value.length || fromIndex === toIndex) return
113 :
114 0 : const updated = [...exampleImages.value]
115 0 : const [moved] = updated.splice(fromIndex, 1)
116 0 : updated.splice(toIndex, 0, moved)
117 0 : exampleImages.value = updated
118 : }
119 :
120 0 : function reorderImages(fromId: string, toId: string) {
121 0 : moveByIndex(
122 : exampleImages.value.findIndex((img) => img.id === fromId),
123 : exampleImages.value.findIndex((img) => img.id === toId)
124 : )
125 : }
126 :
127 0 : function moveImage(id: string, direction: number) {
128 0 : const currentIndex = exampleImages.value.findIndex((img) => img.id === id)
129 0 : moveByIndex(currentIndex, currentIndex + direction)
130 : }
131 :
132 0 : function removeImage(id: string) {
133 0 : const image = exampleImages.value.find((img) => img.id === id)
134 0 : if (image?.file) {
135 0 : URL.revokeObjectURL(image.url)
136 : }
137 0 : exampleImages.value = exampleImages.value.filter((img) => img.id !== id)
138 : }
139 :
140 0 : function createExampleImages(files: FileList): ExampleImage[] {
141 0 : return Array.from(files)
142 : .filter((f) => f.type.startsWith('image/'))
143 : .filter((f) => !isFileTooLarge(f, MAX_IMAGE_SIZE_MB))
144 : .map((file) => ({
145 : id: uuidv4(),
146 : url: URL.createObjectURL(file),
147 : file
148 : }))
149 : }
150 :
151 0 : function addImages(files: FileList) {
152 0 : const remaining = MAX_EXAMPLES - exampleImages.value.length
153 0 : if (remaining <= 0) return
154 :
155 0 : const created = createExampleImages(files)
156 0 : const newImages = created.slice(0, remaining)
157 0 : for (const img of created.slice(remaining)) {
158 0 : URL.revokeObjectURL(img.url)
159 : }
160 0 : if (newImages.length > 0) {
161 0 : exampleImages.value = [...newImages, ...exampleImages.value]
162 : }
163 : }
164 :
165 0 : function insertImagesAt(index: number, files: FileList) {
166 0 : const created = createExampleImages(files)
167 0 : if (created.length === 0) return
168 :
169 0 : const updated = [...exampleImages.value]
170 0 : const safeIndex = Math.min(Math.max(index, 0), updated.length)
171 0 : const remaining = MAX_EXAMPLES - exampleImages.value.length
172 0 : const maxInsert =
173 : remaining <= 0 ? Math.max(updated.length - safeIndex, 0) : remaining
174 0 : const newImages = created.slice(0, maxInsert)
175 0 : for (const img of created.slice(maxInsert)) {
176 0 : URL.revokeObjectURL(img.url)
177 : }
178 :
179 0 : if (newImages.length === 0) return
180 0 : if (remaining <= 0) {
181 0 : const replacedImages = updated.splice(
182 : safeIndex,
183 : newImages.length,
184 : ...newImages
185 : )
186 0 : for (const img of replacedImages) {
187 0 : if (img.file) URL.revokeObjectURL(img.url)
188 : }
189 : } else {
190 0 : updated.splice(safeIndex, 0, ...newImages)
191 : }
192 :
193 0 : exampleImages.value = updated
194 : }
195 :
196 0 : function handleFileSelect(event: Event) {
197 0 : if (!(event.target instanceof HTMLInputElement)) return
198 0 : if (event.target.files?.length) {
199 0 : addImages(event.target.files)
200 : }
201 : }
202 :
203 0 : function handleFileDrop(event: DragEvent) {
204 0 : if (event.dataTransfer?.files?.length) {
205 0 : addImages(event.dataTransfer.files)
206 : }
207 : }
208 : </script>
|