Add PromptDialog to replace window.prompt (#1104)

* Save file prompt dialog

* Don't download if dialog dismissed

* refactor

* style dialog

* nit

* Autofocus
This commit is contained in:
Chenlei Hu
2024-10-04 15:33:27 -04:00
committed by GitHub
parent 39d68bcdc4
commit ebc71b0e46
12 changed files with 148 additions and 76 deletions

View File

@@ -2,6 +2,8 @@ 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
@@ -32,13 +34,18 @@ export class ComfyAppMenu {
])
}
getFilename(defaultName: string) {
if (this.app.ui.settings.getSettingValue('Comfy.PromptFilename', true)) {
defaultName = prompt('Save workflow as:', defaultName)
if (!defaultName) return
if (!defaultName.toLowerCase().endsWith('.json')) {
defaultName += '.json'
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
}
@@ -46,14 +53,14 @@ export class ComfyAppMenu {
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 = this.getFilename(filename)
const file = await this.getFilename(filename)
if (!file) return
downloadBlob(file, blob)
}