Skip to Content

@stesura/core/stores

Per-editor nanostores  atoms, the registry that finds them, and the global user preferences.

createEditorStores({ defaults, extensionStores }) builds one FullEditorStores per editor scope: atoms for the editor mode, TOC, pagination output (page counts, page numbering, the first-layout gate), stylesheet, list configs and block handle. extensionStores is spread into the result; type it by augmenting EditorStoreExtensions. The root editor’s configPlugin (part of basePlugins / stesuraPlugins) creates and registers them with addEditorStoresToMap when its view mounts and removes them on unmount, so integrators never call these; commands and plugins reach the stores through getEditorStores(idOrState) instead of taking a stores parameter. A sub-editor’s state falls through to its parent’s stores. In React, read and write them with useEditorStoreValue / useEditorStore from @stesura/editor-react.

userPreferences holds global atoms shared by every editor. Seed their defaults with initUserPreferences before the first mount.

Functions & values

addEditorStoresToMap

Parameters:
NameTypeDefault
idstring
atomsFullEditorStores
Returns:
boolean

addMountConfigToMap

Parameters:
NameTypeDefault
idstring
stateConfigPluginState
Returns:
boolean

createEditorStores

Parameters:
NameTypeDefault
props{ defaults: Partial<EditorStoreDefaults>; extensionStores: Record<string, PreinitializedWritableAtom<unknown>>; }
Returns:
FullEditorStores

disarmAll

Parameters:
NameTypeDefault
editorIdstring
except"formatPainter" | "highlight"
Returns:
void

getEditorStores

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
FullEditorStores | undefined

getFirstLayoutReady

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
boolean | null

getFirstLayoutReadyAtom

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
WritableAtom<boolean | null> | undefined

getMountConfig

Parameters:
NameTypeDefault
idstring
Returns:
ConfigPluginState | undefined

getNodePageNumbersStore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
Map<string, string> | undefined

getPageCountsStore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
Map<string, number> | undefined

getPageNumberingStore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
Map<string, PageNumberingAttrs> | undefined

getProofReadApiFailedFor

Parameters:
NameTypeDefault
editorIdstring
Returns:
boolean

getProofReadLoadingFor

Parameters:
NameTypeDefault
editorIdstring
Returns:
boolean

getRootEditorView

Parameters:
NameTypeDefault
idstring
Returns:
EditorView | undefined

getTocBreaksStore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
Map<string, TOCbreakEntry[]> | undefined

getTOCstore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
TOCstate | undefined

headerFooterEditorRegistry

NameTypeDefault
atomWritableAtom<ActiveSubEditor<HeaderFooterEditorKey>[]>
keyId(key: HeaderFooterEditorKey) => string

Stable identity for a key within one main editor.

register(entry: ActiveSubEditor<HeaderFooterEditorKey>) => void
unregister(mainEditorId: string, key: HeaderFooterEditorKey) => void
getAll(mainEditorId: string) => ActiveSubEditor<HeaderFooterEditorKey>[]

Entries belonging to mainEditorId only.

get(mainEditorId: string, key: HeaderFooterEditorKey) => ActiveSubEditor<HeaderFooterEditorKey> | undefined

The registered editor bound to key under mainEditorId, if mounted.

initUserPreferences

Parameters:
NameTypeDefault
defaultsPartial<UserPreferences>
Returns:
void

proofReadApiFailedByEditor

NameTypeDefault
valueRecord<string, boolean>
eqKey<Key extends string>(oldValue: (Key extends string ? boolean : never) | undefined, newValue: (Key extends string ? boolean : never) | undefined, key: Key) => boolean

Compares the previous and next values for a key on setKey.

Returning true means the values are the same, so the store keeps the old value and no listener is called.

One function serves every key of the map, so the key name comes as the third argument. The old value is undefined when the key is not in the map yet.

Object.is
listen(listener: (value: Readonly<Record<string, boolean>>, oldValue: Readonly<Record<string, boolean>> | undefined, changedKey: string | undefined) => void) => () => void

