Skip to content

Open source CapCut alternative.

TypeScript

feat(site): timeline rows drag to a new order

David Le7a59a82c8eb0
diff --git a/site/src/cut/components/OverlayLayer.tsx b/site/src/cut/components/OverlayLayer.tsx
index 6136c6f1..536d02c4 100644
--- a/site/src/cut/components/OverlayLayer.tsx
+++ b/site/src/cut/components/OverlayLayer.tsx
@@ -148,8 +148,15 @@ export function OverlayLayer({
captions?: boolean;
}) {
const allOverlays = useEditor((s) => s.overlays);
+ // Lane 0 is the top row, so the deepest lane paints first and lane 0 lands
+ // over it. These boxes stack by DOM order alone; the document's array order
+ // is the order elements were made, which is not the stack the rows show and
+ // not the one the export draws.
const overlays = useMemo(
- () => allOverlays.filter((o) => laneOf(o) >= from && laneOf(o) < to),
+ () =>
+ allOverlays
+ .filter((o) => laneOf(o) >= from && laneOf(o) < to)
+ .sort((a, b) => laneOf(b) - laneOf(a)),
[allOverlays, from, to]
);
const selection = useEditor((s) => s.selection);
@@ -274,15 +281,6 @@ export function OverlayLayer({
className="pointer-events-none absolute inset-0"
style={{ transform, filter }}
>
- {captions && (
- <SubtitleCaptions
- stageWidth={stageWidth}
- stageHeight={stageHeight}
- registerBox={registerBox}
- snap={snap}
- onSnapEnd={clearGuides}
- />
- )}
{overlays.map((o) => {
if (o.hidden) return null;
// An effect has no place in the frame — it grades what plays under it
@@ -313,6 +311,15 @@ export function OverlayLayer({
/>
);
})}
+ {captions && (
+ <SubtitleCaptions
+ stageWidth={stageWidth}
+ stageHeight={stageHeight}
+ registerBox={registerBox}
+ snap={snap}
+ onSnapEnd={clearGuides}
+ />
+ )}
{guides.v.map((x, i) => (
<div
key={`v${i}`}
diff --git a/site/src/cut/components/Timeline.tools.ts b/site/src/cut/components/Timeline.tools.ts
index 729fae06..4fab3985 100644
--- a/site/src/cut/components/Timeline.tools.ts
+++ b/site/src/cut/components/Timeline.tools.ts
@@ -94,6 +94,16 @@ export const TIMELINE_TOOLS = [
"Mute or unmute every clip on one video track at once — the track header's speaker toggle. The picture keeps playing.",
inputSchema: obj({ track: num("Video track index, 0-based"), muted: bool("true to mute") }, ["track", "muted"]),
},
+ {
+ name: "reorder_track",
+ description:
+ "Move a whole timeline row to another place in its own band — the gutter drag. Rows only ever reorder among their own kind: video tracks restack (row 0 is the bottom track, the spine that carries ripple and transitions; higher rows composite in front), element rows and soundtrack lanes renumber from there. Everything on the row travels with it and keeps its times.",
+ inputSchema: obj({
+ kind: { type: "string", enum: ["video", "soundtrack", "text"], description: "Which band the row belongs to" },
+ from: num("The row's current index, 0-based"),
+ to: num("The index it should end up at, 0-based"),
+ }, ["kind", "from", "to"]),
+ },
{
name: "delete_item",
description:
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index 47fa9ef4..da4cfed4 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -88,6 +88,48 @@ const OVERLAY_H = VIDEO_H; // every video track shares the same row height
* and the next row keeps its usual distance below the line. */
const KEYRAIL_EXTRA = 20;
const AUDIO_H = 44;
+/** The `mt-1.5` every row carries, so a row drag knows what one row's worth of
+ * travel is. */
+const ROW_GAP = 6;
+
+/** The three bands whose rows reorder: the video stack, the element rows, and
+ * the soundtrack lanes. Subtitle rows are language tracks, managed in the
+ * panel, so they stay put. */
+type RowBand = "video" | "overlay" | "audio";
+
+/** A row being dragged to a new place in its own band. Rows are display
+ * indices, 0 = the band's top row. The boxes are the band's rows measured in
+ * screen space at the grab: the rows move while the drag runs, so hit-testing
+ * against where they started is what keeps the target from flickering. */
+interface RowDrag {
+ band: RowBand;
+ from: number;
+ to: number;
+ /** Pointer travel, clamped so the carried row stays inside its band. */
+ dy: number;
+ rows: { top: number; h: number }[];
+ /** The viewport the landing outline draws across. */
+ left: number;
+ width: number;
+}
+
+/** Where row `r` shows while `d` is in flight: the carried row is at the
+ * target, and the rows it passed close up behind it. */
+function shownRow(d: RowDrag, r: number): number {
+ if (r === d.from) return d.to;
+ if (d.from < d.to) return r > d.from && r <= d.to ? r - 1 : r;
+ return r >= d.to && r < d.from ? r + 1 : r;
+}
+
+/** The box the carried row will land in, in screen space. */
+function landingBox(d: RowDrag): { top: number; h: number } {
+ const held = d.rows[d.from];
+ const seat = d.rows[d.to];
+ return {
+ top: d.to > d.from ? seat.top + seat.h - held.h : seat.top,
+ h: held.h,
+ };
+}
/** Where a dragged video clip can land. Re-exported name for the store's
* placement union (existing track, 0 included / newly-inserted track). */
@@ -451,7 +493,7 @@ export function Timeline() {
// the rows live in the scrolled content, but the toggles are pinned in the
// gutter, so they need the rows' content-space tops.
const [gutterYs, setGutterYs] = useState<{
- video: { track: number; y: number }[];
+ video: { track: number; y: number; h: number }[];
audioTop: number | null;
textTop: number | null;
subTop: number | null;
@@ -467,6 +509,7 @@ export function Timeline() {
const video = Array.from(el.querySelectorAll<HTMLElement>("[data-tl-vrow]"), (r) => ({
track: Number(r.dataset.tlVrow),
y: Math.round(r.getBoundingClientRect().top - box.top + el.scrollTop),
+ h: Math.round(r.getBoundingClientRect().height),
}));
const audioTop = contentY("[data-tl-arows]");
const textTop = contentY("[data-tl-trows]");
@@ -476,7 +519,9 @@ export function Timeline() {
prev.textTop === textTop &&
prev.subTop === subTop &&
prev.video.length === video.length &&
- prev.video.every((v, i) => v.track === video[i].track && v.y === video[i].y)
+ prev.video.every(
+ (v, i) => v.track === video[i].track && v.y === video[i].y && v.h === video[i].h
+ )
? prev
: { video, audioTop, textTop, subTop }
);
@@ -846,6 +891,91 @@ export function Timeline() {
// a toggle whose track has anything off stays visible on its own.
const [trackHover, setTrackHover] = useState(false);
+ // A whole row on its way to a new place in its band. The gutter is the
+ // handle: a press there grabs the row it sits on, and the drag stays inside
+ // that band — a row is only ever above or below its own kind, so there is
+ // nowhere else for it to go.
+ const [rowDrag, setRowDrag] = useState<RowDrag | null>(null);
+ // The band's rows in screen space, top row first. The video stack measures
+ // its own row elements (their heights vary with the mask key rail); the
+ // stacked bands are uniform, so their rows come off the band's top edge.
+ const rowBoxes = useCallback(
+ (band: RowBand): { top: number; h: number }[] => {
+ if (band === "video")
+ return Array.from(
+ innerRef.current?.querySelectorAll<HTMLElement>("[data-tl-vrow]") ?? [],
+ (el) => {
+ const r = el.getBoundingClientRect();
+ return { top: r.top, h: r.height };
+ }
+ );
+ const el = band === "overlay" ? overlayRef.current : audioRef.current;
+ if (!el) return [];
+ const h = band === "overlay" ? TEXT_H : AUDIO_H;
+ const n = band === "overlay" ? overlayLanes.count : audioLanes.count;
+ const top = el.getBoundingClientRect().top;
+ return Array.from({ length: n }, (_, r) => ({ top: top + r * h, h }));
+ },
+ [overlayLanes.count, audioLanes.count]
+ );
+ // Grab a row by its gutter cell and carry it to a new place in the band.
+ const gripRow = (band: RowBand, row: number) => (e: React.PointerEvent) => {
+ const rows = rowBoxes(band);
+ if (e.button !== 0 || rows.length < 2 || row >= rows.length) return;
+ const held = rows[row];
+ const first = rows[0];
+ const last = rows[rows.length - 1];
+ // The carried row travels between the band's own edges and no further.
+ const floor = first.top - held.top;
+ const ceil = last.top + last.h - (held.top + held.h);
+ let to = row;
+ setRowDrag({ band, from: row, to, dy: 0, rows, left: 0, width: 0 });
+ const view = scrollRef.current?.getBoundingClientRect();
+ const left = view?.left ?? 0;
+ const width = view?.width ?? 0;
+ startDrag(e, {
+ cursor: () => "grabbing",
+ onMove: (_dx, dy) => {
+ const travel = Math.max(floor, Math.min(ceil, dy));
+ const mid = held.top + held.h / 2 + travel;
+ to = rows.length - 1;
+ for (let i = 0; i < rows.length; i++)
+ if (mid < rows[i].top + rows[i].h) {
+ to = i;
+ break;
+ }
+ setRowDrag({ band, from: row, to, dy: travel, rows, left, width });
+ },
+ onUp: () => {
+ setRowDrag(null);
+ if (to === row) return;
+ const s = useEditor.getState();
+ // The video stack draws top-down but numbers bottom-up, so its rows
+ // reach the store flipped.
+ if (band === "video") s.moveVideoTrack(rows.length - 1 - row, rows.length - 1 - to);
+ else if (band === "overlay") s.moveOverlayLane(row, to);
+ else s.moveAudioLane(row, to);
+ },
+ });
+ };
+ /** A stacked band's row top while a row drag runs: the carried row rides the
+ * pointer, the rest sit at the place they are closing up into. */
+ const stackedRowTop = (band: "overlay" | "audio", row: number, h: number) =>
+ rowDrag?.band !== band
+ ? row * h
+ : row === rowDrag.from
+ ? row * h + rowDrag.dy
+ : shownRow(rowDrag, row) * h;
+ /** The same for the video stack, where rows are laid out in flow and shift
+ * by a transform instead. */
+ const videoRowShift = (row: number) => {
+ if (rowDrag?.band !== "video") return 0;
+ const step = rowDrag.rows[rowDrag.from].h + ROW_GAP;
+ if (row === rowDrag.from) return rowDrag.dy;
+ if (rowDrag.from < rowDrag.to) return row > rowDrag.from && row <= rowDrag.to ? -step : 0;
+ return row >= rowDrag.to && row < rowDrag.from ? step : 0;
+ };
+
// Chat mention token per audio clip ("@s1"), keyed by clip id — the same
// handles the chat resolves against, so the token shown on hover is exactly
// what pulls this sound into a message.
@@ -1929,6 +2059,19 @@ export function Timeline() {
style={marquee}
/>
)}
+ {/* Where the carried row will sit once it lands. The rows have already
+ closed up around the slot; the outline says it is the one. */}
+ {rowDrag && rowDrag.to !== rowDrag.from && (
+ <div
+ className="pointer-events-none fixed z-50 rounded-md border border-dashed border-[#0a84ff]/70 bg-[#0a84ff]/5"
+ style={{
+ top: landingBox(rowDrag).top,
+ height: landingBox(rowDrag).h,
+ left: rowDrag.left,
+ width: rowDrag.width,
+ }}
+ />
+ )}
<div
ref={scrollRef}
data-tl-scroll
@@ -2007,7 +2150,7 @@ export function Timeline() {
{gapHighlight(
{ kind: "overlay", index: lane },
TEXT_H - 6,
- r * TEXT_H + topRowShift
+ stackedRowTop("overlay", r, TEXT_H) + topRowShift
)}
</Fragment>
))}
@@ -2131,8 +2274,15 @@ export function Timeline() {
key={o.id}
overlay={o}
pps={pps}
- top={homeRow * TEXT_H + topRowShift}
+ top={stackedRowTop("overlay", homeRow, TEXT_H) + topRowShift}
homeRow={homeRow}
+ rowMove={
+ rowDrag?.band === "overlay"
+ ? homeRow === rowDrag.from
+ ? "lift"
+ : "part"
+ : null
+ }
laneCount={overlayLanes.count}
selected={selKeys.has(`overlay:${o.id}`)}
drag={drag}
@@ -2239,13 +2389,21 @@ export function Timeline() {
{/* A drag carried above the stack opens a new top layer: the row
grows in here and folds away when the drag comes back down. */}
{newTrackRow(topInsertLevel)}
- {aboveTracks.map((track) => {
+ {aboveTracks.map((track, row) => {
const railH = railFor(overlayTrackSpans.get(track) ?? []);
+ const lifted = rowDrag?.band === "video" && rowDrag.from === row;
return (
<div
key={`ov-${track}`}
- className="relative mt-1.5"
- style={{ height: OVERLAY_H + railH }}
+ className={cn(
+ "relative mt-1.5",
+ rowDrag?.band === "video" && !lifted && "transition-transform duration-150 ease-out",
+ lifted && "pointer-events-none z-20 opacity-90 drop-shadow-2xl"
+ )}
+ style={{
+ height: OVERLAY_H + railH,
+ transform: videoRowShift(row) ? `translateY(${videoRowShift(row)}px)` : undefined,
+ }}
data-tl-vrow={track}
onPointerDown={deselectIfSelf}
onContextMenu={openGapMenu({ kind: "video", index: track })}
@@ -2303,8 +2461,19 @@ export function Timeline() {
dropType === "video" ||
samePlacement(overlayDrop?.target ?? null, TRACK_ZERO)) && (
<div
- className="relative mt-1.5"
- style={{ height: VIDEO_H + rail0 }}
+ className={cn(
+ "relative mt-1.5",
+ rowDrag?.band === "video" &&
+ (rowDrag.from === aboveTracks.length
+ ? "pointer-events-none z-20 opacity-90 drop-shadow-2xl"
+ : "transition-transform duration-150 ease-out")
+ )}
+ style={{
+ height: VIDEO_H + rail0,
+ transform: videoRowShift(aboveTracks.length)
+ ? `translateY(${videoRowShift(aboveTracks.length)}px)`
+ : undefined,
+ }}
data-tl-vrow={0}
onPointerDown={deselectIfSelf}
onContextMenu={openGapMenu({ kind: "video", index: 0 })}
@@ -2376,7 +2545,11 @@ export function Timeline() {
)}
{audioLanes.used.map((lane, r) => (
<Fragment key={`agap-${lane}`}>
- {gapHighlight({ kind: "audio", index: lane }, AUDIO_H - 4, r * AUDIO_H)}
+ {gapHighlight(
+ { kind: "audio", index: lane },
+ AUDIO_H - 4,
+ stackedRowTop("audio", r, AUDIO_H)
+ )}
</Fragment>
))}
{laneDrag?.kind === "audio" &&
@@ -2422,8 +2595,15 @@ export function Timeline() {
asset={assets.find((x) => x.id === a.assetId)}
mention={audioMentions.get(a.id)}
pps={pps}
- top={homeRow * AUDIO_H}
+ top={stackedRowTop("audio", homeRow, AUDIO_H)}
homeRow={homeRow}
+ rowMove={
+ rowDrag?.band === "audio"
+ ? homeRow === rowDrag.from
+ ? "lift"
+ : "part"
+ : null
+ }
laneCount={audioLanes.count}
selected={selKeys.has(`audio:${a.id}`)}
drag={drag}
@@ -2543,7 +2723,42 @@ export function Timeline() {
style={{ width: PAD_SIDE, top: RULER_H }}
>
<div ref={gutterCtlRef} className="absolute inset-x-0 bottom-0" style={{ top: -RULER_H }}>
- {gutterYs.video.map(({ track, y }) => {
+ {/* Each row's gutter cell is the handle for the row itself: press and
+ carry it to another place in its own band. Drawn before the
+ toggles so an eye or a speaker still takes its own press. */}
+ {gutterYs.video.map(({ track, y, h }, row) => (
+ <RowGrip
+ key={`vgrip-${track}`}
+ top={y}
+ height={h}
+ barH={VIDEO_H - 4}
+ enabled={gutterYs.video.length > 1}
+ onGrab={gripRow("video", row)}
+ />
+ ))}
+ {gutterYs.textTop !== null &&
+ overlayLanes.used.map((lane, r) => (
+ <RowGrip
+ key={`tgrip-${lane}`}
+ top={gutterYs.textTop! + stackedRowTop("overlay", r, TEXT_H)}
+ height={TEXT_H}
+ barH={TEXT_H - 6}
+ enabled={overlayLanes.count > 1}
+ onGrab={gripRow("overlay", r)}
+ />
+ ))}
+ {gutterYs.audioTop !== null &&
+ audioLanes.used.map((lane, r) => (
+ <RowGrip
+ key={`agrip-${lane}`}
+ top={gutterYs.audioTop! + stackedRowTop("audio", r, AUDIO_H)}
+ height={AUDIO_H}
+ barH={AUDIO_H - 4}
+ enabled={audioLanes.count > 1}
+ onGrab={gripRow("audio", r)}
+ />
+ ))}
+ {gutterYs.video.map(({ track, y }, row) => {
const t = trackState.get(track);
if (!t) return null;
const rows: React.ReactNode[] = [
@@ -2554,6 +2769,7 @@ export function Timeline() {
partial={t.hidden > 0}
reveal={trackHover}
top={y + (t.sound > 0 ? VIDEO_H / 2 - 22 : VIDEO_H / 2 - 8)}
+ onGrab={gripRow("video", row)}
onToggle={() =>
useEditor.getState().setTrackHidden(track, t.hidden !== t.total)
}
@@ -2568,6 +2784,7 @@ export function Timeline() {
partial={t.muted > 0}
reveal={trackHover}
top={y + VIDEO_H / 2 + 6}
+ onGrab={gripRow("video", row)}
onToggle={() =>
useEditor.getState().setTrackMuted(track, t.muted !== t.sound)
}
@@ -2586,7 +2803,8 @@ export function Timeline() {
off={t.hidden === t.total}
partial={t.hidden > 0}
reveal={trackHover}
- top={gutterYs.audioTop! + r * AUDIO_H + AUDIO_H / 2 - 8}
+ top={gutterYs.audioTop! + stackedRowTop("audio", r, AUDIO_H) + AUDIO_H / 2 - 8}
+ onGrab={gripRow("audio", r)}
onToggle={() =>
useEditor.getState().setAudioLaneHidden(lane, t.hidden !== t.total)
}
@@ -2604,7 +2822,8 @@ export function Timeline() {
off={t.hidden === t.total}
partial={t.hidden > 0}
reveal={trackHover}
- top={gutterYs.textTop! + r * TEXT_H + TEXT_H / 2 - 8}
+ top={gutterYs.textTop! + stackedRowTop("overlay", r, TEXT_H) + TEXT_H / 2 - 8}
+ onGrab={gripRow("overlay", r)}
onToggle={() =>
useEditor.getState().setTextLaneHidden(lane, t.hidden !== t.total)
}
@@ -3801,6 +4020,41 @@ function Filmstrip({
* full-strength "whole track". A click always turns the whole track off, and
* the next click turns every segment back on — individually disabled segments
* included. */
+/** One row's gutter cell, and the handle for the whole row: pressing it picks
+ * the row up and carries it to another place in its own band. A band with a
+ * single row has no reorder in it, so the cell stays out of the way and the
+ * gutter scrubs there as it always did. */
+function RowGrip({
+ top,
+ height,
+ barH,
+ enabled,
+ onGrab,
+}: {
+ top: number;
+ height: number;
+ /** The band's bar height, so the hover surface lines up with the bars on
+ * the row instead of the row box they sit inside. */
+ barH: number;
+ enabled: boolean;
+ onGrab: (e: React.PointerEvent) => void;
+}) {
+ if (!enabled) return null;
+ return (
+ <div
+ title="Drag to reorder the row"
+ className="group pointer-events-auto absolute inset-x-0 cursor-grab"
+ style={{ top, height }}
+ onPointerDown={onGrab}
+ >
+ <span
+ className="pointer-events-none absolute inset-x-0 bg-foreground/10 opacity-0 transition-opacity group-hover:opacity-100"
+ style={{ top: 2, height: barH }}
+ />
+ </div>
+ );
+}
+
function GutterToggle({
kind,
off,
@@ -3808,6 +4062,7 @@ function GutterToggle({
reveal,
top,
onToggle,
+ onGrab,
}: {
kind: "hide" | "mute";
off: boolean;
@@ -3815,6 +4070,9 @@ function GutterToggle({
reveal: boolean;
top: number;
onToggle: () => void;
+ /** Grab the row this toggle rides. A press that travels is a row drag; one
+ * that does not is a click on the toggle and nothing more. */
+ onGrab?: (e: React.PointerEvent) => void;
}) {
const active = off || partial;
const title =
@@ -3843,7 +4101,27 @@ function GutterToggle({
: "pointer-events-none opacity-0"
)}
style={{ top }}
- onPointerDown={(e) => e.stopPropagation()}
+ onPointerDown={(e) => {
+ e.stopPropagation();
+ if (!onGrab || e.button !== 0) return;
+ const x = e.clientX;
+ const y = e.clientY;
+ const drop = () => {
+ window.removeEventListener("pointermove", travel);
+ window.removeEventListener("pointerup", drop);
+ window.removeEventListener("pointercancel", drop);
+ };
+ // Once the press has travelled it belongs to the row, so the gesture
+ // hands over from the press's own point and the click never lands.
+ const travel = (ev: PointerEvent) => {
+ if (Math.abs(ev.clientX - x) <= 3 && Math.abs(ev.clientY - y) <= 3) return;
+ drop();
+ onGrab(e);
+ };
+ window.addEventListener("pointermove", travel);
+ window.addEventListener("pointerup", drop);
+ window.addEventListener("pointercancel", drop);
+ }}
onClick={onToggle}
>
<Icon className="size-3" />
@@ -3937,6 +4215,7 @@ function AudioView({
selected,
drag,
parting,
+ rowMove,
onDrag,
onSnap,
}: {
@@ -3955,6 +4234,9 @@ function AudioView({
drag: LaneDrag | null;
/** Another sound is dragging: animate this bar's shifts as it parts. */
parting: boolean;
+ /** A row drag is running in this band: "lift" is the row being carried,
+ * "part" a row sliding into the place it closes up into. */
+ rowMove: "lift" | "part" | null;
/** Publish (or clear) the in-flight drag so the slot and rows track it. */
onDrag: (d: LaneDrag | null) => void;
/** Paint (or clear) the snap guide at this stage-x pixel. */
@@ -4002,7 +4284,10 @@ function AudioView({
clip.hidden && "opacity-40 grayscale",
drag
? "tl-audio-ghost pointer-events-none cursor-grabbing opacity-80 shadow-2xl"
- : parting && "transition-[left] duration-150 ease-out"
+ : parting && "transition-[left] duration-150 ease-out",
+ rowMove === "lift"
+ ? "pointer-events-none opacity-80 shadow-2xl"
+ : rowMove === "part" && "transition-[top] duration-150 ease-out"
)}
style={{
left: drag ? drag.ghostX : clip.start * pps,
@@ -4010,7 +4295,7 @@ function AudioView({
width: Math.max(10, w - CLIP_GAP),
height: AUDIO_H - 4,
// Inline so it beats SELECTED_SHADOW's z-10 class on the same element.
- zIndex: drag ? 20 : undefined,
+ zIndex: drag || rowMove === "lift" ? 20 : undefined,
}}
data-tl-sel={`audio:${clip.id}`}
onPointerDown={(e) => startLaneMove(e, "audio", clip.id, ui)}
@@ -4204,6 +4489,7 @@ function TextBar({
selected,
drag,
parting,
+ rowMove,
onDrag,
onSnap,
onMenu,
@@ -4214,6 +4500,9 @@ function TextBar({
homeRow: number;
laneCount: number;
selected: boolean;
+ /** A row drag is running in this band: "lift" is the row being carried,
+ * "part" a row sliding into the place it closes up into. */
+ rowMove: "lift" | "part" | null;
/** This bar's live drag when it is the one being carried (ghost mode). */
drag: LaneDrag | null;
/** Another title is dragging: animate this bar's shifts as it parts. */
@@ -4261,7 +4550,10 @@ function TextBar({
selected && SELECTED_SHADOW,
drag
? "tl-text-ghost pointer-events-none cursor-grabbing opacity-80 shadow-2xl"
- : parting && "transition-[left] duration-150 ease-out"
+ : parting && "transition-[left] duration-150 ease-out",
+ rowMove === "lift"
+ ? "pointer-events-none opacity-80 shadow-2xl"
+ : rowMove === "part" && "transition-[top] duration-150 ease-out"
)}
style={{
left: drag ? drag.ghostX : o.start * pps,
@@ -4269,7 +4561,7 @@ function TextBar({
width: Math.max(8, w - CLIP_GAP),
height: TEXT_H - 6,
// Inline so it beats SELECTED_SHADOW's z-10 class on the same element.
- zIndex: drag ? 20 : undefined,
+ zIndex: drag || rowMove === "lift" ? 20 : undefined,
}}
data-tl-sel={`overlay:${o.id}`}
onPointerDown={(e) => startLaneMove(e, "overlay", o.id, ui)}
diff --git a/site/src/cut/lib/aiTools.ts b/site/src/cut/lib/aiTools.ts
index 943f20a5..2077a8b7 100644
--- a/site/src/cut/lib/aiTools.ts
+++ b/site/src/cut/lib/aiTools.ts
@@ -72,7 +72,7 @@ import { blobToInlineAudio, refToInlineAudio, visualRefs, type InlineImage } fro
import { characterPrompt, stockAspectDims, stockTitle } from "./stock";
import { STOCK_IMAGES } from "./stockManifest";
import { STOCK_VIDEOS } from "./stockVideoManifest";
-import { applyOverlayPatchSettled, track0Clips, laneGapAt, getClipSpans, nextFreeStart, overlayLayers, parkedTransitions, projectDuration, resolveTransitions, totalDuration, useEditor } from "./store";
+import { applyOverlayPatchSettled, track0Clips, laneGapAt, getClipSpans, nextFreeStart, overlayLaneOrder, overlayLayers, parkedTransitions, projectDuration, resolveTransitions, totalDuration, useEditor } from "./store";
import { playheadAt } from "./playhead";
import { renderProjectFrame } from "./exportRender";
import { rasterCanvasToDataUrl } from "./raster";
@@ -1197,6 +1197,32 @@ const toolRuns: Record<BrowserToolName, ToolRun> = {
return { id: clip.id, hidden: Boolean(input.hidden) };
},
+ reorder_track: (s, input) => {
+ if (!isNum(input.from) || !isNum(input.to) || input.from < 0 || input.to < 0)
+ throw new ToolError("from and to must be 0 or higher.");
+ const from = Math.round(input.from);
+ const to = Math.round(input.to);
+ const kind = String(input.kind);
+ const rows =
+ kind === "video"
+ ? [...new Set(s.clips.map((c) => c.track))].sort((a, b) => a - b)
+ : kind === "soundtrack"
+ ? [...new Set(s.audioClips.map((a) => a.lane ?? 0))].sort((a, b) => a - b)
+ : kind === "text"
+ ? overlayLaneOrder(s.overlays)
+ : null;
+ if (!rows) throw new ToolError("kind must be video, soundtrack, or text.");
+ if (rows.length < 2) throw new ToolError(`There is only one ${kind} row, so nothing to reorder.`);
+ const i = rows.indexOf(from);
+ if (i < 0) throw new ToolError(`No ${kind} row ${from} — rows ${rows.join(", ")} exist.`);
+ const j = Math.min(rows.length - 1, rows.indexOf(to) >= 0 ? rows.indexOf(to) : to);
+ if (i === j) return { kind, from: i, to: j, rows: rows.length, moved: false };
+ if (kind === "video") s.moveVideoTrack(i, j);
+ else if (kind === "soundtrack") s.moveAudioLane(i, j);
+ else s.moveOverlayLane(i, j);
+ return { kind, from: i, to: j, rows: rows.length, moved: true };
+ },
+
set_track_hidden: (s, input) => {
if (!isNum(input.track) || input.track < 0)
throw new ToolError("track must be 0 or higher.");
diff --git a/site/src/cut/lib/store.test.ts b/site/src/cut/lib/store.test.ts
index f0cc5b96..53c3cfc3 100644
--- a/site/src/cut/lib/store.test.ts
+++ b/site/src/cut/lib/store.test.ts
@@ -411,6 +411,61 @@ describe("audio lanes", () => {
});
});
+describe("row reordering", () => {
+ test("a video track carried down the stack renumbers the ones it passes", () => {
+ const bottom = vclip({ track: 0 });
+ const mid = vclip({ track: 1 });
+ const top = vclip({ track: 2 });
+ useEditor.setState({ clips: [bottom, mid, top] });
+ s().moveVideoTrack(2, 0); // the frontmost layer becomes the spine
+ expect(clipById(top.id).track).toBe(0);
+ expect(clipById(bottom.id).track).toBe(1);
+ expect(clipById(mid.id).track).toBe(2);
+ });
+
+ test("reordering closes track number holes", () => {
+ const a = vclip({ track: 0 });
+ const b = vclip({ track: 3 });
+ useEditor.setState({ clips: [a, b] });
+ s().moveVideoTrack(0, 1);
+ expect(clipById(b.id).track).toBe(0);
+ expect(clipById(a.id).track).toBe(1);
+ });
+
+ test("an element row lands where it was carried, times untouched", () => {
+ const t0 = title({ start: 1, end: 2, lane: 0 });
+ const t1 = title({ start: 0, end: 3, lane: 1 });
+ const t2 = title({ start: 4, end: 5, lane: 2 });
+ useEditor.setState({ overlays: [t0, t1, t2] });
+ s().moveOverlayLane(0, 2); // the top row goes to the bottom
+ const by = (id: string) => s().overlays.find((o) => o.id === id)!;
+ expect(by(t1.id).lane).toBe(0);
+ expect(by(t2.id).lane).toBe(1);
+ expect(by(t0.id).lane).toBe(2);
+ expect(by(t0.id).start).toBeCloseTo(1);
+ });
+
+ test("a soundtrack lane move takes every segment on the row", () => {
+ const a1 = aclip({ start: 0, out: 2 });
+ const a2 = aclip({ start: 4, out: 2 });
+ const b1 = aclip({ start: 0, out: 2, lane: 1 });
+ useEditor.setState({ audioClips: [a1, a2, b1] });
+ s().moveAudioLane(1, 0);
+ expect(audioById(b1.id).lane ?? 0).toBe(0);
+ expect(audioById(a1.id).lane).toBe(1);
+ expect(audioById(a2.id).lane).toBe(1);
+ });
+
+ test("a move to the row it already sits on changes nothing", () => {
+ const t0 = title({ lane: 0 });
+ const t1 = title({ lane: 1 });
+ useEditor.setState({ overlays: [t0, t1] });
+ const before = s().overlays;
+ s().moveOverlayLane(1, 1);
+ expect(s().overlays).toBe(before);
+ });
+});
+
describe("title lanes", () => {
const overlayById = (id: string) => s().overlays.find((o) => o.id === id)!;
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index 6f24d2f3..c9d25edf 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -103,6 +103,17 @@ function openInsertSlot(clips: VideoClip[], level: number, exclude?: string): Vi
const shiftTracksUp = (clips: VideoClip[], place: VideoTrackPlacement): VideoClip[] =>
place.kind === "insert" ? openInsertSlot(clips, place.level) : clips;
+/** The renumbering a row move produces: the row at index `from` lifts out of
+ * `used` and drops at `to`, and the result maps every old row key to its new
+ * index. Null when the move is a no-op or points off the end, so callers can
+ * skip the undo step entirely. */
+function rowRenumber(used: number[], from: number, to: number): Map<number, number> | null {
+ if (from === to || from < 0 || to < 0 || from >= used.length || to >= used.length) return null;
+ const next = [...used];
+ next.splice(to, 0, next.splice(from, 1)[0]);
+ return new Map(next.map((row, i) => [row, i]));
+}
+
/** A single-item selection: the primary that drives the Inspector plus the
* one-element multiSelection that bulk actions (delete, copy) and the timeline
* highlight read. Every mutation that selects its own result funnels through
@@ -391,6 +402,15 @@ export interface EditorState {
setAudioLaneHidden: (lane: number, hidden: boolean) => void;
/** Hide or show every title on one title lane, in one undo step. */
setTextLaneHidden: (lane: number, hidden: boolean) => void;
+ /** Reorder the occupied video tracks: the track sitting at z-index `from`
+ * (0 = the bottom track) lands at `to` and the rest close up around it.
+ * Tracks renumber to 0..n-1, so whatever lands at 0 becomes the spine. */
+ moveVideoTrack: (from: number, to: number) => void;
+ /** Reorder the element rows: the row at display index `from` (0 = the top
+ * row) lands at `to`. Lanes renumber to 0..n-1 in the new order. */
+ moveOverlayLane: (from: number, to: number) => void;
+ /** Reorder the soundtrack lanes the same way, top row first. */
+ moveAudioLane: (from: number, to: number) => void;
updateOverlay: (id: string, patch: OverlayPatch) => void;
/** Live-drag updates that should not create undo entries. */
updateOverlayTransient: (id: string, patch: OverlayPatch) => void;
@@ -2373,6 +2393,49 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
push();
get().updateOverlaysTransient(ids.map((id) => ({ id, patch: { hidden } })));
},
+
+ moveVideoTrack: (from, to) => {
+ const s = get();
+ const map = rowRenumber([...new Set(s.clips.map((c) => c.track))].sort((a, b) => a - b), from, to);
+ if (!map) return;
+ push();
+ s.updateClipsTransient(
+ s.clips
+ .filter((c) => map.get(c.track) !== c.track)
+ .map((c) => ({ id: c.id, patch: { track: map.get(c.track)! } }))
+ );
+ },
+
+ moveOverlayLane: (from, to) => {
+ const s = get();
+ const map = rowRenumber(overlayLaneOrder(s.overlays), from, to);
+ if (!map) return;
+ push();
+ s.updateOverlaysTransient(
+ s.overlays
+ .filter((o) => map.get(o.lane ?? 0) !== (o.lane ?? 0))
+ .map((o) => ({ id: o.id, patch: { lane: map.get(o.lane ?? 0)! } }))
+ );
+ },
+
+ moveAudioLane: (from, to) => {
+ const s = get();
+ const map = rowRenumber(
+ [...new Set(s.audioClips.map((a) => a.lane ?? 0))].sort((a, b) => a - b),
+ from,
+ to
+ );
+ if (!map) return;
+ push();
+ s.updateAudiosTransient(
+ s.audioClips
+ .filter((a) => map.get(a.lane ?? 0) !== (a.lane ?? 0))
+ .map((a) => {
+ const lane = map.get(a.lane ?? 0)!;
+ return { id: a.id, patch: { lane: lane > 0 ? lane : undefined } };
+ })
+ );
+ },
updateOverlay: (id, patch) => {
push();
applyOverlayPatchSettled(id, patch);