Line data Source code
1 16 : <template>
2 0 : <div
3 : ref="tileRef"
4 : :class="
5 : cn(
6 : 'group relative aspect-square overflow-hidden rounded-sm outline-offset-2 focus-visible:outline-2 focus-visible:outline-ring',
7 : state === 'dragging' && 'opacity-40',
8 : state === 'over' && 'ring-2 ring-primary'
9 : )
10 : "
11 : tabindex="0"
12 : role="listitem"
13 : :aria-label="
14 : $t('comfyHubPublish.exampleImagePosition', {
15 : index: index + 1,
16 : total: total
17 : })
18 : "
19 : @pointerdown="tileRef && focusVisible(tileRef)"
20 : @keydown.left.prevent="handleArrowKey(-1, $event)"
21 : @keydown.right.prevent="handleArrowKey(1, $event)"
22 : @keydown.delete.prevent="handleRemove"
23 : @keydown.backspace.prevent="handleRemove"
24 : @dragover.prevent.stop
25 : @drop.prevent.stop="handleFileDrop"
26 : >
27 : <img
28 : :src="image.url"
29 : :alt="$t('comfyHubPublish.exampleImage', { index: index + 1 })"
30 : class="pointer-events-none size-full object-cover"
31 : draggable="false"
32 : />
33 : <Button
34 : variant="textonly"
35 : size="icon"
36 : :aria-label="$t('comfyHubPublish.removeExampleImage')"
37 : tabindex="-1"
38 : class="absolute top-1 right-1 flex size-6 bg-black/60 text-white opacity-0 transition-opacity group-focus-visible:opacity-100 hover:bg-black/80 [[data-slot=example-grid]:not(:has(:focus-visible))_&]:group-hover:opacity-100"
39 : @click="$emit('remove', image.id)"
40 : >
41 : <i class="icon-[lucide--x]" aria-hidden="true" />
42 : </Button>
43 : </div>
44 : </template>
45 :
46 : <script setup lang="ts">
47 : import { setCustomNativeDragPreview } from '@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview'
48 : import { nextTick, ref } from 'vue'
49 :
50 : import Button from '@comfyorg/design-system/button/Button'
51 : import {
52 : usePragmaticDraggable,
53 : usePragmaticDroppable
54 : } from '@/composables/usePragmaticDragAndDrop'
55 : import type { ExampleImage } from '@/platform/workflow/sharing/types/comfyHubTypes'
56 : import { cn } from '@comfyorg/tailwind-utils'
57 :
58 : const { image, index, total, instanceId } = defineProps<{
59 : image: ExampleImage
60 : index: number
61 : total: number
62 : instanceId: symbol
63 : }>()
64 :
65 0 : const emit = defineEmits<{
66 : remove: [id: string]
67 : move: [id: string, direction: number]
68 : insertFiles: [index: number, files: FileList]
69 : }>()
70 :
71 : // focusVisible is a Chromium 122+ extension to FocusOptions
72 : // (not yet in TypeScript's lib.dom.d.ts)
73 0 : function focusVisible(el: HTMLElement) {
74 0 : el.focus({ focusVisible: true } as FocusOptions)
75 : }
76 :
77 0 : async function handleArrowKey(direction: number, event: KeyboardEvent) {
78 0 : if (event.shiftKey) {
79 0 : emit('move', image.id, direction)
80 0 : await nextTick()
81 0 : if (tileRef.value) focusVisible(tileRef.value)
82 : } else {
83 0 : focusSibling(direction)
84 : }
85 : }
86 :
87 0 : function focusSibling(direction: number) {
88 0 : const sibling =
89 : direction < 0
90 : ? tileRef.value?.previousElementSibling
91 : : tileRef.value?.nextElementSibling
92 0 : if (sibling instanceof HTMLElement) focusVisible(sibling)
93 : }
94 :
95 0 : async function handleRemove() {
96 0 : const next =
97 : tileRef.value?.nextElementSibling ?? tileRef.value?.previousElementSibling
98 0 : emit('remove', image.id)
99 0 : await nextTick()
100 0 : if (next instanceof HTMLElement) focusVisible(next)
101 : }
102 :
103 0 : function handleFileDrop(event: DragEvent) {
104 0 : if (event.dataTransfer?.files?.length) {
105 0 : emit('insertFiles', index, event.dataTransfer.files)
106 : }
107 : }
108 :
109 0 : const tileRef = ref<HTMLElement | null>(null)
110 :
111 : type DragState = 'idle' | 'dragging' | 'over'
112 0 : const state = ref<DragState>('idle')
113 :
114 0 : const tileGetter = () => tileRef.value as HTMLElement
115 :
116 0 : usePragmaticDraggable(tileGetter, {
117 : getInitialData: () => ({
118 : type: 'example-image',
119 : imageId: image.id,
120 : instanceId
121 : }),
122 : onGenerateDragPreview: ({ nativeSetDragImage }) => {
123 0 : setCustomNativeDragPreview({
124 : nativeSetDragImage,
125 : render: ({ container }) => {
126 0 : const img = tileRef.value?.querySelector('img')
127 0 : if (!img) return
128 0 : const preview = img.cloneNode(true) as HTMLImageElement
129 0 : preview.style.width = '8rem'
130 0 : preview.style.height = '8rem'
131 0 : preview.style.objectFit = 'cover'
132 0 : preview.style.borderRadius = '4px'
133 0 : container.appendChild(preview)
134 : }
135 : })
136 : },
137 : onDragStart: () => {
138 0 : state.value = 'dragging'
139 : },
140 : onDrop: () => {
141 0 : state.value = 'idle'
142 : }
143 : })
144 :
145 0 : usePragmaticDroppable(tileGetter, {
146 : getData: () => ({ imageId: image.id }),
147 : canDrop: ({ source }) =>
148 : source.data.instanceId === instanceId &&
149 : source.data.type === 'example-image' &&
150 : source.data.imageId !== image.id,
151 : onDragEnter: () => {
152 0 : state.value = 'over'
153 : },
154 : onDragLeave: () => {
155 0 : state.value = 'idle'
156 : },
157 : onDrop: () => {
158 0 : state.value = 'idle'
159 : }
160 : })
161 : </script>
|