Line data Source code
1 2 : <template>
2 : <div class="flex min-h-0 flex-1 flex-col">
3 : <Teleport defer to="#layer-editor-header-actions">
4 : <Button
5 : v-if="mode === 'compositor'"
6 : variant="secondary"
7 : size="md"
8 : :disabled="!session.canUndo.value"
9 : @click="onRestore"
10 : >
11 : <i class="icon-[lucide--rotate-ccw]" />
12 : {{ t('g.restore') }}
13 : </Button>
14 : </Teleport>
15 : <div class="flex min-h-0 flex-1">
16 : <LayerPanel :session="session" />
17 : <div class="relative flex min-h-0 min-w-0 flex-1">
18 : <LayerEditorCanvas :session="session" />
19 : <LayerEditorToolbar
20 : :session="session"
21 : :exporting="exporting"
22 : @export-psd="exportPsd"
23 : />
24 : </div>
25 : <LayerPropertiesPanel :session="session" />
26 : </div>
27 : </div>
28 : </template>
29 :
30 : <script setup lang="ts">
31 : import { useEventListener } from '@vueuse/core'
32 : import { onMounted, onUnmounted } from 'vue'
33 : import { useI18n } from 'vue-i18n'
34 :
35 : import LayerEditorCanvas from '@comfyorg/layer-editor-ui/LayerEditorCanvas'
36 : import LayerEditorToolbar from '@comfyorg/layer-editor-ui/LayerEditorToolbar'
37 : import LayerPanel from '@comfyorg/layer-editor-ui/LayerPanel'
38 : import LayerPropertiesPanel from '@comfyorg/layer-editor-ui/LayerPropertiesPanel'
39 : import Button from '@comfyorg/design-system/button/Button'
40 : import { useLayerEditorExport } from '@/renderer/extensions/layerEditor/composables/useLayerEditorExport'
41 : import {
42 : saveCompositorLayerState,
43 : saveCompositorPreview
44 : } from '@/renderer/extensions/layerEditor/compositor/compositorSave'
45 : import { loadCompositorSession } from '@/renderer/extensions/layerEditor/compositor/compositorSession'
46 : import { useCompositorAutoSave } from '@/renderer/extensions/layerEditor/compositor/useCompositorAutoSave'
47 : import {
48 : isTextEditingTarget,
49 : useLayerEditorSession
50 : } from '@comfyorg/layer-editor/vue'
51 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
52 : import { useToast } from '@/components/ui/toast/toastStore'
53 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
54 : import { useNodeOutputStore } from '@/stores/nodeOutputStore'
55 :
56 : const { node, mode = 'images' } = defineProps<{
57 : node: LGraphNode
58 : mode?: 'images' | 'compositor'
59 : }>()
60 :
61 2 : const { t } = useI18n()
62 2 : const session = useLayerEditorSession()
63 2 : const { exporting, exportPsd } = useLayerEditorExport(session)
64 2 : const changeTracker =
65 : mode === 'compositor'
66 : ? useWorkflowStore().activeWorkflow?.changeTracker
67 : : undefined
68 :
69 2 : let autoSave: { stop(): void } | null = null
70 2 : let closed = false
71 :
72 4 : function layerName(url: string, index: number): string {
73 4 : try {
74 4 : const filename = new URL(url, window.location.origin).searchParams.get(
75 : 'filename'
76 : )
77 4 : if (filename) return filename.replace(/\.[^.]+$/, '')
78 : } catch {
79 : void 0
80 : }
81 0 : return t('layerEditor.layerN', { n: index + 1 })
82 : }
83 :
84 2 : useEventListener(document, 'keydown', (e: KeyboardEvent) => {
85 2 : if (e.defaultPrevented || isTextEditingTarget(e.target)) return
86 2 : if (!(e.ctrlKey || e.metaKey)) return
87 0 : if (e.code === 'KeyZ') {
88 0 : e.preventDefault()
89 0 : if (e.shiftKey) session.redo()
90 0 : else session.undo()
91 0 : } else if (e.code === 'KeyY') {
92 0 : e.preventDefault()
93 0 : session.redo()
94 : }
95 : })
96 :
97 0 : function onRestore(): void {
98 0 : session.editor.cancelFloating()
99 0 : while (session.editor.history.canUndo()) session.undo()
100 : }
101 :
102 0 : async function loadCompositorLayers(): Promise<number> {
103 0 : return loadCompositorSession(session, node, (i) =>
104 : t('layerEditor.layerN', { n: i + 1 })
105 : )
106 : }
107 :
108 0 : function sessionHasEdits(): boolean {
109 0 : return (
110 : Boolean(session.editor.floating()) ||
111 : session.editor.history.canUndo() ||
112 : session.editor.history.canRedo()
113 : )
114 : }
115 :
116 0 : function finalizeCompositorSession(): void {
117 0 : try {
118 0 : autoSave?.stop()
119 0 : if (autoSave && sessionHasEdits()) {
120 0 : session.editor.anchorFloating()
121 0 : if (saveCompositorLayerState(session, node)) {
122 0 : void saveCompositorPreview(session, node)
123 0 : node.graph?.setDirtyCanvas(true)
124 : } else {
125 0 : useToast().error(t('g.error'), {
126 : description: t('compositor.saveFailed')
127 : })
128 : }
129 0 : } else if (sessionHasEdits()) {
130 0 : useToast().error(t('g.error'), {
131 : description: t('compositor.saveFailed')
132 : })
133 : }
134 : } finally {
135 0 : changeTracker?.afterChange()
136 : }
137 : }
138 :
139 2 : onMounted(() => {
140 2 : if (mode === 'compositor') {
141 0 : changeTracker?.beforeChange()
142 0 : loadCompositorLayers()
143 : .then((failed) => {
144 0 : if (closed) return
145 0 : if (failed > 0) {
146 0 : useToast().warning(t('layerEditor.title'), {
147 : description: t('layerEditor.layersFailedToLoad', { count: failed })
148 : })
149 0 : return
150 : }
151 0 : autoSave = useCompositorAutoSave(session, node)
152 : })
153 : .catch((err) => {
154 0 : console.error('[Compositor] Loading layers failed:', err)
155 0 : if (closed) return
156 0 : useToast().error(t('g.error'), {
157 : description: t('layerEditor.loadFailed')
158 : })
159 : })
160 0 : return
161 : }
162 2 : const urls = useNodeOutputStore().getNodeImageUrls(node) ?? []
163 2 : const names = urls.map((url, i) => layerName(url, i))
164 2 : session
165 : .loadImages(urls, names)
166 : .then((failed) => {
167 2 : if (closed || failed === 0) return
168 0 : useToast().warning(t('layerEditor.title'), {
169 : description: t('layerEditor.layersFailedToLoad', { count: failed })
170 : })
171 : })
172 : .catch((err) => console.error('[LayerEditor] Loading images failed:', err))
173 : })
174 :
175 2 : onUnmounted(() => {
176 0 : closed = true
177 0 : if (mode === 'compositor') finalizeCompositorSession()
178 0 : session.dispose()
179 : })
180 : </script>
|