Move exportWorkflow from menu to workflowService (#1399)

This commit is contained in:
Chenlei Hu
2024-11-01 19:44:21 -04:00
committed by GitHub
parent d254559e20
commit d719a4e0fb
3 changed files with 42 additions and 36 deletions

View File

@@ -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<string | null> {
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<void> {
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)
}
}

View File

@@ -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<string | null> {
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<void> {
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)
}
}

View File

@@ -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')
}
},
{