replace prompt builder image inputs with ordered refmod sockets

This commit is contained in:
2026-10-20 15:53:13 +00:00
parent 5d41800ffb
commit 6f8fd85405
4 changed files with 52 additions and 139 deletions
+15 -110
View File
@@ -45,7 +45,7 @@ function widgetValue(node, name) {
return get(node, name)?.value;
}
function refModDescriptorsForNode(node) {
function refModDescriptorsForNode(node, inputName = "ref_mods") {
const result = [];
const add = (name, copies = 1) => {
const count = Math.max(1, Math.min(32, Number(copies) || 1));
@@ -55,8 +55,10 @@ function refModDescriptorsForNode(node) {
};
const visited = new Set();
const visit = (source) => {
if (!source || visited.has(source.id)) return;
visited.add(source.id);
if (!source) return;
const identity = source.id ?? source;
if (visited.has(identity)) return;
visited.add(identity);
const className = String(source.comfyClass || source.type || "");
// H3RefModLoader appends its selected item after the incoming bundle,
// so recurse first to preserve the backend's @R ordering.
@@ -83,7 +85,7 @@ function refModDescriptorsForNode(node) {
// A custom bundle node's contents are only known after execution.
add("Attached RefMod");
};
visit(linkedNode(node, "ref_mods"));
visit(linkedNode(node, inputName));
return result;
}
@@ -441,67 +443,12 @@ function configure(node) {
shotChips.append(chip);
});
panel.append(shotChips);
const referenceLabel = el("div", "REFERENCE SLOTS"); referenceLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:7px 0 3px"; panel.append(referenceLabel);
const referenceLabel = el("div", "REFMOD SLOTS"); referenceLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:7px 0 3px"; panel.append(referenceLabel);
const referenceRow = el("div"); referenceRow.style.cssText = "display:flex;gap:7px;overflow:hidden;margin:3px 0 8px;padding:5px;background:#202020;border:1px solid #3f4742;border-radius:5px;align-items:center;flex-wrap:wrap;min-height:38px;box-sizing:border-box";
const refModDescriptors = refModDescriptorsForNode(node);
const connectedReferences = Array.from({ length: 9 }, (_, offset) => offset + 1)
.filter((index) => node.inputs?.some((input) => input.name === `reference_image_${index}` && input.link != null));
if (connectedReferences.length) {
connectedReferences.forEach((index) => {
const button = el("button"); button.type = "button";
button.title = `Insert reference image ${index} as @image${index}`;
button.style.cssText = "display:inline-flex;align-items:center;gap:7px;height:31px;padding:2px 11px 2px 3px;border:2px solid #49c9f5;border-radius:18px;background:#202b31;color:#70d8ff;font:16px monospace;cursor:pointer;white-space:nowrap";
const thumbnail = document.createElement("img");
thumbnail.alt = ""; thumbnail.draggable = false;
thumbnail.style.cssText = "width:25px;height:25px;object-fit:cover;border-radius:50%;background:#68727a;display:block";
const linkId = node.inputs?.find((input) => input.name === `reference_image_${index}`)?.link;
const link = linkId == null ? null : (app.graph?.links?.get?.(linkId) ?? app.graph?.links?.[linkId]);
const source = link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id);
const imageWidget = source?.widgets?.find((widget) => ["image", "filename", "file"].includes(widget.name));
const imageValue = imageWidget?.value;
if (imageValue && typeof imageValue === "object" && imageValue.filename) {
const params = new URLSearchParams({ filename: imageValue.filename, type: imageValue.type || "input", subfolder: imageValue.subfolder || "" });
thumbnail.src = `/view?${params.toString()}`;
} else if (typeof imageValue === "string" && imageValue) {
const params = new URLSearchParams({ filename: imageValue, type: "input", subfolder: "" });
thumbnail.src = `/view?${params.toString()}`;
} else {
thumbnail.src = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='25' height='25' viewBox='0 0 25 25'%3E%3Crect width='25' height='25' fill='%2368727a'/%3E%3Cpath d='M4 19l5-6 3 3 3-4 6 7' fill='none' stroke='%23dce8ed' stroke-width='2'/%3E%3Ccircle cx='17' cy='8' r='2' fill='%23dce8ed'/%3E%3C/svg%3E";
}
thumbnail.onerror = () => { thumbnail.style.opacity = "0.35"; };
button.append(thumbnail, el("span", `@image${index}`));
const rememberPromptSelection = () => {
if (!prompt) return;
promptSelectionStart = prompt.selectionStart ?? prompt.value.length;
promptSelectionEnd = prompt.selectionEnd ?? promptSelectionStart;
};
button.onpointerdown = (event) => {
rememberPromptSelection();
event.preventDefault();
event.stopPropagation();
};
button.onmousedown = (event) => {
event.preventDefault();
event.stopPropagation();
};
button.onclick = (event) => {
event.preventDefault(); event.stopPropagation();
if (!prompt) return;
const start = Math.min(promptSelectionStart, prompt.value.length);
const end = Math.min(Math.max(start, promptSelectionEnd), prompt.value.length);
const token = `@R${index} `;
prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end);
const caret = start + token.length;
focusPrompt(); prompt.setSelectionRange(caret, caret);
promptSelectionStart = caret; promptSelectionEnd = caret;
promptText = prompt.value; clip.prompt = prompt.value;
syncPrompt(clip, clip.prompt); persistState();
};
referenceRow.append(button);
});
}
const refModsConnected = node.inputs?.some((input) => input.name === "ref_mods" && input.link != null);
const firstRefModSlot = Math.max(0, ...connectedReferences);
const connectedRefModSlots = Array.from({ length: 9 }, (_, offset) => offset + 1)
.filter((index) => node.inputs?.some((input) => input.name === `ref_mod_${index}` && input.link != null));
const refModDescriptors = connectedRefModSlots.flatMap((index) => refModDescriptorsForNode(node, `ref_mod_${index}`));
const refModsConnected = connectedRefModSlots.length > 0;
const insertReferenceToken = (slot) => {
if (!prompt) return;
const start = Math.min(promptSelectionStart, prompt.value.length);
@@ -515,7 +462,7 @@ function configure(node) {
syncPrompt(clip, clip.prompt); persistState();
};
refModDescriptors.forEach((descriptor, offset) => {
const slot = firstRefModSlot + offset + 1;
const slot = offset + 1;
const button = el("button"); button.type = "button";
button.title = `Insert ${descriptor.name} as @R${slot}`;
button.style.cssText = "display:inline-flex;align-items:center;gap:7px;height:31px;padding:2px 11px 2px 3px;border:2px solid #b78cff;border-radius:18px;background:#2a2138;color:#d9c2ff;font:14px monospace;cursor:pointer;white-space:nowrap;max-width:260px;overflow:hidden;text-overflow:ellipsis";
@@ -531,14 +478,8 @@ function configure(node) {
button.onclick = (event) => { event.preventDefault(); event.stopPropagation(); insertReferenceToken(slot); };
referenceRow.append(button);
});
if (refModsConnected && !refModDescriptors.length) {
const status = el("div"); status.title = "RefMods are connected to the prompt builder and are assigned after image reference slots.";
status.style.cssText = "display:inline-flex;align-items:center;gap:7px;height:31px;padding:2px 11px 2px 5px;border:2px solid #b78cff;border-radius:18px;background:#2a2138;color:#d9c2ff;font:13px monospace;white-space:nowrap";
const icon = el("span", "R"); icon.style.cssText = "display:grid;place-items:center;width:25px;height:25px;border-radius:50%;background:#7952a8;color:#fff;font-weight:bold;font-size:12px";
status.append(icon, el("span", `RefMods → @R${firstRefModSlot + 1}`)); referenceRow.append(status);
}
if (!connectedReferences.length && !refModsConnected) {
const empty = el("span", "Connect image references or RefMods to populate this row."); empty.style.cssText = "color:#77817a;font:11px Arial;padding:3px 2px"; referenceRow.append(empty);
if (!refModsConnected) {
const empty = el("span", "Connect RefMod sockets to populate this row."); empty.style.cssText = "color:#77817a;font:11px Arial;padding:3px 2px"; referenceRow.append(empty);
}
panel.append(referenceRow);
const promptLabel = el("div", "PROMPT SCRIPT"); promptLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:4px 0 3px"; panel.append(promptLabel);
@@ -552,33 +493,12 @@ function configure(node) {
autocomplete = null;
autocompleteRange = null;
};
const sourceThumbnail = (index) => {
const thumbnail = document.createElement("img");
thumbnail.alt = ""; thumbnail.draggable = false;
thumbnail.style.cssText = "width:24px;height:24px;object-fit:cover;border-radius:50%;background:#68727a;display:block;flex:none";
const linkId = node.inputs?.find((input) => input.name === `reference_image_${index}`)?.link;
const link = linkId == null ? null : (app.graph?.links?.get?.(linkId) ?? app.graph?.links?.[linkId]);
const source = link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id);
const imageWidget = source?.widgets?.find((widget) => ["image", "filename", "file"].includes(widget.name));
const imageValue = imageWidget?.value;
if (imageValue && typeof imageValue === "object" && imageValue.filename) {
const params = new URLSearchParams({ filename: imageValue.filename, type: imageValue.type || "input", subfolder: imageValue.subfolder || "" });
thumbnail.src = `/view?${params.toString()}`;
} else if (typeof imageValue === "string" && imageValue) {
const params = new URLSearchParams({ filename: imageValue, type: "input", subfolder: "" });
thumbnail.src = `/view?${params.toString()}`;
}
thumbnail.onerror = () => { thumbnail.style.opacity = "0.35"; };
return thumbnail;
};
const openAutocomplete = (at) => {
closeAutocomplete();
autocompleteRange = { start: at, end: at + 1 };
const menu = el("div");
menu.style.cssText = "display:block;visibility:visible;opacity:1;position:absolute;z-index:100;left:4px;min-width:220px;max-width:310px;padding:5px;background:#25282b;border:1px solid #4b5960;border-radius:8px;box-shadow:0 8px 24px #000b;color:#e7edf0;pointer-events:auto";
menu.style.top = "0px";
const connected = Array.from({ length: 9 }, (_, offset) => offset + 1)
.filter((index) => node.inputs?.some((input) => input.name === `reference_image_${index}` && input.link != null));
const addOption = (content, title, action) => {
const option = el("button"); option.type = "button"; option.title = title;
option.style.cssText = "display:flex;align-items:center;gap:9px;width:100%;padding:6px 8px;border:0;border-radius:5px;background:transparent;color:#e7edf0;text-align:left;font:12px monospace;cursor:pointer";
@@ -588,23 +508,8 @@ function configure(node) {
option.onclick = (event) => { event.preventDefault(); event.stopPropagation(); action(); };
option.append(...content); menu.append(option);
};
connected.forEach((index) => addOption([
sourceThumbnail(index),
el("span", `@image${index}`),
(() => { const hint = el("span", `<Picture ${index}>`); hint.style.cssText = "margin-left:auto;color:#84939a;font-size:10px"; return hint; })(),
], `Insert @R${index}`, () => {
const start = autocompleteRange?.start ?? prompt.value.length;
const end = autocompleteRange?.end ?? start;
const token = `@R${index} `;
prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end);
const caret = start + token.length;
focusPrompt(); prompt.setSelectionRange(caret, caret);
promptSelectionStart = caret; promptSelectionEnd = caret;
promptText = prompt.value; clip.prompt = prompt.value;
syncPrompt(clip, clip.prompt); persistState(); closeAutocomplete();
}));
refModDescriptors.forEach((descriptor, offset) => {
const slot = firstRefModSlot + offset + 1;
const slot = offset + 1;
addOption([
(() => { const icon = el("span", "R"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:50%;background:#7952a8;color:#fff;font-weight:bold"; return icon; })(),
el("span", `@R${slot} ${descriptor.name}`),