refactor, better workflow instance management

This commit is contained in:
pythongosssss 2024-03-05 21:43:09 +00:00
parent 60ad4f9abc
commit 5a5634bd8e
7 changed files with 702 additions and 569 deletions

View File

@ -130,7 +130,7 @@ class UserManager():
split_path = request.rel_url.query.get('split', '').lower() == "true" split_path = request.rel_url.query.get('split', '').lower() == "true"
if split_path: if split_path:
results = [x.split(os.sep) for x in results] results = [[x] + x.split(os.sep) for x in results]
return web.json_response(results) return web.json_response(results)

View File

@ -450,7 +450,7 @@ class ComfyApi extends EventTarget {
* @param { string } dir The directory in which to list files * @param { string } dir The directory in which to list files
* @param { boolean } [recurse] If the listing should be recursive * @param { boolean } [recurse] If the listing should be recursive
* @param { true } [split] If the paths should be split based on the os path separator * @param { true } [split] If the paths should be split based on the os path separator
* @returns { Promise<string[][]>> } The list of split file paths * @returns { Promise<string[][]>> } The list of split file paths in the format [fullPath, ...splitPath]
*/ */
/** /**
* @overload * @overload

View File

@ -8,7 +8,7 @@ import { addDomClippingSetting } from "./domWidget.js";
import { createImageHost, calculateImageGrid } from "./ui/imagePreview.js"; import { createImageHost, calculateImageGrid } from "./ui/imagePreview.js";
import { ComfyAppMenu } from "./ui/menu/index.js"; import { ComfyAppMenu } from "./ui/menu/index.js";
import { getStorageValue, setStorageValue } from "./utils.js"; import { getStorageValue, setStorageValue } from "./utils.js";
import { ComfyWorkflow } from "./workflows.js"; import { ComfyWorkflowManager } from "./workflows.js";
export const ANIM_PREVIEW_WIDGET = "$$comfy_animation_preview"; export const ANIM_PREVIEW_WIDGET = "$$comfy_animation_preview";
function sanitizeNodeName(string) { function sanitizeNodeName(string) {
@ -54,8 +54,8 @@ export class ComfyApp {
constructor() { constructor() {
this.ui = new ComfyUI(this); this.ui = new ComfyUI(this);
this.logging = new ComfyLogging(this); this.logging = new ComfyLogging(this);
ComfyWorkflow.init(this); this.workflowManager = new ComfyWorkflowManager(this);
new ComfyAppMenu(this); this.menu = new ComfyAppMenu(this);
/** /**
* List of extensions that are registered with the app * List of extensions that are registered with the app
@ -1454,7 +1454,7 @@ export class ComfyApp {
*/ */
async setup() { async setup() {
await this.#setUser(); await this.#setUser();
await this.ui.settings.load(); await Promise.all([this.workflowManager.loadWorkflows(), this.ui.settings.load()]);
await this.#loadExtensions(); await this.#loadExtensions();
// Create and mount the LiteGraph in the DOM // Create and mount the LiteGraph in the DOM
@ -1506,7 +1506,7 @@ export class ComfyApp {
const loadWorkflow = async (json) => { const loadWorkflow = async (json) => {
if (json) { if (json) {
const workflow = JSON.parse(json); const workflow = JSON.parse(json);
const workflowName = getStorageValue("Comfy.LastWorkflow"); const workflowName = getStorageValue("Comfy.PreviousWorkflow");
await this.loadGraphData(workflow, true, workflowName); await this.loadGraphData(workflow, true, workflowName);
return true; return true;
} }
@ -1729,11 +1729,11 @@ export class ComfyApp {
}); });
} }
/** /**
* Populates the graph with the specified workflow data * Populates the graph with the specified workflow data
* @param {*} graphData A serialized graph object * @param {*} graphData A serialized graph object
* @param { boolean } clean If the graph state, e.g. images, should be cleared * @param { boolean } clean If the graph state, e.g. images, should be cleared
* @param { import("./workflows.js").ComfyWorkflow | string | null } name The workflow * @param { import("./workflows.js").ComfyWorkflow | null } name The workflow
*/ */
async loadGraphData(graphData, clean = true, workflow = null) { async loadGraphData(graphData, clean = true, workflow = null) {
if (clean !== false) { if (clean !== false) {
@ -1754,10 +1754,15 @@ export class ComfyApp {
graphData = structuredClone(graphData); graphData = structuredClone(graphData);
} }
if (workflow) { try {
ComfyWorkflow.changeWorkflow(workflow); this.workflowManager.activeWorkflow?.changeTracker?.storeViewport()
} else { } catch (error) {
ComfyWorkflow.newWorkflow(); }
try {
this.workflowManager.setWorkflow(workflow);
} catch (error) {
console.error(error);
} }
const missingNodeTypes = []; const missingNodeTypes = [];
@ -1777,6 +1782,11 @@ export class ComfyApp {
try { try {
this.graph.configure(graphData); this.graph.configure(graphData);
try {
this.workflowManager.activeWorkflow?.track()
} catch (error) {
}
} catch (error) { } catch (error) {
let errorHint = []; let errorHint = [];
// Try extracting filename to see if it was caused by an extension script // Try extracting filename to see if it was caused by an extension script

View File

@ -0,0 +1,204 @@
// @ts-check
import { api } from "./api.js";
import { clone } from "./utils.js";
export class ChangeTracker {
static MAX_HISTORY = 50;
undo = [];
redo = [];
activeState = null;
isOurLoad = false;
/** @type { import("./workflows").ComfyWorkflow } */
workflow;
ds;
get app() {
return this.workflow.manager.app;
}
constructor(workflow) {
this.workflow = workflow;
}
storeViewport() {
this.ds = { scale: this.app.canvas.ds.scale, offset: [...this.app.canvas.ds.offset] };
}
restoreViewport() {
if (!this.ds) return;
this.app.canvas.ds.scale = this.ds.scale;
this.app.canvas.ds.offset = this.ds.offset;
}
checkState() {
const currentState = this.app.graph.serialize();
if (!this.activeState) {
this.activeState = clone(currentState);
return;
}
if (!ChangeTracker.graphEqual(this.activeState, currentState)) {
this.undo.push(this.activeState);
if (this.undo.length > ChangeTracker.MAX_HISTORY) {
this.undo.shift();
}
this.activeState = clone(currentState);
this.redo.length = 0;
this.workflow.unsaved = true;
api.dispatchEvent(new CustomEvent("graphChanged", { detail: this.activeState }));
}
}
async updateState(source, target) {
const prevState = source.pop();
if (prevState) {
target.push(this.activeState);
this.isOurLoad = true;
await this.app.loadGraphData(prevState, false, this.workflow);
this.activeState = prevState;
}
}
async undoRedo(e) {
if (e.ctrlKey || e.metaKey) {
if (e.key === "y") {
this.updateState(this.redo, this.undo);
return true;
} else if (e.key === "z") {
this.updateState(this.undo, this.redo);
return true;
}
}
}
/** @param { import("./app.js").ComfyApp } app */
static init(app) {
const changeTracker = () => app.workflowManager.activeWorkflow.changeTracker;
const loadGraphData = app.loadGraphData;
app.loadGraphData = async function () {
const v = await loadGraphData.apply(this, arguments);
const ct = changeTracker();
if (ct.isOurLoad) {
ct.isOurLoad = false;
} else {
ct.checkState();
}
return v;
};
let keyIgnored = false;
window.addEventListener(
"keydown",
(e) => {
requestAnimationFrame(async () => {
let activeEl;
// If we are auto queue in change mode then we do want to trigger on inputs
if (!app.ui.autoQueueEnabled || app.ui.autoQueueMode === "instant") {
activeEl = document.activeElement;
if (activeEl?.tagName === "INPUT" || activeEl?.["type"] === "textarea") {
// Ignore events on inputs, they have their native history
return;
}
}
keyIgnored = e.key === "Control" || e.key === "Shift" || e.key === "Alt" || e.key === "Meta";
if (keyIgnored) return;
// Check if this is a ctrl+z ctrl+y
if (await changeTracker().undoRedo(e)) return;
// If our active element is some type of input then handle changes after they're done
if (ChangeTracker.bindInput(activeEl)) return;
changeTracker().checkState();
});
},
true
);
window.addEventListener("keyup", (e) => {
if (keyIgnored) {
keyIgnored = false;
changeTracker().checkState();
}
});
// Handle clicking DOM elements (e.g. widgets)
window.addEventListener("mouseup", () => {
changeTracker().checkState();
});
// Handle prompt queue event for dynamic widget changes
api.addEventListener("promptQueued", () => {
changeTracker().checkState();
});
// Handle litegraph clicks
const processMouseUp = LGraphCanvas.prototype.processMouseUp;
LGraphCanvas.prototype.processMouseUp = function (e) {
const v = processMouseUp.apply(this, arguments);
changeTracker().checkState();
return v;
};
const processMouseDown = LGraphCanvas.prototype.processMouseDown;
LGraphCanvas.prototype.processMouseDown = function (e) {
const v = processMouseDown.apply(this, arguments);
changeTracker().checkState();
return v;
};
// Handle litegraph context menu for COMBO widgets
const close = LiteGraph.ContextMenu.prototype.close;
LiteGraph.ContextMenu.prototype.close = function (e) {
const v = close.apply(this, arguments);
changeTracker().checkState();
return v;
};
}
static bindInput(app, activeEl) {
if (activeEl && activeEl.tagName !== "CANVAS" && activeEl.tagName !== "BODY") {
for (const evt of ["change", "input", "blur"]) {
if (`on${evt}` in activeEl) {
const listener = () => {
app.workflowManager.activeWorkflow.changeTracker.checkState();
activeEl.removeEventListener(evt, listener);
};
activeEl.addEventListener(evt, listener);
return true;
}
}
}
}
static graphEqual(a, b, root = true) {
if (a === b) return true;
if (typeof a == "object" && a && typeof b == "object" && b) {
const keys = Object.getOwnPropertyNames(a);
if (keys.length != Object.getOwnPropertyNames(b).length) {
return false;
}
for (const key of keys) {
let av = a[key];
let bv = b[key];
if (root && key === "nodes") {
// Nodes need to be sorted as the order changes when selecting nodes
av = [...av].sort((a, b) => a.id - b.id);
bv = [...bv].sort((a, b) => a.id - b.id);
}
if (!ChangeTracker.graphEqual(av, bv, false)) {
return false;
}
}
return true;
}
return false;
}
}

View File

@ -292,6 +292,7 @@
display: flex; display: flex;
align-items: center; align-items: center;
gap: 7px; gap: 7px;
width: 130px;
} }
.comfyui-workflows-label { .comfyui-workflows-label {
overflow: hidden; overflow: hidden;
@ -299,7 +300,6 @@
direction: rtl; direction: rtl;
flex: auto; flex: auto;
position: relative; position: relative;
width: 130px;
} }
.comfyui-workflows-button.unsaved .comfyui-workflows-label { .comfyui-workflows-button.unsaved .comfyui-workflows-label {

View File

@ -8,27 +8,6 @@ import { ComfyPopup } from "../components/popup.js";
import { createSpinner } from "../spinner.js"; import { createSpinner } from "../spinner.js";
import { ComfyWorkflow } from "../../workflows.js"; import { ComfyWorkflow } from "../../workflows.js";
function reactive(html) {
}
reactive(`
<div>
<header>
<comfy-button icon="plus" text="New Workflow" onclick="newWorkflow" />
</header>
<section>
<h3>Open Workflows</h3>
<div $if="hasWorkflows">
</section>
</div>
`)
function trimJsonExt(name) {
return name.replace(/\.json$/, "");
}
export class ComfyWorkflowsMenu { export class ComfyWorkflowsMenu {
element = $el("div.comfyui-workflows"); element = $el("div.comfyui-workflows");
@ -40,37 +19,27 @@ export class ComfyWorkflowsMenu {
this.popup.open = open; this.popup.open = open;
} }
/**
* @param {import("../../app.js").ComfyApp} app
*/
constructor(app) { constructor(app) {
this.app = app; this.app = app;
let ignoreChange = false; app.workflowManager.addEventListener("change", () => {
let first = true; const active = app.workflowManager.activeWorkflow;
api.addEventListener("workflowsChanged", ({ detail }) => { this.button.tooltip = active.path;
this.currentWorkflow = ComfyWorkflow.activeWorkflow.displayName; this.workflowLabel.textContent = active.name;
console.log(detail); this.unsaved = active.unsaved;
// if (detail) {
// this.unsaved = first ? getStorageValue("Comfy.LastWorkflowUnsaved") === "true" : false;
// } else {
// this.unsaved = true;
// }
// first = false;
// ignoreChange = true;
// setTimeout(() => (ignoreChange = false), 5);
// this.content.updateOpenElement();
}); });
api.addEventListener("graphChanged", () => { api.addEventListener("graphChanged", () => {
if (ignoreChange) { this.unsaved = true;
ignoreChange = false;
} else {
this.unsaved = true;
}
}); });
const classList = { const classList = {
"comfyui-workflows-button": true, "comfyui-workflows-button": true,
"comfyui-button": true, "comfyui-button": true,
unsaved: false, unsaved: getStorageValue("Comfy.PreviousWorkflowUnsaved") === "true",
}; };
this.workflowLabel = $el("span.comfyui-workflows-label", "Unsaved workflow"); this.workflowLabel = $el("span.comfyui-workflows-label", "Unsaved workflow");
this.button = new ComfyButton({ this.button = new ComfyButton({
@ -92,56 +61,18 @@ export class ComfyWorkflowsMenu {
this.unsaved = prop(this, "unsaved", classList.unsaved, (v) => { this.unsaved = prop(this, "unsaved", classList.unsaved, (v) => {
classList.unsaved = v; classList.unsaved = v;
this.button.classList = classList; this.button.classList = classList;
if (!first) { setStorageValue("Comfy.PreviousWorkflowUnsaved", v);
setStorageValue("Comfy.LastWorkflowUnsaved", v);
}
}); });
this.currentWorkflow = prop(this, "currentWorkflow", "", () => {
this.button.element.title = this.workflowLabel.textContent = trimJsonExt(this.currentWorkflow);
});
}
save(saveAs) {
if (!saveAs && this.app.currentWorkflow) {
this.#saveWorkflow(trimJsonExt(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;
} }
} }
export class ComfyWorkflowsContent { export class ComfyWorkflowsContent {
#fileLookup = {};
element = $el("div.comfyui-workflows-panel"); element = $el("div.comfyui-workflows-panel");
/** @type {Set<string>} */
favorites;
/**
* @param {import("../../app.js").ComfyApp} app
* @param {ComfyPopup} popup
*/
constructor(app, popup) { constructor(app, popup) {
this.app = app; this.app = app;
this.popup = popup; this.popup = popup;
@ -150,12 +81,19 @@ export class ComfyWorkflowsContent {
content: "New Workflow", content: "New Workflow",
icon: "plus", icon: "plus",
action: () => { action: () => {
app.currentWorkflow = null; app.workflowManager.setWorkflow(null);
app.clean(); app.clean();
app.graph.clear(); app.graph.clear();
popup.open = false; popup.open = false;
}, },
}).element, }).element,
new ComfyButton({
content: "Load JSON",
action: () => {
popup.open = false;
app.loadGraphData();
},
}).element,
new ComfyButton({ new ComfyButton({
content: "Load Default", content: "Load Default",
action: () => { action: () => {
@ -172,107 +110,80 @@ export class ComfyWorkflowsContent {
this.popup.addEventListener("close", () => this.element.replaceChildren(this.actions, this.spinner)); this.popup.addEventListener("close", () => this.element.replaceChildren(this.actions, this.spinner));
} }
async loadWorkflowsInfo() {
const resp = await api.getUserData("workflows/.index.json");
let info;
if (resp.status === 200) {
info = await resp.json();
}
this.favorites = new Set(info?.favorites ?? []);
}
async storeWorkflowsInfo() {
await api.storeUserData("workflows/.index.json", {
favorites: [...this.favorites.values()],
});
}
async load() { async load() {
const tree = await this.createTreeElement(); this.updateTree();
this.updateFavoritesElement(); this.updateFavorites();
this.updateOpenElement(); this.updateOpenWorkflows();
this.element.replaceChildren(this.actions, this.openElement, this.favoritesElement, tree); this.element.replaceChildren(this.actions, this.openElement, this.favoritesElement, this.treeElement);
} }
updateOpenElement() { updateOpenWorkflows() {
const current = this.openElement; const current = this.openElement;
this.openElement = $el( this.openElement = $el("div.comfyui-workflows-open", [
"div.comfyui-workflows-open", $el("h3", "Open Workflows"),
[$el("h3", "Open Workflows"), ...ComfyWorkflow.openWorkflows.map((w) => { ...this.app.workflowManager.openWorkflows.map((w) => {
const res = new FileNode( const wrapper = new WorkflowElement(w, {
this, buttons: [],
w.displayName, });
w.displayName,
"div",
(f) => {
// entry.isFavorite = f;
// entry.updateFavorite();
// this.updateFavoritesElement();
},
() => {
// res.element.remove();
// entry.remove();
return false;
}
);
return res.element;
})]
);
if (current) { return wrapper.element;
current.replaceWith(this.openElement); }),
} ]);
current?.replaceWith(this.openElement);
} }
updateFavoritesElement() { updateFavorites() {
const favorites = [...this.favorites.values()];
const current = this.favoritesElement; const current = this.favoritesElement;
const favorites = [...this.app.workflowManager.workflows.filter((w) => w.isFavorite)];
this.favoritesElement = $el( this.favoritesElement = $el(
"div.comfyui-workflows-favorites", "div.comfyui-workflows-favorites",
favorites favorites
.map((f) => { .map((f) => {
const entry = this.#fileLookup[f]; return $el("span", f.path);
if (!entry) return null; // const entry = this.#fileLookup[f];
const res = new FileNode( // if (!entry) return null;
this, // const res = new FileNode(
entry.path, // this,
entry.part, // entry.path,
"div", // entry.part,
(f) => { // "div",
entry.isFavorite = f; // (f) => {
entry.updateFavorite(); // entry.isFavorite = f;
this.updateFavoritesElement(); // entry.updateFavorite();
}, // this.updateFavoritesElement();
() => { // },
res.element.remove(); // () => {
entry.remove(); // res.element.remove();
return false; // entry.remove();
} // return false;
); // }
return res.element; // );
// return res.element;
}) })
.filter(Boolean) .filter(Boolean)
); );
if (current) { current?.replaceWith(this.favoritesElement);
current.replaceWith(this.favoritesElement);
}
} }
async createTreeElement() { updateTree() {
await this.loadWorkflowsInfo(); const current = this.treeElement;
const workflows = await api.listUserData("workflows", true, true); const flat = {};
const nodes = {}; const nodes = {};
const rootNode = $el("ul.comfyui-workflows-tree");
for (const pathParts of workflows) { this.treeElement = $el("ul.comfyui-workflows-tree");
for (const workflow of this.app.workflowManager.workflows) {
if (!workflow.pathParts) continue;
let currentPath = ""; let currentPath = "";
let currentRoot = rootNode; let currentRoot = this.treeElement;
for (let i = 0; i < pathParts.length; i++) { for (let i = 0; i < workflow.pathParts.length; i++) {
const part = pathParts[i]; const part = workflow.pathParts[i];
currentPath += (currentPath ? "\\" : "") + part; currentPath += (currentPath ? "\\" : "") + part;
let parentNode = nodes[currentPath]; let parentNode = nodes[currentPath];
@ -289,12 +200,11 @@ export class ComfyWorkflowsContent {
currentRoot.append(parentNode); currentRoot.append(parentNode);
// Create a node for the current part and an inner UL for its children if it isnt a leaf node // Create a node for the current part and an inner UL for its children if it isnt a leaf node
const leaf = i === pathParts.length - 1; const leaf = i === workflow.pathParts.length - 1;
let nodeElement; let nodeElement;
if (leaf) { if (leaf) {
const fileNode = new FileNode(this, currentPath, part); const fileNode = new WorkflowElement(workflow, { buttons: [] });
nodeElement = fileNode.element; nodeElement = fileNode.element;
this.#fileLookup[currentPath] = fileNode;
} else { } else {
nodeElement = $el("li", [$el("i.mdi.mdi-18px.mdi-folder"), $el("span", part)]); nodeElement = $el("li", [$el("i.mdi.mdi-18px.mdi-folder"), $el("span", part)]);
} }
@ -306,173 +216,192 @@ export class ComfyWorkflowsContent {
} }
} }
return rootNode; current?.replaceWith(this.treeElement);
} }
} }
class FileNode { class WorkflowElement {
constructor(parent, path, part, tagName = "li", onFavorite = null, onRemove = null) { /**
this.parent = parent; * @param { ComfyWorkflow } workflow
this.path = path; */
this.part = part; constructor(workflow, { tagName = "li", buttons }) {
this.createFileElement(tagName); this.workflow = workflow;
this.onFavorite = onFavorite; this.buttons = buttons;
this.onRemove = onRemove;
}
updateFavorite() {
if (this.onFavorite?.(this.isFavorite) === false) return;
if (this.isFavorite) {
this.parent.favorites.add(this.path);
this.favoriteButton.icon = "star";
this.favoriteButton.tooltip = "Unfavorite this workflow";
this.nodeIcon.classList.remove("mdi-file-outline");
this.nodeIcon.classList.add("mdi-star");
} else {
this.parent.favorites.delete(this.path);
this.favoriteButton.icon = "star-outline";
this.favoriteButton.tooltip = "Favorite this workflow";
this.nodeIcon.classList.add("mdi-file-outline");
this.nodeIcon.classList.remove("mdi-star");
}
}
remove() {
if (this.onRemove?.() === false) return;
const folderNode = this.element.parentElement.parentElement;
if (folderNode.querySelectorAll("ul").length === 1) {
folderNode.remove();
} else {
this.element.parentElement.remove();
}
}
createFileElement(tagName) {
const fileName = `workflows\\${this.path}`;
this.isFavorite = this.parent.favorites.has(this.path);
this.nodeIcon = $el("i.mdi.mdi-18px");
this.favoriteButton = new ComfyButton({
icon: this.isFavorite ? "star" : "star-outline",
classList: "comfyui-button comfyui-workflows-file-action comfyui-workflows-file-action-favorite",
iconSize: 18,
action: async (e) => {
e.stopImmediatePropagation();
this.isFavorite = !this.isFavorite;
this.updateFavorite();
this.parent.updateFavoritesElement();
await this.parent.storeWorkflowsInfo();
},
});
const deleteButton = new ComfyButton({
icon: "delete",
tooltip: "Delete this workflow",
classList: "comfyui-button comfyui-workflows-file-action",
iconSize: 18,
action: async (e, btn) => {
e.stopImmediatePropagation();
if (btn.icon === "delete-empty") {
btn.enabled = false;
if (this.isFavorite) {
this.parent.favorites.delete(this.path);
await this.parent.storeWorkflowsInfo();
}
if (this.parent.app.currentWorkflow === this.path) {
this.parent.app.currentWorkflow = null;
}
await api.deleteUserData(fileName);
this.remove();
} else {
btn.icon = "delete-empty";
btn.element.style.background = "red";
}
},
});
deleteButton.element.addEventListener("mouseleave", () => {
deleteButton.icon = "delete";
deleteButton.element.style.removeProperty("background");
});
const getWorkflow = async (e) => {
e.stopImmediatePropagation();
const resp = await api.getUserData(fileName);
if (resp.status !== 200) {
alert(`Error loading user data file '${fileName}': ${resp.status} ${resp.statusText}`);
return;
}
return await resp.json();
};
const loadWorkflow = async (e) => {
const data = await getWorkflow(e);
await this.parent.app.loadGraphData(data, true, this.path);
this.parent.popup.open = false;
};
const insertWorkflow = async (e) => {
const data = await getWorkflow(e);
const old = localStorage.getItem("litegrapheditor_clipboard");
const graph = new LGraph(data);
const canvas = new LGraphCanvas(null, graph, { skip_events: true, skip_render: true });
canvas.selectNodes();
canvas.copyToClipboard();
this.parent.app.canvas.pasteFromClipboard();
localStorage.setItem("litegrapheditor_clipboard", old);
};
const name = trimJsonExt(this.part);
this.element = $el( this.element = $el(
tagName + ".comfyui-workflows-tree-file", tagName + ".comfyui-workflows-tree-file",
{ {
onclick: loadWorkflow, onclick: workflow.load,
title: this.path, title: this.workflow.path,
}, },
[ [$el("span", workflow.name)]
this.nodeIcon,
$el("span", name),
new ComfyButton({
icon: "file-move-outline",
tooltip: "Insert this workflow into the current view",
classList: "comfyui-button comfyui-workflows-file-action",
iconSize: 18,
action: insertWorkflow,
}).element,
new ComfyButton({
icon: "pencil",
tooltip: "Rename this workflow",
classList: "comfyui-button comfyui-workflows-file-action",
iconSize: 18,
action: async (e) => {
e.stopImmediatePropagation();
const newName = prompt("Enter new name", trimJsonExt(this.path));
if (newName) {
// Rename file
try {
const res = await api.moveUserData("workflows/" + this.path, "workflows/" + newName);
if (res.status === 409) {
if (!confirm(`Workflow '${newName}' already exists, do you want to overwrite it?`)) return;
await api.moveUserData("workflows/" + this.path, "workflows/" + newName, { overwrite: true });
}
if (this.parent.app.currentWorkflow === this.path) {
this.parent.app.currentWorkflow = newName;
}
await this.parent.load();
} catch (error) {
alert(error.message ?? error);
}
}
},
}).element,
this.favoriteButton.element,
deleteButton.element,
]
); );
this.updateFavorite();
} }
} }
// class FileNode {
// constructor(parent, path, part, tagName = "li", onFavorite = null, onRemove = null) {
// this.parent = parent;
// this.path = path;
// this.part = part;
// this.createFileElement(tagName);
// this.onFavorite = onFavorite;
// this.onRemove = onRemove;
// }
// updateFavorite() {
// if (this.onFavorite?.(this.isFavorite) === false) return;
// if (this.isFavorite) {
// this.parent.favorites.add(this.path);
// this.favoriteButton.icon = "star";
// this.favoriteButton.tooltip = "Unfavorite this workflow";
// this.nodeIcon.classList.remove("mdi-file-outline");
// this.nodeIcon.classList.add("mdi-star");
// } else {
// this.parent.favorites.delete(this.path);
// this.favoriteButton.icon = "star-outline";
// this.favoriteButton.tooltip = "Favorite this workflow";
// this.nodeIcon.classList.add("mdi-file-outline");
// this.nodeIcon.classList.remove("mdi-star");
// }
// }
// remove() {
// if (this.onRemove?.() === false) return;
// const folderNode = this.element.parentElement.parentElement;
// if (folderNode.querySelectorAll("ul").length === 1) {
// folderNode.remove();
// } else {
// this.element.parentElement.remove();
// }
// }
// createFileElement(tagName) {
// const fileName = `workflows\\${this.path}`;
// this.isFavorite = this.parent.favorites.has(this.path);
// this.nodeIcon = $el("i.mdi.mdi-18px");
// this.favoriteButton = new ComfyButton({
// icon: this.isFavorite ? "star" : "star-outline",
// classList: "comfyui-button comfyui-workflows-file-action comfyui-workflows-file-action-favorite",
// iconSize: 18,
// action: async (e) => {
// e.stopImmediatePropagation();
// this.isFavorite = !this.isFavorite;
// this.updateFavorite();
// this.parent.updateFavoritesElement();
// await this.parent.storeWorkflowsInfo();
// },
// });
// const deleteButton = new ComfyButton({
// icon: "delete",
// tooltip: "Delete this workflow",
// classList: "comfyui-button comfyui-workflows-file-action",
// iconSize: 18,
// action: async (e, btn) => {
// e.stopImmediatePropagation();
// if (btn.icon === "delete-empty") {
// btn.enabled = false;
// if (this.isFavorite) {
// this.parent.favorites.delete(this.path);
// await this.parent.storeWorkflowsInfo();
// }
// if (this.parent.app.currentWorkflow === this.path) {
// this.parent.app.currentWorkflow = null;
// }
// await api.deleteUserData(fileName);
// this.remove();
// } else {
// btn.icon = "delete-empty";
// btn.element.style.background = "red";
// }
// },
// });
// deleteButton.element.addEventListener("mouseleave", () => {
// deleteButton.icon = "delete";
// deleteButton.element.style.removeProperty("background");
// });
// const getWorkflow = async (e) => {
// e.stopImmediatePropagation();
// const resp = await api.getUserData(fileName);
// if (resp.status !== 200) {
// alert(`Error loading user data file '${fileName}': ${resp.status} ${resp.statusText}`);
// return;
// }
// return await resp.json();
// };
// const loadWorkflow = async (e) => {
// const data = await getWorkflow(e);
// await this.parent.app.loadGraphData(data, true, this.path);
// this.parent.popup.open = false;
// };
// const insertWorkflow = async (e) => {
// const data = await getWorkflow(e);
// const old = localStorage.getItem("litegrapheditor_clipboard");
// const graph = new LGraph(data);
// const canvas = new LGraphCanvas(null, graph, { skip_events: true, skip_render: true });
// canvas.selectNodes();
// canvas.copyToClipboard();
// this.parent.app.canvas.pasteFromClipboard();
// localStorage.setItem("litegrapheditor_clipboard", old);
// };
// const name = trimJsonExt(this.part);
// this.element = $el(
// tagName + ".comfyui-workflows-tree-file",
// {
// onclick: loadWorkflow,
// title: this.path,
// },
// [
// this.nodeIcon,
// $el("span", name),
// new ComfyButton({
// icon: "file-move-outline",
// tooltip: "Insert this workflow into the current view",
// classList: "comfyui-button comfyui-workflows-file-action",
// iconSize: 18,
// action: insertWorkflow,
// }).element,
// new ComfyButton({
// icon: "pencil",
// tooltip: "Rename this workflow",
// classList: "comfyui-button comfyui-workflows-file-action",
// iconSize: 18,
// action: async (e) => {
// e.stopImmediatePropagation();
// const newName = prompt("Enter new name", trimJsonExt(this.path));
// if (newName) {
// // Rename file
// try {
// const res = await api.moveUserData("workflows/" + this.path, "workflows/" + newName);
// if (res.status === 409) {
// if (!confirm(`Workflow '${newName}' already exists, do you want to overwrite it?`)) return;
// await api.moveUserData("workflows/" + this.path, "workflows/" + newName, { overwrite: true });
// }
// if (this.parent.app.currentWorkflow === this.path) {
// this.parent.app.currentWorkflow = newName;
// }
// await this.parent.load();
// } catch (error) {
// alert(error.message ?? error);
// }
// }
// },
// }).element,
// this.favoriteButton.element,
// deleteButton.element,
// ]
// );
// this.updateFavorite();
// }
// }

View File

@ -1,283 +1,273 @@
// @ts-check // @ts-check
import { api } from "./api.js"; import { api } from "./api.js";
import { clone } from "./utils.js"; import { ChangeTracker } from "./changeTracker.js";
import { getStorageValue, setStorageValue } from "./utils.js";
export class ComfyWorkflow { export class ComfyWorkflowManager extends EventTarget {
/** @type { Array<ComfyWorkflow> } */ #unsavedCount = 0;
static openWorkflows = [];
/** @type {import("./app.js").ComfyApp} */
static app;
static unsavedCount = 0;
unsaved = false; /** @type {Array<ComfyWorkflow>} */
/** @type { string | null } */ workflows = [];
name = null; /** @type {Array<ComfyWorkflow>} */
/** @type { ChangeTracker } */ openWorkflows = [];
changeTracker;
/** @type { string | null } */
tempName = null;
get displayName() { get activeWorkflow() {
return this.name ?? this.tempName;
}
static get activeWorkflow() {
return this.openWorkflows[0]; return this.openWorkflows[0];
} }
static get changeTracker() {
return this.activeWorkflow.changeTracker;
}
/**
* @param {string} [name]
*/
constructor(name) {
this.name = name;
if (!name) {
this.tempName = "Unsaved workflow";
if (ComfyWorkflow.unsavedCount++) {
this.tempName += ` (${ComfyWorkflow.unsavedCount})`;
}
}
this.changeTracker = new ChangeTracker(this);
}
/** /**
* @param {import("./app.js").ComfyApp} app * @param {import("./app.js").ComfyApp} app
*/ */
static init(app) { constructor(app) {
ComfyWorkflow.app = app; super();
this.app = app;
ChangeTracker.init(app); ChangeTracker.init(app);
} }
/** async loadWorkflows() {
* try {
* @param { Partial<ComfyWorkflow> } opts let favorites;
* @returns const resp = await api.getUserData("workflows/.index.json");
*/ let info;
static newWorkflow(opts = {}) { if (resp.status === 200) {
const workflow = new ComfyWorkflow(opts?.name); info = await resp.json();
Object.assign(workflow, opts); favorites = new Set(info?.favorites ?? []);
ComfyWorkflow.openWorkflows.unshift(workflow);
ComfyWorkflow.#workflowsChanged();
return workflow;
}
/**
* @param { ComfyWorkflow | string } workflow
*/
static changeWorkflow(workflow) {
let index;
if (typeof workflow === "string") {
index = this.openWorkflows.findIndex((w) => w.name === workflow);
if (index === -1) {
const r = ComfyWorkflow.newWorkflow({ name: workflow });
return r;
} else { } else {
workflow = this.openWorkflows[index]; favorites = new Set();
} }
} else {
index = this.openWorkflows.indexOf(workflow); const workflows = (await api.listUserData("workflows", true, true)).map((w) => {
return new ComfyWorkflow(this, w[0], w.slice(1), favorites.has(w[0]));
});
this.workflows = workflows;
} catch (error) {
alert("Error loading workflows: " + (error.message ?? error));
this.workflows = [];
} }
if (index !== -1) { }
this.openWorkflows.splice(index, 1);
} async saveWorkflowMetadata() {
this.openWorkflows.unshift(workflow); await api.storeUserData("workflows/.index.json", {
this.#workflowsChanged(); favorites: [...this.workflows.filter((w) => w.isFavorite).map((w) => w.name)],
return workflow; });
} }
/** /**
* @param { ComfyWorkflow } workflow * @param {string | ComfyWorkflow | null} workflow
*/ */
static closeWorkflow(workflow) { setWorkflow(workflow) {
if (workflow && typeof workflow === "string") {
// Selected by path, i.e. on reload of last workflow
const found = this.workflows.find((w) => w.path === workflow);
if (found) {
workflow = found;
workflow.unsaved = !workflow || (getStorageValue("Comfy.PreviousWorkflowUnsaved") === "true");
}
}
if (!(workflow instanceof ComfyWorkflow)) {
// Still not found, either reloading a deleted workflow or blank
workflow = new ComfyWorkflow(this, workflow || "Unsaved Workflow" + (this.#unsavedCount++ ? ` (${this.#unsavedCount})` : ""));
}
const index = this.openWorkflows.indexOf(workflow); const index = this.openWorkflows.indexOf(workflow);
if (index !== -1) { if (index !== -1) {
this.openWorkflows.splice(index, 1); // Swapping to an open workflow
this.openWorkflows.unshift(this.openWorkflows.splice(index, 1)[0]);
} else {
// Opening a new workflow
this.openWorkflows.unshift(workflow);
} }
ComfyWorkflow.#workflowsChanged();
}
static #workflowsChanged() { setStorageValue("Comfy.PreviousWorkflow", this.activeWorkflow.path ?? "");
console.log("workflowsChanged", this.activeWorkflow?.name, this.openWorkflows.length); this.dispatchEvent(new CustomEvent("change"));
api.dispatchEvent(new CustomEvent("workflowsChanged", { detail: this.activeWorkflow }));
} }
} }
export class ChangeTracker { export class ComfyWorkflow {
static MAX_HISTORY = 50; #name;
#path;
#pathParts;
#isFavorite = false;
/** @type {ChangeTracker | null} */
changeTracker = null;
unsaved = false;
undo = []; get name() {
redo = []; return this.#name;
activeState = null;
isOurLoad = false;
/** @type { ComfyWorkflow } */
workflow;
constructor(workflow) {
this.workflow = workflow;
} }
checkState() { get path() {
const currentState = ComfyWorkflow.app.graph.serialize(); return this.#path;
if (!ChangeTracker.graphEqual(this.activeState, currentState)) { }
this.undo.push(this.activeState);
if (this.undo.length > ChangeTracker.MAX_HISTORY) { get pathParts() {
this.undo.shift(); return this.#pathParts;
} }
this.activeState = clone(currentState);
this.redo.length = 0; get isFavorite() {
this.workflow.unsaved = true; return this.#isFavorite;
api.dispatchEvent(new CustomEvent("graphChanged", { detail: this.activeState })); }
get isOpen() {
return !!this.changeTracker;
}
/**
* @overload
* @param {ComfyWorkflowManager} manager
* @param {string} path
*/
/**
* @overload
* @param {ComfyWorkflowManager} manager
* @param {string} path
* @param {string[]} pathParts
* @param {boolean} isFavorite
*/
/**
* @param {ComfyWorkflowManager} manager
* @param {string} path
* @param {string[]} [pathParts]
* @param {boolean} [isFavorite]
*/
constructor(manager, path, pathParts, isFavorite) {
this.manager = manager;
if (pathParts) {
this.#path = path;
this.#pathParts = pathParts;
this.#name = pathParts[pathParts.length - 1].replace(/\.json$/, "");
this.#isFavorite = isFavorite;
} else {
this.#name = path;
this.unsaved = true;
} }
} }
async updateState(source, target) { async getWorkflowData() {
const prevState = source.pop(); const resp = await api.getUserData("workflows/" + this.path);
if (prevState) { if (resp.status !== 200) {
target.push(this.activeState); alert(`Error loading user data file '${this.path}': ${resp.status} ${resp.statusText}`);
this.isOurLoad = true; return;
await ComfyWorkflow.app.loadGraphData(prevState, false, this.workflow); }
this.activeState = prevState; return await resp.json();
}
load = async () => {
if (this.isOpen) {
await this.manager.app.loadGraphData(this.changeTracker.activeState, true, this);
} else {
const data = await this.getWorkflowData();
if (!data) return;
await this.manager.app.loadGraphData(data, true, this);
}
};
async save() {
if (!this.path) {
await this.#save(null, false);
} else {
await this.#save(this.path, true);
} }
} }
async undoRedo(e) { /**
if (e.ctrlKey || e.metaKey) { * @param {boolean} value
if (e.key === "y") { */
this.updateState(this.redo, this.undo); async favorite(value) {
return true; try {
} else if (e.key === "z") { await this.manager.saveWorkflowMetadata();
this.updateState(this.undo, this.redo); this.#isFavorite = value;
return true; this.manager.dispatchEvent(new CustomEvent("favorite", { detail: this }));
} } catch (error) {
alert("Error favoriting workflow " + this.name + "\n" + (error.message ?? error));
} }
} }
static init(app) { /**
const loadGraphData = app.loadGraphData; * @param {string} path
app.loadGraphData = async function () { */
const v = await loadGraphData.apply(this, arguments); async rename(path) {
if (ComfyWorkflow.changeTracker.isOurLoad) { let res = await api.moveUserData("workflows/" + this.path, "workflows/" + path);
ComfyWorkflow.changeTracker.isOurLoad = false;
} else {
ComfyWorkflow.changeTracker.checkState();
}
return v;
};
let keyIgnored = false; if (res.status === 409) {
window.addEventListener( if (!confirm(`Workflow '${path}' already exists, do you want to overwrite it?`)) return res;
"keydown", res = await api.moveUserData("workflows/" + this.path, "workflows/" + path, { overwrite: true });
(e) => { }
requestAnimationFrame(async () => {
let activeEl;
// If we are auto queue in change mode then we do want to trigger on inputs
if (!app.ui.autoQueueEnabled || app.ui.autoQueueMode === "instant") {
activeEl = document.activeElement;
if (activeEl?.tagName === "INPUT" || activeEl?.["type"] === "textarea") {
// Ignore events on inputs, they have their native history
return;
}
}
keyIgnored = e.key === "Control" || e.key === "Shift" || e.key === "Alt" || e.key === "Meta"; this.#updatePath(path);
if (keyIgnored) return; return res;
// Check if this is a ctrl+z ctrl+y
if (await ComfyWorkflow.changeTracker.undoRedo(e)) return;
// If our active element is some type of input then handle changes after they're done
if (ChangeTracker.bindInput(activeEl)) return;
ComfyWorkflow.changeTracker.checkState();
});
},
true
);
window.addEventListener("keyup", (e) => {
if (keyIgnored) {
keyIgnored = false;
ComfyWorkflow.changeTracker.checkState();
}
});
// Handle clicking DOM elements (e.g. widgets)
window.addEventListener("mouseup", () => {
ComfyWorkflow.changeTracker.checkState();
});
// Handle prompt queue event for dynamic widget changes
api.addEventListener("promptQueued", () => {
ComfyWorkflow.changeTracker.checkState();
});
// Handle litegraph clicks
const processMouseUp = LGraphCanvas.prototype.processMouseUp;
LGraphCanvas.prototype.processMouseUp = function (e) {
const v = processMouseUp.apply(this, arguments);
ComfyWorkflow.changeTracker.checkState();
return v;
};
const processMouseDown = LGraphCanvas.prototype.processMouseDown;
LGraphCanvas.prototype.processMouseDown = function (e) {
const v = processMouseDown.apply(this, arguments);
ComfyWorkflow.changeTracker.checkState();
return v;
};
// Handle litegraph context menu for COMBO widgets
const close = LiteGraph.ContextMenu.prototype.close;
LiteGraph.ContextMenu.prototype.close = function (e) {
const v = close.apply(this, arguments);
ComfyWorkflow.changeTracker.checkState();
return v;
};
} }
static bindInput(activeEl) { async insert() {
if (activeEl && activeEl.tagName !== "CANVAS" && activeEl.tagName !== "BODY") { const data = await this.getWorkflowData();
for (const evt of ["change", "input", "blur"]) { if (!data) return;
if (`on${evt}` in activeEl) {
const listener = () => { const old = localStorage.getItem("litegrapheditor_clipboard");
ComfyWorkflow.changeTracker.checkState(); const graph = new LGraph(data);
activeEl.removeEventListener(evt, listener); const canvas = new LGraphCanvas(null, graph, { skip_events: true, skip_render: true });
}; canvas.selectNodes();
activeEl.addEventListener(evt, listener); canvas.copyToClipboard();
return true; this.manager.app.canvas.pasteFromClipboard();
} localStorage.setItem("litegrapheditor_clipboard", old);
} }
async delete() {
if (this.isFavorite) {
await this.favorite(false);
}
await api.deleteUserData(this.path);
this.manager.dispatchEvent(new CustomEvent("delete", { detail: this }));
}
track() {
if (this.changeTracker) {
this.changeTracker.restoreViewport();
} else {
this.changeTracker = new ChangeTracker(this);
} }
} }
static graphEqual(a, b, root = true) { /**
if (a === b) return true; * @param {string} path
*/
#updatePath(path) {
this.#path = path;
if (!path.includes("\\")) {
this.#pathParts = path.split("/");
} else {
this.#pathParts = path.split("\\");
}
this.manager.dispatchEvent(new CustomEvent("rename", { detail: this }));
}
if (typeof a == "object" && a && typeof b == "object" && b) { /**
const keys = Object.getOwnPropertyNames(a); * @param {string|null} path
* @param {boolean} overwrite
if (keys.length != Object.getOwnPropertyNames(b).length) { */
return false; async #save(path, overwrite) {
} if (!path) {
path = prompt("Save workflow as:", this.path ?? "workflow");
for (const key of keys) { if (!path) return;
let av = a[key];
let bv = b[key];
if (root && key === "nodes") {
// Nodes need to be sorted as the order changes when selecting nodes
av = [...av].sort((a, b) => a.id - b.id);
bv = [...bv].sort((a, b) => a.id - b.id);
}
if (!ChangeTracker.graphEqual(av, bv, false)) {
return false;
}
}
return true;
} }
return false; if (!path.toLowerCase().endsWith(".json")) {
path += ".json";
}
const p = await this.manager.app.graphToPrompt();
const json = JSON.stringify(p.workflow, null, 2);
const res = await api.storeUserData("workflows/" + path, json, { stringify: false, throwOnError: false, overwrite });
if (res.status === 409) {
if (!confirm(`Workflow '${path}' already exists, do you want to overwrite it?`)) return;
await api.storeUserData("workflows/" + path, json, { stringify: false });
this.#updatePath(path);
} else if (path === this.path && this.isOpen) {
this.unsaved = false;
this.manager.dispatchEvent(new CustomEvent("save", { detail: this }));
}
} }
} }