First version of code to save workflows in the app

This commit is contained in:
alethiophile 2023-04-28 03:15:06 +00:00
parent fd7c636680
commit aa2e2c02ff
2 changed files with 81 additions and 4 deletions

View File

@ -1,3 +1,16 @@
export function load_saved() {
try {
let rv = JSON.parse(localStorage.getItem("saved") || "{}");
return rv;
} catch (error) {
return {};
}
}
export function save_saved(list) {
localStorage.setItem('saved', JSON.stringify(list));
}
class ComfyApi extends EventTarget { class ComfyApi extends EventTarget {
#registered = new Set(); #registered = new Set();
@ -231,10 +244,14 @@ class ComfyApi extends EventTarget {
* @returns The items of the specified type grouped by their status * @returns The items of the specified type grouped by their status
*/ */
async getItems(type) { async getItems(type) {
if (type === "queue") { switch (type) {
case "queue":
return this.getQueue(); return this.getQueue();
case "history":
return this.getHistory();
case "saved":
return this.getSaved();
} }
return this.getHistory();
} }
/** /**
@ -280,6 +297,39 @@ class ComfyApi extends EventTarget {
async getSystemStats() { async getSystemStats() {
const res = await this.fetchApi("/system_stats"); const res = await this.fetchApi("/system_stats");
return await res.json(); return await res.json();
}
/**
* Gets prompts saved by the user
* @returns Saved prompts with outputs
*/
async getSaved() {
// note that this could, and maybe should, be persisted
// server-side; however, the server doesn't have any persistence
// yet, so rather than add a dependency on sqlite or whatever
// I'll just use client storage for now
try {
const r = load_saved();
console.log("r is", r);
return { Saved: Object.values(r).map((i) => {
console.log(i);
i['remove'] = {
name: "Delete",
cb: () => {
let name = i.prompt[0];
let list = load_saved();
if (list.hasOwnProperty(name)) {
delete list[name];
}
save_saved(list);
},
};
return i;
})};
} catch (error) {
console.error(error);
return { Saved: [] };
}
} }
/** /**

View File

@ -1,4 +1,4 @@
import { api } from "./api.js"; import { api, load_saved, save_saved } from "./api.js";
import { ComfyDialog as _ComfyDialog } from "./ui/dialog.js"; import { ComfyDialog as _ComfyDialog } from "./ui/dialog.js";
import { toggleSwitch } from "./ui/toggleSwitch.js"; import { toggleSwitch } from "./ui/toggleSwitch.js";
import { ComfySettingsDialog } from "./ui/settings.js"; import { ComfySettingsDialog } from "./ui/settings.js";
@ -294,10 +294,12 @@ export class ComfyUI {
this.lastQueueSize = 0; this.lastQueueSize = 0;
this.queue = new ComfyList("Queue"); this.queue = new ComfyList("Queue");
this.history = new ComfyList("History", "history", true); this.history = new ComfyList("History", "history", true);
this.saved = new ComfyList("Saved");
api.addEventListener("status", () => { api.addEventListener("status", () => {
this.queue.update(); this.queue.update();
this.history.update(); this.history.update();
this.saved.update();
}); });
const confirmClear = this.settings.addSetting({ const confirmClear = this.settings.addSetting({
@ -521,9 +523,33 @@ export class ComfyUI {
]), ]),
this.queue.element, this.queue.element,
this.history.element, this.history.element,
this.saved.element,
$el("button", {
id: "comfy-save-local-button",
textContent: "Save",
onclick: () => {
let name = prompt("Name:", "");
let list = load_saved();
const new_item = {
prompt: [
name,
null,
null,
{
extra_pnginfo: {
workflow: app.graph.serialize(),
}
}
],
outputs: app.nodeOutputs,
};
list[name] = new_item;
save_saved(list);
},
}),
$el("button", { $el("button", {
id: "comfy-save-button", id: "comfy-save-button",
textContent: "Save", textContent: "Download",
onclick: () => { onclick: () => {
let filename = "workflow.json"; let filename = "workflow.json";
if (promptFilename.value) { if (promptFilename.value) {
@ -605,6 +631,7 @@ export class ComfyUI {
} }
}), }),
]); ]);
this.saved.show();
const devMode = this.settings.addSetting({ const devMode = this.settings.addSetting({
id: "Comfy.DevMode", id: "Comfy.DevMode",