From 616a38fc9e4c7ffda324b2393be09739730fc120 Mon Sep 17 00:00:00 2001 From: Chris Dumas Date: Thu, 15 Oct 2026 08:57:55 +0000 Subject: [PATCH] Match CSGlide prompt editing lifecycle --- web/sequence_prompt_builder.js | 95 +++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 19 deletions(-) diff --git a/web/sequence_prompt_builder.js b/web/sequence_prompt_builder.js index 77960d4..437ff00 100644 --- a/web/sequence_prompt_builder.js +++ b/web/sequence_prompt_builder.js @@ -7,7 +7,7 @@ const el = (tag, text = "") => { const item = document.createElement(tag); item. function defaultState(count, seconds) { return { version: 1, clips: Array.from({ length: count }, () => ({ - duration: seconds, slots: [{ start: 0, end: seconds, text: "" }], + duration: seconds, slots: [{ start: 0, end: seconds, text: "" }], prompt: "", })) }; } @@ -42,11 +42,17 @@ function configure(node) { let active = 0; let state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4); let promptText = null; - const setState = () => { + // 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); - stateWidget.callback?.(stateWidget.value); 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" @@ -72,8 +78,15 @@ function configure(node) { }); }; 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"); + // 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 promptFor = (clip) => { + if (typeof clip.prompt === "string" && clip.prompt.length) return clip.prompt; + return clip.slots.map((slot, index) => + `[Shot ${index + 1}] At ${formatTime(slot.start)}, ${slot.text || ""}`).join("\n\n"); + }; const syncPrompt = (clip, value) => { const text = String(value || ""); const marks = [...text.matchAll(/\[Shot\s+(\d+)\][^\n]*(?:\n|$)/gi)]; @@ -93,6 +106,21 @@ function configure(node) { 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 = promptFor(clip); + }; const render = () => { const count = Math.max(1, Math.min(MAX_CLIPS, Number(countWidget.value) || 1)); const chunkDuration = Number(secondsWidget.value) || 4; @@ -133,26 +161,43 @@ function configure(node) { 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:12px;height:100%;transform:translateX(-50%);background:#f1f7f2;border:2px solid #18261c;border-radius:3px;cursor:ew-resize;z-index:5;box-sizing:border-box"; + 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; syncPrompt(clip, promptText); } + if (prompt) { promptText = prompt.value; clip.prompt = promptText; } handle.setPointerCapture?.(event.pointerId); - handle.onpointermove = move; - handle.onpointerup = () => { - handle.onpointermove = null; - promptText = retimePrompt(promptText, clip); - if (prompt) { prompt.value = promptText; syncPrompt(clip, promptText); } - setState(); render(); - }; + 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); }); @@ -160,13 +205,25 @@ function configure(node) { panel.append(timeline); const sliderHint = el("div", "Drag the white handles on the bar to resize shots. The bar always represents the complete chunk duration."); sliderHint.style.cssText = "color:#b7d6bf;margin-bottom:4px"; panel.append(sliderHint); prompt = el("textarea"); prompt.rows = 9; 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:#111;font:12px monospace;margin-top:6px"; - prompt.oninput = () => { promptText = prompt.value; }; - 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(); }; + prompt.oninput = () => { promptText = prompt.value; clip.prompt = prompt.value; }; + prompt.onblur = () => { if (prompt) { 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; + const cursor = prompt.selectionStart || 0; + if (cursor !== 0 && prompt.value[cursor - 1] !== "\n") return; + event.preventDefault(); + clip.prompt = prompt.value; + syncPrompt(clip, clip.prompt); + addSlot(clip); + rebuildPromptAfterStructureChange(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); } + 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); display.computeSize = (width) => [Math.max(360, Number(width) || 360), Math.min(620, 160 + clip.slots.length * 42)]; node.setSize?.([Math.max(360, Number(node.size?.[0]) || 360), Number(node.computeSize?.()[1]) || 420]);