Line data Source code
1 2 : <template>
2 9 : <div class="flex w-52 min-w-52 flex-col border-r bg-background">
3 : <div class="flex items-center px-3 py-2">
4 : <span class="text-xs font-semibold text-foreground">
5 : {{ t('layerEditor.layers') }}
6 : </span>
7 : </div>
8 :
9 : <div class="min-h-0 flex-1 overflow-y-auto" @dragleave="onListDragLeave">
10 12 : <div
11 : v-for="node in rows"
12 : :key="node.id"
13 : :class="cn(rowClass(isRowSelected(node.id)), dropHintClass(node.id))"
14 : data-testid="layer-panel-row"
15 : :draggable="renamingId !== node.id"
16 : @click="onRowClick(node.id, $event)"
17 : @dragstart="onRowDragStart(node.id, $event)"
18 : @dragover="onRowDragOver(node.id, $event)"
19 : @drop="onRowDrop(node.id, $event)"
20 : @dragend="endDrag"
21 : >
22 : <button
23 : :class="eyeButtonClass(node.visible)"
24 : :title="t('layerEditor.toggleVisibility')"
25 : :aria-label="t('layerEditor.toggleVisibility')"
26 : @click.stop="session.toggleVisible(node.id)"
27 : >
28 : <i :class="eyeIconClass(node.visible)" />
29 : </button>
30 : <canvas
31 : :ref="(el) => registerThumb(node.id, el)"
32 : width="32"
33 : height="32"
34 : class="size-8 shrink-0 rounded-sm border bg-background"
35 : />
36 : <div class="flex min-w-0 flex-1 flex-col text-xs">
37 : <input
38 : v-if="renamingId === node.id"
39 : :ref="focusInput"
40 : class="min-w-0 rounded-sm border bg-background px-1 text-foreground select-text"
41 : :value="node.name"
42 : @blur="commitRename(node.id, $event)"
43 : @keydown.enter="commitRename(node.id, $event)"
44 : @keydown.escape="renamingId = null"
45 : @click.stop
46 : />
47 : <span
48 : v-else
49 : class="truncate text-foreground"
50 : :title="node.name"
51 : @dblclick="renamingId = node.id"
52 : >
53 : {{ node.name }}
54 : </span>
55 : <span class="truncate text-muted-foreground">
56 : {{ layerSubtitle(node) }}
57 : </span>
58 : </div>
59 : </div>
60 : </div>
61 :
62 : <div
63 : v-if="backgroundLayer"
64 : :class="rowClass(isRowSelected(backgroundLayer.id), 'border-t')"
65 : data-testid="layer-panel-background-row"
66 : @click="session.selectBackground()"
67 : >
68 : <button
69 : :class="eyeButtonClass(backgroundLayer.visible)"
70 : :title="t('layerEditor.toggleVisibility')"
71 : :aria-label="t('layerEditor.toggleVisibility')"
72 : @click.stop="session.setBackgroundVisible(!backgroundLayer.visible)"
73 : >
74 : <i :class="eyeIconClass(backgroundLayer.visible)" />
75 : </button>
76 : <div
77 : class="size-8 shrink-0 rounded-sm border"
78 : :style="{ backgroundColor: backgroundColor }"
79 : data-testid="layer-panel-background-swatch"
80 : />
81 : <div class="flex min-w-0 flex-1 flex-col text-xs">
82 : <span class="truncate text-foreground">
83 : {{ t('layerEditor.background') }}
84 : </span>
85 : <span class="truncate text-muted-foreground">
86 : {{ backgroundSubtitle }}
87 : </span>
88 : </div>
89 : </div>
90 :
91 : <div class="flex items-center justify-end gap-1 border-t p-1">
92 : <Button
93 : variant="textonly"
94 : size="icon"
95 : class="size-7 rounded-md disabled:opacity-40"
96 : :disabled="!canMoveActive"
97 : :title="t('layerEditor.moveUp')"
98 : :aria-label="t('layerEditor.moveUp')"
99 : @click="moveActive(1)"
100 : >
101 : <i class="icon-[lucide--chevron-up]" />
102 : </Button>
103 : <Button
104 : variant="textonly"
105 : size="icon"
106 : class="size-7 rounded-md disabled:opacity-40"
107 : :disabled="!canMoveActive"
108 : :title="t('layerEditor.moveDown')"
109 : :aria-label="t('layerEditor.moveDown')"
110 : @click="moveActive(-1)"
111 : >
112 : <i class="icon-[lucide--chevron-down]" />
113 : </Button>
114 : </div>
115 : </div>
116 : </template>
117 :
118 : <script setup lang="ts">
119 : import Button from '@comfyorg/design-system/button/Button'
120 : import { computed, ref, watch } from 'vue'
121 : import type { ComponentPublicInstance } from 'vue'
122 : import { useI18n } from 'vue-i18n'
123 :
124 : import type { LayerDropPos } from '@comfyorg/layer-editor/vue/layerPanelDnd'
125 : import {
126 : dropPositionFor,
127 : reorderDropIndex
128 : } from '@comfyorg/layer-editor/vue/layerPanelDnd'
129 : import { DEFAULT_BACKGROUND_COLOR } from '@comfyorg/layer-editor/vue'
130 : import type { LayerEditorSession } from '@comfyorg/layer-editor/vue'
131 : import type { RasterData, SceneNode } from '@comfyorg/layer-editor/node'
132 : import { cn } from '@comfyorg/tailwind-utils'
133 :
134 : const { session } = defineProps<{ session: LayerEditorSession }>()
135 :
136 2 : const { t } = useI18n()
137 2 : const {
138 : imageLayers,
139 : backgroundLayer,
140 : activeNode,
141 : activeNodeId,
142 : selectedNodeIds,
143 : canvasSize
144 : } = session
145 2 : const { version } = session
146 :
147 2 : const renamingId = ref<string | null>(null)
148 :
149 2 : const rows = computed<SceneNode[]>(() => [...imageLayers.value].reverse())
150 :
151 2 : const backgroundColor = computed(() => {
152 9 : const fill = backgroundLayer.value?.fill
153 9 : return fill?.type === 'solid' ? fill.color : DEFAULT_BACKGROUND_COLOR
154 : })
155 :
156 2 : const backgroundSubtitle = computed(
157 : () => `${canvasSize.value.w}×${canvasSize.value.h}`
158 : )
159 :
160 2 : const canMoveActive = computed(
161 : () => activeNode.value !== null && activeNode.value.kind !== 'fill'
162 : )
163 :
164 12 : function layerSubtitle(node: SceneNode): string {
165 : const blendInitial = node.mode.blend.charAt(0).toUpperCase()
166 12 : return `${blendInitial} ${Math.round(node.opacity * 100)}%`
167 : }
168 :
169 21 : function isRowSelected(id: string): boolean {
170 21 : return selectedNodeIds.value.includes(id) || activeNodeId.value === id
171 : }
172 :
173 21 : function rowClass(selected: boolean, extra?: string): string {
174 21 : return cn(
175 : 'group relative flex cursor-pointer items-center gap-2 border-border px-2 py-1.5 select-none',
176 : 'hover:bg-accent',
177 : selected && 'bg-muted',
178 : extra
179 : )
180 : }
181 :
182 21 : function eyeButtonClass(visible: boolean): string {
183 21 : return cn(
184 : 'flex size-6 shrink-0 cursor-pointer items-center justify-center rounded-sm border-0 bg-transparent text-foreground hover:bg-accent',
185 : visible && 'opacity-0 group-hover:opacity-100'
186 : )
187 : }
188 :
189 21 : function eyeIconClass(visible: boolean): string {
190 21 : return cn(
191 : 'size-4',
192 : visible ? 'icon-[lucide--eye]' : 'icon-[lucide--eye-off] opacity-50'
193 : )
194 : }
195 :
196 2 : const dragId = ref<string | null>(null)
197 2 : const dropHint = ref<{ id: string; pos: LayerDropPos } | null>(null)
198 :
199 0 : function endDrag(): void {
200 0 : dragId.value = null
201 0 : dropHint.value = null
202 : }
203 :
204 12 : function dropHintClass(id: string): string {
205 12 : const hint = dropHint.value
206 12 : if (hint?.id !== id) return ''
207 12 : return hint.pos === 'above'
208 : ? "before:absolute before:inset-x-0 before:top-0 before:h-0.5 before:bg-foreground before:content-['']"
209 : : "after:absolute after:inset-x-0 after:bottom-0 after:h-0.5 after:bg-foreground after:content-['']"
210 : }
211 :
212 0 : function onRowDragStart(id: string, e: DragEvent): void {
213 0 : dragId.value = id
214 0 : if (e.dataTransfer) {
215 0 : e.dataTransfer.effectAllowed = 'move'
216 0 : e.dataTransfer.setData('text/plain', '')
217 : }
218 : }
219 :
220 0 : function onRowDragOver(id: string, e: DragEvent): void {
221 0 : if (!dragId.value || dragId.value === id) return
222 0 : e.preventDefault()
223 0 : if (e.dataTransfer) e.dataTransfer.dropEffect = 'move'
224 0 : const rect = (e.currentTarget as HTMLElement).getBoundingClientRect()
225 : const ratio = rect.height > 0 ? (e.clientY - rect.top) / rect.height : 0.5
226 0 : const pos = dropPositionFor(ratio)
227 0 : if (dropHint.value?.id !== id || dropHint.value.pos !== pos)
228 0 : dropHint.value = { id, pos }
229 : }
230 :
231 0 : function onRowDrop(id: string, e: DragEvent): void {
232 0 : const dragged = dragId.value
233 0 : const hint = dropHint.value
234 0 : if (!dragged || hint?.id !== id) {
235 0 : endDrag()
236 0 : return
237 : }
238 0 : e.preventDefault()
239 0 : const toIndex = reorderDropIndex(
240 : imageLayers.value.map((n) => n.id),
241 : id,
242 : hint.pos,
243 : backgroundLayer.value ? 1 : 0
244 : )
245 0 : if (toIndex !== null) session.moveLayerTo(dragged, toIndex)
246 0 : endDrag()
247 : }
248 :
249 0 : function onListDragLeave(e: DragEvent): void {
250 : const container = e.currentTarget as HTMLElement
251 0 : if (!container.contains(e.relatedTarget as Node | null)) dropHint.value = null
252 : }
253 :
254 2 : function onRowClick(id: string, e: MouseEvent): void {
255 2 : if (e.ctrlKey || e.metaKey || e.shiftKey) {
256 0 : const current = selectedNodeIds.value
257 0 : session.setSelectedNodes(
258 : current.includes(id)
259 : ? current.filter((sid) => sid !== id)
260 : : [...current, id]
261 : )
262 0 : return
263 : }
264 2 : session.setActiveNode(id)
265 : }
266 :
267 0 : function commitRename(id: string, e: Event): void {
268 0 : if (renamingId.value !== id) return
269 0 : renamingId.value = null
270 0 : session.renameLayer(id, (e.target as HTMLInputElement).value)
271 : }
272 :
273 0 : function focusInput(el: Element | ComponentPublicInstance | null): void {
274 0 : if (el instanceof HTMLInputElement) el.focus()
275 : }
276 :
277 1 : function moveActive(dir: 1 | -1): void {
278 1 : const id = activeNodeId.value
279 1 : if (id) session.moveLayer(id, dir)
280 : }
281 :
282 24 : function computeFit(
283 : boxW: number,
284 : boxH: number,
285 : mediaW: number,
286 : mediaH: number
287 : ): { scale: number; offX: number; offY: number } {
288 24 : if (!boxW || !boxH || !mediaW || !mediaH)
289 0 : return { scale: 1, offX: 0, offY: 0 }
290 24 : const scale = Math.min(boxW / mediaW, boxH / mediaH)
291 24 : return {
292 : scale,
293 : offX: (boxW - mediaW * scale) / 2,
294 : offY: (boxH - mediaH * scale) / 2
295 : }
296 : }
297 :
298 2 : const thumbEls = new Map<string, HTMLCanvasElement>()
299 :
300 24 : function drawThumb(el: HTMLCanvasElement, node: SceneNode): void {
301 24 : const ctx = el.getContext('2d')
302 24 : if (!ctx) return
303 24 : ctx.clearRect(0, 0, el.width, el.height)
304 24 : if (node.kind !== 'raster') return
305 24 : const entry = session.content.get((node as RasterData).contentId)
306 24 : if (!entry) return
307 24 : const fit = computeFit(el.width, el.height, entry.width, entry.height)
308 24 : ctx.drawImage(
309 : entry.canvas,
310 : fit.offX,
311 : fit.offY,
312 : entry.width * fit.scale,
313 : entry.height * fit.scale
314 : )
315 : }
316 :
317 12 : function registerThumb(
318 : id: string,
319 : el: Element | ComponentPublicInstance | null
320 : ): void {
321 12 : if (el instanceof HTMLCanvasElement) {
322 12 : thumbEls.set(id, el)
323 12 : const node = imageLayers.value.find((n) => n.id === id)
324 12 : if (node) drawThumb(el, node)
325 : } else {
326 0 : thumbEls.delete(id)
327 : }
328 : }
329 :
330 2 : watch(
331 : version,
332 : () => {
333 7 : for (const node of imageLayers.value) {
334 12 : const el = thumbEls.get(node.id)
335 12 : if (el) drawThumb(el, node)
336 : }
337 : },
338 : { flush: 'post' }
339 : )
340 : </script>
|