Subscribe to store changes.

In contrast with Store#subscribe it do not call listener immediately.

notify(oldValue?: Readonly<Record<string, boolean>> | undefined, changedKey?: string | undefined) => void

Low-level method to notify listeners about changes in the store.

Can cause unexpected behaviour when combined with frontend frameworks that perform equality checks for values, such as React.

set(newValue: Record<string, boolean>) => void

Change store value.

$settings.set({ theme: 'dark' })

Operation is atomic, subscribers will be notified once with the new value. changedKey will be undefined

setKey<Key extends string>(key: Key, value: (Key extends string ? boolean : never) | undefined) => void

Change key in store value.

$settings.setKey('theme', 'dark')

To delete key set undefined.

$settings.setKey('theme', undefined)
subscribe(listener: (value: Readonly<Record<string, boolean>>, oldValue: Readonly<Record<string, boolean>> | undefined, changedKey: string | undefined) => void) => () => void

Subscribe to store changes and call listener immediately.

import { $router } from '../store' $router.subscribe(page => { console.log(page) })
eq(oldValue: Readonly<Record<string, boolean>> | undefined, newValue: Readonly<Record<string, boolean>>) => boolean

Compares the previous and next values on every set.

Returning true means the values are the same, so the store keeps the old value and no listener is called.

Object.is
get() => Record<string, boolean>

Get store value.

In contrast with ReadableAtom#value this value will be always initialized even if store had no listeners.

$store.get()
initRecord<string, boolean> | undefined

Low-level attribute to read store’s initial value.

lcnumber

Listeners count.

off() => void

Unbind all listeners.

proofReadLoadingByEditor

NameTypeDefault
valueRecord<string, boolean>
eqKey<Key extends string>(oldValue: (Key extends string ? boolean : never) | undefined, newValue: (Key extends string ? boolean : never) | undefined, key: Key) => boolean

Compares the previous and next values for a key on setKey.

Returning true means the values are the same, so the store keeps the old value and no listener is called.

One function serves every key of the map, so the key name comes as the third argument. The old value is undefined when the key is not in the map yet.

Object.is
listen(listener: (value: Readonly<Record<string, boolean>>, oldValue: Readonly<Record<string, boolean>> | undefined, changedKey: string | undefined) => void) => () => void

Subscribe to store changes.

In contrast with Store#subscribe it do not call listener immediately.

notify(oldValue?: Readonly<Record<string, boolean>> | undefined, changedKey?: string | undefined) => void

Low-level method to notify listeners about changes in the store.

Can cause unexpected behaviour when combined with frontend frameworks that perform equality checks for values, such as React.

set(newValue: Record<string, boolean>) => void

Change store value.

$settings.set({ theme: 'dark' })

Operation is atomic, subscribers will be notified once with the new value. changedKey will be undefined

setKey<Key extends string>(key: Key, value: (Key extends string ? boolean : never) | undefined) => void

Change key in store value.

$settings.setKey('theme', 'dark')

To delete key set undefined.

$settings.setKey('theme', undefined)
subscribe(listener: (value: Readonly<Record<string, boolean>>, oldValue: Readonly<Record<string, boolean>> | undefined, changedKey: string | undefined) => void) => () => void

Subscribe to store changes and call listener immediately.

import { $router } from '../store' $router.subscribe(page => { console.log(page) })
eq(oldValue: Readonly<Record<string, boolean>> | undefined, newValue: Readonly<Record<string, boolean>>) => boolean

Compares the previous and next values on every set.

Returning true means the values are the same, so the store keeps the old value and no listener is called.

Object.is
get() => Record<string, boolean>

Get store value.

In contrast with ReadableAtom#value this value will be always initialized even if store had no listeners.

$store.get()
initRecord<string, boolean> | undefined

Low-level attribute to read store’s initial value.

lcnumber

Listeners count.

off() => void

Unbind all listeners.

registerArmedMode

Parameters:
NameTypeDefault
editorIdstring
key"formatPainter" | "highlight"
disarm() => void
Returns:
() => void

