Refactor project components and improve rendering logic

- Renamed the default export function in the starter template from Scene to Project for clarity.
- Updated StageProps interface to include a background property for customizable canvas color.
- Removed redundant project-related UI elements from the dashboard view to streamline the user experience.
- Simplified the ProjectLoader component by eliminating unused camera fitting logic and related comments.
- Enhanced the RuntimeDocument class to better manage the stage and rect entities, including background handling.
This commit is contained in:
konstantin-paulus 2026-08-15 22:41:44 +02:00
parent 7273ed2141
commit 2f5cc9d945
6 changed files with 56 additions and 119 deletions

View File

@ -178,9 +178,9 @@ const TSCONFIG = `{
}
`;
const STARTER = `export default function Scene() {
const STARTER = `export default function Project() {
return (
<stage width={1920} height={1080}>
<stage background="#161616">
<rect x={120} y={120} width={480} height={320} fill="#6366f1" />
</stage>
)
@ -192,15 +192,18 @@ const STARTER = `export default function Scene() {
const TYPES = `// Generated by Diffusion Studio. Types for the tags rendered on the canvas.
// Edits here are overwritten.
/** The infinite canvas. */
interface StageProps {
/** Canvas color, any CSS color. */
background?: string
children?: unknown
}
interface RectProps {
x?: number
y?: number
width?: number
height?: number
children?: unknown
}
interface RectProps extends StageProps {
fill?: string
}

View File

@ -329,11 +329,6 @@ export function DashboardProjectsView() {
</ContextMenu>
)}
</For>
<Show when={projectsRoot() && !projects.loading && (projects()?.length ?? 0) === 0}>
<p class="w-full px-2 pt-2 text-xs text-muted-foreground">
No projects in this folder yet. A project is a folder with an index.tsx.
</p>
</Show>
</DashboardViewSection>
</DashboardSearchPanel>
);

View File

