weasel API - v0.8.0
    Preparing search index...

    Interface Scene<TData, TLayer, TPose>

    interface Scene<TData, TLayer extends string, TPose = RectPose> {
        layers: readonly LayerRecord<TLayer>[];
        nodes: ReadonlyMap<NodeId, SceneNode<TData, TLayer, TPose>>;
        roots: readonly NodeId[];
        add(spec: AddNodeSpec<TData, TLayer, TPose>): NodeId;
        addLayer(spec: AddLayerSpec<TLayer>): void;
        ancestorsOf(id: NodeId): readonly NodeId[];
        applyBatch(ops: Op[], label: string, adapter: unknown): void;
        batch<T>(label: string, fn: () => T): T;
        canRedo(): boolean;
        canUndo(): boolean;
        childrenOf(id: NodeId): readonly NodeId[];
        get(id: NodeId): SceneNode<TData, TLayer, TPose> | undefined;
        getVersion(): number;
        historyEntries(): readonly { id: string; label: string }[];
        historyIndex(): number;
        jumpToHistoryIndex(index: number): boolean;
        loadState(json: SerializedScene<TData, TLayer, TPose>): void;
        move(id: NodeId, parent: NodeId | null, index?: number): void;
        moveLayer(layer: TLayer, index: number): void;
        recordOp<P>(op: { kind: string; payload: P }): void;
        redo(): boolean;
        registerOp<P>(kind: string, handler: RegisteredOp<P>): void;
        remove(id: NodeId): void;
        removeLayer(layer: TLayer): void;
        renameLayer(layer: TLayer, name: string): void;
        renderOrder(): Iterable<NodeId>;
        reorder(id: NodeId, index: number): void;
        restoreHistory(snapshot: SerializedHistory): void;
        serializeHistory(): SerializedHistory;
        setActiveJournalAccessor(fn: (() => Journal | null) | null): void;
        setHistoryAdapter(fn: (() => unknown) | null): void;
        setLayer(id: NodeId, layer: TLayer): void;
        setLayerLocked(layer: TLayer, locked: boolean): void;
        setLayerVisible(layer: TLayer, visible: boolean): void;
        setPose(id: NodeId, pose: TPose): void;
        subscribe(listener: () => void): () => void;
        toJSON(): SerializedScene<TData, TLayer, TPose>;
        undo(): boolean;
        update(id: NodeId, patch: { data: TData }): void;
    }

    Type Parameters

    • TData
    • TLayer extends string
    • TPose = RectPose
    Index

    Properties

    layers: readonly LayerRecord<TLayer>[]
    nodes: ReadonlyMap<NodeId, SceneNode<TData, TLayer, TPose>>
    roots: readonly NodeId[]

    Methods

    • Apply a batch of ops with journal-aware routing.

      • Without active journal (or no getActiveJournal in options): the ops themselves are recorded as one undo entry on the scene's own history, rebound to adapter — undo replays each op's invert() against that same adapter. Consecutive applyBatch entries can coalesce via matching op coalesceKeys when the scene opts into coalesceWindowMs.
      • With active journal: routes ops to journal.applyBatch(ops, label). The scene's history recording is suppressed for the duration so the journal's inner history — not the scene's undo stack — tracks the batch. Mutations still happen on adapter / scene state.

      adapter must be the same adapter the ops expect (typically a SceneCanvasAdapter). Pass this from sceneToAdapter or a compatible adapter.

      Parameters

      • ops: Op[]
      • label: string
      • adapter: unknown

      Returns void

    • Read-only snapshot of every history entry currently reachable from the present state. Oldest applied first, then redoable entries in the order they'd be re-applied. Each entry id is stable.

      Returns readonly { id: string; label: string }[]

    • Jump to the given history index by calling undo/redo repeatedly. Clamps to [0, total]. Returns true if any movement occurred.

      Parameters

      • index: number

      Returns boolean

    • Replace this scene's entire node + layer state in place from a snapshot produced by toJSON(). Unlike sceneFromJSON, the existing Scene instance is preserved — holders such as <SceneCanvas> keep their reference. History (undo/redo) is cleared, matching sceneFromJSON. Bumps getVersion() and notifies subscribers exactly once.

      Throws on an unsupported version or unknown registry/layer ids; on a malformed snapshot the scene is left empty or partially populated (callers should treat a loadState throw as fatal and reload). Snapshots from toJSON() are always well-formed.

      Parameters

      Returns void

    • Reparent id under parent (or to a root when parent is null) at index within the new sibling list, appending when index is omitted. Siblings are reindexed. Recorded as one undoable step.

      Rejected (throws) when:

      • parent exists but is a leaf, not a container;
      • the move would form a cycleparent is id itself or one of id's own descendants;
      • it would drop id below its new parent's layer (child may not render below its parent).

      move(id, null) — detaching to a root — is always allowed regardless of layer, since a root has no parent to render beneath.

      Parameters

      Returns void

    • Delete id and its entire subtree — every descendant is removed in the same operation. Recorded as one undoable step; undo() restores the whole subtree (root + descendants, child order intact).

      Parameters

      Returns void

    • Replace the undo/redo history from a serializeHistory() snapshot. Call on a scene whose node/layer state already matches the snapshot's head state (i.e. right after loadState from the paired scene snapshot); node state is NOT mutated. Ops re-registered via registerOp before this call round-trip; unknown kinds become no-op placeholders; external ops rebuild via the global op-factory registry and replay against the setHistoryAdapter accessor. Restored entries never coalesce with new ones. Notifies once. Do not call mid-batch: the stacks are replaced underneath the open batch, whose eventual flush would graft onto (and evict against) the restored stacks.

      Parameters

      Returns void

    • Snapshot the undo/redo history in a JSON-serializable form (the engine's SerializedHistory). Entries containing any nameless op are dropped (hand-rolled anonymous ops passed to applyBatch); kit and consumer-registered ops always carry names. Payload JSON-safety (e.g. typed arrays inside poses) is the caller's concern. Do not call mid-batch — the open batch's ops are not yet recorded.

      Returns SerializedHistory

    • Install (or clear) the active-journal accessor after scene construction. Useful when the journal source (typically a mode machine) is built with scene.history as a dependency — a chicken-and-egg situation where the accessor can't be passed in via UseSceneOptions.

      Pass null to detach. Overrides any getActiveJournal set in UseSceneOptions.

      Parameters

      Returns void

    • Install (or clear with null) the accessor for the adapter that RESTORED external ops (recorded via applyBatch, rebuilt from a restoreHistory snapshot) apply against on undo/redo. Resolved lazily at each apply, so wiring order relative to restoreHistory doesn't matter. Live applyBatch entries are unaffected (they bind their call-site adapter). If unset when a restored op applies, the op is a debug-warned no-op.

      Parameters

      • fn: (() => unknown) | null

      Returns void

    • Retag id to layer. On a container this cascades: every descendant is moved to the same layer, recorded as a single undo step.

      Invariants:

      • Layer floor — a child may not render below its parent, so retagging to a layer below the node's parent throws. Retagging to the parent's layer or any higher one is allowed; a node with no parent is unconstrained.
      • No-op elision — setting the layer a node already has does nothing and pushes no history entry.

      Parameters

      Returns void