registerRootEditorView

Parameters:
NameTypeDefault
idstring
viewEditorView
Returns:
void

registryVersion

NameTypeDefault
valuenumber

Low-level method to read store’s value without calling onStart.

Try to use only ReadableAtom#get . Without subscribers, value can be undefined.

set(newValue: number) => void

Change store value.

$router.set({ path: location.pathname, page: parse(location.pathname) })
eq(oldValue: number | undefined, newValue: number) => boolean

Compares the previous and next values on every set.

Returning true means the values are the same, so the store keeps the old value and no listener is called.

Object.is
get() => number

Get store value.

In contrast with ReadableAtom#value this value will be always initialized even if store had no listeners.

$store.get()
initnumber | undefined

Low-level attribute to read store’s initial value.

lcnumber

Listeners count.

listen(listener: (value: number, oldValue: number | undefined) => void) => () => void

Subscribe to store changes.

In contrast with Store#subscribe it do not call listener immediately.

notify(oldValue?: number | undefined) => void

Low-level method to notify listeners about changes in the store.

Can cause unexpected behaviour when combined with frontend frameworks that perform equality checks for values, such as React.

off() => void

Unbind all listeners.

subscribe(listener: (value: number, oldValue?: number | undefined) => void) => () => void

Subscribe to store changes and call listener immediately.

import { $router } from '../store' $router.subscribe(page => { console.log(page) })

removeEditorStoresFromMap

Parameters:
NameTypeDefault
idstring
Returns:
void

removeMountConfigFromMap

Parameters:
NameTypeDefault
idstring
Returns:
void

resolveEditorId

Parameters:
NameTypeDefault
idOrStatestring | EditorState
Returns:
string | undefined

setFirstLayoutReady

Parameters:
NameTypeDefault
idOrStatestring | EditorState
readyboolean
Returns:
void

setNodePageNumbersStore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
pageNumbersMap<string, string>
Returns:
void

setPageCountsStore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
pageCountsMap<string, number>
Returns:
void

setPageNumberingStore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
pageNumberingMap<string, PageNumberingAttrs>
Returns:
void

setProofReadApiFailedFor

Parameters:
NameTypeDefault
editorIdstring
valueboolean
Returns:
void

setProofReadLoadingFor

Parameters:
NameTypeDefault
editorIdstring
valueboolean
Returns:
void

setTocBreaksStore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
breaksMap<string, TOCbreakEntry[]>
Returns:
void

setTOCstore

Parameters:
NameTypeDefault
idOrStatestring | EditorState
tocTOCstate
Returns:
void

unregisterRootEditorView

Parameters:
NameTypeDefault
idstring
Returns:
void

userPreferences

NameTypeDefault
showOutlinePreinitializedWritableAtom<boolean> & object
showRulerPreinitializedWritableAtom<boolean> & object
zoomLevelPreinitializedWritableAtom<number> & object
trackChangesPreinitializedWritableAtom<{ show: boolean; enabled: boolean; }> & object
showCommentsPreinitializedWritableAtom<CommentDisplayMode> & object
displayUnitsPreinitializedWritableAtom<DisplayUnits> & object
proofReadEnabledPreinitializedWritableAtom<boolean> & object
proofReadLoadingPreinitializedWritableAtom<boolean> & object
proofReadApiFailedPreinitializedWritableAtom<boolean> & object

Types

HeaderFooterEditorKey

NameTypeDefault
sectionIdstring
target"header" | "footer"
variantHeaderFooterVariant

Other exports

ExportTypeDescription
ARMED_MODE_KEYSreadonly ["formatPainter", "highlight"]The modes that can be armed. ArmedModeKey is its element type.
ArmedModeKey"formatPainter" | "highlight"
UserPreferenceKey"showOutline" | "showRuler" | "showComments" | "displayUnits" | "proofReadEnabled" | "zoomLevel" | "trackChanges" | "proofReadLoading" | "proofReadApiFailed"
Last updated on