Support multiple image loaders

This commit is contained in:
pythongosssss 2024-03-10 11:30:20 +00:00
parent cef8c13703
commit 71725566ac
3 changed files with 124 additions and 20 deletions

View File

@ -1,9 +1,9 @@
import { $el } from "../ui.js";
export class ComfyDialog extends EventTarget {
constructor() {
constructor(type = "div") {
super();
this.element = $el("div.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()]),
]);
}
@ -26,7 +26,7 @@ export class ComfyDialog extends EventTarget {
if (typeof html === "string") {
this.textElement.innerHTML = html;
} else {
this.textElement.replaceChildren(html);
this.textElement.replaceChildren(...(html instanceof Array ? html : [html]));
}
this.element.style.display = "flex";
}

View File

@ -598,3 +598,43 @@
padding: 0;
margin: 0;
}
/** Send to workflow widget selection dialog */
.comfy-widget-selection-dialog {
border: none;
}
.comfy-widget-selection-dialog div {
color: var(--fg-color);
font-family: Arial, Helvetica, sans-serif;
}
.comfy-widget-selection-dialog h2 {
margin-top: 0;
}
.comfy-widget-selection-dialog section {
width: fit-content;
display: flex;
flex-direction: column;
}
.comfy-widget-selection-item {
display: flex;
gap: 10px;
align-items: center;
}
.comfy-widget-selection-item span {
margin-right: auto;
}
.comfy-widget-selection-item span::before {
content: '#' attr(data-id);
opacity: 0.5;
margin-right: 5px;
}
.comfy-modal .comfy-widget-selection-item button {
font-size: 1em;
}

View File

@ -7,6 +7,7 @@ import { api } from "../../api.js";
import { ComfyPopup } from "../components/popup.js";
import { createSpinner } from "../spinner.js";
import { ComfyWorkflow, trimJsonExt } from "../../workflows.js";
import { ComfyDialog } from "../dialog.js";
export class ComfyWorkflowsMenu {
#first = true;
@ -173,6 +174,7 @@ export class ComfyWorkflowsMenu {
const imageId = `${subfolder ? subfolder + "/" : ""}${filename} [${type}]`;
widget.value = imageId;
node.imgs = [img];
app.graph.setDirtyCanvas(true, true);
}
/**
@ -181,35 +183,31 @@ export class ComfyWorkflowsMenu {
*/
async function sendToWorkflow(img, workflow) {
await workflow.load();
let targetNode;
let targetWidget;
let options = [];
const nodes = app.graph.computeExecutionOrder(false);
for (const n of nodes) {
const widget = getImageWidget(n);
for (const node of nodes) {
const widget = getImageWidget(node);
if (widget == null) continue;
if (n.title?.toLowerCase().includes("input")) {
targetWidget = widget;
targetNode = n;
if (node.title?.toLowerCase().includes("input")) {
options = [{ widget, node }];
break;
} else if (!targetWidget) {
targetWidget = widget;
targetNode = n;
} else {
alert(
"Multiple image nodes have been found, the first one has been selected. You can include 'input' in the title of the node to use a specific node."
);
break;
options.push({ widget, node });
}
}
if(!targetWidget) {
if (!options.length) {
alert("No image nodes have been found in this workflow!");
return;
} else if (options.length > 1) {
const dialog = new WidgetSelectionDialog(options);
const res = await dialog.show(app);
if (!res) return;
options = [res];
}
setWidgetImage(targetNode, targetWidget, img);
setWidgetImage(options[0].node, options[0].widget, img);
}
const getExtraMenuOptions = nodeType.prototype["getExtraMenuOptions"];
@ -614,3 +612,69 @@ class WorkflowElement {
);
}
}
class WidgetSelectionDialog extends ComfyDialog {
#options;
#resolve;
/**
* @param {Array<{widget: {name: string}, node: {pos: [number, number], title: string, id: string, type: string}}>} options
*/
constructor(options) {
super("dialog");
this.#options = options;
}
show(app) {
this.element.addEventListener("close", () => {
this.close();
});
this.element.classList.add("comfy-widget-selection-dialog");
super.show(
$el("div", [
$el("h2", "Select image target"),
$el(
"p",
"This workflow has multiple image loader nodes, you can rename a node to include 'input' in the title for it to be automatically selected, or select one below."
),
$el(
"section",
this.#options.map((opt) => {
return $el("div.comfy-widget-selection-item", [
$el("span", { dataset: { id: opt.node.id } }, `${opt.node.title ?? opt.node.type} ${opt.widget.name}`),
$el(
"button.comfyui-button",
{
onclick: () => {
app.canvas.ds.offset[0] = -opt.node.pos[0] + 50;
app.canvas.ds.offset[1] = -opt.node.pos[1] + 50;
app.canvas.selectNode(opt.node);
},
},
"Show"
),
$el(
"button.comfyui-button.primary",
{
onclick: () => {
this.#resolve(opt);
this.close();
},
},
"Select"
),
]);
})
),
])
);
return new Promise((resolve) => {
this.#resolve = resolve;
});
}
close() {
this.#resolve();
super.close();
}
}