From c2ca2291cab668d8ffca15021e3843494c8bdd9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sat, 19 Sep 2026 05:28:06 -0400 Subject: [PATCH] fix(studio): dropping an asset between or above timeline rows opens a new row (#4160) * fix(studio): dropping an asset between or above timeline lanes opens a new track there * refactor(studio): reuse the source patcher and shared drop-placement type for track inserts * fix(studio): the dropped clip must carry its peers' sourceFile, not the resolved path planDropTrackInsert stamped the synthetic dropped element with the resolved target path string, but real elements on the main document carry no sourceFile at all. sameSourceFile compared the two directly, so the dropped element always read as belonging to a different file and every peer was skipped as unwritable, silently landing the drop on an existing track instead of opening one. * refactor(studio): trim drop-track comments and share one can-insert-track predicate Drop the TimelinePlacement alias, compute the file-or-asset check once for dragover and drop, and shorten two comments that narrated history. * fix(studio): plan a dropped-in track against the visible rows, not clips the timeline hides The insert row counts the rows the timeline shows, but the plan was built from every same-file clip. A clip the timeline does not show as its own row shifted the boundary and renumbered lanes it should have left alone. * test(studio): assert the dropped clip lands on the opened lane * fix(studio): a row drop measures its boundary on the same row order a clip drag does The drop path indexed the insert row against the rows the timeline shows, which include audio-group anchor rows, but planned it against a plain sorted track list. An audio group above the boundary shifted the new row by one. The placement now carries the displayed row order and the planner uses it, as the clip drag already does. --- .../studio/src/components/EditorShell.tsx | 2 +- .../hooks/useTimelineAssetDropOps.test.tsx | 42 +++++- .../src/hooks/useTimelineAssetDropOps.ts | 63 ++++++-- .../src/player/components/TimelineCanvas.tsx | 12 +- .../player/components/timelineCallbacks.ts | 16 +-- .../components/timelineDragDrop.test.tsx | 62 +++++++- .../src/player/components/timelineDragDrop.ts | 72 ++++++++-- .../src/utils/timelineDropTrackInsert.test.ts | 134 ++++++++++++++++++ .../src/utils/timelineDropTrackInsert.ts | 83 +++++++++++ 9 files changed, 445 insertions(+), 41 deletions(-) create mode 100644 packages/studio/src/utils/timelineDropTrackInsert.test.ts create mode 100644 packages/studio/src/utils/timelineDropTrackInsert.ts diff --git a/packages/studio/src/components/EditorShell.tsx b/packages/studio/src/components/EditorShell.tsx index a7cbec2941..f7b2b9295b 100644 --- a/packages/studio/src/components/EditorShell.tsx +++ b/packages/studio/src/components/EditorShell.tsx @@ -16,12 +16,12 @@ import type { BlockPreviewInfo } from "./sidebar/BlocksTab"; import type { GestureRecordingState } from "./editor/GestureRecordControl"; import { useTimelineSelectionPreviewSync } from "../hooks/useTimelineSelectionPreviewSync"; import { StudioAgentTools } from "../webmcp/StudioAgentTools"; +import type { TimelineDropPlacement } from "../player/components/timelineCallbacks"; type RenderClipContent = ( element: TimelineElement, style: { clip: string; label: string }, ) => ReactNode; -type TimelineDropPlacement = Pick; // The seven move/resize/split/razor handlers come from TimelineEditCallbackDeps // (shared with useTimelineEditCallbacks); the rest are drop + wiring props. diff --git a/packages/studio/src/hooks/useTimelineAssetDropOps.test.tsx b/packages/studio/src/hooks/useTimelineAssetDropOps.test.tsx index 623a0c6abd..c1ae287345 100644 --- a/packages/studio/src/hooks/useTimelineAssetDropOps.test.tsx +++ b/packages/studio/src/hooks/useTimelineAssetDropOps.test.tsx @@ -5,6 +5,7 @@ import { afterEach, describe, expect, it, vi } from "vitest"; import { useTimelineAssetDropOps } from "./useTimelineAssetDropOps"; import { mountReactHarness } from "./domSelectionTestHarness"; import { usePlayerStore } from "../player/store/playerStore"; +import type { TimelineElement } from "../player"; (globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true; @@ -19,6 +20,7 @@ type DropFn = ReturnType["handleTimelineAssetDro function renderDropHook( sourceContent: string, writeProjectFile: (path: string, content: string, expectedContent?: string) => Promise, + timelineElements: TimelineElement[] = [], ) { vi.stubGlobal( "fetch", @@ -29,7 +31,7 @@ function renderDropHook( const { handleTimelineAssetDrop } = useTimelineAssetDropOps({ projectIdRef: { current: "project" }, activeCompPath: "index.html", - timelineElements: [], + timelineElements, showToast: vi.fn(), writeProjectFile, recordEdit: vi.fn().mockResolvedValue(undefined), @@ -84,4 +86,42 @@ describe("useTimelineAssetDropOps handleTimelineAssetDrop", () => { expect(usePlayerStore.getState().selectedElementId).toBe("index.html#clip"); }); + + it("measures the insert row against the visible rows, not clips the timeline hides", async () => { + const clip = (id: string, track: number): TimelineElement => ({ + id, + key: id, + tag: "div", + start: 0, + duration: 2, + track, + authoredTrack: track, + hfId: `hf-${id}`, + domId: id, + }); + const source = [ + '
', + ...["a:0", "hidden:1", "b:2"].map((t) => { + const [id, track] = t.split(":"); + return `
`; + }), + "
", + ].join("\n"); + usePlayerStore.getState().setTopLevelIds(new Set(["a", "b"])); + const writeProjectFile = vi.fn().mockResolvedValue(undefined); + const getDrop = renderDropHook(source, writeProjectFile, [ + clip("a", 0), + clip("hidden", 1), + clip("b", 2), + ]); + + await act(async () => { + await getDrop()("clip.mp4", { start: 1, track: 1, insertRow: 1, trackOrder: [0, 2] }, 2); + }); + + const [, written] = writeProjectFile.mock.calls[0] as [string, string]; + expect(written).toContain('id="b" data-start="0" data-track-index="2"'); + expect(written).toContain('id="hidden" data-start="0" data-track-index="1"'); + expect(written).toMatch(/