Skip to content

Commit 0b6d103

Browse files
feat(studio): copy, paste and duplicate clip groups on the timeline (#4064)
* feat(studio): copy, paste and duplicate clip groups on the timeline Cmd+C on a multi-clip selection now copies all of them, not just the first. Cmd+V pastes the group at the playhead keeping each clip's offset from the earliest one and its original track when free. Cmd+D duplicates the selection in place. Context menu gains Copy/Paste/Duplicate, and a failed copy now toasts why instead of silently returning false. Root cause was a DOM lookup that couldn't address a composition-instance clip's root; group copy reuses the composition-aware resolver every other timeline editor already uses. Also fixes a duplicated data-hf-id on pasted/duplicated clips found during live testing, and reorders the post-paste selection to land on the new clips instead of stale ones. * fix(studio): scope the data-hf-id strip to tag brackets, not text content A clip's visible text can contain the literal string data-hf-id="..."; the previous whole-string regex stripped it as if it were the attribute, corrupting the copied text. Reproduced and confirmed the fix with a test. * fix(studio): extend root duration and coalesce cut on group paste/duplicate Paste and duplicate could silently truncate a group from export: they inserted clips past the composition's data-duration without growing it, so content existed in the file but never played or rendered. Both now route through the same grow-only ratchet blockInstaller already uses. Cmd+X on a multi-clip selection now deletes the whole selection in one call (the existing batched delete already used by the Delete hotkey), so one Cmd+Z restores all of them instead of needing one per clip. The data-hf-id strip and id-capture on paste now go through DOMParser and a tighter regex respectively, closing two escape hatches: a > inside an earlier attribute value, and a data-id attribute preceding the real id. * refactor(studio): share the id-attribute regex and hoist the clip DOMParser Simplify pass on the previous commit: the id-capture lookbehind was a second copy of the same pattern deduplicateIds already uses, now a single exported ID_ATTR_RE both call. The per-clip DOMParser instance in pasteTimelineClips is now created once per call, not once per clip. Adds a test pinning stripHfIds's no-parseable-root fallback. * fix(studio): close the stripHfIds hoist gap stripHfIds silently returned html unstripped when DOMParser found no body-level element (a tag the parser hoists into <head>, e.g. title or meta). Closes it with a regex fallback and a test reproducing the exact hoisted-tag case, not just the no-tag-at-all case from the prior commit. * refactor(studio): dedupe clip menu item classes into one helper The new Copy/Paste/Duplicate rows in ClipContextMenu each got their own static enabled/disabled className string instead of the parameterized itemClass(enabled) pattern the sibling TrackGapContextMenu already uses for the same job. Consolidated to one function; rendered classes are unchanged for every state. * refactor(studio): reuse timeRangesOverlap for clip paste placement tracksOverlap re-derived the exact half-open interval check timelineCollision.ts already exports and uses elsewhere on the timeline.
1 parent 93921dc commit 0b6d103

17 files changed

Lines changed: 808 additions & 125 deletions

‎packages/studio/src/App.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,15 @@ export function StudioApp() {
211211
const domEditDeleteBridge: DomEditDelete = (s, o) => handleDomEditElementDeleteRef.current(s, o);
212212
const resetKeyframesRef = useRef<() => boolean>(() => false);
213213
const deleteSelectedKeyframesRef = useRef<() => void>(() => {});
214-
const { handleCopy, handlePaste, handleCut } = useClipboard({
214+
const { handleCopy, handlePaste, handleCut, handleDuplicate, canPaste } = useClipboard({
215215
projectId,
216216
activeCompPath,
217217
domEditSelectionRef: domEditSelectionBridgeRef,
218218
showToast,
219219
writeProjectFile: fileManager.writeProjectFile,
220220
recordEdit: editHistory.recordEdit,
221221
reloadPreview,
222-
handleTimelineElementDelete: timelineEditing.handleTimelineElementDelete,
222+
handleTimelineElementsDelete: timelineEditing.handleTimelineElementsDelete,
223223
handleDomEditElementDelete: domEditDeleteBridge,
224224
previewIframeRef,
225225
});
@@ -240,6 +240,7 @@ export function StudioApp() {
240240
handleCopy,
241241
handlePaste,
242242
handleCut,
243+
handleDuplicate,
243244
onResetKeyframes: () => resetKeyframesRef.current(),
244245
onDeleteSelectedKeyframes: () => deleteSelectedKeyframesRef.current(),
245246
onAfterUndoRedo: () => invalidateGsapCacheRef.current(),
@@ -538,6 +539,10 @@ export function StudioApp() {
538539
handleTimelineElementSplit={timelineEditing.handleTimelineElementSplit}
539540
handleRazorSplit={timelineEditing.handleRazorSplit}
540541
handleRazorSplitAll={timelineEditing.handleRazorSplitAll}
542+
onCopyClip={handleCopy}
543+
onPasteClip={handlePaste}
544+
onDuplicateClip={handleDuplicate}
545+
canPasteClip={canPaste}
541546
setCompIdToSrc={setCompIdToSrc}
542547
setCompositionLoading={setCompositionLoading}
543548
shouldShowMotionPath={shouldShowMotionPath}

‎packages/studio/src/components/EditorShell.selectionSync.test.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ describe("EditorShell timeline selection sync", () => {
8686
handleTimelineElementSplit={vi.fn()}
8787
handleRazorSplit={vi.fn()}
8888
handleRazorSplitAll={vi.fn()}
89+
onCopyClip={vi.fn(() => false)}
90+
onPasteClip={vi.fn(async () => {})}
91+
onDuplicateClip={vi.fn(async () => false)}
92+
canPasteClip={vi.fn(() => false)}
8993
setCompIdToSrc={vi.fn()}
9094
setCompositionLoading={vi.fn()}
9195
shouldShowMotionPath={false}

‎packages/studio/src/components/EditorShell.tsx‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ export interface EditorShellProps extends TimelineEditCallbackDeps {
5555
files: File[],
5656
placement?: TimelineDropPlacement,
5757
) => Promise<void> | void;
58+
onCopyClip: () => boolean;
59+
onPasteClip: () => Promise<void>;
60+
onDuplicateClip: () => Promise<boolean>;
61+
canPasteClip: () => boolean;
5862
setCompIdToSrc: (map: Map<string, string>) => void;
5963
setCompositionLoading: (loading: boolean) => void;
6064
shouldShowMotionPath: boolean;
@@ -93,6 +97,10 @@ export function EditorShell({
9397
handleTimelineElementSplit,
9498
handleRazorSplit,
9599
handleRazorSplitAll,
100+
onCopyClip,
101+
onPasteClip,
102+
onDuplicateClip,
103+
canPasteClip,
96104
setCompIdToSrc,
97105
setCompositionLoading,
98106
shouldShowMotionPath,
@@ -181,6 +189,10 @@ export function EditorShell({
181189
onBlockDrop={handleTimelineBlockDrop}
182190
onCompositionDrop={handleTimelineCompositionDrop}
183191
onDeleteElement={handleTimelineElementDelete}
192+
onCopyClip={onCopyClip}
193+
onPasteClip={onPasteClip}
194+
onDuplicateClip={onDuplicateClip}
195+
canPasteClip={canPasteClip}
184196
previewOverlay={
185197
<PreviewOverlays
186198
shouldShowMotionPath={shouldShowMotionPath}
@@ -219,6 +231,10 @@ interface EditorShellBodyProps {
219231
placement: TimelineDropPlacement,
220232
) => Promise<void> | void;
221233
onDeleteElement: (element: TimelineElement) => Promise<void> | void;
234+
onCopyClip: () => boolean;
235+
onPasteClip: () => Promise<void>;
236+
onDuplicateClip: () => Promise<boolean>;
237+
canPasteClip: () => boolean;
222238
}
223239

224240
function EditorShellBody({
@@ -235,6 +251,10 @@ function EditorShellBody({
235251
onBlockDrop,
236252
onCompositionDrop,
237253
onDeleteElement,
254+
onCopyClip,
255+
onPasteClip,
256+
onDuplicateClip,
257+
canPasteClip,
238258
}: EditorShellBodyProps) {
239259
const { compositionStack, updateCompositionStack, containerRef } = useNLEContext();
240260

@@ -289,6 +309,10 @@ function EditorShellBody({
289309
onBlockDrop={onBlockDrop}
290310
onCompositionDrop={onCompositionDrop}
291311
onDeleteElement={onDeleteElement}
312+
onCopyClip={onCopyClip}
313+
onPasteClip={onPasteClip}
314+
onDuplicateClip={onDuplicateClip}
315+
canPasteClip={canPasteClip}
292316
onSelectTimelineElement={onSelectTimelineElement}
293317
timelineFooter={
294318
captionEditMode ? (

‎packages/studio/src/components/editor/SnapToolbar.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function AppHotkeyHarness() {
5353
handleCopy: vi.fn(() => false),
5454
handlePaste: vi.fn(async () => undefined),
5555
handleCut: vi.fn(async () => false),
56+
handleDuplicate: vi.fn(async () => false),
5657
onResetKeyframes: vi.fn(() => false),
5758
onDeleteSelectedKeyframes: vi.fn(),
5859
});

‎packages/studio/src/components/nle/TimelinePane.tsx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ export interface TimelinePaneProps {
102102
) => Promise<void> | void;
103103
onBlockedEditAttempt?: (element: TimelineElement, intent: BlockedTimelineEditIntent) => void;
104104
onSelectTimelineElement?: (element: TimelineElement | null) => void;
105+
/** Copy/paste/duplicate act on the store's own selection, not a passed
106+
* element, so unlike onDeleteElement they need no composition-basis wrapper. */
107+
onCopyClip?: () => boolean;
108+
onPasteClip?: () => Promise<void>;
109+
onDuplicateClip?: () => Promise<boolean>;
110+
canPasteClip?: () => boolean;
105111
}
106112

107113
// fallow-ignore-next-line complexity
@@ -116,6 +122,10 @@ export function TimelinePane({
116122
onCompositionDrop,
117123
onBlockedEditAttempt,
118124
onSelectTimelineElement,
125+
onCopyClip,
126+
onPasteClip,
127+
onDuplicateClip,
128+
canPasteClip,
119129
}: TimelinePaneProps) {
120130
const {
121131
seek,
@@ -288,6 +298,10 @@ export function TimelinePane({
288298
onBlockedEditAttempt={onBlockedEditAttempt}
289299
onSplitElement={handleSplitElement}
290300
onSelectElement={onSelectTimelineElement}
301+
onCopyClip={onCopyClip}
302+
onPasteClip={onPasteClip}
303+
onDuplicateClip={onDuplicateClip}
304+
canPasteClip={canPasteClip}
291305
/>
292306
</div>
293307
{timelineFooter && <div className="flex-shrink-0">{timelineFooter}</div>}

‎packages/studio/src/hooks/appHotkeysDispatch.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export interface HotkeyCallbacks {
4646
handleCopy: () => boolean;
4747
handlePaste: () => Promise<void>;
4848
handleCut: () => Promise<boolean>;
49+
handleDuplicate: () => Promise<boolean>;
4950
onResetKeyframes: () => boolean;
5051
onDeleteSelectedKeyframes: () => void;
5152
onToggleRecording?: () => void;
@@ -127,6 +128,16 @@ export function dispatchModifierKey(
127128
}
128129
return true;
129130
}
131+
if (key === "d") {
132+
// Always own this key here, even with nothing selected — otherwise the
133+
// browser's own Cmd+D (bookmark this page) fires over the editor.
134+
event.preventDefault();
135+
if (usePlayerStore.getState().selectedElementId) {
136+
trackStudioEvent("keyboard_shortcut", { action: "duplicate" });
137+
void cb.handleDuplicate();
138+
}
139+
return true;
140+
}
130141
}
131142
return false;
132143
}

‎packages/studio/src/hooks/useAppHotkeys.textEditing.test.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ function Harness() {
9393
handleCopy: vi.fn(() => false),
9494
handlePaste: vi.fn(async () => undefined),
9595
handleCut: vi.fn(async () => false),
96+
handleDuplicate: vi.fn(async () => false),
9697
onResetKeyframes: vi.fn(() => false),
9798
onDeleteSelectedKeyframes: keyframeDelete,
9899
});

‎packages/studio/src/hooks/useAppHotkeys.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ interface UseAppHotkeysParams {
114114
handleCopy: () => boolean;
115115
handlePaste: () => Promise<void>;
116116
handleCut: () => Promise<boolean>;
117+
handleDuplicate: () => Promise<boolean>;
117118
onResetKeyframes: () => boolean;
118119
onDeleteSelectedKeyframes: () => void;
119120
onAfterUndoRedo?: () => void;
@@ -150,6 +151,7 @@ export function useAppHotkeys({
150151
handleCopy,
151152
handlePaste,
152153
handleCut,
154+
handleDuplicate,
153155
onResetKeyframes,
154156
onDeleteSelectedKeyframes,
155157
onAfterUndoRedo,
@@ -255,6 +257,7 @@ export function useAppHotkeys({
255257
handleCopy,
256258
handlePaste,
257259
handleCut,
260+
handleDuplicate,
258261
onResetKeyframes,
259262
onDeleteSelectedKeyframes,
260263
onToggleRecording,

0 commit comments

Comments
 (0)