Skip to content

Commit f0b490b

Browse files
committed
feat(studio): clip drag engine — preview and commit (unwired)
What: the NLE clip-drag engine core: timelineClipDragPreview (zone-aware preview placement; keeps the legacy resolveDragPreviewPlacement export until the timeline swap), timelineClipDragCommit (drop → move/insert commit, stacking-synced) and timelineElementsMove (batched ripple/insert move with GSAP position shifting), with test suites. Why: the drag-drop brain, reviewable as functions before any component mounts it. How: new files plus one authored intermediate (preview file = final content + legacy export the old engine still imports); consumes the collision/ snapping/zones/stacking modules and the coexistence layer. Test plan: bunx vitest run on both test suites; tsc --noEmit; fallow audit clean.
1 parent 2a55fa3 commit f0b490b

7 files changed

Lines changed: 1227 additions & 17 deletions

File tree

‎.fallowrc.jsonc‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@
103103
"packages/cli/src/cloud/_gen/**",
104104
],
105105
"ignoreExports": [
106+
// TEMP(studio-dnd): consumers land later in the stack; removed by studio-dnd/pr22.
107+
{
108+
"file": "packages/studio/src/hooks/timelineElementsMove.ts",
109+
"exports": ["useTimelineElementsMove"],
110+
},
111+
// TEMP(studio-dnd): consumers land later in the stack; removed by studio-dnd/pr22.
112+
{
113+
"file": "packages/studio/src/player/components/timelineClipDragPreview.ts",
114+
"exports": ["computeDragPreview", "computeResizePreview"],
115+
},
106116
// TEMP(studio-dnd): consumers land later in the stack; removed by studio-dnd/pr22.
107117
{
108118
"file": "packages/studio/src/player/lib/timelineElementHelpers.ts",
@@ -429,6 +439,15 @@
429439
// require intrusive middleware changes beyond this PR's scope.
430440
"minLines": 6,
431441
"ignore": [
442+
// TEMP(studio-dnd): coexistence-window clone (extraction vs still-live original);
443+
// studio-dnd pr22 removes this with the final config.
444+
"packages/studio/src/utils/timelineAssetDrop.ts",
445+
// TEMP(studio-dnd): coexistence-window clone (extraction vs still-live original);
446+
// studio-dnd pr22 removes this with the final config.
447+
"packages/studio/src/components/nle/NLEContext.test.ts",
448+
// TEMP(studio-dnd): coexistence-window clone (extraction vs still-live original);
449+
// studio-dnd pr22 removes this with the final config.
450+
"packages/studio/src/components/nle/NLELayout.test.ts",
432451
// TEMP(studio-dnd): coexistence-window clone (extraction vs still-live original);
433452
// studio-dnd pr22 removes this with the final config.
434453
"packages/studio/src/components/editor/domEditOverlayGeometry.ts",
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
// @vitest-environment jsdom
2+
import { beforeEach, describe, expect, it, vi } from "vitest";
3+
import type { TimelineElement } from "../player";
4+
import { furthestClipEndFromSource } from "../player/lib/timelineElementHelpers";
5+
import { persistTimelineElementsMove, type TimelineElementMoveEdit } from "./timelineElementsMove";
6+
7+
function el(over: Partial<TimelineElement> & Pick<TimelineElement, "id">): TimelineElement {
8+
return { tag: "div", start: 0, duration: 1, track: 0, ...over };
9+
}
10+
11+
// A bed mirroring the user's qa-clean: an 8s audio clip whose real end sits past a
12+
// STALE root data-duration. `furthestClipEndFromSource` must read the raw 8, not
13+
// the stale root or a truncated value.
14+
const bed = (rootDuration: number, musicStart: number) =>
15+
`<!DOCTYPE html><html><body>
16+
<div data-hf-id="hf-root" id="root" data-composition-id="qa-clean" data-start="0" data-duration="${rootDuration}" data-no-timeline="">
17+
<video data-hf-id="hf-v1" id="bg-video" class="clip" data-start="1" data-duration="3" data-track-index="0"></video>
18+
<audio data-hf-id="hf-music" id="bg-music" class="clip" data-start="${musicStart}" data-duration="8" data-track-index="2"></audio>
19+
</div>
20+
</body></html>`;
21+
22+
describe("furthestClipEndFromSource", () => {
23+
it("reads the RAW data-duration, past a stale root duration", () => {
24+
// audio 11.53 + 8 = 19.53, even though the root claims only 15.18
25+
expect(furthestClipEndFromSource(bed(15.18, 11.53))).toBeCloseTo(19.53, 5);
26+
});
27+
28+
it("excludes the composition root itself", () => {
29+
// root data-duration is huge; furthest CLIP end is the audio at 8.88
30+
const src = `<html><body><div data-composition-id="c" data-start="0" data-duration="99" data-no-timeline="">
31+
<audio data-hf-id="a" data-start="4.88" data-duration="4" data-track-index="0"></audio>
32+
</div></body></html>`;
33+
expect(furthestClipEndFromSource(src)).toBeCloseTo(8.88, 5);
34+
});
35+
36+
it("returns 0 when there are no clips", () => {
37+
expect(
38+
furthestClipEndFromSource(
39+
`<html><body><div data-composition-id="c" data-start="0" data-duration="10" data-no-timeline=""></div></body></html>`,
40+
),
41+
).toBe(0);
42+
});
43+
44+
it("returns 0 for empty input", () => {
45+
expect(furthestClipEndFromSource("")).toBe(0);
46+
});
47+
});
48+
49+
// ── Persist-level test (the G2 + §6.1 feedback-loop boundary) ────────────────
50+
// Proves the batched move writes the root duration computed from the SAVED SOURCE
51+
// (raw data-duration), not the stale root value and not a runtime-truncated store
52+
// duration. Without the source-based calc this shrinks/stalls the composition.
53+
const h = vi.hoisted(() => ({ source: "", savedFiles: [] as Record<string, string>[] }));
54+
55+
// Stub the player store so persist's optimistic setDuration() doesn't mutate the
56+
// real store and leak duration into later suite tests (test isolation).
57+
vi.mock("../player", () => ({
58+
usePlayerStore: { getState: () => ({ setDuration: () => {} }) },
59+
}));
60+
61+
vi.mock("../utils/studioFileHistory", () => ({
62+
saveProjectFilesWithHistory: vi.fn(async (input: { files: Record<string, string> }) => {
63+
h.savedFiles.push(input.files);
64+
}),
65+
}));
66+
67+
const readFileContentMock = vi.hoisted(() =>
68+
vi.fn(async (_projectId: string, _path: string): Promise<string> => ""),
69+
);
70+
71+
vi.mock("./timelineEditingHelpers", async (importActual) => {
72+
const actual = await importActual<typeof import("./timelineEditingHelpers")>();
73+
return {
74+
...actual,
75+
readFileContent: readFileContentMock,
76+
patchIframeDomTiming: vi.fn(),
77+
shiftGsapPositionsBatch: vi.fn(async () => {}),
78+
};
79+
});
80+
81+
function move(
82+
element: TimelineElement,
83+
start: number,
84+
track = element.track,
85+
): TimelineElementMoveEdit {
86+
return { element, updates: { start, track } };
87+
}
88+
89+
describe("persistTimelineElementsMove — writes source-derived duration", () => {
90+
beforeEach(() => {
91+
h.savedFiles.length = 0;
92+
readFileContentMock.mockReset();
93+
readFileContentMock.mockImplementation(async () => h.source);
94+
});
95+
96+
it("grows the root duration to the moved audio's real 8s end (not the stale 15.18)", async () => {
97+
// Stale bed: root says 15.18; audio (8s) currently ends there at start 7.18.
98+
h.source = bed(15.18, 7.18);
99+
const music = el({ id: "hf-music", hfId: "hf-music", start: 7.18, duration: 8, track: 2 });
100+
101+
await persistTimelineElementsMove([move(music, 11.53)], {
102+
projectId: "p",
103+
activeCompPath: "index.html",
104+
previewIframe: null,
105+
writeProjectFile: async () => {},
106+
recordEdit: async () => {},
107+
reloadPreview: () => {},
108+
domEditSaveTimestampRef: { current: 0 },
109+
});
110+
111+
expect(h.savedFiles).toHaveLength(1);
112+
const saved = h.savedFiles[0]["index.html"];
113+
expect(saved).toContain('data-start="11.53"'); // audio moved
114+
expect(saved).toContain('data-duration="19.53"'); // grown to the raw 8s end
115+
expect(saved).not.toContain('data-duration="15.18"'); // stale root gone
116+
});
117+
118+
// PORT 2 — group-move rollback discipline. A move whose clips span two source
119+
// files (e.g. a sub-comp) must land as ONE atomic save with ONE history entry,
120+
// not one write-per-file. saveProjectFilesWithHistory already writes-all /
121+
// records-one / rolls-back-all, so passing every file in a single call gives
122+
// all-or-nothing on disk that matches the caller's all-or-nothing store rollback.
123+
it("folds clips from two source files into ONE atomic save (single history entry)", async () => {
124+
// Distinct source per path; a single move edit touches each file.
125+
const perPath: Record<string, string> = {
126+
"index.html": bed(15.18, 7.18),
127+
"scenes/intro.html": bed(15.18, 2),
128+
};
129+
readFileContentMock.mockImplementation(async (_pid: string, path: string) => perPath[path]);
130+
131+
const rootMusic = el({ id: "hf-music", hfId: "hf-music", start: 7.18, duration: 8, track: 2 });
132+
const subMusic = el({
133+
id: "hf-music",
134+
hfId: "hf-music",
135+
start: 2,
136+
duration: 8,
137+
track: 2,
138+
sourceFile: "scenes/intro.html",
139+
});
140+
141+
await persistTimelineElementsMove([move(rootMusic, 9), move(subMusic, 4)], {
142+
projectId: "p",
143+
activeCompPath: "index.html",
144+
previewIframe: null,
145+
writeProjectFile: async () => {},
146+
recordEdit: async () => {},
147+
reloadPreview: () => {},
148+
domEditSaveTimestampRef: { current: 0 },
149+
});
150+
151+
// ONE save call carrying BOTH files (not two per-file saves).
152+
expect(h.savedFiles).toHaveLength(1);
153+
expect(Object.keys(h.savedFiles[0]).sort()).toEqual(["index.html", "scenes/intro.html"]);
154+
expect(h.savedFiles[0]["index.html"]).toContain('data-start="9"');
155+
expect(h.savedFiles[0]["scenes/intro.html"]).toContain('data-start="4"');
156+
});
157+
});

0 commit comments

Comments
 (0)