diff --git a/js/sequence_prompt_builder.js b/js/sequence_prompt_builder.js index d3eb2be..35a8cc6 100644 --- a/js/sequence_prompt_builder.js +++ b/js/sequence_prompt_builder.js @@ -31,6 +31,150 @@ const CAMERA_HELPERS = [ ["Roll", ["The camera rolls clockwise", "The camera rolls counterclockwise"]], ["Shake", ["The camera shakes slightly", "The camera shakes strongly"]], ]; + +// Scoped to this node's DOM widget; no ComfyUI global styles are changed. +function ensureTimelineStyles() { + if (document.getElementById("dumas-h3-shot-timeline-styles")) return; + const style = document.createElement("style"); + style.id = "dumas-h3-shot-timeline-styles"; + style.textContent = ` + .dumas-h3-shot-timeline { + --h3-track-bg: #1b2022; + margin: 9px 0 10px; + padding: 10px; + border: 1px solid #404b4b; + border-radius: 8px; + background: #222829; + color: #e5ebea; + font: 11px/1.35 Arial, sans-serif; + } + .dumas-h3-shot-timeline * { box-sizing: border-box; } + .dumas-h3-shot-timeline__heading, + .dumas-h3-shot-timeline__footer, + .dumas-h3-shot-timeline__selection { + display: flex; align-items: center; gap: 6px; min-width: 0; + } + .dumas-h3-shot-timeline__heading { justify-content: space-between; margin-bottom: 11px; } + .dumas-h3-shot-timeline__heading strong { + font-size: 11px; font-weight: 700; letter-spacing: .07em; + } + .dumas-h3-shot-timeline__meta { + color: #9fadad; font: 10px/1.3 ui-monospace, Consolas, monospace; white-space: nowrap; + } + .dumas-h3-shot-timeline__ruler { + position: relative; height: 23px; margin: 0 6px; + border-bottom: 1px solid #445052; + background: repeating-linear-gradient(to right, + transparent 0, transparent calc(5% - 1px), #394547 calc(5% - 1px), #394547 5%); + background-size: 100% 7px; background-repeat: no-repeat; background-position: left bottom; + } + .dumas-h3-shot-timeline__tick { + position: absolute; bottom: 8px; transform: translateX(-50%); + color: #9caaab; font: 10px ui-monospace, Consolas, monospace; + white-space: nowrap; user-select: none; + } + .dumas-h3-shot-timeline__tick:first-child { transform: none; } + .dumas-h3-shot-timeline__tick:last-child { transform: translateX(-100%); } + .dumas-h3-shot-timeline__track { + position: relative; height: 62px; margin: 7px 6px 0; + background: var(--h3-track-bg); border: 1px solid #3a4548; + border-radius: 6px; overflow: visible; + } + .dumas-h3-shot-timeline__segment { + position: absolute; display: flex; flex-direction: column; + align-items: stretch; justify-content: center; + gap: 4px; top: 0; height: 100%; min-width: 0; margin: 0; + padding: 6px 8px; color: #e6edeb; text-align: left; + border: 0; border-top: 3px solid var(--shot-accent, #7aaea2); + border-right: 1px solid #192022; border-radius: 2px; + background: color-mix(in srgb, var(--shot-accent, #7aaea2) 15%, #253032); + cursor: pointer; overflow: hidden; font: inherit; outline: none; + transition: background .12s ease, box-shadow .12s ease; + } + .dumas-h3-shot-timeline__segment:hover { + background: color-mix(in srgb, var(--shot-accent, #7aaea2) 24%, #253032); + } + .dumas-h3-shot-timeline__segment.is-selected { + background: color-mix(in srgb, var(--shot-accent, #7aaea2) 29%, #253032); + box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--shot-accent, #7aaea2) 65%, #253032); + } + .dumas-h3-shot-timeline__segment:focus-visible { + outline: 2px solid #dce9e5; outline-offset: -3px; + } + .dumas-h3-shot-timeline__shot-head { + display: flex; align-items: center; gap: 6px; white-space: nowrap; + } + .dumas-h3-shot-timeline__shot-number { + display: inline-grid; place-items: center; width: 20px; min-width: 20px; height: 19px; + color: #131b1d; background: var(--shot-accent, #7aaea2); + border-radius: 4px; font: 700 10px ui-monospace, Consolas, monospace; + } + .dumas-h3-shot-timeline__shot-name { font-size: 11px; font-weight: 700; } + .dumas-h3-shot-timeline__description { + display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; + white-space: nowrap; color: #c8d4d3; font-size: 10px; + } + .dumas-h3-shot-timeline__handle { + position: absolute; z-index: 10; top: -4px; bottom: -4px; + width: 20px; padding: 0; margin: 0; transform: translateX(-50%); + border: 0; background: transparent; cursor: ew-resize; + touch-action: none; outline: none; + } + .dumas-h3-shot-timeline__handle::before { + content: ""; position: absolute; top: 3px; bottom: 3px; + width: 9px; left: 50%; transform: translateX(-50%); + border: 1px solid #879b9b; border-radius: 5px; + background: #d3dfdc; box-shadow: 0 1px 5px #0009; + } + .dumas-h3-shot-timeline__handle::after { + content: ""; position: absolute; top: 19px; bottom: 19px; + width: 2px; left: 50%; transform: translateX(-50%); + border-radius: 2px; background: #637777; + box-shadow: -3px 0 #637777, 3px 0 #637777; + } + .dumas-h3-shot-timeline__handle:hover::before, + .dumas-h3-shot-timeline__handle.is-dragging::before, + .dumas-h3-shot-timeline__handle:focus-visible::before { + background: #ffffff; border-color: #ffffff; + box-shadow: 0 0 0 3px #c6ede633, 0 2px 8px #000b; + } + .dumas-h3-shot-timeline__bubble { + position: absolute; left: 50%; bottom: calc(100% + 6px); + transform: translateX(-50%); padding: 3px 6px; + border-radius: 4px; background: #e4eeeb; color: #142123; + font: 700 10px ui-monospace, Consolas, monospace; + white-space: nowrap; box-shadow: 0 2px 7px #0009; + pointer-events: none; opacity: 0; transition: opacity .1s ease; + } + .dumas-h3-shot-timeline__handle:hover .dumas-h3-shot-timeline__bubble, + .dumas-h3-shot-timeline__handle:focus-visible .dumas-h3-shot-timeline__bubble, + .dumas-h3-shot-timeline__handle.is-dragging .dumas-h3-shot-timeline__bubble { + opacity: 1; + } + .dumas-h3-shot-timeline__footer { + justify-content: space-between; flex-wrap: wrap; + margin-top: 9px; min-height: 24px; gap: 5px 10px; + } + .dumas-h3-shot-timeline__selection { + padding: 4px 7px; border: 1px solid #435053; + border-radius: 5px; background: #1b2223; white-space: nowrap; + font: 10px ui-monospace, Consolas, monospace; + } + .dumas-h3-shot-timeline__selected-dot { + flex: 0 0 7px; width: 7px; height: 7px; border-radius: 50%; + background: var(--selected-color, #7aaea2); + } + .dumas-h3-shot-timeline__selection strong { color: #e5ebea; } + .dumas-h3-shot-timeline__selection span { color: #aababb; } + .dumas-h3-shot-timeline__hint { color: #839698; font-size: 10px; } + .dumas-h3-shot-timeline__empty { + display: flex; align-items: center; justify-content: center; + height: 100%; color: #95a4a5; font-size: 11px; + } + `; + document.head.append(style); +} + 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; }; @@ -116,6 +260,7 @@ function configure(node) { const secondsWidget = get(node, "default_chunk_seconds"); const stateWidget = get(node, "timeline_json"); if (!countWidget || !secondsWidget || !stateWidget) return; + ensureTimelineStyles(); hide(secondsWidget); hide(stateWidget); FIXED_TEXT_WIDGETS.forEach(([name, height]) => { @@ -148,6 +293,8 @@ function configure(node) { let active = 0; let state = readState(stateWidget, Number(countWidget.value) || 2, Number(secondsWidget.value) || 4); let promptText = null; + // Selection is visual UI state only; shot timings remain in timeline_json. + const selectedShotByClip = new Map(); // 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. @@ -355,8 +502,9 @@ function configure(node) { disabledNote.style.cssText = "padding:5px 7px;margin:0 0 6px;background:#3a2525;border:1px solid #704848;border-radius:4px;color:#e2b4b4;font:11px monospace"; panel.append(disabledNote); } - 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"]; + // An editor-style timeline: shot blocks are the navigation, and only + // the shared boundaries resize. No separate duplicate "shot track" row. + const palette = ["#7bbb95", "#78a7dc", "#ddb17c", "#b69ada", "#d9919b", "#81c7c1"]; let prompt = null; let promptSelectionStart = 0; let promptSelectionEnd = 0; @@ -373,96 +521,210 @@ function configure(node) { prompt.focus(); } }; + const timelinePanel = el("div"); + timelinePanel.className = "dumas-h3-shot-timeline"; + const timelineHeading = el("div"); + timelineHeading.className = "dumas-h3-shot-timeline__heading"; + const headingText = el("strong", "SHOT TIMELINE"); + const headingMeta = el("span", `${clip.slots.length} shot${clip.slots.length === 1 ? "" : "s"} · ${duration.toFixed(2)}s`); + headingMeta.className = "dumas-h3-shot-timeline__meta"; + timelineHeading.append(headingText, headingMeta); + timelinePanel.append(timelineHeading); + + const ruler = el("div"); + ruler.className = "dumas-h3-shot-timeline__ruler"; + // Use human-friendly seconds rather than arbitrary percentages. + const rulerStep = duration <= 2.5 ? 0.5 : duration <= 6 ? 1 : duration <= 12 ? 2 : duration <= 20 ? 4 : 5; + const rulerTimes = [0]; + for (let time = rulerStep; time < duration - rulerStep * 0.5; time += rulerStep) rulerTimes.push(time); + rulerTimes.push(duration); + rulerTimes.forEach((time, index) => { + const tick = el("span", index === rulerTimes.length - 1 ? `${duration.toFixed(2)}s` : `${time}s`); + tick.className = "dumas-h3-shot-timeline__tick"; + tick.style.left = `${time / duration * 100}%`; + ruler.append(tick); + }); + timelinePanel.append(ruler); + + const timeline = el("div"); + timeline.className = "dumas-h3-shot-timeline__track"; + timeline.setAttribute("role", "group"); + timeline.setAttribute("aria-label", `Clip ${active + 1} shot timeline`); + const footer = el("div"); + footer.className = "dumas-h3-shot-timeline__footer"; + const selectedInfo = el("div"); + selectedInfo.className = "dumas-h3-shot-timeline__selection"; + const selectedDot = el("i"); + selectedDot.className = "dumas-h3-shot-timeline__selected-dot"; + const selectedTitle = el("strong"); + const selectedTimes = el("span"); + selectedInfo.append(selectedDot, selectedTitle, selectedTimes); + const hint = el("span", "Click a shot to edit · drag a divider to retime"); + hint.className = "dumas-h3-shot-timeline__hint"; + footer.append(selectedInfo, hint); const segments = []; const handles = []; + let selectedShot = Math.min(Math.max(selectedShotByClip.get(active) ?? 0, 0), Math.max(0, clip.slots.length - 1)); + + const paintSelection = () => { + segments.forEach((segment, index) => { + const selected = index === selectedShot; + segment.classList.toggle("is-selected", selected); + segment.setAttribute("aria-pressed", String(selected)); + }); + const slot = clip.slots[selectedShot]; + selectedInfo.style.setProperty("--selected-color", palette[selectedShot % palette.length] ?? palette[0]); + selectedTitle.textContent = slot ? `Shot ${selectedShot + 1}` : "No shot selected"; + selectedTimes.textContent = slot + ? `${Number(slot.start).toFixed(2)}–${Number(slot.end).toFixed(2)}s (${(Number(slot.end) - Number(slot.start)).toFixed(2)}s)` + : ""; + }; + const selectShot = (index, focusEditor = true) => { + selectedShot = index; + selectedShotByClip.set(active, index); + paintSelection(); + if (!focusEditor || !prompt) return; + const marker = `[Shot ${index + 1}]`; + const position = prompt.value.indexOf(marker); + focusPrompt(); + if (position >= 0) { + prompt.setSelectionRange(position, position + marker.length); + promptSelectionStart = position; + promptSelectionEnd = position + marker.length; + } + }; 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`; + const start = Number(slot.start); + const end = Number(slot.end); + segments[index].style.left = `${Math.max(0, start / duration * 100)}%`; + segments[index].style.width = `${Math.max(0, (end - start) / duration * 100)}%`; + segments[index].title = `Shot ${index + 1} · ${start.toFixed(2)}–${end.toFixed(2)}s · ${slot.text || "Unscripted"}`; + const description = segments[index].querySelector(".dumas-h3-shot-timeline__description"); + if (description) description.textContent = (slot.text || "Unscripted").replace(/\s+/g, " ").trim(); }); - handles.forEach(({ handle, slot }) => { handle.style.left = `${Number(slot.end) / duration * 100}%`; }); + handles.forEach(({ handle, bubble, slot }) => { + handle.style.left = `${Number(slot.end) / duration * 100}%`; + bubble.textContent = `${Number(slot.end).toFixed(2)}s`; + }); + paintSelection(); }; 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`; + const segment = el("button"); + segment.type = "button"; + segment.className = "dumas-h3-shot-timeline__segment"; + segment.style.setProperty("--shot-accent", palette[index % palette.length]); + const head = el("span"); + head.className = "dumas-h3-shot-timeline__shot-head"; + const number = el("span", String(index + 1).padStart(2, "0")); + number.className = "dumas-h3-shot-timeline__shot-number"; + const name = el("span", `Shot ${index + 1}`); + name.className = "dumas-h3-shot-timeline__shot-name"; + head.append(number, name); + const description = el("span", (slot.text || "Unscripted").replace(/\s+/g, " ").trim()); + description.className = "dumas-h3-shot-timeline__description"; + segment.append(head, description); segment.onclick = (event) => { - event.preventDefault(); event.stopPropagation(); - focusPrompt(); - }; segments.push(segment); timeline.append(segment); + event.preventDefault(); + event.stopPropagation(); + selectShot(index); + }; + 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(); + const handle = el("button"); + handle.type = "button"; + handle.className = "dumas-h3-shot-timeline__handle"; + handle.title = `Resize boundary between Shot ${index + 1} and Shot ${index + 2}`; + handle.setAttribute("aria-label", handle.title); + const bubble = el("span"); + bubble.className = "dumas-h3-shot-timeline__bubble"; + handle.append(bubble); + const minimumShot = Math.min(0.1, duration / (clip.slots.length + 1)); + const adjust = (proposedTime) => { + // Only move the boundary between the adjacent pair; never + // allow it to cross the previous or following boundary. + const low = Number(slot.start) + minimumShot; + const high = Number(next.end) - minimumShot; + if (low > high) return; + const snapped = Math.round(proposedTime * 24) / 24; // H3 at 24fps + const point = Math.max(low, Math.min(high, snapped)); + 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); + const finishEdit = () => { + // Commit without rebuilding the DOM: this prevents losing + // textarea focus or moving the ComfyUI canvas while editing. promptText = retimePrompt(promptText ?? prompt?.value ?? clip.prompt ?? "", clip); - commitPrompt(clip, promptText); - // The timeline is already updated in place. Avoid rebuilding - // the panel, which moves the textarea and canvas viewport. + clip.prompt = promptText; if (prompt) prompt.value = promptText; + persistState(); if (encoder) encoder.textContent = clip.slots .map((item, itemIndex) => `Shot ${itemIndex + 1} ${formatTime(item.start)}`) .join(" "); }; - const moveMouse = (event) => { if (dragging) move(event); }; + let dragging = false; + let dragPointerId = null; + const move = (event) => { + if (!dragging || event.pointerId !== dragPointerId) return; + event.preventDefault(); + event.stopPropagation(); + const rect = timeline.getBoundingClientRect(); + if (!rect.width) return; + adjust(((event.clientX - rect.left) / rect.width) * duration); + }; + const finish = (event) => { + if (!dragging || (event?.pointerId != null && event.pointerId !== dragPointerId)) return; + dragging = false; + dragPointerId = null; + event?.preventDefault?.(); + event?.stopPropagation?.(); + handle.classList.remove("is-dragging"); + document.removeEventListener("pointermove", move, true); + document.removeEventListener("pointerup", finish, true); + document.removeEventListener("pointercancel", finish, true); + finishEdit(); + }; handle.onpointerdown = (event) => { + if (event.button !== 0 || dragging) return; 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. + dragPointerId = event.pointerId; + // Capture uncommitted text without parsing it over the already + // authoritative timeline boundaries. if (prompt) { promptText = prompt.value; clip.prompt = promptText; } - handle.setPointerCapture?.(event.pointerId); + selectShot(index, false); + handle.classList.add("is-dragging"); document.addEventListener("pointermove", move, true); - window.addEventListener("pointerup", finish, true); - document.addEventListener("mousemove", moveMouse, true); - document.addEventListener("mouseup", finish, true); + document.addEventListener("pointerup", finish, true); + document.addEventListener("pointercancel", finish, true); }; - handles.push({ handle, slot }); timeline.append(handle); + handle.onkeydown = (event) => { + if (event.key !== "ArrowLeft" && event.key !== "ArrowRight") return; + event.preventDefault(); + event.stopPropagation(); + if (prompt) { promptText = prompt.value; clip.prompt = promptText; } + selectShot(index, false); + const frames = event.shiftKey ? 5 : 1; + adjust(Number(slot.end) + (event.key === "ArrowRight" ? frames : -frames) / 24); + finishEdit(); + }; + handles.push({ handle, bubble, slot }); + timeline.append(handle); }); + if (!clip.slots.length) { + const empty = el("div", "No shots yet — use Add shot below"); + empty.className = "dumas-h3-shot-timeline__empty"; + timeline.append(empty); + } 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); + timelinePanel.append(timeline, footer); + panel.append(timelinePanel); - const shotLabel = el("div", "SHOT TRACK"); shotLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:4px 0 3px"; panel.append(shotLabel); - const shotChips = el("div"); shotChips.style.cssText = "display:flex;gap:5px;overflow:hidden;margin:3px 0 7px;padding:4px;background:#202020;border:1px solid #3f4742;border-radius: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 = `flex:1 1 90px;min-width:70px;max-width:210px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;padding:5px 9px;border:1px solid ${palette[index % palette.length]};border-radius:5px;background:${palette[index % palette.length]}33;color:#e6e6e6;font:11px Arial;cursor:pointer`; - chip.onclick = (event) => { - event.preventDefault(); event.stopPropagation(); - if (!prompt) return; - const marker = `[Shot ${index + 1}]`; - const position = prompt.value.indexOf(marker); - focusPrompt(); - if (position >= 0) prompt.setSelectionRange(position, position + marker.length); - }; - shotChips.append(chip); - }); - panel.append(shotChips); const referenceLabel = el("div", "REFERENCE IMAGE SLOTS"); referenceLabel.style.cssText = "color:#8fa898;font-size:10px;letter-spacing:.08em;margin:7px 0 3px"; panel.append(referenceLabel); const referenceRow = el("div"); referenceRow.style.cssText = "display:flex;gap:7px;overflow:hidden;margin:3px 0 8px;padding:5px;background:#202020;border:1px solid #3f4742;border-radius:5px;align-items:center;flex-wrap:wrap;min-height:38px;box-sizing:border-box"; const connectedReferenceSlots = Array.from({ length: 9 }, (_, offset) => offset + 1) @@ -582,7 +844,13 @@ function configure(node) { prompt.onselect = rememberPromptSelection; prompt.onkeyup = rememberPromptSelection; prompt.onmouseup = rememberPromptSelection; - prompt.onblur = () => { rememberPromptSelection(); clip.prompt = prompt.value; syncPrompt(clip, clip.prompt); persistState(); }; + prompt.onblur = () => { + rememberPromptSelection(); + clip.prompt = prompt.value; + syncPrompt(clip, clip.prompt); + persistState(); + if (clip.slots.length === segments.length) updateTimeline(); + }; prompt.onchange = () => { commitPrompt(clip, prompt.value); }; prompt.onpointerdown = (event) => event.stopPropagation(); prompt.onkeydown = (event) => {