diff --git a/js/sequence_prompt_builder.js b/js/sequence_prompt_builder.js index dedcd41..89a98cf 100644 --- a/js/sequence_prompt_builder.js +++ b/js/sequence_prompt_builder.js @@ -1,8 +1,9 @@ import { app } from "../../scripts/app.js"; +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 autocomplete v2 (input-triggered) loaded"); +console.info("[DUMAS H3] Prompt Builder v3 (reference thumbnails + autocomplete v2) loaded"); const MAX_CLIPS = 16; const FIXED_TEXT_WIDGETS = [ ["shared_directive", 84], @@ -237,6 +238,81 @@ function widgetValue(node, name) { return get(node, name)?.value; } + +// 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. +function imageViewUrl(image) { + if (!image?.filename) return null; + const params = new URLSearchParams(); + params.set("filename", String(image.filename)); + params.set("type", String(image.type || "output")); + if (image.subfolder) params.set("subfolder", String(image.subfolder)); + return api.apiURL(`/view?${params.toString()}`); +} + +function imageWidgetRef(value) { + if (value && typeof value === "object" && value.filename) { + return { + filename: String(value.filename), + type: String(value.type || "input"), + subfolder: String(value.subfolder || ""), + }; + } + if (typeof value !== "string" || !value.trim()) return null; + let name = value.trim(); + 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. + if (/^(?:blake3|sha256):/i.test(name)) return { filename: name, type }; + name = name.replace(/\\/g, "/"); + const separator = name.lastIndexOf("/"); + return { + filename: separator < 0 ? name : name.slice(separator + 1), + subfolder: separator < 0 ? "" : name.slice(0, separator), + 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"); + 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 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; +} + +function connectedReferenceImageUrl(node, slot) { + const visited = new Set(); + let source = linkedNode(node, `reference_image_${slot}`); + for (let depth = 0; source && depth < 12; depth += 1) { + const identity = source.id ?? source; + if (visited.has(identity)) break; + visited.add(identity); + const className = String(source.comfyClass || source.type || ""); + if (/reroute/i.test(className)) { + source = linkedNode(source, source.inputs?.[0]?.name || ""); + continue; + } + return imagePreviewForNode(source); + } + return null; +} + function refModDescriptorsForNode(node, inputName) { const result = []; const add = (name, copies = 1) => { @@ -341,6 +417,78 @@ 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. + const referenceThumbBindings = new Set(); + let hoverPreview = null; + const removeHoverPreview = () => { hoverPreview?.remove(); hoverPreview = null; }; + const showHoverPreview = (element, binding) => { + removeHoverPreview(); + 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"; + 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"; + 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`; + hoverPreview = bubble; + }; + const refreshReferenceThumbs = () => { + const urlForSlot = 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)); + } + 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 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`; + 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"; + 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.onerror = () => { + binding.failed = true; + image.style.display = "none"; + fallback.style.display = "grid"; + 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); // 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. @@ -495,6 +643,7 @@ function configure(node) { clip.prompt = formatPromptFromSlots(clip); }; const render = () => { + removeHoverPreview(); const count = Math.max(1, Math.min(MAX_CLIPS, Number(countWidget.value) || 1)); const currentSeconds = Number(secondsWidget.value) || DURATION_OPTIONS[6].seconds; const selectedDuration = DURATION_OPTIONS.reduce((best, option) => @@ -795,9 +944,8 @@ 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: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} Reference Image ${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}`)); const rememberPromptSelection = () => { if (!prompt) return; promptSelectionStart = prompt.selectionStart ?? prompt.value.length; @@ -812,6 +960,7 @@ function configure(node) { const empty = el("span", "Connect reference image sockets to populate this row."); empty.style.cssText = "color:#77817a;font:11px Arial;padding:3px 2px"; referenceRow.append(empty); } panel.append(referenceRow); + refreshReferenceThumbs(); const promptLabel = el("div", "PROMPT SCRIPT"); promptLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:4px 0 3px"; panel.append(promptLabel); prompt = el("textarea"); prompt.rows = 10; prompt.value = promptText; prompt.placeholder = "[Shot 1] At 00:00.000, describe the opening action...\n\nType @ at the start of a new line to add the next shot."; prompt.style.cssText = "width:100%;box-sizing:border-box;color:#e8e8e8;background:#171717;border:1px solid #4e4e4e;border-radius:4px;padding:8px;font:12px/1.45 monospace;margin-top:2px;resize:vertical;outline:none"; const rememberPromptSelection = () => { @@ -896,7 +1045,7 @@ function configure(node) { }; connectedReferenceSlots.forEach((slot) => { 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;flex:none"; return icon; })(), + referenceThumbnail(slot, 26), 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}`, () => { @@ -940,6 +1089,7 @@ function configure(node) { }); autocomplete = menu; document.body.append(menu); + refreshReferenceThumbs(); prompt.setAttribute("aria-controls", menu.id); prompt.setAttribute("aria-expanded", "true"); selectAutocompleteOption(0); @@ -1143,6 +1293,8 @@ function configure(node) { const previousRemoved = node.onRemoved; node.onRemoved = function(...args) { this.__dumasCloseAutocomplete?.(); + removeHoverPreview(); + window.clearInterval(referenceThumbTimer); return previousRemoved?.apply(this, args); }; node.__dumasSequencePromptBuilder = refresh;