701 lines
44 KiB
JavaScript
701 lines
44 KiB
JavaScript
import { app } from "../../scripts/app.js";
|
||
|
||
const CLASS = "DumasH3SequencePromptBuilder";
|
||
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"]],
|
||
];
|
||
const get = (node, name) => node.widgets?.find((widget) => widget.name === name);
|
||
const el = (tag, text = "") => { const item = document.createElement(tag); item.textContent = text; return item; };
|
||
|
||
function linkedNode(node, inputName) {
|
||
const input = node.inputs?.find((item) => item.name === inputName);
|
||
if (input?.link == null) return null;
|
||
const link = app.graph?.links?.get?.(input.link) ?? app.graph?.links?.[input.link];
|
||
return link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id);
|
||
}
|
||
|
||
function widgetValue(node, name) {
|
||
return get(node, name)?.value;
|
||
}
|
||
|
||
function refModDescriptorsForNode(node, 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;
|
||
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;
|
||
// 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);
|
||
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);
|
||
}
|
||
const timeline = el("div"); timeline.style.cssText = "position:relative;height:38px;margin:8px 0 4px;background:#171717;border-radius:4px;overflow:hidden;border:1px solid #555";
|
||
const palette = ["#4b9b67", "#4d7fb1", "#a87945", "#8c5aa8", "#a44f5d", "#3f8e8a"];
|
||
let prompt = null;
|
||
let promptSelectionStart = 0;
|
||
let promptSelectionEnd = 0;
|
||
let autocomplete = null;
|
||
let autocompleteRange = null;
|
||
let promptFrame = null;
|
||
let suggestionLayer = null;
|
||
let encoder = null;
|
||
const focusPrompt = () => {
|
||
if (!prompt) return;
|
||
try {
|
||
prompt.focus({ preventScroll: true });
|
||
} catch (_) {
|
||
prompt.focus();
|
||
}
|
||
};
|
||
const segments = [];
|
||
const handles = [];
|
||
const updateTimeline = () => {
|
||
clip.slots.forEach((slot, index) => {
|
||
const left = Math.max(0, Number(slot.start) / duration * 100);
|
||
const width = Math.max(0.1, (Number(slot.end) - Number(slot.start)) / duration * 100);
|
||
segments[index].style.left = `${left}%`;
|
||
segments[index].style.width = `${width}%`;
|
||
segments[index].title = `${Number(slot.start).toFixed(1)}s–${Number(slot.end).toFixed(1)}s`;
|
||
});
|
||
handles.forEach(({ handle, slot }) => { handle.style.left = `${Number(slot.end) / duration * 100}%`; });
|
||
};
|
||
clip.slots.forEach((slot, index) => {
|
||
const segment = el("button", `Shot ${index + 1}\n${(slot.text || "unscripted").slice(0, 18)}`); segment.type = "button";
|
||
segment.style.cssText = `position:absolute;top:0;height:100%;border:0;border-right:2px solid #292929;color:white;background:${palette[index % palette.length]};white-space:pre-wrap;font-size:10px;overflow:hidden;padding:2px;cursor:pointer`;
|
||
segment.onclick = (event) => {
|
||
event.preventDefault(); event.stopPropagation();
|
||
focusPrompt();
|
||
}; segments.push(segment); timeline.append(segment);
|
||
});
|
||
clip.slots.slice(0, -1).forEach((slot, index) => {
|
||
const next = clip.slots[index + 1];
|
||
const handle = el("div"); handle.title = `Resize boundary between Shot ${index + 1} and Shot ${index + 2}`;
|
||
handle.style.cssText = "position:absolute;top:0;width:17px;height:100%;transform:translateX(-50%);background:transparent;border:0;border-radius:3px;cursor:ew-resize;z-index:5;box-sizing:border-box;touch-action:none";
|
||
const ink = el("div");
|
||
ink.style.cssText = "position:absolute;left:7px;top:0;width:2px;height:100%;background:#f1f7f2;border:0;border-radius:1px;pointer-events:none";
|
||
handle.append(ink);
|
||
const move = (event) => {
|
||
event.preventDefault(); event.stopPropagation();
|
||
const rect = timeline.getBoundingClientRect();
|
||
const point = Math.min(duration - 0.1, Math.max(0.1, ((event.clientX - rect.left) / rect.width) * duration));
|
||
slot.end = point; next.start = point; updateTimeline();
|
||
};
|
||
let dragging = false;
|
||
const finish = (event) => {
|
||
if (!dragging) return;
|
||
dragging = false;
|
||
event?.preventDefault?.(); event?.stopPropagation?.();
|
||
document.removeEventListener("pointermove", move, true);
|
||
window.removeEventListener("pointerup", finish, true);
|
||
document.removeEventListener("mousemove", moveMouse, true);
|
||
document.removeEventListener("mouseup", finish, true);
|
||
promptText = retimePrompt(promptText ?? prompt?.value ?? clip.prompt ?? "", clip);
|
||
commitPrompt(clip, promptText);
|
||
// The timeline is already updated in place. Avoid rebuilding
|
||
// the panel, which moves the textarea and canvas viewport.
|
||
if (prompt) prompt.value = promptText;
|
||
if (encoder) encoder.textContent = clip.slots
|
||
.map((item, itemIndex) => `Shot ${itemIndex + 1} ${formatTime(item.start)}`)
|
||
.join(" ");
|
||
};
|
||
const moveMouse = (event) => { if (dragging) move(event); };
|
||
handle.onpointerdown = (event) => {
|
||
event.preventDefault();
|
||
event.stopPropagation();
|
||
dragging = true;
|
||
// Textarea edits are intentionally committed on change, but a
|
||
// drag can happen while it still has focus. Capture them before
|
||
// the timeline redraws the prompt from state.
|
||
if (prompt) { promptText = prompt.value; clip.prompt = promptText; }
|
||
handle.setPointerCapture?.(event.pointerId);
|
||
document.addEventListener("pointermove", move, true);
|
||
window.addEventListener("pointerup", finish, true);
|
||
document.addEventListener("mousemove", moveMouse, true);
|
||
document.addEventListener("mouseup", finish, true);
|
||
};
|
||
handles.push({ handle, slot }); timeline.append(handle);
|
||
});
|
||
updateTimeline();
|
||
panel.append(timeline);
|
||
const sliderHint = el("div", "Drag the white handles to resize shots. The bar represents the complete chunk duration."); sliderHint.style.cssText = "color:#aab6ad;margin-bottom:6px;font-size:11px"; panel.append(sliderHint);
|
||
|
||
const shotLabel = el("div", "SHOT TRACK"); shotLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:4px 0 3px"; panel.append(shotLabel);
|
||
const shotChips = el("div"); shotChips.style.cssText = "display:flex;gap:5px;overflow:hidden;margin:3px 0 7px;padding:4px;background:#202020;border:1px solid #3f4742;border-radius:5px";
|
||
clip.slots.forEach((slot, index) => {
|
||
const chipText = (slot.text || `Shot ${index + 1}`).replace(/\s+/g, " ").trim().slice(0, 31);
|
||
const chip = el("button", chipText || `Shot ${index + 1}`); chip.type = "button";
|
||
chip.title = `Jump to Shot ${index + 1}`;
|
||
chip.style.cssText = `flex:1 1 90px;min-width:70px;max-width:210px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding:5px 9px;border:1px solid ${palette[index % palette.length]};border-radius:5px;background:${palette[index % palette.length]}33;color:#e6e6e6;font:11px Arial;cursor:pointer`;
|
||
chip.onclick = (event) => {
|
||
event.preventDefault(); event.stopPropagation();
|
||
if (!prompt) return;
|
||
const marker = `[Shot ${index + 1}]`;
|
||
const position = prompt.value.indexOf(marker);
|
||
focusPrompt();
|
||
if (position >= 0) prompt.setSelectionRange(position, position + marker.length);
|
||
};
|
||
shotChips.append(chip);
|
||
});
|
||
panel.append(shotChips);
|
||
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 = () => {
|
||
autocomplete?.remove();
|
||
autocomplete = null;
|
||
autocompleteRange = null;
|
||
};
|
||
const openAutocomplete = (at) => {
|
||
closeAutocomplete();
|
||
autocompleteRange = { start: at, end: at + 1 };
|
||
const menu = el("div");
|
||
menu.style.cssText = "display:block;visibility:visible;opacity:1;position:absolute;z-index:100;left:4px;min-width:220px;max-width:310px;padding:5px;background:#25282b;border:1px solid #4b5960;border-radius:8px;box-shadow:0 8px 24px #000b;color:#e7edf0;pointer-events:auto";
|
||
menu.style.top = "0px";
|
||
const addOption = (content, title, action) => {
|
||
const option = el("button"); option.type = "button"; option.title = title;
|
||
option.style.cssText = "display:flex;align-items:center;gap:9px;width:100%;padding:6px 8px;border:0;border-radius:5px;background:transparent;color:#e7edf0;text-align:left;font:12px monospace;cursor:pointer";
|
||
option.onpointerdown = (event) => { event.preventDefault(); event.stopPropagation(); };
|
||
option.onmouseenter = () => { option.style.background = "#3b5661"; };
|
||
option.onmouseleave = () => { option.style.background = "transparent"; };
|
||
option.onclick = (event) => { event.preventDefault(); event.stopPropagation(); action(); };
|
||
option.append(...content); menu.append(option);
|
||
};
|
||
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"; 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;
|
||
const token = `@R${slot} `;
|
||
prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end);
|
||
const caret = start + token.length;
|
||
focusPrompt(); prompt.setSelectionRange(caret, caret);
|
||
promptSelectionStart = caret; promptSelectionEnd = caret;
|
||
promptText = prompt.value; clip.prompt = prompt.value;
|
||
syncPrompt(clip, clip.prompt); persistState(); closeAutocomplete();
|
||
});
|
||
});
|
||
addOption([
|
||
(() => { const icon = el("span", "S"); icon.style.cssText = "display:grid;place-items:center;width:24px;height:24px;border-radius:50%;background:#397a91;color:#dff7ff;font-weight:bold"; return icon; })(),
|
||
el("span", "[Shot " + (clip.slots.length + 1) + "]"),
|
||
(() => { const hint = el("span", "new shot"); hint.style.cssText = "margin-left:auto;color:#84939a;font-size:10px"; return hint; })(),
|
||
], "Create the next timed shot", () => {
|
||
const start = autocompleteRange?.start ?? prompt.value.length;
|
||
prompt.value = prompt.value.slice(0, start) + prompt.value.slice(start + 1);
|
||
promptText = prompt.value; clip.prompt = prompt.value;
|
||
syncPrompt(clip, clip.prompt); addSlot(clip); rebuildPromptAfterStructureChange(clip);
|
||
closeAutocomplete(); setState(); render();
|
||
});
|
||
panel.append(menu);
|
||
menu.style.left = `${Math.max(5, (suggestionLayer?.offsetLeft || 0) + 4)}px`;
|
||
menu.style.top = `${Math.max(5, suggestionLayer?.offsetTop || 0)}px`;
|
||
autocomplete = menu;
|
||
};
|
||
prompt.oninput = () => { rememberPromptSelection(); promptText = prompt.value; clip.prompt = prompt.value; };
|
||
prompt.onpaste = () => {
|
||
// 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 = rememberPromptSelection;
|
||
prompt.onmouseup = rememberPromptSelection;
|
||
prompt.onblur = () => { rememberPromptSelection(); clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); persistState(); };
|
||
prompt.onchange = () => { commitPrompt(clip, prompt.value); };
|
||
prompt.onpointerdown = (event) => event.stopPropagation();
|
||
prompt.onkeydown = (event) => {
|
||
if (event.key !== "@") return;
|
||
event.preventDefault();
|
||
const start = prompt.selectionStart ?? prompt.value.length;
|
||
const end = prompt.selectionEnd ?? start;
|
||
prompt.value = prompt.value.slice(0, start) + "@" + prompt.value.slice(end);
|
||
prompt.setSelectionRange(start + 1, start + 1);
|
||
promptSelectionStart = start;
|
||
promptSelectionEnd = start + 1;
|
||
promptText = prompt.value; clip.prompt = prompt.value;
|
||
syncPrompt(clip, clip.prompt); persistState();
|
||
openAutocomplete(start);
|
||
};
|
||
promptFrame = el("div");
|
||
promptFrame.style.cssText = "position:relative;overflow:visible;width:100%;box-sizing:border-box";
|
||
suggestionLayer = el("div");
|
||
suggestionLayer.style.cssText = "position:relative;z-index:100;height:0;width:100%;pointer-events:none;overflow:visible";
|
||
promptFrame.append(prompt);
|
||
panel.append(suggestionLayer);
|
||
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;
|
||
};
|
||
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); },
|
||
});
|