From abe31760cda36cc686a7d57902812a1f7fa6d5a9 Mon Sep 17 00:00:00 2001 From: Chris Dumas Date: Thu, 15 Oct 2026 08:51:13 +0000 Subject: [PATCH] Preserve raw script while resizing shots --- web/sequence_prompt_builder.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/web/sequence_prompt_builder.js b/web/sequence_prompt_builder.js index 023c8ea..77960d4 100644 --- a/web/sequence_prompt_builder.js +++ b/web/sequence_prompt_builder.js @@ -41,6 +41,7 @@ function configure(node) { display.options ||= {}; display.options.serialize = false; let active = 0; let state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4); + let promptText = null; const setState = () => { stateWidget.value = JSON.stringify(state); stateWidget.callback?.(stateWidget.value); @@ -74,10 +75,22 @@ function configure(node) { 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 text = String(value || ""); + const marks = [...text.matchAll(/\[Shot\s+(\d+)\][^\n]*(?:\n|$)/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 render = () => { @@ -94,6 +107,7 @@ function configure(node) { 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;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:44px;margin:8px 0 3px;background:#15251a;border-radius:3px;overflow:hidden;border:1px solid #41634a"; @@ -130,12 +144,13 @@ function configure(node) { // 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) syncPrompt(clip, prompt.value); + if (prompt) { promptText = prompt.value; syncPrompt(clip, promptText); } handle.setPointerCapture?.(event.pointerId); handle.onpointermove = move; handle.onpointerup = () => { handle.onpointermove = null; - if (prompt) syncPrompt(clip, prompt.value); + promptText = retimePrompt(promptText, clip); + if (prompt) { prompt.value = promptText; syncPrompt(clip, promptText); } setState(); render(); }; }; @@ -144,7 +159,8 @@ function configure(node) { updateTimeline(); 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 = 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 = 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(); }; panel.append(prompt);