Skip to content

Open source CapCut alternative.

TypeScript

feat(site): every sound drop lands by the lane drag's own rule

David Leab2dc5640031
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index 7a1a3427..433462b5 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -21,7 +21,7 @@ import {
renderPreviewProxy,
type ExportDoc,
} from "@/cut/lib/exportClient";
-import { EMPTY_LIBRARY, fileZoneAt, hasRefDrag, parseMentions, selectionRefTokens, useRefCandidates } from "@/cut/lib/assetRef";
+import { EMPTY_LIBRARY, fileZoneAt, hasRefDrag, parseMentions, refCandidatesOf, selectionRefTokens } from "@/cut/lib/assetRef";
import { placeRefAtPlayhead } from "@/cut/lib/refPlace";
import { useLibrary } from "@/cut/lib/queries";
import { copyableRefs } from "@/cut/lib/refCopy";
@@ -65,6 +65,7 @@ import {
} from "@/cut/lib/shortcutGate";
import { docAudioClips, docClips, docOverlays, projectDuration, serializeDoc, storedAssets, useEditor, clipLen, type VideoTrackPlacement } from "@/cut/lib/store";
import { fileLandingAt } from "@/cut/lib/timelineDrop";
+import { landOnRow } from "@/cut/lib/laneTracks";
import { playheadAt, previewAt, skimAt } from "@/cut/lib/playhead";
import type { MediaAsset } from "@/cut/lib/types";
import { AiPanel } from "./AiPanel";
@@ -761,6 +762,10 @@ export function Editor({
* `at`, rippling the row's later clips right, each file after the
* last; without it the file takes track 0's next free slot. */
place?: VideoTrackPlacement;
+ /** The audio display row a timeline drop resolved to, one past
+ * either edge for a new lane. Sound lands there at `at`, each file
+ * after the last; without it the file takes lane 0. */
+ audioRow?: number;
/** Media folder the import files into — the one open in the Media
* panel when the upload started. */
folderId?: string;
@@ -771,6 +776,7 @@ export function Editor({
// opened by the first and the rest join it.
let at = opts?.at;
let place = opts?.place;
+ let audioRow = opts?.audioRow;
// The keyboard comes back to the editor. An import is started from a
// panel control — an Upload button, a library tile, a file input the OS
// dialog hands focus back to — and whichever one it was keeps focus
@@ -823,9 +829,23 @@ export function Editor({
// footage.
s.addClipFromAsset(asset.id, at);
}
+ } else if (audioRow !== undefined && at !== undefined) {
+ // A timeline drop lands on the row it was released over, the
+ // way every sound drag lands, each file after the last.
+ const from = at;
+ landOnRow("audio", audioRow, (lane) =>
+ useEditor.getState().addAudioFromAsset(asset.id, from, { lane })
+ );
+ const next = useEditor.getState();
+ const placed = next.audioClips.find((c) => c.assetId === asset.id && c.start >= from - 1e-6);
+ if (placed) {
+ at = placed.start + clipLen(placed);
+ const used = [...new Set(next.audioClips.map((c) => c.lane ?? 0))].sort((a, b) => a - b);
+ audioRow = used.indexOf(placed.lane ?? 0);
+ }
} else {
- // A timeline drop lands at the pointer; an upload drops at the
- // playhead (the store slides it right only if that spot is taken).
+ // An upload drops at the playhead (the store slides it right
+ // only if that spot is taken).
s.addAudioFromAsset(asset.id, at);
}
}
@@ -923,7 +943,11 @@ export function Editor({
// clip floating at the pointer time reads as broken.
const s = useEditor.getState();
const empty = s.clips.length === 0 && s.audioClips.length === 0;
- void importFiles(e.dataTransfer.files, { at: empty ? 0 : landing.at, place: landing.place });
+ void importFiles(e.dataTransfer.files, {
+ at: empty ? 0 : landing.at,
+ place: landing.place,
+ audioRow: landing.audioRow,
+ });
return;
}
void importFiles(e.dataTransfer.files, { mediaOnly: !overCanvas(e) });
@@ -967,36 +991,19 @@ export function Editor({
// frame copied off the preview, or a timeline selection. A file that Cut
// imports lands under the indicator, on the first row with room for it. A
// composer marks its own paste handled, so this only ever sees the pastes
- // nobody else wanted. The keystroke arms a fallback for a browser that
- // fires no paste event at all; the event, when it comes, disarms it.
- // What a mention token on the clipboard can name — the same candidates the
- // chat composer resolves against — read at the paste, so a tile copied a
- // moment ago resolves against the live project and shelf.
- const candidates = useRefCandidates(!viewer);
+ // nobody else wanted. The keystroke keeps its default so the event fires.
+ // The shelf listing a pasted mention token can name, read at the paste. The
+ // rest of the candidates come off the store at that moment too: nothing
+ // here subscribes to the timeline, so a drag on a clip never re-renders
+ // the editor for a paste that may never come.
const library = useLibrary({ enabled: !viewer }).data ?? EMPTY_LIBRARY;
- const mentionable = useRef({ candidates, library });
+ const libraryRef = useRef(library);
useEffect(() => {
- mentionable.current = { candidates, library };
- }, [candidates, library]);
- const pasteFallback = useRef<ReturnType<typeof setTimeout> | null>(null);
- const disarmPasteFallback = useCallback(() => {
- if (pasteFallback.current === null) return;
- clearTimeout(pasteFallback.current);
- pasteFallback.current = null;
- }, []);
- const armPasteFallback = useCallback(() => {
- disarmPasteFallback();
- const at = Math.max(0, previewAt());
- pasteFallback.current = setTimeout(() => {
- pasteFallback.current = null;
- pasteOwn(at);
- }, 0);
- }, [disarmPasteFallback, pasteOwn]);
- useEffect(() => disarmPasteFallback, [disarmPasteFallback]);
+ libraryRef.current = library;
+ }, [library]);
useEffect(() => {
if (viewer) return;
const onPaste = (e: ClipboardEvent) => {
- disarmPasteFallback();
const s = useEditor.getState();
if (e.defaultPrevented || s.readOnly) return;
// A dialog on top owns the paste, the same way it owns the shortcuts.
@@ -1026,7 +1033,8 @@ export function Editor({
// sticker, an effect, a shape, a transition, a template — put its
// mention token here; each one lands the way the tile's + button
// lands it. Imports run behind the editor; a run lands in copy order.
- const { candidates, library } = mentionable.current;
+ const library = libraryRef.current;
+ const candidates = refCandidatesOf(s, library);
const refs = parseMentions(e.clipboardData?.getData("text/plain") ?? "", candidates).refs;
const projectId = s.projectId;
if (refs.length === 0 || !projectId) return;
@@ -1062,7 +1070,7 @@ export function Editor({
};
window.addEventListener("paste", onPaste);
return () => window.removeEventListener("paste", onPaste);
- }, [disarmPasteFallback, importFiles, pasteOwn, viewer]);
+ }, [importFiles, pasteOwn, viewer]);
// Keyboard shortcuts.
useEffect(() => {
@@ -1146,10 +1154,6 @@ export function Editor({
e.preventDefault();
}
}
- } else if (mod && e.key.toLowerCase() === "v") {
- // The keystroke keeps its default: the paste event it raises is where
- // the editor decides what lands (see the paste listener above).
- armPasteFallback();
} else if (mod && e.key.toLowerCase() === "g") {
// ⌘G groups the multi-selected elements, ⇧⌘G dissolves the primary's
// group — the panel's Group button is out of reach while a
@@ -1191,7 +1195,7 @@ export function Editor({
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
- }, [armPasteFallback, importFiles]);
+ }, [importFiles]);
// The project is here, on this Mac — it just can't be opened without the app
// that holds it. The gate's banner sits above this with the recovery steps,
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index 34c24dc4..9a3db2dc 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -17,12 +17,9 @@ import { Skeleton } from "@/components/ui/skeleton";
import {
clearAssetDrag,
clearElementDrag,
- draggedAssetId,
- draggedLibraryId,
draggingAssetId,
draggingAssetIds,
draggingElement,
- draggingLibrary,
draggingLibraryMany,
draggingTemplate,
hasAssetDrag,
@@ -30,7 +27,7 @@ import {
hasLibraryDrag,
hasTemplateDrag,
} from "@/cut/lib/assetDrag";
-import { registerFileLanding } from "@/cut/lib/timelineDrop";
+import { registerFileLanding, registerTransitionLanding } from "@/cut/lib/timelineDrop";
import { audioClipRefs, clipRefs, draggingRef, hasRefDrag, refFromAsset, type AssetRef } from "@/cut/lib/assetRef";
import { sendFrameToChat, type FrameGrabOrigin } from "@/cut/lib/chatIntake";
import { copyRefImage } from "@/cut/lib/refMedia";
@@ -49,7 +46,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, laneDragFor, laneDragParts, resolveRow, startLaneMove, startLaneTrim, type LaneDrag, type RowBox } from "@/cut/lib/laneTracks";
+import { CLIP_GAP, landOnRow, laneDragFor, laneDragParts, 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";
@@ -411,31 +408,126 @@ const draggedSticker = (): MediaAsset | null => {
return stickerOf(id ? useEditor.getState().assets.find((a) => a.id === id) : null);
};
-/** The image ref being dragged (a stock tile), null for any other drag —
- * asset and library drags carry the ref MIME too but with video/audio kinds.
- * On the timeline it lands on video track 0 as a still image. */
-function draggingStill(e: React.DragEvent): AssetRef | null {
- if (!hasRefDrag(e)) return null;
- const ref = draggingRef();
- return ref?.kind === "image" ? ref : null;
-}
-
-/** The stock-clip ref being dragged (a stock video tile), null for any other
- * drag — project and library videos carry their own MIMEs and are handled
- * first. On the timeline it imports into the project and lands as footage. */
-function draggingStockVideo(e: React.DragEvent): AssetRef | null {
- if (!hasRefDrag(e)) return null;
- const ref = draggingRef();
- return ref?.scope === "stock" && ref.kind === "video" ? ref : null;
+/** One item a media drag carries, whatever tile it came from: a project
+ * asset, a library card, a stock video, sound or image. Every item lands the
+ * same way once it is in the project; only how it gets there differs, and
+ * `land` is that difference. */
+interface CarriedMedia {
+ kind: "video" | "audio" | "image";
+ name: string;
+ /** How much timeline the item takes when it lands. */
+ duration: number;
+ /** The project asset for this item — already there, or imported now. */
+ land: (projectId: string) => Promise<MediaAsset>;
+ /** What a video landing segment paints while it is carried; sound and
+ * stickers paint none. */
+ ghost?: DropGhost;
}
-/** The stock audio ref being dragged — a music sample or a sound effect card —
- * null otherwise. On the soundtrack it imports into the project and lands as
- * an audio clip. */
-function draggingStockAudio(e: React.DragEvent): AssetRef | null {
- if (!hasRefDrag(e)) return null;
+/** Everything a drag carries, the grabbed item first. A single card is a run
+ * of one, so every media drop takes the same path; a font, which has no
+ * place on the timeline, is left out. */
+function carriedMedia(e: React.DragEvent): CarriedMedia[] {
+ if (hasLibraryDrag(e)) {
+ return draggingLibraryMany().flatMap((lib) =>
+ lib.type === "font"
+ ? []
+ : [
+ {
+ kind: lib.type,
+ name: lib.title || lib.name,
+ duration: lib.type === "image" ? STILL_SECONDS : lib.duration,
+ land: (projectId: string) => importLibraryAsset(projectId, lib),
+ ...(isClipMedia(lib.type)
+ ? {
+ ghost: {
+ url: libraryMediaUrl(lib.fileName, lib.residency),
+ kind: lib.type,
+ name: lib.title || lib.name,
+ duration: lib.duration,
+ width: lib.width,
+ height: lib.height,
+ // The card's cover is already painted and cached, so
+ // the segment has a frame of the video in it from the
+ // first move — true frames replace it tile by tile as
+ // the reads land.
+ poster: libraryPosterUrl(lib),
+ },
+ }
+ : {}),
+ },
+ ]
+ );
+ }
+ if (hasAssetDrag(e)) {
+ const assets = useEditor.getState().assets;
+ return draggingAssetIds().flatMap((id) => {
+ const asset = assets.find((a) => a.id === id);
+ if (!asset || asset.type === "font") return [];
+ return [
+ {
+ kind: asset.type,
+ name: asset.name,
+ duration: asset.type === "image" ? STILL_SECONDS : asset.duration,
+ land: async () => asset,
+ ...(isClipMedia(asset.type) && !stickerOf(asset) ? { ghost: { asset } } : {}),
+ },
+ ];
+ });
+ }
+ // A stock tile carries only the unified ref: a sound, a clip, or a still.
+ if (!hasRefDrag(e)) return [];
const ref = draggingRef();
- return ref?.scope === "stock" && ref.kind === "audio" ? ref : null;
+ if (!ref) return [];
+ if (ref.scope === "stock" && ref.kind === "audio") {
+ return [
+ {
+ kind: "audio",
+ name: ref.name,
+ duration: ref.duration ?? 0,
+ land: (projectId) =>
+ importStockAudio(projectId, { url: ref.url, name: ref.name, duration: ref.duration }),
+ },
+ ];
+ }
+ if (ref.scope === "stock" && ref.kind === "video") {
+ return [
+ {
+ kind: "video",
+ name: ref.name,
+ duration: ref.duration ?? 0,
+ land: (projectId) =>
+ importStockVideo(projectId, {
+ url: ref.url,
+ name: ref.name,
+ duration: ref.duration,
+ width: ref.width,
+ height: ref.height,
+ }),
+ ghost: {
+ url: ref.url,
+ kind: "video",
+ name: ref.name,
+ duration: ref.duration ?? 0,
+ width: ref.width,
+ height: ref.height,
+ poster: ref.thumb,
+ },
+ },
+ ];
+ }
+ if (ref.kind === "image") {
+ return [
+ {
+ kind: "image",
+ name: ref.name,
+ duration: STILL_SECONDS,
+ land: (projectId) => importImage(projectId, ref),
+ ghost: { url: ref.url, kind: "image", name: ref.name, duration: STILL_SECONDS },
+ },
+ ];
+ }
+ return [];
}
export function Timeline() {
@@ -1054,16 +1146,15 @@ export function Timeline() {
return map;
}, [clips, assets]);
- // The audio row under a screen y, one past the last row = a new track.
- // Before any audio exists there are no rows, so everything resolves to 0.
+ // The audio row under a screen y, by the resolver a lane drag aims with:
+ // past either edge of the band, beyond the same reach, is a new lane there
+ // (-1 above, one past the last below), and the row already aimed at holds
+ // while the band shifts under the pointer. Before any audio exists there
+ // are no rows, so everything resolves to 0.
const audioRowAt = useCallback(
- (clientY: number): number => {
- const el = audioRef.current;
- if (!el) return 0;
- const top = el.getBoundingClientRect().top;
- return Math.min(audioLanes.count, Math.max(0, Math.floor((clientY - top) / AUDIO_H)));
- },
- [audioLanes.count]
+ (clientY: number): number =>
+ resolveRow(audioRowBoxes(), clientY, audioDrop?.row ?? null, { top: true, bottom: true }),
+ [audioRowBoxes, audioDrop]
);
// The element row under the pointer, or undefined for a point outside the
@@ -1213,6 +1304,15 @@ export function Timeline() {
st.endHistoryBatch();
st.select({ kind: "transition", id });
};
+ // A pasted transition lands through the same closure a dropped one does.
+ const dropTransitionRef = useRef(dropTransitionAt);
+ useEffect(() => {
+ dropTransitionRef.current = dropTransitionAt;
+ });
+ useEffect(
+ () => registerTransitionLanding((time, style) => dropTransitionRef.current(time, style)),
+ []
+ );
// Drag a bar anywhere along the row. An anchor within reach pulls the drop
// onto itself and lights the room it takes; released anywhere else the bar
@@ -1363,7 +1463,7 @@ export function Timeline() {
// drop through `registerFileLanding`. Stashed by this surface's own drop,
// which runs first; a release the surface never saw resolves from the
// geometry at the moment of asking.
- const fileLanding = useRef<{ at: number; place: TrackTarget } | null>(null);
+ const fileLanding = useRef<{ at: number; place: TrackTarget; audioRow: number } | null>(null);
const footerRef = useRef<HTMLElement>(null);
useEffect(
() =>
@@ -1377,17 +1477,18 @@ export function Timeline() {
if (!surface) return null;
const r = surface.getBoundingClientRect();
if (y < r.top || y > r.bottom || x < r.left || x > r.right) return null;
- return { at: dropTimeAt(x), place: resolveDropTrack(y) };
+ return { at: dropTimeAt(x), place: resolveDropTrack(y), audioRow: audioRowAt(y) };
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
- [resolveDropTrack]
+ [resolveDropTrack, audioRowAt]
);
// A transition bar mid-drag, drawn where the pointer has it.
const [transitionDrag, setTransitionDrag] = useState<XBar | null>(null);
// 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 ? TEXT_H : 0;
- const audioTopShift = laneDrag?.kind === "audio" && laneDrag.targetRow < 0 ? AUDIO_H : 0;
+ const audioTopShift =
+ (laneDrag?.kind === "audio" && laneDrag.targetRow < 0) || audioDrop?.row === -1 ? AUDIO_H : 0;
// Where a dropped asset should land. An empty timeline has no arrangement to
// read a position against, so the drop starts the film at 0 no matter where
@@ -1621,10 +1722,9 @@ export function Timeline() {
// drop into a leading gap or between clips lands there.
s.addVideoFromAsset(assetId, place, t);
} else {
- const used = [...new Set(s.audioClips.map((a) => a.lane ?? 0))].sort((a, b) => a - b);
- const lane =
- audioRow < used.length ? used[audioRow] : (used[used.length - 1] ?? -1) + 1;
- s.addAudioFromAsset(assetId, t, { lane });
+ landOnRow("audio", audioRow, (lane) =>
+ useEditor.getState().addAudioFromAsset(assetId, t, { lane })
+ );
}
};
@@ -1646,51 +1746,34 @@ export function Timeline() {
};
};
- /** Lay a whole dragged group down in order, each one after the last. A
- * single card is a group of one, so every drop takes this path. `only`
- * narrows the group to what the row under the pointer accepts. */
- const placeAssetsAt = (
- ids: string[],
- t: number,
- atElement: number,
- audioRow = 0,
- place: TrackTarget = TRACK_ZERO,
- elementLane?: number,
- only?: (asset: MediaAsset) => boolean
- ) => {
- const next = runCursor(t, atElement);
- for (const id of ids) {
- const asset = useEditor.getState().assets.find((a) => a.id === id);
- if (!asset || asset.type === "font") continue;
- if (only && !only(asset)) continue;
- const sticker = !!stickerOf(asset);
- placeAssetAt(id, asset.type, next(asset, sticker), audioRow, place, elementLane);
- }
- };
-
- /** Copy a dragged group of library assets into the project, in order, then
- * lay them down. Each import waits for the last so the run keeps the order
- * they were selected in. */
- const placeLibraryAt = async (
+ /** Lay a carried run down in order, each item after the last — a single
+ * card is a run of one, so every media drop takes this path. Each item is
+ * landed in the project first (a copy, a download, or nothing at all), and
+ * an item that fails leaves the rest of the run alone. `only` narrows the
+ * run to what the row under the pointer accepts. */
+ const placeCarriedAt = async (
projectId: string,
- items: LibraryAsset[],
+ items: CarriedMedia[],
t: number,
atElement: number,
audioRow = 0,
place: TrackTarget = TRACK_ZERO,
+ elementLane?: number,
only?: (asset: MediaAsset) => boolean
) => {
const next = runCursor(t, atElement);
for (const item of items) {
try {
- const asset = await importLibraryAsset(projectId, item);
+ const asset = await item.land(projectId);
+ // The landing outlived a project switch: the asset belongs to the
+ // project it was landed in.
+ if (useEditor.getState().projectId !== projectId) return;
if (asset.type === "font") continue;
if (only && !only(asset)) continue;
const sticker = !!stickerOf(asset);
- placeAssetAt(asset.id, asset.type, next(asset, sticker), audioRow, place);
+ placeAssetAt(asset.id, asset.type, next(asset, sticker), audioRow, place, elementLane);
} catch (err) {
- // A copy that fails leaves the rest of the run alone.
- reportSwallowed(`[cut] library drop failed for ${item.fileName}`, err);
+ reportSwallowed(`[cut] drop failed for ${item.name}`, err);
}
}
};
@@ -1699,59 +1782,12 @@ export function Timeline() {
* mixed selection may also be carrying. */
const clipsOnly = (asset: MediaAsset) => isClipMedia(asset.type) && !stickerOf(asset);
- // The video being dragged — project media, a library clip, or an image ref
- // (which lands as a still) — with the ghost its landing segment paints.
+ // The video the drag carries — project media, a library clip, a stock clip
+ // or an image (which lands as a still) — with the ghost its landing
+ // segment paints. A sound or a sticker is no video.
const draggedVideo = (e: React.DragEvent): { duration: number; ghost: DropGhost } | null => {
- if (hasLibraryDrag(e)) {
- const lib = draggingLibrary();
- if (!lib || !isClipMedia(lib.type)) return null;
- return {
- duration: lib.type === "image" ? STILL_SECONDS : lib.duration,
- ghost: {
- url: libraryMediaUrl(lib.fileName, lib.residency),
- kind: lib.type,
- name: lib.title || lib.name,
- duration: lib.duration,
- width: lib.width,
- height: lib.height,
- // The card's cover is already painted and cached, so the segment
- // has a frame of the video in it from the first move — true frames
- // replace it tile by tile as the reads land.
- poster: libraryPosterUrl(lib),
- },
- };
- }
- const id = draggingAssetId();
- if (id) {
- const asset = useEditor.getState().assets.find((a) => a.id === id);
- if (!asset || stickerOf(asset) || !isClipMedia(asset.type)) return null;
- return {
- duration: asset.type === "image" ? STILL_SECONDS : asset.duration,
- ghost: { asset },
- };
- }
- const stockVideo = draggingStockVideo(e);
- if (stockVideo) {
- return {
- duration: stockVideo.duration ?? 0,
- ghost: {
- url: stockVideo.url,
- kind: "video",
- name: stockVideo.name,
- duration: stockVideo.duration ?? 0,
- width: stockVideo.width,
- height: stockVideo.height,
- poster: stockVideo.thumb,
- },
- };
- }
- const still = draggingStill(e);
- return still
- ? {
- duration: STILL_SECONDS,
- ghost: { url: still.url, kind: "image", name: still.name, duration: STILL_SECONDS },
- }
- : null;
+ const first = carriedMedia(e)[0];
+ return first?.ghost ? { duration: first.duration, ghost: first.ghost } : null;
};
// Drop targets for the upper tracks and between-track seams: dragging a
@@ -1792,42 +1828,13 @@ export function Timeline() {
const t = Math.max(0, timeAt(e.clientX));
endCrossDrag();
setDropType(null);
- const lib = draggingLibrary();
- const libId = draggedLibraryId(e);
- // The whole dragged group, read before the drag is cleared.
- const libGroup = draggingLibraryMany();
- const assetGroup = draggingAssetIds();
- const still = draggingStill(e);
- const stockVideo = draggingStockVideo(e);
+ // The whole carried run, read before the drag is cleared; the row
+ // takes the clips in it and leaves the rest.
+ const carried = carriedMedia(e).filter((item) => isClipMedia(item.kind));
const projectId = useEditor.getState().projectId;
clearAssetDrag();
- if (libId && lib && isClipMedia(lib.type) && projectId) {
- void placeLibraryAt(projectId, libGroup, t, t, 0, place, clipsOnly);
- return;
- }
- const id = draggedAssetId(e);
- const asset = id ? useEditor.getState().assets.find((a) => a.id === id) : null;
- if (id && !stickerOf(asset) && isClipMedia(asset?.type)) {
- placeAssetsAt(assetGroup, t, t, 0, place, undefined, clipsOnly);
- return;
- }
- if (stockVideo && projectId) {
- void importStockVideo(projectId, {
- url: stockVideo.url,
- name: stockVideo.name,
- duration: stockVideo.duration,
- width: stockVideo.width,
- height: stockVideo.height,
- })
- .then((vid) => useEditor.getState().addVideoFromAsset(vid.id, place, t))
- .catch((err: unknown) => reportSwallowed("[cut] stock video drop failed", err));
- return;
- }
- if (still && projectId) {
- void importImage(projectId, still)
- .then((img) => useEditor.getState().addVideoFromAsset(img.id, place, t))
- .catch((err: unknown) => reportSwallowed("[cut] image drop failed", err));
- }
+ if (carried.length > 0 && projectId)
+ void placeCarriedAt(projectId, carried, t, t, 0, place, undefined, clipsOnly);
},
};
@@ -1974,13 +1981,9 @@ export function Timeline() {
});
return;
}
- const isLib = hasLibraryDrag(e);
- const still = draggingStill(e);
- const stockVideo = draggingStockVideo(e);
- const stockAudio = draggingStockAudio(e);
+ const carried = carriedMedia(e);
const element = hasElementDrag(e) ? draggingElement() : null;
- if (!hasAssetDrag(e) && !isLib && !still && !stockVideo && !stockAudio && !element)
- return;
+ if (carried.length === 0 && !element) return;
e.preventDefault();
e.dataTransfer.dropEffect = "copy";
// What the event carries is read now — the drag data is only readable
@@ -2028,29 +2031,11 @@ export function Timeline() {
});
return;
}
- // Preview where a video would land; audio drops free-form. Library and
- // stock drags carry their own shape since they aren't in the project yet.
- let type: "video" | "audio" | "image" | undefined;
- let duration = 0;
- if (isLib) {
- const lib = draggingLibrary();
- type = lib && lib.type !== "font" ? lib.type : undefined;
- duration = lib?.duration ?? 0;
- } else if (stockAudio) {
- type = "audio";
- duration = stockAudio.duration ?? 0;
- } else if (stockVideo) {
- type = "video";
- duration = stockVideo.duration ?? 0;
- } else if (still) {
- type = "video";
- duration = STILL_SECONDS;
- } else {
- const id = draggingAssetId();
- const asset = id ? useEditor.getState().assets.find((a) => a.id === id) : null;
- type = asset && asset.type !== "font" ? asset.type : undefined;
- duration = asset?.type === "image" ? STILL_SECONDS : asset?.duration ?? 0;
- }
+ // Preview where the grabbed item would land: a video on its row, a
+ // sound free-form on its lane. Every tile carries the same shape,
+ // whether or not the item is in the project yet.
+ const type = carried[0]?.kind;
+ const duration = carried[0]?.duration ?? 0;
// What the ghost paints: the source's frames, from wherever it lives.
const ghost = draggedVideo(e)?.ghost;
schedulePreview(() => {
@@ -2108,6 +2093,7 @@ export function Timeline() {
fileLanding.current = {
at: dropTimeAt(e.clientX),
place: resolveDropTrack(e.clientY),
+ audioRow: audioRowAt(e.clientY),
};
}
// Resolve the hovered rows before the previews (and their rows) clear.
@@ -2123,16 +2109,9 @@ export function Timeline() {
setDropType(null);
const t = dropTimeAt(e.clientX);
- // A library asset must be copied into the project before it can land.
- const lib = draggingLibrary();
- const libId = draggedLibraryId(e);
// Everything the drag is carrying — one card, or the whole marquee
// selection it was grabbed from — read before the drag is cleared.
- const libGroup = draggingLibraryMany();
- const assetGroup = draggingAssetIds();
- const still = draggingStill(e);
- const stockVideo = draggingStockVideo(e);
- const stockAudio = draggingStockAudio(e);
+ const carried = carriedMedia(e);
const tpl = draggingTemplate();
const element = hasElementDrag(e) ? draggingElement() : null;
const projectId = useEditor.getState().projectId;
@@ -2162,53 +2141,9 @@ export function Timeline() {
);
return;
}
- if (libId && lib && projectId) {
- e.preventDefault();
- void placeLibraryAt(projectId, libGroup, t, atElement, audioRow, videoPlace);
- return;
- }
-
- const id = draggedAssetId(e);
- if (id) {
- e.preventDefault();
- placeAssetsAt(assetGroup, t, atElement, audioRow, videoPlace, elementLane);
- return;
- }
-
- // A stock music sample or sound effect imports as an audio asset and
- // lands on the hovered soundtrack lane.
- if (stockAudio && projectId) {
- e.preventDefault();
- void importStockAudio(projectId, {
- url: stockAudio.url,
- name: stockAudio.name,
- duration: stockAudio.duration,
- })
- .then((asset) => placeAssetAt(asset.id, "audio", t, audioRow))
- .catch((err: unknown) => reportSwallowed("[cut] stock audio drop failed", err));
- return;
- }
-
- // A stock clip imports as footage, an image ref as a still, then each
- // lands in the resolved video slot.
- if (stockVideo && projectId) {
- e.preventDefault();
- void importStockVideo(projectId, {
- url: stockVideo.url,
- name: stockVideo.name,
- duration: stockVideo.duration,
- width: stockVideo.width,
- height: stockVideo.height,
- })
- .then((asset) => placeAssetAt(asset.id, "video", t, 0, videoPlace))
- .catch((err: unknown) => reportSwallowed("[cut] stock video drop failed", err));
- return;
- }
- if (still && projectId) {
+ if (carried.length > 0 && projectId) {
e.preventDefault();
- void importImage(projectId, still)
- .then((asset) => placeAssetAt(asset.id, "image", t, 0, videoPlace))
- .catch((err: unknown) => reportSwallowed("[cut] image drop failed", err));
+ void placeCarriedAt(projectId, carried, t, atElement, audioRow, videoPlace, elementLane);
}
}}
>
@@ -2829,7 +2764,7 @@ export function Timeline() {
className="tl-audio-drop-slot pointer-events-none absolute rounded-[2.5px] border-[1.5px] border-dashed border-emerald-500/80 bg-emerald-500/10 transition-[left] duration-150 ease-out"
style={{
left: audioDrop.t * pps,
- top: audioDrop.row * AUDIO_H + 2,
+ top: audioDrop.row * AUDIO_H + 2 + audioTopShift,
width: Math.max(10, audioDrop.len * pps - CLIP_GAP),
height: AUDIO_H - 4,
}}
diff --git a/site/src/cut/lib/assetRef.ts b/site/src/cut/lib/assetRef.ts
index d176cd66..8ce4ed5e 100644
--- a/site/src/cut/lib/assetRef.ts
+++ b/site/src/cut/lib/assetRef.ts
@@ -25,8 +25,7 @@ import {
TRANSITION_STYLE_IDS,
TRANSITION_STYLE_LABELS,
type ShapeKind,
- type TransitionStyle,
-} from "./types";
+ type TransitionStyle, type LibraryTemplate } from "./types";
import type {
AudioClip,
MediaAsset,
@@ -889,34 +888,44 @@ export function useRefCandidates(enabled = true): AssetRef[] {
return useMemo(() => {
if (!assets || !clips || !audioClips || !overlays || !transitions || !subtitles || !projectTemplates) return [];
- const project = projectRefs(assets);
- const seen = new Set<string>();
- const out: AssetRef[] = [];
- for (const ref of [
- ...clipRefs(clips, assets),
- ...audioClipRefs(audioClips, assets),
- ...entityRefs({ clips, assets, overlays, transitions, subtitles }),
- ...project,
- ...projectTemplates.map(refFromTemplate),
- // A font is used from the font menu, so there is nothing to point a tool
- // at; library fonts stay out of the candidates the way project fonts do.
- ...lib.assets.filter((a) => a.type !== "font").map(refFromLibrary),
- ...lib.templates.map(refFromTemplate),
- ...STOCK_IMAGES.map(refFromStock),
- ...STOCK_VIDEOS.map(refFromStockVideo),
- ...STOCK_MUSIC.map(refFromStockMusic),
- ...STOCK_SFX.map(refFromStockSfx),
- ...catalogRefs(),
- ]) {
- const key = ref.name.toLowerCase();
- if (seen.has(key)) continue;
- seen.add(key);
- out.push(ref);
- }
- return out;
+ return refCandidatesOf({ assets, clips, audioClips, overlays, transitions, subtitles, templates: projectTemplates }, lib);
}, [assets, clips, audioClips, overlays, transitions, subtitles, projectTemplates, lib]);
}
+/** The candidates for one state and one shelf listing — what the hook memoizes,
+ * for a reader that wants them once, at an event, without subscribing. */
+export function refCandidatesOf(
+ s: EntitySources & { audioClips: AudioClip[]; templates: LibraryTemplate[] },
+ lib: LibraryData
+): AssetRef[] {
+ const { assets, clips, audioClips, overlays, transitions, subtitles, templates } = s;
+ const project = projectRefs(assets);
+ const seen = new Set<string>();
+ const out: AssetRef[] = [];
+ for (const ref of [
+ ...clipRefs(clips, assets),
+ ...audioClipRefs(audioClips, assets),
+ ...entityRefs({ clips, assets, overlays, transitions, subtitles }),
+ ...project,
+ ...templates.map(refFromTemplate),
+ // A font is used from the font menu, so there is nothing to point a tool
+ // at; library fonts stay out of the candidates the way project fonts do.
+ ...lib.assets.filter((a) => a.type !== "font").map(refFromLibrary),
+ ...lib.templates.map(refFromTemplate),
+ ...STOCK_IMAGES.map(refFromStock),
+ ...STOCK_VIDEOS.map(refFromStockVideo),
+ ...STOCK_MUSIC.map(refFromStockMusic),
+ ...STOCK_SFX.map(refFromStockSfx),
+ ...catalogRefs(),
+ ]) {
+ const key = ref.name.toLowerCase();
+ if (seen.has(key)) continue;
+ seen.add(key);
+ out.push(ref);
+ }
+ return out;
+}
+
/** The prompt token for an asset name: `@name`, quoted when it has spaces. */
export const mentionToken = (name: string) =>
/[\s"]/.test(name) ? `@"${name.replace(/"/g, "")}"` : `@${name}`;
diff --git a/site/src/cut/lib/laneTracks.test.ts b/site/src/cut/lib/laneTracks.test.ts
index e636806b..d66d92cb 100644
--- a/site/src/cut/lib/laneTracks.test.ts
+++ b/site/src/cut/lib/laneTracks.test.ts
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, test } from "bun:test";
-import { commitRow, NEW_ROW_PX, partAround, resolveRow } from "./laneTracks";
+import { commitRow, landOnRow, NEW_ROW_PX, partAround, resolveRow } from "./laneTracks";
import { clipLen, useEditor } from "./store";
import { emptySubtitles } from "./types";
import type { AudioClip, VideoClip } from "./types";
@@ -156,6 +156,52 @@ describe("commitRow on the video stack", () => {
});
});
+describe("landOnRow on the audio lanes", () => {
+ // What the store's add does with the lane it is handed: 0 is stored as none.
+ const land = (_row: number, lane: number) => {
+ const c = aclip({ start: 5, ...(lane > 0 ? { lane } : {}) });
+ useEditor.setState({ audioClips: [...s().audioClips, c] });
+ return c.id;
+ };
+
+ test("a row in use lands on that lane", () => {
+ const bed = aclip({});
+ const fx = aclip({ lane: 1 });
+ useEditor.setState({ audioClips: [bed, fx] });
+ let id = "";
+ landOnRow("audio", 1, (lane) => (id = land(1, lane)));
+ expect(audioById(id).lane).toBe(1);
+ expect(audioById(bed.id).lane).toBeUndefined();
+ });
+
+ test("the row past the top opens a new first lane, the way a drag does", () => {
+ const bed = aclip({});
+ const fx = aclip({ lane: 1 });
+ useEditor.setState({ audioClips: [bed, fx] });
+ let id = "";
+ landOnRow("audio", -1, (lane) => (id = land(-1, lane)));
+ expect(audioById(id).lane).toBeUndefined();
+ expect(audioById(bed.id).lane).toBe(1);
+ expect(audioById(fx.id).lane).toBe(2);
+ });
+
+ test("the row past the bottom opens a new last lane", () => {
+ const bed = aclip({});
+ useEditor.setState({ audioClips: [bed] });
+ let id = "";
+ landOnRow("audio", 1, (lane) => (id = land(1, lane)));
+ expect(audioById(id).lane).toBe(1);
+ expect(audioById(bed.id).lane).toBeUndefined();
+ });
+
+ test("an empty band lands on lane 0", () => {
+ useEditor.setState({ audioClips: [] });
+ let id = "";
+ landOnRow("audio", 0, (lane) => (id = land(0, lane)));
+ expect(audioById(id).lane).toBeUndefined();
+ });
+});
+
describe("commitRow on the audio lanes", () => {
test("the row past the top opens a new first lane", () => {
const bed = aclip({});
diff --git a/site/src/cut/lib/laneTracks.ts b/site/src/cut/lib/laneTracks.ts
index 88fa8bbb..83b5e5ea 100644
--- a/site/src/cut/lib/laneTracks.ts
+++ b/site/src/cut/lib/laneTracks.ts
@@ -1117,6 +1117,45 @@ export function commitRow(kind: LaneKind, id: string, targetRow: number) {
ad.apply(raws.map((r, i) => ad.lanePatch!(r, remap.get(moved[i]) ?? 0)));
}
+/**
+ * Land a new item on a display row of a multi-lane band — the row under a
+ * drop, or one past either edge — by the lane arithmetic a lane drag commits
+ * with, so a drop and a drag open rows the same way. `add` places the item
+ * on the lane the row names. A row opened past the top first moves the whole
+ * band down one, and the band is renumbered from 0 once the item has landed,
+ * the way a committed drag leaves it. One undo step.
+ */
+export function landOnRow(kind: LaneKind, row: number, add: (lane: number) => void): void {
+ const s = useEditor.getState();
+ const ad = ADAPTERS[kind];
+ if (!ad.multiLane || !ad.lanePatch) {
+ add(0);
+ return;
+ }
+ const raws = ad.raws(s);
+ const views = raws.map((r) => ad.view(r));
+ const used = laneOrder(kind, s, views.map((v) => v.lane));
+ let lane = laneAtRow(kind, used, row);
+ s.beginHistoryBatch();
+ try {
+ if (lane < 0) {
+ const lift = -lane;
+ ad.apply(raws.map((r, i) => ad.lanePatch!(r, views[i].lane + lift)));
+ lane = 0;
+ }
+ add(lane);
+ const after = ad.raws(useEditor.getState());
+ const lanes = after.map((r) => ad.view(r).lane);
+ const usedNext = [...new Set(lanes)].sort((a, b) => a - b);
+ if (usedNext.some((l, i) => l !== i)) {
+ const remap = new Map(usedNext.map((l, i) => [l, i]));
+ ad.apply(after.map((r, i) => ad.lanePatch!(r, remap.get(lanes[i]) ?? 0)));
+ }
+ } finally {
+ s.endHistoryBatch();
+ }
+}
+
export interface LaneTrimUI {
pps: number;
/** Paint (or clear) the snap guide at this stage-x pixel. */
diff --git a/site/src/cut/lib/refPlace.ts b/site/src/cut/lib/refPlace.ts
index 93c53e76..a4926e3c 100644
--- a/site/src/cut/lib/refPlace.ts
+++ b/site/src/cut/lib/refPlace.ts
@@ -3,8 +3,9 @@
/**
* ⌘V of anything a panel offers: the tile's + button, for a ref that arrived
* as a mention token on the system clipboard. A copied media tile, library
- * card, stock sound, sticker, effect, shape, transition or template lands
- * under the indicator the way its tile's + button lands it.
+ * card, stock sound, sticker, effect, shape or template lands under the
+ * indicator the way its tile's + button lands it; a transition lands the way
+ * a dropped one does, through the timeline's own landing.
*/
import { CATALOG_PREFIX, type AssetRef } from "./assetRef";
@@ -16,14 +17,9 @@ import {
} from "./library";
import { isLottieAsset } from "./lottieAssets";
import { importImage, importStockAudio, importStockVideo } from "./media";
-import { clipLen, useEditor } from "./store";
-import {
- transitionDefaultSeconds,
- XBAR_MAGNET_PX,
- type MediaAsset,
- type ShapeKind,
- type TransitionStyle,
-} from "./types";
+import { useEditor } from "./store";
+import { landTransitionAt } from "./timelineDrop";
+import type { MediaAsset, ShapeKind, TransitionStyle } from "./types";
import type { EffectId } from "@donkeycut/effects-kit";
/** A project asset lands as the tile's + button lands it: a sticker as a
@@ -39,26 +35,6 @@ function placeAsset(asset: MediaAsset, at: number): void {
s.addAssetAtPlayhead(asset.id, at);
}
-/** A transition takes the clip tail within the same reach a dragged bar
- * snaps by, replacing whatever plays there; with no edge in reach it parks
- * at the indicator, the way a bar dropped in open row does. */
-function placeTransition(style: TransitionStyle, at: number): void {
- const s = useEditor.getState();
- const reach = XBAR_MAGNET_PX / s.pxPerSec;
- let best: { id: string; gap: number } | null = null;
- for (const c of s.clips) {
- const gap = Math.abs(c.start + clipLen(c) - at);
- if (gap <= reach && (!best || gap < best.gap)) best = { id: c.id, gap };
- }
- const seconds = transitionDefaultSeconds(style);
- if (best) {
- s.setClipTransition(best.id, seconds, style);
- return;
- }
- const id = s.addTransition({ start: at, seconds, style });
- s.select({ kind: "transition", id });
-}
-
/**
* Land one ref at `at`. True when the ref named something a panel places;
* false for a live timeline entity or a chat attachment, which no tile
@@ -121,8 +97,7 @@ export async function placeRefAtPlayhead(
s.addShape(pick as ShapeKind, { at });
return true;
case "transition":
- placeTransition(pick as TransitionStyle, at);
- return true;
+ return landTransitionAt(at, pick as TransitionStyle);
case "template": {
const own = s.templates.find((t) => t.id === pick);
if (own) {
diff --git a/site/src/cut/lib/timelineDrop.ts b/site/src/cut/lib/timelineDrop.ts
index 8a5acd8f..c7e4180b 100644
--- a/site/src/cut/lib/timelineDrop.ts
+++ b/site/src/cut/lib/timelineDrop.ts
@@ -1,15 +1,18 @@
"use client";
import type { VideoTrackPlacement } from "./store";
+import type { TransitionStyle } from "./types";
/** Where an OS file released over the timeline lands: the time under the
- * pointer and the video row it resolved to, a new track included. The
- * timeline owns that resolution — the same one every media drag uses — and
- * publishes it here for the window-level file drop in the editor, which
- * owns the import. */
+ * pointer, the video row it resolved to and the audio row, a new track or
+ * lane included. The timeline owns that resolution — the same one every
+ * media drag uses — and publishes it here for the window-level file drop in
+ * the editor, which owns the import. */
export interface FileLanding {
at: number;
place: VideoTrackPlacement;
+ /** The audio display row, one past either edge for a new lane. */
+ audioRow: number;
}
let resolver: ((clientX: number, clientY: number) => FileLanding | null) | null = null;
@@ -30,3 +33,25 @@ export function registerFileLanding(
export function fileLandingAt(clientX: number, clientY: number): FileLanding | null {
return resolver?.(clientX, clientY) ?? null;
}
+
+let transitionLanding: ((t: number, style: TransitionStyle) => void) | null = null;
+
+/** The mounted timeline registers how a transition lands at a time — the
+ * nearest cut or clip edge within reach, else parked there — so a paste lands
+ * a bar exactly as a drop does; returns the unregister. */
+export function registerTransitionLanding(
+ land: (t: number, style: TransitionStyle) => void
+): () => void {
+ transitionLanding = land;
+ return () => {
+ if (transitionLanding === land) transitionLanding = null;
+ };
+}
+
+/** Land a transition bar at `t` the way the timeline lands a dropped one.
+ * False when no timeline is mounted. */
+export function landTransitionAt(t: number, style: TransitionStyle): boolean {
+ if (!transitionLanding) return false;
+ transitionLanding(t, style);
+ return true;
+}