Skip to content

🎥 Make videos programmatically with React

`@remotion/studio`: Share one "Copy context for agents" item between single and multi selection

Cursor Agentd6994b0ab1b1
diff --git a/packages/studio/src/components/Timeline/get-copy-context-for-agents-menu-item.ts b/packages/studio/src/components/Timeline/get-copy-context-for-agents-menu-item.ts
index 132d070591..f2ceba9a10 100644
--- a/packages/studio/src/components/Timeline/get-copy-context-for-agents-menu-item.ts
+++ b/packages/studio/src/components/Timeline/get-copy-context-for-agents-menu-item.ts
@@ -4,7 +4,8 @@ import {showNotification} from '../Notifications/NotificationCenter';
export const getCopyContextForAgentsMenuItem = ({
contextForAgents,
}: {
- readonly contextForAgents: string | null;
+ // Pass a function to only resolve the context once the item is clicked.
+ readonly contextForAgents: string | null | (() => Promise<string | null>);
}): ComboboxValue => {
return {
type: 'item',
@@ -14,16 +15,31 @@ export const getCopyContextForAgentsMenuItem = ({
leftItem: null,
disabled: !contextForAgents,
onClick: () => {
- if (!contextForAgents) {
- return;
- }
+ const pendingContext =
+ typeof contextForAgents === 'function'
+ ? contextForAgents()
+ : Promise.resolve(contextForAgents);
- navigator.clipboard.writeText(contextForAgents).catch((err) => {
- showNotification(
- `Could not copy to clipboard: ${(err as Error).message}`,
- 1000,
- );
- });
+ // Passing a pending Blob keeps the write tied to the click, which
+ // browsers require even if the context is resolved afterwards.
+ navigator.clipboard
+ .write([
+ new ClipboardItem({
+ 'text/plain': pendingContext.then((context) => {
+ if (!context) {
+ throw new Error('No source location found');
+ }
+
+ return new Blob([context], {type: 'text/plain'});
+ }),
+ }),
+ ])
+ .catch((err) => {
+ showNotification(
+ `Could not copy to clipboard: ${(err as Error).message}`,
+ 2000,
+ );
+ });
},
quickSwitcherLabel: null,
subMenu: null,
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 a24654d3e0..66c95bbe9b 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
@@ -137,41 +137,7 @@ export const getMultiSequenceContextMenuItems = ({
readonly onSplitSelectedSequences: () => void;
}): ComboboxValue[] => {
return [
- {
- type: 'item',
- id: 'copy-context-for-agents',
- keyHint: null,
- label: 'Copy context for agents',
- leftItem: null,
- disabled: false,
- onClick: () => {
- // Passing a pending Blob keeps the write tied to the click, which
- // browsers require even though the context is resolved afterwards.
- navigator.clipboard
- .write([
- new ClipboardItem({
- 'text/plain': getContextForAgents().then((contextForAgents) => {
- if (!contextForAgents) {
- throw new Error(
- 'No source location found for the selected sequences',
- );
- }
-
- return new Blob([contextForAgents], {type: 'text/plain'});
- }),
- }),
- ])
- .catch((err) => {
- showNotification(
- `Could not copy to clipboard: ${(err as Error).message}`,
- 2000,
- );
- });
- },
- quickSwitcherLabel: null,
- subMenu: null,
- value: 'copy-context-for-agents',
- },
+ getCopyContextForAgentsMenuItem({contextForAgents: getContextForAgents}),
{
type: 'divider',
id: 'copy-context-for-agents-divider',
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 fdf7b12221..56da5baa87 100644
--- a/packages/studio/src/test/sequence-context-menu-items.test.ts
+++ b/packages/studio/src/test/sequence-context-menu-items.test.ts
@@ -23,6 +23,10 @@ const originalNavigatorDescriptor = Object.getOwnPropertyDescriptor(
globalThis,
'navigator',
);
+const originalClipboardItemDescriptor = Object.getOwnPropertyDescriptor(
+ globalThis,
+ 'ClipboardItem',
+);
afterEach(() => {
if (originalWindowDescriptor) {
@@ -36,6 +40,16 @@ afterEach(() => {
} else {
Reflect.deleteProperty(globalThis, 'navigator');
}
+
+ if (originalClipboardItemDescriptor) {
+ Object.defineProperty(
+ globalThis,
+ 'ClipboardItem',
+ originalClipboardItemDescriptor,
+ );
+ } else {
+ Reflect.deleteProperty(globalThis, 'ClipboardItem');
+ }
});
const installTestWindow = () => {
@@ -94,14 +108,28 @@ test('finds direct and nested elements that can copy a frame', () => {
expect(findCopyableFrameElement(null)).toBeNull();
});
-test('copy context menu items write their agent context to the clipboard', () => {
- const copiedTexts: string[] = [];
+test('copy context menu items write their agent context to the clipboard', async () => {
+ class TestClipboardItem {
+ readonly items: Record<string, Promise<Blob>>;
+
+ constructor(items: Record<string, Promise<Blob>>) {
+ this.items = items;
+ }
+ }
+
+ const copiedTexts: Promise<string>[] = [];
+ Object.defineProperty(globalThis, 'ClipboardItem', {
+ configurable: true,
+ value: TestClipboardItem,
+ });
Object.defineProperty(globalThis, 'navigator', {
configurable: true,
value: {
clipboard: {
- writeText: (text: string) => {
- copiedTexts.push(text);
+ write: (clipboardItems: TestClipboardItem[]) => {
+ copiedTexts.push(
+ clipboardItems[0].items['text/plain'].then((blob) => blob.text()),
+ );
return Promise.resolve();
},
},
@@ -116,7 +144,9 @@ test('copy context menu items write their agent context to the clipboard', () =>
}
item.onClick('copy-context-for-agents', null);
- expect(copiedTexts).toEqual(['Property "style.opacity" in src/Video.tsx:10']);
+ expect(await Promise.all(copiedTexts)).toEqual([
+ 'Property "style.opacity" in src/Video.tsx:10',
+ ]);
});
test('the file manager entry is only shown on macOS', () => {