Update js/sequence_prompt_builder.js

This commit is contained in:
2026-11-12 10:39:59 +00:00
parent 509a1e95dd
commit 8549283b75
+135 -68
View File
@@ -3,7 +3,7 @@ import { api } from "../../scripts/api.js";
const CLASS = "DumasH3SequencePromptBuilder";
// Check the browser console for this line to confirm the patched file loaded.
console.info("[DUMAS H3] Prompt Builder v3 (reference thumbnails + autocomplete v2) loaded");
console.info("[DUMAS H3] Prompt Builder v4 (live reference previews) loaded");
const MAX_CLIPS = 16;
const FIXED_TEXT_WIDGETS = [
["shared_directive", 84],
@@ -230,8 +230,9 @@ function textareaCaretRect(textarea, position) {
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);
const graph = node.graph || app.graph;
const link = graph?.links?.get?.(input.link) ?? graph?.links?.[input.link];
return link?.origin_id == null ? null : graph?.getNodeById?.(link.origin_id);
}
function widgetValue(node, name) {
@@ -239,9 +240,8 @@ function widgetValue(node, name) {
}
// Image thumbnails are a UI-only feature. A LoadImage node exposes its current
// file via its image widget; many generated-image nodes expose a preview after
// their first execution. No IMAGE tensors are serialized or sent through HTTP.
// Browser-visible previews are deliberately UI-only; the actual IMAGE values
// are still provided by ComfyUI's IMAGE sockets. Tensor data itself is not a URL.
function imageViewUrl(image) {
if (!image?.filename) return null;
const params = new URLSearchParams();
@@ -251,55 +251,79 @@ function imageViewUrl(image) {
return api.apiURL(`/view?${params.toString()}`);
}
function imageWidgetRef(value) {
if (value && typeof value === "object" && value.filename) {
return {
function imageWidgetRef(value, defaultType = "input") {
if (value && typeof value === "object") {
if (value.filename) return {
filename: String(value.filename),
type: String(value.type || "input"),
type: String(value.type || defaultType),
subfolder: String(value.subfolder || ""),
};
if (typeof value.value === "string") return imageWidgetRef(value.value, defaultType);
return null;
}
if (typeof value !== "string" || !value.trim()) return null;
let name = value.trim();
const annotation = name.match(/\s+\[(input|output|temp)\]$/i);
const annotation = name.match(/\s*\[(input|output|temp)\]$/i);
if (annotation) name = name.slice(0, -annotation[0].length);
const type = annotation ? annotation[1].toLowerCase() : "input";
// Newer ComfyUI asset IDs are hashes and must remain intact.
const type = annotation ? annotation[1].toLowerCase() : defaultType;
if (/^(?:https?:|blob:|data:)/i.test(name)) return { url: name };
if (/^(?:blake3|sha256):/i.test(name)) return { filename: name, type };
name = name.replace(/\\/g, "/");
const separator = name.lastIndexOf("/");
// Do not expose local paths outside ComfyUI's image directories.
if (/^[a-z]:\//i.test(name) || name.startsWith("/")) return null;
const slash = name.lastIndexOf("/");
return {
filename: separator < 0 ? name : name.slice(separator + 1),
subfolder: separator < 0 ? "" : name.slice(0, separator),
filename: slash < 0 ? name : name.slice(slash + 1),
subfolder: slash < 0 ? "" : name.slice(0, slash),
type,
};
}
function imagePreviewForNode(source) {
if (!source) return null;
// Prefer the currently selected loader widget to avoid showing an older
// source.imgs preview after the user changes a LoadImage selection.
const widgetImage = widgetValue(source, "image");
function imagePreviewCandidates(source) {
if (!source) return [];
const candidates = [];
const add = (url) => {
if (typeof url === "string" && url && !candidates.includes(url)) candidates.push(url);
};
const addRef = (value, type) => {
const ref = imageWidgetRef(value, type);
if (ref?.url) add(ref.url);
else if (ref?.filename) add(imageViewUrl(ref));
};
const className = String(source.comfyClass || source.type || "");
if (widgetImage != null && (/load.?image/i.test(className) || /image.?loader/i.test(className))) {
const url = imageViewUrl(imageWidgetRef(widgetImage));
if (url) return url;
const isOutputLoader = /LoadImageOutput|image.?output/i.test(className);
const imageWidget = get(source, "image");
// File-backed image loaders have an authoritative filename available before
// running the workflow. This takes priority over stale preview images.
if (imageWidget && (typeof imageWidget.value === "string" || imageWidget.value?.filename || imageWidget.value?.value)) {
addRef(imageWidget.value, isOutputLoader ? "output" : "input");
}
const img = source.imgs?.[source.imageIndex ?? 0] || source.imgs?.[0];
if (typeof img?.src === "string" && img.src) return img.src;
if (source.images?.[0]?.filename) return imageViewUrl(source.images[0]);
const saved = app.nodeOutputs?.[source.id]?.images?.[0]
|| app.nodeOutputs?.get?.(source.id)?.images?.[0];
if (saved?.filename) return imageViewUrl(saved);
// Some custom input-image nodes have an `image` widget but no preview yet.
if (widgetImage != null) return imageViewUrl(imageWidgetRef(widgetImage));
return null;
// Reuse images that the source node is already drawing on ComfyUI's canvas.
// This works for many other nodes that have no 'image' filename widget.
const images = source.imgs || source.previewImages || [];
if (images?.length) {
const selected = images[source.imageIndex ?? 0] || images[0];
add(selected?.src || selected?.currentSrc);
if (selected?.filename) addRef(selected, "output");
}
// Results from upstream generation/processing nodes after execution.
for (const item of source.images || []) if (item?.filename) add(imageViewUrl(item));
const result = app.nodeOutputs?.[source.id]
|| app.nodeOutputs?.get?.(source.id)
|| app.nodeOutputStore?.getNodeOutputs?.(source);
for (const item of result?.images || []) if (item?.filename) add(imageViewUrl(item));
// Some custom file loader nodes call their selector 'filename'.
if (!candidates.length && /image|photo|picture/i.test(className)) {
const fileWidget = get(source, "filename") || get(source, "file_name");
if (fileWidget) addRef(fileWidget.value, isOutputLoader ? "output" : "input");
}
return candidates;
}
function connectedReferenceImageUrl(node, slot) {
function connectedReferenceImageCandidates(node, slot) {
const visited = new Set();
let source = linkedNode(node, `reference_image_${slot}`);
for (let depth = 0; source && depth < 12; depth += 1) {
for (let depth = 0; source && depth < 16; depth += 1) {
const identity = source.id ?? source;
if (visited.has(identity)) break;
visited.add(identity);
@@ -308,9 +332,9 @@ function connectedReferenceImageUrl(node, slot) {
source = linkedNode(source, source.inputs?.[0]?.name || "");
continue;
}
return imagePreviewForNode(source);
return imagePreviewCandidates(source);
}
return null;
return [];
}
function refModDescriptorsForNode(node, inputName) {
@@ -417,8 +441,8 @@ function configure(node) {
let active = 0;
let state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4);
let promptText = null;
// Bind thumbnail widgets to live upstream previews. A cheap refresh also
// picks up changed LoadImage selections and new previews after execution.
// Keep the reference card image visible even when not hovered. Each card
// and autocomplete row has its own <img>, sharing the same upstream URL.
const referenceThumbBindings = new Set();
let hoverPreview = null;
const removeHoverPreview = () => { hoverPreview?.remove(); hoverPreview = null; };
@@ -427,68 +451,107 @@ function configure(node) {
if (!binding.url || binding.failed) return;
const bubble = el("div");
bubble.className = "dumas-h3-reference-hover-preview";
bubble.style.cssText = "position:fixed;z-index:2147483645;width:180px;padding:6px;border:1px solid #9a77c9;border-radius:10px;background:#20212a;box-shadow:0 12px 30px #000d;pointer-events:none;box-sizing:border-box";
bubble.style.cssText = "position:fixed;z-index:2147483645;width:210px;padding:6px;border:1px solid #9a77c9;border-radius:10px;background:#20212a;box-shadow:0 12px 30px #000d;pointer-events:none;box-sizing:border-box";
const image = el("img");
image.src = binding.url;
image.alt = `Reference image ${binding.slot}`;
image.style.cssText = "display:block;width:100%;height:168px;object-fit:contain;background:#111318;border-radius:6px";
image.style.cssText = "display:block;width:100%;height:198px;object-fit:contain;background:#111318;border-radius:6px";
bubble.append(image);
document.body.append(bubble);
const bounds = element.getBoundingClientRect();
let left = bounds.right + 8;
if (left + 180 > window.innerWidth - 8) left = bounds.left - 188;
left = Math.max(8, left);
const top = Math.min(Math.max(8, bounds.top - 48), Math.max(8, window.innerHeight - 192));
bubble.style.left = `${left}px`;
bubble.style.top = `${top}px`;
if (left + 210 > window.innerWidth - 8) left = bounds.left - 218;
bubble.style.left = `${Math.max(8, left)}px`;
bubble.style.top = `${Math.min(Math.max(8, bounds.top - 48), Math.max(8, window.innerHeight - 224))}px`;
hoverPreview = bubble;
};
const refreshReferenceThumbs = () => {
const urlForSlot = new Map();
const choicesBySlot = new Map();
for (const binding of referenceThumbBindings) {
if (!binding.element.isConnected) {
referenceThumbBindings.delete(binding);
continue;
}
if (!urlForSlot.has(binding.slot)) {
urlForSlot.set(binding.slot, connectedReferenceImageUrl(node, binding.slot));
if (!choicesBySlot.has(binding.slot)) {
choicesBySlot.set(binding.slot, connectedReferenceImageCandidates(node, binding.slot));
}
const choices = choicesBySlot.get(binding.slot);
const signature = JSON.stringify(choices);
if (signature !== binding.signature) {
binding.signature = signature;
binding.choices = choices;
binding.index = 0;
binding.failed = false;
binding.url = null;
}
// New preview may appear after node execution without its graph
// changing. Retry a failed candidate after a short delay.
if (binding.failed && Date.now() - binding.lastAttempt < 6000) continue;
if (!binding.url || binding.failed) {
const url = binding.choices[binding.index];
if (!url) {
binding.image.removeAttribute("src");
binding.image.style.display = "none";
binding.fallback.style.display = "grid";
binding.element.title = `@R${binding.slot}: No browser-visible image preview available`;
continue;
}
binding.lastAttempt = Date.now();
binding.url = url;
binding.failed = false;
binding.image.style.display = "block";
binding.fallback.style.display = "none";
binding.image.src = url;
binding.element.title = `@R${binding.slot}: Image preview`;
}
const url = urlForSlot.get(binding.slot);
if (binding.url === url) continue;
binding.url = url;
binding.failed = false;
binding.image.style.display = url ? "block" : "none";
binding.fallback.style.display = url ? "none" : "grid";
if (url) binding.image.src = url;
else binding.image.removeAttribute("src");
}
};
const referenceThumbnail = (slot, size = 26) => {
const referenceThumbnail = (slot, size = 52) => {
const holder = el("span");
holder.className = "dumas-h3-reference-thumbnail";
holder.style.cssText = `display:inline-grid;place-items:center;position:relative;flex:none;width:${size}px;height:${size}px;overflow:hidden;background:#7952a8;border:1px solid #ac83e5;border-radius:5px;vertical-align:middle`;
holder.style.cssText = `display:inline-grid;box-sizing:border-box;place-items:center;position:relative;flex:none;width:${size}px;height:${size}px;overflow:hidden;background:#201b2b;border:1px solid #7952a8;border-radius:6px;vertical-align:middle`;
const fallback = el("span", "R");
fallback.style.cssText = "display:grid;place-items:center;width:100%;height:100%;font:700 12px ui-monospace,Consolas,monospace;color:#fff";
fallback.style.cssText = "display:grid;place-items:center;width:100%;height:100%;font:700 14px ui-monospace,Consolas,monospace;color:#d9c2ff";
const image = el("img");
image.alt = `Reference ${slot} thumbnail`;
image.draggable = false;
image.style.cssText = "display:none;width:100%;height:100%;object-fit:cover";
const binding = { slot, element: holder, fallback, image, url: null, failed: false };
image.style.cssText = "display:none;width:100%;height:100%;object-fit:contain;background:#15151b";
const binding = { slot, element: holder, fallback, image, choices: [], index: 0, url: null, failed: false, signature: "", lastAttempt: 0 };
image.onload = () => {
binding.failed = false;
image.style.display = "block";
fallback.style.display = "none";
};
image.onerror = () => {
// First try another candidate (e.g. an existing source.imgs image
// if a custom loader has an incompatible filename widget).
if (binding.index + 1 < binding.choices.length) {
binding.index += 1;
binding.url = null;
binding.failed = false;
refreshReferenceThumbs();
return;
}
binding.failed = true;
image.style.display = "none";
fallback.style.display = "grid";
holder.title = `@R${slot}: Preview unavailable; see Console for diagnostics`;
removeHoverPreview();
};
holder.addEventListener("mouseenter", () => showHoverPreview(holder, binding));
holder.addEventListener("mouseleave", removeHoverPreview);
holder.append(fallback, image);
referenceThumbBindings.add(binding);
// The DOM has not necessarily been attached when this is called.
// Initial refresh takes place after the row / popup is appended.
return holder;
};
const referenceThumbTimer = window.setInterval(refreshReferenceThumbs, 1500);
// Expose read-only diagnostics for unusual custom image loaders, without
// logging image URLs or changing ComfyUI's prompt/output format.
node.__dumasReferencePreviewStatus = () => Array.from({length: 9}, (_, i) => i + 1).map(slot => ({
slot, connected: !!node.inputs?.find(input => input.name === `reference_image_${slot}` && input.link != null),
candidateCount: connectedReferenceImageCandidates(node, slot).length,
sourceType: String(linkedNode(node, `reference_image_${slot}`)?.comfyClass || linkedNode(node, `reference_image_${slot}`)?.type || ""),
}));
// Selection is visual UI state only; shot timings remain in timeline_json.
const selectedShotByClip = new Map();
// Writing the hidden widget value is safe while a textarea owns focus.
@@ -926,7 +989,7 @@ function configure(node) {
panel.append(timelinePanel);
const referenceLabel = el("div", "REFERENCE IMAGE 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 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:70px;box-sizing:border-box";
const connectedReferenceSlots = Array.from({ length: 9 }, (_, offset) => offset + 1)
.filter((index) => node.inputs?.some((input) => input.name === `reference_image_${index}` && input.link != null));
const insertReferenceToken = (slot) => {
@@ -944,8 +1007,12 @@ function configure(node) {
connectedReferenceSlots.forEach((slot) => {
const button = el("button"); button.type = "button";
button.title = `Insert Reference Image ${slot} as @R${slot}`;
button.style.cssText = "display:inline-flex;align-items:center;gap:7px;height:36px;padding:2px 10px 2px 3px;border:1px solid #a57bda;border-radius:7px;background:#2a2138;color:#d9c2ff;font:12px ui-monospace,Consolas,monospace;cursor:pointer;white-space:nowrap;max-width:260px;overflow:hidden;text-overflow:ellipsis";
button.append(referenceThumbnail(slot, 29), el("span", `@R${slot} Reference Image ${slot}`));
button.style.cssText = "display:inline-flex;align-items:center;gap:9px;height:62px;min-width:180px;padding:3px 12px 3px 4px;border:1px solid #946bbe;border-radius:8px;background:#2a2138;color:#e5d4ff;font:12px ui-monospace,Consolas,monospace;cursor:pointer;white-space:nowrap;max-width:270px;overflow:hidden;text-overflow:ellipsis";
const labels = el("span"); labels.style.cssText = "display:flex;flex-direction:column;align-items:flex-start;gap:3px;min-width:0";
const refName = el("strong", `@R${slot}`); refName.style.cssText = "color:#f0e5ff;font-size:12px";
const refDescription = el("span", `Reference Image ${slot}`); refDescription.style.cssText = "color:#baa8d6;font-size:11px";
labels.append(refName, refDescription);
button.append(referenceThumbnail(slot, 52), labels);
const rememberPromptSelection = () => {
if (!prompt) return;
promptSelectionStart = prompt.selectionStart ?? prompt.value.length;
@@ -1045,7 +1112,7 @@ function configure(node) {
};
connectedReferenceSlots.forEach((slot) => {
addOption([
referenceThumbnail(slot, 26),
referenceThumbnail(slot, 36),
el("span", `@R${slot} Reference Image ${slot}`),
(() => { const hint = el("span", "Image"); hint.style.cssText = "margin-left:auto;color:#9bd0a8;font-size:10px"; return hint; })(),
], `Insert Reference Image ${slot} as @R${slot}`, () => {