diff --git a/web/scripts/api.js b/web/scripts/api.js index 8c8155be6..902155610 100644 --- a/web/scripts/api.js +++ b/web/scripts/api.js @@ -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 { #registered = new Set(); @@ -231,10 +244,14 @@ class ComfyApi extends EventTarget { * @returns The items of the specified type grouped by their status */ async getItems(type) { - if (type === "queue") { + switch (type) { + case "queue": 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() { const res = await this.fetchApi("/system_stats"); 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: [] }; + } } /** diff --git a/web/scripts/ui.js b/web/scripts/ui.js index 5ca6214eb..0decba04b 100644 --- a/web/scripts/ui.js +++ b/web/scripts/ui.js @@ -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 { toggleSwitch } from "./ui/toggleSwitch.js"; import { ComfySettingsDialog } from "./ui/settings.js"; @@ -294,10 +294,12 @@ export class ComfyUI { this.lastQueueSize = 0; this.queue = new ComfyList("Queue"); this.history = new ComfyList("History", "history", true); + this.saved = new ComfyList("Saved"); api.addEventListener("status", () => { this.queue.update(); this.history.update(); + this.saved.update(); }); const confirmClear = this.settings.addSetting({ @@ -521,9 +523,33 @@ export class ComfyUI { ]), this.queue.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", { id: "comfy-save-button", - textContent: "Save", + textContent: "Download", onclick: () => { let filename = "workflow.json"; if (promptFilename.value) { @@ -605,6 +631,7 @@ export class ComfyUI { } }), ]); + this.saved.show(); const devMode = this.settings.addSetting({ id: "Comfy.DevMode",