Skip to content

🎥 Make videos programmatically with React

Add text insertion action to Studio composition inspector

Igor Samokhovets3c4ef1412960
diff --git a/packages/codemods/src/insertable-element.ts b/packages/codemods/src/insertable-element.ts
index eea89bc3ec..061514ddad 100644
--- a/packages/codemods/src/insertable-element.ts
+++ b/packages/codemods/src/insertable-element.ts
@@ -55,7 +55,7 @@ const toCodemodValue = (value: unknown): CodemodValue => {
};
/**
- * Translates a Studio insertion request for a solid, asset or component into
+ * Translates a Studio insertion request for text, a solid, asset or component into
* an element for `addElement()`. SVG markup and compositions keep using the
* dedicated insertion pipeline.
*/
@@ -110,6 +110,35 @@ export const createElementFromInsertable = ({
style: getPositionStyle(element.position),
},
});
+ } else if (element.type === 'text') {
+ if (typeof element.text !== 'string' || element.text.trim() === '') {
+ throw new Error('Text must be a non-empty string');
+ }
+
+ if (!Number.isFinite(element.fontSize) || element.fontSize < 1) {
+ throw new Error('fontSize must be a positive number');
+ }
+
+ if (typeof element.color !== 'string' || element.color.trim() === '') {
+ throw new Error('Text color must be a non-empty string');
+ }
+
+ inner = createElement({
+ component: 'Interactive.Div',
+ importPath: 'remotion',
+ importName: 'Interactive',
+ props: {
+ name: 'Text',
+ ...(from === null ? {} : {from}),
+ style: {
+ ...(positioned ? getPositionStyle(element.position) : {}),
+ fontFamily: 'sans-serif',
+ fontSize: element.fontSize,
+ color: element.color,
+ },
+ },
+ children: [element.text],
+ });
} else if (element.type === 'asset') {
if (element.srcType === 'remote' && !isUrl(element.src)) {
throw new Error('Remote asset source must be a URL');
diff --git a/packages/studio-server/src/preview-server/routes/insert-composition-element.ts b/packages/studio-server/src/preview-server/routes/insert-composition-element.ts
index d191392236..9f9c3dbdb0 100644
--- a/packages/studio-server/src/preview-server/routes/insert-composition-element.ts
+++ b/packages/studio-server/src/preview-server/routes/insert-composition-element.ts
@@ -91,6 +91,19 @@ const validateElement = (
return;
}
+ if (element.type === 'text') {
+ if (typeof element.text !== 'string' || element.text.trim() === '') {
+ throw new Error('Text must be a non-empty string');
+ }
+
+ validateDimension('fontSize', element.fontSize);
+ if (typeof element.color !== 'string' || element.color.trim() === '') {
+ throw new Error('Text color must be a non-empty string');
+ }
+
+ return;
+ }
+
if (element.type === 'svg') {
if (typeof element.markup !== 'string' || element.markup.trim() === '') {
throw new Error('SVG markup must be a non-empty string');
@@ -176,6 +189,10 @@ const validateElement = (
};
const getElementLabel = (element: InsertableCompositionElement) => {
+ if (element.type === 'text') {
+ return '<Interactive.Div>';
+ }
+
if (element.type === 'solid') {
return '<Solid>';
}
diff --git a/packages/studio-shared/src/api-requests.ts b/packages/studio-shared/src/api-requests.ts
index 5dd8b689f0..a3a7b9fb52 100644
--- a/packages/studio-shared/src/api-requests.ts
+++ b/packages/studio-shared/src/api-requests.ts
@@ -1088,6 +1088,13 @@ export type ReplaceVideoSourceResponse =
| {success: false; reason: string; stack: string};
export type InsertableCompositionElement =
+ | {
+ type: 'text';
+ text: string;
+ fontSize: number;
+ color: string;
+ position: InsertableCompositionElementPosition | null;
+ }
| {
type: 'solid';
width: number;
diff --git a/packages/studio-shared/src/required-package.ts b/packages/studio-shared/src/required-package.ts
index 05625fbe86..383a7508a6 100644
--- a/packages/studio-shared/src/required-package.ts
+++ b/packages/studio-shared/src/required-package.ts
@@ -26,7 +26,11 @@ export const getRequiredPackageForImportPath = (
export const getRequiredPackageForInsertableElement = (
element: InsertableCompositionElement,
): string | null => {
- if (element.type === 'solid' || element.type === 'svg') {
+ if (
+ element.type === 'solid' ||
+ element.type === 'svg' ||
+ element.type === 'text'
+ ) {
return null;
}
diff --git a/packages/studio/src/components/InspectorPanel/CompositionInspector.tsx b/packages/studio/src/components/InspectorPanel/CompositionInspector.tsx
index 4489f9082b..7f5ebdcddb 100644
--- a/packages/studio/src/components/InspectorPanel/CompositionInspector.tsx
+++ b/packages/studio/src/components/InspectorPanel/CompositionInspector.tsx
@@ -11,6 +11,7 @@ import type {_InternalTypes} from 'remotion';
import {StudioServerConnectionCtx} from '../../helpers/client-id';
import {isStudioInteractivityEnabled} from '../../helpers/interactivity-enabled';
import {CloudDownloadIcon} from '../../icons/cloud-download';
+import {FontFileIcon} from '../../icons/font';
import {PicIcon} from '../../icons/frame';
import {SolidIcon} from '../../icons/solid';
import {SparklesIcon} from '../../icons/sparkles';
@@ -65,12 +66,15 @@ const CompositionActions: React.FC = () => {
canInsertAsset,
canInsertComposition,
canInsertSolid,
+ canInsertText,
canShowInsertAsset,
canShowInsertComposition,
canShowInsertSolid,
+ canShowInsertText,
insertAsset,
insertComposition,
insertSolid,
+ insertText,
generateWithAgent,
canShowGenerateWithAgent,
} = useCompositionActions();
@@ -80,6 +84,7 @@ const CompositionActions: React.FC = () => {
!canShowInsertAsset &&
!canShowInsertComposition &&
!canShowInsertSolid &&
+ !canShowInsertText &&
!canShowGenerateWithAgent &&
downloadProject === null
) {
@@ -104,6 +109,17 @@ const CompositionActions: React.FC = () => {
Add Solid
</InspectorQuickAction>
) : null}
+ {canShowInsertText ? (
+ <InspectorQuickAction
+ disabled={!canInsertText}
+ onClick={insertText}
+ renderIcon={(color) => (
+ <FontFileIcon color={color} style={inspectorActionIconStyle} />
+ )}
+ >
+ Add text
+ </InspectorQuickAction>
+ ) : null}
{canShowInsertAsset ? (
<InspectorQuickAction
disabled={!canInsertAsset}
diff --git a/packages/studio/src/components/InspectorPanel/use-composition-actions.ts b/packages/studio/src/components/InspectorPanel/use-composition-actions.ts
index 658ca6b0ca..193f2534d1 100644
--- a/packages/studio/src/components/InspectorPanel/use-composition-actions.ts
+++ b/packages/studio/src/components/InspectorPanel/use-composition-actions.ts
@@ -34,6 +34,7 @@ export const useCompositionActions = () => {
);
const videoConfig = Internals.useUnsafeVideoConfig();
const [isAddingSolid, setIsAddingSolid] = useState(false);
+ const [isAddingText, setIsAddingText] = useState(false);
const [isAddingAsset, setIsAddingAsset] = useState(false);
const [isAddingComposition, setIsAddingComposition] = useState(false);
const {setSelectedModal} = useContext(SetSelectedModalContext);
@@ -84,6 +85,8 @@ export const useCompositionActions = () => {
compositionFile !== null &&
videoConfig !== null;
const canInsertSolid = canShowInsertSolid && !isAddingSolid;
+ const canShowInsertText = canShowInsertSolid;
+ const canInsertText = canShowInsertText && !isAddingText;
const canShowInsertAsset =
previewInteractive &&
@@ -145,6 +148,58 @@ export const useCompositionActions = () => {
videoConfig,
]);
+ const insertText = useCallback(async () => {
+ if (
+ !canInsertText ||
+ currentCompositionId === null ||
+ compositionFile === null ||
+ videoConfig === null
+ ) {
+ return;
+ }
+
+ setIsAddingText(true);
+ try {
+ const request: InsertCompositionElementRequest = {
+ compositionFile,
+ compositionId: currentCompositionId,
+ from: null,
+ element: {
+ type: 'text',
+ text: 'Text',
+ fontSize: Math.max(
+ 1,
+ Math.round(Math.min(videoConfig.width, videoConfig.height) * 0.075),
+ ),
+ color: 'white',
+ position: {
+ x: videoConfig.width * 0.1,
+ y: videoConfig.height * 0.1,
+ },
+ },
+ };
+ const result = browserStudioOperations
+ ? await browserStudioOperations.insertCompositionElement(request)
+ : await callApi('/api/insert-composition-element', request);
+
+ if (result.success) {
+ return;
+ }
+
+ showNotification(result.reason, 4000);
+ } catch (err) {
+ showNotification((err as Error).message, 4000);
+ } finally {
+ setIsAddingText(false);
+ }
+ }, [
+ browserStudioOperations,
+ canInsertText,
+ compositionFile,
+ currentCompositionId,
+ videoConfig,
+ ]);
+
const onFilesSelected = useCallback(async () => {
if (
!canInsertAsset ||
@@ -367,13 +422,16 @@ export const useCompositionActions = () => {
canInsertAsset,
canInsertComposition,
canInsertSolid,
+ canInsertText,
canShowInsertAsset,
canShowInsertComposition,
canShowInsertSolid,
+ canShowInsertText,
canShowGenerateWithAgent,
insertAsset,
insertComposition,
insertSolid,
+ insertText,
generateWithAgent,
};
};