The VS Code of video editing, built for agents.
TypeScript
/* 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 { Show, createMemo, createSignal, onMount } from "solid-js";
import { Canvas } from "@/components/canvas";
import { leftSidebarWidth } from "@/agent-chat";
import { Timeline, Layers } from "@/components/timeline";
import { Soundboard, Inspector } from "@/components/sidebar-right";
import { EditorTitleBar, FloatingProjectHeader, SidebarLeft } from "@/components/sidebar-left";
import { useLayout, MIN_TIMELINE_HEIGHT } from "@/context/layout";
import { useEditorApi } from "@/dapi";
import { RULER_HEIGHT } from "@/engine/timeline";
import { createEffect, onCleanup, untrack } from 'solid-js';
import { toast } from 'somoto';
import { useWorld } from '@diffusionstudio/koota-solid';
import { mount } from '@diffusionstudio/reconciler';
import { getDocumentEditor } from '@/engine/editor';
import { getEditHistory } from '@/engine/history';
import { setInspectEntries } from '@/engine/inspect';
import { attachLibrary, isLibraryFile } from '@/engine/library';
import { attachProjectConfig, isProjectConfigFile } from '@/engine/project-config';
import { loadProjectBundle, rememberProjectBundle } from '@/lib/db';
import { isCacheFile } from '@diffusionstudio/assets';
import { createEditWriter } from '@/projects/edits';
import { compileProject, isWindowsDesktop, refreshProject, 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';
const MIN_CANVAS_HEIGHT = 200;
const INSPECTOR_WIDTH = 264;
export function EditorPage() {
const { uiVisible, timelineMinimized, timelineHeight, setTimelineHeight } = useLayout();
const { isDesktop, isFullscreen } = useEditorApi();
const [resizing, setResizing] = createSignal(false);
const project = useProject();
const world = useWorld();
const engine = useEngineContext();
// Everything below holds the folder's path — the watcher, the library, the
// writer — and is torn down with the editor.
createEffect(() => {
const dir = project.dir();
if (!dir) return;
let mounted: Mount | undefined;
let mountedCode: string | undefined;
let writer: EditWriter | undefined;
let unlisten: (() => void) | undefined;
let disposed = false;
let generation = 0;
// The library first: a mounted project's `src` values name its assets.
const library = attachLibrary(world, dir);
// The project's own settings (package.json `diffusion`), next to the scene.
const config = attachProjectConfig(world, dir);
const unmount = (): void => {
// Before the entities go: what the editor changed is still owed to the
// file, whatever happens to the scene that showed it.
unlisten?.();
unlisten = undefined;
writer?.dispose();
writer = undefined;
mounted?.dispose();
mounted = undefined;
mountedCode = undefined;
// The entries hold the dead mount's signals; the inspector must not.
setInspectEntries(world, []);
};
/** Puts `code` on the stage, unless it is what is there already. */
const applyBundle = (code: string): void => {
if (code === mountedCode) return;
// The old render goes first: there is only one stage per world.
unmount();
mounted = mount(code, world);
mountedCode = code;
// The `@inspect` variables this mount declared, for the inspector.
setInspectEntries(world, mounted.inspect);
// The rendered scene knows which element every entity came from, so
// from here on an edit in the editor can find its way back.
writer = createEditWriter(dir, world);
const editor = getDocumentEditor(world);
unlisten = editor.onEdit((edit) => writer?.push(edit));
// A mount comes from the file: edits recorded against the document it
// replaced cannot be replayed against this one.
getEditHistory(world).reset();
};
const loadProject = async (): Promise<void> => {
const current = ++generation;
const compiling = compileProject(dir);
const loading = library.load();
// First open only: the bundle the last session mounted, straight from
// the app's database, goes on the stage while the compile chews
// through the sources — unless the compile wins the race outright. A
// bundle the sources have outgrown can fail against today's assets;
// the compile that is already running replaces it either way.
if (current === 1) {
// Neither arm may reject: the loser would be an unhandled rejection,
// and the compile's real failure is dealt with below.
const cached = await Promise.race([
Promise.all([loadProjectBundle(untrack(project.id)), loading])
.then(([code]) => code, () => null),
compiling.then(() => null, () => null),
]);
if (disposed || current !== generation) return;
if (cached && mountedCode === undefined) {
try {
applyBundle(cached);
} catch {
// The compile lands next, with a toast of its own if it must.
}
}
}
const [result] = await Promise.all([compiling, loading]);
if (disposed || current !== generation) return;
// A broken edit keeps the last good render on the canvas.
if (!result.ok) {
console.error('[projects] compile failed:', result.error);
toast.error('Project failed to compile', { description: result.error });
return;
}
try {
applyBundle(result.code);
// What an export renders a second time, and the next open's head
// start (see `rememberProjectBundle`) — recorded only once it has
// actually mounted, so the record never runs ahead of the canvas.
rememberProjectBundle(untrack(project.id), result.code).catch((error) =>
console.error('[projects] could not save the bundle', error));
} catch (error) {
console.error('[projects] render failed:', error);
toast.error('Project failed to render', { description: (error as Error).message });
}
};
const load = (): void => {
loadProject().catch((error) => {
console.error('[projects] load failed:', error);
toast.error('Project failed to load', { description: (error as Error).message });
});
};
load();
// A burst arrives as the whole set of files it touched, so a checkout that
// rewrites the library and the sources at once reloads both — reading only
// the last path of a burst would answer for one of them and drop the rest.
const unwatch = watchProject(dir, (paths) => {
const changed = paths.filter((path) => !isCacheFile(path));
if (changed.some(isLibraryFile)) library.load();
const source = changed.filter((path) => !isLibraryFile(path));
if (!source.length) return;
// package.json is the config and the record (`main`, `displayName`)
// in one, so a hand edit to it reloads both; the app's own config
// writes never reach here (main keeps them from the watcher).
if (source.some(isProjectConfigFile)) {
config.load();
project.refresh();
}
load();
});
onCleanup(() => {
disposed = true;
captureProjectCover(dir, engine.snapshot());
refreshProject(dir);
unwatch();
unmount();
config.dispose();
library.dispose();
});
});
// The left column follows the sidebar's tab (264 px on Assets, 340 px on
// Chat) and animates between the two — except on load, where the stored
// tab is read before first paint and the transition only comes on after
// the first frame. Toggling `uiVisible` changes the track count, which
// Chromium does not interpolate, so that still snaps as before.
const [animateColumns, setAnimateColumns] = createSignal(false);
onMount(() => {
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) return;
requestAnimationFrame(() => requestAnimationFrame(() => setAnimateColumns(true)));
});
const timelineStyles = createMemo(() => {
if (!uiVisible()) return;
const height = timelineMinimized() ? RULER_HEIGHT : timelineHeight();
return {
'grid-template-columns': `${leftSidebarWidth()}px 1px 1fr 1px ${INSPECTOR_WIDTH}px`,
'grid-template-rows': `1fr 1px ${height}px`,
...(animateColumns() ? { transition: 'grid-template-columns 200ms ease-out' } : {}),
};
});
const handleResizeStart = (e: PointerEvent) => {
e.preventDefault();
e.stopPropagation();
const startY = e.clientY;
const startHeight = timelineHeight();
setResizing(true);
const handleMove = (ev: PointerEvent) => {
const deltaY = startY - ev.clientY;
const maxHeight = Math.max(
MIN_TIMELINE_HEIGHT,
window.innerHeight - MIN_CANVAS_HEIGHT - 1,
);
const next = Math.max(MIN_TIMELINE_HEIGHT, Math.min(maxHeight, startHeight + deltaY));
setTimelineHeight(next);
};
const handleEnd = () => {
setResizing(false);
document.removeEventListener('pointermove', handleMove);
document.removeEventListener('pointerup', handleEnd);
};
document.addEventListener('pointermove', handleMove);
document.addEventListener('pointerup', handleEnd);
};
return (
<div
class="bg-sidebar h-screen w-full overflow-hidden grid pt-(--titlebar-height)"
classList={{
'grid-cols-[1fr]': !uiVisible(),
'grid-rows-[1fr]': !uiVisible(),
}}
style={timelineStyles()}
>
<EditorTitleBar leftWidth={leftSidebarWidth() + 1} controlsWidth={INSPECTOR_WIDTH + 1} />
<Show when={isDesktop && !isWindowsDesktop() && !isFullscreen()}>
<div class="fixed top-0 left-0 right-0 h-10 z-20" style="-webkit-app-region: drag;" />
</Show>
<Show when={uiVisible()}>
<SidebarLeft />
<div class="bg-border-strong" />
</Show>
<Canvas />
<Show when={uiVisible()}>
<div class="bg-border-strong" />
<Inspector />
</Show>
<Show when={uiVisible()}>
<div class="col-span-full bg-border-strong relative">
<Show when={!timelineMinimized()}>
<div
class="absolute left-0 right-0 -top-px h-0.75 z-10 cursor-ns-resize group"
onPointerDown={handleResizeStart}
>
<div
class="absolute left-0 right-0 top-px h-px transition-colors group-hover:bg-primary"
classList={{ 'bg-primary': resizing() }}
/>
</div>
</Show>
</div>
</Show>
<Show when={uiVisible()}>
<Layers />
<div class="bg-border-strong" />
</Show>
<Show when={uiVisible()}>
<Timeline />
</Show>
<Show when={uiVisible()}>
<div class="bg-border-strong" />
<Show when={!timelineMinimized()}>
<Soundboard />
</Show>
</Show>
{/* The Windows title bar stays up with the UI hidden and offers the same. */}
<Show when={!uiVisible() && !isWindowsDesktop()}>
<FloatingProjectHeader />
</Show>
</div>
);
}