Skip to content

The VS Code of video editing, built for agents.

TypeScript

Enhance editing capabilities with text node support and tool integration

konstantin-paulus19a7639a7055
diff --git a/apps/desktop/src/edit.ts b/apps/desktop/src/edit.ts
index da88225d..2b1e81bc 100644
--- a/apps/desktop/src/edit.ts
+++ b/apps/desktop/src/edit.ts
@@ -48,7 +48,8 @@ export interface SourceSet {
* the child named by `before` or last. `source` is the pending name the canvas
* knows the new element by; the write answers with the real one in `ids`.
* A parent may itself be pending when it was inserted earlier in the same
- * write.
+ * write. `text`, when present, is the element's literal text content
+ * (`<text>Hello</text>`); without it the element is written self-closing.
*/
export interface SourceInsert {
kind: "insert";
@@ -57,6 +58,7 @@ export interface SourceInsert {
tag: string;
props: Record<string, PropValue>;
before?: string;
+ text?: string;
}
export type SourceEdit = SourceSet | SourceInsert;
@@ -197,6 +199,15 @@ function setProp(tag: JsxTag, name: string, value: PropValue): void {
else tag.addAttribute({ name, initializer: initializerText(value) });
}
+/**
+ * Text content as JSX spells it: bare when JSX would read it back verbatim,
+ * otherwise as a string expression (JSX collapses whitespace around line
+ * breaks and reads `{`, `}`, `<`, `>`, `&` as syntax).
+ */
+function jsxText(text: string): string {
+ return /^[^\s{}<>&][^\n{}<>&]*[^\s{}<>&]$|^[^\s{}<>&]$/.test(text) ? text : `{${JSON.stringify(text)}}`;
+}
+
// ---------------------------------------------------------------------------
// Layout
@@ -616,7 +627,9 @@ class SourceWriter {
// one attribute at a time, re-found by name after each (editing one
// attribute forgets its siblings).
const id = nextId();
- insertChild(sourceFile, parent, `<${edit.tag} ${ID_ATTR}="${id}" />`, before);
+ const opening = `<${edit.tag} ${ID_ATTR}="${id}"`;
+ const child = edit.text === undefined ? `${opening} />` : `${opening}>${jsxText(edit.text)}</${edit.tag}>`;
+ insertChild(sourceFile, parent, child, before);
for (const [name, value] of Object.entries(edit.props)) {
setProp(findTag(sourceFile, id)!, name, value);
}
diff --git a/apps/web/src/components/canvas/canvas.tsx b/apps/web/src/components/canvas/canvas.tsx
index 6bd783df..e9b5c9cb 100644
--- a/apps/web/src/components/canvas/canvas.tsx
+++ b/apps/web/src/components/canvas/canvas.tsx
@@ -10,7 +10,7 @@ import { DrawOverlay } from "./draw-overlay";
import { DesktopAppBanner } from "./desktop-app-banner";
import { toast } from "somoto"
import { findSceneAt, screenToWorld, worldToLocal, secondsToFrames, GeometryType, PaintType, createEntity, addComponent, appendChild, setComponent, resizeEntity } from "../engine";
-// import { SceneInitOverlay } from "./scene-init-overlay";
+import { SceneInitOverlay } from "./scene-init-overlay";
import { CLASSIC_PRESET_HEIGHT, CLASSIC_PRESET_WIDTH } from "../engine/decoders/caption/classic";
@@ -156,10 +156,7 @@ export function Canvas() {
<Toolbar />
<DesktopAppBanner />
<DrawOverlay />
- {/* <SceneInitOverlay
- onDrop={handleDropEvent}
- onDragOver={handleDragOver}
- /> */}
+ <SceneInitOverlay />
<EngineCanvas />
<CameraController />
diff --git a/apps/web/src/components/canvas/draw-overlay.tsx b/apps/web/src/components/canvas/draw-overlay.tsx
index fbcf8bd7..f247becd 100644
--- a/apps/web/src/components/canvas/draw-overlay.tsx
+++ b/apps/web/src/components/canvas/draw-overlay.tsx
@@ -2,16 +2,31 @@
* 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 { useEngine } from "@/context/engine";
import { Show } from "solid-js";
-import { screenToWorld, worldToLocal, findSceneAt, ToolType, getNextName, GeometryType, PaintType, type Point, createEntity, switchActiveScene, addComponent, appendChild, setComponent, resizeEntity, clearComponent } from "@/components/engine";
-import { useECS } from "@/context/ecs";
-
+import { useWorld } from "@diffusionstudio/koota-solid";
+import { Rect, Scene, Text } from "@diffusionstudio/reconciler";
+import {
+ Computed,
+ findSceneAt,
+ getNextName,
+ Root,
+ screenToWorld,
+ Selected,
+ Source,
+ store,
+ switchActiveScene,
+ Tool,
+ ToolType,
+ worldToLocal,
+} from "@diffusionstudio/runtime";
+import { useEditor, useTool } from "@/engine";
+
+import type { Entity } from "koota";
+import type { Point } from "@diffusionstudio/runtime";
type ToolConfig = {
- geometry: GeometryType;
isScene?: boolean;
- fillColor: number;
+ fillColor: string;
previewColor: string;
namePrefix: string;
label: string;
@@ -21,8 +36,7 @@ type ToolConfig = {
const TOOL_CONFIG: Partial<Record<ToolType, ToolConfig>> = {
[ToolType.RECT]: {
- geometry: GeometryType.RECT,
- fillColor: 0xE0E0E0,
+ fillColor: '#E0E0E0',
previewColor: '#E0E0E0',
namePrefix: 'Rect',
label: 'Create shape',
@@ -30,9 +44,8 @@ const TOOL_CONFIG: Partial<Record<ToolType, ToolConfig>> = {
defaultHeight: 300,
},
[ToolType.SCENE]: {
- geometry: GeometryType.RECT,
isScene: true,
- fillColor: 0x000000,
+ fillColor: '#000000',
previewColor: '#000000',
namePrefix: 'Scene',
label: 'Create scene',
@@ -40,8 +53,7 @@ const TOOL_CONFIG: Partial<Record<ToolType, ToolConfig>> = {
defaultHeight: 1080,
},
[ToolType.TEXT]: {
- geometry: GeometryType.TEXT,
- fillColor: 0xFFFFFF,
+ fillColor: '#FFFFFF',
previewColor: 'transparent',
namePrefix: 'Text',
label: 'Create text',
@@ -54,10 +66,9 @@ const TOOL_CONFIG: Partial<Record<ToolType, ToolConfig>> = {
const CLICK_THRESHOLD = 10;
export function DrawOverlay() {
- const { world } = useEngine();
- const { selectedTool } = useECS();
-
- const c = world.components;
+ const world = useWorld();
+ const editor = useEditor();
+ const selectedTool = useTool();
let overlayRef!: HTMLDivElement;
let previewRef!: HTMLDivElement;
@@ -66,7 +77,7 @@ export function DrawOverlay() {
let isDrawing = false;
let startPoint: Point | null = null;
let currentPoint: Point | null = null;
- let targetSceneEid: number | null = null;
+ let targetScene: Entity | null = null;
const config = () => TOOL_CONFIG[selectedTool()];
const isActiveTool = () => !!config();
@@ -120,7 +131,7 @@ export function DrawOverlay() {
const point = getLocalPoint(e);
const worldPt = screenToWorld(world, point.x, point.y);
- targetSceneEid = findSceneAt(world, worldPt.x, worldPt.y);
+ targetScene = findSceneAt(world, worldPt.x, worldPt.y);
overlayRef.setPointerCapture(e.pointerId);
isDrawing = true;
@@ -138,7 +149,7 @@ export function DrawOverlay() {
isDrawing = false;
startPoint = null;
currentPoint = null;
- targetSceneEid = null;
+ targetScene = null;
updatePreview();
};
@@ -160,8 +171,8 @@ export function DrawOverlay() {
// Approximate text size from the parent scene's height.
let fontSize = 16;
- if (tool === ToolType.TEXT && targetSceneEid !== null) {
- const sceneHeight = c.Computed.height[targetSceneEid];
+ if (tool === ToolType.TEXT && targetScene !== null) {
+ const sceneHeight = store(world, Computed).height[targetScene.id()] ?? 0;
fontSize = Math.max(8, Math.round(sceneHeight / 22.5));
}
@@ -183,52 +194,47 @@ export function DrawOverlay() {
let posY = isClick ? worldTopLeft.y - height / 2 : worldTopLeft.y;
// Scenes always live at the root; rect/text may parent into a hovered scene.
- const parentScene = tool === ToolType.SCENE ? null : targetSceneEid;
+ const parentScene = tool === ToolType.SCENE ? null : targetScene;
if (parentScene !== null) {
const local = worldToLocal(world, parentScene, posX, posY);
posX = local.x;
posY = local.y;
}
- const eid = world.history.transaction(cfg.isScene ? 'Add scene' : 'Draw shape', () => {
- const newEid = createEntity(world);
- setComponent(world, newEid, c.Geometry, cfg.geometry);
+ const parent = parentScene ?? world.get(Root)!;
+ // Nothing to draw into until a project is mounted: the element would have
+ // no file to be written to.
+ if (!parent.get(Source)?.value) {
+ reset();
+ return;
+ }
+
+ const name = getNextName(world, cfg.namePrefix);
+ const x = Math.round(posX);
+ const y = Math.round(posY);
+ // A clicked-in text sizes itself to its glyphs, so it takes no size.
+ const size = tool !== ToolType.TEXT || !isClick ? { width, height } : {};
+
+ const [entity] = editor.insertElement(parent, () => {
if (cfg.isScene) {
- addComponent(world, newEid, c.Scene);
- addComponent(world, newEid, c.ClipsContent);
- setComponent(world, newEid, c.Playback, {});
+ return <Scene name={name} x={x} y={y} width={width} height={height} fill={cfg.fillColor} />;
}
- setComponent(world, newEid, c.Name, getNextName(world, cfg.namePrefix));
- setComponent(world, newEid, c.Position, { x: Math.round(posX), y: Math.round(posY) });
-
if (tool === ToolType.TEXT) {
- setComponent(world, newEid, c.Chars, 'Text');
- setComponent(world, newEid, c.TextStyle, { fontSize: fontSize });
- }
-
- if (tool !== ToolType.TEXT || !isClick) {
- resizeEntity(world, newEid, { width, height });
+ return <Text name={name} x={x} y={y} {...size} fontSize={fontSize} color={cfg.fillColor}>Text</Text>;
}
+ return <Rect name={name} x={x} y={y} {...size} fill={cfg.fillColor} />;
+ });
- const fillEid = createEntity(world);
- setComponent(world, fillEid, c.Paint, PaintType.SOLID);
- setComponent(world, fillEid, c.Color, cfg.fillColor);
- appendChild(world, fillEid, newEid);
-
- if (parentScene !== null) {
- appendChild(world, newEid, parentScene);
+ if (entity) {
+ if (tool === ToolType.SCENE) {
+ switchActiveScene(world, entity);
}
- return newEid;
- });
- if (tool === ToolType.SCENE) {
- switchActiveScene(world, eid);
+ for (const selected of world.query(Selected)) selected.remove(Selected);
+ entity.add(Selected);
}
- clearComponent(world, c.Selected, false);
- addComponent(world, eid, c.Selected, false);
-
- world.selection.tool = tool === ToolType.TEXT ? ToolType.TEXT_EDIT : ToolType.MOVE;
+ world.set(Tool, { value: tool === ToolType.TEXT ? ToolType.TEXT_EDIT : ToolType.MOVE });
reset();
};
diff --git a/apps/web/src/components/canvas/scene-init-overlay.tsx b/apps/web/src/components/canvas/scene-init-overlay.tsx
index 5b96f9f4..5d43d901 100644
--- a/apps/web/src/components/canvas/scene-init-overlay.tsx
+++ b/apps/web/src/components/canvas/scene-init-overlay.tsx
@@ -3,6 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { createMemo, createSignal, For, Show } from "solid-js";
+import { useQuery, useTrait, useWorld } from "@diffusionstudio/koota-solid";
+import { Scene as SceneElement } from "@diffusionstudio/reconciler";
+import { ChildOf, getCameraMatrix, getNextName, Root, Selected, setCamera, Source, switchActiveScene } from "@diffusionstudio/runtime";
import { Icon } from "@/components/ui/icon";
import {
DropdownMenu,
@@ -18,11 +21,7 @@ import {
import {
PRESET_CATEGORIES,
} from "@/lib/layout-presets";
-import { getNextName, persistWorldState, GeometryType, PaintType, createEntity, switchActiveScene, addComponent, appendChild, setComponent, resizeEntity, clearComponent } from "../engine";
-import { useEngine } from "@/context/engine";
-import { MAIN_CANVAS_ID } from "./canvas";
-import { assert } from "@/utils";
-import { getAllEntities } from "bitecs";
+import { useEditor } from "@/engine";
import type { LayoutPreset } from "@/lib/layout-presets";
@@ -30,25 +29,26 @@ import type { LayoutPreset } from "@/lib/layout-presets";
const DEFAULT_NAME = "New Scene";
const DEFAULT_PRESET: LayoutPreset = { label: "Long-form 16:9", width: 1920, height: 1080 };
-type DropOverlayProps = {
- onDrop(event: DragEvent): void;
- onDragOver(event: DragEvent): void;
-};
-
-export function SceneInitOverlay(props: DropOverlayProps) {
- const { world, initialized } = useEngine();
- const c = world.components;
+/**
+ * The empty-project prompt: a placeholder frame in the middle of the canvas
+ * that becomes the first scene on click, with the camera fitted to it so the
+ * scene lands exactly where the placeholder was.
+ */
+export function SceneInitOverlay() {
+ const world = useWorld();
+ const editor = useEditor();
+ const root = world.get(Root)!;
+ const source = useTrait(root, Source);
+ const children = useQuery(ChildOf(root));
const [editing, setEditing] = createSignal(false);
const [selectedPreset, setSelectedPreset] = createSignal<LayoutPreset>(DEFAULT_PRESET);
const [sceneName, setSceneName] = createSignal(DEFAULT_NAME);
+ let overlayRef: HTMLDivElement | undefined;
let buttonRef: HTMLButtonElement | undefined;
- const showOverlay = createMemo(() => {
- world.timelineIndex(); // ensures reactivity
- return initialized() && getAllEntities(world).length === 0;
- });
+ const showOverlay = createMemo(() => source()?.value && children().length === 0);
const aspectRatio = () => {
const p = selectedPreset();
@@ -88,7 +88,8 @@ export function SceneInitOverlay(props: DropOverlayProps) {
const handleInitializeScene = () => {
const buttonRect = buttonRef?.getBoundingClientRect();
- if (!buttonRect) return;
+ const canvasRect = overlayRef?.getBoundingClientRect();
+ if (!buttonRect || !canvasRect) return;
const template = selectedPreset();
const worldX = Math.round(-template.width / 2);
@@ -98,68 +99,29 @@ export function SceneInitOverlay(props: DropOverlayProps) {
? sceneName()
: getNextName(world, 'Scene');
- let sceneEntity!: number;
- world.history.untrack(() => {
- const canvas = document.getElementById(MAIN_CANVAS_ID);
- const canvasRect = canvas?.parentElement?.getBoundingClientRect();
- assert(canvasRect, 'Canvas rect not found');
-
- // Screen position of the button relative to the canvas container
- const screenX = buttonRect.left - canvasRect.left;
- const screenY = buttonRect.top - canvasRect.top;
- const scale = buttonRect.width / template.width;
-
- world.camera.a = scale;
- world.camera.b = 0;
- world.camera.c = 0;
- world.camera.d = scale;
- world.camera.e = screenX - worldX * scale;
- world.camera.f = screenY - worldY * scale;
-
- persistWorldState(world);
-
- sceneEntity = createEntity(world);
- setComponent(world, sceneEntity, c.Geometry, GeometryType.RECT);
- addComponent(world, sceneEntity, c.Scene);
- addComponent(world, sceneEntity, c.ClipsContent);
- setComponent(world, sceneEntity, c.Playback, {});
- setComponent(world, sceneEntity, c.Name, name);
- setComponent(world, sceneEntity, c.Position, { x: worldX, y: worldY });
- resizeEntity(world, sceneEntity, {
- width: template.width,
- height: template.height,
- });
-
- // append a solid fill to the scene
- const fillEntity = createEntity(world);
- setComponent(world, fillEntity, c.Paint, PaintType.SOLID);
- setComponent(world, fillEntity, c.Color, 0x000000);
- appendChild(world, fillEntity, sceneEntity);
-
- switchActiveScene(world, sceneEntity);
- clearComponent(world, c.Selected);
- addComponent(world, sceneEntity, c.Selected);
- });
-
- return sceneEntity;
+ // Fit the camera so the scene appears where the placeholder is: the
+ // overlay covers the canvas, so its rect is the canvas rect.
+ const screenX = buttonRect.left - canvasRect.left;
+ const screenY = buttonRect.top - canvasRect.top;
+ const scale = buttonRect.width / template.width;
+ setCamera(world, { a: scale, b: 0, c: 0, d: scale, e: screenX - worldX * scale, f: screenY - worldY * scale });
+ editor.reportEdit(root, 'camera', getCameraMatrix(world));
+
+ const [scene] = editor.insertElement(root, () => (
+ <SceneElement name={name} x={worldX} y={worldY} width={template.width} height={template.height} fill="#000000" />
+ ));
+ if (!scene) return;
+
+ switchActiveScene(world, scene);
+ for (const selected of world.query(Selected)) selected.remove(Selected);
+ scene.add(Selected);
};
- const handleDrop = (e: DragEvent) => {
- e.preventDefault();
- e.stopPropagation();
- props.onDrop(e);
- }
-
- const handleDragOver = (e: DragEvent) => {
- props.onDragOver(e);
- }
-
return (
<Show when={showOverlay()}>
<div
+ ref={overlayRef}
class="absolute inset-0 z-2 flex items-center justify-center"
- on:drop={handleDrop}
- on:dragover={handleDragOver}
>
<div
class="relative"
diff --git a/apps/web/src/components/canvas/toolbar.tsx b/apps/web/src/components/canvas/toolbar.tsx
index 0a5354ce..bd4be2aa 100644
--- a/apps/web/src/components/canvas/toolbar.tsx
+++ b/apps/web/src/components/canvas/toolbar.tsx
@@ -14,21 +14,21 @@ import {
DropdownMenuPortal,
DropdownMenuShortcut,
} from "@/components/ui/dropdown-menu";
-import { useEngine } from "@/context/engine";
import { PromptInput } from "../genai/prompt-input";
import { ActionBar } from "../genai/action-bar";
import { Show } from "solid-js";
-import { ToolType } from "../engine/components";
-import { useECS } from "@/context/ecs";
+import { Tool, ToolType } from "@diffusionstudio/runtime";
+import { useWorld } from "@diffusionstudio/koota-solid";
+import { useTool } from "@/engine";
import { usePromptInput } from "@/context/prompt-input";
export function Toolbar() {
- const { world } = useEngine();
+ const world = useWorld();
const { promptInputOpen, promptInputConfig, openPromptInput, setPromptInputOpen } = usePromptInput();
- const { selectedTool } = useECS();
+ const selectedTool = useTool();
const handleToolChange = (tool: ToolType) => {
- world.selection.tool = tool;
+ world.set(Tool, { value: tool });
}
return (
diff --git a/apps/web/src/components/sidebar-left/project-menu/tool-menu.tsx b/apps/web/src/components/sidebar-left/project-menu/tool-menu.tsx
index 6d3a6fc5..d17449f5 100644
--- a/apps/web/src/components/sidebar-left/project-menu/tool-menu.tsx
+++ b/apps/web/src/components/sidebar-left/project-menu/tool-menu.tsx
@@ -9,12 +9,13 @@ import {
DropdownMenuGroup,
} from "@/components/ui/dropdown-menu";
import { usePromptInput } from "@/context/prompt-input";
-import { useEngine } from "@/context/engine";
-import { ToolType } from "@/components/engine/components";
+import { useWorld } from "@diffusionstudio/koota-solid";
+import { Tool, ToolType } from "@diffusionstudio/runtime";
export function ToolMenu() {
const { setPromptInputOpen } = usePromptInput();
- const { world } = useEngine();
+ const world = useWorld();
+ const setTool = (value: ToolType) => world.set(Tool, { value });
return (
<>
@@ -27,15 +28,15 @@ export function ToolMenu() {
<DropdownMenuSeparator />
<DropdownMenuGroup>
- <DropdownMenuItem onSelect={() => (world.selection.tool = ToolType.SCENE)}>
+ <DropdownMenuItem onSelect={() => setTool(ToolType.SCENE)}>
Scene
<DropdownMenuShortcut>F</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => (world.selection.tool = ToolType.TEXT)}>
+ <DropdownMenuItem onSelect={() => setTool(ToolType.TEXT)}>
Text
<DropdownMenuShortcut>T</DropdownMenuShortcut>
</DropdownMenuItem>
- <DropdownMenuItem onSelect={() => (world.selection.tool = ToolType.RECT)}>
+ <DropdownMenuItem onSelect={() => setTool(ToolType.RECT)}>
Rectangle
<DropdownMenuShortcut>R</DropdownMenuShortcut>
</DropdownMenuItem>
diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.ts
index 1ed99c88..f7c09404 100644
--- a/apps/web/src/engine/editor.ts
+++ b/apps/web/src/engine/editor.ts
@@ -10,7 +10,7 @@
* where the commands live.
*/
-import { getEntityChildren, getParentEntity, Source } from '@diffusionstudio/runtime';
+import { Chars, getEntityChildren, getParentEntity, isText, Source } from '@diffusionstudio/runtime';
import { SOURCE_ATTR } from '@diffusionstudio/jsx';
import { createRoot } from 'solid-js';
@@ -40,6 +40,8 @@ export interface PropEdit {
* disk answers with the real one and re-stamps (see `isPendingSource`).
* `parent` is the source of the element it was inserted under and `before`,
* when present, the sibling it was placed in front of; without it, appended.
+ * `text` is the literal content of a text element (its `children`), which is
+ * not a prop and so travels separately.
*/
export interface InsertEdit {
kind: 'insert';
@@ -48,6 +50,7 @@ export interface InsertEdit {
tag: string;
props: Record<string, PropValue>;
before?: string;
+ text?: string;
}
export type EntityEdit = PropEdit | InsertEdit;
@@ -170,6 +173,10 @@ export class DocumentEditor {
.find((sibling) => !created.has(sibling))
?.get(Source)?.value;
+ // A text element's content arrived as text nodes, which the document
+ // has already folded into Chars; that is what the file gets.
+ const text = isText(entity) ? entity.get(Chars)?.value : undefined;
+
this.sink?.({
kind: 'insert',
source: entity.get(Source)!.value,
@@ -177,6 +184,7 @@ export class DocumentEditor {
tag,
props,
...(before ? { before } : {}),
+ ...(text === undefined ? {} : { text }),
});
}
diff --git a/apps/web/src/engine/hooks/index.ts b/apps/web/src/engine/hooks/index.ts
index 3cd7b2f4..b3dabbcf 100644
--- a/apps/web/src/engine/hooks/index.ts
+++ b/apps/web/src/engine/hooks/index.ts
@@ -6,3 +6,4 @@ export * from './use-active-scene';
export * from './use-camera';
export * from './use-running';
export * from './use-editor';
+export * from './use-tool';
diff --git a/apps/web/src/engine/hooks/use-tool.ts b/apps/web/src/engine/hooks/use-tool.ts
new file mode 100644
index 00000000..3e6cca1f
--- /dev/null
+++ b/apps/web/src/engine/hooks/use-tool.ts
@@ -0,0 +1,14 @@
+/* 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 { Tool, ToolType } from '@diffusionstudio/runtime';
+import { useTrait, useWorld } from '@diffusionstudio/koota-solid';
+
+import type { Accessor } from 'solid-js';
+
+/** Reactive read of the armed canvas tool. Write it with world.set(Tool, { value }). */
+export function useTool(): Accessor<ToolType> {
+ const tool = useTrait(useWorld(), Tool);
+ return () => tool()?.value ?? ToolType.MOVE;
+}
diff --git a/apps/web/src/projects/edits.ts b/apps/web/src/projects/edits.ts
index e32e1c6f..e23ca5ad 100644
--- a/apps/web/src/projects/edits.ts
+++ b/apps/web/src/projects/edits.ts
@@ -81,13 +81,14 @@ class EditWriter {
const edits: SourceEdit[] = [
...[...this.inserts.values()]
.filter((insert) => !heldInserts.has(insert.source))
- .map(({ kind, source, parent, tag, props, before }): SourceEdit => ({
+ .map(({ kind, source, parent, tag, props, before, text }): SourceEdit => ({
kind,
source,
parent,
tag,
props,
...(before === undefined ? {} : { before }),
+ ...(text === undefined ? {} : { text }),
})),
...[...this.pending]
.filter(([source]) => !held.has(source))
diff --git a/packages/runtime/MIGRATION.md b/packages/runtime/MIGRATION.md
index 989c7f87..360045e9 100644
--- a/packages/runtime/MIGRATION.md
+++ b/packages/runtime/MIGRATION.md
@@ -133,6 +133,8 @@ apps/web now depends on `@diffusionstudio/runtime` and `@diffusionstudio/koota-s
**Element insertion (write-back of new elements).** Editing commands live app-side in `DocumentEditor` (`apps/web/src/engine/editor.ts`, one per world via `getDocumentEditor(world)`, hook `useEditor()`; the reconciler exports `insert`/`withDocument` for it), not in `RuntimeDocument`, which stays a plain `ProjectDocument` host: `onEdit`, `editProperty`, `reportEdit`, `insertElement`, `restamp`, `discardPending`. `editor.insertElement(parent, () => <Rect x={10} width={200} height={300} />, anchor?)` renders the thunk through Solid's universal `insert` under `withDocument` with a recording proxy of the document (same host, every `createElement` stamped pending and `setProperty` noted; the document never learns it was watched), so it goes through the document's own `createElement`/`setProperty`/`insertNode` and is a first-class node on the canvas at once, no remount. The app's JSX compiles for the DOM, so lowercase `<rect>` is unusable there; the PascalCase components (`Rect`, `Scene`, ... now exported from the reconciler index) compile identically in both modes and are the spelling to use. `createElement` lowercases the first letter, so both spellings reach the same recipe. While the thunk renders, every element created is stamped with a pending source (`pending#n`, colon-free so `parseSource` rejects it as an address) and its tag + literal props recorded; afterwards one `InsertEdit` per element is reported in creation order (`EntityEdit` is now `PropEdit | InsertEdit`, discriminated by `kind`), a nested `<Scene><Rect/></Scene>` naming its parent by pending source, and `before` set to the next pre-existing sibling. Returns the top-level entities created. The desktop `applyEdits` grew a matching `SourceInsert` (`SourceEdit = SourceSet | SourceInsert`): it appends `<tag {...props} id="…" />` under the parent (or in front of `before`, which must be a direct child) as text edits at positions ts-morph supplies (`getIndentationText`/`getChildIndentationText`/`isFirstNodeOnLine`, with `manipulationSettings.indentationText` matched to the file first since ts-morph does not detect it; it has no insert-child or open-self-closing operation short of re-printing the body), props via the same `setProp`/`addAttribute` path as prop edits, allocates the id on the spot, and answers with `ids[pending] = real`. A pending parent earlier in the same batch resolves through that map, so a rect drawn into a just-drawn scene lands in one write. The `EditWriter` class (`apps/web/src/projects/edits.ts`) sends inserts first, folds prop edits into a not-yet-flushed insert, holds back edits (and child inserts) addressed through an in-flight pending name until the answer arrives, then re-keys them and re-stamps entities via `DocumentEditor.restamp`; an insert the file would not take (`skipped`) is undone on the canvas via `DocumentEditor.discardPending`. `<text>` is hosted too: the recipe is TEXT geometry + `Chars` + `TextStyle` and no `Size` (a text without one sizes itself to its glyphs, which is what a click-inserted text wants); `color` writes the entity's `Color` (the intrinsic fill the text renderer already draws glyphs with, so no paint child), and `fontSize`/`fontFamily`/`fontWeight`/`fontStyle`/`textAlign`/`textBaseline` write `TextStyle` (an unset or invalid value clears the field to `undefined`, i.e. the renderer's default). Text content travels as Solid text nodes, and the runtime has no node for those, so a plain `TextNode` object (`{ text, parent }`, `HostNode = SceneNode | TextNode`, `isSceneNode` narrows) stands in for the DOM text node the way the original bitecs host's `{ kind: "text" }` did: held by identity (Solid compares handles with `===` and calls `replaceText(getFirstChild(parent))`), kept per text entity in an ordered list, and the list's join is the entity's `Chars`, so `<Text>Hello {name()}</Text>` reconciles through `replaceText`/`insertNode`/`removeNode` on the text nodes alone. Text under anything but a text entity throws, as does an entity under a text. `insertElement`'s recording proxy passes text nodes through untouched (only `SceneNode`s are stamped/recorded). Not built yet: the DrawOverlay port itself.
+ **Draw tools (`components/canvas/draw-overlay.tsx`) and Toolbar are on the koota world.** The armed tool is the `Tool` world trait (`traits/interaction.ts`, attached by `createRuntimeWorld`; read with the app's `useTool()`, written with `world.set(Tool, { value })`). Toolbar and the sidebar ToolMenu write it; the bitecs keyboard system's V/H/F/R/T shortcuts still target the old world and stay dead for the new tools until it migrates. Drawing goes through `DocumentEditor.insertElement` with the reconciler's `Scene`/`Rect`/`Text` components (`fill`/`color` instead of a `SolidPaint` child), so a drawn element is written to the source like any authored one; a draw with no mounted project is a no-op. `InsertEdit`/`SourceInsert` gained `text` for a text element's literal content (`Chars` after render), which the desktop writer emits as `<text id="…">Hello</text>` (bare when JSX reads it back verbatim, `{"…"}` otherwise). Runtime gained `findSceneAt`/`worldToLocal` (`queries/hit-test.ts`), reading `LocalTransform` from the store like `getEntityBounds`. Not carried over: `world.history.transaction` — there is no undo layer yet.
+
**Left stale on purpose** (deferred to a docs/examples pass): all eight `examples/*.tsx` still open with `<rect scene="...">` and no `<stage>`, so `tsc -p examples --noEmit` fails on them; `reference/jsx/{scene,roots}.md` still describes the pre-`<stage>` model where a mount grafts roots into an existing document; and `reference/jsx/{audio-sync,video,audio,elements}.md` still document `key` as the `syncTo` target. Deliberately not half-migrated: `reference/jsx` is stale as a whole, and patching the `key` rows alone would hide that.
## Remaining work (suggested order)
diff --git a/packages/runtime/src/queries/hit-test.ts b/packages/runtime/src/queries/hit-test.ts
new file mode 100644
index 00000000..d53cb73b
--- /dev/null
+++ b/packages/runtime/src/queries/hit-test.ts
@@ -0,0 +1,66 @@
+/* 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/. */
+
+// Document-space hit testing and coordinate mapping for top-level entities.
+// LocalTransform is written straight into the SoA store by the transform
+// system (never added as a trait), so it is read by entity id here, like
+// WorldBounds in ./camera.
+
+import { Not } from 'koota';
+
+import { Computed, Culled, Geometry, Hidden, LocalTransform, Scene } from '../traits';
+import { store } from '../world/store';
+import { aabbFromTransformedRect, invert2D, transformPoint } from '../math';
+
+import type { Entity, World } from 'koota';
+import type { Mat2D, Point } from '../math';
+
+/** Below this the transform is singular and the mapping is undefined. */
+const MIN_DETERMINANT = 1e-12;
+
+function localMatrix(world: World, entity: Entity): Mat2D {
+ const local = store(world, LocalTransform);
+ const eid = entity.id();
+
+ return {
+ a: local.a[eid] ?? 1,
+ b: local.b[eid] ?? 0,
+ c: local.c[eid] ?? 0,
+ d: local.d[eid] ?? 1,
+ e: local.e[eid] ?? 0,
+ f: local.f[eid] ?? 0,
+ };
+}
+
+/**
+ * Convert a document-space point (as produced by `screenToWorld`) to an
+ * entity's local coordinates via its LocalTransform. Meant for top-level
+ * entities (scenes), whose LocalTransform is in document space.
+ */
+export function worldToLocal(world: World, entity: Entity, worldX: number, worldY: number): Point {
+ const mat = localMatrix(world, entity);
+ if (Math.abs(mat.a * mat.d - mat.b * mat.c) < MIN_DETERMINANT) return { x: worldX, y: worldY };
+ return transformPoint(invert2D(mat), worldX, worldY);
+}
+
+/**
+ * The first visible top-level scene whose document-space bounds contain the
+ * point (as produced by `screenToWorld`), or null.
+ */
+export function findSceneAt(world: World, worldX: number, worldY: number): Entity | null {
+ const computed = store(world, Computed);
+
+ // Scenes are direct children of the stage, so their LocalTransform is in
+ // document space already.
+ for (const entity of world.query(Scene, Geometry, Not(Culled), Not(Hidden))) {
+ const eid = entity.id();
+ const bounds = aabbFromTransformedRect(localMatrix(world, entity), computed.width[eid] ?? 0, computed.height[eid] ?? 0);
+
+ if (worldX >= bounds.minX && worldX <= bounds.maxX && worldY >= bounds.minY && worldY <= bounds.maxY) {
+ return entity;
+ }
+ }
+
+ return null;
+}
diff --git a/packages/runtime/src/queries/index.ts b/packages/runtime/src/queries/index.ts
index b52cbf34..f6aca160 100644
--- a/packages/runtime/src/queries/index.ts
+++ b/packages/runtime/src/queries/index.ts
@@ -2,3 +2,4 @@ export * from './predicates';
export * from './camera';
export * from './hierarchy';
export * from './timeline-index';
+export * from './hit-test';
diff --git a/packages/runtime/src/traits/interaction.ts b/packages/runtime/src/traits/interaction.ts
index 93285702..028cdcfb 100644
--- a/packages/runtime/src/traits/interaction.ts
+++ b/packages/runtime/src/traits/interaction.ts
@@ -4,8 +4,10 @@
import { trait } from 'koota';
-// Editor interaction state. Defined here so all trait definitions share one
-// home, but only ever added by the editor app; never serialized.
+import { ToolType } from '../constants';
+
+
+export const Tool = trait({ value: ToolType.MOVE });
export const Selected = trait();
diff --git a/packages/runtime/src/world/create-world.ts b/packages/runtime/src/world/create-world.ts
index 1a262179..2286d4ff 100644
--- a/packages/runtime/src/world/create-world.ts
+++ b/packages/runtime/src/world/create-world.ts
@@ -25,6 +25,7 @@ import {
FramePromises,
HitRegions,
} from '../traits/world';
+import { Tool } from '../traits/interaction';
/**
* World with the headless runtime singletons attached. The host injects its
@@ -48,6 +49,7 @@ export function createRuntimeWorld(projectId: string) {
Mounts,
FramePromises,
HitRegions,
+ Tool,
Root,
);