🎥 Make videos programmatically with React
`@remotion/studio`: Split captions with Enter
JonnyBurger864029fff3a8
diff --git a/packages/codemods/src/update-inline-caption-patches.ts b/packages/codemods/src/update-inline-caption-patches.tsindex 619f0ce474..ef60032830 100644--- a/packages/codemods/src/update-inline-caption-patches.ts+++ b/packages/codemods/src/update-inline-caption-patches.ts@@ -162,17 +162,18 @@ const getSourceValueRange = ({};};-const getPropertyInsertionReplacements = ({+const getObjectSourceRange = ({input,caption,- key,- value,}: {input: string;caption: ObjectExpression;- key: CaptionKey;- value: string | number | boolean | null;-}): SourceReplacement[] => {+}): {+ start: number;+ end: number;+ lastPropertyKey: string;+ lastPropertyValue: {start: number; end: number};+} => {const firstProperty = caption.properties.find((property): property is ObjectProperty =>property.type === 'ObjectProperty',@@ -201,12 +202,39 @@ const getPropertyInsertionReplacements = ({property: lastProperty,key: lastPropertyKey,});- const openingBrace = input.lastIndexOf('{', firstPropertyValue.start);+ const start = input.lastIndexOf('{', firstPropertyValue.start);const closingBrace = input.indexOf('}', lastPropertyValue.end);- if (openingBrace === -1 || closingBrace === -1) {- throw new Error(`Could not locate caption ${key} in the source file`);+ if (start === -1 || closingBrace === -1) {+ throw new Error('Could not locate a caption in the source file');}+ return {+ start,+ end: closingBrace + 1,+ lastPropertyKey,+ lastPropertyValue,+ };+};++const getPropertyInsertionReplacements = ({+ input,+ caption,+ key,+ value,+}: {+ input: string;+ caption: ObjectExpression;+ key: CaptionKey;+ value: string | number | boolean | null;+}): SourceReplacement[] => {+ const {+ start: openingBrace,+ end,+ lastPropertyKey,+ lastPropertyValue,+ } = getObjectSourceRange({input, caption});+ const closingBrace = end - 1;+const objectSource = input.slice(openingBrace, closingBrace + 1);if (!objectSource.includes('\n')) {const currentProperties = input.slice(openingBrace + 1, closingBrace);@@ -274,7 +302,10 @@ const updateCaption = ({}const changedKeys = Object.keys(patch.changes);- if (changedKeys.length === 0 || !changedKeys.every(isCaptionKey)) {+ if (+ (changedKeys.length === 0 && patch.insertAfter === null) ||+ !changedKeys.every(isCaptionKey)+ ) {throw new Error('Caption patches must change at least one caption field');}@@ -388,6 +419,72 @@ export const updateInlineCaptionPatches = ({const result = updateCaption({input, caption, patch});changedFields.push(result.changedFields);replacements.push(...result.replacements);++ if (patch.insertAfter !== null) {+ const current = getStaticCaption(caption);+ if (+ current.pageBreakAfter !== null &&+ patch.insertAfter.pageBreakAfter === null+ ) {+ throw new Error(+ 'An inserted caption must preserve an existing pageBreakAfter field',+ );+ }++ const insertedCaption = updateCaption({+ input,+ caption,+ patch: {+ index: patch.index + 1,+ before: patch.before,+ changes: {+ text: patch.insertAfter.text,+ startMs: patch.insertAfter.startMs,+ endMs: patch.insertAfter.endMs,+ timestampMs: patch.insertAfter.timestampMs,+ confidence: patch.insertAfter.confidence,+ ...(patch.insertAfter.pageBreakAfter === null+ ? {}+ : {pageBreakAfter: patch.insertAfter.pageBreakAfter}),+ },+ insertAfter: null,+ },+ });+ const {start: captionStart, end: captionEnd} = getObjectSourceRange({+ input,+ caption,+ });+ const arrayLocation = captionsAttribute.value.expression.loc;+ if (!arrayLocation) {+ throw new Error('Could not locate the inline caption to insert after');+ }++ const captionSource = input.slice(captionStart, captionEnd);+ const insertedSource = insertedCaption.replacements+ .sort((a, b) => b.start - a.start)+ .reduce((currentSource, replacement) => {+ const start = replacement.start - captionStart;+ const end = replacement.end - captionStart;+ return (+ currentSource.slice(0, start) ++ replacement.value ++ currentSource.slice(end)+ );+ }, captionSource);+ const closingLineStart = input.lastIndexOf('\n', captionEnd - 1) + 1;+ const indentation =+ input.slice(closingLineStart, captionEnd - 1).match(/^\s*/)?.[0] ?? '';+ const arrayIsMultiline =+ arrayLocation.start.line !== arrayLocation.end.line;+ const hasTrailingComma = input[captionEnd] === ',';+ replacements.push({+ start: captionEnd + (hasTrailingComma ? 1 : 0),+ end: captionEnd + (hasTrailingComma ? 1 : 0),+ value: arrayIsMultiline+ ? `${hasTrailingComma ? '' : ','}\n${indentation}${insertedSource},`+ : `${hasTrailingComma ? '' : ','} ${insertedSource},`,+ });+ }}const output = replacementsdiff --git a/packages/studio-server/src/test/save-sequence-props.test.ts b/packages/studio-server/src/test/save-sequence-props.test.tsindex 220c058060..3edf8370ad 100644--- a/packages/studio-server/src/test/save-sequence-props.test.ts+++ b/packages/studio-server/src/test/save-sequence-props.test.ts@@ -281,6 +281,7 @@ test('saveSequenceProps saves inline caption patches as an undoable source edit'confidence: null,pageBreakAfter: null,},+ insertAfter: null,changes: {text: 'Updated', pageBreakAfter: true},},],diff --git a/packages/studio-server/src/test/update-inline-caption-patches.test.ts b/packages/studio-server/src/test/update-inline-caption-patches.test.tsindex 1f7d3f6cba..70f6b4aac7 100644--- a/packages/studio-server/src/test/update-inline-caption-patches.test.ts+++ b/packages/studio-server/src/test/update-inline-caption-patches.test.ts@@ -46,6 +46,7 @@ test('updates only the patched inline caption fields', () => {timestampMs: 1500,pageBreakAfter: null,},+ insertAfter: null,changes: {startMs: 1100, endMs: 2100},},],@@ -75,6 +76,7 @@ test('adds and updates pageBreakAfter on inline captions', () => {confidence: null,pageBreakAfter: null,},+ insertAfter: null,changes: {pageBreakAfter: true},},],@@ -99,6 +101,7 @@ test('adds and updates pageBreakAfter on inline captions', () => {confidence: null,pageBreakAfter: true,},+ insertAfter: null,changes: {pageBreakAfter: false},},],@@ -126,6 +129,7 @@ test('adds pageBreakAfter to a single-line inline caption', () => {confidence: null,pageBreakAfter: null,},+ insertAfter: null,changes: {pageBreakAfter: true},},],@@ -150,6 +154,7 @@ test('rejects a queued patch whose source caption has changed', () => {confidence: null,pageBreakAfter: null,},+ insertAfter: null,changes: {endMs: 900},},],@@ -178,6 +183,7 @@ test('rejects captions wrapped in a satisfies expression', () => {confidence: null,pageBreakAfter: null,},+ insertAfter: null,changes: {text: 'Updated'},},],diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.tsindex 033510901a..f522330752 100644--- a/packages/studio-shared/src/api-requests.ts+++ b/packages/studio-shared/src/api-requests.ts@@ -533,6 +533,14 @@ export type CaptionPatch = {confidence: number | null;pageBreakAfter: boolean | null;};+ insertAfter: {+ text: string;+ startMs: number;+ endMs: number;+ timestampMs: number | null;+ confidence: number | null;+ pageBreakAfter: boolean | null;+ } | null;changes: Partial<{text: string;startMs: number;diff --git a/packages/studio/src/components/CaptionTextEditor.tsx b/packages/studio/src/components/CaptionTextEditor.tsxindex 8808955868..3f0d0aef70 100644--- a/packages/studio/src/components/CaptionTextEditor.tsx+++ b/packages/studio/src/components/CaptionTextEditor.tsx@@ -69,6 +69,7 @@ export const CaptionTextEditor: React.FC<{const listRef = useRef<HTMLDivElement>(null);const cancelledBlurIndexes = useRef(new Set<number>());const dirtyRef = useRef(false);+ const pendingFocusIndex = useRef<number | null>(null);const latestRef = useRef({captions, onSave});latestRef.current = {captions, onSave};const captionRows = useMemo(() => {@@ -154,6 +155,73 @@ export const CaptionTextEditor: React.FC<{input?.scrollIntoView({block: 'nearest'});}, []);+ useEffect(() => {+ const index = pendingFocusIndex.current;+ if (index === null) {+ return;+ }++ pendingFocusIndex.current = null;+ const input = listRef.current?.querySelector<HTMLInputElement>(+ `[data-caption-index="${index}"]`,+ );+ input?.focus();+ input?.setSelectionRange(0, 0);+ input?.scrollIntoView({block: 'nearest'});+ }, [captions]);++ const splitCaption = useCallback(+ (index: number, characterIndex: number) => {+ const currentCaptions = latestRef.current.captions;+ const caption = currentCaptions[index];+ if (!caption || caption.text.length === 0) {+ return;+ }++ const splitIndex = Math.min(+ Math.max(characterIndex, 0),+ caption.text.length,+ );+ const splitTimestamp = Math.round(+ caption.startMs ++ ((caption.endMs - caption.startMs) * splitIndex) /+ caption.text.length,+ );+ const {pageBreakAfter, ...captionWithoutPageBreak} = caption;+ const nextCaptions = [+ ...currentCaptions.slice(0, index),+ {+ ...captionWithoutPageBreak,+ text: caption.text.slice(0, splitIndex),+ endMs: splitTimestamp,+ timestampMs:+ caption.timestampMs === null+ ? null+ : Math.round((caption.startMs + splitTimestamp) / 2),+ ...(pageBreakAfter === undefined ? {} : {pageBreakAfter: false}),+ },+ {+ ...captionWithoutPageBreak,+ text: caption.text.slice(splitIndex),+ startMs: splitTimestamp,+ timestampMs:+ caption.timestampMs === null+ ? null+ : Math.round((splitTimestamp + caption.endMs) / 2),+ ...(pageBreakAfter === undefined ? {} : {pageBreakAfter}),+ },+ ...currentCaptions.slice(index + 1),+ ];++ latestRef.current.captions = nextCaptions;+ dirtyRef.current = true;+ pendingFocusIndex.current = index + 1;+ onChange(nextCaptions);+ commitPending();+ },+ [commitPending, onChange],+ );+return (<div style={container}><div ref={listRef} style={list}>@@ -184,6 +252,15 @@ export const CaptionTextEditor: React.FC<{}}onChange={(event) => updateText(index, event.target.value)}onKeyDown={(event) => {+ if (event.key === 'Enter' && !event.nativeEvent.isComposing) {+ event.preventDefault();+ splitCaption(+ index,+ event.currentTarget.selectionStart ??+ event.currentTarget.value.length,+ );+ }+if (event.key === 'ArrowDown' &&index < captions.length - 1diff --git a/packages/studio/src/components/InlineCaptionInspector.tsx b/packages/studio/src/components/InlineCaptionInspector.tsxindex 28a5f8c87e..b3283843e4 100644--- a/packages/studio/src/components/InlineCaptionInspector.tsx+++ b/packages/studio/src/components/InlineCaptionInspector.tsx@@ -31,7 +31,68 @@ const getCaptionPatches = ({previous: Caption[];next: Caption[];}): CaptionPatch[] | null => {- if (previous.length !== next.length) {+ if (previous.length !== next.length && previous.length + 1 !== next.length) {+ return null;+ }++ if (previous.length + 1 === next.length) {+ for (const [index, before] of previous.entries()) {+ if (+ serializeCaptions(previous.slice(0, index)) !==+ serializeCaptions(next.slice(0, index)) ||+ serializeCaptions(previous.slice(index + 1)) !==+ serializeCaptions(next.slice(index + 2))+ ) {+ continue;+ }++ const after = next[index];+ const inserted = next[index + 1];+ if (!after || !inserted) {+ return null;+ }++ const changes: CaptionPatch['changes'] = {};+ if (before.text !== after.text) {+ changes.text = after.text;+ }++ if (before.startMs !== after.startMs) {+ changes.startMs = after.startMs;+ }++ if (before.endMs !== after.endMs) {+ changes.endMs = after.endMs;+ }++ if (before.timestampMs !== after.timestampMs) {+ changes.timestampMs = after.timestampMs;+ }++ if (before.confidence !== after.confidence) {+ changes.confidence = after.confidence;+ }++ if (Boolean(before.pageBreakAfter) !== Boolean(after.pageBreakAfter)) {+ changes.pageBreakAfter = Boolean(after.pageBreakAfter);+ }++ return [+ {+ index,+ before: {+ ...before,+ pageBreakAfter: before.pageBreakAfter ?? null,+ },+ changes,+ insertAfter: {+ ...inserted,+ pageBreakAfter: inserted.pageBreakAfter ?? null,+ },+ },+ ];+ }+return null;}@@ -59,6 +120,7 @@ const getCaptionPatches = ({pageBreakAfter: before.pageBreakAfter ?? null,},changes,+ insertAfter: null,});}}