Skip to content

The VS Code of video editing, built for agents.

TypeScript

Update project configuration handling and export logic

konstantin-pauluse821756676dd
diff --git a/apps/desktop/src/main-channels.ts b/apps/desktop/src/main-channels.ts
index 91f351b6..40ea7388 100644
--- a/apps/desktop/src/main-channels.ts
+++ b/apps/desktop/src/main-channels.ts
@@ -153,7 +153,7 @@ export type MainRequestMap = {
[MAIN_CHANNELS.PROJECTS_MANIFEST_WRITE]: { request: { dir: string; manifest: unknown }; response: void };
// The project's config: the `diffusion` field of its package.json, as
// parsed (null when absent). The renderer owns its shape; see
- // `projects/config` in the web app.
+ // `engine/project-config` in the web app.
[MAIN_CHANNELS.PROJECTS_CONFIG_READ]: { request: { dir: string }; response: unknown };
[MAIN_CHANNELS.PROJECTS_CONFIG_WRITE]: { request: { dir: string; config: unknown }; response: void };
// Project file system, for the asset library. `source` is project-relative
diff --git a/apps/web/src/components/sidebar-right/inspector/export.tsx b/apps/web/src/components/sidebar-right/inspector/export.tsx
index a80bae74..5ecb2ed6 100644
--- a/apps/web/src/components/sidebar-right/inspector/export.tsx
+++ b/apps/web/src/components/sidebar-right/inspector/export.tsx
@@ -65,12 +65,11 @@ function templateSettings(id: string): ProjectExportConfig | null {
}
/**
- * How the project is exported. Project configuration rather than part of
- * the composition: read from and written through the project config
- * (package.json, see `@/engine/project-config`). Shown on the scene since
- * that is what gets exported; per-scene settings would come as a per-scene
- * accessor on the config, falling back to the project's. The row and the
- * floating inspector both read the same accessor, so they never disagree.
+ * How the selected scene is exported. Project configuration rather than
+ * part of the composition: read from and written through the project config
+ * (package.json `diffusion.export.<scene id>`, see
+ * `@/engine/project-config`). The row and the floating inspector both read
+ * the same accessor, so they never disagree.
*/
export function ExportPanel(props: ExportPanelProps) {
const world = useWorld();
@@ -79,7 +78,7 @@ export function ExportPanel(props: ExportPanelProps) {
const [isInspectorOpen, setIsInspectorOpen] = createSignal(false);
- const settings = () => config()?.export() ?? undefined;
+ const settings = () => config()?.exportOf(entity()) ?? undefined;
const frameRate = useTrait(world, FrameRate);
const duration = useDerived(() => entity().get(Computed)?.duration ?? 0);
const durationInSeconds = createMemo(() => duration() / (frameRate()?.value ?? 30));
@@ -101,7 +100,7 @@ export function ExportPanel(props: ExportPanelProps) {
});
const write = (value: ProjectExportConfig | null) => {
- void config()?.setExport(value);
+ void config()?.setExport(entity(), value);
};
// Replaces the settings with a preset's, wholesale.
diff --git a/apps/web/src/engine/project-config.ts b/apps/web/src/engine/project-config.ts
index ee4b344f..c6eec410 100644
--- a/apps/web/src/engine/project-config.ts
+++ b/apps/web/src/engine/project-config.ts
@@ -3,26 +3,32 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// The project's config in the app: what is about the project rather than the
-// composition (how it is exported, for a start), kept in the `diffusion`
-// field of its package.json — the project record — rather than in the JSX,
-// which says what the composition is. Attached to the world for the project
-// on disk, like the library: the world holds the handle, the handle holds
-// the values as signals for the panels to read, and writes go to the file.
-// Nothing here is the runtime's business (a headless world is not exported
-// by its own settings; the encoder is handed its config), so no trait.
+// composition (how its scenes are exported, for a start), kept in the
+// `diffusion` field of its package.json — the project record — rather than
+// in the JSX, which says what the composition is. Attached to the world for
+// the project on disk, like the library: the world holds the handle, the
+// handle holds the values as signals for the panels to read, and writes go
+// to the file. Nothing here is the runtime's business (a headless world is
+// not exported by its own settings; the encoder is handed its config), so no
+// trait.
//
-// Project-wide today. Per-scene settings would come as a further key here
-// (e.g. `scenes: { [name]: { export } }`) and a per-scene accessor, falling
-// back to the project's.
+// Each setting is a map keyed by the scene's `id` in the JSX (see ID_ATTR in
+// @diffusionstudio/jsx): stamped once by the compiler where the author wrote
+// none and never rewritten, so it is the one durable name every element has.
+// A scene with no entry has nothing set up.
+//
+// { "export": { "intro": { "format": "mp4", ... } } }
import { createSignal } from 'solid-js';
+import { Source } from '@diffusionstudio/runtime';
+import { parseSource } from '@diffusionstudio/jsx';
import { useTrait, useWorld } from '@diffusionstudio/koota-solid';
import { readProjectConfig, writeProjectConfig } from '@/projects/host';
import { ProjectConfig as ProjectConfigTrait } from './traits';
import type { VideoCodec, AudioCodec } from 'mediabunny';
-import type { World } from 'koota';
+import type { Entity, World } from 'koota';
import type { Accessor } from 'solid-js';
/** The file the config lives in, relative to the project. */
@@ -46,7 +52,7 @@ export type ExportAudioSettings = {
};
/**
- * How the project is exported, in the shape the encoder takes its config
+ * How a scene is exported, in the shape the encoder takes its config
* (format + video + audio), so the value goes to it as it is. `template`
* names the editor preset the values came from — only a label, the fields
* are what the export reads.
@@ -58,6 +64,17 @@ export type ExportConfig = {
audio?: ExportAudioSettings;
};
+/**
+ * The key a scene's config sits under: its `id` in the JSX, read off the
+ * source stamp (`<file>:<id>`). Undefined before it has one (nothing
+ * mounted, or an element the compiler has yet to stamp).
+ */
+export function sceneConfigKey(scene: Entity): string | undefined {
+ const source = scene.get(Source)?.value;
+ const locator = source ? parseSource(source)?.locator : undefined;
+ return typeof locator === 'string' ? locator : undefined;
+}
+
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}
@@ -108,9 +125,20 @@ export function parseExportConfig(value: unknown): ExportConfig | undefined {
});
}
+/** The `export` field as read from the file: one entry per scene id, anything malformed left out. */
+function parseExports(value: unknown): Record<string, ExportConfig> {
+ if (!isRecord(value)) return {};
+ const exports: Record<string, ExportConfig> = {};
+ for (const [key, entry] of Object.entries(value)) {
+ const parsed = parseExportConfig(entry);
+ if (parsed) exports[key] = parsed;
+ }
+ return exports;
+}
+
/**
* The config of the project at `dir`. `load` reads the file; the setters
- * update the signal first and write the file after, so a panel sees its
+ * update the signals first and write the file after, so a panel sees its
* change at once. Fields the app does not know are carried through a write
* untouched.
*/
@@ -119,18 +147,13 @@ export class ProjectConfig {
private readonly dir: string;
private raw: Record<string, unknown> = {};
private disposed = false;
- private readonly exportSignal = createSignal<ExportConfig | null>(null);
+ private readonly exports = createSignal<Record<string, ExportConfig>>({});
public constructor(world: World, dir: string) {
this.world = world;
this.dir = dir;
}
- /** How the project is exported, or null until it is set up. Reactive. */
- public get export(): Accessor<ExportConfig | null> {
- return this.exportSignal[0];
- }
-
/** Reads the config from the project. */
public async load(): Promise<void> {
let value: unknown = null;
@@ -142,16 +165,35 @@ export class ProjectConfig {
if (this.disposed) return;
this.raw = isRecord(value) ? value : {};
- this.exportSignal[1](parseExportConfig(this.raw.export) ?? null);
+ this.exports[1](parseExports(this.raw.export));
+ }
+
+ /** How `scene` is exported, or null until it is set up. Reactive. */
+ public exportOf(scene: Entity): ExportConfig | null {
+ const key = sceneConfigKey(scene);
+ return (key && this.exports[0]()[key]) || null;
}
- /** Sets how the project is exported (null for none): here now, in the file after. */
- public async setExport(settings: ExportConfig | null): Promise<void> {
+ /**
+ * Sets how `scene` is exported (null for none): here now, in the file
+ * after. No-op for a scene without an id (nothing mounted).
+ */
+ public async setExport(scene: Entity, settings: ExportConfig | null): Promise<void> {
+ const key = sceneConfigKey(scene);
+ if (!key) {
+ console.warn('Cannot write export settings for a scene without an id');
+ return;
+ }
+
+ const exports = isRecord(this.raw.export) ? { ...this.raw.export } : {};
+ if (settings) exports[key] = exportToJson(settings);
+ else delete exports[key];
+
const next = { ...this.raw };
- if (settings) next.export = exportToJson(settings);
+ if (Object.keys(exports).length) next.export = exports;
else delete next.export;
this.raw = next;
- this.exportSignal[1](settings && exportToJson(settings));
+ this.exports[1](parseExports(next.export));
try {
await writeProjectConfig(this.dir, Object.keys(next).length ? next : null);