Files
ComfyUI-H3-Continuum-Tiled/web/sequence_prompt_builder.js
T
chris.dumas a89df61004
tests / test (push) Canceled after 0s
tests / windows-storage (push) Canceled after 0s
Add CSGlide-style timeline slot controls
2026-10-15 08:13:13 +00:00

126 lines
9.0 KiB
JavaScript

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 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 durationLabel = el("label", "Chunk duration (seconds)");
panel.append(durationLabel, 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(); }));
const heading = el("div", "Time slots are local to this clip; output is converted to absolute times."); heading.style.cssText = "margin:5px 0;color:#b7d6bf"; panel.append(heading);
clip.slots.forEach((slot, slotIndex) => {
const row = el("fieldset"); row.style.cssText = "border:1px solid #4a7255;margin:5px 0;padding:4px";
const legend = el("legend", `Slot ${slotIndex + 1}`); row.append(legend);
const track = el("div"); track.style.cssText = "height:7px;position:relative;margin:4px 0 7px;background:#15251a;border-radius:4px";
const fill = el("div"); fill.style.cssText = "height:100%;position:absolute;background:#79c98a;border-radius:4px";
const duration = Number(clip.duration) || 0.1;
const updateTrack = () => { fill.style.left = `${Math.max(0, Number(slot.start) / duration * 100)}%`; fill.style.width = `${Math.max(0, (Number(slot.end) - Number(slot.start)) / duration * 100)}%`; };
track.append(fill); row.append(track);
const times = el("div"); times.style.cssText = "display:flex;gap:4px";
const startSlider = input("range", slot.start, (value) => { slot.start = Math.min(Math.max(0, Number(value)), Number(slot.end) - 0.1); updateTrack(); setState(); });
startSlider.min = 0; startSlider.max = Math.max(0.1, duration - 0.1); startSlider.step = 0.1;
const endSlider = input("range", slot.end, (value) => { slot.end = Math.max(Number(slot.start) + 0.1, Math.min(duration, Number(value))); updateTrack(); setState(); });
endSlider.min = 0.1; endSlider.max = duration; endSlider.step = 0.1;
const startNumber = input("number", slot.start, (value) => { slot.start = Math.min(Math.max(0, Number(value) || 0), Number(slot.end) - 0.1); startSlider.value = slot.start; updateTrack(); setState(); });
const endNumber = input("number", slot.end, (value) => { slot.end = Math.max(Number(slot.start) + 0.1, Math.min(duration, Number(value) || duration)); endSlider.value = slot.end; updateTrack(); setState(); });
startNumber.step = endNumber.step = 0.1; startNumber.style.width = endNumber.style.width = "62px";
const startLabel = el("label", "Start"); startLabel.append(startSlider, startNumber);
const endLabel = el("label", "End"); endLabel.append(endSlider, endNumber);
times.append(startLabel, endLabel); row.append(times); updateTrack();
const text = el("textarea"); text.value = slot.text || ""; text.rows = 3; text.placeholder = "Describe this timed action. Type @ at the start of a new line to add the next slot."; text.style.cssText = "width:100%;box-sizing:border-box;color:#111";
text.onchange = () => { slot.text = text.value; setState(); };
text.onkeydown = (event) => {
if (event.key !== "@") return;
const cursor = text.selectionStart || 0;
if (cursor !== 0 && text.value[cursor - 1] !== "\n") return;
event.preventDefault(); addSlot(clip, slotIndex); setState(); render();
};
row.append(text);
const remove = el("button", "Remove slot"); remove.type = "button"; remove.onclick = () => { clip.slots.splice(slotIndex, 1); setState(); render(); }; row.append(remove); panel.append(row);
});
const add = el("button", "@ Add next time slot"); add.type = "button"; add.title = "Add a new slot after the last slot"; add.onclick = () => { addSlot(clip); setState(); render(); }; panel.append(add);
display.computeSize = (width) => [Math.max(360, Number(width) || 360), Math.min(620, 110 + clip.slots.length * 150)];
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); },
});