Skip to content

🎥 Make videos programmatically with React

nice

Jonny Burger2ea6f4c9f4a5
diff --git a/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts b/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts
index fd36354cf7..e67ea4b486 100644
--- a/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts
+++ b/packages/studio-server/src/preview-server/routes/apply-visual-control-change.ts
@@ -11,7 +11,11 @@ import {writeFileAndNotifyFileWatchers} from '../../file-watcher';
import {makeHyperlink} from '../../hyperlinks/make-link';
import type {ApiHandler} from '../api-types';
import {suppressHmrForFile} from '../hmr-suppression';
-import {pushToUndoStack, suppressUndoStackInvalidation} from '../undo-stack';
+import {
+ printUndoHint,
+ pushToUndoStack,
+ suppressUndoStackInvalidation,
+} from '../undo-stack';
import {warnAboutPrettierOnce} from './log-update';
export const applyVisualControlHandler: ApiHandler<
@@ -66,7 +70,13 @@ export const applyVisualControlHandler: ApiHandler<
// Prettier not available, use unformatted output
}
- pushToUndoStack(absolutePath, fileContents, logLevel);
+ pushToUndoStack(
+ absolutePath,
+ fileContents,
+ logLevel,
+ remotionRoot,
+ 'visual control change',
+ );
suppressUndoStackInvalidation(absolutePath);
suppressHmrForFile(absolutePath);
writeFileAndNotifyFileWatchers(absolutePath, output);
@@ -85,6 +95,8 @@ export const applyVisualControlHandler: ApiHandler<
warnAboutPrettierOnce(logLevel);
}
+ printUndoHint(logLevel);
+
return {
success: true,
};
diff --git a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
index d1eea908da..480b19b9d1 100644
--- a/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/save-sequence-props.ts
@@ -8,7 +8,11 @@ import {updateSequenceProps} from '../../codemods/update-sequence-props';
import {writeFileAndNotifyFileWatchers} from '../../file-watcher';
import type {ApiHandler} from '../api-types';
import {suppressHmrForFile} from '../hmr-suppression';
-import {pushToUndoStack, suppressUndoStackInvalidation} from '../undo-stack';
+import {
+ printUndoHint,
+ pushToUndoStack,
+ suppressUndoStackInvalidation,
+} from '../undo-stack';
import {computeSequencePropsStatus} from './can-update-sequence-props';
import {logUpdate} from './log-update';
@@ -37,7 +41,13 @@ export const saveSequencePropsHandler: ApiHandler<
defaultValue: defaultValue !== null ? JSON.parse(defaultValue) : null,
});
- pushToUndoStack(absolutePath, fileContents, logLevel);
+ pushToUndoStack(
+ absolutePath,
+ fileContents,
+ logLevel,
+ remotionRoot,
+ 'sequence props update',
+ );
suppressUndoStackInvalidation(absolutePath);
suppressHmrForFile(absolutePath);
writeFileAndNotifyFileWatchers(absolutePath, output);
@@ -57,6 +67,8 @@ export const saveSequencePropsHandler: ApiHandler<
logLevel,
});
+ printUndoHint(logLevel);
+
const newStatus = computeSequencePropsStatus({
fileName,
keys: observedKeys,
diff --git a/packages/studio-server/src/preview-server/routes/update-default-props.ts b/packages/studio-server/src/preview-server/routes/update-default-props.ts
index 634ad00f43..f2946b9d2f 100644
--- a/packages/studio-server/src/preview-server/routes/update-default-props.ts
+++ b/packages/studio-server/src/preview-server/routes/update-default-props.ts
@@ -11,7 +11,11 @@ import {makeHyperlink} from '../../hyperlinks/make-link';
import type {ApiHandler} from '../api-types';
import {suppressHmrForFile} from '../hmr-suppression';
import {getProjectInfo} from '../project-info';
-import {pushToUndoStack, suppressUndoStackInvalidation} from '../undo-stack';
+import {
+ printUndoHint,
+ pushToUndoStack,
+ suppressUndoStackInvalidation,
+} from '../undo-stack';
import {suppressBundlerUpdateForFile} from '../watch-ignore-next-change';
import {checkIfTypeScriptFile} from './can-update-default-props';
import {warnAboutPrettierOnce} from './log-update';
@@ -41,7 +45,13 @@ export const updateDefaultPropsHandler: ApiHandler<
enumPaths,
});
- pushToUndoStack(projectInfo.rootFile, fileContents, logLevel);
+ pushToUndoStack(
+ projectInfo.rootFile,
+ fileContents,
+ logLevel,
+ remotionRoot,
+ 'default props update',
+ );
suppressUndoStackInvalidation(projectInfo.rootFile);
suppressHmrForFile(projectInfo.rootFile);
suppressBundlerUpdateForFile(projectInfo.rootFile);
@@ -65,6 +75,8 @@ export const updateDefaultPropsHandler: ApiHandler<
warnAboutPrettierOnce(logLevel);
}
+ printUndoHint(logLevel);
+
return {
success: true,
};
diff --git a/packages/studio-server/src/preview-server/undo-stack.ts b/packages/studio-server/src/preview-server/undo-stack.ts
index 5c8b1df005..1d126ec2cf 100644
--- a/packages/studio-server/src/preview-server/undo-stack.ts
+++ b/packages/studio-server/src/preview-server/undo-stack.ts
@@ -1,10 +1,12 @@
import {readFileSync} from 'node:fs';
+import path from 'node:path';
import type {LogLevel} from '@remotion/renderer';
import {RenderInternals} from '@remotion/renderer';
import {
installFileWatcher,
writeFileAndNotifyFileWatchers,
} from '../file-watcher';
+import {makeHyperlink} from '../hyperlinks/make-link';
import {suppressHmrForFile} from './hmr-suppression';
import {waitForLiveEventsListener} from './live-events';
import {suppressBundlerUpdateForFile} from './watch-ignore-next-change';
@@ -12,6 +14,7 @@ import {suppressBundlerUpdateForFile} from './watch-ignore-next-change';
interface UndoEntry {
filePath: string;
oldContents: string;
+ description: string;
}
const MAX_ENTRIES = 100;
@@ -20,6 +23,7 @@ const redoStack: UndoEntry[] = [];
const suppressedWrites = new Map<string, number>();
const watchers = new Map<string, {unwatch: () => void}>();
let storedLogLevel: LogLevel = 'info';
+let storedRemotionRoot: string | null = null;
let printedUndoHint = false;
function broadcastState() {
@@ -41,9 +45,12 @@ export function pushToUndoStack(
filePath: string,
oldContents: string,
logLevel: LogLevel,
+ remotionRoot: string,
+ description: string,
) {
storedLogLevel = logLevel;
- undoStack.push({filePath, oldContents});
+ storedRemotionRoot = remotionRoot;
+ undoStack.push({filePath, oldContents, description});
if (undoStack.length > MAX_ENTRIES) {
undoStack.shift();
}
@@ -57,21 +64,27 @@ export function pushToUndoStack(
),
);
+ ensureWatching(filePath);
+ broadcastState();
+}
+
+export function printUndoHint(logLevel: LogLevel) {
if (!printedUndoHint) {
printedUndoHint = true;
const shortcut = process.platform === 'darwin' ? 'Cmd+Z' : 'Ctrl+Z';
RenderInternals.Log.info(
{indent: false, logLevel},
- RenderInternals.chalk.gray(`${shortcut} in Studio to undo`),
+ RenderInternals.chalk.gray(`Tip: ${shortcut} in Studio to undo`),
);
}
-
- ensureWatching(filePath);
- broadcastState();
}
-export function pushToRedoStack(filePath: string, oldContents: string) {
- redoStack.push({filePath, oldContents});
+export function pushToRedoStack(
+ filePath: string,
+ oldContents: string,
+ description: string,
+) {
+ redoStack.push({filePath, oldContents, description});
if (redoStack.length > MAX_ENTRIES) {
redoStack.shift();
}
@@ -180,6 +193,21 @@ function cleanupWatchers() {
}
}
+function logFileAction(action: string, filePath: string) {
+ const locationLabel = storedRemotionRoot
+ ? path.relative(storedRemotionRoot, filePath)
+ : filePath;
+ const fileLink = makeHyperlink({
+ url: `file://${filePath}`,
+ text: locationLabel,
+ fallback: locationLabel,
+ });
+ RenderInternals.Log.info(
+ {indent: false, logLevel: storedLogLevel},
+ `${RenderInternals.chalk.blueBright(`${fileLink}:`)} ${action}`,
+ );
+}
+
export function popUndo(): {success: true} | {success: false; reason: string} {
const entry = undoStack.pop();
if (!entry) {
@@ -187,7 +215,11 @@ export function popUndo(): {success: true} | {success: false; reason: string} {
}
const currentContents = readFileSync(entry.filePath, 'utf-8');
- redoStack.push({filePath: entry.filePath, oldContents: currentContents});
+ redoStack.push({
+ filePath: entry.filePath,
+ oldContents: currentContents,
+ description: entry.description,
+ });
suppressUndoStackInvalidation(entry.filePath);
suppressHmrForFile(entry.filePath);
@@ -200,6 +232,7 @@ export function popUndo(): {success: true} | {success: false; reason: string} {
`Undo: restored ${entry.filePath} (undo: ${undoStack.length}, redo: ${redoStack.length})`,
),
);
+ logFileAction(`Undid ${entry.description}`, entry.filePath);
ensureWatching(entry.filePath);
broadcastState();
@@ -213,7 +246,11 @@ export function popRedo(): {success: true} | {success: false; reason: string} {
}
const currentContents = readFileSync(entry.filePath, 'utf-8');
- undoStack.push({filePath: entry.filePath, oldContents: currentContents});
+ undoStack.push({
+ filePath: entry.filePath,
+ oldContents: currentContents,
+ description: entry.description,
+ });
suppressUndoStackInvalidation(entry.filePath);
suppressHmrForFile(entry.filePath);
@@ -226,6 +263,7 @@ export function popRedo(): {success: true} | {success: false; reason: string} {
`Redo: restored ${entry.filePath} (undo: ${undoStack.length}, redo: ${redoStack.length})`,
),
);
+ logFileAction(`Redid ${entry.description}`, entry.filePath);
ensureWatching(entry.filePath);
broadcastState();
diff --git a/packages/studio/src/components/ObserveDefaultPropsContext.tsx b/packages/studio/src/components/ObserveDefaultPropsContext.tsx
index 5344dcdcbc..94d8af1219 100644
--- a/packages/studio/src/components/ObserveDefaultPropsContext.tsx
+++ b/packages/studio/src/components/ObserveDefaultPropsContext.tsx
@@ -40,6 +40,33 @@ export const ObserveDefaultProps: React.FC<{
? previewServerState.clientId
: null;
+ const applyResult = (
+ compId: string,
+ result:
+ | {canUpdate: true; currentDefaultProps: Record<string, unknown>}
+ | {canUpdate: false; reason: string},
+ ) => {
+ if (result.canUpdate) {
+ setCanSaveDefaultProps((prevState) => ({
+ ...prevState,
+ [compId]: {canUpdate: true},
+ }));
+ Internals.editorPropsProviderRef.current?.setProps((prev) => ({
+ ...prev,
+ [compId]: result.currentDefaultProps,
+ }));
+ } else {
+ setCanSaveDefaultProps((prevState) => ({
+ ...prevState,
+ [compId]: {
+ canUpdate: false,
+ reason: result.reason,
+ determined: true,
+ },
+ }));
+ }
+ };
+
useEffect(() => {
if (readOnlyStudio || !clientId || compositionId === null) {
return;
@@ -47,31 +74,13 @@ export const ObserveDefaultProps: React.FC<{
callApi('/api/subscribe-to-default-props', {compositionId, clientId})
.then((can) => {
- if (can.canUpdate) {
- setCanSaveDefaultProps((prevState) => ({
- ...prevState,
- [compositionId]: {canUpdate: true},
- }));
- } else {
- setCanSaveDefaultProps((prevState) => ({
- ...prevState,
- [compositionId]: {
- canUpdate: false,
- reason: can.reason,
- determined: true,
- },
- }));
- }
+ applyResult(compositionId, can);
})
.catch((err) => {
- setCanSaveDefaultProps((prevState) => ({
- ...prevState,
- [compositionId]: {
- canUpdate: false,
- reason: (err as Error).message,
- determined: true,
- },
- }));
+ applyResult(compositionId, {
+ canUpdate: false,
+ reason: (err as Error).message,
+ });
});
return () => {
@@ -94,26 +103,7 @@ export const ObserveDefaultProps: React.FC<{
return;
}
- const {result} = e;
- if (result.canUpdate) {
- setCanSaveDefaultProps((prevState) => ({
- ...prevState,
- [e.compositionId]: {canUpdate: true},
- }));
- Internals.editorPropsProviderRef.current?.setProps((prev) => ({
- ...prev,
- [e.compositionId]: result.currentDefaultProps,
- }));
- } else {
- setCanSaveDefaultProps((prevState) => ({
- ...prevState,
- [e.compositionId]: {
- canUpdate: false,
- reason: result.reason,
- determined: true,
- },
- }));
- }
+ applyResult(e.compositionId, e.result);
});
return () => {