Skip to content

Open source CapCut alternative.

TypeScript

fix(site): land every member of a timeline group drag

David Le8d9d0e90bfe6
diff --git a/site/scripts/eval-cut-timeline-rows.ts b/site/scripts/eval-cut-timeline-rows.ts
index cba0f736..cb8c18d9 100644
--- a/site/scripts/eval-cut-timeline-rows.ts
+++ b/site/scripts/eval-cut-timeline-rows.ts
@@ -77,6 +77,44 @@ try {
assert.deepEqual(await page.evaluate(() => window.__cutDev.useEditor.getState().overlays), before);
console.log(`PASS existing sticker: drag to new row ${side} and undo`);
}
+ // A group drag reorders the whole set past the band's edges: the grabbed
+ // member reaches the new row and the rest open the rows beyond it.
+ for (const side of ["above", "below"] as const) {
+ const ids = await page.evaluate(() => {
+ const store = window.__cutDev.useEditor;
+ store.setState({ overlays: [], selection: null, multiSelection: [] });
+ store.getState().addOverlay({ at: 0, lane: 0 });
+ const title = store.getState().selection!.id;
+ store.getState().addSticker({ assetId: "sticker-asset", at: 0, lane: 1 });
+ const a = store.getState().selection!.id;
+ store.getState().addSticker({ assetId: "sticker-asset", at: 5, lane: 2 });
+ const b = store.getState().selection!.id;
+ return { title, a, b };
+ });
+ const grabbed = side === "below" ? ids.title : ids.b;
+ const other = ids.a;
+ await page.evaluate(({ grabbed, other }) => {
+ window.__cutDev.useEditor.getState().setMultiSelection([{ kind: "overlay", id: grabbed }, { kind: "overlay", id: other }]);
+ }, { grabbed, other });
+ const bar = await page.locator(`[data-tl-sel="overlay:${grabbed}"]`).boundingBox();
+ const band = await page.locator("[data-tl-trows]").boundingBox();
+ assert(bar && band);
+ const before = await page.evaluate(() => window.__cutDev.useEditor.getState().overlays);
+ const x = bar.x + bar.width / 2;
+ const y = side === "above" ? band.y - NEW_ROW_PX - 2 : band.y + band.height + NEW_ROW_PX + 2;
+ await page.mouse.move(x, bar.y + bar.height / 2);
+ await page.mouse.down();
+ await page.mouse.move(x, y, { steps: 8 });
+ const rows = await page.locator("[data-tl-trows]").boundingBox();
+ assert(rows && rows.height > band.height);
+ await page.mouse.up();
+ const lanes = await page.evaluate(() => Object.fromEntries(window.__cutDev.useEditor.getState().overlays.map((o) => [o.id, o.lane ?? 0])));
+ if (side === "below") assert.deepEqual(lanes, { [ids.b]: 0, [ids.title]: 1, [ids.a]: 2 });
+ else assert.deepEqual(lanes, { [ids.a]: 0, [ids.b]: 1, [ids.title]: 2 });
+ await page.evaluate(() => window.__cutDev.useEditor.getState().undo());
+ assert.deepEqual(await page.evaluate(() => window.__cutDev.useEditor.getState().overlays), before);
+ console.log(`PASS group: drag two members to new rows ${side} and undo`);
+ }
} finally {
await browser.close();
}
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index b5da25e2..4ec930bb 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -49,7 +49,7 @@ import { useExports } from "@/cut/lib/exportStore";
import { isDragActive, startDrag, subscribeDragActive } from "@/cut/lib/drag";
import { keyboardToEditor, shortcutDecline } from "@/cut/lib/shortcutGate";
import { additiveClick } from "@/cut/lib/hostKeys";
-import { CLIP_GAP, landOnRow, laneDragFor, laneDragParts, resolveRow, startLaneMove, startLaneTrim, type LaneDrag, type RowBox } from "@/cut/lib/laneTracks";
+import { CLIP_GAP, landOnRow, laneDragFor, laneDragParts, laneDragSlots, resolveRow, startLaneMove, startLaneTrim, type LaneDrag, type RowBox } from "@/cut/lib/laneTracks";
import { reportSwallowed } from "@/cut/lib/report";
import { downloadMedia, dropEdgeFrames, ensurePeaks, importImage, importStockAudio, importStockVideo, peekEdgeFrame, requestEdgeFrame, revealMedia, stripFailedFor, subscribeStripStatus } from "@/cut/lib/media";
import { planFilmstrip, type FilmTile } from "@/cut/lib/filmstrip";
@@ -943,10 +943,10 @@ export function Timeline() {
}, [overlays]);
// The color an element drag paints its landing slot with: the carried item's
// own, so the move reads as one thing from grab to drop.
- const draggedFamily = useMemo<OverlayFamily>(() => {
- const held = laneDrag?.kind === "overlay" ? overlays.find((o) => o.id === laneDrag.id) : null;
+ const slotFamily = (id: string): OverlayFamily => {
+ const held = overlays.find((o) => o.id === id);
return held ? overlayFamily(held) : "text";
- }, [laneDrag, overlays]);
+ };
// Video tracks above track 0 (PiP / composited layers), listed highest-first
// so the top row is the frontmost layer and track 0 sits at the bottom of
@@ -1404,10 +1404,16 @@ export function Timeline() {
// A transition bar mid-drag, drawn where the pointer has it.
// A drag past the top edge opens a row there, pushing the stack down by one
// for as long as it is aimed that way.
- const topRowShift =
- (laneDrag?.kind === "overlay" && laneDrag.targetRow < 0) || elementDrop?.row === -1 ? TEXT_H : 0;
- const audioTopShift =
- (laneDrag?.kind === "audio" && laneDrag.targetRow < 0) || audioDrop?.row === -1 ? AUDIO_H : 0;
+ // A group drag aims every member at its own row, so the band opens as many
+ // rows past either end as the set needs.
+ const overlaySlots = laneDragSlots(laneDrag, "overlay");
+ const audioSlots = laneDragSlots(laneDrag, "audio");
+ const videoSlots = laneDragSlots(laneDrag, "video");
+ const cueSlots = laneDragSlots(laneDrag, "cue");
+ const rowsAbove = (slots: LaneDrag[]) => Math.max(0, ...slots.map((d) => -d.targetRow));
+ const lastRow = (slots: LaneDrag[]) => Math.max(-1, ...slots.map((d) => d.targetRow));
+ const topRowShift = Math.max(rowsAbove(overlaySlots), elementDrop?.row === -1 ? 1 : 0) * TEXT_H;
+ const audioTopShift = Math.max(rowsAbove(audioSlots), audioDrop?.row === -1 ? 1 : 0) * AUDIO_H;
// Scrub with auto-scroll when the pointer nears the viewport edges.
//
@@ -1822,21 +1828,26 @@ export function Timeline() {
// carries the rows below it down with the pointer's own ghost), so the row
// the drag came from stays where it was. Dropping here opens a brand-new
// track at z-level `level`.
+ // A group drag can carry members several rows past the edge; each row it
+ // reaches mounts, nearest the stack last on top and first below.
const newTrackRow = (level: number) => {
const place: TrackTarget = { kind: "insert", level };
+ const edge = level === 0 ? videoRowCount : -1;
+ const reach = level === 0 ? lastRow(videoSlots) - videoRowCount + 1 : rowsAbove(videoSlots);
const aimed =
- (laneDrag?.kind === "video" && laneDrag.targetRow === (level === 0 ? videoRowCount : -1)) ||
+ reach > 0 ||
(dropType === "video" && samePlacement(overlayDrop?.target ?? null, place));
if (!aimed) return null;
- return (
- <div className="relative mt-1.5" style={{ height: OVERLAY_H }}>
+ const rows = Array.from({ length: Math.max(1, reach) }, (_, i) => (level === 0 ? edge + i : edge - (reach - 1 - i)));
+ return rows.map((row) => (
+ <div key={row} className="relative mt-1.5" style={{ height: OVERLAY_H }}>
{laneRail(OVERLAY_H - 2)}
- {trackSlot(place, OVERLAY_H - 4)}
- {laneDrag?.kind === "video" && (
- <LaneSlot drag={laneDrag} pps={pps} top={2} barH={OVERLAY_H - 4} className={VIDEO_SLOT} />
- )}
+ {row === edge && trackSlot(place, OVERLAY_H - 4)}
+ {videoSlots.filter((d) => d.targetRow === row).map((d) => (
+ <LaneSlot key={d.id} drag={d} pps={pps} top={2} barH={OVERLAY_H - 4} className={VIDEO_SLOT} />
+ ))}
</div>
- );
+ ));
};
@@ -2234,8 +2245,8 @@ export function Timeline() {
Math.max(
overlayLanes.count,
// An in-flight element drag aimed under the band opens
- // the row it will land on.
- laneDrag?.kind === "overlay" ? laneDrag.targetRow + 1 : 0,
+ // the rows it will land on.
+ lastRow(overlaySlots) + 1,
// A panel drag over an empty band opens the first row for
// its landing preview.
elementDrop !== null ? elementDrop.row + 1 : 0
@@ -2249,9 +2260,8 @@ export function Timeline() {
{Array.from(
{
length:
- Math.max(overlayLanes.count, (elementDrop?.row ?? -1) + 1) +
- (topRowShift ? 1 : 0) +
- (laneDrag?.kind === "overlay" && laneDrag.targetRow === overlayLanes.count ? 1 : 0),
+ Math.max(overlayLanes.count, (elementDrop?.row ?? -1) + 1, lastRow(overlaySlots) + 1) +
+ topRowShift / TEXT_H,
},
(_, r) => laneRail((r + 1) * TEXT_H - 4, r)
)}
@@ -2342,15 +2352,16 @@ export function Timeline() {
</>
);
})()}
- {laneDrag?.kind === "overlay" && (
+ {overlaySlots.map((d) => (
<LaneSlot
- drag={laneDrag}
+ key={d.id}
+ drag={d}
pps={pps}
- top={laneDrag.targetRow * TEXT_H + 2 + topRowShift}
+ top={d.targetRow * TEXT_H + 2 + topRowShift}
barH={TEXT_H - 6}
- className={cn("rounded-[3px]", FAMILY_STYLE[draggedFamily].slot)}
+ className={cn("rounded-[3px]", FAMILY_STYLE[slotFamily(d.id)].slot)}
/>
- )}
+ ))}
{overlays.map((o) => {
const homeRow = overlayLanes.rowOf.get(o.lane ?? 0) ?? 0;
const drag = laneDragFor(laneDrag, "overlay", o.id);
@@ -2517,9 +2528,9 @@ export function Timeline() {
>
{laneRail(rowH - 2 + railH)}
{gapHighlight({ kind: "video", index: track }, rowH - 4)}
- {laneDrag?.kind === "video" && laneDrag.targetRow === row && (
- <LaneSlot drag={laneDrag} pps={pps} top={2} barH={rowH - 4} className={VIDEO_SLOT} />
- )}
+ {videoSlots.filter((d) => d.targetRow === row).map((d) => (
+ <LaneSlot key={d.id} drag={d} pps={pps} top={2} barH={rowH - 4} className={VIDEO_SLOT} />
+ ))}
{trackSpans.map((span) => (
<ClipView
key={span.clip.id}
@@ -2573,9 +2584,9 @@ export function Timeline() {
{spans.length > 0 && laneRail(rowH0 - 2 + rail0)}
{gapHighlight({ kind: "video", index: 0 }, rowH0 - 4)}
{trackSlot(TRACK_ZERO, rowH0 - 4)}
- {laneDrag?.kind === "video" && laneDrag.targetRow === aboveTracks.length && (
- <LaneSlot drag={laneDrag} pps={pps} top={2} barH={rowH0 - 4} className={VIDEO_SLOT} />
- )}
+ {videoSlots.filter((d) => d.targetRow === aboveTracks.length).map((d) => (
+ <LaneSlot key={d.id} drag={d} pps={pps} top={2} barH={rowH0 - 4} className={VIDEO_SLOT} />
+ ))}
{assetDrop &&
(assetDrop.ghost ? (
<GhostClip ghost={assetDrop.ghost} t={assetDrop.t} len={assetDrop.len} h={rowH0 - 4} pps={pps} />
@@ -2626,7 +2637,7 @@ export function Timeline() {
audioLanes.count,
// An in-flight audio drag aimed under the band opens the
// row it will land on.
- laneDrag?.kind === "audio" ? laneDrag.targetRow + 1 : 0,
+ lastRow(audioSlots) + 1,
(audioDrop?.row ?? -1) + 1
) *
AUDIO_H +
@@ -2638,9 +2649,8 @@ export function Timeline() {
{Array.from(
{
length:
- Math.max(audioLanes.count, (audioDrop?.row ?? -1) + 1) +
- (audioTopShift ? 1 : 0) +
- (laneDrag?.kind === "audio" && laneDrag.targetRow === audioLanes.count ? 1 : 0),
+ Math.max(audioLanes.count, (audioDrop?.row ?? -1) + 1, lastRow(audioSlots) + 1) +
+ audioTopShift / AUDIO_H,
},
(_, r) => laneRail((r + 1) * AUDIO_H - 2, r)
)}
@@ -2665,15 +2675,16 @@ export function Timeline() {
}}
/>
)}
- {laneDrag?.kind === "audio" && (
+ {audioSlots.map((d) => (
<LaneSlot
- drag={laneDrag}
+ key={d.id}
+ drag={d}
pps={pps}
- top={laneDrag.targetRow * AUDIO_H + 2 + audioTopShift}
+ top={d.targetRow * AUDIO_H + 2 + audioTopShift}
barH={AUDIO_H - 4}
className="rounded-[2.5px] bg-emerald-500/10 shadow-[inset_0_0_0_1.5px_rgba(16,185,129,0.5)]"
/>
- )}
+ ))}
{audioClips.map((a) => {
const homeRow = audioLanes.rowOf.get(a.lane ?? 0) ?? 0;
const drag = laneDragFor(laneDrag, "audio", a.id);
@@ -2717,15 +2728,16 @@ export function Timeline() {
{[...new Set(subtitles.cues.map((c) => c.lane ?? 0))].map((lane) =>
laneRail((lane + 1) * SUB_H - 3, lane)
)}
- {laneDrag?.kind === "cue" && (
+ {cueSlots.map((d) => (
<LaneSlot
- drag={laneDrag}
+ key={d.id}
+ drag={d}
pps={pps}
- top={laneDrag.targetRow * SUB_H + 1}
+ top={d.targetRow * SUB_H + 1}
barH={SUB_H - 4}
className="rounded-[2.5px] bg-amber-400/15 shadow-[inset_0_0_0_1.5px_rgba(245,158,11,0.55)]"
/>
- )}
+ ))}
{subtitles.cues.map((c) => (
<SubBar
key={c.id}
diff --git a/site/src/cut/lib/laneTracks.ts b/site/src/cut/lib/laneTracks.ts
index e8df4081..48aec0fa 100644
--- a/site/src/cut/lib/laneTracks.ts
+++ b/site/src/cut/lib/laneTracks.ts
@@ -486,8 +486,19 @@ export interface LaneDrag {
slotStart: number; // resolved landing start, seconds
len: number; // dragged item length, seconds
/** The rest of a group drag's set: each member rides the pointer as its own
- * ghost, shifted by the same delta as the grabbed item. */
- members?: { kind: LaneKind; id: string; ghostX: number }[];
+ * ghost, shifted by the same delta as the grabbed item, and aims at its own
+ * landing row and slot. */
+ members?: LaneDragMember[];
+}
+
+export interface LaneDragMember {
+ kind: LaneKind;
+ id: string;
+ ghostX: number;
+ ghostY: number;
+ targetRow: number;
+ slotStart: number;
+ len: number;
}
/** The drag a bar renders with: the carried item's own LaneDrag, or — when
@@ -497,7 +508,16 @@ export function laneDragFor(d: LaneDrag | null, kind: LaneKind, id: string): Lan
if (!d) return null;
if (d.kind === kind && d.id === id) return d;
const m = d.members?.find((x) => x.kind === kind && x.id === id);
- return m ? { ...d, kind, id, ghostX: m.ghostX } : null;
+ return m ? { ...d, ...m } : null;
+}
+
+/** Every landing slot a drag draws in one band: the carried item's and each
+ * member's of that kind, so a group drag shows where the whole set lands. */
+export function laneDragSlots(d: LaneDrag | null, kind: LaneKind): LaneDrag[] {
+ if (!d) return [];
+ const out: LaneDrag[] = d.kind === kind ? [d] : [];
+ for (const m of d.members ?? []) if (m.kind === kind) out.push({ ...d, ...m });
+ return out;
}
/** True when a live drag moves items on this kind's lanes and this bar is
@@ -635,27 +655,34 @@ function startGroupMove(
? laneOrder(grabbed.kind, s, [...ADAPTERS[grabbed.kind].raws(s).map((r) => ADAPTERS[grabbed.kind].view(r).lane)])
: [];
const rowOf = (lane: number) => usedLanes.indexOf(lane);
- // A row past either end opens a brand-new track, and a selection that holds
- // an outermost row by itself has nothing to open on that side: the row it
- // leaves collapses behind it and the fresh one renumbers straight back to
- // where the picture already was — see `startLaneMove`, which draws the same
- // line for a single item.
- const rowsHeld = new Set(members.map((m) => rowOf(m.lane)));
+ const homeRow = vertical ? rowOf(grabbed.lane) : ui.homeRow;
+ // The set reorders across the band the way one item does: the grabbed
+ // member reaches the row past either end and the rest ride beyond it, each
+ // opening the row it needs. A side is open when something outside the set
+ // sits past a member on that side; with nothing there the move would
+ // collapse straight back to the picture already on screen.
+ const rowsHeld = members.map((m) => rowOf(m.lane));
const heldIds = new Set(members.map((m) => m.id));
- const others = vertical
+ const otherRows = vertical
? ADAPTERS[grabbed.kind]
.raws(s)
.map((r) => ADAPTERS[grabbed.kind].view(r))
.filter((v) => !heldIds.has(v.id))
+ .map((v) => rowOf(v.lane))
: [];
- const holdsRowAlone = (row: number) =>
- rowsHeld.has(row) && !others.some((v) => rowOf(v.lane) === row);
- const edges = { top: !holdsRowAlone(0), bottom: !holdsRowAlone(usedLanes.length - 1) };
- const rowLo = vertical ? (edges.top ? -1 : 0) - Math.min(...rowsHeld) : 0;
- const rowHi = vertical
- ? usedLanes.length - (edges.bottom ? 0 : 1) - Math.max(...rowsHeld)
- : 0;
- const homeRow = vertical ? rowOf(grabbed.lane) : ui.homeRow;
+ const edges = {
+ top: otherRows.some((r) => r < Math.max(...rowsHeld)),
+ bottom: otherRows.some((r) => r > Math.min(...rowsHeld)),
+ };
+ const rowLo = vertical && edges.top ? -1 - homeRow : 0;
+ const rowHi = vertical && edges.bottom ? usedLanes.length - homeRow : 0;
+ // Where each member rests in its own band, for the slot it draws while a
+ // mixed set (which only moves in time) is on the move.
+ const restRow = (m: GroupMember) => {
+ if (vertical) return rowOf(m.lane);
+ const ad = ADAPTERS[m.kind];
+ return ad.multiLane ? laneOrder(m.kind, s, ad.raws(s).map((r) => ad.view(r).lane)).indexOf(m.lane) : 0;
+ };
const scroller = (e.currentTarget as HTMLElement).closest<HTMLElement>("[data-tl-scroll]");
const sc0 = scroller?.scrollLeft ?? 0;
@@ -748,9 +775,10 @@ function startGroupMove(
ui.onSnap(guide);
layout(dt);
// Ghosts ride the raw pointer delta, clamped so the set's earliest
- // member holds at 0 — the same rigid floor the landing uses.
+ // member holds at 0 — the same rigid floor the landing uses. A set that
+ // only moves in time keeps every bar in its row.
const dtGhost = Math.max(-minStart, effDx / ui.pps);
- const ghostY = dy - (homeTop(rows) - rowTop0);
+ const ghostY = vertical ? dy - (homeTop(rows) - rowTop0) : 0;
ui.onDrag({
kind: grabbed.kind,
id: grabbed.id,
@@ -763,6 +791,10 @@ function startGroupMove(
kind: m.kind,
id: m.id,
ghostX: (m.start + dtGhost) * ui.pps,
+ ghostY,
+ targetRow: restRow(m) + rowDelta,
+ slotStart: m.start + dt,
+ len: m.len,
})),
});
},