Line data Source code
1 : import { ref } from 'vue'
2 : import { useI18n } from 'vue-i18n'
3 :
4 : import { downloadBlob } from '@/base/common/downloadUtil'
5 : import type { LayerEditorSession } from '@comfyorg/layer-editor/vue'
6 : import { buildSessionPsdBlob } from '@comfyorg/layer-editor/export/psdExport'
7 : import { useToast } from '@/components/ui/toast/toastStore'
8 :
9 0 : function exportTimestamp(d: Date): string {
10 0 : const pad = (n: number) => String(n).padStart(2, '0')
11 : const date = `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}`
12 : const time = `${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`
13 : return `${date}-${time}`
14 : }
15 :
16 0 : export function psdExportFilename(d: Date): string {
17 0 : return `comfyui-layers-${exportTimestamp(d)}.psd`
18 : }
19 :
20 2 : export function useLayerEditorExport(session: LayerEditorSession) {
21 2 : const { t } = useI18n()
22 2 : const toast = useToast()
23 2 : const exporting = ref(false)
24 :
25 0 : async function exportPsd(): Promise<void> {
26 0 : if (!session.glOk.value || exporting.value) return
27 0 : exporting.value = true
28 0 : try {
29 0 : const blob = await buildSessionPsdBlob(session)
30 0 : downloadBlob(psdExportFilename(new Date()), blob)
31 : } catch (err) {
32 0 : console.warn('[LayerEditor] PSD export failed', err)
33 0 : toast.error(t('g.error'), {
34 : description: t('layerEditor.exportPsdFailed')
35 : })
36 : } finally {
37 0 : exporting.value = false
38 0 : session.requestRender()
39 : }
40 : }
41 :
42 2 : return { exporting, exportPsd }
43 : }
|