various fixes

add close unsaved warning
This commit is contained in:
pythongosssss 2024-03-12 21:41:14 +00:00
parent c6e8c3be6f
commit 35d3ecbdff
6 changed files with 187 additions and 60 deletions

View File

@ -44,6 +44,8 @@ export class ChangeTracker {
} }
checkState() { checkState() {
if(!this.app.graph) return;
const currentState = this.app.graph.serialize(); const currentState = this.app.graph.serialize();
if (!this.activeState) { if (!this.activeState) {
this.activeState = clone(currentState); this.activeState = clone(currentState);

View File

@ -0,0 +1,64 @@
import { ComfyDialog } from "../dialog.js";
import { $el } from "../../ui.js";
export class ComfyAsyncDialog extends ComfyDialog {
#resolve;
constructor(actions) {
super(
"dialog.comfy-dialog",
actions.map((opt) => {
if (typeof opt === "string") {
opt = { text: opt };
}
return $el("button.comfyui-button", {
type: "button",
textContent: opt.text,
onclick: () => this.close(opt.value ?? opt.text),
});
})
);
}
show(html) {
this.element.addEventListener("close", () => {
this.close();
});
super.show(html);
return new Promise((resolve) => {
this.#resolve = resolve;
});
}
showModal(html) {
this.element.addEventListener("close", () => {
this.close();
});
super.show(html);
this.element.showModal();
return new Promise((resolve) => {
this.#resolve = resolve;
});
}
close(result = null) {
this.#resolve(result);
this.element.close();
super.close();
}
static async prompt({ title = null, message, actions }) {
const dialog = new ComfyAsyncDialog(actions);
const content = [$el("span", message)];
if (title) {
content.unshift($el("h3", title));
}
const res = await dialog.showModal(content);
dialog.element.remove();
return res;
}
}

View File

@ -1,21 +1,26 @@
import { $el } from "../ui.js"; import { $el } from "../ui.js";
export class ComfyDialog extends EventTarget { export class ComfyDialog extends EventTarget {
constructor(type = "div") { #buttons;
constructor(type = "div", buttons = null) {
super(); super();
this.#buttons = buttons;
this.element = $el(type + ".comfy-modal", { parent: document.body }, [ this.element = $el(type + ".comfy-modal", { parent: document.body }, [
$el("div.comfy-modal-content", [$el("p", { $: (p) => (this.textElement = p) }), ...this.createButtons()]), $el("div.comfy-modal-content", [$el("p", { $: (p) => (this.textElement = p) }), ...this.createButtons()]),
]); ]);
} }
createButtons() { createButtons() {
return [ return (
$el("button", { this.#buttons ?? [
type: "button", $el("button", {
textContent: "Close", type: "button",
onclick: () => this.close(), textContent: "Close",
}), onclick: () => this.close(),
]; }),
]
);
} }
close() { close() {

View File

@ -7,7 +7,7 @@ import { api } from "../../api.js";
import { ComfyPopup } from "../components/popup.js"; import { ComfyPopup } from "../components/popup.js";
import { createSpinner } from "../spinner.js"; import { createSpinner } from "../spinner.js";
import { ComfyWorkflow, trimJsonExt } from "../../workflows.js"; import { ComfyWorkflow, trimJsonExt } from "../../workflows.js";
import { ComfyDialog } from "../dialog.js"; import { ComfyAsyncDialog } from "../components/asyncDialog.js";
export class ComfyWorkflowsMenu { export class ComfyWorkflowsMenu {
#first = true; #first = true;
@ -213,39 +213,42 @@ export class ComfyWorkflowsMenu {
const getExtraMenuOptions = nodeType.prototype["getExtraMenuOptions"]; const getExtraMenuOptions = nodeType.prototype["getExtraMenuOptions"];
nodeType.prototype["getExtraMenuOptions"] = function (_, options) { nodeType.prototype["getExtraMenuOptions"] = function (_, options) {
const r = getExtraMenuOptions?.apply?.(this, arguments); const r = getExtraMenuOptions?.apply?.(this, arguments);
const t = /** @type { {imageIndex?: number, overIndex?: number, imgs: string[]} } */ /** @type {any} */ (this);
let img;
if (t.imageIndex != null) {
// An image is selected so select that
img = t.imgs?.[t.imageIndex];
} else if (t.overIndex != null) {
// No image is selected but one is hovered
img = t.img?.s[t.overIndex];
}
if (img) { if (app.ui.settings.getSettingValue("Comfy.UseNewMenu", false) === true) {
let pos = options.findIndex((o) => o.content === "Save Image"); const t = /** @type { {imageIndex?: number, overIndex?: number, imgs: string[]} } */ /** @type {any} */ (this);
if (pos === -1) { let img;
pos = 0; if (t.imageIndex != null) {
} else { // An image is selected so select that
pos++; img = t.imgs?.[t.imageIndex];
} else if (t.overIndex != null) {
// No image is selected but one is hovered
img = t.img?.s[t.overIndex];
} }
options.splice(pos, 0, { if (img) {
content: "Send to workflow", let pos = options.findIndex((o) => o.content === "Save Image");
has_submenu: true, if (pos === -1) {
submenu: { pos = 0;
options: [ } else {
{ pos++;
callback: () => sendToWorkflow(img, app.workflowManager.activeWorkflow), }
title: "[Current workflow]",
}, options.splice(pos, 0, {
...self.#getFavoriteMenuOptions(sendToWorkflow.bind(null, img)), content: "Send to workflow",
null, has_submenu: true,
...self.#getMenuOptions(sendToWorkflow.bind(null, img)), submenu: {
], options: [
}, {
}); callback: () => sendToWorkflow(img, app.workflowManager.activeWorkflow),
title: "[Current workflow]",
},
...self.#getFavoriteMenuOptions(sendToWorkflow.bind(null, img)),
null,
...self.#getMenuOptions(sendToWorkflow.bind(null, img)),
],
},
});
}
} }
return r; return r;
@ -277,6 +280,7 @@ export class ComfyWorkflowsContent {
icon: "file-code", icon: "file-code",
iconSize: 18, iconSize: 18,
classList: "comfyui-button primary", classList: "comfyui-button primary",
tooltip: "Load default workflow",
action: () => { action: () => {
popup.open = false; popup.open = false;
app.loadGraphData(); app.loadGraphData();
@ -286,6 +290,7 @@ export class ComfyWorkflowsContent {
content: "Browse", content: "Browse",
icon: "folder", icon: "folder",
iconSize: 18, iconSize: 18,
tooltip: "Browse for an image or exported workflow",
action: () => { action: () => {
popup.open = false; popup.open = false;
app.ui.loadFile(); app.ui.loadFile();
@ -295,6 +300,7 @@ export class ComfyWorkflowsContent {
content: "Blank", content: "Blank",
icon: "plus-thick", icon: "plus-thick",
iconSize: 18, iconSize: 18,
tooltip: "Create a new blank workflow",
action: () => { action: () => {
app.workflowManager.setWorkflow(null); app.workflowManager.setWorkflow(null);
app.clean(); app.clean();
@ -351,6 +357,7 @@ export class ComfyWorkflowsContent {
icon: "close", icon: "close",
iconSize: 18, iconSize: 18,
classList: "comfyui-button comfyui-workflows-file-action", classList: "comfyui-button comfyui-workflows-file-action",
tooltip: "Close workflow",
action: (e) => { action: (e) => {
e.stopImmediatePropagation(); e.stopImmediatePropagation();
this.app.workflowManager.closeWorkflow(w); this.app.workflowManager.closeWorkflow(w);
@ -469,6 +476,11 @@ export class ComfyWorkflowsContent {
return workflow.isFavorite ? "star-off" : "star-outline"; return workflow.isFavorite ? "star-off" : "star-outline";
} }
/** @param {ComfyWorkflow} workflow */
#getFavoriteTooltip(workflow) {
return workflow.isFavorite ? "Remove this workflow from your favorites" : "Add this workflow to your favorites";
}
/** @param {ComfyWorkflow} workflow */ /** @param {ComfyWorkflow} workflow */
#getFavoriteButton(workflow, primary) { #getFavoriteButton(workflow, primary) {
return new ComfyButton({ return new ComfyButton({
@ -476,6 +488,7 @@ export class ComfyWorkflowsContent {
overIcon: this.#getFavoriteOverIcon(workflow), overIcon: this.#getFavoriteOverIcon(workflow),
iconSize: 18, iconSize: 18,
classList: "comfyui-button comfyui-workflows-file-action-favorite" + (primary ? " comfyui-workflows-file-action-primary" : ""), classList: "comfyui-button comfyui-workflows-file-action-favorite" + (primary ? " comfyui-workflows-file-action-primary" : ""),
tooltip: this.#getFavoriteTooltip(workflow),
action: (e) => { action: (e) => {
e.stopImmediatePropagation(); e.stopImmediatePropagation();
workflow.favorite(!workflow.isFavorite); workflow.favorite(!workflow.isFavorite);
@ -613,24 +626,20 @@ class WorkflowElement {
} }
} }
class WidgetSelectionDialog extends ComfyDialog { class WidgetSelectionDialog extends ComfyAsyncDialog {
#options; #options;
#resolve;
/** /**
* @param {Array<{widget: {name: string}, node: {pos: [number, number], title: string, id: string, type: string}}>} options * @param {Array<{widget: {name: string}, node: {pos: [number, number], title: string, id: string, type: string}}>} options
*/ */
constructor(options) { constructor(options) {
super("dialog"); super();
this.#options = options; this.#options = options;
} }
show(app) { show(app) {
this.element.addEventListener("close", () => {
this.close();
});
this.element.classList.add("comfy-widget-selection-dialog"); this.element.classList.add("comfy-widget-selection-dialog");
super.show( return super.show(
$el("div", [ $el("div", [
$el("h2", "Select image target"), $el("h2", "Select image target"),
$el( $el(
@ -649,6 +658,7 @@ class WidgetSelectionDialog extends ComfyDialog {
app.canvas.ds.offset[0] = -opt.node.pos[0] + 50; app.canvas.ds.offset[0] = -opt.node.pos[0] + 50;
app.canvas.ds.offset[1] = -opt.node.pos[1] + 50; app.canvas.ds.offset[1] = -opt.node.pos[1] + 50;
app.canvas.selectNode(opt.node); app.canvas.selectNode(opt.node);
app.graph.setDirtyCanvas(true, true);
}, },
}, },
"Show" "Show"
@ -657,8 +667,7 @@ class WidgetSelectionDialog extends ComfyDialog {
"button.comfyui-button.primary", "button.comfyui-button.primary",
{ {
onclick: () => { onclick: () => {
this.#resolve(opt); this.close(opt);
this.close();
}, },
}, },
"Select" "Select"
@ -668,13 +677,5 @@ class WidgetSelectionDialog extends ComfyDialog {
), ),
]) ])
); );
return new Promise((resolve) => {
this.#resolve = resolve;
});
}
close() {
this.#resolve();
super.close();
} }
} }

View File

@ -2,6 +2,7 @@
import { api } from "./api.js"; import { api } from "./api.js";
import { ChangeTracker } from "./changeTracker.js"; import { ChangeTracker } from "./changeTracker.js";
import { ComfyAsyncDialog } from "./ui/components/asyncDialog.js";
import { getStorageValue, setStorageValue } from "./utils.js"; import { getStorageValue, setStorageValue } from "./utils.js";
function appendJsonExt(path) { function appendJsonExt(path) {
@ -171,9 +172,33 @@ export class ComfyWorkflowManager extends EventTarget {
/** /**
* @param {ComfyWorkflow} workflow * @param {ComfyWorkflow} workflow
*/ */
async closeWorkflow(workflow) { async closeWorkflow(workflow, warnIfUnsaved = true) {
if (!workflow.isOpen) { if (!workflow.isOpen) {
return; return true;
}
if (workflow.unsaved && warnIfUnsaved) {
const res = await ComfyAsyncDialog.prompt({
title: "Save Changes?",
message: `Do you want to save changes to "${workflow.path ?? workflow.name}" before closing?`,
actions: ["Yes", "No", "Cancel"],
});
if (res === "Yes") {
const active = this.activeWorkflow;
if (active !== workflow) {
// We need to switch to the workflow to save it
await workflow.load();
}
if (!(await workflow.save())) {
// Save was canceled, restore the previous workflow
if (active !== workflow) {
await active.load();
}
return;
}
} else if (res === "Cancel") {
return;
}
} }
workflow.changeTracker = null; workflow.changeTracker = null;
this.openWorkflows.splice(this.openWorkflows.indexOf(workflow), 1); this.openWorkflows.splice(this.openWorkflows.indexOf(workflow), 1);
@ -284,9 +309,9 @@ export class ComfyWorkflow {
async save(saveAs = false) { async save(saveAs = false) {
if (!this.path || saveAs) { if (!this.path || saveAs) {
await this.#save(null, false); return !!(await this.#save(null, false));
} else { } else {
await this.#save(this.path, true); return !!(await this.#save(this.path, true));
} }
} }
@ -331,6 +356,7 @@ export class ComfyWorkflow {
await this.favorite(true); await this.favorite(true);
} }
this.manager.dispatchEvent(new CustomEvent("rename", { detail: this })); this.manager.dispatchEvent(new CustomEvent("rename", { detail: this }));
setStorageValue("Comfy.PreviousWorkflow", this.path ?? "");
} }
async insert() { async insert() {
@ -405,6 +431,7 @@ export class ComfyWorkflow {
await this.manager.loadWorkflows(); await this.manager.loadWorkflows();
this.unsaved = false; this.unsaved = false;
this.manager.dispatchEvent(new CustomEvent("rename", { detail: this })); this.manager.dispatchEvent(new CustomEvent("rename", { detail: this }));
setStorageValue("Comfy.PreviousWorkflow", this.path ?? "");
} else if (path !== this.path) { } else if (path !== this.path) {
// Saved as, open the new copy // Saved as, open the new copy
await this.manager.loadWorkflows(); await this.manager.loadWorkflows();
@ -415,5 +442,7 @@ export class ComfyWorkflow {
this.unsaved = false; this.unsaved = false;
this.manager.dispatchEvent(new CustomEvent("save", { detail: this })); this.manager.dispatchEvent(new CustomEvent("save", { detail: this }));
} }
return true;
} }
} }

View File

@ -372,6 +372,32 @@ dialog::backdrop {
background: rgba(0, 0, 0, 0.5); background: rgba(0, 0, 0, 0.5);
} }
.comfy-dialog.comfy-modal {
top: 0;
font-family: Arial, sans-serif;
border-color: var(--bg-color);
}
.comfy-dialog .comfy-modal-content {
flex-direction: row;
flex-wrap: wrap;
gap: 10px;
color: var(--fg-color);
}
.comfy-dialog .comfy-modal-content h3 {
margin-top: 0;
}
.comfy-dialog .comfy-modal-content > p {
width: 100%;
}
.comfy-dialog .comfy-modal-content > .comfyui-button {
flex: 1;
justify-content: center;
}
#comfy-settings-dialog { #comfy-settings-dialog {
padding: 0; padding: 0;
width: 41rem; width: 41rem;