@ -6,12 +6,10 @@ import { createEffect, onCleanup } from 'solid-js';
import { toast } from 'somoto';
import { useWorld } from '@diffusionstudio/koota-solid';
import { mount } from '@diffusionstudio/reconciler';
import { ActiveScene, Camera, Position, RenderSurface, Size } from '@diffusionstudio/runtime';
import { compileProject, projectDir, watchProject } from './host';
import type { Mount } from '@diffusionstudio/reconciler';
import type { World } from 'koota';
export interface ProjectLoaderProps {
/** Project folder name under the projects root. */
@ -33,8 +31,6 @@ export function ProjectLoader(props: ProjectLoaderProps): null {
let mounted: Mount | undefined;
let disposed = false;
let generation = 0;
// Only the first mount of a project frames the camera; reloads keep the view.
let fitted = false;
const unmount = (): void => {
mounted?.dispose();
@ -52,16 +48,13 @@ export function ProjectLoader(props: ProjectLoaderProps): null {
return;
}
// The old render goes first: there is only one active stage per world.
// The old render goes first: there is only one stage per world.
unmount();
try {
mounted = mount(result.code, world);
} catch (error) {
toast.error('Project failed to render', { description: (error as Error).message });
return;
}
if (!fitted) fitted = await fitCameraToStage(world);
};
void load();
@ -76,50 +69,3 @@ export function ProjectLoader(props: ProjectLoaderProps): null {
return null;
}
const FIT_PADDING = 48;
const FIT_RETRIES = 30;
/**
* Centers the active stage in the canvas. The canvas is sized by a resize
* observer, so right after mount it may still be 0x0: retry a few frames.
* Resolves true once the camera was set (or there is nothing to fit).
*/
function fitCameraToStage(world: World): Promise<boolean> {
return new Promise((resolve) => {
let attempts = 0;
const attempt = (): void => {
const stage = world.get(ActiveScene)?.entity;
if (!stage) return resolve(true);
const surface = world.get(RenderSurface);
const canvas = surface?.canvas;
const resolution = surface?.resolution || 1;
const width = canvas ? canvas.width / resolution : 0;
const height = canvas ? canvas.height / resolution : 0;
if (!width || !height) {
if (++attempts >= FIT_RETRIES) return resolve(false);
requestAnimationFrame(attempt);
return;
}
const size = stage.get(Size) ?? { width: 1, height: 1 };
const position = stage.get(Position) ?? { x: 0, y: 0 };
const scale = Math.min(
(width - FIT_PADDING * 2) / Math.max(1, size.width),
(height - FIT_PADDING * 2) / Math.max(1, size.height),
);
world.set(Camera, {
a: scale,
b: 0,
c: 0,
d: scale,
e: (width - size.width * scale) / 2 - position.x * scale,
f: (height - size.height * scale) / 2 - position.y * scale,
});
resolve(true);
};
attempt();
});
}

View File

@ -1,21 +0,0 @@
# @diffusionstudio/reconciler
Renders a project's JSX into a `@diffusionstudio/runtime` world. Sits between the compiler (which turns a project folder into a CommonJS bundle with `babel-preset-solid` in universal mode, `moduleName: "@diffusionstudio/jsx"`) and the runtime (which knows nothing about JSX):
```
project folder → compile (esbuild + babel) → code string → reconciler → runtime entities
```
- `evaluate(code)` runs the bundle against the app's own `solid-js` and `@diffusionstudio/jsx` (one reactive graph, one JSX runtime) and returns its default-exported component.
- `RuntimeDocument` implements `ProjectDocument` from `@diffusionstudio/jsx` over a runtime world: `<stage>` becomes a Scene entity, `<rect>` a rect entity, `fill` a solid paint child. It is the only place that knows how the runtime models nodes.
- `mount(code, world)` does both and returns a disposer that also destroys the rendered entities (Solid's universal render disposer only tears down the reactive graph).
```ts
import { mount } from '@diffusionstudio/reconciler';
const mounted = mount(compiled.code, world);
// ...later, e.g. on a file change:
mounted.dispose();
```
Supported today: `<stage>` and `<rect>` with `x`, `y`, `width`, `height`, and `fill` (rect only).

View File

@ -6,32 +6,32 @@
// @diffusionstudio/jsx) writes project JSX straight into runtime entities, so
// a rendered project IS the composition, with no intermediate tree. This is
// the only file in the reconciler that knows how the runtime models nodes.
// Supported tags for now: <stage> (the composition root, a Scene) and <rect>,
// with x, y, width, height and fill (rect only).
//
// Supported tags for now:
// - <stage>: the infinite canvas, i.e. the world's document root. It has no
// size or position; `background` sets the canvas color.
// - <rect>: a rect entity with x, y, width, height and fill.
import { Not } from 'koota';
import {
appendChild,
Background,
ChildOf,
ClipsContent,
Color,
createEntity,
DEFAULT_BACKGROUND,
Deleted,
Geometry,
GeometryType,
getDocument,
getParentEntity,
ItemIndex,
Name,
Paint,
PaintType,
parseColor,
Playback,
Position,
resizeEntity,
Scene,
sortByItemIndex,
switchActiveScene,
} from '@diffusionstudio/runtime';
import type { Entity, World } from 'koota';
@ -48,7 +48,7 @@ const isTag = (tag: string): tag is Tag => (TAGS as readonly string[]).includes(
*/
export interface SceneNode {
readonly entity: Entity;
/** null for the mount root (the world's document root). */
/** null for the mount root. */
readonly tag: Tag | null;
}
@ -59,11 +59,12 @@ const toNumber = (value: unknown): number | undefined => {
};
export class RuntimeDocument implements ProjectDocument<SceneNode> {
/** The mount root: the world's document root entity. */
/** The mount root. Both it and the <stage> element stand for the document root entity. */
public readonly stage: SceneNode;
/** Elements this document created, by entity, so ordering only ever considers them (never paint sub-entities). */
private readonly nodes = new Map<Entity, SceneNode>();
private stageNode: SceneNode | undefined;
public constructor(private readonly world: World) {
this.stage = { entity: getDocument(world), tag: null };
@ -72,26 +73,20 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
public createElement(tag: string): SceneNode {
if (!isTag(tag)) throw new Error(`<${tag}> is not supported yet (only <stage> and <rect>).`);
if (tag === 'stage') {
if (this.stageNode) throw new Error('A project renders exactly one <stage>.');
// The stage is the document root: infinite, no entity of its own.
this.stageNode = { entity: this.stage.entity, tag };
return this.stageNode;
}
const world = this.world;
const entity = createEntity(world);
entity.add(Geometry);
entity.set(Geometry, { value: GeometryType.RECT });
entity.add(Position);
entity.set(Position, { x: 0, y: 0 });
if (tag === 'stage') {
// A scene is a RECT carrying Scene/ClipsContent/Playback plus a solid
// fill; the same recipe the scene tool uses.
entity.add(Scene);
entity.add(ClipsContent);
entity.add(Playback);
entity.add(Name);
entity.set(Name, { value: 'Stage' });
resizeEntity(world, entity, { width: 1920, height: 1080 });
this.setFill(entity, '#000000');
} else {
resizeEntity(world, entity, { width: 100, height: 100 });
}
resizeEntity(world, entity, { width: 100, height: 100 });
const node: SceneNode = { entity, tag };
this.nodes.set(entity, node);
@ -125,10 +120,13 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
return;
}
case 'fill': {
if (node.tag !== 'rect') return;
this.setFill(entity, value);
return;
}
case 'background': {
this.setBackground(value);
return;
}
default:
// children/ref and anything from a richer vocabulary: ignored, so
// such a project still renders what this host understands.
@ -139,18 +137,25 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
public insertNode(parent: SceneNode, node: SceneNode, anchor?: SceneNode): void {
if (parent === this.stage) {
if (node.tag !== 'stage') throw new Error('The root element must be <stage>.');
// createEntity already parented the node to the document.
switchActiveScene(this.world, node.entity);
} else {
if (parent.tag !== 'stage') throw new Error('<rect> cannot have children.');
appendChild(this.world, node.entity, parent.entity);
// The stage is the root itself; nothing to attach.
return;
}
if (parent.tag !== 'stage') throw new Error('<rect> cannot have children.');
if (node.tag === 'stage') throw new Error('<stage> is only allowed as the root element.');
// createEntity already parented the node to the document (= the stage).
this.reorder(parent, node, anchor);
}
public removeNode(_parent: SceneNode, node: SceneNode): void {
if (node.tag === 'stage') {
for (const child of this.children(node)) this.removeNode(node, child);
this.stageNode = undefined;
this.setBackground(undefined);
return;
}
this.nodes.delete(node.entity);
// ChildOf auto-destroys orphans, so paints and children go with it.
// ChildOf auto-destroys orphans, so paints go with it.
node.entity.destroy();
}
@ -159,21 +164,25 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
* disposer only tears down the reactive graph, never the host nodes.
*/
public dispose(): void {
if (this.stageNode) this.removeNode(this.stage, this.stageNode);
for (const node of this.children(this.stage)) this.removeNode(this.stage, node);
this.nodes.clear();
}
public getParentNode(node: SceneNode): SceneNode | undefined {
if (node.tag === 'stage') return this.stage;
const parent = getParentEntity(node.entity);
if (parent === null) return undefined;
return parent === this.stage.entity ? this.stage : this.nodes.get(parent);
return parent === this.stage.entity ? (this.stageNode ?? this.stage) : this.nodes.get(parent);
}
public getFirstChild(node: SceneNode): SceneNode | undefined {
if (node === this.stage) return this.stageNode;
return this.children(node)[0];
}
public getNextSibling(node: SceneNode): SceneNode | undefined {
if (node.tag === 'stage') return undefined;
const parent = this.getParentNode(node);
if (!parent) return undefined;
const siblings = this.children(parent);
@ -200,6 +209,11 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {
}
}
private setBackground(value: unknown): void {
const color = typeof value === 'string' ? parseColor(value) : null;
this.world.set(Background, { value: color ?? DEFAULT_BACKGROUND });
}
private setFill(entity: Entity, value: unknown): void {
const color = typeof value === 'string' ? parseColor(value) : null;
const paint = this.world.queryFirst(ChildOf(entity), Paint);

View File

@ -108,7 +108,7 @@ apps/web now depends on `@diffusionstudio/runtime` and `@diffusionstudio/koota-s
- **Engine shell** (`apps/web/src/engine/`, not in this package — it's browser/DOM-coupled, which this package deliberately isn't): `Engine` (`create-engine.ts`) is a class wrapping `createRuntimeWorld` + `runSystems`; it owns canvas mount/resize/DPR, an AudioContext (created or injected, closed on `dispose()` only if it created it), and its own `requestAnimationFrame` loop (`start`/`stop`, plus a public `step(timestamp)` escape hatch for a future host to drive the tick itself and interleave app-side input/HUD systems around it). `context.tsx` (`EngineProvider`/`useEngineContext`) composes koota-solid's `WorldProvider` around `engine.world`, so trait state is read via `useTrait`/`useQuery` directly against the world, not funneled through a context bag. `canvas.tsx` (`EngineCanvas`) is the reusable mount/resize/start component. `hooks/` has a few illustrative per-trait wrappers so far: `useCamera`, `useActiveScene`, `useRunning`.
- **Wired into the live app**: `KootaEngineProvider` (the new `EngineProvider`, aliased) is mounted in `pages/home.tsx` at the same level and lifetime as the old bitecs `EngineProvider` (both keyed on `projectId`), so anything under `EditorPage` can reach the koota world as more gets migrated. In `components/canvas/canvas.tsx`, the koota `EngineCanvas` now occupies the visible `z-1` canvas slot. The old bitecs `<canvas id="engine-canvas">` stays mounted at the same DOM id (so its `EngineProvider`'s `init()` still finds it and keeps initializing, and everything still on `useEngine()`/`useECS()` — Toolbar, drag/drop, sidebars, timeline — keeps working) but is `opacity-0 pointer-events-none`: no longer visible or interactive. Drag/drop handlers moved from the canvas element to its wrapping div so `pointer-events-none` didn't silently kill them.
- **Projects on disk (desktop only)**: `apps/web/src/projects/` loads real content into the koota world. A project is a folder under a user-picked root (persisted in `dspro.ui.projects.root`) holding an `index.tsx` whose default export renders `<stage>`; the electron main process (`apps/desktop/src/projects.ts`) scans the root, scaffolds new folders (`index.tsx`, `package.json`, `tsconfig.json`, generated `jsx.d.ts`), bundles with esbuild + babel-preset-solid (universal, moduleName `@diffusionstudio/jsx`) and watches for changes. The new `@diffusionstudio/reconciler` package (`packages/reconciler`, modeled on ds-next's `src/reconciler/`) evaluates the CJS bundle against the app's solid-js and `@diffusionstudio/jsx`, and its `RuntimeDocument` implements `ProjectDocument` over the world (`mount(code, world)` wraps both): `<stage>` = Scene entity (x/y/width/height), `<rect>` = RECT entity (x/y/width/height/fill as a solid Paint child). Host nodes wrap entities in objects because Solid's universal renderer treats numbers as text. `ProjectLoader` (mounted in `pages/project.tsx`) compiles → renders → refits the camera once → re-renders on disk changes. Routes: `/` is the full-screen dashboard (`pages/home.tsx`, no longer a dialog), `/projects/*name` is the editor with the project folder `name` loaded (`useProjectId()` reads it from the route); the old default project id is gone, so there is no editor without a project. Only those two tags/five props so far; no write-back from canvas edits to source yet.
- **Projects on disk (desktop only)**: `apps/web/src/projects/` loads real content into the koota world. A project is a folder under a user-picked root (persisted in `dspro.ui.projects.root`) holding an `index.tsx` whose default export renders `<stage>`; the electron main process (`apps/desktop/src/projects.ts`) scans the root, scaffolds new folders (`index.tsx`, `package.json`, `tsconfig.json`, generated `jsx.d.ts`), bundles with esbuild + babel-preset-solid (universal, moduleName `@diffusionstudio/jsx`) and watches for changes. The new `@diffusionstudio/reconciler` package (`packages/reconciler`, modeled on ds-next's `src/reconciler/`) evaluates the CJS bundle against the app's solid-js and `@diffusionstudio/jsx`, and its `RuntimeDocument` implements `ProjectDocument` over the world (`mount(code, world)` wraps both): `<stage>` = the infinite canvas, i.e. the document root itself (`background` sets the Background world trait; no size, no camera fitting), `<rect>` = top-level RECT entity (x/y/width/height/fill as a solid Paint child). Host nodes wrap entities in objects because Solid's universal renderer treats numbers as text. `ProjectLoader` (mounted in `pages/project.tsx`) compiles → renders → re-renders on disk changes. Routes: `/` is the full-screen dashboard (`pages/home.tsx`, no longer a dialog), `/projects/*name` is the editor with the project folder `name` loaded (`useProjectId()` reads it from the route); the old default project id is gone, so there is no editor without a project. Only those two tags/five props so far; no write-back from canvas edits to source yet.
## Remaining work (suggested order)