Open source CapCut alternative.
TypeScript
feat(cut): file library templates into folders [rebuild]
David Le05c43d389c5f
diff --git a/site/src/cut/components/LibraryView.tsx b/site/src/cut/components/LibraryView.tsxindex fe400bba..82529b45 100644--- a/site/src/cut/components/LibraryView.tsx+++ b/site/src/cut/components/LibraryView.tsx@@ -39,7 +39,7 @@ import {fetchLibrary,importUrlToLibrary,libraryMediaUrl,- moveLibraryAsset,+ moveLibraryItem,renameLibraryFolder,renameTemplate,uploadToLibrary,@@ -109,7 +109,7 @@ export function LibraryView() {try {const asset = await uploadToLibrary(file);if (openFolder) {- await moveLibraryAsset(asset.id, openFolder).catch(() => {});+ await moveLibraryItem(asset.id, openFolder).catch(() => {});asset.folderId = openFolder;}setAssets((prev) => [asset, ...(prev ?? [])]);@@ -130,7 +130,7 @@ export function LibraryView() {const imported = await importUrlToLibrary(value);if (openFolder) {for (const asset of imported) {- await moveLibraryAsset(asset.id, openFolder).catch(() => {});+ await moveLibraryItem(asset.id, openFolder).catch(() => {});asset.folderId = openFolder;}}@@ -163,12 +163,13 @@ export function LibraryView() {router.push(homeHref(base, "library", id));};- const moveAssets = async (ids: string[], folderId: string | null) => {+ const moveItems = async (ids: string[], folderId: string | null) => {if (ids.length === 0) return;const idset = new Set(ids);setAssets((prev) => (prev ?? []).map((a) => (idset.has(a.id) ? { ...a, folderId } : a)));+ setTemplates((prev) => prev.map((t) => (idset.has(t.id) ? { ...t, folderId } : t)));setSelected(new Set());- await Promise.all(ids.map((id) => moveLibraryAsset(id, folderId))).catch(() => void reload());+ await Promise.all(ids.map((id) => moveLibraryItem(id, folderId))).catch(() => void reload());};// Carry the current selection (or just this card) as a folder-move payload,@@ -191,6 +192,7 @@ export function LibraryView() {const all = assets ?? [];const shown = all.filter((a) => (a.folderId ?? null) === openFolder);+ const shownTemplates = templates.filter((t) => (t.folderId ?? null) === openFolder);const openFolderName = folders.find((f) => f.id === openFolder)?.name;const hasContent = all.length > 0 || folders.length > 0 || templates.length > 0;@@ -205,7 +207,7 @@ export function LibraryView() {name={openFolderName ?? "Folder"}mime={LIBRARY_MOVE_MIME}onBack={() => gotoFolder(null)}- onDropOut={(ids) => void moveAssets(ids, null)}+ onDropOut={(ids) => void moveItems(ids, null)}/>)}<div className="flex items-center gap-2">@@ -240,7 +242,11 @@ export function LibraryView() {mime={LIBRARY_MOVE_MIME}creating={folderCreating}onCreatingChange={setFolderCreating}- statOf={(id) => ({ count: all.filter((a) => (a.folderId ?? null) === id).length })}+ statOf={(id) => ({+ count:+ all.filter((a) => (a.folderId ?? null) === id).length ++ templates.filter((t) => (t.folderId ?? null) === id).length,+ })}onOpen={gotoFolder}onCreate={async (name) => {const f = await createLibraryFolder(name);@@ -255,20 +261,28 @@ export function LibraryView() {setAssets((prev) =>(prev ?? []).map((a) => (a.folderId === id ? { ...a, folderId: null } : a)));+ setTemplates((prev) =>+ prev.map((t) => (t.folderId === id ? { ...t, folderId: null } : t))+ );if (openFolder === id) router.replace(homeHref(base, "library"));await deleteLibraryFolder(id).catch(() => void reload());}}- onDropIds={(ids, fid) => void moveAssets(ids, fid)}+ onDropIds={(ids, fid) => void moveItems(ids, fid)}/>) : null}- {openFolder === null && templates.length > 0 && (+ {shownTemplates.length > 0 && (<div className="mb-6 grid grid-cols-[repeat(auto-fill,minmax(240px,1fr))] gap-3">- {templates.map((t) => (+ {shownTemplates.map((t) => (<TemplateCardkey={t.id}template={t}mediaSrc={libraryMediaUrl}+ dragScope="library"+ onDragStartExtra={(e) => {+ e.dataTransfer.setData(LIBRARY_MOVE_MIME, JSON.stringify([t.id]));+ e.dataTransfer.effectAllowed = "copyMove";+ }}onRename={(name) => void renameTpl(t.id, name)}onDelete={() => void removeTpl(t.id)}/>diff --git a/site/src/cut/components/SidePanel.tsx b/site/src/cut/components/SidePanel.tsxindex 8d82e7e7..9f08c2a9 100644--- a/site/src/cut/components/SidePanel.tsx+++ b/site/src/cut/components/SidePanel.tsx@@ -42,7 +42,7 @@ import {importLibraryAsset,importTemplateToProject,libraryMediaUrl,- moveLibraryAsset,+ moveLibraryItem,renameLibraryFolder,renameTemplate,saveAssetToLibrary,@@ -769,9 +769,10 @@ function LibraryPanel({ projectId }: { projectId: string }) {}};- const move = async (assetId: string, folderId: string | null) => {- setAssets((prev) => (prev ?? []).map((a) => (a.id === assetId ? { ...a, folderId } : a)));- await moveLibraryAsset(assetId, folderId).catch(() => void reload());+ const move = async (id: string, folderId: string | null) => {+ setAssets((prev) => (prev ?? []).map((a) => (a.id === id ? { ...a, folderId } : a)));+ setTemplates((prev) => prev.map((t) => (t.id === id ? { ...t, folderId } : t)));+ await moveLibraryItem(id, folderId).catch(() => void reload());};// Let a clip be dragged onto a folder tile to file it (alongside the timeline@@ -784,6 +785,7 @@ function LibraryPanel({ projectId }: { projectId: string }) {const all = assets ?? [];const shown = all.filter((a) => (a.folderId ?? null) === openFolder);+ const shownTemplates = templates.filter((t) => (t.folderId ?? null) === openFolder);const openFolderName = folders.find((f) => f.id === openFolder)?.name;return (@@ -802,15 +804,19 @@ function LibraryPanel({ projectId }: { projectId: string }) {) : (<PanelHead title="Library" />)}- {templates.length > 0 && (+ {shownTemplates.length > 0 && (<div className="shrink-0 px-3.5 pb-3"><div className="flex flex-col gap-1.5">- {templates.map((t) => (+ {shownTemplates.map((t) => (<TemplateCardkey={t.id}template={t}mediaSrc={libraryMediaUrl}dragScope="library"+ onDragStartExtra={(e) => {+ e.dataTransfer.setData(LIBRARY_MOVE_MIME, JSON.stringify([t.id]));+ e.dataTransfer.effectAllowed = "copyMove";+ }}addTitle="Add to this project"onAdd={() => void addTemplateToProject(projectId, t)}onRename={(name) => void commitTemplateRename(t.id, name)}@@ -835,7 +841,11 @@ function LibraryPanel({ projectId }: { projectId: string }) {<FolderShelffolders={folders}mime={LIBRARY_MOVE_MIME}- statOf={(id) => ({ count: all.filter((a) => (a.folderId ?? null) === id).length })}+ statOf={(id) => ({+ count:+ all.filter((a) => (a.folderId ?? null) === id).length ++ templates.filter((t) => (t.folderId ?? null) === id).length,+ })}onOpen={(id) => setOpenFolder(id)}onRename={async (id, name) => {setFolders((prev) => prev.map((f) => (f.id === id ? { ...f, name } : f)));@@ -846,6 +856,9 @@ function LibraryPanel({ projectId }: { projectId: string }) {setAssets((prev) =>(prev ?? []).map((a) => (a.folderId === id ? { ...a, folderId: null } : a)));+ setTemplates((prev) =>+ prev.map((t) => (t.folderId === id ? { ...t, folderId: null } : t))+ );if (openFolder === id) setOpenFolder(null);await deleteLibraryFolder(id).catch(() => void reload());}}@@ -857,7 +870,7 @@ function LibraryPanel({ projectId }: { projectId: string }) {const asset = useEditor.getState().assets.find((a) => a.id === ref.id);if (!asset) return;void saveAssetToLibrary(projectId, asset)- .then((saved) => moveLibraryAsset(saved.id, fid))+ .then((saved) => moveLibraryItem(saved.id, fid)).then(() => void reload()).catch(() => {});}}@@ -872,7 +885,9 @@ function LibraryPanel({ projectId }: { projectId: string }) {// At the root, filed-away assets, folders, and templates all count as// content — "No items" is only for a truly empty library.openFolder !== null ? (- <div className="px-3.5 py-6 text-center text-xs text-muted-foreground">Empty folder</div>+ shownTemplates.length === 0 ? (+ <div className="px-3.5 py-6 text-center text-xs text-muted-foreground">Empty folder</div>+ ) : null) : all.length === 0 && folders.length === 0 && templates.length === 0 ? (<div className="px-3.5 py-6 text-center text-xs text-muted-foreground">No items</div>) : nulldiff --git a/site/src/cut/components/TemplateCard.tsx b/site/src/cut/components/TemplateCard.tsxindex c0405508..ae5b7969 100644--- a/site/src/cut/components/TemplateCard.tsx+++ b/site/src/cut/components/TemplateCard.tsx@@ -50,6 +50,7 @@ export function TemplateCard({onRename,onDelete,onRefDrop,+ onDragStartExtra,extraMenu,}: {template: LibraryTemplate;@@ -65,6 +66,8 @@ export function TemplateCard({/** When set, the card accepts media drops (cards and timeline clips) —* filter by `ref.scope` and append the item to the template. */onRefDrop?: (ref: AssetRef) => void;+ /** Extra drag payload (e.g. a folder-move id) set alongside the template drag. */+ onDragStartExtra?: (e: React.DragEvent) => void;/** Extra menu items rendered between Rename and Delete. */extraMenu?: React.ReactNode;}) {@@ -133,6 +136,7 @@ export function TemplateCard({onDragStart={(e) => {if (!dragScope) return;setTemplateDragData(e, dragScope, t);+ onDragStartExtra?.(e);setCardDragImage(e, e.currentTarget);}}onDragEnd={clearAssetDrag}diff --git a/site/src/cut/lib/library.ts b/site/src/cut/lib/library.tsindex 5cba7608..d48461f3 100644--- a/site/src/cut/lib/library.ts+++ b/site/src/cut/lib/library.ts@@ -83,13 +83,14 @@ export async function deleteLibraryFolder(id: string): Promise<void> {if (!res.ok) throw new Error("Could not delete folder.");}-export async function moveLibraryAsset(assetId: string, folderId: string | null): Promise<void> {+/** File a library item — an asset or a template — into a folder (`null` ungroups). */+export async function moveLibraryItem(id: string, folderId: string | null): Promise<void> {const res = await apiFetch("/api/cut/library/move", {method: "POST",headers: { "Content-Type": "application/json" },- body: JSON.stringify({ assetId, folderId }),+ body: JSON.stringify({ id, folderId }),});- if (!res.ok) throw new Error("Could not move asset.");+ if (!res.ok) throw new Error("Could not move item.");}export async function uploadToLibrary(file: File): Promise<LibraryAsset> {diff --git a/site/src/cut/lib/types.ts b/site/src/cut/lib/types.tsindex 4eba86c8..17fb5b85 100644--- a/site/src/cut/lib/types.ts+++ b/site/src/cut/lib/types.ts@@ -270,6 +270,7 @@ export interface LibraryTemplate {id: string;name: string;addedAt: number;+ folderId?: string | null;duration: number;media: TemplateMedia[];layers: TemplateLayer[];diff --git a/site/src/cut/server/http/library.ts b/site/src/cut/server/http/library.tsindex f667c302..4b784d1f 100644--- a/site/src/cut/server/http/library.ts+++ b/site/src/cut/server/http/library.ts@@ -9,7 +9,7 @@ import {listFolders,listLibrary,listTemplates,- moveAsset,+ moveItem,removeAsset,renameFolder,renameTemplate,@@ -124,17 +124,17 @@ export const libraryApi = {}},- /** Move an asset into a folder (or `null` to ungroup). */+ /** Move an item — asset or template — into a folder (or `null` to ungroup). */async move(req: Request) {try {- const { assetId, folderId } = (await req.json()) as {- assetId: string;+ const { id, folderId } = (await req.json()) as {+ id: string;folderId: string | null;};- await moveAsset(assetId, folderId ?? null);+ await moveItem(id, folderId ?? null);return Response.json({ ok: true });} catch (e) {- return caught(e, "Could not move asset.");+ return caught(e, "Could not move item.");}},diff --git a/site/src/cut/server/library.ts b/site/src/cut/server/library.tsindex 6f1b58ad..0f32f7b1 100644--- a/site/src/cut/server/library.ts+++ b/site/src/cut/server/library.ts@@ -80,6 +80,7 @@ export interface LibraryTemplate {id: string;name: string;addedAt: number;+ folderId?: string | null;duration: number;media: TemplateMedia[];layers: TemplateLayer[];@@ -335,19 +336,22 @@ export async function renameFolder(id: string, name: string): Promise<LibraryFolexport async function deleteFolder(id: string) {await mutateIndex((idx) => {idx.folders = (idx.folders ?? []).filter((f) => f.id !== id);- // Assets in the folder fall back to ungrouped rather than vanishing.+ // Items in the folder fall back to ungrouped rather than vanishing.for (const a of idx.assets) if (a.folderId === id) a.folderId = null;+ for (const t of idx.templates ?? []) if (t.folderId === id) t.folderId = null;});}-export async function moveAsset(assetId: string, folderId: string | null) {+/** File a library item — an asset or a template — into a folder (`null` ungroups). */+export async function moveItem(id: string, folderId: string | null) {await mutateIndex((idx) => {- const asset = idx.assets.find((a) => a.id === assetId);- if (!asset) throw new Error("Library asset not found.");+ const item =+ idx.assets.find((a) => a.id === id) ?? (idx.templates ?? []).find((t) => t.id === id);+ if (!item) throw new Error("Library item not found.");if (folderId && !(idx.folders ?? []).some((f) => f.id === folderId)) {throw new Error("Folder not found.");}- asset.folderId = folderId;+ item.folderId = folderId;});}