mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-20 22:17:02 +08:00
refactor, better workflow instance management
This commit is contained in:
parent
60ad4f9abc
commit
5a5634bd8e
@ -130,7 +130,7 @@ class UserManager():
|
||||
|
||||
split_path = request.rel_url.query.get('split', '').lower() == "true"
|
||||
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)
|
||||
|
||||
|
||||
@ -450,7 +450,7 @@ class ComfyApi extends EventTarget {
|
||||
* @param { string } dir The directory in which to list files
|
||||
* @param { boolean } [recurse] If the listing should be recursive
|
||||
* @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
|
||||
|
||||
@ -8,7 +8,7 @@ import { addDomClippingSetting } from "./domWidget.js";
|
||||
import { createImageHost, calculateImageGrid } from "./ui/imagePreview.js";
|
||||
import { ComfyAppMenu } from "./ui/menu/index.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";
|
||||
|
||||
function sanitizeNodeName(string) {
|
||||
@ -54,8 +54,8 @@ export class ComfyApp {
|
||||
constructor() {
|
||||
this.ui = new ComfyUI(this);
|
||||
this.logging = new ComfyLogging(this);
|
||||
ComfyWorkflow.init(this);
|
||||
new ComfyAppMenu(this);
|
||||
this.workflowManager = new ComfyWorkflowManager(this);
|
||||
this.menu = new ComfyAppMenu(this);
|
||||
|
||||
/**
|
||||
* List of extensions that are registered with the app
|
||||
@ -1454,7 +1454,7 @@ export class ComfyApp {
|
||||
*/
|
||||
async setup() {
|
||||
await this.#setUser();
|
||||
await this.ui.settings.load();
|
||||
await Promise.all([this.workflowManager.loadWorkflows(), this.ui.settings.load()]);
|
||||
await this.#loadExtensions();
|
||||
|
||||
// Create and mount the LiteGraph in the DOM
|
||||
@ -1506,7 +1506,7 @@ export class ComfyApp {
|
||||
const loadWorkflow = async (json) => {
|
||||
if (json) {
|
||||
const workflow = JSON.parse(json);
|
||||
const workflowName = getStorageValue("Comfy.LastWorkflow");
|
||||
const workflowName = getStorageValue("Comfy.PreviousWorkflow");
|
||||
await this.loadGraphData(workflow, true, workflowName);
|
||||
return true;
|
||||
}
|
||||
@ -1729,11 +1729,11 @@ 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 { 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) {
|
||||
if (clean !== false) {
|
||||
@ -1754,10 +1754,15 @@ export class ComfyApp {
|
||||
graphData = structuredClone(graphData);
|
||||
}
|
||||
|
||||
if (workflow) {
|
||||
ComfyWorkflow.changeWorkflow(workflow);
|
||||
} else {
|
||||
ComfyWorkflow.newWorkflow();
|
||||
try {
|
||||
this.workflowManager.activeWorkflow?.changeTracker?.storeViewport()
|
||||
} catch (error) {
|
||||
}
|
||||
|
||||
try {
|
||||
this.workflowManager.setWorkflow(workflow);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
const missingNodeTypes = [];
|
||||
@ -1777,6 +1782,11 @@ export class ComfyApp {
|
||||
|
||||
try {
|
||||
this.graph.configure(graphData);
|
||||
|
||||
try {
|
||||
this.workflowManager.activeWorkflow?.track()
|
||||
} catch (error) {
|
||||
}
|
||||
} catch (error) {
|
||||
let errorHint = [];
|
||||
// Try extracting filename to see if it was caused by an extension script
|
||||
|
||||
204
web/scripts/changeTracker.js
Normal file
204
web/scripts/changeTracker.js
Normal 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;
|
||||
}
|
||||
}
|
||||
@ -292,6 +292,7 @@
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 7px;
|
||||
width: 130px;
|
||||
}
|
||||
.comfyui-workflows-label {
|
||||
overflow: hidden;
|
||||
@ -299,7 +300,6 @@
|
||||
direction: rtl;
|
||||
flex: auto;
|
||||
position: relative;
|
||||
width: 130px;
|
||||
}
|
||||
|
||||
.comfyui-workflows-button.unsaved .comfyui-workflows-label {
|
||||
|
||||
@ -8,27 +8,6 @@ import { ComfyPopup } from "../components/popup.js";
|
||||
import { createSpinner } from "../spinner.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 {
|
||||
element = $el("div.comfyui-workflows");
|
||||
|
||||
@ -40,37 +19,27 @@ export class ComfyWorkflowsMenu {
|
||||
this.popup.open = open;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {import("../../app.js").ComfyApp} app
|
||||
*/
|
||||
constructor(app) {
|
||||
this.app = app;
|
||||
|
||||
let ignoreChange = false;
|
||||
let first = true;
|
||||
api.addEventListener("workflowsChanged", ({ detail }) => {
|
||||
this.currentWorkflow = ComfyWorkflow.activeWorkflow.displayName;
|
||||
console.log(detail);
|
||||
// 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();
|
||||
app.workflowManager.addEventListener("change", () => {
|
||||
const active = app.workflowManager.activeWorkflow;
|
||||
this.button.tooltip = active.path;
|
||||
this.workflowLabel.textContent = active.name;
|
||||
this.unsaved = active.unsaved;
|
||||
});
|
||||
|
||||
api.addEventListener("graphChanged", () => {
|
||||
if (ignoreChange) {
|
||||
ignoreChange = false;
|
||||
} else {
|
||||
this.unsaved = true;
|
||||
}
|
||||
this.unsaved = true;
|
||||
});
|
||||
|
||||
const classList = {
|
||||
"comfyui-workflows-button": true,
|
||||
"comfyui-button": true,
|
||||
unsaved: false,
|
||||
unsaved: getStorageValue("Comfy.PreviousWorkflowUnsaved") === "true",
|
||||
};
|
||||
this.workflowLabel = $el("span.comfyui-workflows-label", "Unsaved workflow");
|
||||
this.button = new ComfyButton({
|
||||
@ -92,56 +61,18 @@ export class ComfyWorkflowsMenu {
|
||||
this.unsaved = prop(this, "unsaved", classList.unsaved, (v) => {
|
||||
classList.unsaved = v;
|
||||
this.button.classList = classList;
|
||||
if (!first) {
|
||||
setStorageValue("Comfy.LastWorkflowUnsaved", v);
|
||||
}
|
||||
setStorageValue("Comfy.PreviousWorkflowUnsaved", 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 {
|
||||
#fileLookup = {};
|
||||
element = $el("div.comfyui-workflows-panel");
|
||||
/** @type {Set<string>} */
|
||||
favorites;
|
||||
|
||||
/**
|
||||
* @param {import("../../app.js").ComfyApp} app
|
||||
* @param {ComfyPopup} popup
|
||||
*/
|
||||
constructor(app, popup) {
|
||||
this.app = app;
|
||||
this.popup = popup;
|
||||
@ -150,12 +81,19 @@ export class ComfyWorkflowsContent {
|
||||
content: "New Workflow",
|
||||
icon: "plus",
|
||||
action: () => {
|
||||
app.currentWorkflow = null;
|
||||
app.workflowManager.setWorkflow(null);
|
||||
app.clean();
|
||||
app.graph.clear();
|
||||
popup.open = false;
|
||||
},
|
||||
}).element,
|
||||
new ComfyButton({
|
||||
content: "Load JSON",
|
||||
action: () => {
|
||||
popup.open = false;
|
||||
app.loadGraphData();
|
||||
},
|
||||
}).element,
|
||||
new ComfyButton({
|
||||
content: "Load Default",
|
||||
action: () => {
|
||||
@ -172,107 +110,80 @@ export class ComfyWorkflowsContent {
|
||||
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() {
|
||||
const tree = await this.createTreeElement();
|
||||
this.updateFavoritesElement();
|
||||
this.updateOpenElement();
|
||||
this.element.replaceChildren(this.actions, this.openElement, this.favoritesElement, tree);
|
||||
this.updateTree();
|
||||
this.updateFavorites();
|
||||
this.updateOpenWorkflows();
|
||||
this.element.replaceChildren(this.actions, this.openElement, this.favoritesElement, this.treeElement);
|
||||
}
|
||||
|
||||
updateOpenElement() {
|
||||
updateOpenWorkflows() {
|
||||
const current = this.openElement;
|
||||
|
||||
this.openElement = $el(
|
||||
"div.comfyui-workflows-open",
|
||||
[$el("h3", "Open Workflows"), ...ComfyWorkflow.openWorkflows.map((w) => {
|
||||
const res = new FileNode(
|
||||
this,
|
||||
w.displayName,
|
||||
w.displayName,
|
||||
"div",
|
||||
(f) => {
|
||||
// entry.isFavorite = f;
|
||||
// entry.updateFavorite();
|
||||
// this.updateFavoritesElement();
|
||||
},
|
||||
() => {
|
||||
// res.element.remove();
|
||||
// entry.remove();
|
||||
return false;
|
||||
}
|
||||
);
|
||||
return res.element;
|
||||
})]
|
||||
);
|
||||
this.openElement = $el("div.comfyui-workflows-open", [
|
||||
$el("h3", "Open Workflows"),
|
||||
...this.app.workflowManager.openWorkflows.map((w) => {
|
||||
const wrapper = new WorkflowElement(w, {
|
||||
buttons: [],
|
||||
});
|
||||
|
||||
if (current) {
|
||||
current.replaceWith(this.openElement);
|
||||
}
|
||||
return wrapper.element;
|
||||
}),
|
||||
]);
|
||||
|
||||
current?.replaceWith(this.openElement);
|
||||
}
|
||||
|
||||
updateFavoritesElement() {
|
||||
const favorites = [...this.favorites.values()];
|
||||
updateFavorites() {
|
||||
const current = this.favoritesElement;
|
||||
const favorites = [...this.app.workflowManager.workflows.filter((w) => w.isFavorite)];
|
||||
|
||||
this.favoritesElement = $el(
|
||||
"div.comfyui-workflows-favorites",
|
||||
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;
|
||||
return $el("span", f.path);
|
||||
// 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) {
|
||||
current.replaceWith(this.favoritesElement);
|
||||
}
|
||||
current?.replaceWith(this.favoritesElement);
|
||||
}
|
||||
|
||||
async createTreeElement() {
|
||||
await this.loadWorkflowsInfo();
|
||||
const workflows = await api.listUserData("workflows", true, true);
|
||||
updateTree() {
|
||||
const current = this.treeElement;
|
||||
const flat = {};
|
||||
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 currentRoot = rootNode;
|
||||
let currentRoot = this.treeElement;
|
||||
|
||||
for (let i = 0; i < pathParts.length; i++) {
|
||||
const part = pathParts[i];
|
||||
for (let i = 0; i < workflow.pathParts.length; i++) {
|
||||
const part = workflow.pathParts[i];
|
||||
currentPath += (currentPath ? "\\" : "") + part;
|
||||
let parentNode = nodes[currentPath];
|
||||
|
||||
@ -289,12 +200,11 @@ export class ComfyWorkflowsContent {
|
||||
currentRoot.append(parentNode);
|
||||
|
||||
// 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;
|
||||
if (leaf) {
|
||||
const fileNode = new FileNode(this, currentPath, part);
|
||||
const fileNode = new WorkflowElement(workflow, { buttons: [] });
|
||||
nodeElement = fileNode.element;
|
||||
this.#fileLookup[currentPath] = fileNode;
|
||||
} else {
|
||||
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 {
|
||||
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;
|
||||
}
|
||||
class WorkflowElement {
|
||||
/**
|
||||
* @param { ComfyWorkflow } workflow
|
||||
*/
|
||||
constructor(workflow, { tagName = "li", buttons }) {
|
||||
this.workflow = workflow;
|
||||
this.buttons = buttons;
|
||||
|
||||
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,
|
||||
onclick: workflow.load,
|
||||
title: this.workflow.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,
|
||||
]
|
||||
[$el("span", workflow.name)]
|
||||
);
|
||||
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();
|
||||
// }
|
||||
// }
|
||||
|
||||
@ -1,283 +1,273 @@
|
||||
// @ts-check
|
||||
|
||||
import { api } from "./api.js";
|
||||
import { clone } from "./utils.js";
|
||||
import { ChangeTracker } from "./changeTracker.js";
|
||||
import { getStorageValue, setStorageValue } from "./utils.js";
|
||||
|
||||
export class ComfyWorkflow {
|
||||
/** @type { Array<ComfyWorkflow> } */
|
||||
static openWorkflows = [];
|
||||
/** @type {import("./app.js").ComfyApp} */
|
||||
static app;
|
||||
static unsavedCount = 0;
|
||||
export class ComfyWorkflowManager extends EventTarget {
|
||||
#unsavedCount = 0;
|
||||
|
||||
unsaved = false;
|
||||
/** @type { string | null } */
|
||||
name = null;
|
||||
/** @type { ChangeTracker } */
|
||||
changeTracker;
|
||||
/** @type { string | null } */
|
||||
tempName = null;
|
||||
/** @type {Array<ComfyWorkflow>} */
|
||||
workflows = [];
|
||||
/** @type {Array<ComfyWorkflow>} */
|
||||
openWorkflows = [];
|
||||
|
||||
get displayName() {
|
||||
return this.name ?? this.tempName;
|
||||
}
|
||||
|
||||
static get activeWorkflow() {
|
||||
get activeWorkflow() {
|
||||
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
|
||||
*/
|
||||
static init(app) {
|
||||
ComfyWorkflow.app = app;
|
||||
constructor(app) {
|
||||
super();
|
||||
this.app = app;
|
||||
ChangeTracker.init(app);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param { Partial<ComfyWorkflow> } opts
|
||||
* @returns
|
||||
*/
|
||||
static newWorkflow(opts = {}) {
|
||||
const workflow = new ComfyWorkflow(opts?.name);
|
||||
Object.assign(workflow, opts);
|
||||
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;
|
||||
async loadWorkflows() {
|
||||
try {
|
||||
let favorites;
|
||||
const resp = await api.getUserData("workflows/.index.json");
|
||||
let info;
|
||||
if (resp.status === 200) {
|
||||
info = await resp.json();
|
||||
favorites = new Set(info?.favorites ?? []);
|
||||
} 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);
|
||||
}
|
||||
this.openWorkflows.unshift(workflow);
|
||||
this.#workflowsChanged();
|
||||
return workflow;
|
||||
}
|
||||
|
||||
async saveWorkflowMetadata() {
|
||||
await api.storeUserData("workflows/.index.json", {
|
||||
favorites: [...this.workflows.filter((w) => w.isFavorite).map((w) => w.name)],
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
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() {
|
||||
console.log("workflowsChanged", this.activeWorkflow?.name, this.openWorkflows.length);
|
||||
api.dispatchEvent(new CustomEvent("workflowsChanged", { detail: this.activeWorkflow }));
|
||||
setStorageValue("Comfy.PreviousWorkflow", this.activeWorkflow.path ?? "");
|
||||
this.dispatchEvent(new CustomEvent("change"));
|
||||
}
|
||||
}
|
||||
|
||||
export class ChangeTracker {
|
||||
static MAX_HISTORY = 50;
|
||||
export class ComfyWorkflow {
|
||||
#name;
|
||||
#path;
|
||||
#pathParts;
|
||||
#isFavorite = false;
|
||||
/** @type {ChangeTracker | null} */
|
||||
changeTracker = null;
|
||||
unsaved = false;
|
||||
|
||||
undo = [];
|
||||
redo = [];
|
||||
activeState = null;
|
||||
isOurLoad = false;
|
||||
/** @type { ComfyWorkflow } */
|
||||
workflow;
|
||||
|
||||
constructor(workflow) {
|
||||
this.workflow = workflow;
|
||||
get name() {
|
||||
return this.#name;
|
||||
}
|
||||
|
||||
checkState() {
|
||||
const currentState = ComfyWorkflow.app.graph.serialize();
|
||||
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 }));
|
||||
get path() {
|
||||
return this.#path;
|
||||
}
|
||||
|
||||
get pathParts() {
|
||||
return this.#pathParts;
|
||||
}
|
||||
|
||||
get isFavorite() {
|
||||
return this.#isFavorite;
|
||||
}
|
||||
|
||||
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) {
|
||||
const prevState = source.pop();
|
||||
if (prevState) {
|
||||
target.push(this.activeState);
|
||||
this.isOurLoad = true;
|
||||
await ComfyWorkflow.app.loadGraphData(prevState, false, this.workflow);
|
||||
this.activeState = prevState;
|
||||
async getWorkflowData() {
|
||||
const resp = await api.getUserData("workflows/" + this.path);
|
||||
if (resp.status !== 200) {
|
||||
alert(`Error loading user data file '${this.path}': ${resp.status} ${resp.statusText}`);
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
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 {boolean} value
|
||||
*/
|
||||
async favorite(value) {
|
||||
try {
|
||||
await this.manager.saveWorkflowMetadata();
|
||||
this.#isFavorite = value;
|
||||
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;
|
||||
app.loadGraphData = async function () {
|
||||
const v = await loadGraphData.apply(this, arguments);
|
||||
if (ComfyWorkflow.changeTracker.isOurLoad) {
|
||||
ComfyWorkflow.changeTracker.isOurLoad = false;
|
||||
} else {
|
||||
ComfyWorkflow.changeTracker.checkState();
|
||||
}
|
||||
return v;
|
||||
};
|
||||
/**
|
||||
* @param {string} path
|
||||
*/
|
||||
async rename(path) {
|
||||
let res = await api.moveUserData("workflows/" + this.path, "workflows/" + path);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (res.status === 409) {
|
||||
if (!confirm(`Workflow '${path}' already exists, do you want to overwrite it?`)) return res;
|
||||
res = await api.moveUserData("workflows/" + this.path, "workflows/" + path, { overwrite: true });
|
||||
}
|
||||
|
||||
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 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;
|
||||
};
|
||||
this.#updatePath(path);
|
||||
return res;
|
||||
}
|
||||
|
||||
static bindInput(activeEl) {
|
||||
if (activeEl && activeEl.tagName !== "CANVAS" && activeEl.tagName !== "BODY") {
|
||||
for (const evt of ["change", "input", "blur"]) {
|
||||
if (`on${evt}` in activeEl) {
|
||||
const listener = () => {
|
||||
ComfyWorkflow.changeTracker.checkState();
|
||||
activeEl.removeEventListener(evt, listener);
|
||||
};
|
||||
activeEl.addEventListener(evt, listener);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
async insert() {
|
||||
const data = await this.getWorkflowData();
|
||||
if (!data) return;
|
||||
|
||||
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.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);
|
||||
|
||||
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;
|
||||
/**
|
||||
* @param {string|null} path
|
||||
* @param {boolean} overwrite
|
||||
*/
|
||||
async #save(path, overwrite) {
|
||||
if (!path) {
|
||||
path = prompt("Save workflow as:", this.path ?? "workflow");
|
||||
if (!path) return;
|
||||
}
|
||||
|
||||
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 }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user