The VS Code of video editing, built for agents.
TypeScript
Refactor background handling and document interactions
konstantin-pauluse21b7bbffd2e
diff --git a/apps/web/src/components/sidebar-right/inspector/background.tsx b/apps/web/src/components/sidebar-right/inspector/background.tsxindex 65549df9..4602d22c 100644--- a/apps/web/src/components/sidebar-right/inspector/background.tsx+++ b/apps/web/src/components/sidebar-right/inspector/background.tsx@@ -18,9 +18,11 @@ import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltipimport { createSignal } from "solid-js";import { Background, DEFAULT_BACKGROUND, Root } from "@diffusionstudio/runtime";import { useTrait, useWorld } from "@diffusionstudio/koota-solid";+import { useDocument } from "@/engine/hooks/use-document";export function BackgroundSettings() {const world = useWorld();+ const document = useDocument();const [isPickerOpen, setIsPickerOpen] = createSignal(false);const background = useTrait(world.get(Root)!, Background);@@ -29,7 +31,10 @@ export function BackgroundSettings() {let anchorRef: HTMLDivElement | undefined;- const assignColor = (value: number) => world.get(Root)!.set(Background, { value });+ const assignColor = (value: number) => {+ const doc = document();+ doc.setProperty(doc.stage, "background", value);+ };return (<PanelSection title="Background" ref={anchorRef}>diff --git a/apps/web/src/engine/hooks/use-document.ts b/apps/web/src/engine/hooks/use-document.tsnew file mode 100644index 00000000..13a6e0c2--- /dev/null+++ b/apps/web/src/engine/hooks/use-document.ts@@ -0,0 +1,7 @@+import { useWorld } from "@diffusionstudio/koota-solid";+import { getRuntimeDocument } from "@diffusionstudio/reconciler";++export function useDocument() {+ const world = useWorld();+ return () => getRuntimeDocument(world);+}diff --git a/packages/reconciler/src/document.ts b/packages/reconciler/src/document.tsindex 2b3619d5..30c06938 100644--- a/packages/reconciler/src/document.ts+++ b/packages/reconciler/src/document.ts@@ -7,6 +7,11 @@// 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.//+// It is also the single write router into a world: the editor UI reaches its+// document with `getRuntimeDocument(world)` and edits through the same+// `setProperty` a rendering project goes through, so front and backend share+// one vocabulary and one translation to traits.+//// 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.@@ -106,7 +111,7 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {return;}case 'fill': {- const color = typeof value === 'string' ? parseColor(value) : null;+ const color = parseColor(value);if (color === null) {entity.remove(Color);@@ -118,7 +123,7 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {return;}case 'background': {- const color = typeof value === 'string' ? parseColor(value) : null;+ const color = parseColor(value);this.stage.entity.set(Background, { value: color ?? DEFAULT_BACKGROUND });return;}@@ -179,3 +184,21 @@ export class RuntimeDocument implements ProjectDocument<SceneNode> {return getEntityChildren(this.world, parent.entity).map((entity) => ({ entity }));}}++const documents = new WeakMap<World, RuntimeDocument>();++export function getRuntimeDocument(world: World): RuntimeDocument {+ const document = documents.get(world);++ if (document === undefined) {+ throw new Error("The requested world has no runtime document");+ }++ return document;+}++export function createRuntimeDocument(world: World): RuntimeDocument {+ const document = new RuntimeDocument(world);+ documents.set(world, document);+ return document;+}diff --git a/packages/reconciler/src/index.ts b/packages/reconciler/src/index.tsindex 21dd0062..4689f3e0 100644--- a/packages/reconciler/src/index.ts+++ b/packages/reconciler/src/index.ts@@ -2,7 +2,7 @@* 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/. */-export { RuntimeDocument, type SceneNode } from './document';+export * from './document';export { evaluate } from './evaluate';export { mount, type Mount } from './mount';export { PATCH_PROP_KEYS } from './patch';diff --git a/packages/reconciler/src/mount.ts b/packages/reconciler/src/mount.tsindex 87898733..ae78d792 100644--- a/packages/reconciler/src/mount.ts+++ b/packages/reconciler/src/mount.ts@@ -2,7 +2,7 @@* 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 { RuntimeDocument } from './document';+import { createRuntimeDocument } from './document';import { evaluate } from './evaluate';import { renderProject } from './renderer';@@ -20,7 +20,7 @@ export interface Mount {*/export function mount(code: string, world: World): Mount {const component = evaluate(code);- const document = new RuntimeDocument(world);+ const document = createRuntimeDocument(world);let dispose: () => void;try {diff --git a/packages/runtime/src/utils/color.ts b/packages/runtime/src/utils/color.tsindex 08b145c6..604d5a67 100644--- a/packages/runtime/src/utils/color.ts+++ b/packages/runtime/src/utils/color.ts@@ -61,7 +61,15 @@ const NAMED_COLORS: Record<string, string> = {* Arbitrary strings fall back to Figma-style hex normalization: strip non-hex* characters, then repeat/pad to six digits (empty input returns null).*/-export function parseColor(input: string): number | null {+export function parseColor(input: unknown): number | null {+ if (typeof input === 'number') {+ return Number.isFinite(input) ? input & 0xFFFFFF : null;+ }++ if (typeof input !== 'string') {+ return null;+ }+const trimmed = input.trim();if (trimmed.length === 0) return null;