Open source CapCut alternative.
TypeScript
fix(site): open uploading library files at their shape
David Le7f3e9791600f
diff --git a/site/src/cut/components/ImportVideoPoster.tsx b/site/src/cut/components/ImportVideoPoster.tsxindex cf57f99d..fa9da36c 100644--- a/site/src/cut/components/ImportVideoPoster.tsx+++ b/site/src/cut/components/ImportVideoPoster.tsx@@ -3,17 +3,24 @@import { useCallback, useEffect, useEffectEvent, useState } from "react";import { Film } from "lucide-react";import { Skeleton } from "@/components/ui/skeleton";-import { importPoster } from "@/cut/lib/importPoster";+import { importPoster, type ImportPosterFrame } from "@/cut/lib/importPoster";import { holdMemory } from "@/cut/lib/memoryBudget";-type Props = { file: File; size: number; onShape?: (shape: { width: number; height: number }) => void };+type Props = {+ file: File;+ size: number;+ onShape?: (shape: { width: number; height: number }) => void;+ /** The decoded frame while the tile holds it, and undefined once it lets go. */+ onFrame?: (frame: ImportPosterFrame | undefined) => void;+};/** A local frame while the original file is being imported to any shelf. */-export function ImportVideoPoster({ file, size, onShape }: Props) {+export function ImportVideoPoster({ file, size, onShape, onFrame }: Props) {const [visible, setVisible] = useState(false);const [poster, setPoster] = useState<string>();const [unreadable, setUnreadable] = useState(false);const measured = useEffectEvent((shape: { width: number; height: number }) => onShape?.(shape));+ const framed = useEffectEvent((frame: ImportPosterFrame | undefined) => onFrame?.(frame));const ref = useCallback((node: HTMLDivElement | null) => {if (!node) return;const observer = new IntersectionObserver(([entry]) => setVisible(entry.isIntersecting));@@ -26,9 +33,11 @@ export function ImportVideoPoster({ file, size, onShape }: Props) {const abort = new AbortController();let url: string | undefined;let release: (() => void) | undefined;- void importPoster(file, size, abort.signal).then(({ blob, width, height }) => {+ void importPoster(file, size, abort.signal).then((frame) => {if (abort.signal.aborted) return;+ const { blob, width, height } = frame;if (width && height) measured({ width, height });+ framed(frame);url = URL.createObjectURL(blob);release = holdMemory("libraryPictures", () => blob.size + size * size * 4);setPoster(url);@@ -41,6 +50,7 @@ export function ImportVideoPoster({ file, size, onShape }: Props) {if (url) URL.revokeObjectURL(url);release?.();setPoster(undefined);+ framed(undefined);};}, [file, size, visible]);diff --git a/site/src/cut/components/LibraryImportCard.tsx b/site/src/cut/components/LibraryImportCard.tsxindex ba726fdd..d3ccb44b 100644--- a/site/src/cut/components/LibraryImportCard.tsx+++ b/site/src/cut/components/LibraryImportCard.tsx@@ -18,6 +18,7 @@ import {libraryTileBox,} from "@/cut/components/LibraryCard";import { lutFileName } from "@/cut/lib/library";+import type { ImportPosterFrame } from "@/cut/lib/importPoster";import { openLocalImport } from "@/cut/lib/localImportPreview";import { setLibraryImportShape, type LibraryArrival } from "@/cut/lib/libraryIntake";import { fileKind } from "@/cut/lib/media";@@ -32,6 +33,7 @@ export function LibraryImportCard({onRetry,onDismiss,onUse,+ opensOn = "doubleClick",}: {item: LibraryArrival;/** The finished card's area; unset, the tile fills its grid cell. */@@ -39,9 +41,14 @@ export function LibraryImportCard({onRetry: () => void;onDismiss: () => void;onUse?: () => void;+ /** The gesture that opens the viewer: a click on the library page, where a+ * click opens every card, and a double-click in the editor's panel. */+ opensOn?: "click" | "doubleClick";}) {const elapsed = useElapsed(item.error ? null : item.startedAt);const [previewUrl, setPreviewUrl] = useState<string>();+ // The clip's decoded frame and length while its poster holds them.+ const [frame, setFrame] = useState<ImportPosterFrame>();const file = item.file;const mediaType = item.mediaType;const font = mediaType === "font";@@ -49,8 +56,15 @@ export function LibraryImportCard({const canPreview = !!file && !!previewKind && !item.error;// Only media goes on a timeline; a font or LUT is used from its own menu.const use = canPreview ? onUse : undefined;+ // The viewer opens at the shape the tile measured, wearing the frame the+ // tile shows, so it looks like the finished asset from the first paint.const openPreview = () => {- if (file && previewKind) openLocalImport(file, previewKind);+ if (!file || !previewKind) return;+ openLocalImport(file, previewKind, {+ ...item.shape,+ duration: frame?.duration,+ poster: frame?.blob,+ });};const tileRef = useCallback((node: HTMLDivElement | null) => {@@ -90,7 +104,8 @@ export function LibraryImportCard({role={canPreview ? "button" : undefined}tabIndex={canPreview ? 0 : undefined}aria-label={canPreview ? `Preview ${item.name}` : undefined}- onDoubleClick={canPreview ? openPreview : undefined}+ onClick={canPreview && opensOn === "click" ? openPreview : undefined}+ onDoubleClick={canPreview && opensOn === "doubleClick" ? openPreview : undefined}onKeyDown={canPreview ? (event) => {if (event.target !== event.currentTarget) return;if (event.key !== "Enter" && event.key !== " ") return;@@ -150,7 +165,12 @@ export function LibraryImportCard({onLoad={(event) => measured({ width: event.currentTarget.naturalWidth, height: event.currentTarget.naturalHeight })}/>) : file && mediaType === "video" ? (- <ImportVideoPoster file={file} size={Math.ceil(Math.sqrt(area ?? LIBRARY_TILE_AREA))} onShape={measured} />+ <ImportVideoPoster+ file={file}+ size={Math.ceil(Math.sqrt(area ?? LIBRARY_TILE_AREA))}+ onShape={measured}+ onFrame={setFrame}+ />) : file && !mediaType ? (<div className="flex size-full flex-col items-center justify-center gap-2 text-muted-foreground"><FileIcon className="size-8" />diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex 7dd77e4c..ee857f70 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -882,6 +882,7 @@ export function LibraryView() {area={tile.pending.mediaType === "video" || tile.pending.mediaType === "image" ? TILE_AREA : audioArea}onRetry={() => retryPending(tile.pending)}onDismiss={() => dropPending(tile.pending.id)}+ opensOn="click"/>);const a = tile.asset;diff --git a/site/src/cut/lib/importPoster.ts b/site/src/cut/lib/importPoster.tsindex 3c06bbc4..f83559dc 100644--- a/site/src/cut/lib/importPoster.ts+++ b/site/src/cut/lib/importPoster.ts@@ -4,12 +4,15 @@ import { rasterCanvasToBlob } from "@/cut/lib/raster";// Visible import cards share one decoder slot, separate from the upload queue.let tail: Promise<unknown> = Promise.resolve();+/** A decoded frame of an arriving clip, with its display size and length. */+export type ImportPosterFrame = { blob: Blob; width: number; height: number; duration: number };+/** A frame from the file, with the clip's display size so its tile can take the finished asset's shape. */export function importPoster(file: Blob,size: number,signal: AbortSignal,-): Promise<{ blob: Blob; width: number; height: number }> {+): Promise<ImportPosterFrame> {const result = tail.then(async () => {signal.throwIfAborted();const input = openMedia(file);@@ -36,8 +39,12 @@ export function importPoster(signal.throwIfAborted();if (!frame) throw new Error("No readable video frame.");try {- const blob = await rasterCanvasToBlob(frame.canvas, "image/jpeg");- return { blob, width, height };+ // The length rides along so the viewer's transport reads it from the start.+ const [blob, duration] = await Promise.all([+ rasterCanvasToBlob(frame.canvas, "image/jpeg"),+ track.computeDuration(),+ ]);+ return { blob, width, height, duration };} finally {frame.canvas.width = 0;frame.canvas.height = 0;diff --git a/site/src/cut/lib/localImportPreview.test.ts b/site/src/cut/lib/localImportPreview.test.tsnew file mode 100644index 00000000..9b3bab22--- /dev/null+++ b/site/src/cut/lib/localImportPreview.test.ts@@ -0,0 +1,27 @@+import { describe, expect, test } from "bun:test";+import { useLightbox } from "@/cut/lib/lightbox";+import { openLocalImport } from "@/cut/lib/localImportPreview";++describe("openLocalImport", () => {+ // The viewer opens at the arriving clip's shape, wearing its frame and length.+ test("carries the tile's shape, length and frame", () => {+ const file = new File([new Uint8Array(8)], "IMG_5388.MOV", { type: "video/quicktime" });+ const poster = new Blob([new Uint8Array(4)], { type: "image/jpeg" });+ openLocalImport(file, "video", { width: 1080, height: 1920, duration: 21.8, poster });+ const item = useLightbox.getState().item;+ expect(item?.ratio).toBe(1080 / 1920);+ expect(item?.duration).toBe(21.8);+ expect(item?.poster).toStartWith("blob:");+ useLightbox.getState().close();+ });++ // Nothing measured yet leaves the viewer to learn the shape from the file.+ test("opens without a shape when the tile has none", () => {+ const file = new File([new Uint8Array(8)], "a.png", { type: "image/png" });+ openLocalImport(file, "image");+ const item = useLightbox.getState().item;+ expect(item?.ratio).toBeUndefined();+ expect(item?.poster).toBeUndefined();+ useLightbox.getState().close();+ });+});diff --git a/site/src/cut/lib/localImportPreview.ts b/site/src/cut/lib/localImportPreview.tsindex fbedbffe..a67ad3e1 100644--- a/site/src/cut/lib/localImportPreview.ts+++ b/site/src/cut/lib/localImportPreview.ts@@ -1,15 +1,26 @@import { forgetRegistered, registerBlobFile } from "@/cut/lib/backend/browser/registry";import { useLightbox, type LightboxItem } from "@/cut/lib/lightbox";+/** What the arriving tile already knows about the file, so the viewer opens+ * at the media's own shape with its frame showing. */+export type LocalImportFacts = { width?: number; height?: number; duration?: number; poster?: Blob };+/** The viewer owns these local bytes until it closes or opens another item. */-export function openLocalImport(file: File, kind: "video" | "image" | "audio") {+export function openLocalImport(file: File, kind: "video" | "image" | "audio", facts: LocalImportFacts = {}) {const path = `pending-preview/${crypto.randomUUID()}`;const src = registerBlobFile(path, file);- const item: LightboxItem = { kind, src, name: file.name, prompt: "", assetId: null, bare: true };+ const poster = facts.poster && URL.createObjectURL(facts.poster);+ const item: LightboxItem = {+ kind, src, name: file.name, prompt: "", assetId: null, bare: true,+ ...(facts.width && facts.height ? { ratio: facts.width / facts.height } : {}),+ ...(facts.duration ? { duration: facts.duration } : {}),+ ...(poster ? { poster } : {}),+ };useLightbox.getState().open(item);const unsubscribe = useLightbox.subscribe((state) => {if (state.item === item) return;unsubscribe();forgetRegistered(path);+ if (poster) URL.revokeObjectURL(poster);});}