Skip to content

🎥 Make videos programmatically with React

feat(player-example): delete selected layers

JonnyBurgercaf4d2742d31
diff --git a/packages/codemods/src/get-jsx-elements-with-node-paths.ts b/packages/codemods/src/get-jsx-elements-with-node-paths.ts
new file mode 100644
index 0000000000..5449af2426
--- /dev/null
+++ b/packages/codemods/src/get-jsx-elements-with-node-paths.ts
@@ -0,0 +1,40 @@
+import * as recast from 'recast';
+import type {SequenceNodePath} from 'remotion';
+import {getNodePathForRecastPath} from './sequence-props';
+import {parseAst} from './sequence-props/parse-ast';
+
+type JsxName =
+ | {type: 'JSXIdentifier'; name: string}
+ | {
+ type: 'JSXMemberExpression';
+ object: JsxName;
+ property: {name: string};
+ };
+
+const jsxNameToString = (name: JsxName): string => {
+ if (name.type === 'JSXIdentifier') {
+ return name.name;
+ }
+
+ return `${jsxNameToString(name.object)}.${name.property.name}`;
+};
+
+export const getJsxElementsWithNodePaths = ({source}: {source: string}) => {
+ const ast = parseAst(source);
+ const elements: {tagName: string; nodePath: SequenceNodePath}[] = [];
+ recast.visit(ast, {
+ visitJSXOpeningElement(path) {
+ const {name} = path.node;
+ if (name.type !== 'JSXNamespacedName') {
+ elements.push({
+ tagName: jsxNameToString(name as JsxName),
+ nodePath: getNodePathForRecastPath(path, ast),
+ });
+ }
+
+ return this.traverse(path);
+ },
+ });
+
+ return elements;
+};
diff --git a/packages/codemods/src/index.ts b/packages/codemods/src/index.ts
index 50eeeed533..ac0123fdc2 100644
--- a/packages/codemods/src/index.ts
+++ b/packages/codemods/src/index.ts
@@ -36,6 +36,7 @@ import {
getCompositionFile,
getFolderFile,
getJsxElementTagLabel,
+ getJsxElementsWithNodePaths,
getRootFileForProject,
insertBasicCaptions,
insertJsxElementIntoComposition,
@@ -91,9 +92,9 @@ export type {
ResolvedCompositionComponent,
ResolvedCompositionComponentWithFile,
SequenceKeyframeUpdate,
+ SequencePropUpdate,
SequencePropsNodeUpdate,
SequencePropsNodeUpdateResult,
- SequencePropUpdate,
UpdateEffectPropsResult,
} from './internals';
@@ -135,6 +136,7 @@ export const CodemodsInternals = {
getCompositionFile,
getFolderFile,
getJsxElementTagLabel,
+ getJsxElementsWithNodePaths,
getRootFileForProject,
insertBasicCaptions,
insertJsxElementIntoComposition,
diff --git a/packages/codemods/src/internals.ts b/packages/codemods/src/internals.ts
index 9e600f7aab..8dda3182fa 100644
--- a/packages/codemods/src/internals.ts
+++ b/packages/codemods/src/internals.ts
@@ -14,6 +14,10 @@ import {getNodePathForRecastPath} from './sequence-props';
import {parseAst} from './sequence-props/parse-ast';
export {applyVisualControl} from './apply-visual-control';
+export {
+ basicCaptionsElementSource,
+ getBasicCaptionsElementFile,
+} from './basic-captions-element-source';
export {
deleteJsxElementAtPath,
deleteJsxNodes,
@@ -41,8 +45,8 @@ export {
reorderEffect,
updateEffectProps,
updateEffectPropsAst,
- type EffectDeletionTarget,
type EffectArrayElement,
+ type EffectDeletionTarget,
type EffectPropUpdate,
type EffectTarget,
type FormatEffectFile,
@@ -59,13 +63,8 @@ export {
generateCanvasCaptureComposition,
makeNewCompositionComponentSource,
} from './generate-canvas-capture-composition';
-export {parseAndApplyCodemod} from './parse-and-apply-codemod';
-export {
- applyCodemod,
- type ApplyCodeModReturnType,
- type Change,
-} from './recast-mods';
-export {reorderSequence} from './reorder-sequence';
+export {getJsxElementsWithNodePaths} from './get-jsx-elements-with-node-paths';
+export {insertBasicCaptions} from './insert-basic-captions';
export {
insertJsxElementIntoComposition,
insertJsxElementIntoProjectWithNodePathRemappings,
@@ -76,6 +75,13 @@ export {
type ResolvedCompositionComponent,
type ResolvedCompositionComponentWithFile,
} from './insert-jsx-element';
+export {parseAndApplyCodemod} from './parse-and-apply-codemod';
+export {
+ applyCodemod,
+ type ApplyCodeModReturnType,
+ type Change,
+} from './recast-mods';
+export {reorderSequence} from './reorder-sequence';
export {
computeSequencePropsStatusFromContent,
computeSequencePropsSubscriptionFromContent,
@@ -85,34 +91,29 @@ export {JsxElementNotFoundAtLocationError} from './sequence-props/jsx-element-no
export {simpleDiff} from './simple-diff';
export {splitJsxSequence, splitJsxSequences} from './split-jsx-sequence';
export {splitVideoFromAudio} from './split-video-from-audio';
-export {insertBasicCaptions} from './insert-basic-captions';
-export {
- basicCaptionsElementSource,
- getBasicCaptionsElementFile,
-} from './basic-captions-element-source';
export {
getCompositionDefaultPropsLine,
updateDefaultProps,
} from './update-default-props';
export {updateInlineCaptionPatches} from './update-inline-caption-patches';
export {
+ updateEffectKeyframes,
+ updateEffectKeyframesAst,
+ updateSequenceKeyframes,
+ updateSequenceKeyframesAst,
type EffectKeyframeUpdate,
type FormatKeyframesFile,
type IntroducedKeyframeIdentifiers,
type KeyframeOperation,
type SequenceKeyframeUpdate,
- updateEffectKeyframes,
- updateEffectKeyframesAst,
- updateSequenceKeyframes,
- updateSequenceKeyframesAst,
} from './update-keyframes';
export {
+ updateMultipleSequenceProps,
+ updateSequencePropsAst,
type RemovedProp,
type SequencePropsNodeUpdate,
type SequencePropsNodeUpdateResult,
type SequencePropUpdate,
- updateMultipleSequenceProps,
- updateSequencePropsAst,
} from './update-sequence-props';
type AstNode = {
diff --git a/packages/codemods/src/test/get-jsx-elements-with-node-paths.test.ts b/packages/codemods/src/test/get-jsx-elements-with-node-paths.test.ts
new file mode 100644
index 0000000000..df3a5f8144
--- /dev/null
+++ b/packages/codemods/src/test/get-jsx-elements-with-node-paths.test.ts
@@ -0,0 +1,20 @@
+import {expect, test} from 'bun:test';
+import {getJsxElementsWithNodePaths} from '../get-jsx-elements-with-node-paths';
+
+test('lists JSX elements with their deletion paths in source order', () => {
+ const elements = getJsxElementsWithNodePaths({
+ source: `export const Video = () => (
+ <AbsoluteFill>
+ <Sequence name="Intro" />
+ <Series.Sequence durationInFrames={30} />
+ </AbsoluteFill>
+);`,
+ });
+
+ expect(elements.map(({tagName}) => tagName)).toEqual([
+ 'AbsoluteFill',
+ 'Sequence',
+ 'Series.Sequence',
+ ]);
+ expect(elements.every(({nodePath}) => nodePath.length > 0)).toBe(true);
+});
diff --git a/packages/player-example/src/BrowserBundlerExample.tsx b/packages/player-example/src/BrowserBundlerExample.tsx
index a8fefefda2..13f4c93a9a 100644
--- a/packages/player-example/src/BrowserBundlerExample.tsx
+++ b/packages/player-example/src/BrowserBundlerExample.tsx
@@ -3,11 +3,13 @@ import {
createBrowserBundler,
type VirtualProject,
} from '@remotion/browser-bundler';
-import {addSolid, deleteJsxNodes} from '@remotion/codemods';
+import {CodemodsInternals, addSolid, deleteJsxNodes} from '@remotion/codemods';
import Link from 'next/link';
import React, {useCallback, useEffect, useRef, useState} from 'react';
-import type {SequenceNodePath} from 'remotion';
-import type {BrowserBundlerPreview} from './browser-bundler-preview/bridge';
+import type {
+ BrowserBundlerPreview,
+ BrowserBundlerPreviewNode,
+} from './browser-bundler-preview/bridge';
const initialSource = `import React, {useState} from 'react';
import {AbsoluteFill, Sequence, useCurrentFrame, useVideoConfig} from 'remotion';
@@ -92,6 +94,8 @@ export const Orb: React.FC<{accent: string}> = ({accent}) => {
},
};
+const layerElementNames = new Set(['AbsoluteFill', 'Sequence', 'Solid']);
+
const getCompilationErrorMessage = (error: unknown): string => {
if (error instanceof BrowserBundlerError) {
return [error.message, ...error.diagnostics].join('\n\n');
@@ -118,10 +122,7 @@ export const BrowserBundlerExample: React.FC = () => {
const [state, setState] = useState<CompilationState>({type: 'loading'});
const [progress, setProgress] = useState<string | null>(null);
const [warnings, setWarnings] = useState<string[]>([]);
- const [insertedSolid, setInsertedSolid] = useState<{
- filePath: string;
- nodePath: SequenceNodePath;
- } | null>(null);
+ const sourceRef = useRef(initialSource);
const iframeRef = useRef<HTMLIFrameElement | null>(null);
const sessionRef = useRef<CompilationSession | null>(null);
@@ -162,7 +163,12 @@ export const BrowserBundlerExample: React.FC = () => {
// Coalesce edits before compilation, but never discard a compiled
// update: the next HMR update builds on this bundle's module graph.
- await preview.applyBundle(bundle);
+ const sourceNodes = CodemodsInternals.getJsxElementsWithNodePaths({
+ source: videoSource,
+ })
+ .filter(({tagName}) => layerElementNames.has(tagName))
+ .map(({nodePath}) => ({filePath: 'src/Video.tsx', nodePath}));
+ await preview.applyBundle(bundle, sourceNodes);
if (session.disposed || revision !== session.revision) {
return;
}
@@ -182,6 +188,38 @@ export const BrowserBundlerExample: React.FC = () => {
});
await session.queue;
}, []);
+ const deleteNodes = useCallback(
+ async (nodes: BrowserBundlerPreviewNode[]) => {
+ setState({type: 'loading'});
+ try {
+ const result = await deleteJsxNodes({
+ nodes,
+ project: {
+ ...project,
+ files: {
+ ...project.files,
+ 'src/Video.tsx': sourceRef.current,
+ },
+ rootDir: '/',
+ },
+ });
+ const nextSource = result.project.files['src/Video.tsx'];
+ if (!nextSource) {
+ throw new Error('The codemod did not return Video.tsx.');
+ }
+
+ sourceRef.current = nextSource;
+ setSource(nextSource);
+ await compile(nextSource);
+ } catch (error) {
+ setState({
+ message: getCompilationErrorMessage(error),
+ type: 'error',
+ });
+ }
+ },
+ [compile],
+ );
useEffect(() => {
const iframe = iframeRef.current;
@@ -284,7 +322,10 @@ export const BrowserBundlerExample: React.FC = () => {
return;
}
- previewRuntime = createPreview({onError: reportPreviewError});
+ previewRuntime = createPreview({
+ onDeleteJsxNodes: deleteNodes,
+ onError: reportPreviewError,
+ });
if (loadTimeout !== null) {
window.clearTimeout(loadTimeout);
}
@@ -322,7 +363,7 @@ export const BrowserBundlerExample: React.FC = () => {
message: getCompilationErrorMessage(error),
});
}
- }, [compile]);
+ }, [compile, deleteNodes]);
return (
<main
@@ -378,7 +419,7 @@ export const BrowserBundlerExample: React.FC = () => {
}
setSource(nextSource);
- setInsertedSolid(result.insertedNode);
+ sourceRef.current = nextSource;
void compile(nextSource);
} catch (error) {
setState({
@@ -400,57 +441,6 @@ export const BrowserBundlerExample: React.FC = () => {
>
Add Solid
</button>
- <button
- type="button"
- disabled={state.type === 'loading' || insertedSolid === null}
- onClick={() => {
- if (!insertedSolid) {
- return;
- }
-
- setState({type: 'loading'});
- void deleteJsxNodes({
- nodes: [
- {
- filePath: insertedSolid.filePath,
- nodePath: insertedSolid.nodePath,
- },
- ],
- project: {
- ...project,
- files: {...project.files, 'src/Video.tsx': source},
- rootDir: '/',
- },
- })
- .then((result) => {
- const nextSource = result.project.files['src/Video.tsx'];
- if (!nextSource) {
- throw new Error('The codemod did not return Video.tsx.');
- }
-
- setSource(nextSource);
- setInsertedSolid(null);
- void compile(nextSource);
- })
- .catch((error) => {
- setState({
- message: getCompilationErrorMessage(error),
- type: 'error',
- });
- });
- }}
- style={{
- backgroundColor: insertedSolid ? '#7f1d1d' : '#242933',
- border: 0,
- borderRadius: 5,
- color: insertedSolid ? 'white' : '#6b7280',
- fontSize: 12,
- fontWeight: 600,
- padding: '7px 11px',
- }}
- >
- Remove Solid
- </button>
<p
role="status"
aria-live="polite"
@@ -508,7 +498,7 @@ export const BrowserBundlerExample: React.FC = () => {
onChange={(event) => {
const nextSource = event.target.value;
setSource(nextSource);
- setInsertedSolid(null);
+ sourceRef.current = nextSource;
void compile(nextSource);
}}
spellCheck={false}
diff --git a/packages/player-example/src/browser-bundler-preview/bridge.ts b/packages/player-example/src/browser-bundler-preview/bridge.ts
index 1e1d669227..9b4893b8c5 100644
--- a/packages/player-example/src/browser-bundler-preview/bridge.ts
+++ b/packages/player-example/src/browser-bundler-preview/bridge.ts
@@ -1,11 +1,21 @@
import type {BrowserBundle} from '@remotion/browser-bundler';
+import type {SequenceNodePath} from 'remotion';
+
+export type BrowserBundlerPreviewNode = {
+ readonly filePath: string;
+ readonly nodePath: SequenceNodePath;
+};
export type BrowserBundlerPreview = {
- applyBundle: (bundle: BrowserBundle) => Promise<void>;
+ applyBundle: (
+ bundle: BrowserBundle,
+ sourceNodes: BrowserBundlerPreviewNode[],
+ ) => Promise<void>;
dispose: () => void;
};
export type CreateBrowserBundlerPreview = (options: {
+ onDeleteJsxNodes: (nodes: BrowserBundlerPreviewNode[]) => Promise<void>;
onError: (message: string) => void;
}) => BrowserBundlerPreview;
diff --git a/packages/player-example/src/browser-bundler-preview/runtime.tsx b/packages/player-example/src/browser-bundler-preview/runtime.tsx
index ce2b181e9a..3e110d6659 100644
--- a/packages/player-example/src/browser-bundler-preview/runtime.tsx
+++ b/packages/player-example/src/browser-bundler-preview/runtime.tsx
@@ -12,19 +12,25 @@ import {
type SequenceNodePathInfo,
type TimelineTrackData,
} from '@remotion/canvas';
-import React, {useEffect, useSyncExternalStore} from 'react';
+import React, {useEffect, useMemo, useSyncExternalStore} from 'react';
import {createRoot} from 'react-dom/client';
-import type {CreateBrowserBundlerPreview} from './bridge';
+import type {
+ BrowserBundlerPreviewNode,
+ CreateBrowserBundlerPreview,
+} from './bridge';
+
+const fallbackAbsolutePath = 'browser-bundler-example';
const getExampleNodePathInfo = (
layer: TimelineTrackData,
+ sourceNode: BrowserBundlerPreviewNode | undefined,
): SequenceNodePathInfo => {
return (
layer.nodePathInfo ?? {
sequenceSubscriptionKey: {
- absolutePath: 'browser-bundler-example',
+ absolutePath: sourceNode?.filePath ?? fallbackAbsolutePath,
effectKeys: [],
- nodePath: ['sequence', layer.sequence.id],
+ nodePath: sourceNode?.nodePath ?? ['sequence', layer.sequence.id],
sequenceKeys: [],
videoConfigValues: null,
},
@@ -39,8 +45,18 @@ const getExampleNodePathInfo = (
const Preview: React.FC<{
readonly composition: BrowserComposition;
readonly revision: number;
+ readonly sourceNodes: BrowserBundlerPreviewNode[];
+ readonly onDeleteJsxNodes: (
+ nodes: BrowserBundlerPreviewNode[],
+ ) => Promise<void>;
readonly onReady: (revision: number) => void;
-}> = ({composition: preview, revision, onReady}) => {
+}> = ({
+ composition: preview,
+ revision,
+ sourceNodes,
+ onDeleteJsxNodes,
+ onReady,
+}) => {
const controller = useCanvasController();
const layers = useSyncExternalStore(
controller.timeline.subscribe,
@@ -48,15 +64,71 @@ const Preview: React.FC<{
controller.timeline.getSnapshot,
);
const selection = useCanvasSelection(controller.selection);
- const selectedKeys = new Set(
- selection.selectedItems.map(getCanvasSelectionItemKey),
+ const selectedKeys = useMemo(
+ () => new Set(selection.selectedItems.map(getCanvasSelectionItemKey)),
+ [selection.selectedItems],
);
- const selectableLayers: CanvasSelectionItem[] = layers.map((layer) => ({
- type: 'sequence',
- nodePathInfo: getExampleNodePathInfo(layer),
- }));
+ const selectableLayers: CanvasSelectionItem[] = layers.map(
+ (layer, index) => ({
+ type: 'sequence',
+ nodePathInfo: getExampleNodePathInfo(layer, sourceNodes[index]),
+ }),
+ );
+ const selectedNodes = useMemo(() => {
+ const nodes = new Map<string, BrowserBundlerPreviewNode>();
+ for (const [index, layer] of layers.entries()) {
+ const layerSelection: CanvasSelectionItem = {
+ type: 'sequence',
+ nodePathInfo: getExampleNodePathInfo(layer, sourceNodes[index]),
+ };
+ if (!selectedKeys.has(getCanvasSelectionItemKey(layerSelection))) {
+ continue;
+ }
+
+ const node = sourceNodes[index];
+ if (!node) {
+ continue;
+ }
+
+ nodes.set(JSON.stringify(node), node);
+ }
+
+ return [...nodes.values()];
+ }, [layers, selectedKeys, sourceNodes]);
useEffect(() => onReady(revision), [onReady, preview, revision]);
+ useEffect(() => {
+ const onKeyDown = (event: KeyboardEvent) => {
+ if (
+ (event.key !== 'Backspace' && event.key !== 'Delete') ||
+ event.repeat ||
+ event.altKey ||
+ event.ctrlKey ||
+ event.metaKey ||
+ selectedNodes.length === 0
+ ) {
+ return;
+ }
+
+ const target = event.target;
+ if (
+ target instanceof HTMLElement &&
+ (target.isContentEditable ||
+ target.tagName === 'INPUT' ||
+ target.tagName === 'SELECT' ||
+ target.tagName === 'TEXTAREA')
+ ) {
+ return;
+ }
+
+ event.preventDefault();
+ controller.selection.clear();
+ void onDeleteJsxNodes(selectedNodes);
+ };
+
+ window.addEventListener('keydown', onKeyDown);
+ return () => window.removeEventListener('keydown', onKeyDown);
+ }, [controller.selection, onDeleteJsxNodes, selectedNodes]);
return (
<section
@@ -153,7 +225,9 @@ const Preview: React.FC<{
margin: 0,
}}
>
- Selection ({selection.selectedItems.length})
+ {selectedNodes.length === 0
+ ? 'Select a layer, then press Backspace to delete it'
+ : `${selectedNodes.length} selected · Backspace to delete`}
</h2>
<button
type="button"
@@ -184,8 +258,11 @@ const Preview: React.FC<{
padding: 0,
}}
>
- {layers.map((layer) => {
- const nodePathInfo = getExampleNodePathInfo(layer);
+ {layers.map((layer, index) => {
+ const nodePathInfo = getExampleNodePathInfo(
+ layer,
+ sourceNodes[index],
+ );
const layerSelection: CanvasSelectionItem = {
type: 'sequence',
nodePathInfo,
@@ -271,6 +348,7 @@ const Preview: React.FC<{
};
export const createBrowserBundlerPreview: CreateBrowserBundlerPreview = ({
+ onDeleteJsxNodes,
onError,
}) => {
const container = document.getElementById('browser-bundler-preview');
@@ -281,6 +359,7 @@ export const createBrowserBundlerPreview: CreateBrowserBundlerPreview = ({
const runtime = createBrowserBundleRuntime();
let disposed = false;
let revision = 0;
+ let sourceNodes: BrowserBundlerPreviewNode[] = [];
let pending: {
revision: number;
resolve: () => void;
@@ -321,6 +400,8 @@ export const createBrowserBundlerPreview: CreateBrowserBundlerPreview = ({
<Preview
composition={composition}
revision={revision}
+ sourceNodes={sourceNodes}
+ onDeleteJsxNodes={onDeleteJsxNodes}
onReady={onReady}
/>,
);
@@ -354,11 +435,12 @@ export const createBrowserBundlerPreview: CreateBrowserBundlerPreview = ({
window.addEventListener('pagehide', dispose);
return {
- applyBundle: async (bundle) => {
+ applyBundle: async (bundle, nextSourceNodes) => {
if (disposed) {
throw new Error('The Canvas preview was disposed.');
}
+ sourceNodes = nextSourceNodes;
const RegisteredRoot = await runtime.applyBundle(bundle);
if (disposed) {
throw new Error('The Canvas preview was disposed.');