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: "" }], 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(stateWidget); 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 (afterIndex === clip.slots.length - 1 && afterIndex >= 0) { const previous = clip.slots[afterIndex]; const split = Number(previous.start) + Math.max(0.1, (duration - Number(previous.start)) / 2); previous.end = Math.min(duration - 0.1, split); clip.slots.splice(afterIndex + 1, 0, { start: previous.end, end: duration, text: "" }); return; } 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")}`; // 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 syncPrompt = (clip, value) => { const text = String(value || ""); // 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(); }); }; 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) => { // Adding/removing a shot is the one operation allowed to add/remove // marker lines. Preserve all existing shot text before formatting. syncPrompt(clip, clip.prompt || promptFor(clip)); clip.prompt = formatPromptFromSlots(clip); }; const render = () => { const count = Math.max(1, Math.min(MAX_CLIPS, Number(countWidget.value) || 1)); const chunkDuration = Number(secondsWidget.value) || 4; while (state.clips.length < count) state.clips.push({ duration: chunkDuration, 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 button = el("button", `Clip ${index + 1}`); button.type = "button"; button.disabled = index === active; button.style.cssText = `padding:3px 10px;border:1px solid ${index === active ? "#8bc99b" : "#555"};border-radius:3px;background:${index === active ? "#4b6f55" : "#333"};color:${index === active ? "#fff" : "#bbb"};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 durationRow = el("div"); durationRow.style.cssText = "display:flex;align-items:center;gap:8px;margin:4px 0 6px;color:#b7d6bf"; durationRow.append(el("strong", `Clip ${active + 1}`), el("span", `· ${duration.toFixed(1)}s shared chunk duration`)); panel.append(durationRow); 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; 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 = () => { prompt.focus(); }; 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); render(); }; 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 shotChips = el("div"); shotChips.style.cssText = "display:flex;gap:4px;overflow:hidden;margin:3px 0 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 = `max-width:180px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding:3px 8px;border:1px solid ${palette[index % palette.length]};border-radius:4px;background:#1e1e1e;color:#e6e6e6;font:11px Arial;cursor:pointer`; chip.onclick = () => { if (!prompt) return; const marker = `[Shot ${index + 1}]`; const position = prompt.value.indexOf(marker); prompt.focus(); if (position >= 0) prompt.setSelectionRange(position, position + marker.length); }; shotChips.append(chip); }); panel.append(shotChips); const referenceRow = el("div"); referenceRow.style.cssText = "display:flex;gap:8px;overflow:hidden;margin:7px 0 8px;align-items:center;flex-wrap:wrap"; const connectedReferences = Array.from({ length: 9 }, (_, offset) => offset + 1) .filter((index) => node.inputs?.some((input) => input.name === `reference_image_${index}` && input.link != null)); if (connectedReferences.length) { connectedReferences.forEach((index) => { const button = el("button"); button.type = "button"; button.title = `Insert reference image ${index} as @image${index}`; button.style.cssText = "display:inline-flex;align-items:center;gap:7px;height:31px;padding:2px 11px 2px 3px;border:2px solid #49c9f5;border-radius:18px;background:#202b31;color:#70d8ff;font:16px monospace;cursor:pointer;white-space:nowrap"; const thumbnail = document.createElement("img"); thumbnail.alt = ""; thumbnail.draggable = false; thumbnail.style.cssText = "width:25px;height:25px;object-fit:cover;border-radius:50%;background:#68727a;display:block"; const linkId = node.inputs?.find((input) => input.name === `reference_image_${index}`)?.link; const link = linkId == null ? null : (app.graph?.links?.get?.(linkId) ?? app.graph?.links?.[linkId]); const source = link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id); const imageWidget = source?.widgets?.find((widget) => ["image", "filename", "file"].includes(widget.name)); const imageValue = imageWidget?.value; if (imageValue && typeof imageValue === "object" && imageValue.filename) { const params = new URLSearchParams({ filename: imageValue.filename, type: imageValue.type || "input", subfolder: imageValue.subfolder || "" }); thumbnail.src = `/view?${params.toString()}`; } else if (typeof imageValue === "string" && imageValue) { const params = new URLSearchParams({ filename: imageValue, type: "input", subfolder: "" }); thumbnail.src = `/view?${params.toString()}`; } else { thumbnail.src = "data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='25' height='25' viewBox='0 0 25 25'%3E%3Crect width='25' height='25' fill='%2368727a'/%3E%3Cpath d='M4 19l5-6 3 3 3-4 6 7' fill='none' stroke='%23dce8ed' stroke-width='2'/%3E%3Ccircle cx='17' cy='8' r='2' fill='%23dce8ed'/%3E%3C/svg%3E"; } thumbnail.onerror = () => { thumbnail.style.opacity = "0.35"; }; button.append(thumbnail, el("span", `@image${index}`)); 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(); 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${index} `; prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end); const caret = start + token.length; prompt.focus(); prompt.setSelectionRange(caret, caret); promptSelectionStart = caret; promptSelectionEnd = caret; promptText = prompt.value; clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); persistState(); }; referenceRow.append(button); }); } 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 sourceThumbnail = (index) => { const thumbnail = document.createElement("img"); thumbnail.alt = ""; thumbnail.draggable = false; thumbnail.style.cssText = "width:24px;height:24px;object-fit:cover;border-radius:50%;background:#68727a;display:block;flex:none"; const linkId = node.inputs?.find((input) => input.name === `reference_image_${index}`)?.link; const link = linkId == null ? null : (app.graph?.links?.get?.(linkId) ?? app.graph?.links?.[linkId]); const source = link?.origin_id == null ? null : app.graph?.getNodeById?.(link.origin_id); const imageWidget = source?.widgets?.find((widget) => ["image", "filename", "file"].includes(widget.name)); const imageValue = imageWidget?.value; if (imageValue && typeof imageValue === "object" && imageValue.filename) { const params = new URLSearchParams({ filename: imageValue.filename, type: imageValue.type || "input", subfolder: imageValue.subfolder || "" }); thumbnail.src = `/view?${params.toString()}`; } else if (typeof imageValue === "string" && imageValue) { const params = new URLSearchParams({ filename: imageValue, type: "input", subfolder: "" }); thumbnail.src = `/view?${params.toString()}`; } thumbnail.onerror = () => { thumbnail.style.opacity = "0.35"; }; return thumbnail; }; 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"; const line = prompt.value.slice(0, promptSelectionStart).split("\n").length - 1; menu.style.top = `${Math.min(Math.max(4, prompt.clientHeight - 150), 4 + line * 17)}px`; const connected = Array.from({ length: 9 }, (_, offset) => offset + 1) .filter((index) => node.inputs?.some((input) => input.name === `reference_image_${index}` && input.link != null)); 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); }; connected.forEach((index) => addOption([ sourceThumbnail(index), el("span", `@image${index}`), (() => { const hint = el("span", ``); hint.style.cssText = "margin-left:auto;color:#84939a;font-size:10px"; return hint; })(), ], `Insert @R${index}`, () => { const start = autocompleteRange?.start ?? prompt.value.length; const end = autocompleteRange?.end ?? start; const token = `@R${index} `; prompt.value = prompt.value.slice(0, start) + token + prompt.value.slice(end); const caret = start + token.length; prompt.focus(); 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(); }); promptFrame?.append(menu); autocomplete = menu; }; prompt.oninput = () => { rememberPromptSelection(); promptText = prompt.value; clip.prompt = prompt.value; }; 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"; promptFrame.append(prompt); panel.append(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); const 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); }, });