The VS Code of video editing, built for agents.
TypeScript
Implement selection management for entities in the editor
konstantin-paulusae004e49aa66
diff --git a/apps/desktop/src/edit.ts b/apps/desktop/src/edit.tsindex 2b1e81bc..03143858 100644--- a/apps/desktop/src/edit.ts+++ b/apps/desktop/src/edit.ts@@ -185,7 +185,11 @@ const initializerText = (value: PropValue): string =>? `"${value}"`: `{${literalText(value)}}`;-/** Writes a prop onto a tag as one would write it: `muted`, not `muted={true}`. */+/**+ * Writes a prop onto a tag as one would write it: `muted`, not `muted={true}`,+ * and no attribute at all rather than `muted={false}`, since absence is what a+ * boolean prop's false reads as.+ */function setProp(tag: JsxTag, name: string, value: PropValue): void {const attribute = attributeOf(tag, name);@@ -195,6 +199,11 @@ function setProp(tag: JsxTag, name: string, value: PropValue): void {return;}+ if (value === false) {+ attribute?.remove();+ return;+ }+if (attribute) attribute.setInitializer(initializerText(value));else tag.addAttribute({ name, initializer: initializerText(value) });}diff --git a/apps/web/src/components/canvas/draw-overlay.tsx b/apps/web/src/components/canvas/draw-overlay.tsxindex f247becd..d3e74f0c 100644--- a/apps/web/src/components/canvas/draw-overlay.tsx+++ b/apps/web/src/components/canvas/draw-overlay.tsx@@ -11,7 +11,6 @@ import {getNextName,Root,screenToWorld,- Selected,Source,store,switchActiveScene,@@ -230,8 +229,7 @@ export function DrawOverlay() {switchActiveScene(world, entity);}- for (const selected of world.query(Selected)) selected.remove(Selected);- entity.add(Selected);+ editor.select(entity);}world.set(Tool, { value: tool === ToolType.TEXT ? ToolType.TEXT_EDIT : ToolType.MOVE });diff --git a/apps/web/src/components/canvas/scene-init-overlay.tsx b/apps/web/src/components/canvas/scene-init-overlay.tsxindex 5d43d901..4ab11000 100644--- a/apps/web/src/components/canvas/scene-init-overlay.tsx+++ b/apps/web/src/components/canvas/scene-init-overlay.tsx@@ -5,7 +5,7 @@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 { ChildOf, getCameraMatrix, getNextName, Root, setCamera, Source, switchActiveScene } from "@diffusionstudio/runtime";import { Icon } from "@/components/ui/icon";import {DropdownMenu,@@ -113,8 +113,7 @@ export function SceneInitOverlay() {if (!scene) return;switchActiveScene(world, scene);- for (const selected of world.query(Selected)) selected.remove(Selected);- scene.add(Selected);+ editor.select(scene);};return (diff --git a/apps/web/src/engine/editor.ts b/apps/web/src/engine/editor.tsindex f7c09404..a1831d7f 100644--- a/apps/web/src/engine/editor.ts+++ b/apps/web/src/engine/editor.ts@@ -10,7 +10,7 @@* where the commands live.*/-import { Chars, getEntityChildren, getParentEntity, isText, Source } from '@diffusionstudio/runtime';+import { Chars, getEntityChildren, getParentEntity, isText, Selected, Source, Stage } from '@diffusionstudio/runtime';import { SOURCE_ATTR } from '@diffusionstudio/jsx';import { createRoot } from 'solid-js';@@ -129,6 +129,46 @@ export class DocumentEditor {}}+ /**+ * Selects `entities`, replacing the current selection unless `extend` is+ * set. Selection is a document property (`selected` on the element, see+ * `PatchProps`), so it goes the way every editor change goes: the trait for+ * the canvas, an edit for the file. Deselection reports `false`, which the+ * writer spells as the attribute's absence. Entities without a source+ * (nothing mounted yet) only get the trait; the stage is not selectable.+ */+ public select(entities: Entity | Entity[], options: { extend?: boolean } = {}): void {+ const next = new Set(Array.isArray(entities) ? entities : [entities]);++ if (!options.extend) {+ for (const entity of [...this.world.query(Selected)]) {+ if (!next.has(entity)) this.setSelected(entity, false);+ }+ }++ for (const entity of next) {+ this.setSelected(entity, true);+ }+ }++ /** Takes `entities` out of the selection, leaving the rest as it is. */+ public deselect(entities: Entity | Entity[]): void {+ for (const entity of Array.isArray(entities) ? entities : [entities]) {+ this.setSelected(entity, false);+ }+ }++ public clearSelection(): void {+ this.select([]);+ }++ private setSelected(entity: Entity, selected: boolean): void {+ if (!entity.isAlive() || entity.has(Stage) || entity.has(Selected) === selected) return;+ if (selected) entity.add(Selected);+ else entity.remove(Selected);+ this.reportEdit(entity, 'selected', selected);+ }+/*** Adds elements the way a project would author them, under `parent` and in* front of `anchor` (or last):diff --git a/packages/jsx/src/types.ts b/packages/jsx/src/types.tsindex 54c46b2a..d77582ed 100644--- a/packages/jsx/src/types.ts+++ b/packages/jsx/src/types.ts@@ -118,6 +118,15 @@ export type CaptionPreset =export type PatchProps = {/** Human-readable node name. */name?: string;+ /**+ * Whether the editor has this element selected. Editor state rather than+ * part of the composition (nothing rendered or exported depends on it), but+ * the source is the document, so it lives here for the same reason+ * `<stage>`'s `camera` does: a click on the canvas has nowhere else to be+ * written to, and the selection survives a recompile. Absent means not+ * selected; the editor writes the bare attribute and removes it again.+ */+ selected?: boolean;/** Position relative to the parent, px. Defaults to 0. Animatable. */x?: Animatable<number>;y?: Animatable<number>;@@ -228,7 +237,7 @@ type TimingProps = Pick<PatchProps, "start" | "end" | "sourceIn" | "sourceOut">;type CommonProps = TimingProps &Pick<PatchProps,- | "name" | "x" | "y" | "offsetX" | "offsetY" | "width" | "height" | "rotation"+ | "name" | "selected" | "x" | "y" | "offsetX" | "offsetY" | "width" | "height" | "rotation"| "opacity" | "cornerRadius" | "transition" | "animations">;@@ -271,13 +280,14 @@ export type StageProps = {* `width`×`height` and owns the timeline they are placed on, so it takes no* timing of its own — nothing outside a scene has a clock to place it against.*- * `x`/`y` are where the frame sits on the infinite canvas. That is an editor- * concern rather than part of the composition, but it lives here for the same- * reason `<stage>`'s `camera` does: the source is the document, so a scene- * dragged on the canvas has nowhere else to be written back to.+ * `x`/`y` are where the frame sits on the infinite canvas, and `selected`+ * whether the editor has it selected. Those are editor concerns rather than+ * part of the composition, but they live here for the same reason `<stage>`'s+ * `camera` does: the source is the document, so a scene dragged or clicked on+ * the canvas has nowhere else to be written back to.*/export type SceneProps = Required<Pick<PatchProps, "width" | "height">> &- Pick<PatchProps, "name" | "x" | "y" | "fill"> & {+ Pick<PatchProps, "name" | "selected" | "x" | "y" | "fill"> & {children?: SolidJSX.Element;};diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.tsindex a2382881..e4e93aa9 100644--- a/packages/reconciler/src/document.ts+++ b/packages/reconciler/src/document.ts@@ -33,6 +33,7 @@ import {Stage,Root,Scene,+ Selected,Source,SourceIn,SourceOut,@@ -214,6 +215,14 @@ export class RuntimeDocument implements ProjectDocument<HostNode> {entity.set(Name, { value });return;}+ case 'selected': {+ if (value === true && entity !== this.stage.entity) {+ entity.add(Selected);+ } else {+ entity.remove(Selected);+ }+ return;+ }case 'x':case 'y': {entity.add(Position);diff --git a/packages/reconciler/src/patch.ts b/packages/reconciler/src/patch.tsindex 1db6001b..ed526c8f 100644--- a/packages/reconciler/src/patch.ts+++ b/packages/reconciler/src/patch.ts@@ -12,6 +12,7 @@ import type { PatchProps } from '@diffusionstudio/jsx';*/export const PATCH_PROP_KEYS = Object.keys({name: true,+ selected: true,x: true,y: true,offsetX: true,diff --git a/packages/runtime/MIGRATION.md b/packages/runtime/MIGRATION.mdindex 360045e9..9e8f6060 100644--- a/packages/runtime/MIGRATION.md+++ b/packages/runtime/MIGRATION.md@@ -135,6 +135,8 @@ apps/web now depends on `@diffusionstudio/runtime` and `@diffusionstudio/koota-s**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.+ **Selection is a document property.** `selected?: boolean` joined `PatchProps` (picked into `CommonProps` and `SceneProps`, listed in `PATCH_PROP_KEYS`), for the same reason `<stage camera>` and `<scene x y>` exist: the source is the document, so a click on the canvas has nowhere else to be written to, and it means the selection survives the unmount/remount every recompile does. `RuntimeDocument.setProperty('selected')` adds the runtime's `Selected` trait for `true` and removes it for anything else (a bare attribute, a reactive prop going undefined; the stage is never selectable). The editor direction is `DocumentEditor.select(entities, { extend? })` / `deselect` / `clearSelection` (`apps/web/src/engine/editor.ts`): they write the trait and report a `selected` prop edit per entity that changed, `false` for the ones deselected. Selection surfaces go through these rather than touching `Selected` directly (DrawOverlay and SceneInitOverlay do), so nothing else has to know it is a source prop; not an observer on the trait, because a mount adding `Selected` from the file, or an unmount destroying selected entities, must not report as edits. The desktop `setProp` writes `false` as the attribute's absence (`attribute.remove()`), so deselection never leaves `selected={false}` behind, and the same now holds for any boolean prop (`muted`). Not carried over: the bitecs `selectStage/Parent/ChildEntities` helpers and the input system's click/marquee selection, which are still on the old world.+**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)