Add dynamic RefMod prompt chips

This commit is contained in:
2026-10-20 15:42:42 +00:00
parent 9887511646
commit e45f69ecb8
+96 -2
View File
@@ -34,6 +34,51 @@ const CAMERA_HELPERS = [
const get = (node, name) => node.widgets?.find((widget) => widget.name === name);
const el = (tag, text = "") => { const item = document.createElement(tag); item.textContent = text; return item; };
function linkedNode(node, inputName) {
const input = node.inputs?.find((item) => item.name === inputName);
if (input?.link == null) return null;
const link = app.graph?.links?.get?.(input.link) ?? app.graph?.links?.[input.link];
return link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id);
}
function widgetValue(node, name) {
return get(node, name)?.value;
}
function refModDescriptorsForNode(node) {
const source = linkedNode(node, "ref_mods");
if (!source) return [];
const className = String(source.comfyClass || source.type || "");
const result = [];
const add = (name, copies = 1) => {
const count = Math.max(1, Math.min(32, Number(copies) || 1));
for (let index = 0; index < count; index += 1) {
result.push({ name: String(name || `RefMod ${result.length + 1}`), source });
}
};
// The loader exposes the selected bundle rows as mod_1..mod_8 and copies.
if (className.includes("RefModsLoader")) {
for (let index = 1; index <= 8; index += 1) {
const name = widgetValue(source, `mod_${index}`);
const strength = Number(widgetValue(source, `strength_${index}`) ?? 1);
if (name && name !== "(none)" && strength > 0) add(name, widgetValue(source, `copies_${index}`));
}
// The picker loader appends one selected RefMod to an existing bundle.
} else if (className.includes("H3RefModLoader")) {
const name = widgetValue(source, "mod");
const weight = Number(widgetValue(source, "video_weight") ?? widgetValue(source, "weight") ?? 1);
if (name && name !== "(none)" && weight > 0) add(name, Math.ceil(weight));
} else if (className.includes("RefModExtract") || className.includes("RefModCreate")) {
add(widgetValue(source, "name") || "Created RefMod");
} else {
// A bundle can also come from a custom/bridge node whose contents are
// only known when the graph executes. Keep the UI usable and reserve
// one slot rather than hiding the connection completely.
add("Attached RefMod");
}
return result;
}
function defaultState(count, seconds) {
return { version: 1, clips: Array.from({ length: count }, () => ({
duration: seconds, slots: [{ start: 0, end: seconds, text: "" }], prompt: "",
@@ -390,6 +435,7 @@ function configure(node) {
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 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) {
@@ -447,12 +493,42 @@ function configure(node) {
});
}
const refModsConnected = node.inputs?.some((input) => input.name === "ref_mods" && input.link != null);
const firstRefModSlot = Math.max(0, ...connectedReferences);
const insertReferenceToken = (slot) => {
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${slot} `;
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();
};
refModDescriptors.forEach((descriptor, offset) => {
const slot = firstRefModSlot + 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";
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;flex:none";
button.append(icon, el("span", `@R${slot} ${descriptor.name}`));
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(); insertReferenceToken(slot); };
referenceRow.append(button);
});
if (refModsConnected) {
const firstRefModSlot = Math.max(0, ...connectedReferences);
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);
const endSlot = firstRefModSlot + Math.max(1, refModDescriptors.length);
status.append(icon, el("span", `RefMods → @R${firstRefModSlot + 1}${endSlot === firstRefModSlot + 1 ? "" : `–@R${endSlot}`}`)); 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);
@@ -520,6 +596,24 @@ function configure(node) {
promptText = prompt.value; clip.prompt = prompt.value;
syncPrompt(clip, clip.prompt); persistState(); closeAutocomplete();
}));
refModDescriptors.forEach((descriptor, offset) => {
const slot = firstRefModSlot + 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}`),
(() => { const hint = el("span", "<RefMod>"); hint.style.cssText = "margin-left:auto;color:#c9a9f3;font-size:10px"; return hint; })(),
], `Insert ${descriptor.name} as @R${slot}`, () => {
const start = autocompleteRange?.start ?? prompt.value.length;
const end = autocompleteRange?.end ?? start;
const token = `@R${slot} `;
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();
});
});
addOption([
(() => { const icon = el("span", "S"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:50%;background:#397a91;color:#dff7ff;font-weight:bold"; return icon; })(),
el("span", "[Shot " + (clip.slots.length + 1) + "]"),