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.
This commit is contained in:
Miguel Ángel
2026-09-19 05:28:06 -04:00
committed by GitHub
parent e8fdfd0d65
commit c2ca2291ca
9 changed files with 445 additions and 41 deletions
@@ -0,0 +1,134 @@
import { describe, expect, it } from "vitest";
import type { TimelineElement } from "../player";
import {
applyTrackRenumbers,
planDropTrackInsert,
resolveDropTrack,
} from "./timelineDropTrackInsert";
function clip(id: string, track: number, start: number): TimelineElement {
return {
id,
key: id,
tag: "div",
start,
duration: 2,
track,
authoredTrack: track,
hfId: `hf-${id}`,
domId: id,
// Main-document elements carry no sourceFile, as in production.
};
}
const trackOrder = [0, 1, 2];
const dropped = { id: "new", tag: "img", start: 5, duration: 3 };
const elements = [clip("a", 0, 0), clip("b", 1, 0), clip("c", 2, 0)];
const source = elements
.map(
(e) =>
`<div data-hf-id="${e.hfId}" id="${e.id}" data-start="0" data-track-index="${e.track}"></div>`,
)
.join("\n");
describe("planDropTrackInsert", () => {
it("opens a lane between two rows and pushes the lanes below down", () => {
const plan = planDropTrackInsert({ elements, trackOrder, insertRow: 1, dropped });
expect(plan?.track).toBe(1);
expect(plan?.renumbers.map((r) => [r.element.id, r.track])).toEqual([
["b", 2],
["c", 3],
]);
});
it("opens a lane above the first row", () => {
const plan = planDropTrackInsert({ elements, trackOrder, insertRow: 0, dropped });
expect(plan?.track).toBe(0);
expect(plan?.renumbers.map((r) => [r.element.id, r.track])).toEqual([
["a", 1],
["b", 2],
["c", 3],
]);
});
it("still renumbers when the peers' own sourceFile is a real path, not undefined", () => {
const subComp = elements.map((e) => ({ ...e, sourceFile: "sub.html" }));
const plan = planDropTrackInsert({ elements: subComp, trackOrder, insertRow: 1, dropped });
expect(plan?.track).toBe(1);
expect(plan?.renumbers.map((r) => [r.element.id, r.track])).toEqual([
["b", 2],
["c", 3],
]);
});
});
describe("planDropTrackInsert against the display row order", () => {
it("counts an audio-group anchor row the way a clip drag does", () => {
// Rows shown: group anchor (-0.5), track 0, track 1. Row boundary 2 is between 0 and 1.
const grouped = elements.slice(0, 2).map((e) => (e.id === "a" ? { ...e, audioGroup: "g" } : e));
const plan = planDropTrackInsert({
elements: grouped,
trackOrder: [-0.5, 0, 1],
insertRow: 2,
dropped,
});
expect(plan?.track).toBe(1);
expect(plan?.renumbers.map((r) => [r.element.id, r.track])).toEqual([["b", 2]]);
});
});
describe("applyTrackRenumbers", () => {
it("rewrites only the renumbered clips' data-track-index", () => {
const plan = planDropTrackInsert({ elements, trackOrder, insertRow: 1, dropped });
const out = applyTrackRenumbers(source, plan!);
const tracks = [...out.matchAll(/id="(\w)"[^>]*data-track-index="(\d+)"/g)].map((m) => [
m[1],
+m[2],
]);
expect(tracks).toEqual([
["a", 0],
["b", 2],
["c", 3],
]);
});
it("throws when a clip's opening tag is missing", () => {
const plan = planDropTrackInsert({ elements, trackOrder, insertRow: 1, dropped });
expect(() => applyTrackRenumbers("<div></div>", plan!)).toThrow(/Cannot renumber/);
});
});
describe("resolveDropTrack", () => {
it("keeps the aimed lane and the source when no insert is asked for", () => {
const out = resolveDropTrack({
source,
elements,
placement: { track: 2 },
dropped,
});
expect(out).toEqual({ source, track: 2 });
});
it("returns the new lane and the source with the lanes below pushed down", () => {
const out = resolveDropTrack({
source,
elements,
placement: { track: 1, insertRow: 1, trackOrder },
dropped,
});
expect(out.track).toBe(1);
expect(out.source).toContain('id="b" data-start="0" data-track-index="2"');
});
it("refuses to insert when a locked clip would have to move", () => {
const locked = elements.map((e) => (e.id === "b" ? { ...e, timelineLocked: true } : e));
expect(() =>
resolveDropTrack({
source,
elements: locked,
placement: { track: 1, insertRow: 1, trackOrder },
dropped,
}),
).toThrow(/locked/);
});
});
@@ -0,0 +1,83 @@
import type { TimelineElement } from "../player";
import { layoutAfterTrackInsert } from "../player/components/timelineDragLanding";
import { canMoveTimelineElement } from "../player/components/timelineAuthoredMoveTarget";
import type { TimelineDropPlacement } from "../player/components/timelineCallbacks";
import { applyPatchByTarget, readAttributeByTarget } from "./sourcePatcher";
import { buildPatchTarget } from "../hooks/timelineEditingHelpers";
import { formatTimelineAttributeNumber } from "../player/components/timelineEditing";
export interface DropTrackInsertPlan {
/** Lane the dropped clip is written on. */
track: number;
/** Existing clips whose lane changes to make room. */
renumbers: Array<{ element: TimelineElement; track: number }>;
}
/** Plan a new lane at boundary `insertRow` with the renumber a clip drag into a gutter uses.
* Null when a locked clip would have to move. */
export function planDropTrackInsert(input: {
elements: TimelineElement[];
/** The row order the timeline shows (anchor rows included), which `insertRow` indexes. */
trackOrder: readonly number[];
insertRow: number;
dropped: Pick<TimelineElement, "id" | "tag" | "start" | "duration">;
}): DropTrackInsertPlan | null {
const { elements, trackOrder, insertRow, dropped } = input;
const newElement: TimelineElement = {
...dropped,
key: dropped.id,
// Parked on an existing lane so it adds no lane of its own to the topology.
track: elements[0]?.track ?? 0,
// sameSourceFile compares this raw field: borrow the peers' value, not the resolved path.
sourceFile: elements[0]?.sourceFile,
};
const layout = layoutAfterTrackInsert(newElement, dropped.start, insertRow, null, {
elements: [...elements, newElement],
trackOrder: [...trackOrder],
});
if (!layout) return null;
const byKey = new Map(elements.map((e) => [e.key ?? e.id, e]));
const renumbers: DropTrackInsertPlan["renumbers"] = [];
for (const norm of layout.normalized) {
const key = norm.key ?? norm.id;
if (key === dropped.id) continue;
const src = byKey.get(key);
if (!src || norm.track === (src.authoredTrack ?? src.track)) continue;
if (!canMoveTimelineElement(src)) return null;
renumbers.push({ element: src, track: norm.track });
}
const track = layout.normalized.find((n) => (n.key ?? n.id) === dropped.id)?.track;
return track == null ? null : { track, renumbers };
}
/** Rewrite `data-track-index` on each renumbered clip's opening tag, via the shared source patcher. */
export function applyTrackRenumbers(source: string, plan: DropTrackInsertPlan): string {
let out = source;
for (const { element, track } of plan.renumbers) {
const target = buildPatchTarget(element);
if (!target || readAttributeByTarget(out, target, "track-index") === undefined) {
throw new Error(`Cannot renumber the track of "${element.id}" in the source`);
}
out = applyPatchByTarget(out, target, {
type: "attribute",
property: "track-index",
value: formatTimelineAttributeNumber(track),
});
}
return out;
}
/** The lane a dropped clip is written on and the source with any lanes pushed down to make room. */
export function resolveDropTrack(input: {
source: string;
elements: TimelineElement[];
placement: TimelineDropPlacement;
dropped: Pick<TimelineElement, "id" | "tag" | "start" | "duration">;
}): { source: string; track: number } {
const { source, elements, placement, dropped } = input;
if (placement.insertRow == null) return { source, track: placement.track };
const { insertRow, trackOrder } = placement;
const plan = planDropTrackInsert({ elements, trackOrder, insertRow, dropped });
if (!plan) throw new Error("Cannot open a new track here: a locked clip would have to move.");
return { source: applyTrackRenumbers(source, plan), track: plan.track };
}