Refactor background handling and document interactions

- Updated BackgroundSettings to utilize the new useDocument hook for managing background properties.
- Enhanced RuntimeDocument to streamline background color parsing and setting, ensuring consistent handling of background traits.
- Refactored mount function to create RuntimeDocument instances through a dedicated factory method, improving clarity and encapsulation.
- Adjusted color parsing logic to handle both string and number inputs, enhancing flexibility in background color management.
This commit is contained in:
konstantin-paulus 2026-08-16 15:59:31 +02:00
parent 2bcad215a9
commit e21b7bbffd
6 changed files with 50 additions and 7 deletions

View File

@ -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 (
<PanelSection title="Background" ref={anchorRef}>

View File

@ -0,0 +1,7 @@
import { useWorld } from "@diffusionstudio/koota-solid";
import { getRuntimeDocument } from "@diffusionstudio/reconciler";
export function useDocument() {
const world = useWorld();
return () => getRuntimeDocument(world);
}

View File

@ -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;
}

View File

@ -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';

View File

@ -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 {

View File

@ -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;