diff --git a/packages/core/src/runtime/captionOverrides.ts b/packages/core/src/runtime/captionOverrides.ts index 47699474ed..8e5b34360a 100644 --- a/packages/core/src/runtime/captionOverrides.ts +++ b/packages/core/src/runtime/captionOverrides.ts @@ -83,25 +83,30 @@ export function applyCaptionOverrides(): void { if (override.fontWeight !== undefined) styleProps.fontWeight = override.fontWeight; if (override.fontFamily !== undefined) styleProps.fontFamily = override.fontFamily; - // Replace color values in existing GSAP tweens by timeline order. - // For any word, color tweens follow: dim (setup) → active (spoken) → after. - // Sort by startTime and assign by position, not by content heuristics. + // Replace color values in existing GSAP tweens. + // Instead of relying on timeline position order (fragile if custom + // color tweens exist), we classify each tween by comparing its + // target color to the current computed color of the element. + // Tweens that match the current color are "dim" tweens; tweens + // with a different color are "active" tweens. if (override.activeColor || override.dimColor) { const allTweens = gsap.getTweensOf(el); const colorTweens = allTweens .filter((tw) => tw.vars.color !== undefined) .sort((a, b) => a.startTime() - b.startTime()); - for (let i = 0; i < colorTweens.length; i++) { - if (i === 0 && override.dimColor) { - // First color tween = dim setup - colorTweens[i].vars.color = override.dimColor; - } else if (i === 1 && override.activeColor) { - // Second color tween = active/spoken - colorTweens[i].vars.color = override.activeColor; - } else if (i >= 2 && override.dimColor) { - // Third+ = after/deactivate (use dim color) - colorTweens[i].vars.color = override.dimColor; + // Use the first tween's color as the dim baseline — if no tweens, + // fall back to computed style. + const dimBaseline = colorTweens.length > 0 ? String(colorTweens[0].vars.color) : ""; + + for (const tw of colorTweens) { + const tweenColor = String(tw.vars.color); + if (tweenColor === dimBaseline) { + // This tween targets the dim/inactive color + if (override.dimColor) tw.vars.color = override.dimColor; + } else { + // This tween targets the active/spoken color + if (override.activeColor) tw.vars.color = override.activeColor; } } diff --git a/packages/studio/src/App.tsx b/packages/studio/src/App.tsx index 3595613e78..9c42a7969f 100644 --- a/packages/studio/src/App.tsx +++ b/packages/studio/src/App.tsx @@ -65,20 +65,18 @@ export function StudioApp() { const [rightWidth, setRightWidth] = useState(400); const [leftCollapsed, setLeftCollapsed] = useState(false); const [rightCollapsed, setRightCollapsed] = useState(true); - // Auto-enter caption edit mode when viewing a captions composition // Auto-enter caption edit mode when the iframe contains .caption-group elements. - // Listens for the runtime's postMessage events (state/timeline) which fire after - // all compositions are loaded, then checks for caption groups. + // This is a subscription to external events (postMessage from runtime) — useEffect + // is appropriate here. The runtime fires "state"/"timeline" messages after all + // compositions load, which triggers caption detection. // eslint-disable-next-line no-restricted-syntax useEffect(() => { if (!projectId) return; - let pollId: ReturnType | null = null; let activating = false; const tryActivateCaptions = () => { if (useCaptionStore.getState().isEditMode || activating) { - if (pollId) { clearInterval(pollId); pollId = null; } return; } @@ -88,7 +86,9 @@ export function StudioApp() { try { doc = iframe?.contentDocument ?? null; win = iframe?.contentWindow ?? null; - } catch { return; } + } catch { + return; + } if (!doc || !win) return; const groups = doc.querySelectorAll(".caption-group"); @@ -102,7 +102,8 @@ export function StudioApp() { // Strategy 1: data-composition-src or data-composition-file attributes const compHosts = doc.querySelectorAll("[data-composition-src], [data-composition-file]"); for (const host of compHosts) { - const src = host.getAttribute("data-composition-src") || host.getAttribute("data-composition-file"); + const src = + host.getAttribute("data-composition-src") || host.getAttribute("data-composition-file"); if (src && src.includes("captions")) { captionSrcPath = src; break; @@ -154,7 +155,9 @@ export function StudioApp() { captionSync.loadOverrides(); }) .catch(() => {}) - .finally(() => { activating = false; }); + .finally(() => { + activating = false; + }); }; // Listen for runtime messages that signal composition loading is complete @@ -168,14 +171,11 @@ export function StudioApp() { window.addEventListener("message", handleMessage); // Try immediately in case compositions are already loaded tryActivateCaptions(); - // Poll until captions are detected — sub-composition scripts run async - pollId = setInterval(tryActivateCaptions, 200); return () => { window.removeEventListener("message", handleMessage); - if (pollId) clearInterval(pollId); }; - }, [activeCompPath, projectId, compIdToSrc]); + }, [activeCompPath, projectId, compIdToSrc, captionSync]); // Auto-expand right panel when a caption word is selected // eslint-disable-next-line no-restricted-syntax @@ -296,7 +296,6 @@ export function StudioApp() { const previewIframeRef = useRef(null); const consoleErrorsRef = useRef([]); - // Listen for external file changes (user editing HTML outside the editor). // In dev: use Vite HMR. In embedded/production: use SSE from /api/events. useMountEffect(() => { @@ -850,14 +849,15 @@ export function StudioApp() { }); }} previewOverlay={ - captionEditMode ? ( - - ) : undefined + captionEditMode ? : undefined } timelineFooter={ captionEditMode ? ( -
-
+
+
Captions diff --git a/packages/studio/src/captions/components/CaptionOverlay.tsx b/packages/studio/src/captions/components/CaptionOverlay.tsx index 377d4ac271..9fbc6043c2 100644 --- a/packages/studio/src/captions/components/CaptionOverlay.tsx +++ b/packages/studio/src/captions/components/CaptionOverlay.tsx @@ -51,7 +51,9 @@ function readWordBoxes( if (!groupEl) continue; const computed = win.getComputedStyle(groupEl); if (parseFloat(computed.opacity) <= 0.01 || computed.visibility === "hidden") continue; - // Find word spans — may be direct children or inside wrappers + // Find word elements — handles both per-word spans (generator output) + // and grouped text nodes (existing caption templates that use + // el.textContent = line.text instead of individual word spans). const resolvedWordEls: HTMLElement[] = []; for (const child of groupEl.children) { const c = child as HTMLElement; @@ -62,6 +64,39 @@ function readWordBoxes( resolvedWordEls.push(c); } } + // Fallback: if no word spans found but group has text content, + // the template uses grouped text. Wrap each word in a span so + // the overlay can target them individually. + if (resolvedWordEls.length === 0 && groupEl.textContent?.trim()) { + const textNode = groupEl.childNodes[0]; + if (textNode && textNode.nodeType === Node.TEXT_NODE) { + const words = (textNode.textContent || "").split(/\s+/).filter(Boolean); + const frag = doc.createDocumentFragment(); + for (const word of words) { + const span = doc.createElement("span"); + span.textContent = word + " "; + span.style.display = "inline"; + frag.appendChild(span); + resolvedWordEls.push(span); + } + groupEl.replaceChild(frag, textNode); + } else { + // Single span child with all text (e.g. vignelli template) + const singleSpan = groupEl.querySelector(":scope > span"); + if (singleSpan && singleSpan.textContent?.trim()) { + const words = singleSpan.textContent.split(/\s+/).filter(Boolean); + const frag = doc.createDocumentFragment(); + for (const word of words) { + const span = doc.createElement("span"); + span.textContent = word + " "; + span.style.display = "inline"; + frag.appendChild(span); + resolvedWordEls.push(span); + } + singleSpan.replaceWith(frag); + } + } + } for (let wi = 0; wi < group.segmentIds.length; wi++) { const segId = group.segmentIds[wi]; const wordEl = resolvedWordEls[wi] as HTMLElement | undefined; diff --git a/packages/studio/src/captions/components/CaptionPropertyPanel.tsx b/packages/studio/src/captions/components/CaptionPropertyPanel.tsx index 2bd7f4c59d..5f8135212b 100644 --- a/packages/studio/src/captions/components/CaptionPropertyPanel.tsx +++ b/packages/studio/src/captions/components/CaptionPropertyPanel.tsx @@ -179,6 +179,14 @@ export const CaptionPropertyPanel = memo(function CaptionPropertyPanel({ const y = effectiveStyle.y ?? 0; const rotation = effectiveStyle.rotation ?? 0; const scaleX = effectiveStyle.scaleX ?? 1; + const fontSize = effectiveStyle.fontSize ?? 48; + const fontWeight = effectiveStyle.fontWeight ?? 700; + const fontFamily = effectiveStyle.fontFamily ?? "Inter"; + const color = effectiveStyle.color ?? "#ffffff"; + const activeColor = effectiveStyle.activeColor ?? "#ffffff"; + const dimColor = effectiveStyle.dimColor ?? "#999999"; + const letterSpacing = effectiveStyle.letterSpacing ?? 0; + const opacity = effectiveStyle.opacity ?? 1; // Count label const countLabel = selectedSegmentIds.size === 1 ? "1 word" : `${selectedSegmentIds.size} words`; @@ -222,9 +230,88 @@ export const CaptionPropertyPanel = memo(function CaptionPropertyPanel({ {/* Animation tab */} {activeTab === "animation" && } - {/* Style tab — Transform only */} + {/* Style tab */} {activeTab === "style" && (
+
+ + handleStyleChange({ fontFamily: e.target.value })} + className={inputCls} + /> + + + handleStyleChange({ fontSize: Number(e.target.value) })} + className={inputCls} + /> + + + handleStyleChange({ fontWeight: Number(e.target.value) })} + className={inputCls} + /> + + + handleStyleChange({ letterSpacing: Number(e.target.value) })} + className={inputCls} + /> + +
+ +
+ + handleStyleChange({ color: e.target.value })} + className={inputCls + " h-6 p-0.5 cursor-pointer"} + /> + + + handleStyleChange({ activeColor: e.target.value })} + className={inputCls + " h-6 p-0.5 cursor-pointer"} + /> + + + handleStyleChange({ dimColor: e.target.value })} + className={inputCls + " h-6 p-0.5 cursor-pointer"} + /> + + + handleStyleChange({ opacity: Number(e.target.value) })} + className={inputCls} + /> + +
+
- - - {timelineFooter}
+ {timelineFooter &&
{timelineFooter}
}
)}