Skip to content

Open source CapCut alternative.

TypeScript

feat(site): a pasted picture lands under the indicator

David Le2a72d41a4e9b
diff --git a/site/src/cut/components/Editor.tsx b/site/src/cut/components/Editor.tsx
index 6f0c2a94..f8303587 100644
--- a/site/src/cut/components/Editor.tsx
+++ b/site/src/cut/components/Editor.tsx
@@ -23,7 +23,7 @@ import {
import { fileZoneAt, hasRefDrag, selectionRefTokens } from "@/cut/lib/assetRef";
import { startUpload } from "@/cut/lib/importQueue";
import { enrichAsset, importFileToProject, prepareImport } from "@/cut/lib/media";
-import { clearCopiedFrame, hasCopiedFrame, pasteCopiedFrame } from "@/cut/lib/stageFrame";
+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.
import "@/cut/lib/genScene";
@@ -648,7 +648,18 @@ export function Editor({
const importFiles = useCallback(
async (
files: FileList | File[],
- opts?: { at?: number; origin?: MediaAsset["origin"]; mediaOnly?: boolean }
+ opts?: {
+ at?: number;
+ origin?: MediaAsset["origin"];
+ mediaOnly?: boolean;
+ /** Display name for the asset, when the file's own name is a storage
+ * detail (a copied frame). */
+ name?: string;
+ /** Land at `at` itself on the first row with room, opening a new row
+ * when every one of them is busy there — what a paste means by "here".
+ * Without it the file takes track 0's next free slot. */
+ atFirstFit?: boolean;
+ }
) => {
const list = Array.from(files);
setImporting((n) => n + list.length);
@@ -666,12 +677,21 @@ export function Editor({
// Recordings are created media: tag them so they land on the timeline
// but never in the Media panel (reserved for user imports).
if (opts?.origin) asset.origin = opts.origin;
+ if (opts?.name) asset.name = opts.name;
const s = useEditor.getState();
+ // Probing, naming and (for a pasted frame) rendering all outlive a
+ // project switch. The asset was prepared against `projectId` and its
+ // bytes live in that project's storage, so filing it into whatever
+ // document is open now would point the new project at the old one's
+ // media.
+ if (s.projectId !== projectId) continue;
s.addAsset(asset);
// mediaOnly stocks the Media panel and leaves the timeline alone
// (drops that land outside the timeline); placement is up to the user.
if (!opts?.mediaOnly) {
- if (asset.type === "video" || asset.type === "image") {
+ if (opts?.atFirstFit) {
+ s.addAssetAtPlayhead(asset.id, opts.at);
+ } else if (asset.type === "video" || asset.type === "image") {
// A drop on the timeline lands at the pointer (sliding to track
// 0's next free slot); an upload appends at the end. A still
// rides track 0 like footage.
@@ -809,7 +829,8 @@ export function Editor({
// 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
// composer marks its own paste handled, so this only ever sees the pastes
- // nobody else wanted. The picture lands at the preview time.
+ // nobody else wanted. The picture lands under the indicator, on the first
+ // row with room for it.
useEffect(() => {
if (viewer) return;
const onPaste = (e: ClipboardEvent) => {
@@ -830,7 +851,7 @@ export function Editor({
.filter((f): f is File => f !== null);
if (files.length === 0) return;
e.preventDefault();
- void importFiles(files, { at: Math.max(0, previewAt()) });
+ void importFiles(files, { at: Math.max(0, previewAt()), atFirstFit: true });
};
window.addEventListener("paste", onPaste);
return () => window.removeEventListener("paste", onPaste);
@@ -895,11 +916,25 @@ export function Editor({
}
} else if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "v") {
// The timeline clipboard first; a frame copied off the preview canvas
- // lands as a still at the preview time.
+ // 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();
- void pasteCopiedFrame().catch((err) => reportSwallowed("[cut] paste frame failed", err));
+ 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));
}
} else if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "g") {
// ⌘G groups the multi-selected elements, ⇧⌘G dissolves the primary's
@@ -939,7 +974,7 @@ export function Editor({
};
window.addEventListener("keydown", onKey);
return () => window.removeEventListener("keydown", onKey);
- }, []);
+ }, [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/Preview.tsx b/site/src/cut/components/Preview.tsx
index cdbb0e03..c5fc3bd6 100644
--- a/site/src/cut/components/Preview.tsx
+++ b/site/src/cut/components/Preview.tsx
@@ -11,7 +11,7 @@ import {
useSkim,
} from "@/cut/lib/playhead";
import { getClipSpans, projectDuration, useEditor } from "@/cut/lib/store";
-import { copyStageFrame, stageHasFrame } from "@/cut/lib/stageFrame";
+import { copyStageFrame, primeStageFrame, stageHasFrame } from "@/cut/lib/stageFrame";
import {
capturePoster,
capturePosterWhenReady,
@@ -335,6 +335,9 @@ export function Preview() {
if (s.playing) s.setPlaying(false);
setCopyState("idle");
setFrameMenu({ x: e.clientX, y: e.clientY });
+ // The picture is drawn now, while the pointer travels to the item: the
+ // decode it costs is what stood between the click and "Copied".
+ primeStageFrame();
};
useEffect(() => {
diff --git a/site/src/cut/lib/stageFrame.ts b/site/src/cut/lib/stageFrame.ts
index 4065363e..a88a2d58 100644
--- a/site/src/cut/lib/stageFrame.ts
+++ b/site/src/cut/lib/stageFrame.ts
@@ -10,25 +10,32 @@
* an image — a chat composer, a message, another app — and is held here for ⌘V
* onto the timeline, where it becomes a project image at the preview time.
*
+ * Drawing it costs a decode of the footage under the playhead, so the draw
+ * starts when the menu opens, not when the item is clicked: by the time the
+ * pointer reaches "Copy frame" the picture is usually in hand, and the copy
+ * reads as instant. The hold is a promise, so a ⌘V that beats the draw still
+ * lands the frame it was promised.
+ *
* The held frame and the timeline clipboard are one clipboard to the person
* using them, so a copy on either side clears the other and ⌘V has one answer.
*/
import { renderProjectFrame } from "./exportRender";
-import { enrichAsset, uploadProjectImage } from "./media";
+import { uploadProjectImage } from "./media";
import { previewAt } from "./playhead";
import { rasterCanvasToPng } from "./raster";
import { projectDuration, useEditor } from "./store";
import { frameOf, type MediaAsset } from "./types";
-/** A copied frame, waiting for a paste. */
-interface CopiedFrame {
- png: Blob;
- /** Timeline second it was taken at — it names the asset. */
+/** A frame being drawn, or drawn already. */
+interface StageFrame {
+ png: Promise<Blob>;
+ /** Timeline second it was taken at — it names the still. */
at: number;
}
-let held: CopiedFrame | null = null;
+let held: StageFrame | null = null;
+let primed: StageFrame | null = null;
/** m:ss, for the still's name. */
function stampOf(at: number): string {
@@ -63,24 +70,38 @@ export async function renderStageFrame(at: number): Promise<Blob> {
/** Whether the stage has a picture worth copying. */
export const stageHasFrame = (): boolean => projectDuration(useEditor.getState()) > 0;
+/** The frame at the preview time, drawn once and kept: the canvas menu primes
+ * it as it opens so the copy behind it has nothing left to wait for. A failure
+ * is carried on the promise and reported when a copy asks for it. */
+export function primeStageFrame(): void {
+ const at = Math.max(0, previewAt());
+ if (primed?.at === at) return;
+ const png = renderStageFrame(at);
+ void png.catch(() => {});
+ primed = { png, at };
+}
+
/**
* Copy the frame the preview is showing.
*
* The blob goes onto the system clipboard as a promise rather than an awaited
* value: Safari ties a clipboard write to the gesture that asked for it, and
- * compositing the frame first spends that gesture. Rejects when the frame
- * cannot be drawn or the browser refuses the write, which is the caller's cue
- * to say so — a frame that drew is held for the timeline either way.
+ * compositing the frame first spends that gesture. The frame is held for ⌘V
+ * before either settles, so a paste right behind the click has it. Rejects
+ * when the frame cannot be drawn or the browser refuses the write, which is
+ * the caller's cue to say so — a frame that drew is held for the timeline
+ * either way.
*/
export async function copyStageFrame(): Promise<void> {
const at = Math.max(0, previewAt());
- const png = renderStageFrame(at);
+ const png = primed?.at === at ? primed.png : renderStageFrame(at);
+ primed = null;
+ held = { png, at };
// The write's verdict is kept rather than awaited here: a frame that fails
// to draw fails the write with it, and the draw is the failure worth
// reporting. A browser that refuses the call on the spot — no clipboard on
// an insecure context, a ClipboardItem that wants a settled blob — throws
- // where it stands, so it is caught into the same verdict and the frame
- // below is still held for ⌘V.
+ // where it stands, so it is caught into the same verdict.
let wroteOk: Promise<boolean>;
try {
wroteOk = navigator.clipboard.write([new ClipboardItem({ "image/png": png })]).then(
@@ -90,9 +111,17 @@ export async function copyStageFrame(): Promise<void> {
} catch {
wroteOk = Promise.resolve(false);
}
- held = { png: await png, at };
- // The frame is the clipboard's contents now; a timeline copy no longer
- // outranks it at the next ⌘V.
+ try {
+ await png;
+ } catch (err) {
+ // Nothing was drawn, so there is nothing to paste either — and the
+ // timeline's own clipboard is untouched, so the clip the user copied
+ // before this is still theirs to paste.
+ held = null;
+ throw err;
+ }
+ // The frame drew, so it is the clipboard's contents now; a timeline copy no
+ // longer outranks it at the next ⌘V.
useEditor.getState().clearClipboard();
if (!(await wroteOk)) throw new Error("The browser refused the clipboard write.");
}
@@ -105,6 +134,15 @@ export const clearCopiedFrame = (): void => {
held = null;
};
+/** The still's file name and display name for the moment it was taken. */
+function stillNames(at: number): { fileName: string; name: string } {
+ const stamp = stampOf(at);
+ return {
+ fileName: `frame-${stamp.replace(":", "m")}s-${crypto.randomUUID().slice(0, 4)}.png`,
+ name: `Frame ${stamp}`,
+ };
+}
+
/** Store a composited still as a project image, named for the moment it was
* taken. Created media, so it carries the freeze origin and stays out of the
* Media panel. */
@@ -114,30 +152,21 @@ export async function storeStageStill(
at: number,
failMessage?: string
): Promise<MediaAsset> {
- const stamp = stampOf(at);
- const fileName = `frame-${stamp.replace(":", "m")}s-${crypto.randomUUID().slice(0, 4)}.png`;
+ const { fileName, name } = stillNames(at);
const body = await uploadProjectImage(projectId, png, fileName, {
- name: `Frame ${stamp}`,
+ name,
...(failMessage ? { failMessage } : {}),
});
return { ...body, origin: "freeze" };
}
-/**
- * Land the copied frame on the timeline: a project image placed at the preview
- * time on the first row with room for it.
- */
-export async function pasteCopiedFrame(): Promise<void> {
+/** The copied frame as a file to import, or null when nothing is held. The
+ * hold survives the paste, so the same frame drops as many times as ⌘V is
+ * pressed. */
+export async function copiedFrameFile(): Promise<{ file: File; name: string } | null> {
const frame = held;
- const s = useEditor.getState();
- if (!frame || !s.projectId || s.readOnly) return;
- const projectId = s.projectId;
- const asset = await storeStageStill(projectId, frame.png, frame.at, "Could not paste the frame.");
- const cur = useEditor.getState();
- // The project can change while the upload runs; the still belongs to the one
- // it was pasted into.
- if (cur.projectId !== projectId) return;
- cur.addAsset(asset);
- cur.addAssetAtPlayhead(asset.id);
- void enrichAsset(asset);
+ if (!frame) return null;
+ const png = await frame.png;
+ const { fileName, name } = stillNames(frame.at);
+ return { file: new File([png], fileName, { type: "image/png" }), name };
}
diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.ts
index f8b8ccbb..101835e2 100644
--- a/site/src/cut/lib/store.ts
+++ b/site/src/cut/lib/store.ts
@@ -354,11 +354,14 @@ export interface EditorState {
* to that gain while it plays; `opts.lane` picks the audio track it lands
* on (default: the first one). */
addAudioFromAsset: (assetId: string, start?: number, opts?: { duck?: number; lane?: number }) => void;
- /** The panel “+” add: the asset lands at the preview time (the skimmer while
- * one is live, the playhead otherwise), on the lowest video track or audio
- * lane with room for its whole length there. When every existing row is
- * occupied at that moment it opens a new row above. */
- addAssetAtPlayhead: (assetId: string) => void;
+ /** The panel “+” add, and every paste: the asset lands at `at` — by default
+ * the preview time (the skimmer while one is live, the playhead otherwise) —
+ * on the lowest video track or audio lane with room for its whole length
+ * there. When every existing row is occupied at that moment it opens a new
+ * row above. A paste passes the time of the keystroke, so the still lands
+ * where the indicator was rather than wherever it drifted while the import
+ * ran. */
+ addAssetAtPlayhead: (assetId: string, at?: number) => void;
/** Set (or clear) the persisted brief-to-video run. Replaces the object by
* reference so autosave detects the change. */
setGenvideo: (project: VideoProject | undefined) => void;
@@ -2201,10 +2204,10 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {
}));
},
- addAssetAtPlayhead: (assetId) => {
+ addAssetAtPlayhead: (assetId, at) => {
const asset = get().assets.find((a) => a.id === assetId);
if (!asset) return;
- const t = Math.max(0, previewAt());
+ const t = Math.max(0, at ?? previewAt());
// A row fits when `nextFreeStart` keeps the clip at the preview time
// itself; the scan climbs until one does. A brand-new row always fits,
// so the clip lands under the indicator no matter how full the stack is.