Line data Source code
1 : <script setup lang="ts">
2 : import { useDropZone } from '@vueuse/core'
3 : import { computed, ref } from 'vue'
4 :
5 : import { useClickDragGuard } from '@/composables/useClickDragGuard'
6 : import { cn } from '@comfyorg/tailwind-utils'
7 :
8 : import DropZoneIndicatorContent from './DropZoneIndicatorContent.vue'
9 : import DropZoneMediaActions from './DropZoneMediaActions.vue'
10 :
11 : defineOptions({ inheritAttrs: false })
12 :
13 : const {
14 : onDragOver,
15 : onDragDrop,
16 : dropIndicator,
17 : forceHovered = false
18 : } = defineProps<{
19 : onDragOver?: (e: DragEvent) => boolean
20 : onDragDrop?: (e: DragEvent) => Promise<boolean> | boolean
21 : dropIndicator?: {
22 : iconClass?: string
23 : mediaUrl?: string
24 : mediaType?: 'image' | 'video' | 'audio'
25 : label?: string
26 : onClick?: (e: MouseEvent) => void
27 : onMaskEdit?: () => void
28 : }
29 : forceHovered?: boolean
30 : }>()
31 :
32 2355 : const mediaType = computed(() => dropIndicator?.mediaType ?? 'image')
33 : // Video's native controls and AudioPreviewPlayer's buttons are invalid
34 : // markup nested inside a <button> — render a <div> instead once one is shown.
35 119 : const hasPlayableMedia = computed(
36 : () => mediaType.value !== 'image' && !!dropIndicator?.mediaUrl
37 : )
38 :
39 119 : const dropZoneRef = ref<HTMLElement | null>(null)
40 119 : const canAcceptDrop = ref(false)
41 119 : const clickGuard = useClickDragGuard(5)
42 :
43 0 : function onPointerDown(e: PointerEvent) {
44 0 : clickGuard.recordStart(e)
45 : }
46 :
47 0 : function onIndicatorClick(e: MouseEvent) {
48 0 : const dragged = e.detail !== 0 && clickGuard.wasDragged(e)
49 0 : clickGuard.reset()
50 0 : if (dragged) return
51 0 : dropIndicator?.onClick?.(e)
52 : }
53 :
54 119 : const { isOverDropZone } = useDropZone(dropZoneRef, {
55 : onDrop: (_files, event) => {
56 : // Stop propagation to prevent global handlers from creating a new node
57 0 : event.stopPropagation()
58 :
59 0 : if (onDragDrop && event) {
60 0 : onDragDrop(event)
61 : }
62 0 : canAcceptDrop.value = false
63 : },
64 : onOver: (_, event) => {
65 0 : if (onDragOver && event) {
66 0 : canAcceptDrop.value = onDragOver(event)
67 : }
68 : },
69 : onLeave: () => {
70 0 : canAcceptDrop.value = false
71 : }
72 : })
73 :
74 119 : const isHovered = computed(
75 : () => forceHovered || (canAcceptDrop.value && isOverDropZone.value)
76 : )
77 119 : const indicatorTag = computed(() =>
78 : dropIndicator?.onClick && !hasPlayableMedia.value ? 'button' : 'div'
79 : )
80 119 : </script>
81 : <template>
82 : <div
83 131 : v-if="onDragOver && onDragDrop"
84 : ref="dropZoneRef"
85 : v-bind="$attrs"
86 : data-slot="drop-zone"
87 : :class="cn('rounded-lg transition-colors', isHovered && 'bg-muted')"
88 : >
89 : <slot />
90 : <div v-if="dropIndicator" class="group/dropzone relative">
91 : <component
92 : :is="indicatorTag"
93 : :type="indicatorTag === 'button' ? 'button' : undefined"
94 : :aria-label="
95 : indicatorTag === 'button' ? dropIndicator.label : undefined
96 : "
97 : data-slot="drop-zone-indicator"
98 : data-testid="drop-zone-indicator"
99 : :class="
100 : cn(
101 : 'm-3 block h-25 resize-y appearance-none overflow-hidden rounded-lg border border-border bg-transparent p-1 text-left text-muted-foreground transition-colors',
102 : indicatorTag === 'button' && 'cursor-pointer'
103 : )
104 : "
105 : @pointerdown="onPointerDown"
106 : @click.prevent="onIndicatorClick"
107 : >
108 : <DropZoneIndicatorContent
109 : :media-type="mediaType"
110 : :media-url="dropIndicator.mediaUrl"
111 : :label="dropIndicator.label"
112 : :icon-class="dropIndicator.iconClass"
113 : :is-hovered="isHovered"
114 : />
115 : </component>
116 : <DropZoneMediaActions
117 : v-if="mediaType === 'image' && dropIndicator.mediaUrl"
118 : :media-url="dropIndicator.mediaUrl"
119 : :label="dropIndicator.label"
120 : :on-mask-edit="dropIndicator.onMaskEdit"
121 : />
122 : </div>
123 : </div>
124 : <slot v-else />
125 : </template>
|