From 71725566ac5413ae6cedd64c09fe33e80ead5508 Mon Sep 17 00:00:00 2001 From: pythongosssss <125205205+pythongosssss@users.noreply.github.com> Date: Sun, 10 Mar 2024 11:30:20 +0000 Subject: [PATCH] Support multiple image loaders --- web/scripts/ui/dialog.js | 6 +- web/scripts/ui/menu/menu.css | 40 +++++++++++++ web/scripts/ui/menu/workflows.js | 98 ++++++++++++++++++++++++++------ 3 files changed, 124 insertions(+), 20 deletions(-) diff --git a/web/scripts/ui/dialog.js b/web/scripts/ui/dialog.js index 3cd9c4e55..16c12b406 100644 --- a/web/scripts/ui/dialog.js +++ b/web/scripts/ui/dialog.js @@ -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"; } diff --git a/web/scripts/ui/menu/menu.css b/web/scripts/ui/menu/menu.css index c19ef619f..79f1eb985 100644 --- a/web/scripts/ui/menu/menu.css +++ b/web/scripts/ui/menu/menu.css @@ -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; +} \ No newline at end of file diff --git a/web/scripts/ui/menu/workflows.js b/web/scripts/ui/menu/workflows.js index 514dfdbbe..95415a35d 100644 --- a/web/scripts/ui/menu/workflows.js +++ b/web/scripts/ui/menu/workflows.js @@ -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(); + } +} \ No newline at end of file