The VS Code of video editing, built for agents.
TypeScript
Implement project cover capture and management functionality
konstantin-paulusd5ff75f4319d
diff --git a/apps/web/src/components/dashboard/projects-view.tsx b/apps/web/src/components/dashboard/projects-view.tsxindex bb6a0797..b9632a5f 100644--- a/apps/web/src/components/dashboard/projects-view.tsx+++ b/apps/web/src/components/dashboard/projects-view.tsx@@ -52,20 +52,15 @@ import {isDesktop,listProjects,projectKey,+ projectCoverKey,projectsRoot,+ readProjectCover,renameProject,type ProjectInfo,} from "@/projects";import type { ProjectSortOption } from "./types";-// Projects live on disk under a user-picked root (see @/projects); their-// package.json is the record. Thumbnails have no on-disk backing yet: the UI-// stays, the lookup is a no-op until the desktop host grows it.-async function getProjectThumbnail(_id: string): Promise<Blob | undefined> {- return undefined;-}-export function DashboardProjectsView() {const navigate = useNavigate();const [search, setSearch] = createSignal("");@@ -298,7 +293,7 @@ export function DashboardProjectsView() {onDelete={() => setPendingDelete(project)}><DashboardCardPreview>- <ProjectThumbnail id={projectKey(project)} />+ <ProjectThumbnail dir={project.dir} /></DashboardCardPreview><div class="flex flex-col gap-1 px-2"><div class="relative h-4 w-full">@@ -388,17 +383,22 @@ export function DashboardProjectsView() {);}-function ProjectThumbnail(props: { id: string }) {- const [thumbnail] = createResource(() => props.id, getProjectThumbnail);- const url = createMemo(() => {- const blob = thumbnail();- if (!blob) return null;- return URL.createObjectURL(blob);- });+function ProjectThumbnail(props: { dir: string }) {+ const [cover] = createResource(+ () => projectCoverKey(props.dir),+ () => readProjectCover(props.dir),+ );++ // The URL the last cover was under is released as this one takes its place.+ const url = createMemo<string | null>((previous) => {+ if (previous) URL.revokeObjectURL(previous);+ const blob = cover();+ return blob ? URL.createObjectURL(blob) : null;+ }, null);onCleanup(() => {- const u = url();- if (u) URL.revokeObjectURL(u);+ const current = url();+ if (current) URL.revokeObjectURL(current);});return (diff --git a/apps/web/src/engine/create-engine.ts b/apps/web/src/engine/create-engine.tsindex b9c034e6..531443f5 100644--- a/apps/web/src/engine/create-engine.ts+++ b/apps/web/src/engine/create-engine.ts@@ -2,7 +2,7 @@* License, v. 2.0. If a copy of the MPL was not distributed with this* file, You can obtain one at http://mozilla.org/MPL/2.0/. */-import { renderSystem, transformSystem, playbackSystem, motionSystem, AudioEngine, createRuntimeWorld, RenderSurface, Time, ChildOf, syncInteractiveState } from '@diffusionstudio/runtime';+import { renderSystem, transformSystem, playbackSystem, motionSystem, AudioEngine, createRuntimeWorld, Mode, RenderSurface, Time, ChildOf, syncInteractiveState } from '@diffusionstudio/runtime';import { hudSystem } from './hud';import { createSignal, type Accessor, type Setter } from 'solid-js';import { AssetSelection, Hud, Keys, Pointer, PointerEvents, ProjectConfig, SnapLines } from './traits';@@ -223,6 +223,23 @@ class Engine {this.resizeObserver.observe(canvas.parentElement!);}+ public snapshot(): Promise<Blob | null> {+ const canvas = this.canvas;+ if (!canvas?.width || !canvas.height) return Promise.resolve(null);++ const mode = this.world.get(Mode)?.value ?? 'realtime';+ this.world.set(Mode, { value: 'offline-video' });+ renderSystem(this.world);++ const encoded = new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, 'image/png'));++ this.world.set(Mode, { value: mode });+ renderSystem(this.world);+ hudSystem(this.world);++ return encoded;+ }+public dispose(): void {this.stop();if (this.ownsAudioContext) {diff --git a/apps/web/src/pages/editor.tsx b/apps/web/src/pages/editor.tsxindex c6b75ae2..d834cf84 100644--- a/apps/web/src/pages/editor.tsx+++ b/apps/web/src/pages/editor.tsx@@ -20,7 +20,9 @@ import { attachProjectConfig, isProjectConfigFile } from '@/engine/project-confiimport { isCacheFile } from '@diffusionstudio/assets';import { createEditWriter } from '@/projects/edits';import { compileProject, watchProject } from '@/projects/host';+import { captureProjectCover } from '@/projects/cover';import { useProject } from "@/context/project";+import { useEngineContext } from "@/engine";import type { Mount } from '@diffusionstudio/reconciler';import type { EditWriter } from '@/projects/edits';@@ -33,6 +35,7 @@ export function EditorPage() {const [resizing, setResizing] = createSignal(false);const project = useProject();const world = useWorld();+ const engine = useEngineContext();// Keyed on the folder, not the project: a rename moves it, and everything// below holds a path — the watcher, the library, the writer — so all of it@@ -108,6 +111,7 @@ export function EditorPage() {onCleanup(() => {disposed = true;+ captureProjectCover(dir, engine.snapshot());unwatch();unmount();config.dispose();diff --git a/apps/web/src/projects/cover.ts b/apps/web/src/projects/cover.tsnew file mode 100644index 00000000..135443a9--- /dev/null+++ b/apps/web/src/projects/cover.ts@@ -0,0 +1,101 @@+/* This Source Code Form is subject to the terms of the Mozilla Public+ * License, v. 2.0. If a copy of the MPL was not distributed with this+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */++import { createSignal } from 'solid-js';++import { CACHE_DIR } from '@diffusionstudio/assets';++import { createProjectFS } from './fs';+import { getProject, isDesktop } from './host';++/** Where the snapshot lives inside the project folder. */+export const COVER_PATH = `${CACHE_DIR}/cover.png`;++/** How many covers are kept in hand — enough for the projects one session closes. */+const RECENT_LIMIT = 8;++/** Captures under way, by project folder. */+const pending = new Map<string, Promise<void>>();+/** Covers taken this session, by project folder: the file, without the trip to disk. */+const recent = new Map<string, Blob>();+/** How often each project's cover has been replaced this session. */+const [revisions, setRevisions] = createSignal<Record<string, number>>({});++/**+ * Takes the cover of the project in `dir` from `snapshot` and writes it.+ * Call it as the project closes, with the snapshot already under way: the+ * capture is registered synchronously, so a read that follows waits for this+ * cover rather than answering with the last one.+ */+export function captureProjectCover(dir: string, snapshot: Promise<Blob | null>): Promise<void> {+ if (!dir || !isDesktop()) return Promise.resolve();++ // Until the new one arrives there is no cover in hand: what is there+ // shows the project as it was before this session's edits.+ recent.delete(dir);++ const done = capture(dir, snapshot)+ .catch((error) => console.warn('[projects] could not write the project cover', error))+ .finally(() => {+ if (pending.get(dir) === done) pending.delete(dir);+ });++ pending.set(dir, done);+ return done;+}++/**+ * What to key a read of the project's cover on: a value that changes once a+ * new cover has been taken for `dir`, and stays put when another project's+ * cover is replaced. Read it as a resource's source and the read runs again+ * when the cover lands — which is what a card asking for the cover of a+ * project still closing needs.+ */+export function projectCoverKey(dir: string): string {+ return `${revisions()[dir] ?? 0}:${dir}`;+}++/** The cover of the project in `dir`, or null while it has none. */+export async function readProjectCover(dir: string): Promise<Blob | null> {+ if (!dir || !isDesktop()) return null;++ if (!recent.has(dir)) await pending.get(dir);++ const captured = recent.get(dir);+ if (captured) return captured;++ try {+ const fs = createProjectFS(dir);+ // Asked for before it is read: a missing cover is the normal case+ // (every project that has not been opened yet), not a failed read.+ if (!(await fs.stat(COVER_PATH))) return null;+ return await fs.file(COVER_PATH);+ } catch (error) {+ console.warn('[projects] could not read the project cover', error);+ return null;+ }+}++async function capture(dir: string, snapshot: Promise<Blob | null>): Promise<void> {+ const blob = await snapshot;+ if (!blob) return;++ // A project closed by deleting it is already in the trash, and writing+ // would put its folder back — holding nothing but this one file.+ if (!(await getProject(dir))) return;++ await createProjectFS(dir).write(COVER_PATH, blob);++ remember(dir, blob);+ setRevisions((current) => ({ ...current, [dir]: (current[dir] ?? 0) + 1 }));+}++function remember(dir: string, blob: Blob): void {+ recent.set(dir, blob);+ // Insertion order, so the cover taken longest ago is the first to go.+ for (const key of recent.keys()) {+ if (recent.size <= RECENT_LIMIT) break;+ recent.delete(key);+ }+}diff --git a/apps/web/src/projects/index.ts b/apps/web/src/projects/index.tsindex bf37a731..b61524d0 100644--- a/apps/web/src/projects/index.ts+++ b/apps/web/src/projects/index.ts@@ -3,3 +3,4 @@* file, You can obtain one at http://mozilla.org/MPL/2.0/. */export * from './host';+export * from './cover';