diff --git a/apps/web/src/components/sidebar-right/inspector/background.tsx b/apps/web/src/components/sidebar-right/inspector/background.tsx index 65549df..4602d22 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/tooltip import { 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 ( diff --git a/apps/web/src/engine/hooks/use-document.ts b/apps/web/src/engine/hooks/use-document.ts new file mode 100644 index 0000000..13a6e0c --- /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.ts index 2b3619d..30c0693 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: // - : 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 { 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 { 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 { return getEntityChildren(this.world, parent.entity).map((entity) => ({ entity })); } } + +const documents = new WeakMap(); + +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.ts index 21dd006..4689f3e 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.ts index 8789873..ae78d79 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.ts index 08b145c..604d5a6 100644 --- a/packages/runtime/src/utils/color.ts +++ b/packages/runtime/src/utils/color.ts @@ -61,7 +61,15 @@ const NAMED_COLORS: Record = { * 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;