Skip to content

Open source CapCut alternative.

TypeScript

feat(site): paste files and copied tiles onto the timeline [rebuild]

David Lef6c3ac2f3bc3
diff --git a/site/src/cut/components/AiPanel.tsx b/site/src/cut/components/AiPanel.tsx
index 1d55a717..aefdb2eb 100644
--- a/site/src/cut/components/AiPanel.tsx
+++ b/site/src/cut/components/AiPanel.tsx
@@ -50,6 +50,7 @@ import { useCutCaps, useLocalCompute } from "@/cut/lib/backend/hooks";
import { localBackend } from "@/cut/lib/backend/local";
import { buildAiContext } from "@/cut/lib/aiContext";
import { setAssetDragData } from "@/cut/lib/assetDrag";
+import { useRefCopy } from "@/cut/lib/refCopy";
import { registerChatIntake } from "@/cut/lib/chatIntake";
import { videoModel } from "@/cut/lib/videoModels";
import {
@@ -1712,8 +1713,11 @@ function MessageAssetCard({ asset }: { asset: AssetRef }) {
// jump the side panel to the asset.
const clickTimer = useRef<number | undefined>(undefined);
useEffect(() => () => window.clearTimeout(clickTimer.current), []);
+ // ⌘C over the card copies what it stands for, the way its drag carries it.
+ const copyRef = useRefCopy(() => [asset]);
return (
<div
+ ref={copyRef}
className={cn(
"ai-msg-asset group/chip relative",
// Audio gets the wide timeline-pill treatment; the row still wraps
diff --git a/site/src/cut/components/AudioPanel.tsx b/site/src/cut/components/AudioPanel.tsx
index b9260a97..ef5b08a6 100644
--- a/site/src/cut/components/AudioPanel.tsx
+++ b/site/src/cut/components/AudioPanel.tsx
@@ -2,7 +2,7 @@
import {
type DragEventHandler,
- type HTMLAttributes,
+ type ComponentProps,
type ReactNode,
useEffect,
useRef,
@@ -41,7 +41,8 @@ import {
import { LiveElapsed } from "@/cut/components/Elapsed";
import { clearAssetDrag, setAssetDragData } from "@/cut/lib/assetDrag";
import { MUSIC_VARIANTS, synthesizeMusic, type MusicVariant } from "@/cut/lib/audioGen";
-import { draggingRef, hasRefDrag } from "@/cut/lib/assetRef";
+import { draggingRef, hasRefDrag, refFromAsset, type AssetRef } from "@/cut/lib/assetRef";
+import { useRefCopy } from "@/cut/lib/refCopy";
import { useMusicGen } from "@/cut/lib/musicGen";
import { STOCK_MUSIC } from "@/cut/lib/stockMusicManifest";
import { stockAssetInDoc } from "@/cut/lib/genvideo/docWriter";
@@ -563,7 +564,7 @@ export function AudioPillSurface({
className,
children,
...rest
-}: { peaks?: number[] } & HTMLAttributes<HTMLDivElement>) {
+}: { peaks?: number[] } & ComponentProps<"div">) {
return (
<div
className={cn(
@@ -706,6 +707,8 @@ function buildAudioDragGhost(name: string, width: number, peaks?: number[]): HTM
* the chat's audio cards), styled like its timeline clip: emerald gradient,
* white waveform, name overlaid, plus a play button, duration badge, and a
* hover-revealed menu + add button. */
+const NO_REFS = (): AssetRef[] => [];
+
export function AudioRow({
name,
duration,
@@ -717,6 +720,7 @@ export function AudioRow({
menu,
onDragStart,
pulse,
+ refs,
}: {
name: string;
duration: number;
@@ -731,9 +735,14 @@ export function AudioRow({
onDragStart?: DragEventHandler<HTMLDivElement>;
/** A freshly-generated clip the user hasn't seen — pulses blue for a beat. */
pulse?: boolean;
+ /** What a ⌘C over the row copies: the asset's ref, so the row pastes onto
+ * the timeline and into a prompt the way it drags. */
+ refs?: () => AssetRef[];
}) {
+ const copyRef = useRefCopy(refs ?? NO_REFS);
return (
<AudioPillSurface
+ ref={copyRef}
peaks={peaks}
className="audio-row group/row h-10 shrink-0 cursor-grab"
draggable={!!onDragStart}
@@ -835,6 +844,7 @@ function ProjectAudio({
pulse={pulsing.includes(a.id)}
onTogglePlay={onTogglePlay}
onAdd={() => useEditor.getState().addAssetAtPlayhead(a.id)}
+ refs={() => [refFromAsset(a)]}
menu={
<GeneratedAssetMenu
asset={a}
@@ -897,6 +907,7 @@ function ProjectMusic({
pulse={pulsing.includes(a.id)}
onTogglePlay={onTogglePlay}
onAdd={() => useEditor.getState().addAssetAtPlayhead(a.id)}
+ refs={() => [refFromAsset(a)]}
menu={
<GeneratedAssetMenu
asset={a}
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index 957ae9dd..7a1a3427 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -21,10 +21,13 @@ import {
renderPreviewProxy,
type ExportDoc,
} from "@/cut/lib/exportClient";
-import { fileZoneAt, hasRefDrag, selectionRefTokens } from "@/cut/lib/assetRef";
+import { EMPTY_LIBRARY, fileZoneAt, hasRefDrag, parseMentions, selectionRefTokens, useRefCandidates } from "@/cut/lib/assetRef";
+import { placeRefAtPlayhead } from "@/cut/lib/refPlace";
+import { useLibrary } from "@/cut/lib/queries";
import { copyableRefs } from "@/cut/lib/refCopy";
import { startUpload } from "@/cut/lib/importQueue";
-import { enrichAsset, importFileToProject, prepareImport } from "@/cut/lib/media";
+import { enrichAsset, importFileToProject, isFontFile, isMediaFile, prepareImport } from "@/cut/lib/media";
+import { uploadLibraryFont } from "@/cut/lib/linkedLibrary";
import { clearCopiedFrame, copiedFrameFile, hasCopiedFrame } from "@/cut/lib/stageFrame";
// Side-effect import: registers the brief-to-video resume subscription, so a
// persisted run resumes on project load even when the AI panel never mounts.
@@ -937,32 +940,129 @@ export function Editor({
};
}, [importFiles, viewer]);
- // An image on the system clipboard joins the cut: a frame copied from
- // another tab's preview, a screenshot, a picture from anywhere else. The
- // editor's own ⌘V claims the timeline clipboard and the copied frame before
- // this — both stop the keystroke, so no paste event follows them — and a
+ // The editor's own copies: the timeline clipboard first, then a frame
+ // copied off the preview canvas, which lands as a still under the
+ // indicator. Where it lands is read at the keystroke, so a still that waits
+ // on its draw or its upload still arrives where the paste was aimed. True
+ // when something of ours was pasted.
+ const pasteOwn = useCallback(
+ (at: number): boolean => {
+ if (useEditor.getState().paste()) return true;
+ if (!hasCopiedFrame()) return false;
+ void copiedFrameFile()
+ .then((grab) => {
+ if (grab) {
+ return importFiles([grab.file], { at, atFirstFit: true, origin: "freeze", name: grab.name });
+ }
+ })
+ .catch((err) => reportSwallowed("[cut] paste frame failed", err));
+ return true;
+ },
+ [importFiles]
+ );
+
+ // ⌘V is decided at the paste event, the one place the system clipboard can
+ // be read, so the newest copy wins whichever app made it: a recording
+ // copied out of Voice Memos, a screenshot, a video file from the Finder, a
+ // 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 picture lands under the indicator, on the first
- // row with room for it.
+ // 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);
+ const library = useLibrary({ enabled: !viewer }).data ?? EMPTY_LIBRARY;
+ const mentionable = useRef({ candidates, 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]);
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.
if (s.exportOpen || dialogOnTop()) return;
if (isPasteTarget(e.target)) return;
- const files = Array.from(e.clipboardData?.items ?? [])
- .filter((i) => i.kind === "file" && i.type.startsWith("image/"))
- .map((i) => i.getAsFile())
- .filter((f): f is File => f !== null);
- if (files.length === 0) return;
+ const at = Math.max(0, previewAt());
+ const pasted = Array.from(e.clipboardData?.files ?? []);
+ // A font is not media for the timeline: it goes on the shelf and lights
+ // up in the font menu, the way the Library panel takes a dropped one.
+ const fonts = pasted.filter(isFontFile);
+ if (fonts.length > 0) {
+ e.preventDefault();
+ for (const font of fonts) {
+ void uploadLibraryFont(font).catch((err) =>
+ reportSwallowed(`[cut] paste failed for ${font.name}`, err)
+ );
+ }
+ }
+ const files = pasted.filter((f) => isMediaFile(f) && !isFontFile(f));
+ if (files.length === 0) {
+ if (fonts.length > 0) return;
+ if (pasteOwn(at)) {
+ e.preventDefault();
+ return;
+ }
+ // A ⌘C on a tile — a media card, a library card, a stock sound, a
+ // 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 refs = parseMentions(e.clipboardData?.getData("text/plain") ?? "", candidates).refs;
+ const projectId = s.projectId;
+ if (refs.length === 0 || !projectId) return;
+ e.preventDefault();
+ void (async () => {
+ for (const ref of refs) {
+ try {
+ await placeRefAtPlayhead(ref, { projectId, library, at });
+ } catch (err) {
+ reportSwallowed(`[cut] paste failed for ${ref.name}`, err);
+ }
+ }
+ })();
+ return;
+ }
e.preventDefault();
- void importFiles(files, { at: Math.max(0, previewAt()), atFirstFit: true });
+ // The frame the preview copied went out to the system clipboard as a
+ // PNG, so it comes back here as a file like any other. It is told apart
+ // by its bytes and keeps its freeze origin and its name; any other file
+ // is a newer copy from elsewhere.
+ void copiedFrameFile()
+ .then((grab) => {
+ const own =
+ grab !== null &&
+ files.length === 1 &&
+ files[0].type === grab.file.type &&
+ files[0].size === grab.file.size;
+ return own
+ ? importFiles([grab.file], { at, atFirstFit: true, origin: "freeze", name: grab.name })
+ : importFiles(files, { at, atFirstFit: true });
+ })
+ .catch((err) => reportSwallowed("[cut] paste failed", err));
};
window.addEventListener("paste", onPaste);
return () => window.removeEventListener("paste", onPaste);
- }, [importFiles, viewer]);
+ }, [disarmPasteFallback, importFiles, pasteOwn, viewer]);
// Keyboard shortcuts.
useEffect(() => {
@@ -1035,35 +1135,21 @@ export function Editor({
// copy, so ⌘V still pastes what was copied for the timeline.
if (copyableRefs().length > 0) return;
const token = selectionRefTokens(s);
- if (token) void navigator.clipboard.writeText(token).catch(() => {});
// The newer copy owns the clipboard: a copied preview frame steps
- // aside for this one.
+ // aside for this one, and so does a file copied from another app —
+ // the system clipboard takes the token, or nothing at all, so a
+ // paste finds the timeline copy first.
const copied = s.copySelection();
if (copied) clearCopiedFrame();
- if (copied || token) e.preventDefault();
+ if (copied || token) {
+ void navigator.clipboard.writeText(token ?? "").catch(() => {});
+ e.preventDefault();
+ }
}
} else if (mod && e.key.toLowerCase() === "v") {
- // The timeline clipboard first; a frame copied off the preview canvas
- // lands as a still under the indicator. Where it lands is read at the
- // keystroke, so a still that waits on its draw or its upload still
- // arrives where the paste was aimed.
- if (s.paste()) e.preventDefault();
- else if (hasCopiedFrame()) {
- e.preventDefault();
- const at = Math.max(0, previewAt());
- void copiedFrameFile()
- .then((grab) => {
- if (grab) {
- return importFiles([grab.file], {
- at,
- atFirstFit: true,
- origin: "freeze",
- name: grab.name,
- });
- }
- })
- .catch((err) => reportSwallowed("[cut] paste frame failed", err));
- }
+ // 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
@@ -1105,7 +1191,7 @@ export function Editor({
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
- }, [importFiles]);
+ }, [armPasteFallback, 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 c806f508..34c24dc4 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -62,7 +62,7 @@ import { useMatteBakes } from "@/cut/lib/removal/bakeJobs";
import { laneHidden, subtitleLaneCount } from "@/cut/lib/subtitles";
import { formatTime, formatTimecode } from "@/cut/lib/time";
import { EFFECT_LABELS, hasSpeedCurve, headSrc, retimeOf, SPEED_CURVE_MAX, SPEED_CURVE_MIN, tailSrc, type EffectId, type Retime, type SpeedNode } from "@donkeycut/effects-kit";
-import { assetIsSilent, emptySubtitles, IMAGE_CLIP_SECONDS, isAudioTransition, SHAPE_LABELS, TRANSITION_MAX, TRANSITION_STYLE_LABELS, transitionBarStart, transitionDefaultSeconds, type ShapeKind } from "@/cut/lib/types";
+import { assetIsSilent, emptySubtitles, IMAGE_CLIP_SECONDS, isAudioTransition, SHAPE_LABELS, TRANSITION_MAX, TRANSITION_STYLE_LABELS, transitionBarStart, transitionDefaultSeconds, XBAR_MAGNET_PX, type ShapeKind } from "@/cut/lib/types";
import type { AudioClip, ClipSpan, ColorGrade, MediaAsset, Overlay, Selection, StickerOverlay, SubtitleCue, TimelineTransition, TransitionBoundaryKind, TransitionStyle, VideoClip } from "@/cut/lib/types";
import { isLottieAsset } from "@/cut/lib/lottieAssets";
import { gradeCssApprox } from "@donkeycut/effects-kit";
@@ -273,10 +273,6 @@ type XBar = {
label: string;
};
-/** How far an anchor reaches while a bar is in flight, px. Inside it the drop
- * aligns to the anchor; outside it the bar lands exactly where it is. */
-const XBAR_MAGNET_PX = 16;
-
const overlayFamily = (o: Overlay): OverlayFamily =>
o.kind === "effect" ? "effect" : o.kind === "shape" || o.kind === "sticker" ? "element" : "text";
diff --git a/site/src/cut/lib/assetRef.ts b/site/src/cut/lib/assetRef.ts
index 84f442b5..d176cd66 100644
--- a/site/src/cut/lib/assetRef.ts
+++ b/site/src/cut/lib/assetRef.ts
@@ -447,7 +447,7 @@ export function useAssetDrop(
// ---------------------------------------------------------------------------
// Candidates and @name mentions
-const EMPTY_LIBRARY: LibraryData = { assets: [], folders: [], templates: [] };
+export const EMPTY_LIBRARY: LibraryData = { assets: [], folders: [], templates: [] };
/** The open project's media as refs with their session handles, assigned in
* media order — `v1` videos, `i1` generated stills, `a1` audio — so a prompt
diff --git a/site/src/cut/lib/refCopy.ts b/site/src/cut/lib/refCopy.ts
index 94787af0..1a96bf56 100644
--- a/site/src/cut/lib/refCopy.ts
+++ b/site/src/cut/lib/refCopy.ts
@@ -28,6 +28,7 @@ import {
refToken,
type AssetRef,
} from "./assetRef";
+import { clearCopiedFrame } from "./stageFrame";
import { useEditor } from "./store";
type RefsFn = () => AssetRef[];
@@ -160,6 +161,10 @@ export const refClipboardText = (refs: AssetRef[]) => refs.map(refToken).join("
export function copyRefs(refs: AssetRef[]): boolean {
if (refs.length === 0) return false;
void navigator.clipboard?.writeText(refClipboardText(refs.map(withHandle))).catch(() => {});
+ // The newer copy owns the clipboard: the timeline's own copy and a copied
+ // preview frame step aside, so ⌘V on the timeline lands what was copied.
+ useEditor.getState().clearClipboard();
+ clearCopiedFrame();
return true;
}
diff --git a/site/src/cut/lib/refPlace.ts b/site/src/cut/lib/refPlace.ts
new file mode 100644
index 00000000..93c53e76
--- /dev/null
+++ b/site/src/cut/lib/refPlace.ts
@@ -0,0 +1,144 @@
+"use client";
+
+/**
+ * ⌘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.
+ */
+
+import { CATALOG_PREFIX, type AssetRef } from "./assetRef";
+import {
+ addProjectTemplateToTimeline,
+ addTemplateToProject,
+ importLibraryAsset,
+ type LibraryData,
+} 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 type { EffectId } from "@donkeycut/effects-kit";
+
+/** A project asset lands as the tile's + button lands it: a sticker as a
+ * sticker element, footage and stills on the first video row with room, a
+ * sound on the first audio lane with room. */
+function placeAsset(asset: MediaAsset, at: number): void {
+ const s = useEditor.getState();
+ if (asset.type === "font") return;
+ if (asset.origin === "sticker") {
+ s.addSticker({ assetId: asset.id, ...(isLottieAsset(asset) ? { lottie: true } : {}), at });
+ return;
+ }
+ 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
+ * offers. Imports (library copies, stock downloads) run behind the editor and
+ * place when they settle.
+ */
+export async function placeRefAtPlayhead(
+ ref: AssetRef,
+ ctx: { projectId: string; library: LibraryData; at: number }
+): Promise<boolean> {
+ const { projectId, library, at } = ctx;
+ const placeWhenReady = (asset: MediaAsset) => {
+ // The import outlived a project switch: its bytes belong to the project
+ // it was prepared against.
+ if (useEditor.getState().projectId !== projectId) return;
+ placeAsset(asset, at);
+ };
+ switch (ref.scope) {
+ case "project": {
+ const asset = useEditor.getState().assets.find((a) => a.id === ref.id);
+ if (!asset) return false;
+ placeAsset(asset, at);
+ return true;
+ }
+ case "library": {
+ const lib = library.assets.find((a) => a.id === ref.id);
+ if (!lib || lib.type === "font") return false;
+ placeWhenReady(await importLibraryAsset(projectId, lib));
+ return true;
+ }
+ case "stock": {
+ const asset =
+ ref.kind === "audio"
+ ? await importStockAudio(projectId, { url: ref.url, name: ref.name, duration: ref.duration })
+ : ref.kind === "video"
+ ? await importStockVideo(projectId, {
+ url: ref.url,
+ name: ref.name,
+ duration: ref.duration,
+ width: ref.width,
+ height: ref.height,
+ })
+ : ref.kind === "image"
+ ? await importImage(projectId, { url: ref.url, name: ref.name })
+ : null;
+ if (!asset) return false;
+ placeWhenReady(asset);
+ return true;
+ }
+ case "entity": {
+ if (!ref.id.startsWith(CATALOG_PREFIX)) return false;
+ const [kind, ...rest] = ref.id.slice(CATALOG_PREFIX.length).split(":");
+ const pick = rest.join(":");
+ const s = useEditor.getState();
+ switch (kind) {
+ case "effect":
+ s.addEffect(pick as EffectId, { at });
+ return true;
+ case "shape":
+ s.addShape(pick as ShapeKind, { at });
+ return true;
+ case "transition":
+ placeTransition(pick as TransitionStyle, at);
+ return true;
+ case "template": {
+ const own = s.templates.find((t) => t.id === pick);
+ if (own) {
+ await addProjectTemplateToTimeline(projectId, own, at);
+ return true;
+ }
+ const shelf = library.templates.find((t) => t.id === pick);
+ if (!shelf) return false;
+ await addTemplateToProject(projectId, shelf, at);
+ return true;
+ }
+ default:
+ return false;
+ }
+ }
+ default:
+ return false;
+ }
+}
diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.ts
index 5e107ee7..2e3aad13 100644
--- a/site/src/cut/lib/types.ts
+++ b/site/src/cut/lib/types.ts
@@ -647,6 +647,11 @@ export const TRANSITION_DEFAULT_SECONDS = 0.5;
export const CROSS_DISSOLVE_DEFAULT_SECONDS = 0.3;
/** How long a bar of this style runs when it lands on a bare cut. */
+/** How far a cut or clip edge reaches while a transition bar is in flight,
+ * px. Inside it the bar aligns to the edge; outside it the bar lands exactly
+ * where it is. */
+export const XBAR_MAGNET_PX = 16;
+
export const transitionDefaultSeconds = (style: TransitionStyle): number =>
isAudioTransition(style) ? CROSS_DISSOLVE_DEFAULT_SECONDS : TRANSITION_DEFAULT_SECONDS;