🎥 Make videos programmatically with React
clipboard error handling + allowing to copy staticFile() name
Jonny Burger6d6ac6dfb6dc
diff --git a/packages/cli/src/editor/components/AssetSelectorItem.tsx b/packages/cli/src/editor/components/AssetSelectorItem.tsxindex 7821148733..b9af951887 100644--- a/packages/cli/src/editor/components/AssetSelectorItem.tsx+++ b/packages/cli/src/editor/components/AssetSelectorItem.tsx@@ -2,12 +2,17 @@ import type {MouseEventHandler} from 'react';import React, {useCallback, useMemo, useState} from 'react';import type {StaticFile} from 'remotion';import {BACKGROUND, LIGHT_TEXT} from '../helpers/colors';+import {copyText} from '../helpers/copy-text';+import {ClipboardIcon} from '../icons/clipboard';import {FileIcon} from '../icons/file';import {ExpandedFolderIconSolid} from '../icons/folder';import type {RenderInlineAction} from './InlineAction';import {InlineAction} from './InlineAction';import {Row, Spacing} from './layout';-import {sendErrorNotification} from './Notifications/NotificationCenter';+import {+ notificationCenter,+ sendErrorNotification,+} from './Notifications/NotificationCenter';import {openInFileExplorer} from './RenderQueue/actions';const ASSET_ITEM_HEIGHT = 32;@@ -83,10 +88,14 @@ export const AssetSelectorItem: React.FC<{console.log('Clicked', item);}, [item]);- const renderAction: RenderInlineAction = useCallback((color) => {+ const renderFileExplorerAction: RenderInlineAction = useCallback((color) => {return <ExpandedFolderIconSolid style={revealIconStyle} color={color} />;}, []);+ const renderCopyAction: RenderInlineAction = useCallback((color) => {+ return <ClipboardIcon style={revealIconStyle} color={color} />;+ }, []);+const revealInExplorer = React.useCallback(() => {openInFileExplorer({directory: 'public/' + item.name,@@ -95,15 +104,29 @@ export const AssetSelectorItem: React.FC<{});}, [item.name]);+ const copyToClipboard = useCallback(() => {+ copyText(`staticFile("${item.name}")`)+ .then(() => {+ notificationCenter.current?.addNotification({+ content: `Copied 'staticFile("${item.name}")' to clipboard`,+ created: Date.now(),+ duration: 1000,+ id: String(Math.random()),+ });+ })+ .catch((err) => {+ sendErrorNotification(`Could not copy: ${err.message}`);+ });+ }, [item.name]);+return (<Row align="center">- <button+ <divstyle={style}onPointerEnter={onPointerEnter}onPointerLeave={onPointerLeave}tabIndex={tabIndex}onClick={onClick}- type="button"title={item.name}><FileIcon style={iconStyle} color={LIGHT_TEXT} />@@ -113,12 +136,19 @@ export const AssetSelectorItem: React.FC<{<><Spacing x={0.5} /><InlineAction- renderAction={renderAction}+ title="Copy staticFile() name"+ renderAction={renderCopyAction}+ onClick={copyToClipboard}+ />+ <Spacing x={0.5} />+ <InlineAction+ title="Open in Explorer"+ renderAction={renderFileExplorerAction}onClick={revealInExplorer}/></>) : null}- </button>+ </div></Row>);};diff --git a/packages/cli/src/editor/components/CopyButton.tsx b/packages/cli/src/editor/components/CopyButton.tsxindex b54ed7479c..2e0320ecee 100644--- a/packages/cli/src/editor/components/CopyButton.tsx+++ b/packages/cli/src/editor/components/CopyButton.tsx@@ -2,6 +2,7 @@ import React, {useCallback, useEffect, useState} from 'react';import {Button} from '../../preview-server/error-overlay/remotion-overlay/Button';import {copyText} from '../helpers/copy-text';import {Spacing} from './layout';+import {sendErrorNotification} from './Notifications/NotificationCenter';const iconStyle: React.CSSProperties = {width: 16,@@ -45,8 +46,13 @@ export const CopyButton: React.FC<{const [copied, setCopied] = useState<false | number>(false);const onClick = useCallback(() => {- copyText(textToCopy);- setCopied(Date.now());+ copyText(textToCopy)+ .then(() => {+ setCopied(Date.now());+ })+ .catch((err) => {+ sendErrorNotification(`Could not copy: ${err.message}`);+ });}, [textToCopy]);useEffect(() => {diff --git a/packages/cli/src/editor/components/InlineAction.tsx b/packages/cli/src/editor/components/InlineAction.tsxindex 2572ebfad8..516c38997f 100644--- a/packages/cli/src/editor/components/InlineAction.tsx+++ b/packages/cli/src/editor/components/InlineAction.tsx@@ -8,7 +8,8 @@ export const InlineAction: React.FC<{onClick: React.MouseEventHandler<HTMLAnchorElement>;disabled?: boolean;renderAction: RenderInlineAction;-}> = ({renderAction, onClick, disabled}) => {+ title?: string;+}> = ({renderAction, onClick, disabled, title}) => {const {tabIndex} = useZIndex();const [hovered, setHovered] = useState(false);@@ -45,6 +46,7 @@ export const InlineAction: React.FC<{onClick={onClick}style={style}tabIndex={tabIndex}+ title={title}>{renderAction(hovered ? 'white' : LIGHT_TEXT)}</a>diff --git a/packages/cli/src/editor/components/RenderModal/CliCopyButton.tsx b/packages/cli/src/editor/components/RenderModal/CliCopyButton.tsxindex b5025ba2e1..5543cf88c2 100644--- a/packages/cli/src/editor/components/RenderModal/CliCopyButton.tsx+++ b/packages/cli/src/editor/components/RenderModal/CliCopyButton.tsx@@ -1,5 +1,6 @@import {useCallback, useEffect, useMemo, useState} from 'react';import {LIGHT_TEXT} from '../../helpers/colors';+import {ClipboardIcon} from '../../icons/clipboard';const svgStyle: React.CSSProperties = {width: 16,height: 16,@@ -35,24 +36,7 @@ export const CliCopyButton: React.FC<{valueToCopy: string}> = ({return hovered ? 'white' : LIGHT_TEXT;}, [hovered]);- const clipboardIcon = (- <svg- aria-hidden="true"- focusable="false"- data-prefix="far"- data-icon="clipboard"- className="svg-inline--fa fa-clipboard fa-w-12"- role="img"- xmlns="http://www.w3.org/2000/svg"- viewBox="0 0 384 512"- style={svgStyle}- >- <path- fill={fillColor}- d="M336 64h-80c0-35.3-28.7-64-64-64s-64 28.7-64 64H48C21.5 64 0 85.5 0 112v352c0 26.5 21.5 48 48 48h288c26.5 0 48-21.5 48-48V112c0-26.5-21.5-48-48-48zM192 40c13.3 0 24 10.7 24 24s-10.7 24-24 24-24-10.7-24-24 10.7-24 24-24zm144 418c0 3.3-2.7 6-6 6H54c-3.3 0-6-2.7-6-6V118c0-3.3 2.7-6 6-6h42v36c0 6.6 5.4 12 12 12h168c6.6 0 12-5.4 12-12v-36h42c3.3 0 6 2.7 6 6z"- />- </svg>- );+ const clipboardIcon = <ClipboardIcon color={fillColor} style={svgStyle} />;const checkSvg = (<svgdiff --git a/packages/cli/src/editor/components/UpdateModal/UpdateModal.tsx b/packages/cli/src/editor/components/UpdateModal/UpdateModal.tsxindex 2437ee5226..85c85e17df 100644--- a/packages/cli/src/editor/components/UpdateModal/UpdateModal.tsx+++ b/packages/cli/src/editor/components/UpdateModal/UpdateModal.tsx@@ -6,6 +6,7 @@ import {CopyButton} from '../CopyButton';import {Flex, Row, Spacing} from '../layout';import {ModalContainer} from '../ModalContainer';import {NewCompHeader} from '../ModalHeader';+import {sendErrorNotification} from '../Notifications/NotificationCenter';import type {UpdateInfo} from '../UpdateCheck';const container: React.CSSProperties = {@@ -54,7 +55,14 @@ export const UpdateModal: React.FC<{</p><Row align="center"><Flex>- <pre onClick={() => copyText(command)} style={code}>+ <pre+ onClick={() =>+ copyText(command).catch((err) => {+ sendErrorNotification(`Could not copy: ${err.message}`);+ })+ }+ style={code}+ >{command}</pre></Flex>diff --git a/packages/cli/src/editor/helpers/copy-text.ts b/packages/cli/src/editor/helpers/copy-text.tsindex 8f9a9a020e..ec7ea7ac1b 100644--- a/packages/cli/src/editor/helpers/copy-text.ts+++ b/packages/cli/src/editor/helpers/copy-text.ts@@ -1,14 +1,19 @@export const copyText = (cmd: string) => {const permissionName = 'clipboard-write' as PermissionName;- navigator.permissions- .query({name: permissionName})- .then((result) => {- if (result.state === 'granted' || result.state === 'prompt') {- navigator.clipboard.writeText(cmd);- }- })- .catch((err) => {- // eslint-disable-next-line no-alert- alert('Could not copy:' + err);- });++ return new Promise<void>((resolve, reject) => {+ navigator.permissions+ .query({name: permissionName})+ .then((result) => {+ if (result.state === 'granted' || result.state === 'prompt') {+ navigator.clipboard.writeText(cmd);+ resolve();+ } else {+ reject(new Error('Permission to copy not granted'));+ }+ })+ .catch((err) => {+ reject(err);+ });+ });};diff --git a/packages/cli/src/editor/helpers/pick-color.tsx b/packages/cli/src/editor/helpers/pick-color.tsxindex 9348476840..974615e099 100644--- a/packages/cli/src/editor/helpers/pick-color.tsx+++ b/packages/cli/src/editor/helpers/pick-color.tsx@@ -1,6 +1,9 @@import {Spacing} from '../components/layout';import {ColorDot} from '../components/Notifications/ColorDot';-import {notificationCenter} from '../components/Notifications/NotificationCenter';+import {+ notificationCenter,+ sendErrorNotification,+} from '../components/Notifications/NotificationCenter';import {copyText} from './copy-text';export const pickColor = () => {@@ -12,18 +15,23 @@ export const pickColor = () => {}>).then((color) => {- copyText(color.sRGBHex);- notificationCenter.current?.addNotification({- content: (- <>- <ColorDot color={color.sRGBHex} /> <Spacing x={1} /> Copied{' '}- {color.sRGBHex}- </>- ),- created: Date.now(),- duration: 2000,- id: String(Math.random()),- });+ copyText(color.sRGBHex)+ .then(() => {+ notificationCenter.current?.addNotification({+ content: (+ <>+ <ColorDot color={color.sRGBHex} /> <Spacing x={1} /> Copied{' '}+ {color.sRGBHex}+ </>+ ),+ created: Date.now(),+ duration: 2000,+ id: String(Math.random()),+ });+ })+ .catch((err) => {+ sendErrorNotification(`Could not copy: ${err.message}`);+ });}).catch((err: Error) => {if (err.message.includes('canceled')) {diff --git a/packages/cli/src/editor/icons/clipboard.tsx b/packages/cli/src/editor/icons/clipboard.tsxnew file mode 100644index 0000000000..abd4182dc5--- /dev/null+++ b/packages/cli/src/editor/icons/clipboard.tsx@@ -0,0 +1,12 @@+import type {SVGProps} from 'react';++export const ClipboardIcon: React.FC<+ SVGProps<SVGSVGElement> & {color?: string}+> = ({color, ...props}) => (+ <svg viewBox="0 0 384 512" {...props}>+ <path+ fill={color}+ d="M336 64h-80c0-35.3-28.7-64-64-64s-64 28.7-64 64H48C21.5 64 0 85.5 0 112v352c0 26.5 21.5 48 48 48h288c26.5 0 48-21.5 48-48V112c0-26.5-21.5-48-48-48zM192 40c13.3 0 24 10.7 24 24s-10.7 24-24 24-24-10.7-24-24 10.7-24 24-24zm144 418c0 3.3-2.7 6-6 6H54c-3.3 0-6-2.7-6-6V118c0-3.3 2.7-6 6-6h42v36c0 6.6 5.4 12 12 12h168c6.6 0 12-5.4 12-12v-36h42c3.3 0 6 2.7 6 6z"+ />+ </svg>+);