Files
ComfyUI-H3-Continuum-Tiled/web/sequence_prompt_builder.js
T
chris.dumas 34e8c26b1d
tests / test (push) Canceled after 0s
tests / windows-storage (push) Canceled after 0s
Match sequence editor to CSGlide timeline UI
2026-10-15 08:18:36 +00:00

124 lines
9.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { app } from "../../scripts/app.js";
const CLASS = "DumasH3SequencePromptBuilder";
const MAX_CLIPS = 16;
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 defaultState(count, seconds) {
return { version: 1, clips: Array.from({ length: count }, () => ({
duration: seconds, slots: [{ start: 0, end: seconds, text: "" }],
})) };
}
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(stateWidget);
const panel = el("div");
panel.style.cssText = "box-sizing:border-box;min-width:360px;padding:7px;color:#e7f4e9;background:#203827;font:12px Arial;pointer-events:auto";
const host = el("div"); host.style.cssText = "pointer-events:auto"; 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 });
display.options ||= {}; display.options.serialize = false;
let active = 0;
let state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4);
const setState = () => {
stateWidget.value = JSON.stringify(state);
stateWidget.callback?.(stateWidget.value);
node.setDirtyCanvas?.(true, true);
};
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;
const previousEnd = afterIndex >= 0 ? Number(clip.slots[afterIndex]?.end) || 0 : 0;
const remaining = Math.max(0.1, duration - previousEnd);
clip.slots.splice(afterIndex + 1, 0, {
start: previousEnd,
end: Math.min(duration, previousEnd + Math.max(0.1, remaining / 2)),
text: "",
});
};
const formatTime = (seconds) => `00:${Number(seconds).toFixed(3).padStart(6, "0")}`;
const promptFor = (clip) => clip.slots.map((slot, index) =>
`[Shot ${index + 1}] At ${formatTime(slot.start)}, ${slot.text || ""}`).join("\n\n");
const syncPrompt = (clip, value) => {
const blocks = String(value || "").split(/\n\s*\n(?=\[Shot\s+\d+\])/i);
blocks.forEach((block, index) => {
if (!clip.slots[index]) return;
clip.slots[index].text = block.replace(/^\[Shot\s+\d+\][^\n]*\n?/i, "").trim();
});
};
const render = () => {
const count = Math.max(1, Math.min(MAX_CLIPS, Number(countWidget.value) || 1));
while (state.clips.length < count) state.clips.push({ duration: Number(secondsWidget.value) || 4, 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:3px;margin-bottom:6px";
state.clips.forEach((clip, index) => { const button = el("button", `Clip ${index + 1}`); button.type = "button"; button.disabled = index === active; button.onclick = () => { active = index; render(); }; tabs.append(button); });
panel.append(tabs);
const clip = state.clips[active];
const durationRow = el("div"); durationRow.style.cssText = "display:flex;align-items:center;gap:8px;margin:4px 0";
durationRow.append(el("strong", `Clip ${active + 1}`), el("span", "duration"));
const duration = input("number", clip.duration, (value) => { clip.duration = Math.max(0.1, Number(value) || 0.1); clip.slots.forEach((slot) => { slot.end = Math.min(slot.end, clip.duration); }); setState(); render(); });
duration.step = 0.1; duration.style.width = "70px"; durationRow.append(duration, el("span", "seconds")); panel.append(durationRow);
const timeline = el("div"); timeline.style.cssText = "display:flex;height:38px;gap:2px;margin:8px 0 3px;background:#15251a;border-radius:3px;overflow:hidden";
const palette = ["#4b9b67", "#4d7fb1", "#a87945", "#8c5aa8", "#a44f5d", "#3f8e8a"];
clip.slots.forEach((slot, index) => { const segment = el("button", `Shot ${index + 1}\n${(slot.text || "unscripted").slice(0, 18)}`); segment.type = "button"; segment.title = `${slot.start.toFixed(1)}s–${slot.end.toFixed(1)}s`; segment.style.cssText = `flex:${Math.max(0.1, slot.end - slot.start)};border:0;color:white;background:${palette[index % palette.length]};white-space:pre-wrap;font-size:10px;overflow:hidden`; segment.onclick = () => { prompt.focus(); }; timeline.append(segment); });
panel.append(timeline);
const sliderHint = el("div", "Drag shot boundaries to retime the prompt. Grey areas are unscripted."); sliderHint.style.cssText = "color:#b7d6bf;margin-bottom:4px"; panel.append(sliderHint);
clip.slots.slice(0, -1).forEach((slot, index) => {
const next = clip.slots[index + 1]; const boundary = input("range", slot.end, (value) => { const point = Math.min(Number(clip.duration) - 0.1, Math.max(0.1, Number(value))); slot.end = point; next.start = point; setState(); render(); });
boundary.min = 0.1; boundary.max = Math.max(0.1, Number(clip.duration) - 0.1); boundary.step = 0.1;
const row = el("label", `Shot ${index + 1} / ${index + 2} · ${Number(slot.end).toFixed(1)}s`); row.style.cssText = "display:block;color:#d9f0dc"; row.append(boundary); panel.append(row);
});
const prompt = el("textarea"); prompt.rows = 9; prompt.value = promptFor(clip); 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:#111;font:12px monospace;margin-top:6px";
prompt.onchange = () => { syncPrompt(clip, prompt.value); setState(); render(); };
prompt.onkeydown = (event) => { if (event.key !== "@") return; const cursor = prompt.selectionStart || 0; if (cursor !== 0 && prompt.value[cursor - 1] !== "\n") return; event.preventDefault(); addSlot(clip); setState(); render(); };
panel.append(prompt);
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 = () => { addSlot(clip); setState(); render(); }; actions.append(add);
if (clip.slots.length > 1) { const remove = el("button", "Remove last shot"); remove.type = "button"; remove.onclick = () => { clip.slots.pop(); clip.slots.at(-1).end = clip.duration; setState(); render(); }; actions.append(remove); }
panel.append(actions);
display.computeSize = (width) => [Math.max(360, Number(width) || 360), Math.min(620, 185 + clip.slots.length * 42)];
node.setSize?.([Math.max(360, Number(node.size?.[0]) || 360), Number(node.computeSize?.()[1]) || 420]);
};
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); render(); return result; })(secondsWidget.callback);
stateWidget.callback = ((previous) => function(...args) { const result = previous?.apply(this, args); render(); return result; })(stateWidget.callback);
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); },
});