Skip to content

🎥 Make videos programmatically with React

2.7 KB
Raw Blame
import {getAssetSchemaKeys} from '@remotion/studio-shared';
import type {SequenceRegistrationControls} from 'remotion';
import {NoReactInternals} from 'remotion/no-react';
import {pushUrl} from '../../helpers/url-state';

type LinkInfo =
	| {
			kind: 'local';
			assetPath: string;
			title: string;
	  }
	| {
			kind: 'remote';
			href: string;
			title: string;
	  }
	| null;

export type TimelineAssetLinkInfo = Exclude<LinkInfo, null>;

export const splitRemoteSourceForMiddleEllipsis = (src: string) => {
	const protocolEnd = src.indexOf('://');
	const minimumSlashIndex = protocolEnd === -1 ? 0 : protocolEnd + 3;
	const lastSlash = src.lastIndexOf('/');

	if (lastSlash >= minimumSlashIndex && lastSlash < src.length - 1) {
		return {
			leading: src.slice(0, lastSlash + 1),
			trailing: src.slice(lastSlash + 1),
		};
	}

	const middle = Math.ceil(src.length / 2);
	return {
		leading: src.slice(0, middle),
		trailing: src.slice(middle),
	};
};

export const getTimelineAssetSrcFromSchema = (
	controls: SequenceRegistrationControls | null,
	runtimeValues = controls?.runtimeValues.getSnapshot(),
): string | null => {
	if (!controls || !getAssetSchemaKeys(controls.schema).includes('src')) {
		return null;
	}

	const {src} = runtimeValues ?? {};
	return typeof src === 'string' ? src : null;
};

export const getTimelineAssetLinkInfo = (src: string): LinkInfo => {
	if (src.startsWith(NoReactInternals.FILE_TOKEN)) {
		const encodedAssetPath = src.slice(NoReactInternals.FILE_TOKEN.length);
		let assetPath = encodedAssetPath;
		try {
			assetPath = encodedAssetPath.split('/').map(decodeURIComponent).join('/');
		} catch {
			// Keep the encoded path if it contains an invalid escape sequence.
		}

		return {
			kind: 'local',
			assetPath,
			title: assetPath,
		};
	}

	const staticBase =
		typeof window === 'undefined' ? null : window.remotion_staticBase;

	if (staticBase && src.startsWith(staticBase + '/')) {
		const assetPath = src.slice(staticBase.length + 1);
		return {
			kind: 'local',
			assetPath: decodeURIComponent(assetPath),
			title: decodeURIComponent(assetPath),
		};
	}

	if (
		src.startsWith('http://') ||
		src.startsWith('https://') ||
		src.startsWith('//')
	) {
		try {
			const url = new URL(src.startsWith('//') ? 'https:' + src : src);
			return {kind: 'remote', href: src, title: url.hostname};
		} catch {
			return {kind: 'remote', href: src, title: src};
		}
	}

	return null;
};

export const openTimelineAssetLink = (
	linkInfo: TimelineAssetLinkInfo,
	selectAsset: (asset: string) => void,
) => {
	if (linkInfo.kind === 'local') {
		selectAsset(linkInfo.assetPath);
		pushUrl(`/assets/${linkInfo.assetPath}`);
		return;
	}

	window.open(linkInfo.href, '_blank', 'noopener,noreferrer');
};