Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Allow copying SVG when right-clicking `<Interactive.Svg>` (#9166)

Jonny Burger871d1efea7a0
diff --git a/packages/studio/src/components/Timeline/get-sequence-context-menu-items.ts b/packages/studio/src/components/Timeline/get-sequence-context-menu-items.ts
index 6658986e6d..c56c42dced 100644
--- a/packages/studio/src/components/Timeline/get-sequence-context-menu-items.ts
+++ b/packages/studio/src/components/Timeline/get-sequence-context-menu-items.ts
@@ -26,6 +26,8 @@ const normalizeMenuDividers = (items: ComboboxValue[]): ComboboxValue[] => {
return normalized;
};
+const interactiveSvgComponentIdentity = 'dev.remotion.remotion.Interactive.Svg';
+
export const getSequenceContextMenuItems = ({
assetLinkInfo,
canOpenInEditor,
@@ -61,6 +63,8 @@ export const getSequenceContextMenuItems = ({
}): ComboboxValue[] => {
const editorName = window.remotion_editorName;
const {documentationLink} = sequence;
+ const isInteractiveSvg =
+ sequence.controls?.componentIdentity === interactiveSvgComponentIdentity;
const items = [
editorName
@@ -143,6 +147,43 @@ export const getSequenceContextMenuItems = ({
id: 'sequence-link-divider',
}
: null,
+ isInteractiveSvg
+ ? {
+ type: 'item' as const,
+ id: 'copy-svg',
+ keyHint: null,
+ label: 'Copy SVG',
+ leftItem: null,
+ disabled: !sequence.refForOutline?.current,
+ onClick: () => {
+ const svg = sequence.refForOutline?.current;
+ if (!svg) {
+ return;
+ }
+
+ navigator.clipboard
+ .writeText(svg.outerHTML)
+ .then(() => {
+ showNotification('Copied SVG to clipboard', 1000);
+ })
+ .catch((err) => {
+ showNotification(
+ `Could not copy to clipboard: ${(err as Error).message}`,
+ 1000,
+ );
+ });
+ },
+ quickSwitcherLabel: null,
+ subMenu: null,
+ value: 'copy-svg',
+ }
+ : null,
+ isInteractiveSvg
+ ? {
+ type: 'divider' as const,
+ id: 'copy-svg-divider',
+ }
+ : null,
sourceActions.length > 0
? {
type: 'divider' as const,
diff --git a/packages/studio/src/test/sequence-context-menu-items.test.ts b/packages/studio/src/test/sequence-context-menu-items.test.ts
index 0b2b9f0d11..4a77c5ab66 100644
--- a/packages/studio/src/test/sequence-context-menu-items.test.ts
+++ b/packages/studio/src/test/sequence-context-menu-items.test.ts
@@ -8,14 +8,23 @@ const originalWindowDescriptor = Object.getOwnPropertyDescriptor(
globalThis,
'window',
);
+const originalNavigatorDescriptor = Object.getOwnPropertyDescriptor(
+ globalThis,
+ 'navigator',
+);
afterEach(() => {
if (originalWindowDescriptor) {
Object.defineProperty(globalThis, 'window', originalWindowDescriptor);
- return;
+ } else {
+ Reflect.deleteProperty(globalThis, 'window');
}
- Reflect.deleteProperty(globalThis, 'window');
+ if (originalNavigatorDescriptor) {
+ Object.defineProperty(globalThis, 'navigator', originalNavigatorDescriptor);
+ } else {
+ Reflect.deleteProperty(globalThis, 'navigator');
+ }
});
const installTestWindow = () => {
@@ -92,6 +101,65 @@ test('sequence context menu does not put two dividers between docs and rename',
).toEqual(['divider']);
});
+test('Interactive.Svg context menu can copy the rendered SVG', () => {
+ installTestWindow();
+
+ const copiedTexts: string[] = [];
+ Object.defineProperty(globalThis, 'navigator', {
+ configurable: true,
+ value: {
+ clipboard: {
+ writeText: (text: string) => {
+ copiedTexts.push(text);
+ return new Promise<void>(() => undefined);
+ },
+ },
+ },
+ });
+
+ const items = getSequenceContextMenuItems({
+ assetLinkInfo: null,
+ canOpenInEditor: false,
+ deleteDisabled: false,
+ disableInteractivityDisabled: false,
+ duplicateDisabled: false,
+ fileLocation: 'src/Video.tsx:10:2',
+ includeSourceEditItems: true,
+ onDeleteSequenceFromSource: noop,
+ onDisableSequenceInteractivity: noop,
+ onDuplicateSequenceFromSource: noop,
+ openInEditor: noop,
+ originalLocation: null,
+ selectAsset: noop,
+ sequence: {
+ controls: {
+ componentIdentity: 'dev.remotion.remotion.Interactive.Svg',
+ },
+ documentationLink: 'https://www.remotion.dev/docs/interactive',
+ refForOutline: {
+ current: {outerHTML: '<svg><circle /></svg>'},
+ },
+ } as unknown as TSequence,
+ });
+
+ const copySvg = items.find((item) => item.id === 'copy-svg');
+ if (copySvg?.type !== 'item') {
+ throw new Error('Expected copy SVG menu item');
+ }
+
+ copySvg.onClick('copy-svg', null);
+ expect(copiedTexts).toEqual(['<svg><circle /></svg>']);
+
+ const docsIndex = items.findIndex(
+ (item) => item.id === 'open-component-docs',
+ );
+ const copySvgIndex = items.findIndex((item) => item.id === 'copy-svg');
+ expect(
+ items.slice(docsIndex + 1, copySvgIndex).map((item) => item.type),
+ ).toEqual(['divider']);
+ expect(items[copySvgIndex + 1]?.type).toBe('divider');
+});
+
test('sequence freeze context menu item is hidden for audio', () => {
expect(shouldShowFreezeFrameMenuItem({type: 'audio'} as TSequence)).toBe(
false,