1649 lines
87 KiB
JavaScript
1649 lines
87 KiB
JavaScript
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 v7 (compact clip editor) loaded");
|
||
const MAX_CLIPS = 16;
|
||
const FIXED_TEXT_WIDGETS = [
|
||
["shared_directive", 84],
|
||
["soundscape", 60],
|
||
["music", 60],
|
||
];
|
||
const DURATION_OPTIONS = [
|
||
["1–5", 5, 2, 0.21], ["6–22", 22, 7, 0.92], ["23–39", 39, 12, 1.63],
|
||
["40–56", 56, 17, 2.33], ["57–73", 73, 22, 3.04], ["74–90", 90, 27, 3.75],
|
||
["91–107", 107, 32, 4.46], ["108–124", 124, 37, 5.17], ["125–141", 141, 42, 5.88],
|
||
["142–158", 158, 47, 6.58], ["159–175", 175, 52, 7.29], ["176–192", 192, 57, 8.00],
|
||
["193–209", 209, 62, 8.71], ["210–226", 226, 67, 9.42], ["227–243", 243, 72, 10.13],
|
||
["244–260", 260, 77, 10.83], ["261–277", 277, 82, 11.54], ["278–294", 294, 87, 12.25],
|
||
["295–311", 311, 92, 12.96], ["312–328", 328, 97, 13.67], ["329–345", 345, 102, 14.38],
|
||
["346–362", 362, 107, 15.08],
|
||
].map(([requested, actual, latent, labelSeconds]) => ({
|
||
requested, actual, latent, labelSeconds, seconds: actual / 24,
|
||
}));
|
||
const CAMERA_HELPERS = [
|
||
["Push / Pull", ["The camera pushes in", "The camera pulls out"]],
|
||
["Zoom", ["The camera zooms in", "The camera zooms out"]],
|
||
["Pan", ["The camera pans left", "The camera pans right"]],
|
||
["Truck", ["The camera trucks left", "The camera trucks right"]],
|
||
["Tilt", ["The camera tilts up", "The camera tilts down"]],
|
||
["Pedestal", ["The camera pedestals up", "The camera pedestals down"]],
|
||
["Movement", ["The camera holds a static shot", "The camera arcs around the subject", "The camera tracks the subject"]],
|
||
["POV", ["The camera holds the subject's point of view"]],
|
||
["Roll", ["The camera rolls clockwise", "The camera rolls counterclockwise"]],
|
||
["Shake", ["The camera shakes slightly", "The camera shakes strongly"]],
|
||
];
|
||
|
||
// Scoped to this node's DOM widget; no ComfyUI global styles are changed.
|
||
function ensureTimelineStyles() {
|
||
if (document.getElementById("dumas-h3-shot-timeline-styles")) return;
|
||
const style = document.createElement("style");
|
||
style.id = "dumas-h3-shot-timeline-styles";
|
||
style.textContent = `
|
||
.dumas-h3-shot-timeline {
|
||
--h3-track-bg: #1b2022;
|
||
margin: 9px 0 10px;
|
||
padding: 10px;
|
||
border: 1px solid #404b4b;
|
||
border-radius:4px;
|
||
background: #222829;
|
||
color: #e5ebea;
|
||
font: 11px/1.35 Arial, sans-serif;
|
||
}
|
||
.dumas-h3-shot-timeline * { box-sizing: border-box; }
|
||
.dumas-h3-shot-timeline__heading,
|
||
.dumas-h3-shot-timeline__footer,
|
||
.dumas-h3-shot-timeline__selection {
|
||
display: flex; align-items: center; gap: 6px; min-width: 0;
|
||
}
|
||
.dumas-h3-shot-timeline__heading { justify-content: space-between; margin-bottom: 11px; }
|
||
.dumas-h3-shot-timeline__heading strong {
|
||
font-size: 11px; font-weight: 700; letter-spacing: .07em;
|
||
}
|
||
.dumas-h3-shot-timeline__meta {
|
||
color: #9fadad; font: 10px/1.3 ui-monospace, Consolas, monospace; white-space: nowrap;
|
||
}
|
||
.dumas-h3-shot-timeline__ruler {
|
||
position: relative; height: 23px; margin: 0 6px;
|
||
border-bottom: 1px solid #445052;
|
||
background: repeating-linear-gradient(to right,
|
||
transparent 0, transparent calc(5% - 1px), #394547 calc(5% - 1px), #394547 5%);
|
||
background-size: 100% 7px; background-repeat: no-repeat; background-position: left bottom;
|
||
}
|
||
.dumas-h3-shot-timeline__tick {
|
||
position: absolute; bottom: 8px; transform: translateX(-50%);
|
||
color: #9caaab; font: 10px ui-monospace, Consolas, monospace;
|
||
white-space: nowrap; user-select: none;
|
||
}
|
||
.dumas-h3-shot-timeline__tick:first-child { transform: none; }
|
||
.dumas-h3-shot-timeline__tick:last-child { transform: translateX(-100%); }
|
||
.dumas-h3-shot-timeline__track {
|
||
position: relative; height: 62px; margin: 7px 6px 0;
|
||
background: var(--h3-track-bg); border: 1px solid #3a4548;
|
||
border-radius:4px; overflow: visible;
|
||
}
|
||
.dumas-h3-shot-timeline__segment {
|
||
position: absolute; display: flex; flex-direction: column;
|
||
align-items: stretch; justify-content: center;
|
||
gap: 4px; top: 0; height: 100%; min-width: 0; margin: 0;
|
||
padding: 6px 8px; color: #e6edeb; text-align: left;
|
||
border: 0; border-top: 3px solid var(--shot-accent, #7aaea2);
|
||
border-right: 1px solid #192022; border-radius: 2px;
|
||
background: color-mix(in srgb, var(--shot-accent, #7aaea2) 15%, #253032);
|
||
cursor: pointer; overflow: hidden; font: inherit; outline: none;
|
||
transition: background .12s ease, box-shadow .12s ease;
|
||
}
|
||
.dumas-h3-shot-timeline__segment:hover {
|
||
background: color-mix(in srgb, var(--shot-accent, #7aaea2) 24%, #253032);
|
||
}
|
||
.dumas-h3-shot-timeline__segment.is-selected {
|
||
background: color-mix(in srgb, var(--shot-accent, #7aaea2) 29%, #253032);
|
||
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--shot-accent, #7aaea2) 65%, #253032);
|
||
}
|
||
.dumas-h3-shot-timeline__segment:focus-visible {
|
||
outline: 2px solid #dce9e5; outline-offset: -3px;
|
||
}
|
||
.dumas-h3-shot-timeline__shot-head {
|
||
display: flex; align-items: center; gap: 6px; white-space: nowrap;
|
||
}
|
||
.dumas-h3-shot-timeline__shot-number {
|
||
display: inline-grid; place-items: center; width: 20px; min-width: 20px; height: 19px;
|
||
color: #131b1d; background: var(--shot-accent, #7aaea2);
|
||
border-radius: 4px; font: 700 10px ui-monospace, Consolas, monospace;
|
||
}
|
||
.dumas-h3-shot-timeline__shot-name { font-size: 11px; font-weight: 700; }
|
||
.dumas-h3-shot-timeline__description {
|
||
display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis;
|
||
white-space: nowrap; color: #c8d4d3; font-size: 10px;
|
||
}
|
||
.dumas-h3-shot-timeline__handle {
|
||
position: absolute; z-index: 10; top: -4px; bottom: -4px;
|
||
width: 20px; padding: 0; margin: 0; transform: translateX(-50%);
|
||
border: 0; background: transparent; cursor: ew-resize;
|
||
touch-action: none; outline: none;
|
||
}
|
||
.dumas-h3-shot-timeline__handle::before {
|
||
content: ""; position: absolute; top: 3px; bottom: 3px;
|
||
width: 9px; left: 50%; transform: translateX(-50%);
|
||
border: 1px solid #879b9b; border-radius:4px;
|
||
background: #d3dfdc; box-shadow: 0 1px 5px #0009;
|
||
}
|
||
.dumas-h3-shot-timeline__handle::after {
|
||
content: ""; position: absolute; top: 19px; bottom: 19px;
|
||
width: 2px; left: 50%; transform: translateX(-50%);
|
||
border-radius: 2px; background: #637777;
|
||
box-shadow: -3px 0 #637777, 3px 0 #637777;
|
||
}
|
||
.dumas-h3-shot-timeline__handle:hover::before,
|
||
.dumas-h3-shot-timeline__handle.is-dragging::before,
|
||
.dumas-h3-shot-timeline__handle:focus-visible::before {
|
||
background: #ffffff; border-color: #ffffff;
|
||
box-shadow: 0 0 0 3px #c6ede633, 0 2px 8px #000b;
|
||
}
|
||
.dumas-h3-shot-timeline__bubble {
|
||
position: absolute; left: 50%; bottom: calc(100% + 6px);
|
||
transform: translateX(-50%); padding: 3px 6px;
|
||
border-radius: 4px; background: #e4eeeb; color: #142123;
|
||
font: 700 10px ui-monospace, Consolas, monospace;
|
||
white-space: nowrap; box-shadow: 0 2px 7px #0009;
|
||
pointer-events: none; opacity: 0; transition: opacity .1s ease;
|
||
}
|
||
.dumas-h3-shot-timeline__handle:hover .dumas-h3-shot-timeline__bubble,
|
||
.dumas-h3-shot-timeline__handle:focus-visible .dumas-h3-shot-timeline__bubble,
|
||
.dumas-h3-shot-timeline__handle.is-dragging .dumas-h3-shot-timeline__bubble {
|
||
opacity: 1;
|
||
}
|
||
.dumas-h3-shot-timeline__footer {
|
||
justify-content: space-between; flex-wrap: wrap;
|
||
margin-top: 9px; min-height: 24px; gap: 5px 10px;
|
||
}
|
||
.dumas-h3-shot-timeline__selection {
|
||
padding: 4px 7px; border: 1px solid #435053;
|
||
border-radius:4px; background: #1b2223; white-space: nowrap;
|
||
font: 10px ui-monospace, Consolas, monospace;
|
||
}
|
||
.dumas-h3-shot-timeline__selected-dot {
|
||
flex: 0 0 7px; width: 7px; height: 7px; border-radius:4px;
|
||
background: var(--selected-color, #7aaea2);
|
||
}
|
||
.dumas-h3-shot-timeline__selection strong { color: #e5ebea; }
|
||
.dumas-h3-shot-timeline__selection span { color: #aababb; }
|
||
.dumas-h3-shot-timeline__hint { color: #839698; font-size: 10px; }
|
||
.dumas-h3-shot-timeline__empty {
|
||
display: flex; align-items: center; justify-content: center;
|
||
height: 100%; color: #95a4a5; font-size: 11px;
|
||
}
|
||
/* Clip editor: scoped to the DUMAS node DOM widget, not ComfyUI itself. */
|
||
.dumas-h3-clip-tabs {
|
||
display:flex; flex-wrap:wrap; align-items:center; gap:4px;
|
||
border-bottom:1px solid #48504d; padding:0 0 6px;
|
||
margin:0 0 9px;
|
||
}
|
||
.dumas-h3-clip-tab {
|
||
padding:5px 11px; min-height:26px;
|
||
border:1px solid #525b56; border-radius:3px;
|
||
background:#303332; color:#bec8c1;
|
||
font:600 11px Arial,sans-serif; text-align:center;
|
||
cursor:pointer;
|
||
}
|
||
.dumas-h3-clip-tab:hover:not(:disabled) {
|
||
background:#3c4640; color:#eef5ef; border-color:#718a79;
|
||
}
|
||
.dumas-h3-clip-tab.is-active {
|
||
border-color:#78bd8c; background:#355643;
|
||
color:#f3fff5; cursor:default;
|
||
}
|
||
.dumas-h3-clip-tab.is-disabled {
|
||
color:#a9a2a2; border-color:#625253;
|
||
background:#312d2e;
|
||
}
|
||
.dumas-h3-clip-tab.is-active.is-disabled {
|
||
color:#f3dddd; border-color:#a47777; background:#513a3c;
|
||
}
|
||
.dumas-h3-chunk-duration {
|
||
display:flex; align-items:center; gap:8px;
|
||
margin:0 0 7px;
|
||
}
|
||
.dumas-h3-chunk-duration__label {
|
||
flex:none; color:#c9d6cf; font:600 11px Arial,sans-serif;
|
||
white-space:nowrap;
|
||
}
|
||
.dumas-h3-chunk-duration__select {
|
||
flex:1; min-width:0; height:27px; padding:4px 24px 4px 7px;
|
||
border:1px solid #52635a; border-radius:3px;
|
||
background:#1c1f1d; color:#e7eee9;
|
||
font:11px ui-monospace,Consolas,monospace;
|
||
}
|
||
.dumas-h3-chunk-duration__select:focus-visible {
|
||
outline:2px solid #87c89a; outline-offset:1px;
|
||
}
|
||
.dumas-h3-clip-status {
|
||
display:flex; flex-wrap:wrap; align-items:center;
|
||
gap:5px 7px; min-height:30px;
|
||
padding:1px 0 6px; color:#b6c8bd;
|
||
font:11px Arial,sans-serif;
|
||
}
|
||
.dumas-h3-clip-status__title { font-weight:700; color:#dbe7df; }
|
||
.dumas-h3-clip-status__meta { color:#a9b9af; min-width:0; }
|
||
.dumas-h3-clip-enable {
|
||
margin-left:auto; display:inline-flex; align-items:center; gap:8px;
|
||
flex:none; min-height:27px; padding:4px 9px;
|
||
border:1px solid #5ca976; border-radius:3px;
|
||
color:#d9f3df; background:#23402d;
|
||
font:700 11px Arial,sans-serif; cursor:pointer;
|
||
user-select:none;
|
||
}
|
||
.dumas-h3-clip-enable.is-off {
|
||
border-color:#9b6868; color:#f1d0d0; background:#4b3435;
|
||
}
|
||
.dumas-h3-clip-enable:focus-within {
|
||
outline:2px solid #cce9d6; outline-offset:1px;
|
||
}
|
||
.dumas-h3-clip-enable__input {
|
||
appearance:none; -webkit-appearance:none;
|
||
position:relative; flex:none; width:30px; height:16px; margin:0;
|
||
border:1px solid #659472; border-radius:4px;
|
||
background:#152b1f; cursor:pointer; box-sizing:border-box;
|
||
}
|
||
.dumas-h3-clip-enable__input::before {
|
||
content:""; position:absolute; left:2px; top:2px;
|
||
width:10px; height:10px; border-radius:2px;
|
||
background:#b9c3bf; transition:transform .12s ease;
|
||
}
|
||
.dumas-h3-clip-enable__input:checked { background:#2c9b4f; border-color:#8add9a; }
|
||
.dumas-h3-clip-enable__input:checked::before {
|
||
transform:translateX(14px); background:#fff;
|
||
}
|
||
.dumas-h3-clip-enable__input:focus-visible { outline:2px solid #e8f7e9; outline-offset:2px; }
|
||
@media (max-width:510px) {
|
||
.dumas-h3-chunk-duration { flex-wrap:wrap; }
|
||
.dumas-h3-chunk-duration__select { flex-basis:100%; }
|
||
}
|
||
`;
|
||
document.head.append(style);
|
||
}
|
||
|
||
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; };
|
||
|
||
/**
|
||
* Resolve a textarea caret in viewport pixels, even when the ComfyUI DOM
|
||
* widget is zoomed/scaled and its text has wrapped or been scrolled.
|
||
* A mirror preserves the textarea's font, padding, line wrapping and tabs.
|
||
*/
|
||
function textareaCaretRect(textarea, position) {
|
||
const style = window.getComputedStyle(textarea);
|
||
const mirror = document.createElement("div");
|
||
const properties = [
|
||
"boxSizing", "fontFamily", "fontSize", "fontWeight", "fontStyle",
|
||
"fontVariant", "fontStretch", "lineHeight", "letterSpacing",
|
||
"textTransform", "textIndent", "textAlign", "direction",
|
||
"tabSize", "paddingTop", "paddingRight", "paddingBottom",
|
||
"paddingLeft", "borderTopWidth", "borderRightWidth",
|
||
"borderBottomWidth", "borderLeftWidth", "borderTopStyle",
|
||
"borderRightStyle", "borderBottomStyle", "borderLeftStyle",
|
||
];
|
||
mirror.style.cssText = "position:fixed;left:-100000px;top:0;visibility:hidden;pointer-events:none;white-space:pre-wrap;overflow-wrap:break-word;word-wrap:break-word;";
|
||
for (const property of properties) mirror.style[property] = style[property];
|
||
mirror.style.width = `${textarea.offsetWidth}px`;
|
||
mirror.style.height = "auto";
|
||
mirror.style.minHeight = "0";
|
||
mirror.style.maxHeight = "none";
|
||
mirror.style.overflow = "hidden";
|
||
const caret = document.createElement("span");
|
||
caret.textContent = "\u200b";
|
||
mirror.append(document.createTextNode(textarea.value.slice(0, position)), caret);
|
||
document.body.append(mirror);
|
||
const mirrorBox = mirror.getBoundingClientRect();
|
||
const caretBox = caret.getBoundingClientRect();
|
||
const textareaBox = textarea.getBoundingClientRect();
|
||
// CSS canvas zoom scales the DOM widget's rect but not its layout pixels.
|
||
const scaleX = textareaBox.width / Math.max(1, textarea.offsetWidth);
|
||
const scaleY = textareaBox.height / Math.max(1, textarea.offsetHeight);
|
||
const lineHeight = parseFloat(style.lineHeight) || parseFloat(style.fontSize) * 1.45 || 18;
|
||
const result = {
|
||
left: textareaBox.left + (caretBox.left - mirrorBox.left - textarea.scrollLeft) * scaleX,
|
||
top: textareaBox.top + (caretBox.top - mirrorBox.top - textarea.scrollTop) * scaleY,
|
||
height: lineHeight * scaleY,
|
||
};
|
||
mirror.remove();
|
||
result.bottom = result.top + result.height;
|
||
return result;
|
||
}
|
||
|
||
|
||
// LiteGraph uses graph._links (Map or plain object) in current ComfyUI;
|
||
// older builds and test harnesses may expose graph.links instead.
|
||
function graphLink(graph, linkId) {
|
||
if (!graph || linkId == null) return null;
|
||
try {
|
||
const fromGetter = graph.getLink?.(linkId);
|
||
if (fromGetter) return fromGetter;
|
||
} catch (_) { /* compatibility with older LiteGraph */ }
|
||
for (const links of [graph._links, graph.links]) {
|
||
const found = links?.get?.(linkId) ?? links?.[linkId];
|
||
if (found) return found;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
function linkedNode(node, inputName) {
|
||
const input = node.inputs?.find((item) => item.name === inputName);
|
||
if (input?.link == null) return null;
|
||
const graph = node.graph || app.graph;
|
||
const link = graphLink(graph, input.link);
|
||
return link?.origin_id == null ? null : graph?.getNodeById?.(link.origin_id);
|
||
}
|
||
|
||
// KJNodes GetNode/SetNode are *virtual* relays. A Get has an output wire
|
||
// but no matching input wire. Its Constant widget is paired to the same
|
||
// named SetNode's Constant, which DOES have an input wire. The methods
|
||
// below also support the KJNodes 2026 cross-subgraph naming scopes.
|
||
function virtualGetterSources(getter, trace) {
|
||
const type = String(getter?.type || getter?.comfyClass || "");
|
||
if (!/^(?:GetNode|GetNode\|.*|Get\b.*)$/i.test(type)) return [];
|
||
const name = String(getter.widgets?.find(w => w.name === "Constant" || w.name === "name")?.value
|
||
?? getter.widgets?.[0]?.value ?? "").trim();
|
||
if (!name) {
|
||
trace.push({id:getter.id, type: "Get (no variable selected)"});
|
||
return [];
|
||
}
|
||
const graph = getter.graph || app.graph;
|
||
let setter = null;
|
||
try { setter = getter.findSetter?.(graph) || getter.currentSetter || null; } catch (_) { /* fallback below */ }
|
||
// KJNodes' findSetter already handles parent graphs. Fall back to a
|
||
// local/parent name lookup when other packs implement the same convention.
|
||
if (!setter) {
|
||
const inspected = new Set();
|
||
const graphs = [graph];
|
||
const root = graph?.rootGraph || app.graph?.rootGraph || app.graph;
|
||
if (root && root !== graph) graphs.push(root);
|
||
let g = graph;
|
||
while (g?.parentGraph && !inspected.has(g)) {
|
||
inspected.add(g);
|
||
g = g.parentGraph;
|
||
graphs.push(g);
|
||
}
|
||
for (const candidateGraph of graphs) {
|
||
setter = candidateGraph?._nodes?.find(n =>
|
||
/^(?:SetNode|SetNode\|.*|Set\b.*)$/i.test(String(n.type || n.comfyClass || "")) &&
|
||
String(n.widgets?.find(w => w.name === "Constant" || w.name === "name")?.value
|
||
?? n.widgets?.[0]?.value ?? "").trim() === name
|
||
);
|
||
if (setter) break;
|
||
}
|
||
}
|
||
if (!setter) {
|
||
trace.push({id:getter.id, type:`Get \"${name}\" (Set not found)`});
|
||
return [];
|
||
}
|
||
trace.push({id:setter.id, type:`Set \"${name}\"`});
|
||
const inputs = (setter.inputs || []).filter(input => input?.link != null);
|
||
const preferred = inputs.filter(input => /IMAGE|\*/i.test(String(input.type || "")));
|
||
const followed = preferred.length ? preferred : inputs;
|
||
const upstream = followed.map(input => linkedNode(setter, input.name)).filter(Boolean);
|
||
// KJNodes GetNode exposes the already-resolved upstream link; use it
|
||
// when a custom Set node does not expose a conventional input socket.
|
||
if (!upstream.length) {
|
||
try {
|
||
const link = getter.getInputLink?.(0);
|
||
const resolved = link?.origin_id == null ? null :
|
||
(graph?.getNodeById?.(link.origin_id) || setter.graph?.getNodeById?.(link.origin_id));
|
||
if (resolved) upstream.push(resolved);
|
||
} catch (_) { /* no virtual link on this implementation */ }
|
||
}
|
||
return upstream;
|
||
}
|
||
function widgetValue(node, name) {
|
||
return get(node, name)?.value;
|
||
}
|
||
|
||
|
||
// 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();
|
||
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, defaultType = "input") {
|
||
if (value && typeof value === "object") {
|
||
if (value.filename) return {
|
||
filename: String(value.filename),
|
||
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);
|
||
if (annotation) name = name.slice(0, -annotation[0].length);
|
||
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, "/");
|
||
// 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: slash < 0 ? name : name.slice(slash + 1),
|
||
subfolder: slash < 0 ? "" : name.slice(0, slash),
|
||
type,
|
||
};
|
||
}
|
||
|
||
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 || "");
|
||
const isOutputLoader = /LoadImageOutput|image.?output/i.test(className);
|
||
// A selected filename works even before the source node has been executed.
|
||
const imageWidget = get(source, "image") || get(source, "image_file") || get(source, "input_image");
|
||
if (imageWidget) addRef(imageWidget.value, isOutputLoader ? "output" : "input");
|
||
|
||
// Native ComfyUI and many custom nodes expose a ready-to-draw image.
|
||
const imgs = source.imgs || source.previewImages || [];
|
||
if (Array.isArray(imgs) && imgs.length) {
|
||
const selected = imgs[source.imageIndex ?? 0] || imgs[0];
|
||
add(selected?.currentSrc || selected?.src);
|
||
if (selected?.filename) addRef(selected, "output");
|
||
if (typeof selected?.toDataURL === "function") {
|
||
try { add(selected.toDataURL("image/png")); } catch (_) { /* canvas may be tainted */ }
|
||
}
|
||
}
|
||
for (const item of source.images || []) {
|
||
if (item?.filename) add(imageViewUrl(item));
|
||
else add(item?.src || item?.currentSrc);
|
||
}
|
||
const output = app.nodeOutputs?.[source.id]
|
||
|| app.nodeOutputs?.get?.(source.id)
|
||
|| app.nodeOutputStore?.getNodeOutputs?.(source);
|
||
for (const item of output?.images || []) if (item?.filename) add(imageViewUrl(item));
|
||
for (const item of output?.dumas_reference_previews || []) if (item?.filename) add(imageViewUrl(item));
|
||
|
||
// Nodes with a custom loader widget may not call it 'image'.
|
||
for (const name of ["filename", "file_name", "image_path", "path"]) {
|
||
const field = get(source, name);
|
||
if (field && /load|image|photo|picture/i.test(className)) addRef(field.value, isOutputLoader ? "output" : "input");
|
||
}
|
||
return candidates;
|
||
}
|
||
|
||
// Resolve executed tensor previews and live upstream image loader previews.
|
||
// v6 handles virtual GetNode -> SetNode links and LiteGraph _links.
|
||
function connectedReferenceImageCandidates(node, slot, trace = []) {
|
||
const candidates = [];
|
||
const add = (url) => {
|
||
if (typeof url === "string" && url && !candidates.includes(url)) candidates.push(url);
|
||
};
|
||
const ownOutput = app.nodeOutputs?.[node.id]
|
||
|| app.nodeOutputs?.get?.(node.id)
|
||
|| app.nodeOutputStore?.getNodeOutputs?.(node);
|
||
for (const item of ownOutput?.dumas_reference_previews || []) {
|
||
if (Number(item.slot) === slot && item.filename) add(imageViewUrl(item));
|
||
}
|
||
const visited = new Set();
|
||
const first = linkedNode(node, `reference_image_${slot}`);
|
||
const queue = first ? [{source:first, depth:0}] : [];
|
||
while (queue.length && visited.size < 96) {
|
||
const {source, depth} = queue.shift();
|
||
// Node objects are unique even when nested graphs reuse numeric IDs.
|
||
if (!source || visited.has(source)) continue;
|
||
visited.add(source);
|
||
const className = String(source.comfyClass || source.type || "Unknown");
|
||
trace.push({id:source.id, type:className});
|
||
for (const url of imagePreviewCandidates(source)) add(url);
|
||
if (depth >= 20) continue;
|
||
|
||
// GetNode has no wire leading into it. Follow its matching SetNode
|
||
// (including KJNodes parent-subgraph scoping) then the Set input.
|
||
const virtualSources = virtualGetterSources(source, trace);
|
||
if (virtualSources.length) {
|
||
for (const upstream of virtualSources) queue.push({source:upstream, depth:depth+1});
|
||
continue;
|
||
}
|
||
if (/^GetNode$/i.test(className)) continue; // avoid an unrelated preview
|
||
|
||
const inputs = (source.inputs || []).filter(input => input?.link != null);
|
||
const imageInputs = inputs.filter(input =>
|
||
/image|any|\*/i.test(String(input.type || "")) ||
|
||
/image|pixels|input|source/i.test(String(input.name || "")) ||
|
||
/reroute|switch|pipe|relay|setnode/i.test(className)
|
||
);
|
||
const connectedInputs = imageInputs.length ? imageInputs :
|
||
(/image|crop|resize|scale|switch|reroute|pipe|setnode/i.test(className) ? inputs : []);
|
||
for (const input of connectedInputs) {
|
||
const upstream = linkedNode(source, input.name);
|
||
if (upstream) queue.push({source:upstream, depth:depth+1});
|
||
}
|
||
}
|
||
return candidates;
|
||
}
|
||
function refModDescriptorsForNode(node, inputName) {
|
||
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}`) });
|
||
}
|
||
};
|
||
const visited = new Set();
|
||
const visit = (source) => {
|
||
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.
|
||
if (className.includes("H3RefModLoader")) {
|
||
visit(linkedNode(source, "mods"));
|
||
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));
|
||
return;
|
||
}
|
||
// The loader exposes 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}`));
|
||
}
|
||
return;
|
||
}
|
||
if (className.includes("RefModExtract") || className.includes("RefModCreate")) {
|
||
add(widgetValue(source, "name") || "Created RefMod");
|
||
return;
|
||
}
|
||
// A custom bundle node's contents are only known after execution.
|
||
add("Attached RefMod");
|
||
};
|
||
visit(linkedNode(node, inputName));
|
||
return result;
|
||
}
|
||
|
||
function defaultState(count, seconds) {
|
||
return { version: 1, clips: Array.from({ length: count }, () => ({
|
||
duration: seconds, enabled: true, slots: [{ start: 0, end: seconds, text: "" }], prompt: "",
|
||
})) };
|
||
}
|
||
|
||
function readState(widget, count, seconds) {
|
||
try {
|
||
const value = JSON.parse(String(widget?.value || ""));
|
||
if (Array.isArray(value.clips)) return value;
|
||
} catch (_) { /* replace malformed editor state with a usable default */ }
|
||
return defaultState(count, seconds);
|
||
}
|
||
|
||
function hide(widget) {
|
||
if (!widget) return;
|
||
widget.type = "hidden"; widget.hidden = true; widget.options ||= {};
|
||
widget.options.hidden = true; widget.computeSize = () => [0, 0]; widget.draw = () => {};
|
||
}
|
||
|
||
function configure(node) {
|
||
if (node?.comfyClass !== CLASS || typeof document === "undefined" || typeof node.addDOMWidget !== "function") return;
|
||
if (node.__dumasSequencePromptBuilder) return node.__dumasSequencePromptBuilder();
|
||
const countWidget = get(node, "clip_count");
|
||
const secondsWidget = get(node, "default_chunk_seconds");
|
||
const stateWidget = get(node, "timeline_json");
|
||
if (!countWidget || !secondsWidget || !stateWidget) return;
|
||
ensureTimelineStyles();
|
||
hide(secondsWidget);
|
||
hide(stateWidget);
|
||
FIXED_TEXT_WIDGETS.forEach(([name, height]) => {
|
||
const widget = get(node, name);
|
||
if (!widget) return;
|
||
widget.options ||= {};
|
||
widget.options.multiline = true;
|
||
widget.__dumasFixedHeight = height;
|
||
widget.computeSize = (width) => [
|
||
Math.max(300, Number(width) || Number(node.size?.[0]) || 420),
|
||
height,
|
||
];
|
||
});
|
||
const panel = el("div");
|
||
panel.style.cssText = "box-sizing:border-box;position:relative;width:calc(100% + 14px);margin-left:-7px;min-width:420px;padding:9px;color:#d9e3dc;background:#292929;border:1px solid #454545;border-radius:4px;font:12px Arial;pointer-events:auto";
|
||
const host = el("div"); host.style.cssText = "box-sizing:border-box;pointer-events:auto;background:#292929;width:100%"; host.append(panel);
|
||
["pointerdown", "mousedown", "mouseup", "dblclick"].forEach((name) => host.addEventListener(name, (event) => event.stopPropagation()));
|
||
const display = node.addDOMWidget("clip_tabs", "dumas_h3_clip_tabs", host, { serialize: false });
|
||
if (!node.__dumasSequenceResizeWrapped) {
|
||
const previousResize = node.onResize;
|
||
node.onResize = function (size) {
|
||
if (this.__dumasSequenceMinHeight > 0 && size[1] < this.__dumasSequenceMinHeight) {
|
||
size[1] = this.__dumasSequenceMinHeight;
|
||
}
|
||
return previousResize?.apply(this, arguments);
|
||
};
|
||
node.__dumasSequenceResizeWrapped = true;
|
||
}
|
||
display.options ||= {}; display.options.serialize = false;
|
||
let active = 0;
|
||
let state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4);
|
||
let promptText = null;
|
||
// 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; };
|
||
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:210px;padding:6px;border:1px solid #9a77c9;border-radius:4px;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:198px;object-fit:contain;background:#111318;border-radius:4px";
|
||
bubble.append(image);
|
||
document.body.append(bubble);
|
||
const bounds = element.getBoundingClientRect();
|
||
let left = bounds.right + 8;
|
||
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 choicesBySlot = new Map();
|
||
for (const binding of referenceThumbBindings) {
|
||
if (!binding.element.isConnected) {
|
||
referenceThumbBindings.delete(binding);
|
||
continue;
|
||
}
|
||
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.lastError = "";
|
||
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 preview available yet (execute builder for tensor preview)`;
|
||
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 referenceThumbnail = (slot, size = 52) => {
|
||
const holder = el("span");
|
||
holder.className = "dumas-h3-reference-thumbnail";
|
||
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:4px;vertical-align:middle`;
|
||
const fallback = el("span", "R");
|
||
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: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;
|
||
binding.lastError = "";
|
||
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;
|
||
binding.lastError = "Image URL could not load";
|
||
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);
|
||
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 => {
|
||
const trace = [];
|
||
const candidates = connectedReferenceImageCandidates(node, slot, trace);
|
||
const binding = [...referenceThumbBindings].find(b => b.slot === slot && b.element.isConnected);
|
||
return {
|
||
slot,
|
||
connected: !!node.inputs?.find(input => input.name === `reference_image_${slot}` && input.link != null),
|
||
upstreamNodes: trace.map(item => item.type).join(" -> "),
|
||
candidateCount: candidates.length,
|
||
displayed: !!binding?.image?.complete && !!binding?.image?.naturalWidth,
|
||
previewError: binding?.lastError || "",
|
||
};
|
||
});
|
||
// 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.
|
||
// Calling its callback is not: ComfyUI callbacks redraw the DOM widget,
|
||
// which destroys the focused textarea during blur/pointer transitions.
|
||
const persistState = () => {
|
||
stateWidget.value = JSON.stringify(state);
|
||
node.setDirtyCanvas?.(true, true);
|
||
};
|
||
const setState = () => {
|
||
persistState();
|
||
stateWidget.callback?.(stateWidget.value);
|
||
};
|
||
const input = (type, value, onChange) => {
|
||
const item = el("input"); item.type = type; item.value = value;
|
||
item.style.cssText = type === "range"
|
||
? "width:100%;box-sizing:border-box;margin:2px 0;accent-color:#79c98a"
|
||
: "width:100%;box-sizing:border-box;margin:2px 0;color:#111";
|
||
item.onchange = () => onChange(item.value); return item;
|
||
};
|
||
const addSlot = (clip, afterIndex = clip.slots.length - 1) => {
|
||
const duration = Number(clip.duration) || 0.1;
|
||
if (!clip.slots.length) {
|
||
clip.slots.push({ start: 0, end: duration, text: "" });
|
||
return;
|
||
}
|
||
const newCount = clip.slots.length + 1;
|
||
// Keep a minimum only when the selected duration allows it. The old
|
||
// fixed 0.1s floor made later shots collapse at short frame choices.
|
||
const minSlice = Math.min(0.1, duration / newCount);
|
||
let splitIndex = Math.max(0, Math.min(afterIndex, clip.slots.length - 1));
|
||
const lengthOf = (slot) => Math.max(0, Number(slot.end) - Number(slot.start));
|
||
if (lengthOf(clip.slots[splitIndex]) < minSlice * 2) {
|
||
splitIndex = clip.slots.reduce(
|
||
(best, slot, index) => lengthOf(slot) > lengthOf(clip.slots[best]) ? index : best,
|
||
0,
|
||
);
|
||
}
|
||
const target = clip.slots[splitIndex];
|
||
const start = Number(target?.start) || 0;
|
||
const end = Math.min(duration, Number(target?.end) || duration);
|
||
let split = start + (end - start) / 2;
|
||
split = Math.max(start + minSlice, Math.min(end - minSlice, split));
|
||
if (!(split > start && split < end)) {
|
||
// This is only reachable for malformed or extremely short state;
|
||
// redistribute boundaries evenly so the new shot is still real.
|
||
const texts = clip.slots.map((slot) => slot.text || "");
|
||
texts.splice(splitIndex + 1, 0, "");
|
||
clip.slots = texts.map((text, index) => ({
|
||
start: duration * index / newCount,
|
||
end: duration * (index + 1) / newCount,
|
||
text,
|
||
}));
|
||
return;
|
||
}
|
||
target.end = split;
|
||
clip.slots.splice(splitIndex + 1, 0, { start: split, end, text: "" });
|
||
};
|
||
const formatTime = (seconds) => `00:${Number(seconds).toFixed(3).padStart(6, "0")}`;
|
||
// Keep the user's script as the source of truth, like CSGlide does. The
|
||
// slots are only the timeline model used to paint the bar and to locate
|
||
// boundaries. Rebuilding the textarea from slots is lossy (and was the
|
||
// reason a redraw could erase text that had just been typed).
|
||
const formatPromptFromSlots = (clip) => clip.slots.map((slot, index) =>
|
||
`[Shot ${index + 1}] At ${formatTime(slot.start)}, ${slot.text || ""}`).join("\n\n");
|
||
const promptFor = (clip) => {
|
||
if (typeof clip.prompt === "string" && clip.prompt.length) return clip.prompt;
|
||
return formatPromptFromSlots(clip);
|
||
};
|
||
const parseTime = (value) => {
|
||
const parts = String(value || "").split(":").map(Number);
|
||
if (parts.some((part) => !Number.isFinite(part))) return NaN;
|
||
if (parts.length === 3) return parts[0] * 3600 + parts[1] * 60 + parts[2];
|
||
if (parts.length === 2) return parts[0] * 60 + parts[1];
|
||
return parts[0];
|
||
};
|
||
const parsePromptSlots = (clip, value) => {
|
||
const text = String(value || "");
|
||
// Accept the formats users commonly paste from notes and scripts:
|
||
// [Shot 1] At 00:00.000, ...
|
||
// Shot 1: ...
|
||
// SHOT 2 - ...
|
||
// Timestamps are optional; when absent, shots are divided evenly
|
||
// across the existing chunk duration.
|
||
const markerPattern = /(?:^|\n)\s*(?:\[\s*)?Shot\s+(\d+)(?:\s*\])?(?:\s+(?:At\s+)?((?:\d+(?::\d+){1,2}(?:\.\d+)?|\d+(?:\.\d+)?)))?\s*(?:[,\-:]\s*|\s+)/gim;
|
||
const markers = [...text.matchAll(markerPattern)];
|
||
const duration = Math.max(0.1, Number(clip.duration) || 0.1);
|
||
if (!markers.length) {
|
||
// A script with no [Shot N] markers is a single-shot script. This
|
||
// lets users remove their last extra shot by deleting its block.
|
||
clip.slots = [{ start: 0, end: duration, text: text.trim() }];
|
||
return true;
|
||
}
|
||
const hasTimestamps = markers.some((marker) => marker[2]);
|
||
const slots = markers.map((marker, index) => {
|
||
const rawStart = marker[2] ? parseTime(marker[2]) : NaN;
|
||
const start = hasTimestamps && Number.isFinite(rawStart)
|
||
? Math.min(Math.max(0, rawStart), Math.max(0, duration - 0.1))
|
||
: (duration * index / markers.length);
|
||
const next = markers[index + 1];
|
||
const rawEnd = next?.[2] ? parseTime(next[2]) : duration;
|
||
const fallbackEnd = hasTimestamps && Number.isFinite(rawEnd)
|
||
? rawEnd
|
||
: (duration * (index + 1) / markers.length);
|
||
const end = Math.min(duration, Math.max(start + 0.1, fallbackEnd));
|
||
const from = marker.index + marker[0].length;
|
||
const to = next ? next.index : text.length;
|
||
return { start, end, text: text.slice(from, to).trim() };
|
||
});
|
||
slots.forEach((slot, index) => {
|
||
if (index > 0) slot.start = slots[index - 1].end;
|
||
if (slot.end <= slot.start) slot.end = Math.min(duration, slot.start + 0.1);
|
||
});
|
||
slots[0].start = 0;
|
||
slots[slots.length - 1].end = duration;
|
||
clip.slots = slots;
|
||
return true;
|
||
};
|
||
const syncPrompt = (clip, value) => {
|
||
const text = String(value || "");
|
||
if (parsePromptSlots(clip, text)) return true;
|
||
// The shot description starts on the same line as the marker in the
|
||
// CSGlide-style editor. Only consume the marker prefix through its
|
||
// timestamp comma; consuming the whole line would discard text such
|
||
// as `[Shot 1] At 00:00.000, the character walks...` when a new shot
|
||
// is added and the marker scaffold is rebuilt.
|
||
const marks = [...text.matchAll(/\[Shot\s+(\d+)\]\s+At\s+[^,\n]+,\s*/gi)];
|
||
if (!marks.length) { if (clip.slots[0]) clip.slots[0].text = text.trim(); return; }
|
||
marks.forEach((mark, index) => {
|
||
const slotIndex = Number(mark[1]) - 1;
|
||
if (!clip.slots[slotIndex]) return;
|
||
const from = mark.index + mark[0].length;
|
||
const to = index + 1 < marks.length ? marks[index + 1].index : text.length;
|
||
clip.slots[slotIndex].text = text.slice(from, to).trim();
|
||
});
|
||
return false;
|
||
};
|
||
const retimePrompt = (text, clip) => {
|
||
let index = 0;
|
||
return String(text || "").replace(/(\[Shot\s+\d+\]\s+At\s+)[^,\n]+(,?)/gi, (whole, prefix, suffix) => {
|
||
const start = clip.slots[index++]?.start ?? 0;
|
||
return `${prefix}${formatTime(start)}${suffix}`;
|
||
});
|
||
};
|
||
const commitPrompt = (clip, value, redraw = false) => {
|
||
clip.prompt = String(value ?? "");
|
||
syncPrompt(clip, clip.prompt);
|
||
// Do not invoke the widget callback for ordinary text edits. The
|
||
// callback redraws the DOM and can run while the textarea is blurring
|
||
// to a timeline handle, exactly the interaction CSGlide avoids.
|
||
persistState();
|
||
if (redraw) render();
|
||
};
|
||
const rebuildPromptAfterStructureChange = (clip) => {
|
||
// The caller syncs the current text before changing the slot list.
|
||
// Do not parse clip.prompt again here: after adding a shot it still
|
||
// has the old marker count, and reparsing would discard the new slot.
|
||
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) =>
|
||
Math.abs(option.seconds - currentSeconds) < Math.abs(best.seconds - currentSeconds) ? option : best,
|
||
DURATION_OPTIONS[6]);
|
||
if (Number(secondsWidget.value) !== selectedDuration.seconds) secondsWidget.value = selectedDuration.seconds;
|
||
const chunkDuration = selectedDuration.seconds;
|
||
while (state.clips.length < count) state.clips.push({ duration: chunkDuration, enabled: true, slots: [] });
|
||
state.clips = state.clips.slice(0, count); active = Math.min(active, count - 1);
|
||
node.__dumasCloseAutocomplete?.();
|
||
panel.replaceChildren();
|
||
const tabs = el("div");
|
||
tabs.className = "dumas-h3-clip-tabs";
|
||
tabs.setAttribute("role", "tablist");
|
||
tabs.setAttribute("aria-label", "Clips");
|
||
state.clips.forEach((clip, index) => {
|
||
const enabled = clip.enabled !== false;
|
||
const button = el("button", `${enabled ? "" : "× "}Clip ${index + 1}`);
|
||
button.type = "button";
|
||
button.className = `dumas-h3-clip-tab${index === active ? " is-active" : ""}${enabled ? "" : " is-disabled"}`;
|
||
button.setAttribute("role", "tab");
|
||
button.setAttribute("aria-selected", String(index === active));
|
||
button.title = enabled ? `Edit Clip ${index + 1}` : `Clip ${index + 1} is disabled`;
|
||
button.disabled = index === active;
|
||
button.onclick = () => { active = index; render(); };
|
||
tabs.append(button);
|
||
});
|
||
panel.append(tabs);
|
||
const clip = state.clips[active];
|
||
// All clips share the one authoritative project duration widget.
|
||
const duration = chunkDuration;
|
||
clip.duration = duration;
|
||
if (clip.slots.length) clip.slots[clip.slots.length - 1].end = duration;
|
||
promptText = promptFor(clip);
|
||
const durationControl = el("div");
|
||
durationControl.className = "dumas-h3-chunk-duration";
|
||
const durationLabel = el("span", "Chunk duration / frame choice");
|
||
durationLabel.className = "dumas-h3-chunk-duration__label";
|
||
const durationSelect = document.createElement("select");
|
||
durationSelect.className = "dumas-h3-chunk-duration__select";
|
||
durationSelect.setAttribute("aria-label", "Shared chunk duration and H3 frame choice");
|
||
DURATION_OPTIONS.forEach((option) => {
|
||
const item = document.createElement("option");
|
||
item.value = String(option.seconds);
|
||
item.textContent = `${option.actual} real · ${option.latent} latent · ${option.labelSeconds.toFixed(2)}s`;
|
||
item.selected = option === selectedDuration;
|
||
durationSelect.append(item);
|
||
});
|
||
durationSelect.title = "Choose a native H3 frame length. This duration is shared by every clip.";
|
||
durationSelect.onchange = () => {
|
||
secondsWidget.value = Number(durationSelect.value);
|
||
secondsWidget.callback?.(secondsWidget.value);
|
||
};
|
||
durationControl.append(durationLabel, durationSelect);
|
||
panel.append(durationControl);
|
||
const durationRow = el("div");
|
||
durationRow.className = "dumas-h3-clip-status";
|
||
const clipName = el("strong", `Clip ${active + 1}`);
|
||
clipName.className = "dumas-h3-clip-status__title";
|
||
const clipMeta = el("span", `· ${selectedDuration.labelSeconds.toFixed(2)}s · shared across clips`);
|
||
clipMeta.className = "dumas-h3-clip-status__meta";
|
||
const enabledLabel = el("label");
|
||
enabledLabel.className = `dumas-h3-clip-enable${clip.enabled === false ? " is-off" : ""}`;
|
||
enabledLabel.title = "Include this clip in the output sequence";
|
||
const enabledToggle = document.createElement("input");
|
||
enabledToggle.type = "checkbox";
|
||
enabledToggle.className = "dumas-h3-clip-enable__input";
|
||
enabledToggle.checked = clip.enabled !== false;
|
||
enabledToggle.setAttribute("aria-label", `Enable Clip ${active + 1}`);
|
||
const enabledText = el("span", enabledToggle.checked ? "Enabled" : "Disabled");
|
||
enabledToggle.onchange = () => {
|
||
clip.enabled = enabledToggle.checked;
|
||
persistState();
|
||
render();
|
||
};
|
||
enabledLabel.append(enabledToggle, enabledText);
|
||
durationRow.append(clipName, clipMeta, enabledLabel);
|
||
panel.append(durationRow);
|
||
if (clip.enabled === false) {
|
||
const disabledNote = el("div", "This chunk is disabled and will not be sent to the output sockets.");
|
||
disabledNote.style.cssText = "padding:5px 7px;margin:0 0 6px;background:#3a2525;border:1px solid #704848;border-radius:4px;color:#e2b4b4;font:11px monospace";
|
||
panel.append(disabledNote);
|
||
}
|
||
// An editor-style timeline: shot blocks are the navigation, and only
|
||
// the shared boundaries resize. No separate duplicate "shot track" row.
|
||
const palette = ["#7bbb95", "#78a7dc", "#ddb17c", "#b69ada", "#d9919b", "#81c7c1"];
|
||
let prompt = null;
|
||
let promptSelectionStart = 0;
|
||
let promptSelectionEnd = 0;
|
||
let autocomplete = null;
|
||
let autocompleteRange = null;
|
||
let promptFrame = null;
|
||
let autocompleteOptions = [];
|
||
let autocompleteSelected = 0;
|
||
let autocompleteCleanup = null;
|
||
let encoder = null;
|
||
const focusPrompt = () => {
|
||
if (!prompt) return;
|
||
try {
|
||
prompt.focus({ preventScroll: true });
|
||
} catch (_) {
|
||
prompt.focus();
|
||
}
|
||
};
|
||
const timelinePanel = el("div");
|
||
timelinePanel.className = "dumas-h3-shot-timeline";
|
||
const timelineHeading = el("div");
|
||
timelineHeading.className = "dumas-h3-shot-timeline__heading";
|
||
const headingText = el("strong", "SHOT TIMELINE");
|
||
const headingMeta = el("span", `${clip.slots.length} shot${clip.slots.length === 1 ? "" : "s"} · ${duration.toFixed(2)}s`);
|
||
headingMeta.className = "dumas-h3-shot-timeline__meta";
|
||
timelineHeading.append(headingText, headingMeta);
|
||
timelinePanel.append(timelineHeading);
|
||
|
||
const ruler = el("div");
|
||
ruler.className = "dumas-h3-shot-timeline__ruler";
|
||
// Use human-friendly seconds rather than arbitrary percentages.
|
||
const rulerStep = duration <= 2.5 ? 0.5 : duration <= 6 ? 1 : duration <= 12 ? 2 : duration <= 20 ? 4 : 5;
|
||
const rulerTimes = [0];
|
||
for (let time = rulerStep; time < duration - rulerStep * 0.5; time += rulerStep) rulerTimes.push(time);
|
||
rulerTimes.push(duration);
|
||
rulerTimes.forEach((time, index) => {
|
||
const tick = el("span", index === rulerTimes.length - 1 ? `${duration.toFixed(2)}s` : `${time}s`);
|
||
tick.className = "dumas-h3-shot-timeline__tick";
|
||
tick.style.left = `${time / duration * 100}%`;
|
||
ruler.append(tick);
|
||
});
|
||
timelinePanel.append(ruler);
|
||
|
||
const timeline = el("div");
|
||
timeline.className = "dumas-h3-shot-timeline__track";
|
||
timeline.setAttribute("role", "group");
|
||
timeline.setAttribute("aria-label", `Clip ${active + 1} shot timeline`);
|
||
const footer = el("div");
|
||
footer.className = "dumas-h3-shot-timeline__footer";
|
||
const selectedInfo = el("div");
|
||
selectedInfo.className = "dumas-h3-shot-timeline__selection";
|
||
const selectedDot = el("i");
|
||
selectedDot.className = "dumas-h3-shot-timeline__selected-dot";
|
||
const selectedTitle = el("strong");
|
||
const selectedTimes = el("span");
|
||
selectedInfo.append(selectedDot, selectedTitle, selectedTimes);
|
||
const hint = el("span", "Click a shot to edit · drag a divider to retime");
|
||
hint.className = "dumas-h3-shot-timeline__hint";
|
||
footer.append(selectedInfo, hint);
|
||
const segments = [];
|
||
const handles = [];
|
||
let selectedShot = Math.min(Math.max(selectedShotByClip.get(active) ?? 0, 0), Math.max(0, clip.slots.length - 1));
|
||
|
||
const paintSelection = () => {
|
||
segments.forEach((segment, index) => {
|
||
const selected = index === selectedShot;
|
||
segment.classList.toggle("is-selected", selected);
|
||
segment.setAttribute("aria-pressed", String(selected));
|
||
});
|
||
const slot = clip.slots[selectedShot];
|
||
selectedInfo.style.setProperty("--selected-color", palette[selectedShot % palette.length] ?? palette[0]);
|
||
selectedTitle.textContent = slot ? `Shot ${selectedShot + 1}` : "No shot selected";
|
||
selectedTimes.textContent = slot
|
||
? `${Number(slot.start).toFixed(2)}–${Number(slot.end).toFixed(2)}s (${(Number(slot.end) - Number(slot.start)).toFixed(2)}s)`
|
||
: "";
|
||
};
|
||
const selectShot = (index, focusEditor = true) => {
|
||
selectedShot = index;
|
||
selectedShotByClip.set(active, index);
|
||
paintSelection();
|
||
if (!focusEditor || !prompt) return;
|
||
const marker = `[Shot ${index + 1}]`;
|
||
const position = prompt.value.indexOf(marker);
|
||
focusPrompt();
|
||
if (position >= 0) {
|
||
prompt.setSelectionRange(position, position + marker.length);
|
||
promptSelectionStart = position;
|
||
promptSelectionEnd = position + marker.length;
|
||
}
|
||
};
|
||
const updateTimeline = () => {
|
||
clip.slots.forEach((slot, index) => {
|
||
const start = Number(slot.start);
|
||
const end = Number(slot.end);
|
||
segments[index].style.left = `${Math.max(0, start / duration * 100)}%`;
|
||
segments[index].style.width = `${Math.max(0, (end - start) / duration * 100)}%`;
|
||
segments[index].title = `Shot ${index + 1} · ${start.toFixed(2)}–${end.toFixed(2)}s · ${slot.text || "Unscripted"}`;
|
||
const description = segments[index].querySelector(".dumas-h3-shot-timeline__description");
|
||
if (description) description.textContent = (slot.text || "Unscripted").replace(/\s+/g, " ").trim();
|
||
});
|
||
handles.forEach(({ handle, bubble, slot }) => {
|
||
handle.style.left = `${Number(slot.end) / duration * 100}%`;
|
||
bubble.textContent = `${Number(slot.end).toFixed(2)}s`;
|
||
});
|
||
paintSelection();
|
||
};
|
||
clip.slots.forEach((slot, index) => {
|
||
const segment = el("button");
|
||
segment.type = "button";
|
||
segment.className = "dumas-h3-shot-timeline__segment";
|
||
segment.style.setProperty("--shot-accent", palette[index % palette.length]);
|
||
const head = el("span");
|
||
head.className = "dumas-h3-shot-timeline__shot-head";
|
||
const number = el("span", String(index + 1).padStart(2, "0"));
|
||
number.className = "dumas-h3-shot-timeline__shot-number";
|
||
const name = el("span", `Shot ${index + 1}`);
|
||
name.className = "dumas-h3-shot-timeline__shot-name";
|
||
head.append(number, name);
|
||
const description = el("span", (slot.text || "Unscripted").replace(/\s+/g, " ").trim());
|
||
description.className = "dumas-h3-shot-timeline__description";
|
||
segment.append(head, description);
|
||
segment.onclick = (event) => {
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
selectShot(index);
|
||
};
|
||
segments.push(segment);
|
||
timeline.append(segment);
|
||
});
|
||
clip.slots.slice(0, -1).forEach((slot, index) => {
|
||
const next = clip.slots[index + 1];
|
||
const handle = el("button");
|
||
handle.type = "button";
|
||
handle.className = "dumas-h3-shot-timeline__handle";
|
||
handle.title = `Resize boundary between Shot ${index + 1} and Shot ${index + 2}`;
|
||
handle.setAttribute("aria-label", handle.title);
|
||
const bubble = el("span");
|
||
bubble.className = "dumas-h3-shot-timeline__bubble";
|
||
handle.append(bubble);
|
||
const minimumShot = Math.min(0.1, duration / (clip.slots.length + 1));
|
||
const adjust = (proposedTime) => {
|
||
// Only move the boundary between the adjacent pair; never
|
||
// allow it to cross the previous or following boundary.
|
||
const low = Number(slot.start) + minimumShot;
|
||
const high = Number(next.end) - minimumShot;
|
||
if (low > high) return;
|
||
const snapped = Math.round(proposedTime * 24) / 24; // H3 at 24fps
|
||
const point = Math.max(low, Math.min(high, snapped));
|
||
slot.end = point;
|
||
next.start = point;
|
||
updateTimeline();
|
||
};
|
||
const finishEdit = () => {
|
||
// Commit without rebuilding the DOM: this prevents losing
|
||
// textarea focus or moving the ComfyUI canvas while editing.
|
||
promptText = retimePrompt(promptText ?? prompt?.value ?? clip.prompt ?? "", clip);
|
||
clip.prompt = promptText;
|
||
if (prompt) prompt.value = promptText;
|
||
persistState();
|
||
if (encoder) encoder.textContent = clip.slots
|
||
.map((item, itemIndex) => `Shot ${itemIndex + 1} ${formatTime(item.start)}`)
|
||
.join(" ");
|
||
};
|
||
let dragging = false;
|
||
let dragPointerId = null;
|
||
const move = (event) => {
|
||
if (!dragging || event.pointerId !== dragPointerId) return;
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
const rect = timeline.getBoundingClientRect();
|
||
if (!rect.width) return;
|
||
adjust(((event.clientX - rect.left) / rect.width) * duration);
|
||
};
|
||
const finish = (event) => {
|
||
if (!dragging || (event?.pointerId != null && event.pointerId !== dragPointerId)) return;
|
||
dragging = false;
|
||
dragPointerId = null;
|
||
event?.preventDefault?.();
|
||
event?.stopPropagation?.();
|
||
handle.classList.remove("is-dragging");
|
||
document.removeEventListener("pointermove", move, true);
|
||
document.removeEventListener("pointerup", finish, true);
|
||
document.removeEventListener("pointercancel", finish, true);
|
||
finishEdit();
|
||
};
|
||
handle.onpointerdown = (event) => {
|
||
if (event.button !== 0 || dragging) return;
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
dragging = true;
|
||
dragPointerId = event.pointerId;
|
||
// Capture uncommitted text without parsing it over the already
|
||
// authoritative timeline boundaries.
|
||
if (prompt) { promptText = prompt.value; clip.prompt = promptText; }
|
||
selectShot(index, false);
|
||
handle.classList.add("is-dragging");
|
||
document.addEventListener("pointermove", move, true);
|
||
document.addEventListener("pointerup", finish, true);
|
||
document.addEventListener("pointercancel", finish, true);
|
||
};
|
||
handle.onkeydown = (event) => {
|
||
if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return;
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
if (prompt) { promptText = prompt.value; clip.prompt = promptText; }
|
||
selectShot(index, false);
|
||
const frames = event.shiftKey ? 5 : 1;
|
||
adjust(Number(slot.end) + (event.key === "ArrowRight" ? frames : -frames) / 24);
|
||
finishEdit();
|
||
};
|
||
handles.push({ handle, bubble, slot });
|
||
timeline.append(handle);
|
||
});
|
||
if (!clip.slots.length) {
|
||
const empty = el("div", "No shots yet — use Add shot below");
|
||
empty.className = "dumas-h3-shot-timeline__empty";
|
||
timeline.append(empty);
|
||
}
|
||
updateTimeline();
|
||
timelinePanel.append(timeline, footer);
|
||
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:4px;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) => {
|
||
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();
|
||
};
|
||
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:9px;height:62px;min-width:180px;padding:3px 12px 3px 4px;border:1px solid #946bbe;border-radius:4px;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;
|
||
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 (!connectedReferenceSlots.length) {
|
||
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 = () => {
|
||
promptSelectionStart = prompt.selectionStart ?? prompt.value.length;
|
||
promptSelectionEnd = prompt.selectionEnd ?? promptSelectionStart;
|
||
};
|
||
const closeAutocomplete = () => {
|
||
autocompleteCleanup?.();
|
||
autocompleteCleanup = null;
|
||
autocomplete?.remove();
|
||
autocomplete = null;
|
||
autocompleteRange = null;
|
||
autocompleteOptions = [];
|
||
prompt?.removeAttribute("aria-activedescendant");
|
||
prompt?.removeAttribute("aria-controls");
|
||
prompt?.removeAttribute("aria-expanded");
|
||
};
|
||
node.__dumasCloseAutocomplete = closeAutocomplete;
|
||
// The overlay lives on document.body rather than inside the ComfyUI
|
||
// node. It cannot be clipped by a scaled/transformed node or misplaced
|
||
// by using the panel's origin as the textarea's caret location.
|
||
const positionAutocomplete = () => {
|
||
if (!autocomplete || !prompt?.isConnected) { closeAutocomplete(); return; }
|
||
let caret;
|
||
try {
|
||
caret = textareaCaretRect(prompt, prompt.selectionStart ?? prompt.value.length);
|
||
if (!Number.isFinite(caret.left) || !Number.isFinite(caret.bottom)) throw new Error("invalid caret coordinates");
|
||
} catch (error) {
|
||
// Keep autocomplete usable even if a host UI layout cannot be
|
||
// mirrored precisely. The fallback anchors at the textarea.
|
||
const box = prompt.getBoundingClientRect();
|
||
caret = { left: box.left + 12, top: box.top, bottom: box.top + 22 };
|
||
console.warn("[DUMAS H3] Caret positioning fallback:", error);
|
||
}
|
||
const gutter = 8;
|
||
const gap = 5;
|
||
const width = autocomplete.offsetWidth;
|
||
const height = autocomplete.offsetHeight;
|
||
const x = Math.min(Math.max(gutter, caret.left), Math.max(gutter, window.innerWidth - width - gutter));
|
||
let y = caret.bottom + gap;
|
||
if (y + height > window.innerHeight - gutter) y = caret.top - height - gap;
|
||
y = Math.max(gutter, Math.min(y, Math.max(gutter, window.innerHeight - height - gutter)));
|
||
autocomplete.style.left = `${Math.round(x)}px`;
|
||
autocomplete.style.top = `${Math.round(y)}px`;
|
||
};
|
||
const selectAutocompleteOption = (index) => {
|
||
if (!autocompleteOptions.length) return;
|
||
autocompleteSelected = (index + autocompleteOptions.length) % autocompleteOptions.length;
|
||
autocompleteOptions.forEach(({ option }, itemIndex) => {
|
||
const active = itemIndex === autocompleteSelected;
|
||
option.style.background = active ? "#3a5360" : "transparent";
|
||
option.style.color = active ? "#fff" : "#e7edf0";
|
||
option.setAttribute("aria-selected", String(active));
|
||
});
|
||
const selected = autocompleteOptions[autocompleteSelected].option;
|
||
prompt.setAttribute("aria-activedescendant", selected.id);
|
||
selected.scrollIntoView?.({ block: "nearest" });
|
||
};
|
||
const openAutocomplete = (at) => {
|
||
closeAutocomplete();
|
||
autocompleteRange = { start: at, end: at + 1 };
|
||
const menu = el("div");
|
||
menu.id = `dumas-h3-autocomplete-${node.id ?? "editor"}-${active}`;
|
||
menu.setAttribute("role", "listbox");
|
||
menu.setAttribute("aria-label", "Insert reference image or new shot");
|
||
menu.style.cssText = "position:fixed;z-index:2147483646;min-width:220px;width:max-content;max-width:min(310px,calc(100vw - 16px));max-height:min(360px,calc(100vh - 16px));overflow-y:auto;box-sizing:border-box;padding:5px;background:#252b30;border:1px solid #58646c;border-radius:4px;box-shadow:0 12px 30px #000c;color:#e7edf0;pointer-events:auto";
|
||
const addOption = (content, title, action) => {
|
||
const option = el("button");
|
||
option.type = "button";
|
||
option.title = title;
|
||
option.id = `${menu.id}-option-${autocompleteOptions.length}`;
|
||
option.setAttribute("role", "option");
|
||
option.setAttribute("aria-selected", "false");
|
||
option.style.cssText = "display:flex;align-items:center;gap:9px;width:100%;padding:7px 8px;border:0;border-radius:4px;background:transparent;color:#e7edf0;text-align:left;font:12px monospace;cursor:pointer";
|
||
// Keep focus in the textarea: Enter/arrow selection must work.
|
||
option.onpointerdown = (event) => { event.preventDefault(); event.stopPropagation(); };
|
||
option.onmouseenter = () => selectAutocompleteOption(autocompleteOptions.findIndex((entry) => entry.option === option));
|
||
option.onclick = (event) => { event.preventDefault(); event.stopPropagation(); action(); };
|
||
option.append(...content);
|
||
menu.append(option);
|
||
autocompleteOptions.push({ option, action });
|
||
};
|
||
connectedReferenceSlots.forEach((slot) => {
|
||
addOption([
|
||
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}`, () => {
|
||
const start = autocompleteRange?.start ?? prompt.value.length;
|
||
const end = autocompleteRange?.end ?? start + 1;
|
||
const token = `@R${slot} `;
|
||
prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end);
|
||
const caret = start + token.length;
|
||
closeAutocomplete();
|
||
focusPrompt();
|
||
prompt.setSelectionRange(caret, caret);
|
||
promptSelectionStart = caret; promptSelectionEnd = caret;
|
||
promptText = prompt.value; clip.prompt = prompt.value;
|
||
syncPrompt(clip, clip.prompt); persistState();
|
||
});
|
||
});
|
||
addOption([
|
||
(() => { const icon = el("span", "S"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:4px;background:#397a91;color:#dff7ff;font-weight:bold;flex:none"; return icon; })(),
|
||
el("span", `[Shot ${clip.slots.length + 1}]`),
|
||
(() => { const hint = el("span", "New shot"); hint.style.cssText = "margin-left:auto;color:#a5b7bf;font-size:10px"; return hint; })(),
|
||
], "Create the next timed shot", () => {
|
||
const start = autocompleteRange?.start ?? prompt.value.length;
|
||
const end = autocompleteRange?.end ?? start + 1;
|
||
prompt.value = prompt.value.slice(0, start) + prompt.value.slice(end);
|
||
promptText = prompt.value; clip.prompt = prompt.value;
|
||
syncPrompt(clip, clip.prompt); addSlot(clip); rebuildPromptAfterStructureChange(clip);
|
||
// The editor is still focused when Enter chooses this item.
|
||
// A programmatic redraw will blur the OLD textarea, whose old
|
||
// two-shot text would otherwise parse over our added slot.
|
||
prompt.onblur = null;
|
||
prompt.onchange = null;
|
||
closeAutocomplete();
|
||
setState(); // the hidden widget callback renders the new editor
|
||
const newPrompt = panel.querySelector("textarea");
|
||
if (newPrompt) {
|
||
try { newPrompt.focus({ preventScroll: true }); }
|
||
catch (_) { newPrompt.focus(); }
|
||
// Put the cursor where the new shot description begins.
|
||
newPrompt.setSelectionRange(newPrompt.value.length, newPrompt.value.length);
|
||
}
|
||
});
|
||
autocomplete = menu;
|
||
document.body.append(menu);
|
||
refreshReferenceThumbs();
|
||
prompt.setAttribute("aria-controls", menu.id);
|
||
prompt.setAttribute("aria-expanded", "true");
|
||
selectAutocompleteOption(0);
|
||
positionAutocomplete();
|
||
const onOutsidePointer = (event) => {
|
||
if (!menu.contains(event.target) && event.target !== prompt) closeAutocomplete();
|
||
};
|
||
const onViewportChange = () => positionAutocomplete();
|
||
document.addEventListener("pointerdown", onOutsidePointer, true);
|
||
// Capturing scroll events covers scrolling within the prompt, the
|
||
// node viewport and any ComfyUI canvas container.
|
||
document.addEventListener("scroll", onViewportChange, true);
|
||
window.addEventListener("resize", onViewportChange);
|
||
autocompleteCleanup = () => {
|
||
document.removeEventListener("pointerdown", onOutsidePointer, true);
|
||
document.removeEventListener("scroll", onViewportChange, true);
|
||
window.removeEventListener("resize", onViewportChange);
|
||
};
|
||
};
|
||
// Trigger on the actual text insertion, not only keydown. On some
|
||
// browsers / keyboard layouts an input event follows the @ keydown;
|
||
// the old handler immediately CLOSED the menu it had just opened.
|
||
// Handling the inserted character works with UK keyboards, IMEs and
|
||
// browser-mediated text input without manually inserting duplicates.
|
||
prompt.oninput = (event) => {
|
||
const before = String(promptText ?? clip.prompt ?? "");
|
||
const after = prompt.value;
|
||
const caret = prompt.selectionStart ?? after.length;
|
||
const atCaret = caret > 0 && after.charAt(caret - 1) === "@";
|
||
const insertedSingleAt = atCaret && event?.inputType !== "insertFromPaste" && (
|
||
(event?.data === "@") ||
|
||
(after.length === before.length + 1 &&
|
||
after.slice(0, caret - 1) === before.slice(0, caret - 1) &&
|
||
after.slice(caret) === before.slice(caret - 1))
|
||
);
|
||
rememberPromptSelection();
|
||
promptText = after;
|
||
clip.prompt = after;
|
||
if (insertedSingleAt) {
|
||
openAutocomplete(caret - 1);
|
||
} else if (autocomplete) {
|
||
// Typing anything after the @ lets the user continue with a
|
||
// literal token, e.g. @R1, without an obstructing menu.
|
||
closeAutocomplete();
|
||
}
|
||
};
|
||
prompt.onpaste = () => {
|
||
closeAutocomplete();
|
||
// Let the browser insert the pasted text first, then parse its
|
||
// shot markers and redraw the track without losing the paste.
|
||
setTimeout(() => {
|
||
syncPrompt(clip, prompt.value);
|
||
promptText = prompt.value;
|
||
clip.prompt = prompt.value;
|
||
// Notify ComfyUI as well as updating the hidden widget value.
|
||
// Without the callback, the editor redraws locally but queueing
|
||
// can still send the previous one-shot timeline_json value.
|
||
setState();
|
||
// A paste can change the text of an existing shot without
|
||
// adding/removing shot markers. Redraw in both cases so the
|
||
// timeline labels and shot chips reflect the pasted text.
|
||
// The previous conditional left the shot track stale when
|
||
// syncPrompt returned false for plain or partially formatted
|
||
// pasted text.
|
||
render();
|
||
}, 0);
|
||
};
|
||
prompt.onselect = rememberPromptSelection;
|
||
prompt.onkeyup = (event) => {
|
||
rememberPromptSelection();
|
||
// Fallback for environments that deliver keyboard text changes
|
||
// without a normal InputEvent. Never fabricate the @ character.
|
||
if (event.key === "@" && !autocomplete &&
|
||
promptSelectionStart > 0 &&
|
||
prompt.value.charAt(promptSelectionStart - 1) === "@") {
|
||
openAutocomplete(promptSelectionStart - 1);
|
||
}
|
||
};
|
||
prompt.onmouseup = rememberPromptSelection;
|
||
prompt.onblur = () => {
|
||
closeAutocomplete();
|
||
rememberPromptSelection();
|
||
clip.prompt = prompt.value;
|
||
const priorShotCount = segments.length;
|
||
syncPrompt(clip, clip.prompt);
|
||
if (clip.slots.length !== priorShotCount) {
|
||
// A deleted [Shot N] line must remove the timeline segment,
|
||
// and any remaining marker numbers/times must stay in sync.
|
||
let nextIndex = 0;
|
||
clip.prompt = clip.prompt.replace(
|
||
/(\[Shot\s+)\d+(\]\s+At\s+)[^,\n]+(?=,)/gi,
|
||
(match, prefix, middle) => {
|
||
const slot = clip.slots[nextIndex];
|
||
if (!slot) return match;
|
||
nextIndex += 1;
|
||
return `${prefix}${nextIndex}${middle}${formatTime(slot.start)}`;
|
||
},
|
||
);
|
||
prompt.value = clip.prompt;
|
||
syncPrompt(clip, clip.prompt);
|
||
persistState();
|
||
render();
|
||
} else {
|
||
persistState();
|
||
updateTimeline();
|
||
}
|
||
};
|
||
prompt.onchange = () => { commitPrompt(clip, prompt.value); };
|
||
prompt.onpointerdown = (event) => event.stopPropagation();
|
||
prompt.onkeydown = (event) => {
|
||
if (autocomplete) {
|
||
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
||
event.preventDefault(); event.stopPropagation();
|
||
selectAutocompleteOption(autocompleteSelected + (event.key === "ArrowDown" ? 1 : -1));
|
||
return;
|
||
}
|
||
if (event.key === "Enter" || event.key === "Tab") {
|
||
event.preventDefault(); event.stopPropagation();
|
||
autocompleteOptions[autocompleteSelected]?.action();
|
||
return;
|
||
}
|
||
if (event.key === "Escape") {
|
||
event.preventDefault(); event.stopPropagation();
|
||
closeAutocomplete();
|
||
return;
|
||
}
|
||
}
|
||
if (event.key !== "@" || event.ctrlKey || event.metaKey || event.altKey) return;
|
||
// Let the browser insert @. The input event opens the menu after
|
||
// the selection/caret has advanced to its true position.
|
||
// Stop ComfyUI graph-level keyboard shortcuts from intercepting it.
|
||
event.stopPropagation();
|
||
};
|
||
promptFrame = el("div");
|
||
promptFrame.style.cssText = "position:relative;overflow:visible;width:100%;box-sizing:border-box";
|
||
promptFrame.append(prompt);
|
||
panel.append(promptFrame);
|
||
promptSelectionStart = prompt.value.length;
|
||
promptSelectionEnd = prompt.value.length;
|
||
const insertPromptText = (text) => {
|
||
if (!prompt) return;
|
||
const start = Math.min(promptSelectionStart, prompt.value.length);
|
||
const end = Math.min(Math.max(start, promptSelectionEnd), prompt.value.length);
|
||
const before = prompt.value.slice(0, start);
|
||
const after = prompt.value.slice(end);
|
||
const prefix = before && !/\s$/.test(before) ? " " : "";
|
||
const token = `${prefix}${text} `;
|
||
prompt.value = before + token + after;
|
||
const caret = before.length + token.length;
|
||
focusPrompt(); prompt.setSelectionRange(caret, caret);
|
||
promptSelectionStart = caret; promptSelectionEnd = caret;
|
||
promptText = prompt.value; clip.prompt = prompt.value;
|
||
syncPrompt(clip, clip.prompt); persistState();
|
||
};
|
||
const helperPanel = el("div");
|
||
helperPanel.style.cssText = "display:flex;flex-wrap:wrap;gap:4px;align-items:center;margin:5px 0 7px;padding:5px 6px;background:#202020;border:1px solid #414141;border-radius:4px";
|
||
const helperTitle = el("span", "CAMERA HELPERS");
|
||
helperTitle.style.cssText = "width:100%;color:#8fa898;font-size:10px;letter-spacing:.08em;margin-bottom:1px";
|
||
helperPanel.append(helperTitle);
|
||
CAMERA_HELPERS.forEach(([category, phrases]) => phrases.forEach((phrase) => {
|
||
const button = el("button", phrase.replace(/^The camera /, ""));
|
||
button.type = "button";
|
||
button.title = `${category}: insert \"${phrase}\"`;
|
||
button.style.cssText = "padding:3px 7px;border:1px solid #596b60;border-radius:4px;background:#2b3b31;color:#b9e6c4;font:11px Arial;cursor:pointer;white-space:nowrap";
|
||
button.onpointerdown = (event) => { event.preventDefault(); event.stopPropagation(); };
|
||
button.onmousedown = (event) => { event.preventDefault(); event.stopPropagation(); };
|
||
button.onclick = (event) => { event.preventDefault(); event.stopPropagation(); insertPromptText(phrase); };
|
||
helperPanel.append(button);
|
||
}));
|
||
panel.insertBefore(helperPanel, promptFrame);
|
||
const checkLabel = el("div", "PROMPT CHECK"); checkLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:7px 0 3px"; panel.append(checkLabel);
|
||
const check = el("div", `${clip.slots.length} shot${clip.slots.length === 1 ? "" : "s"} · ${duration.toFixed(1)}s chunk · @ references preserved`); check.style.cssText = "padding:6px 8px;background:#171717;border:1px solid #444;border-radius:4px;color:#9bd0a8;font:11px monospace"; panel.append(check);
|
||
const encoderLabel = el("div", "SENT TO ENCODER AS"); encoderLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:7px 0 3px"; panel.append(encoderLabel);
|
||
encoder = el("div", clip.slots.map((slot, index) => `Shot ${index + 1} ${formatTime(slot.start)}`).join(" ")); encoder.style.cssText = "padding:6px 8px;background:#171717;border:1px solid #444;border-radius:4px;color:#7cc4d5;font:11px monospace;white-space:nowrap;overflow:hidden;text-overflow:ellipsis"; panel.append(encoder);
|
||
// Adding a shot is available in the working @ autocomplete menu.
|
||
// Deleting a complete [Shot N] block from the script removes that shot.
|
||
// No duplicate add/remove actions at the bottom of the node.
|
||
// The editor now contains chips, the prompt check and encoder summary;
|
||
// the old slot-count estimate left a large blank tail in the node.
|
||
// Measure the actual DOM panel instead of guessing from slot count.
|
||
const measuredHeight = Math.ceil(panel.getBoundingClientRect().height || panel.scrollHeight || 0);
|
||
display.computeSize = (width) => [Math.max(420, Number(width) || 420), measuredHeight + 2];
|
||
// CSGlide keeps the DOM panel floating but stores a node-level floor
|
||
// from measured content. Temporarily hide this widget from the
|
||
// ordinary LiteGraph calculation to get the non-panel chrome, then
|
||
// add the real panel height and clamp future manual resizes.
|
||
const realComputeSize = display.computeSize;
|
||
display.computeSize = () => [420, 0];
|
||
const chromeHeight = Number(node.computeSize?.()[1]) || 0;
|
||
display.computeSize = realComputeSize;
|
||
const width = Math.max(420, Number(node.size?.[0]) || 420);
|
||
const desiredHeight = chromeHeight + measuredHeight + 2;
|
||
node.__dumasSequenceMinHeight = desiredHeight;
|
||
if (typeof node.setSize === "function"
|
||
&& (Number(node.size?.[0]) !== width || Number(node.size?.[1]) < desiredHeight)) {
|
||
node.setSize([width, desiredHeight]);
|
||
}
|
||
};
|
||
const refresh = () => { state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4); render(); setState(); };
|
||
countWidget.callback = ((previous) => function(...args) { const result = previous?.apply(this, args); refresh(); return result; })(countWidget.callback);
|
||
secondsWidget.callback = ((previous) => function(...args) {
|
||
const result = previous?.apply(this, args);
|
||
const duration = Math.max(0.1, Number(secondsWidget.value) || 0.1);
|
||
state.clips.forEach((clip) => {
|
||
clip.duration = duration;
|
||
clip.slots.forEach((slot) => {
|
||
slot.start = Math.min(Number(slot.start) || 0, Math.max(0, duration - 0.1));
|
||
slot.end = Math.min(Math.max(Number(slot.end) || duration, slot.start + 0.1), duration);
|
||
});
|
||
});
|
||
setState(); render(); return result;
|
||
})(secondsWidget.callback);
|
||
stateWidget.callback = ((previous) => function(...args) { const result = previous?.apply(this, args); render(); return result; })(stateWidget.callback);
|
||
const previousConnections = node.onConnectionsChange;
|
||
node.onConnectionsChange = function(...args) {
|
||
const result = previousConnections?.apply(this, args);
|
||
render();
|
||
return result;
|
||
};
|
||
const previousRemoved = node.onRemoved;
|
||
node.onRemoved = function(...args) {
|
||
this.__dumasCloseAutocomplete?.();
|
||
removeHoverPreview();
|
||
window.clearInterval(referenceThumbTimer);
|
||
return previousRemoved?.apply(this, args);
|
||
};
|
||
node.__dumasSequencePromptBuilder = refresh;
|
||
refresh();
|
||
}
|
||
|
||
// Browser console: dumasH3ReferenceStatus() shows exactly which image nodes
|
||
// were found, without exposing the user's image data.
|
||
window.dumasH3ReferenceStatus = () => {
|
||
const entries = (app.graph?._nodes || [])
|
||
.filter(node => node.comfyClass === CLASS && typeof node.__dumasReferencePreviewStatus === "function")
|
||
.flatMap(node => node.__dumasReferencePreviewStatus()
|
||
.filter(item => item.connected)
|
||
.map(item => ({builderId:node.id, ...item})));
|
||
console.table(entries);
|
||
return entries;
|
||
};
|
||
|
||
app.registerExtension({
|
||
name: "DUMAS.H3.SequencePromptBuilder",
|
||
nodeCreated(node) { setTimeout(() => configure(node), 0); },
|
||
loadedGraphNode(node) { setTimeout(() => configure(node), 0); },
|
||
afterConfigureGraph() { for (const node of app.graph?._nodes || []) configure(node); },
|
||
});
|