diff --git a/web/sequence_prompt_builder.js b/web/sequence_prompt_builder.js index e15a112..076eb3d 100644 --- a/web/sequence_prompt_builder.js +++ b/web/sequence_prompt_builder.js @@ -113,20 +113,41 @@ function configure(node) { }; 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: "" }); + if (!clip.slots.length) { + clip.slots.push({ start: 0, 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 newCount = clip.slots.length + 1; + // Keep a minimum only when the selected duration allows it. The old + // fixed 0.1s floor made later shots collapse at short frame choices. + const minSlice = Math.min(0.1, duration / newCount); + let splitIndex = Math.max(0, Math.min(afterIndex, clip.slots.length - 1)); + const lengthOf = (slot) => Math.max(0, Number(slot.end) - Number(slot.start)); + if (lengthOf(clip.slots[splitIndex]) < minSlice * 2) { + splitIndex = clip.slots.reduce( + (best, slot, index) => lengthOf(slot) > lengthOf(clip.slots[best]) ? index : best, + 0, + ); + } + const target = clip.slots[splitIndex]; + const start = Number(target?.start) || 0; + const end = Math.min(duration, Number(target?.end) || duration); + let split = start + (end - start) / 2; + split = Math.max(start + minSlice, Math.min(end - minSlice, split)); + if (!(split > start && split < end)) { + // This is only reachable for malformed or extremely short state; + // redistribute boundaries evenly so the new shot is still real. + const texts = clip.slots.map((slot) => slot.text || ""); + texts.splice(splitIndex + 1, 0, ""); + clip.slots = texts.map((text, index) => ({ + start: duration * index / newCount, + end: duration * (index + 1) / newCount, + text, + })); + return; + } + target.end = split; + clip.slots.splice(splitIndex + 1, 0, { start: split, end, 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