Skip to content

🎥 Make videos programmatically with React

Add asset folder actions

JonnyBurger2e8a71722e33
diff --git a/packages/browser-studio/e2e/browser-studio.test.ts b/packages/browser-studio/e2e/browser-studio.test.ts
index 4532e6cfd4..e078c71735 100644
--- a/packages/browser-studio/e2e/browser-studio.test.ts
+++ b/packages/browser-studio/e2e/browser-studio.test.ts
@@ -1204,6 +1204,10 @@ test('clears hover backgrounds even if pointer leave events are lost', async ({
contents: new TextEncoder().encode('hover test').buffer,
filePath: 'hover-test.txt',
});
+ await window.remotion_browserStudio.writeStaticFile({
+ contents: new TextEncoder().encode('folder action test').buffer,
+ filePath: 'hover-folder/inside.txt',
+ });
});
const addSolid = studio.getByRole('button', {name: 'Add Solid'});
@@ -1239,6 +1243,21 @@ test('clears hover backgrounds even if pointer leave events are lost', async ({
const neutralArea = studio.locator('[data-sidebar-toggle="right"]');
await studio.getByRole('button', {name: 'Assets', exact: true}).click();
+ const assetFolder = studio.getByTitle('hover-folder', {exact: true});
+ await expect(assetFolder).toBeVisible();
+ await assetFolder.hover();
+ const folderActions = assetFolder.getByRole('button', {
+ name: 'More actions',
+ });
+ await expect(folderActions).toBeVisible();
+ await folderActions.click();
+ await expect(
+ studio.getByText('Copy folder path', {exact: true}),
+ ).toBeVisible();
+ await expect(studio.getByText('Upload...', {exact: true})).toBeVisible();
+ await expect(studio.getByText(/^Show in /)).toHaveCount(0);
+ await page.keyboard.press('Escape');
+
const assetItem = studio.getByTitle('hover-test.txt', {exact: true});
await expect(assetItem).toBeVisible();
await assetItem.hover();
diff --git a/packages/studio/src/components/AssetSelector.tsx b/packages/studio/src/components/AssetSelector.tsx
index 3a6666740e..4ede04766e 100644
--- a/packages/studio/src/components/AssetSelector.tsx
+++ b/packages/studio/src/components/AssetSelector.tsx
@@ -3,7 +3,7 @@ import {copyRenderOutputToAsset} from '../api/copy-render-output-to-asset';
import {writeStaticFile} from '../api/write-static-file';
import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';
import {StudioServerConnectionCtx} from '../helpers/client-id';
-import {BACKGROUND, WHITE_ALPHA_06, LIGHT_TEXT} from '../helpers/colors';
+import {BACKGROUND, LIGHT_TEXT, WHITE_ALPHA_06} from '../helpers/colors';
import {buildAssetFolderStructure} from '../helpers/create-folder-tree';
import {toggleBooleanMapKey} from '../helpers/persist-boolean-map';
import {persistExpandedFolders} from '../helpers/persist-open-folders';
@@ -191,29 +191,33 @@ export const AssetSelector: React.FC<{
},
[dropLocation, writeFilesToPublicFolder],
);
- const uploadAssets = useCallback(async () => {
- try {
- const files = await pickFilesToImport();
- if (files.length === 0) {
- return;
- }
+ const uploadAssets = useCallback(
+ async (assetPath: string | null) => {
+ try {
+ const files = await pickFilesToImport();
+ if (files.length === 0) {
+ return;
+ }
- const wereWritten = await writeFilesToPublicFolder({
- files,
- assetPath: null,
- });
- if (wereWritten) {
- showNotification(
- files.length === 1
- ? `Uploaded ${files[0].name} to public folder`
- : `Uploaded ${files.length} assets to public folder`,
- 3000,
- );
+ const wereWritten = await writeFilesToPublicFolder({
+ files,
+ assetPath,
+ });
+ if (wereWritten) {
+ const destination = assetPath ? `'${assetPath}'` : 'public folder';
+ showNotification(
+ files.length === 1
+ ? `Uploaded ${files[0].name} to ${destination}`
+ : `Uploaded ${files.length} assets to ${destination}`,
+ 3000,
+ );
+ }
+ } catch (error) {
+ showNotification(`Error during upload: ${error}`, 3000);
}
- } catch (error) {
- showNotification(`Error during upload: ${error}`, 3000);
- }
- }, [writeFilesToPublicFolder]);
+ },
+ [writeFilesToPublicFolder],
+ );
const getAssetActions = useCallback((): ComboboxValue[] => {
return [
{
@@ -221,7 +225,7 @@ export const AssetSelector: React.FC<{
keyHint: null,
label: 'Upload...',
leftItem: null,
- onClick: uploadAssets,
+ onClick: () => uploadAssets(null),
quickSwitcherLabel: 'Upload assets...',
subMenu: null,
type: 'item',
@@ -282,6 +286,7 @@ export const AssetSelector: React.FC<{
dropLocation={dropLocation}
setDropLocation={setDropLocation}
readOnlyStudio={readOnlyStudio}
+ uploadAssets={uploadAssets}
/>
</div>
)}
diff --git a/packages/studio/src/components/AssetSelectorItem.tsx b/packages/studio/src/components/AssetSelectorItem.tsx
index ecf5095814..f809d0eacf 100644
--- a/packages/studio/src/components/AssetSelectorItem.tsx
+++ b/packages/studio/src/components/AssetSelectorItem.tsx
@@ -13,11 +13,11 @@ import {getBrowserStudioOperations} from '../helpers/browser-studio-operations';
import {StudioServerConnectionCtx} from '../helpers/client-id';
import {
BACKGROUND,
- WHITE_ALPHA_06,
CURRENT_COLOR,
LIGHT_TEXT,
TRANSPARENT,
WHITE,
+ WHITE_ALPHA_06,
} from '../helpers/colors';
import {copyText} from '../helpers/copy-text';
import type {AssetFolder, AssetStructure} from '../helpers/create-folder-tree';
@@ -110,6 +110,10 @@ const inlineActionsStyle: React.CSSProperties = {
display: 'flex',
};
+const renderContextMenuAction: RenderInlineAction = (color) => {
+ return <EllipsisIcon svgProps={ellipsisIconStyle} fill={color} />;
+};
+
export const getAssetActionAvailability = ({
browserStudioCanMutateAssets,
readOnlyStudio,
@@ -274,6 +278,87 @@ export const getAssetContextMenuItems = ({
return items.filter(NoReactInternals.truthy);
};
+export const getAssetFolderContextMenuItems = ({
+ folderPath,
+ fileManagerName,
+ copyFolderPath,
+ copyAbsolutePath,
+ openFolderInExplorer,
+ uploadAssets,
+ fileExplorerAvailable,
+ fileExplorerDisabled,
+ mutationsDisabled,
+}: {
+ folderPath: string;
+ fileManagerName: string;
+ copyFolderPath: () => void;
+ copyAbsolutePath: (() => void) | null;
+ openFolderInExplorer: () => void;
+ uploadAssets: () => void;
+ fileExplorerAvailable: boolean;
+ fileExplorerDisabled: boolean;
+ mutationsDisabled: boolean;
+}): ComboboxValue[] => {
+ const items: (ComboboxValue | null)[] = [
+ fileExplorerAvailable
+ ? {
+ id: 'open-asset-folder-in-explorer',
+ keyHint: null,
+ label: `Show in ${fileManagerName}`,
+ leftItem: null,
+ onClick: openFolderInExplorer,
+ quickSwitcherLabel: `Show asset folder in ${fileManagerName}`,
+ subMenu: null,
+ type: 'item',
+ value: 'open-asset-folder-in-explorer',
+ disabled: fileExplorerDisabled,
+ }
+ : null,
+ {
+ id: 'copy-asset-folder-path',
+ keyHint: null,
+ label: 'Copy folder path',
+ leftItem: null,
+ onClick: copyFolderPath,
+ quickSwitcherLabel: 'Copy asset folder path',
+ subMenu: null,
+ type: 'item',
+ value: 'copy-asset-folder-path',
+ },
+ copyAbsolutePath
+ ? {
+ id: 'copy-asset-folder-absolute-path',
+ keyHint: null,
+ label: 'Copy absolute path',
+ leftItem: null,
+ onClick: copyAbsolutePath,
+ quickSwitcherLabel: 'Copy asset folder absolute path',
+ subMenu: null,
+ type: 'item',
+ value: 'copy-asset-folder-absolute-path',
+ }
+ : null,
+ {
+ type: 'divider',
+ id: 'asset-folder-upload-divider',
+ },
+ {
+ id: 'upload-assets-to-folder',
+ keyHint: null,
+ label: 'Upload...',
+ leftItem: null,
+ onClick: uploadAssets,
+ quickSwitcherLabel: `Upload assets to ${folderPath}...`,
+ subMenu: null,
+ type: 'item',
+ value: 'upload-assets-to-folder',
+ disabled: mutationsDisabled,
+ },
+ ];
+
+ return items.filter(NoReactInternals.truthy);
+};
+
const AssetFolderItem: React.FC<{
readonly item: AssetFolder;
readonly tabIndex: number;
@@ -286,6 +371,7 @@ const AssetFolderItem: React.FC<{
readonly dropLocation: string | null;
readonly setDropLocation: React.Dispatch<React.SetStateAction<string | null>>;
readonly readOnlyStudio: boolean;
+ readonly uploadAssets: (assetPath: string | null) => void;
}> = ({
tabIndex,
item,
@@ -295,8 +381,17 @@ const AssetFolderItem: React.FC<{
dropLocation,
setDropLocation,
readOnlyStudio,
+ uploadAssets,
}) => {
const openFolderTimerRef = useRef<number | null>(null);
+ const connectionStatus = useContext(StudioServerConnectionCtx)
+ .previewServerState.type;
+ const fileManagerName = getFileManagerName(
+ window.remotion_fileSystemPlatform,
+ );
+ const folderPath = useMemo(() => {
+ return parentFolder ? `${parentFolder}/${item.name}` : item.name;
+ }, [item.name, parentFolder]);
const {isDropDiv, onDragEnter, onDragLeave} = useAssetDragEvents({
name: item.name,
@@ -322,6 +417,80 @@ const AssetFolderItem: React.FC<{
toggleFolder(item.name, parentFolder);
}, [item.name, parentFolder, toggleFolder]);
+ const copyFolderPath = useCallback(() => {
+ copyText(folderPath)
+ .then(() => {
+ showNotification(`Copied '${folderPath}' to clipboard`, 1000);
+ })
+ .catch((err) => {
+ showNotification(`Could not copy: ${err.message}`, 2000);
+ });
+ }, [folderPath]);
+
+ const copyAbsolutePath = useCallback(() => {
+ if (window.remotion_publicFolderExists === null) {
+ return;
+ }
+
+ const content = `${window.remotion_publicFolderExists}/${folderPath}`;
+ copyText(content)
+ .then(() => {
+ showNotification(`Copied '${content}' to clipboard`, 1000);
+ })
+ .catch((err) => {
+ showNotification(`Could not copy: ${err.message}`, 2000);
+ });
+ }, [folderPath]);
+
+ const openFolderInExplorer = useCallback(() => {
+ if (!window.remotion_publicFolderExists) {
+ showNotification('Could not find the public folder', 2000);
+ return;
+ }
+
+ openInFileExplorer({
+ directory: `${window.remotion_publicFolderExists}/${folderPath}`,
+ }).catch((err) => {
+ showNotification(`Could not open folder: ${err.message}`, 2000);
+ });
+ }, [folderPath]);
+
+ const uploadAssetsToFolder = useCallback(() => {
+ uploadAssets(folderPath);
+ }, [folderPath, uploadAssets]);
+
+ const {mutationsDisabled, fileExplorerDisabled} = getAssetActionAvailability({
+ browserStudioCanMutateAssets:
+ getBrowserStudioOperations() === null ? null : true,
+ readOnlyStudio,
+ connectionStatus,
+ publicFolderExists: window.remotion_publicFolderExists,
+ });
+
+ const getContextMenuItems = useCallback((): ComboboxValue[] => {
+ return getAssetFolderContextMenuItems({
+ folderPath,
+ fileManagerName,
+ copyFolderPath,
+ copyAbsolutePath:
+ window.remotion_publicFolderExists === null ? null : copyAbsolutePath,
+ openFolderInExplorer,
+ uploadAssets: uploadAssetsToFolder,
+ fileExplorerAvailable: getBrowserStudioOperations() === null,
+ fileExplorerDisabled,
+ mutationsDisabled,
+ });
+ }, [
+ copyAbsolutePath,
+ copyFolderPath,
+ fileExplorerDisabled,
+ fileManagerName,
+ folderPath,
+ mutationsDisabled,
+ openFolderInExplorer,
+ uploadAssetsToFolder,
+ ]);
+
const Icon = item.expanded ? ExpandedFolderIcon : CollapsedFolderIcon;
return (
@@ -332,39 +501,55 @@ const AssetFolderItem: React.FC<{
backgroundColor: isDropDiv ? WHITE_ALPHA_06 : BACKGROUND,
}}
>
- <div
- style={folderStyle}
- className={HOVERABLE_CLASS_NAME}
- tabIndex={tabIndex}
- title={item.name}
- onClick={onClick}
- onDragEnter={(event) => {
- if (!isAssetUploadDragEvent(event)) {
- return;
- }
-
- if (!item.expanded) {
- openFolderTimerRef.current = window.setTimeout(() => {
- toggleFolder(item.name, parentFolder);
- }, 1000);
- }
- }}
- onDragLeave={(event) => {
- if (!isAssetUploadDragEvent(event)) {
- return;
- }
-
- if (openFolderTimerRef.current) {
- clearTimeout(openFolderTimerRef.current);
- }
- }}
- >
- <Row>
- <Icon style={iconStyle} color={CURRENT_COLOR} />
- <Spacing x={1} />
- <div style={labelStyle}>{item.name}</div>
+ <ContextMenu getItems={getContextMenuItems}>
+ <Row align="center">
+ <div
+ style={folderStyle}
+ className={`${HOVERABLE_CLASS_NAME} ${HOVER_GROUP_CLASS_NAME}`}
+ tabIndex={tabIndex}
+ title={item.name}
+ onClick={onClick}
+ onDragEnter={(event) => {
+ if (!isAssetUploadDragEvent(event)) {
+ return;
+ }
+
+ if (!item.expanded) {
+ openFolderTimerRef.current = window.setTimeout(() => {
+ toggleFolder(item.name, parentFolder);
+ }, 1000);
+ }
+ }}
+ onDragLeave={(event) => {
+ if (!isAssetUploadDragEvent(event)) {
+ return;
+ }
+
+ if (openFolderTimerRef.current) {
+ clearTimeout(openFolderTimerRef.current);
+ }
+ }}
+ >
+ <Icon style={iconStyle} color={CURRENT_COLOR} />
+ <Spacing x={1} />
+ <div style={labelStyle}>{item.name}</div>
+ <div
+ className={HOVER_GROUP_REVEAL_CLASS_NAME}
+ style={inlineActionsStyle}
+ >
+ <Spacing x={0.5} />
+ <InlineDropdown
+ variant={null}
+ title="More actions"
+ renderAction={renderContextMenuAction}
+ getItems={getContextMenuItems}
+ style={NO_HOVER_BACKGROUND_STYLE}
+ className={FOCUS_VISIBLE_ONLY_CLASS_NAME}
+ />
+ </div>
+ </div>
</Row>
- </div>
+ </ContextMenu>
{item.expanded ? (
<AssetFolderTree
@@ -378,6 +563,7 @@ const AssetFolderItem: React.FC<{
dropLocation={dropLocation}
setDropLocation={setDropLocation}
readOnlyStudio={readOnlyStudio}
+ uploadAssets={uploadAssets}
/>
) : null}
</div>
@@ -397,6 +583,7 @@ export const AssetFolderTree: React.FC<{
readonly dropLocation: string | null;
readonly setDropLocation: React.Dispatch<React.SetStateAction<string | null>>;
readonly readOnlyStudio: boolean;
+ readonly uploadAssets: (assetPath: string | null) => void;
}> = ({
item,
level,
@@ -407,6 +594,7 @@ export const AssetFolderTree: React.FC<{
dropLocation,
setDropLocation,
readOnlyStudio,
+ uploadAssets,
}) => {
const combinedParents = useMemo(() => {
return [parentFolder, name].filter(NoReactInternals.truthy).join('/');
@@ -425,6 +613,7 @@ export const AssetFolderTree: React.FC<{
dropLocation={dropLocation}
setDropLocation={setDropLocation}
readOnlyStudio={readOnlyStudio}
+ uploadAssets={uploadAssets}
/>
);
})}
@@ -560,10 +749,6 @@ const AssetSelectorItem: React.FC<{
return <ExpandedFolderIcon style={revealIconStyle} color={color} />;
}, []);
- const renderContextMenuAction: RenderInlineAction = useCallback((color) => {
- return <EllipsisIcon svgProps={ellipsisIconStyle} fill={color} />;
- }, []);
-
const copyFileName = useCallback(() => {
copyText(item.name)
.then(() => {
diff --git a/packages/studio/src/test/asset-context-menu.test.ts b/packages/studio/src/test/asset-context-menu.test.ts
index 4ba0b5d2ed..2d9d410068 100644
--- a/packages/studio/src/test/asset-context-menu.test.ts
+++ b/packages/studio/src/test/asset-context-menu.test.ts
@@ -1,8 +1,9 @@
import {expect, test} from 'bun:test';
import {
getAssetActionAvailability,
- getCanDragAsset,
getAssetContextMenuItems,
+ getAssetFolderContextMenuItems,
+ getCanDragAsset,
} from '../components/AssetSelectorItem';
const noop = () => undefined;
@@ -74,6 +75,50 @@ test('hides file-manager asset actions in Browser Studio', () => {
);
});
+test('offers supported asset folder actions in Browser Studio', () => {
+ const items = getAssetFolderContextMenuItems({
+ folderPath: 'nested/assets',
+ fileManagerName: 'Finder',
+ copyFolderPath: noop,
+ copyAbsolutePath: null,
+ openFolderInExplorer: noop,
+ uploadAssets: noop,
+ fileExplorerAvailable: false,
+ fileExplorerDisabled: true,
+ mutationsDisabled: false,
+ });
+
+ expect(getItem(items, 'copy-asset-folder-path').disabled).not.toBe(true);
+ expect(getItem(items, 'upload-assets-to-folder').disabled).not.toBe(true);
+ expect(
+ items.find((item) => item.id === 'open-asset-folder-in-explorer'),
+ ).toBeUndefined();
+ expect(
+ items.find((item) => item.id === 'copy-asset-folder-absolute-path'),
+ ).toBeUndefined();
+});
+
+test('keeps non-mutating asset folder actions in read-only Studio', () => {
+ const items = getAssetFolderContextMenuItems({
+ folderPath: 'nested/assets',
+ fileManagerName: 'Finder',
+ copyFolderPath: noop,
+ copyAbsolutePath: noop,
+ openFolderInExplorer: noop,
+ uploadAssets: noop,
+ fileExplorerAvailable: true,
+ fileExplorerDisabled: true,
+ mutationsDisabled: true,
+ });
+
+ expect(getItem(items, 'copy-asset-folder-path').disabled).not.toBe(true);
+ expect(getItem(items, 'copy-asset-folder-absolute-path').disabled).not.toBe(
+ true,
+ );
+ expect(getItem(items, 'open-asset-folder-in-explorer').disabled).toBe(true);
+ expect(getItem(items, 'upload-assets-to-folder').disabled).toBe(true);
+});
+
test('only offers Remotion Convert for audio and video assets', () => {
const getItems = (relativePath: string) =>
getAssetContextMenuItems({