🎥 Make videos programmatically with React
import {
makeAssetDragData,
parseAssetDragData,
type AssetDragData,
} from './asset-drag-data';
import {
makeComponentDragData,
parseComponentDragData,
type ComponentDimensions,
type ComponentDragData,
type ComponentProp,
} from './component-drag-data';
import {
getDragPreviewMetadata,
makeDragMimeType,
parseDragMimeType,
type AssetDragPreviewMetadata,
type ComponentDragPreviewMetadata,
type DragPreviewMetadata,
type EffectDragPreviewMetadata,
type ElementDragPreviewMetadata,
type RenderOutputDragPreviewMetadata,
type SfxDragPreviewMetadata,
} from './drag-preview-metadata';
import {
makeEffectDragData,
parseEffectDragData,
type EffectDragData,
} from './effect-drag-data';
import {
makeElementDragData,
parseElementDragData,
type ElementDragData,
} from './element-drag-data';
import {
makeRenderOutputDragData,
parseRenderOutputDragData,
type RenderOutputDragData,
} from './render-output-drag-data';
import {
makeSfxDragData,
parseSfxDragData,
type SfxDragData,
} from './sfx-drag-data';
export type MakeAssetDragDataInput = {
readonly type: 'asset';
readonly assetPath: string;
readonly width: number | null;
readonly height: number | null;
readonly durationInSeconds: number | null;
};
export type MakeComponentDragDataInput = {
readonly type: 'component';
readonly componentName: string;
readonly dimensions?: ComponentDimensions | null;
readonly importName: string;
readonly importPath: string;
readonly props: ComponentProp[];
};
export type MakeEffectDragDataInput = EffectDragData['effect'] & {
readonly type: 'effect';
};
export type MakeElementDragDataInput = Omit<
ElementDragData['element'],
'assets' | 'initialProps'
> & {
readonly type: 'element';
readonly assets?: ElementDragData['element']['assets'];
readonly durationInFrames: number;
readonly initialProps?: ElementDragData['element']['initialProps'];
};
export type MakeSfxDragDataInput = SfxDragData['sfx'] & {
readonly type: 'sfx';
};
export type MakeRenderOutputDragDataInput = {
readonly type: 'render-output';
readonly outputPath: string;
readonly fileName: string;
};
export type MakeDragDataInput =
| MakeAssetDragDataInput
| MakeComponentDragDataInput
| MakeEffectDragDataInput
| MakeElementDragDataInput
| MakeRenderOutputDragDataInput
| MakeSfxDragDataInput;
export type ConstructedDragData<
TData extends RemotionDragData = RemotionDragData,
> = {
readonly mimeType: string;
readonly payload: string;
readonly data: TData;
};
export type SerializedDragData = Pick<
ConstructedDragData,
'mimeType' | 'payload'
>;
export type DragDataTransfer = {
readonly types: ArrayLike<string>;
readonly getData: (mimeType: string) => string;
};
export type RemotionDragData =
| AssetDragData
| ComponentDragData
| EffectDragData
| ElementDragData
| RenderOutputDragData
| SfxDragData;
export type ParsedDragData =
| {
readonly type: 'asset';
readonly data: AssetDragData;
readonly preview: AssetDragPreviewMetadata;
}
| {
readonly type: 'component';
readonly data: ComponentDragData;
readonly preview: ComponentDragPreviewMetadata;
}
| {
readonly type: 'effect';
readonly data: EffectDragData;
readonly preview: EffectDragPreviewMetadata;
}
| {
readonly type: 'element';
readonly data: ElementDragData;
readonly preview: ElementDragPreviewMetadata;
}
| {
readonly type: 'render-output';
readonly data: RenderOutputDragData;
readonly preview: RenderOutputDragPreviewMetadata;
}
| {
readonly type: 'sfx';
readonly data: SfxDragData;
readonly preview: SfxDragPreviewMetadata;
};
const construct = <TData extends RemotionDragData>(
data: TData,
preview: DragPreviewMetadata,
): ConstructedDragData<TData> => {
return {
mimeType: makeDragMimeType(preview),
payload: JSON.stringify(data),
data,
};
};
const makeAssetPreview = (
input: MakeAssetDragDataInput,
): AssetDragPreviewMetadata => {
if (
input.width === undefined ||
input.height === undefined ||
input.durationInSeconds === undefined
) {
throw new TypeError(
'width, height, and durationInSeconds must be set to a value or null',
);
}
if ((input.width === null) !== (input.height === null)) {
throw new TypeError(
'width and height must either both be numbers or both be null',
);
}
return {
type: input.type,
width: input.width ?? undefined,
height: input.height ?? undefined,
durationInSeconds: input.durationInSeconds ?? undefined,
};
};
type MakeDragData = {
(input: MakeAssetDragDataInput): ConstructedDragData<AssetDragData>;
(input: MakeComponentDragDataInput): ConstructedDragData<ComponentDragData>;
(input: MakeEffectDragDataInput): ConstructedDragData<EffectDragData>;
(input: MakeElementDragDataInput): ConstructedDragData<ElementDragData>;
(
input: MakeRenderOutputDragDataInput,
): ConstructedDragData<RenderOutputDragData>;
(input: MakeSfxDragDataInput): ConstructedDragData<SfxDragData>;
(input: MakeDragDataInput): ConstructedDragData;
};
export const makeDragData = ((
input: MakeDragDataInput,
): ConstructedDragData => {
switch (input.type) {
case 'asset':
return construct(
makeAssetDragData(input.assetPath),
makeAssetPreview(input),
);
case 'component':
return construct(
makeComponentDragData({
componentName: input.componentName,
dimensions: input.dimensions,
importName: input.importName,
importPath: input.importPath,
props: input.props,
}),
{
type: input.type,
...(input.dimensions ?? {}),
},
);
case 'effect':
return construct(
makeEffectDragData({
name: input.name,
importPath: input.importPath,
config: input.config,
}),
{type: input.type},
);
case 'element':
return construct(
makeElementDragData({
assets: input.assets ?? [],
dependencies: input.dependencies,
dimensions: input.dimensions,
displayName: input.displayName,
durationInFrames: input.durationInFrames,
initialProps: input.initialProps ?? null,
slug: input.slug,
sourceCode: input.sourceCode,
installationMode: input.installationMode,
isCaptionStyle: input.isCaptionStyle,
}),
{
type: input.type,
...(input.dimensions ?? {}),
durationInFrames: input.durationInFrames,
},
);
case 'render-output':
return construct(
makeRenderOutputDragData({
outputPath: input.outputPath,
fileName: input.fileName,
}),
{type: input.type},
);
case 'sfx':
return construct(makeSfxDragData({name: input.name, url: input.url}), {
type: input.type,
});
default:
throw new TypeError('Unknown drag data type');
}
}) as MakeDragData;
const dimensionsMatch = (
preview: ComponentDragPreviewMetadata | ElementDragPreviewMetadata,
dimensions: ComponentDimensions | null | undefined,
) => {
if (dimensions === null || dimensions === undefined) {
return preview.width === undefined && preview.height === undefined;
}
return (
preview.width === dimensions.width && preview.height === dimensions.height
);
};
export const parseDragData = (
source: SerializedDragData | DragDataTransfer,
): ParsedDragData | null => {
let serialized: SerializedDragData;
if ('types' in source) {
const candidate = getDragPreviewMetadata(source.types);
if (candidate === null) {
return null;
}
serialized = {
mimeType: candidate.mimeType,
payload: source.getData(candidate.mimeType),
};
} else {
serialized = source;
}
const {mimeType, payload} = serialized;
const preview = parseDragMimeType(mimeType);
if (preview === null) {
return null;
}
switch (preview.type) {
case 'asset': {
const data = parseAssetDragData(payload);
return data === null ? null : {type: preview.type, data, preview};
}
case 'component': {
const data = parseComponentDragData(payload);
if (
data === null ||
!dimensionsMatch(preview, data.component.dimensions)
) {
return null;
}
return {type: preview.type, data, preview};
}
case 'effect': {
const data = parseEffectDragData(payload);
return data === null ? null : {type: preview.type, data, preview};
}
case 'element': {
const data = parseElementDragData(payload);
if (!data || !dimensionsMatch(preview, data.element.dimensions)) {
return null;
}
return {type: preview.type, data, preview};
}
case 'render-output': {
const data = parseRenderOutputDragData(payload);
return data === null ? null : {type: preview.type, data, preview};
}
case 'sfx': {
const data = parseSfxDragData(payload);
return data === null ? null : {type: preview.type, data, preview};
}
default:
return null;
}
};