Line data Source code
1 16 : <template>
2 : <div
3 : class="widget-expands relative flex size-full flex-col gap-1"
4 : @pointerdown.stop
5 : @pointermove.stop
6 : @pointerup.stop
7 : >
8 : <!-- Image preview container -->
9 : <div
10 : ref="containerEl"
11 : class="relative min-h-0 flex-1 overflow-hidden rounded-[5px] bg-node"
12 : >
13 : <div
14 : v-if="!imageUrl"
15 : class="flex size-full flex-col items-center justify-center text-center"
16 : data-testid="crop-empty-state"
17 : >
18 : <i
19 : class="mb-2 icon-[lucide--image] size-12"
20 : data-testid="crop-empty-icon"
21 : />
22 : <p class="text-sm">{{ $t('imageCrop.noInputImage') }}</p>
23 : </div>
24 :
25 : <template v-else>
26 : <img
27 : ref="imageEl"
28 : :src="imageUrl"
29 : :alt="$t('imageCrop.cropPreviewAlt')"
30 : draggable="false"
31 : class="block size-full object-contain select-none"
32 : @load="handleImageLoad"
33 : @error="handleImageError"
34 : @dragstart.prevent
35 : />
36 :
37 : <div
38 : v-if="isLoading"
39 : aria-live="polite"
40 : class="absolute inset-0 z-10 flex size-full items-center justify-center bg-node/90"
41 : >
42 : <span class="text-sm">{{ $t('imageCrop.loading') }}</span>
43 : </div>
44 :
45 : <div
46 : v-if="!isLoading"
47 : :class="
48 : cn(
49 : 'absolute box-content cursor-move border border-white shadow-[0_0_0_9999px_rgba(0,0,0,0.5)]',
50 : isDisabled && 'pointer-events-none opacity-60'
51 : )
52 : "
53 : :style="cropBoxStyle"
54 : data-testid="crop-overlay"
55 : @pointerdown="handleDragStart"
56 : @pointermove="handleDragMove"
57 : @pointerup="handleDragEnd"
58 : />
59 :
60 : <template v-for="handle in resizeHandles" :key="handle.direction">
61 1148 : <div
62 : v-show="!isLoading"
63 : :data-testid="`crop-resize-${handle.direction}`"
64 : :class="
65 : cn(
66 : 'absolute',
67 : handle.class,
68 : isDisabled && 'pointer-events-none opacity-60'
69 : )
70 : "
71 : :style="handle.style"
72 : @pointerdown="(e) => handleResizeStart(e, handle.direction)"
73 : @pointermove="handleResizeMove"
74 : @pointerup="handleResizeEnd"
75 : />
76 : </template>
77 : </template>
78 : </div>
79 :
80 : <div v-if="!isDisabled" class="flex shrink-0 items-center gap-2">
81 : <label>
82 : {{ $t('imageCrop.ratio') }}
83 : </label>
84 : <Select v-model="selectedRatio">
85 : <SelectTrigger class="h-7 w-24 text-xs">
86 : <SelectValue />
87 : </SelectTrigger>
88 : <SelectContent>
89 558 : <SelectItem v-for="key in ratioKeys" :key="key" :value="key">
90 : {{ key === 'custom' ? $t('imageCrop.custom') : key }}
91 : </SelectItem>
92 : </SelectContent>
93 : </Select>
94 : <Button
95 : size="icon"
96 : :variant="isLockEnabled ? 'primary' : 'secondary'"
97 : class="size-7"
98 : :aria-label="
99 : isLockEnabled
100 : ? $t('imageCrop.unlockRatio')
101 : : $t('imageCrop.lockRatio')
102 : "
103 : @click="isLockEnabled = !isLockEnabled"
104 : >
105 : <i
106 : :class="
107 : isLockEnabled
108 : ? 'icon-[lucide--lock] size-3.5'
109 : : 'icon-[lucide--lock-open] size-3.5'
110 : "
111 : />
112 : </Button>
113 : <WidgetHistoryButtons
114 : class="ml-auto"
115 : :can-undo
116 : :can-redo
117 : @undo="undo"
118 : @redo="redo"
119 : />
120 : </div>
121 :
122 : <WidgetBoundingBox
123 : v-model="effectiveBounds"
124 : :disabled="isDisabled"
125 : class="shrink-0"
126 : @edit-end="commitAfterUpdate"
127 : />
128 : </div>
129 : </template>
130 :
131 : <script setup lang="ts">
132 : import { computed, nextTick, useTemplateRef } from 'vue'
133 :
134 : import WidgetBoundingBox from '@/components/boundingbox/WidgetBoundingBox.vue'
135 : import Button from '@comfyorg/design-system/button/Button'
136 : import Select from '@comfyorg/design-system/select/Select'
137 : import SelectContent from '@comfyorg/design-system/select/SelectContent'
138 : import SelectItem from '@comfyorg/design-system/select/SelectItem'
139 : import SelectTrigger from '@comfyorg/design-system/select/SelectTrigger'
140 : import SelectValue from '@comfyorg/design-system/select/SelectValue'
141 : import WidgetHistoryButtons from '@/components/widgetHistory/WidgetHistoryButtons.vue'
142 : import { ASPECT_RATIOS, useImageCrop } from '@/composables/useImageCrop'
143 : import {
144 : boundsExtractor,
145 : useUpstreamValue
146 : } from '@/composables/useUpstreamValue'
147 : import { useWidgetValueHistory } from '@/composables/useWidgetValueHistory'
148 : import { widgetHistoryScope } from '@/composables/widgetHistoryScope'
149 : import type { Bounds } from '@comfyorg/shared-frontend-utils/geometry'
150 : import { isBoundsObject } from '@/renderer/core/layout/utils/geometry'
151 : import type { NodeId } from '@comfyorg/graph-model/nodeId'
152 : import type { SimplifiedWidget } from '@/types/simplifiedWidget'
153 : import { cn } from '@comfyorg/tailwind-utils'
154 :
155 : const { widget, nodeId } = defineProps<{
156 : widget: SimplifiedWidget
157 : nodeId: NodeId
158 : }>()
159 :
160 49 : const modelValue = defineModel<Bounds>({
161 : default: () => ({ x: 0, y: 0, width: 512, height: 512 })
162 : })
163 :
164 49 : const isDisabled = computed(() => !!widget.options?.disabled)
165 :
166 49 : const upstreamValue = useUpstreamValue(
167 : () => widget.linkedUpstream,
168 : boundsExtractor()
169 : )
170 :
171 49 : const effectiveBounds = computed({
172 : get: () =>
173 : isDisabled.value && upstreamValue.value
174 : ? upstreamValue.value
175 : : modelValue.value,
176 : set: (v) => {
177 26 : if (!isDisabled.value) modelValue.value = v
178 : }
179 : })
180 :
181 49 : const { canUndo, canRedo, undo, redo, commit } = useWidgetValueHistory({
182 : scope: () => widgetHistoryScope(widget, nodeId),
183 : read: () => modelValue.value,
184 : apply: (value) => {
185 0 : if (isBoundsObject(value)) modelValue.value = value
186 : }
187 : })
188 :
189 48 : function commitAfterUpdate(mergeKey?: string) {
190 48 : void nextTick(() => commit(mergeKey))
191 : }
192 :
193 49 : const imageEl = useTemplateRef<HTMLImageElement>('imageEl')
194 49 : const containerEl = useTemplateRef<HTMLDivElement>('containerEl')
195 :
196 49 : const ratioKeys = Object.keys(ASPECT_RATIOS)
197 :
198 49 : const {
199 : imageUrl,
200 : isLoading,
201 :
202 : selectedRatio,
203 : isLockEnabled,
204 :
205 : cropBoxStyle,
206 : resizeHandles,
207 :
208 : handleImageLoad,
209 : handleImageError,
210 : handleDragStart,
211 : handleDragMove,
212 : handleDragEnd,
213 : handleResizeStart,
214 : handleResizeMove,
215 : handleResizeEnd
216 : } = useImageCrop(nodeId, {
217 : widget,
218 : imageEl,
219 : containerEl,
220 : modelValue: effectiveBounds,
221 : onEditEnd: () => commitAfterUpdate()
222 : })
223 : </script>
|