Skip to content

🎥 Make videos programmatically with React

partially working

Patric Salvisberg326f02ba332f
diff --git a/packages/cli/src/editor/components/CompositionSelector.tsx b/packages/cli/src/editor/components/CompositionSelector.tsx
index 46837b6fb8..682ee31acc 100644
--- a/packages/cli/src/editor/components/CompositionSelector.tsx
+++ b/packages/cli/src/editor/components/CompositionSelector.tsx
@@ -1,4 +1,11 @@
-import React, {useCallback, useContext, useMemo, useState} from 'react';
+import React, {
+ createRef,
+ useCallback,
+ useContext,
+ useImperativeHandle,
+ useMemo,
+ useState,
+} from 'react';
import {Internals} from 'remotion';
import {BACKGROUND} from '../helpers/colors';
import {
@@ -44,6 +51,10 @@ export const getKeysToExpand = (
return getKeysToExpand(name, parent, initial);
};
+export const compositionSelectorRef = createRef<{
+ expandComposition: (compName: string) => void;
+}>();
+
export const CompositionSelector: React.FC = () => {
const {compositions, currentComposition, folders} = useContext(
Internals.CompositionManager
@@ -70,6 +81,52 @@ export const CompositionSelector: React.FC = () => {
[]
);
+ useImperativeHandle(
+ compositionSelectorRef,
+ () => {
+ return {
+ expandComposition: (compName) => {
+ const compositionToExpand = compositions.find(
+ (c) => c.id === compName
+ );
+ if (!compositionToExpand) {
+ return;
+ }
+
+ const {folderName, parentFolderName} = compositionToExpand;
+ if (folderName === null) {
+ return;
+ }
+
+ setFoldersExpanded((previousState) => {
+ const foldersExpandedState: ExpandedFoldersState = {
+ ...previousState,
+ };
+
+ let currentFolder: string | null = folderName;
+ let currentParentName: string | null = parentFolderName;
+
+ while (currentFolder) {
+ const key = openFolderKey(currentFolder, currentParentName);
+ foldersExpandedState[key] = true;
+
+ const parentFolder = folders.find(
+ (f) => f.name === currentParentName && currentParentName
+ );
+ currentFolder = parentFolder?.name ?? null;
+ currentParentName = parentFolder?.parent ?? null;
+ }
+
+ persistExpandedFolders(foldersExpandedState);
+
+ return foldersExpandedState;
+ });
+ },
+ };
+ },
+ [compositions, folders]
+ );
+
const items = useMemo(() => {
return createFolderTree(compositions, folders, foldersExpanded);
}, [compositions, folders, foldersExpanded]);
diff --git a/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherContent.tsx b/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherContent.tsx
index 6edc6ec622..c9829f0056 100644
--- a/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherContent.tsx
+++ b/packages/cli/src/editor/components/QuickSwitcher/QuickSwitcherContent.tsx
@@ -15,6 +15,7 @@ import {
useMenuStructure,
} from '../../helpers/use-menu-structure';
import {ModalsContext} from '../../state/modals';
+import {compositionSelectorRef} from '../CompositionSelector';
import {useSelectComposition} from '../InitialCompositionLoader';
import {KeyboardShortcutsExplainer} from '../KeyboardShortcutsExplainer';
import {Spacing} from '../layout';
@@ -190,10 +191,16 @@ export const QuickSwitcherContent: React.FC<{
onSelected: () => {
selectComposition(c, true);
setSelectedModal(null);
- document
- .querySelector(`.__remotion-composition[data-compname=${c.id}`)
- ?.scrollIntoView({block: 'center'});
+ const selector = `.__remotion-composition[data-compname=${c.id}`;
+
+ compositionSelectorRef.current?.expandComposition(c.id);
+ waitForElm(selector).then(() => {
+ document
+ .querySelector(selector)
+
+ ?.scrollIntoView({block: 'center'});
+ });
},
compositionType: isCompositionStill(c) ? 'still' : 'composition',
};
@@ -420,3 +427,24 @@ export const QuickSwitcherContent: React.FC<{
</div>
);
};
+
+function waitForElm(selector: string) {
+ return new Promise((resolve) => {
+ if (document.querySelector(selector)) {
+ resolve(document.querySelector(selector));
+ return;
+ }
+
+ const observer = new MutationObserver(() => {
+ if (document.querySelector(selector)) {
+ resolve(document.querySelector(selector));
+ observer.disconnect();
+ }
+ });
+
+ observer.observe(document.body, {
+ childList: true,
+ subtree: true,
+ });
+ });
+}