@mocanvas/editor · class
declare class EditorAtom<T>A piece of reactive state that belongs to an editor rather than to a module.
Library code often wants "one value per editor" — which overlay is hovered,
whether a tool has armed itself, a cached measurement. A module-level atom
is wrong (two editors on one page would share it) and an instance field is
wrong too when the code holding the value is not part of the Editor class.
EditorAtom is the middle: declare it once at module scope, and every editor
that touches it gets its own atom, created on first use and dropped with the
editor.
``ts
const hoveredId = new EditorAtom<string | null>("hoveredOverlay", () => null)
hoveredId.set(editor, "brush")
hoveredId.get(editor) // reactive read
``
SEMANTICS-ASSUMED: the per-editor atoms are held in a WeakMap, so nothing
here keeps a disposed editor alive. The initial value is produced lazily, per
editor, so a mutable default (an array, a Set) is not shared between them —
the trap a plain initialValue parameter would set.
Methods · 5
constructor(name: string, getInitialValue: (editor: Editor) => T)@param name - Debug label; the per-editor atoms are named after it. @param getInitialValue - Produces the starting value for an editor that has not touched this state yet. Called at most once per editor.
get(editor: Editor): TRead the value. Reactive: reading inside a signal subscribes to it.
getAtom(editor: Editor): Atom<T>The underlying atom for editor, created on first use.
set(editor: Editor, value: T): TReplace the value.
update(editor: Editor, updater: (value: T) => T): TDerive the next value from the current one.
Mentioned here
mocanvas is built by Symbio. Free for non-commercial, non-profit and development use; commercial use needs an agreement. Not affiliated with or endorsed by tldraw.