From d719a4e0fb33187872d82526ae9233d521d911c3 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Fri, 1 Nov 2024 19:44:21 -0400 Subject: [PATCH] Move exportWorkflow from menu to workflowService (#1399) --- src/scripts/ui/menu/index.ts | 34 ---------------------------- src/services/workflowService.ts | 39 +++++++++++++++++++++++++++++++++ src/stores/commandStore.ts | 5 +++-- 3 files changed, 42 insertions(+), 36 deletions(-) create mode 100644 src/services/workflowService.ts diff --git a/src/scripts/ui/menu/index.ts b/src/scripts/ui/menu/index.ts index 17dd88af39..4a78a3959b 100644 --- a/src/scripts/ui/menu/index.ts +++ b/src/scripts/ui/menu/index.ts @@ -1,9 +1,6 @@ import type { ComfyApp } from '@/scripts/app' import { $el } from '../../ui' -import { downloadBlob } from '../../utils' import { ComfyButtonGroup } from '../components/buttonGroup' -import { showPromptDialog } from '@/services/dialogService' -import { useSettingStore } from '@/stores/settingStore' import './menu.css' // Export to make sure following components are shimmed and exported by vite @@ -33,35 +30,4 @@ export class ComfyAppMenu { this.viewGroup.element ]) } - - async getFilename(defaultName: string): Promise { - if (useSettingStore().get('Comfy.PromptFilename')) { - let filename = await showPromptDialog({ - title: 'Export Workflow', - message: 'Enter the filename:', - defaultValue: defaultName - }) - if (!filename) return null - if (!filename.toLowerCase().endsWith('.json')) { - filename += '.json' - } - return filename - } - return defaultName - } - - async exportWorkflow( - filename: string, - promptProperty: 'workflow' | 'output' - ): Promise { - if (this.app.workflowManager.activeWorkflow?.path) { - filename = this.app.workflowManager.activeWorkflow.name - } - const p = await this.app.graphToPrompt() - const json = JSON.stringify(p[promptProperty], null, 2) - const blob = new Blob([json], { type: 'application/json' }) - const file = await this.getFilename(filename) - if (!file) return - downloadBlob(file, blob) - } } diff --git a/src/services/workflowService.ts b/src/services/workflowService.ts new file mode 100644 index 0000000000..f07b0523a6 --- /dev/null +++ b/src/services/workflowService.ts @@ -0,0 +1,39 @@ +import { downloadBlob } from '@/scripts/utils' +import { useSettingStore } from '@/stores/settingStore' +import { useWorkflowStore } from '@/stores/workflowStore' +import { showPromptDialog } from './dialogService' +import { app } from '@/scripts/app' + +async function getFilename(defaultName: string): Promise { + if (useSettingStore().get('Comfy.PromptFilename')) { + let filename = await showPromptDialog({ + title: 'Export Workflow', + message: 'Enter the filename:', + defaultValue: defaultName + }) + if (!filename) return null + if (!filename.toLowerCase().endsWith('.json')) { + filename += '.json' + } + return filename + } + return defaultName +} + +export const workflowService = { + async exportWorkflow( + filename: string, + promptProperty: 'workflow' | 'output' + ): Promise { + const workflow = useWorkflowStore().activeWorkflow + if (workflow?.path) { + filename = workflow.name + } + const p = await app.graphToPrompt() + const json = JSON.stringify(p[promptProperty], null, 2) + const blob = new Blob([json], { type: 'application/json' }) + const file = await getFilename(filename) + if (!file) return + downloadBlob(file, blob) + } +} diff --git a/src/stores/commandStore.ts b/src/stores/commandStore.ts index 9169cf8c7c..dd64a43988 100644 --- a/src/stores/commandStore.ts +++ b/src/stores/commandStore.ts @@ -20,6 +20,7 @@ import { type KeybindingImpl, useKeybindingStore } from './keybindingStore' import { useBottomPanelStore } from './workspace/bottomPanelStore' import { LGraphNode } from '@comfyorg/litegraph' import { useWorkspaceStore } from './workspaceStore' +import { workflowService } from '@/services/workflowService' export interface ComfyCommand { id: string @@ -167,7 +168,7 @@ export const useCommandStore = defineStore('command', () => { label: 'Export Workflow', menubarLabel: 'Export', function: () => { - app.menu.exportWorkflow('workflow', 'workflow') + workflowService.exportWorkflow('workflow', 'workflow') } }, { @@ -176,7 +177,7 @@ export const useCommandStore = defineStore('command', () => { label: 'Export Workflow (API Format)', menubarLabel: 'Export (API)', function: () => { - app.menu.exportWorkflow('workflow_api', 'output') + workflowService.exportWorkflow('workflow_api', 'output') } }, {