92 lines
6.4 KiB
JavaScript
92 lines
6.4 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 = "width:100%;box-sizing:border-box;margin:2px 0;color:#111";
|
|
item.onchange = () => onChange(item.value); return item;
|
|
};
|
|
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 times = el("div"); times.style.cssText = "display:flex;gap:4px";
|
|
times.append(el("span", "Start"), input("number", slot.start, (value) => { slot.start = Math.max(0, Number(value) || 0); setState(); }), el("span", "End"), input("number", slot.end, (value) => { slot.end = Math.max(slot.start + 0.1, Number(value) || slot.start + 0.1); setState(); })); row.append(times);
|
|
const text = el("textarea"); text.value = slot.text || ""; text.rows = 3; text.style.cssText = "width:100%;box-sizing:border-box;color:#111"; text.onchange = () => { slot.text = text.value; setState(); }; 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 time slot"); add.type = "button"; add.onclick = () => { const start = clip.slots.length ? Number(clip.slots.at(-1).end) : 0; clip.slots.push({ start, end: Math.min(start + 1, Number(clip.duration)), text: "" }); 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); },
|
|
});
|