🎥 Make videos programmatically with React
Fix timeline asset reveal in Studio
Cursor Agent2aa4806f808f
diff --git a/packages/studio/src/components/ExplorerPanel.tsx b/packages/studio/src/components/ExplorerPanel.tsxindex ea0185ea07..cf57509cd2 100644--- a/packages/studio/src/components/ExplorerPanel.tsx+++ b/packages/studio/src/components/ExplorerPanel.tsx@@ -1,8 +1,9 @@-import {createRef, useCallback, useImperativeHandle, useState} from 'react';+import {useCallback, useImperativeHandle, useState} from 'react';import {BACKGROUND} from '../helpers/colors';import {AssetSelector} from './AssetSelector';import {CompositionSelector} from './CompositionSelector';import {CompSelectorRef} from './CompSelectorRef';+import {explorerSidebarTabs} from './ExplorerPanelRef';import {Tab, Tabs} from './Tabs';const container: React.CSSProperties = {@@ -35,11 +36,6 @@ const persistSelectedOptionsSidebarPanel = (panel: OptionsSidebarPanel) => {localStorage.setItem(localStorageKey, panel);};-export const explorerSidebarTabs = createRef<{- selectAssetsPanel: () => void;- selectCompositionPanel: () => void;-}>();-export const ExplorerPanel: React.FC<{readOnlyStudio: boolean;}> = ({readOnlyStudio}) => {diff --git a/packages/studio/src/components/ExplorerPanelRef.ts b/packages/studio/src/components/ExplorerPanelRef.tsnew file mode 100644index 0000000000..8b35814a68--- /dev/null+++ b/packages/studio/src/components/ExplorerPanelRef.ts@@ -0,0 +1,6 @@+import {createRef} from 'react';++export const explorerSidebarTabs = createRef<{+ selectAssetsPanel: () => void;+ selectCompositionPanel: () => void;+}>();diff --git a/packages/studio/src/components/InitialCompositionLoader.tsx b/packages/studio/src/components/InitialCompositionLoader.tsxindex 17eb229df4..12b793c020 100644--- a/packages/studio/src/components/InitialCompositionLoader.tsx+++ b/packages/studio/src/components/InitialCompositionLoader.tsx@@ -9,38 +9,11 @@ import {persistExpandedFolders} from '../helpers/persist-open-folders';import {getRoute, pushUrl} from '../helpers/url-state';import {FolderContext} from '../state/folders';import {SidebarContext} from '../state/sidebar';-import {explorerSidebarTabs} from './ExplorerPanel';+import {explorerSidebarTabs} from './ExplorerPanelRef';import {deriveCanvasContentFromUrl} from './load-canvas-content-from-url';+import {useSelectAsset} from './use-select-asset';import {useStaticFiles} from './use-static-files';-export const useSelectAsset = () => {- const {setCanvasContent} = useContext(Internals.CompositionSetters);- const {setAssetFoldersExpanded} = useContext(FolderContext);-- return useCallback(- (asset: string) => {- setCanvasContent({type: 'asset', asset});- explorerSidebarTabs.current?.selectAssetsPanel();- setAssetFoldersExpanded((ex) => {- const split = asset.split('/');-- const keysToExpand = split.map((_, i) => {- return split.slice(0, i).join('/');- });- const newState: ExpandedFoldersState = {- ...ex,- };- for (const key of keysToExpand) {- newState[key] = true;- }-- return newState;- });- },- [setAssetFoldersExpanded, setCanvasContent],- );-};-export const useSelectComposition = () => {const {setCompositionFoldersExpanded} = useContext(FolderContext);const {setCanvasContent} = useContext(Internals.CompositionSetters);diff --git a/packages/studio/src/components/Timeline/TimelineMediaInfo.tsx b/packages/studio/src/components/Timeline/TimelineMediaInfo.tsxindex 777df1ca18..f202f702f2 100644--- a/packages/studio/src/components/Timeline/TimelineMediaInfo.tsx+++ b/packages/studio/src/components/Timeline/TimelineMediaInfo.tsx@@ -1,7 +1,11 @@import React, {useCallback, useMemo, useState} from 'react';import {Internals} from 'remotion';import {LIGHT_TEXT, VERY_LIGHT_TEXT} from '../../helpers/colors';-import {pushUrl} from '../../helpers/url-state';+import {useSelectAsset} from '../use-select-asset';+import {+ getTimelineAssetLinkInfo,+ openTimelineAssetLink,+} from './timeline-asset-link';import {SELECTION_ENABLED} from './TimelineSelection';const lineStyle: React.CSSProperties = {@@ -14,67 +18,8 @@ const lineStyle: React.CSSProperties = {display: 'inline-block',};-type LinkInfo =- | {- kind: 'local';- assetPath: string;- title: string;- }- | {- kind: 'remote';- href: string;- title: string;- }- | null;--export type TimelineAssetLinkInfo = Exclude<LinkInfo, null>;--export const getTimelineAssetLinkInfo = (src: string): LinkInfo => {- 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,- setCanvasContent: React.ContextType<- typeof Internals.CompositionSetters- >['setCanvasContent'],-) => {- if (linkInfo.kind === 'local') {- setCanvasContent({type: 'asset', asset: linkInfo.assetPath});- pushUrl(`/assets/${linkInfo.assetPath}`);- return;- }-- window.open(linkInfo.href, '_blank', 'noopener,noreferrer');-};-const useAssetLink = (src: string) => {- const {setCanvasContent} = React.useContext(Internals.CompositionSetters);+ const selectAsset = useSelectAsset();const [hovered, setHovered] = useState(false);const linkInfo = useMemo(() => getTimelineAssetLinkInfo(src), [src]);@@ -89,9 +34,9 @@ const useAssetLink = (src: string) => {e.preventDefault();e.stopPropagation();- openTimelineAssetLink(linkInfo, setCanvasContent);+ openTimelineAssetLink(linkInfo, selectAsset);},- [linkInfo, setCanvasContent],+ [linkInfo, selectAsset],);const onPointerEnter = useCallback(() => setHovered(true), []);diff --git a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsxindex 83db28ce8f..4e5fadcdac 100644--- a/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx+++ b/packages/studio/src/components/Timeline/TimelineSequenceItem.tsx@@ -22,8 +22,13 @@ import {} from '../ExpandedTracksProvider';import type {ComboboxValue} from '../NewComposition/ComboBox';import {showNotification} from '../Notifications/NotificationCenter';+import {useSelectAsset} from '../use-select-asset';import {duplicateSequencesFromSource} from './duplicate-selected-timeline-item';import {saveSequenceProps} from './save-sequence-prop';+import {+ getTimelineAssetLinkInfo,+ openTimelineAssetLink,+} from './timeline-asset-link';import {TimelineExpandArrowButton,TimelineExpandArrowSpacer,@@ -31,11 +36,7 @@ import {import {TimelineExpandedSection} from './TimelineExpandedSection';import {TimelineItemStack} from './TimelineItemStack';import {TimelineLayerEye, TimelineLayerEyeSpacer} from './TimelineLayerEye';-import {- getTimelineAssetLinkInfo,- openTimelineAssetLink,- TimelineMediaInfo,-} from './TimelineMediaInfo';+import {TimelineMediaInfo} from './TimelineMediaInfo';import {TimelineRowChrome} from './TimelineRowChrome';import {isTimelineSelectionModifierEvent,@@ -103,7 +104,7 @@ export const TimelineSequenceItem: React.FC<{const {toggleTrack} = useContext(ExpandedTracksSetterContext);const {codeValues} = useContext(Internals.VisualModeCodeValuesContext);const {setCodeValues} = useContext(Internals.VisualModeSettersContext);- const {setCanvasContent} = useContext(Internals.CompositionSetters);+ const selectAsset = useSelectAsset();const {onSelect, selectable, selected} =useTimelineRowSelection(nodePathInfo);const containsSelection = useTimelineRowContainsSelection(nodePathInfo);@@ -278,7 +279,7 @@ export const TimelineSequenceItem: React.FC<{leftItem: null,disabled: false,onClick: () => {- openTimelineAssetLink(assetLinkInfo, setCanvasContent);+ openTimelineAssetLink(assetLinkInfo, selectAsset);},quickSwitcherLabel: null,subMenu: null,@@ -338,8 +339,8 @@ export const TimelineSequenceItem: React.FC<{canOpenInEditor,openInEditor,previewConnected,+ selectAsset,sequence,- setCanvasContent,]);const isExpanded =diff --git a/packages/studio/src/components/Timeline/timeline-asset-link.ts b/packages/studio/src/components/Timeline/timeline-asset-link.tsnew file mode 100644index 0000000000..2543b68d2f--- /dev/null+++ b/packages/studio/src/components/Timeline/timeline-asset-link.ts@@ -0,0 +1,58 @@+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 getTimelineAssetLinkInfo = (src: string): LinkInfo => {+ 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');+};diff --git a/packages/studio/src/components/use-select-asset.ts b/packages/studio/src/components/use-select-asset.tsnew file mode 100644index 0000000000..a229929bb7--- /dev/null+++ b/packages/studio/src/components/use-select-asset.ts@@ -0,0 +1,33 @@+import {useCallback, useContext} from 'react';+import {Internals} from 'remotion';+import type {ExpandedFoldersState} from '../helpers/persist-open-folders';+import {FolderContext} from '../state/folders';+import {explorerSidebarTabs} from './ExplorerPanelRef';++export const useSelectAsset = () => {+ const {setCanvasContent} = useContext(Internals.CompositionSetters);+ const {setAssetFoldersExpanded} = useContext(FolderContext);++ return useCallback(+ (asset: string) => {+ setCanvasContent({type: 'asset', asset});+ explorerSidebarTabs.current?.selectAssetsPanel();+ setAssetFoldersExpanded((ex) => {+ const split = asset.split('/');++ const keysToExpand = split.map((_, i) => {+ return split.slice(0, i).join('/');+ });+ const newState: ExpandedFoldersState = {+ ...ex,+ };+ for (const key of keysToExpand) {+ newState[key] = true;+ }++ return newState;+ });+ },+ [setAssetFoldersExpanded, setCanvasContent],+ );+};diff --git a/packages/studio/src/test/timeline-video-info.test.ts b/packages/studio/src/test/timeline-video-info.test.tsindex a67b96db34..4d00f8c6be 100644--- a/packages/studio/src/test/timeline-video-info.test.ts+++ b/packages/studio/src/test/timeline-video-info.test.ts@@ -1,5 +1,57 @@-import {expect, test} from 'bun:test';+import {afterEach, expect, test} from 'bun:test';import {getTimelineVideoInfoWidths} from '../components/Timeline/get-timeline-video-info-widths';+import {+ getTimelineAssetLinkInfo,+ openTimelineAssetLink,+} from '../components/Timeline/timeline-asset-link';++type TestWindow = Pick<+ Window,+ | 'history'+ | 'location'+ | 'open'+ | 'remotion_isReadOnlyStudio'+ | 'remotion_staticBase'+>;++const originalWindowDescriptor = Object.getOwnPropertyDescriptor(+ globalThis,+ 'window',+);++afterEach(() => {+ if (originalWindowDescriptor) {+ Object.defineProperty(globalThis, 'window', originalWindowDescriptor);+ return;+ }++ Reflect.deleteProperty(globalThis, 'window');+});++const installTestWindow = ({+ onPushState,+ onOpen,+}: {+ onPushState: (url: string | URL | null | undefined) => void;+ onOpen: Window['open'];+}) => {+ const testWindow: TestWindow = {+ remotion_staticBase: '/static-abcdef',+ remotion_isReadOnlyStudio: false,+ history: {+ pushState: (_state, _title, url) => onPushState(url),+ } as History,+ location: {+ pathname: '/',+ } as Location,+ open: onOpen,+ };++ Object.defineProperty(globalThis, 'window', {+ configurable: true,+ value: testWindow,+ });+};test('video timeline thumbnails ignore premount and postmount width', () => {const withoutPremount = getTimelineVideoInfoWidths({@@ -31,3 +83,70 @@ test('video timeline thumbnail widths never go negative', () => {mediaNaturalWidth: 0,});});++test('timeline local asset links select the asset and push the asset route', () => {+ const pushedUrls: (string | URL | null | undefined)[] = [];+ installTestWindow({+ onPushState: (url) => {+ pushedUrls.push(url);+ },+ onOpen: () => null,+ });++ const linkInfo = getTimelineAssetLinkInfo(+ '/static-abcdef/folder%20name/image.png',+ );+ const selectedAssets: string[] = [];++ expect(linkInfo).toEqual({+ kind: 'local',+ assetPath: 'folder name/image.png',+ title: 'folder name/image.png',+ });++ if (!linkInfo) {+ throw new Error('Expected local asset link');+ }++ openTimelineAssetLink(linkInfo, (asset) => {+ selectedAssets.push(asset);+ });++ expect(selectedAssets).toEqual(['folder name/image.png']);+ expect(pushedUrls).toEqual(['/assets/folder name/image.png']);+});++test('timeline remote asset links still open in a new tab', () => {+ const pushedUrls: (string | URL | null | undefined)[] = [];+ const openedUrls: Parameters<Window['open']>[] = [];+ installTestWindow({+ onPushState: (url) => {+ pushedUrls.push(url);+ },+ onOpen: (...args) => {+ openedUrls.push(args);+ return null;+ },+ });++ const linkInfo = getTimelineAssetLinkInfo('https://example.com/image.png');++ expect(linkInfo).toEqual({+ kind: 'remote',+ href: 'https://example.com/image.png',+ title: 'example.com',+ });++ if (!linkInfo) {+ throw new Error('Expected remote asset link');+ }++ openTimelineAssetLink(linkInfo, () => {+ throw new Error('Remote links should not select assets');+ });++ expect(openedUrls).toEqual([+ ['https://example.com/image.png', '_blank', 'noopener,noreferrer'],+ ]);+ expect(pushedUrls).toEqual([]);+});