1158 lines
66 KiB
JavaScript
1158 lines
66 KiB
JavaScript
import { app } from "../../scripts/app.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");
|
||
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: 8px;
|
||
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: 6px; 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: 5px;
|
||
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: 5px; 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: 50%;
|
||
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;
|
||
}
|
||
`;
|
||
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;
|
||
}
|
||
|
||
|
||
function linkedNode(node, inputName) {
|
||
const input = node.inputs?.find((item) => item.name === inputName);
|
||
if (input?.link == null) return null;
|
||
const link = app.graph?.links?.get?.(input.link) ?? app.graph?.links?.[input.link];
|
||
return link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id);
|
||
}
|
||
|
||
function widgetValue(node, name) {
|
||
return get(node, name)?.value;
|
||
}
|
||
|
||
function refModDescriptorsForNode(node, 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;
|
||
// 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)];
|
||
if (!markers.length) return false;
|
||
const duration = Math.max(0.1, Number(clip.duration) || 0.1);
|
||
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[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 = () => {
|
||
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.style.cssText = "display:flex;flex-wrap:wrap;gap:4px;margin-bottom:8px;border-bottom:1px solid #444;padding-bottom:6px";
|
||
state.clips.forEach((clip, index) => {
|
||
const enabled = clip.enabled !== false;
|
||
const button = el("button", `${enabled ? "" : "× "}Clip ${index + 1}`); button.type = "button"; button.disabled = index === active;
|
||
button.style.cssText = `padding:3px 10px;border:1px solid ${index === active ? (enabled ? "#8bc99b" : "#c98b8b") : "#555"};border-radius:3px;background:${index === active ? (enabled ? "#4b6f55" : "#6f4b4b") : "#333"};color:${index === active ? "#fff" : (enabled ? "#bbb" : "#888")};cursor:pointer`;
|
||
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.style.cssText = "display:flex;align-items:center;gap:8px;margin:0 0 8px;color:#b7d6bf";
|
||
const durationLabel = el("span", "Chunk duration / frame choice");
|
||
durationLabel.style.cssText = "font-weight:bold;white-space:nowrap";
|
||
const durationSelect = document.createElement("select");
|
||
durationSelect.style.cssText = "flex:1;min-width:0;box-sizing:border-box;padding:4px 6px;color:#e8e8e8;background:#171717;border:1px solid #596b60;border-radius:4px;font:11px monospace";
|
||
DURATION_OPTIONS.forEach((option) => {
|
||
const item = document.createElement("option");
|
||
item.value = String(option.seconds);
|
||
item.textContent = `${option.actual} Real Frames - ${option.latent} Latent Frames - ${option.labelSeconds.toFixed(2)}s`;
|
||
item.selected = option === selectedDuration;
|
||
durationSelect.append(item);
|
||
});
|
||
durationSelect.title = "Choose a native H3 frame length; actual frames are encoded from the latent-frame choice.";
|
||
durationSelect.onchange = () => {
|
||
secondsWidget.value = Number(durationSelect.value);
|
||
secondsWidget.callback?.(secondsWidget.value);
|
||
};
|
||
durationControl.append(durationLabel, durationSelect);
|
||
panel.append(durationControl);
|
||
const durationRow = el("div"); durationRow.style.cssText = "display:flex;align-items:center;gap:8px;margin:4px 0 6px;color:#b7d6bf";
|
||
const enabledLabel = el("label"); enabledLabel.style.cssText = "display:inline-flex;align-items:center;gap:4px;margin-left:auto;color:#c7d8cb;font-weight:normal;cursor:pointer";
|
||
const enabledToggle = document.createElement("input"); enabledToggle.type = "checkbox"; enabledToggle.checked = clip.enabled !== false; enabledToggle.title = "Include this chunk in the output sockets";
|
||
enabledToggle.onchange = () => { clip.enabled = enabledToggle.checked; persistState(); render(); };
|
||
enabledLabel.append(enabledToggle, el("span", "Enabled"));
|
||
durationRow.append(el("strong", `Clip ${active + 1}`), el("span", `· ${selectedDuration.actual} actual frames · ${selectedDuration.latent} latent frames · ${selectedDuration.labelSeconds.toFixed(2)}s shared chunk duration`), 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:5px;align-items:center;flex-wrap:wrap;min-height:38px;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: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}`));
|
||
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);
|
||
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:8px;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:5px;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([
|
||
(() => { 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; })(),
|
||
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:50%;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);
|
||
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;
|
||
syncPrompt(clip, clip.prompt);
|
||
persistState();
|
||
if (clip.slots.length === segments.length) 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);
|
||
const actions = el("div"); actions.style.cssText = "display:flex;gap:5px;margin-top:5px";
|
||
const add = el("button", "@ Add shot"); add.type = "button"; add.onclick = () => { clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); addSlot(clip); rebuildPromptAfterStructureChange(clip); setState(); render(); }; actions.append(add);
|
||
if (clip.slots.length > 1) { const remove = el("button", "Remove last shot"); remove.type = "button"; remove.onclick = () => { clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); clip.slots.pop(); clip.slots.at(-1).end = clip.duration; rebuildPromptAfterStructureChange(clip); setState(); render(); }; actions.append(remove); }
|
||
panel.append(actions);
|
||
// 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?.();
|
||
return previousRemoved?.apply(this, args);
|
||
};
|
||
node.__dumasSequencePromptBuilder = refresh;
|
||
refresh();
|
||
}
|
||
|
||
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); },
|
||
});
|