mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-20 22:37:00 +08:00
workflow saving/loading
This commit is contained in:
parent
b823baf2be
commit
ec3822b1f7
@ -58,16 +58,16 @@ class UserManager():
|
||||
if os.path.commonpath((root_dir, user_root)) != root_dir:
|
||||
return None
|
||||
|
||||
parent = user_root
|
||||
|
||||
if file is not None:
|
||||
# prevent leaving /{type}/{user}
|
||||
path = os.path.abspath(os.path.join(user_root, file))
|
||||
if os.path.commonpath((user_root, path)) != user_root:
|
||||
return None
|
||||
|
||||
parent = os.path.split(path)[0]
|
||||
|
||||
if create_dir and not os.path.exists(parent):
|
||||
os.mkdir(parent)
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
|
||||
return path
|
||||
|
||||
@ -161,8 +161,13 @@ class UserManager():
|
||||
path = get_user_data_path(request)
|
||||
if not isinstance(path, str):
|
||||
return path
|
||||
|
||||
overwrite = request.query["overwrite"] != "false"
|
||||
if not overwrite and os.path.exists(path):
|
||||
return web.Response(status=409)
|
||||
|
||||
body = await request.read()
|
||||
|
||||
with open(path, "wb") as f:
|
||||
f.write(body)
|
||||
|
||||
|
||||
@ -404,18 +404,19 @@ class ComfyApi extends EventTarget {
|
||||
* Stores a user data file for the current user
|
||||
* @param { string } file The name of the userdata file to save
|
||||
* @param { unknown } data The data to save to the file
|
||||
* @param { RequestInit & { stringify?: boolean, throwOnError?: boolean } } [options]
|
||||
* @returns { Promise<void> }
|
||||
* @param { RequestInit & { overwrite?: boolean, stringify?: boolean, throwOnError?: boolean } } [options]
|
||||
* @returns { Promise<Response> }
|
||||
*/
|
||||
async storeUserData(file, data, options = { stringify: true, throwOnError: true }) {
|
||||
const resp = await this.fetchApi(`/userdata/${encodeURIComponent(file)}`, {
|
||||
async storeUserData(file, data, options = { overwrite: true, stringify: true, throwOnError: true }) {
|
||||
const resp = await this.fetchApi(`/userdata/${encodeURIComponent(file)}?overwrite=${options?.overwrite}`, {
|
||||
method: "POST",
|
||||
body: options?.stringify ? JSON.stringify(data) : data,
|
||||
...options,
|
||||
});
|
||||
if (resp.status !== 200) {
|
||||
if (resp.status !== 200 && options?.throwOnError !== false) {
|
||||
throw new Error(`Error storing user data file '${file}': ${resp.status} ${(await resp).statusText}`);
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -40,6 +40,29 @@ export class ComfyApp {
|
||||
*/
|
||||
#processingQueue = false;
|
||||
|
||||
/** The name of the current workflow
|
||||
* @type { string | null }
|
||||
*/
|
||||
#currentWorkflow = null;
|
||||
|
||||
/**
|
||||
* Content Clipboard
|
||||
* @type {serialized node object}
|
||||
*/
|
||||
static clipspace = null;
|
||||
static clipspace_invalidate_handler = null;
|
||||
static open_maskeditor = null;
|
||||
static clipspace_return_node = null;
|
||||
|
||||
get currentWorkflow() {
|
||||
return this.#currentWorkflow;
|
||||
}
|
||||
|
||||
set currentWorkflow(value) {
|
||||
this.#currentWorkflow = value ?? null;
|
||||
api.dispatchEvent(new CustomEvent("workflowChanged", { detail: value ?? null }));
|
||||
}
|
||||
|
||||
/**
|
||||
* Content Clipboard
|
||||
* @type {serialized node object}
|
||||
@ -1503,7 +1526,8 @@ export class ComfyApp {
|
||||
const loadWorkflow = async (json) => {
|
||||
if (json) {
|
||||
const workflow = JSON.parse(json);
|
||||
await this.loadGraphData(workflow);
|
||||
const workflowName = localStorage.getItem("Comfy.LastWorkflow");
|
||||
await this.loadGraphData(workflow, true, workflowName);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
@ -1725,12 +1749,13 @@ export class ComfyApp {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* Populates the graph with the specified workflow data
|
||||
* @param {*} graphData A serialized graph object
|
||||
* @param { boolean } clean If the graph state, e.g. images, should be cleared
|
||||
* @param { string } name The name of the workflow
|
||||
*/
|
||||
async loadGraphData(graphData, clean = true, keepOld = false) {
|
||||
async loadGraphData(graphData, clean = true, name = null) {
|
||||
if (clean !== false) {
|
||||
this.clean();
|
||||
}
|
||||
@ -1749,6 +1774,9 @@ export class ComfyApp {
|
||||
graphData = structuredClone(graphData);
|
||||
}
|
||||
|
||||
this.currentWorkflow = name;
|
||||
localStorage.setItem("Comfy.LastWorkflow", name ?? "");
|
||||
|
||||
const missingNodeTypes = [];
|
||||
await this.#invokeExtensionsAsync("beforeConfigureGraph", graphData, missingNodeTypes);
|
||||
for (let n of graphData.nodes) {
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
import { $el } from "../../ui.js";
|
||||
import { api } from "../../api.js";
|
||||
import { addStylesheet, downloadBlob } from "../../utils.js";
|
||||
import { ComfyButton } from "../components/button.js";
|
||||
import { ComfyButtonGroup } from "../components/buttonGroup.js";
|
||||
@ -29,16 +30,16 @@ export class ComfyAppMenu {
|
||||
constructor(app) {
|
||||
this.app = app;
|
||||
|
||||
this.workflows = new ComfyWorkflowsMenu(app);
|
||||
const getSaveButton = (t) =>
|
||||
new ComfyButton({
|
||||
icon: "content-save",
|
||||
tooltip: "Save the current workflow",
|
||||
action: () => this.exportWorkflow("workflow", "workflow"),
|
||||
action: () => this.workflows.save(),
|
||||
content: t,
|
||||
});
|
||||
|
||||
this.logo = $el("h1.comfyui-logo.sm-hide", { title: "ComfyUI" }, "ComfyUI");
|
||||
this.workflows = new ComfyWorkflowsMenu(app);
|
||||
this.element = $el("nav.comfyui-menu", { parent: document.body }, [
|
||||
this.logo,
|
||||
this.workflows.element,
|
||||
@ -52,15 +53,7 @@ export class ComfyAppMenu {
|
||||
icon: "content-save-edit",
|
||||
content: "Save As",
|
||||
tooltip: "Save the current graph as a new workflow",
|
||||
action: () => {
|
||||
let filename = prompt("Save workflow as:", "workflow");
|
||||
if (!filename) return;
|
||||
if (!filename.toLowerCase().endsWith(".json")) {
|
||||
filename += ".json";
|
||||
}
|
||||
|
||||
this.exportWorkflow("workflow", "workflow");
|
||||
},
|
||||
action: () => this.workflows.save(true),
|
||||
}),
|
||||
new ComfyButton({
|
||||
icon: "download",
|
||||
|
||||
@ -250,7 +250,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
@media only screen and (max-width: 490px) {
|
||||
@media only screen and (max-width: 520px) {
|
||||
.comfyui-workflows-label {
|
||||
display: none;
|
||||
}
|
||||
@ -272,6 +272,7 @@
|
||||
/* Workflows */
|
||||
.comfyui-workflows-button {
|
||||
flex-direction: row-reverse;
|
||||
max-width: 200px;
|
||||
}
|
||||
|
||||
.comfyui-workflows-button.popup-open {
|
||||
@ -282,16 +283,34 @@
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.comfyui-workflows-button.unsaved .comfyui-workflows-label:after {
|
||||
content: "*";
|
||||
.comfyui-workflows-button>span {
|
||||
flex: auto;
|
||||
text-align: left;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.comfyui-workflows-button-inner {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 7px;
|
||||
}
|
||||
.comfyui-workflows-label {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
direction: rtl;
|
||||
flex: auto;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.comfyui-workflows-button.unsaved .comfyui-workflows-label {
|
||||
padding-left: 8px;
|
||||
}
|
||||
|
||||
.comfyui-workflows-button.unsaved .comfyui-workflows-label:after {
|
||||
content: "*";
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
}
|
||||
.comfyui-workflows-button-inner .mdi-graph::before {
|
||||
transform: rotate(-90deg);
|
||||
}
|
||||
@ -307,6 +326,16 @@
|
||||
border-bottom-left-radius: 4px;
|
||||
}
|
||||
|
||||
.comfyui-workflows-panel {
|
||||
min-height: 150px;
|
||||
}
|
||||
|
||||
.comfyui-workflows-panel .lds-ring {
|
||||
transform: translate(-50%);
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
top: 75px;
|
||||
}
|
||||
.comfyui-workflows-actions {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
@ -335,17 +364,21 @@
|
||||
.comfyui-workflows-tree {
|
||||
padding: 0;
|
||||
padding-top: 5px;
|
||||
margin-bottom: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.comfyui-workflows-tree:empty::after {
|
||||
content: "No saved workflows";
|
||||
display: block;
|
||||
text-align: center;
|
||||
}
|
||||
.comfyui-workflows-tree>ul {
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.comfyui-workflows-tree>ul ul {
|
||||
margin: 0;
|
||||
padding: 0 0 0 30px;
|
||||
padding: 0 0 0 25px;
|
||||
}
|
||||
|
||||
.comfyui-workflows-tree .closed>ul {
|
||||
|
||||
@ -5,6 +5,11 @@ import { prop } from "../../utils.js";
|
||||
import { $el } from "../../ui.js";
|
||||
import { api } from "../../api.js";
|
||||
import { ComfyPopup } from "../components/popup.js";
|
||||
import { createSpinner } from "../spinner.js";
|
||||
|
||||
function trimJsonExt(name) {
|
||||
return name.replace(/\.json$/, "");
|
||||
}
|
||||
|
||||
export class ComfyWorkflowsMenu {
|
||||
element = $el("div.comfyui-workflows");
|
||||
@ -18,13 +23,39 @@ export class ComfyWorkflowsMenu {
|
||||
}
|
||||
|
||||
constructor(app) {
|
||||
this.app = app;
|
||||
|
||||
let ignoreChange = false;
|
||||
let first = true;
|
||||
api.addEventListener("workflowChanged", ({ detail }) => {
|
||||
if (detail) {
|
||||
this.unsaved = first ? localStorage.getItem("Comfy.LastWorkflowUnsaved") === "true" : false;
|
||||
this.currentWorkflow = detail;
|
||||
} else {
|
||||
this.unsaved = true;
|
||||
this.currentWorkflow = "Unsaved workflow";
|
||||
}
|
||||
first = false;
|
||||
ignoreChange = true;
|
||||
setTimeout(() => (ignoreChange = false), 5);
|
||||
});
|
||||
|
||||
api.addEventListener("graphChanged", () => {
|
||||
if (ignoreChange) {
|
||||
ignoreChange = false;
|
||||
} else {
|
||||
this.unsaved = true;
|
||||
}
|
||||
});
|
||||
|
||||
const classList = {
|
||||
"comfyui-workflows-button": true,
|
||||
"comfyui-button": true,
|
||||
unsaved: true,
|
||||
unsaved: false,
|
||||
};
|
||||
this.workflowLabel = $el("span.comfyui-workflows-label", "Unsaved workflow");
|
||||
this.button = new ComfyButton({
|
||||
content: $el("div.comfyui-workflows-button-inner", [$el("i.mdi.mdi-graph"), $el("span.comfyui-workflows-label", "Unsaved workflow")]),
|
||||
content: $el("div.comfyui-workflows-button-inner", [$el("i.mdi.mdi-graph"), this.workflowLabel]),
|
||||
icon: "chevron-down",
|
||||
classList,
|
||||
});
|
||||
@ -41,11 +72,48 @@ export class ComfyWorkflowsMenu {
|
||||
this.unsaved = prop(this, "unsaved", classList.unsaved, (v) => {
|
||||
classList.unsaved = v;
|
||||
this.button.classList = classList;
|
||||
if (!first) {
|
||||
localStorage.setItem("Comfy.LastWorkflowUnsaved", v);
|
||||
}
|
||||
});
|
||||
this.currentWorkflow = prop(this, "currentWorkflow", "", () => {
|
||||
this.button.element.title = this.workflowLabel.textContent = trimJsonExt(this.currentWorkflow);
|
||||
});
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
this.popup.open = true;
|
||||
}, 500);
|
||||
save(saveAs) {
|
||||
if (!saveAs && this.app.currentWorkflow) {
|
||||
this.#saveWorkflow(this.app.currentWorkflow);
|
||||
} else {
|
||||
this.#saveWorkflow();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} [filename]
|
||||
*/
|
||||
async #saveWorkflow(filename) {
|
||||
let overwrite = true;
|
||||
if (!filename) {
|
||||
overwrite = false;
|
||||
filename = prompt("Save workflow as:", this.app.currentWorkflow || "workflow");
|
||||
if (!filename) return;
|
||||
if (!filename.toLowerCase().endsWith(".json")) {
|
||||
filename += ".json";
|
||||
}
|
||||
}
|
||||
|
||||
const p = await this.app.graphToPrompt();
|
||||
const json = JSON.stringify(p.workflow, null, 2);
|
||||
const res = await api.storeUserData("workflows/" + filename, json, { stringify: false, throwOnError: false, overwrite });
|
||||
if (res.status === 409) {
|
||||
if (!confirm(`Workflow '${filename}' already exists, do you want to overwrite it?`)) return;
|
||||
await api.storeUserData("workflows/" + filename, json, { stringify: false });
|
||||
}
|
||||
|
||||
this.unsaved = false;
|
||||
this.app.currentWorkflow = filename;
|
||||
localStorage.setItem("Comfy.LastWorkflow", filename);
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,10 +127,30 @@ export class ComfyWorkflowsContent {
|
||||
this.app = app;
|
||||
this.popup = popup;
|
||||
this.actions = $el("div.comfyui-workflows-actions", [
|
||||
new ComfyButton({ content: "New Workflow", icon: "plus" }).element,
|
||||
new ComfyButton({ content: "Load Default" }).element,
|
||||
new ComfyButton({
|
||||
content: "New Workflow",
|
||||
icon: "plus",
|
||||
action: () => {
|
||||
app.currentWorkflow = null;
|
||||
app.clean();
|
||||
app.graph.clear();
|
||||
popup.open = false;
|
||||
},
|
||||
}).element,
|
||||
new ComfyButton({
|
||||
content: "Load Default",
|
||||
action: () => {
|
||||
popup.open = false;
|
||||
app.loadGraphData();
|
||||
},
|
||||
}).element,
|
||||
]);
|
||||
this.load();
|
||||
|
||||
this.spinner = createSpinner();
|
||||
this.element.replaceChildren(this.actions, this.spinner);
|
||||
|
||||
this.popup.addEventListener("open", () => this.load());
|
||||
this.popup.addEventListener("close", () => this.element.replaceChildren(this.actions, this.spinner));
|
||||
}
|
||||
|
||||
async loadWorkflowsInfo() {
|
||||
@ -93,26 +181,29 @@ export class ComfyWorkflowsContent {
|
||||
|
||||
this.favoritesElement = $el(
|
||||
"div.comfyui-workflows-favorites",
|
||||
favorites.map((f) => {
|
||||
const entry = this.#fileLookup[f];
|
||||
const res = new FileNode(
|
||||
this,
|
||||
entry.path,
|
||||
entry.part,
|
||||
"div",
|
||||
(f) => {
|
||||
entry.isFavorite = f;
|
||||
entry.updateFavorite();
|
||||
this.updateFavoritesElement();
|
||||
},
|
||||
() => {
|
||||
res.element.remove();
|
||||
entry.remove();
|
||||
return false;
|
||||
}
|
||||
);
|
||||
return res.element;
|
||||
})
|
||||
favorites
|
||||
.map((f) => {
|
||||
const entry = this.#fileLookup[f];
|
||||
if (!entry) return null;
|
||||
const res = new FileNode(
|
||||
this,
|
||||
entry.path,
|
||||
entry.part,
|
||||
"div",
|
||||
(f) => {
|
||||
entry.isFavorite = f;
|
||||
entry.updateFavorite();
|
||||
this.updateFavoritesElement();
|
||||
},
|
||||
() => {
|
||||
res.element.remove();
|
||||
entry.remove();
|
||||
return false;
|
||||
}
|
||||
);
|
||||
return res.element;
|
||||
})
|
||||
.filter(Boolean)
|
||||
);
|
||||
|
||||
if (current) {
|
||||
@ -239,7 +330,7 @@ class FileNode {
|
||||
this.parent.favorites.delete(this.path);
|
||||
await this.parent.storeWorkflowsInfo();
|
||||
}
|
||||
// await api.deleteUserData(fileName);
|
||||
await api.deleteUserData(fileName);
|
||||
this.remove();
|
||||
} else {
|
||||
btn.icon = "delete-empty";
|
||||
@ -252,7 +343,7 @@ class FileNode {
|
||||
deleteButton.element.style.removeProperty("background");
|
||||
});
|
||||
|
||||
const loadWorkflow = async (e, insert) => {
|
||||
const loadWorkflow = async (e) => {
|
||||
e.stopImmediatePropagation();
|
||||
const resp = await api.getUserData(fileName);
|
||||
if (resp.status !== 200) {
|
||||
@ -260,16 +351,16 @@ class FileNode {
|
||||
return;
|
||||
}
|
||||
const data = await resp.json();
|
||||
// TODO: fix insert, LiteGraph has a dont clear parameter but it doesnt fix duplicate ids
|
||||
await this.parent.app.loadGraphData(data, !insert, insert);
|
||||
await this.parent.app.loadGraphData(data, true, this.path);
|
||||
this.parent.popup.open = false;
|
||||
};
|
||||
|
||||
const name = this.part.replace(/\.json$/, "");
|
||||
const name = trimJsonExt(this.part);
|
||||
this.element = $el(
|
||||
tagName + ".comfyui-workflows-tree-file",
|
||||
{
|
||||
onclick: (e) => loadWorkflow(e, false),
|
||||
onclick: (e) => loadWorkflow(e),
|
||||
title: this.path,
|
||||
},
|
||||
[
|
||||
this.nodeIcon,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user