@stesura/automation
Document automation nodes (conditionals, repeats, components) and the commands that apply
your app’s results to them. Framework-free; the editor UI is in
@stesura/automation-react. Concepts and setup are in the
Automation guide.
Schema and plugins
withAutomation
Merges the automation nodes and grants into stesuraSchema props:
stesuraSchema(withAutomation({ nodes: myNodes })). The caller’s own
grants for the same attribute are merged, not replaced.
| Name | Type | Default |
|---|---|---|
props | SchemaProps |
{ nodes: { [x: string]: StesuraNodeSpec; }; globalAttributes: { nodes: { hidden: { conditional_block: true; conditional_inline: true; repeat_block_template: true; repeat_inline_template: true; }; }; definitions?: Record<...>; }; marks?: Record<...>; }automationPlugins
Every automation plugin, for extraPlugins.
| Name | Type | Default |
|---|---|---|
options | { mode?: AutomationMode; } |
RepeatInlinePunctuation
Punctuation between inline repeat items: rendered by the node view and export, never stored as text.
Applying results
Each apply is one transaction, kept out of undo history and track changes. Run setRepeatItems first: it creates the copies the other commands address.
setVisibility
Shows or hides nodes by id: true shows, false hides, an absent id is left
alone. Applies to every node carrying the hidden attribute except repeat
templates, whose visibility follows the mode. One undo-less, untracked
transaction; false when nothing changes.
| Name | Type | Default |
|---|---|---|
visibleById | ReadonlyMap<string, boolean> |
setRepeatItems
Sets the items of repeat repeatId to keys, in that order (see
reconcileItems). An undo-less, untracked apply; false when the repeat is
missing or already matches.
| Name | Type | Default |
|---|---|---|
repeatId | string | |
keys | readonly string[] |
setFieldValues
Sets the value of field nodes. An apply; false when nothing changes.
| Name | Type | Default |
|---|---|---|
values | FieldValues |
FieldValues
type FieldValues =
| ReadonlyMap<string, string | null>
| ((field: FieldTarget) => string | null | undefined);A field’s value by name, or a resolver for fields whose value depends on where they are
(a field in a repeat item takes that item’s value). A string shows it, null goes back to
{{name}}, absent or undefined leaves the field.
FieldTarget
A field as a value resolver sees it: its name and the repeat items around it.
| Name | Type | Default |
|---|---|---|
name | string | |
frames | Frame[] | |
pos | number |
setComponentContent
Replaces the content of every component whose componentId is componentId
(it bypasses the lock), and its version when given. An apply; false when
nothing changes.
| Name | Type | Default |
|---|---|---|
componentId | string | |
content | Fragment | |
version | string |
createRepeatItem
A new item for key: a copy of the template’s content with derived ids.
| Name | Type | Default |
|---|---|---|
repeat | Node | |
key | string |
Structure
Template-mode authoring commands, and the mode itself.
insertConditional
Inserts a conditional at the cursor, or wraps the selection in one (selected
blocks for block, the selected inline content for inline). Template mode
only.
| Name | Type | Default |
|---|---|---|
kind | AutomationKind |
wrapInConditional
Wraps the selection in a conditional; false on an empty selection. Template mode only.
| Name | Type | Default |
|---|---|---|
kind | AutomationKind |
insertRepeat
Inserts a repeat whose template holds the selection (or placeholder content
at a bare cursor), with no items: items come from setRepeatItems. Template
mode only.
| Name | Type | Default |
|---|---|---|
kind | AutomationKind |
unwrapAutomationNode
Replaces the automation node at pos with its content. Template mode only.
| Name | Type | Default |
|---|---|---|
pos | number |
deleteAutomationNode
Deletes the automation node at pos with its content. Template mode only.
| Name | Type | Default |
|---|---|---|
pos | number |
addRepeatItem
Adds item key to repeat repeatId at index (default: last). False when the key exists.
| Name | Type | Default |
|---|---|---|
repeatId | string | |
key | string | |
index | number |
removeRepeatItem
Removes item key from repeat repeatId.
| Name | Type | Default |
|---|---|---|
repeatId | string | |
key | string |
AutomationKind
type AutomationKind = "block" | "inline";Whether a wrapper holds blocks or inline content.
setAutomationMode
Switches the automation mode. Entering document hides every repeat
template; entering template reveals everything, so the author sees all the
content (conditional visibility in document mode is the consumer’s, through
setVisibility). The writes are an apply: no undo, no tracking. Wrapper
chrome changes with the mode, so pagination remeasures the wrappers.
| Name | Type | Default |
|---|---|---|
mode | AutomationMode |
getAutomationMode
The automation mode; template without the plugin.
| Name | Type | Default |
|---|---|---|
state | EditorState |
AutomationMode
type AutomationMode = "template" | "document";template: authoring. Wrappers show their chrome, nothing is hidden, repeat templates are visible and editable, fields show{{name}}.document: the applied result. No chrome, conditionals hidden as the consumer decided, templates hidden, fields show their value.
Orthogonal to core’s edit/review/view EditorMode.
Diagnostics
setDiagnostics
Replaces the consumer’s diagnostics (by node id).
| Name | Type | Default |
|---|---|---|
byId | ReadonlyMap<string, readonly Diagnostic[]> |
getDiagnostics
A node’s diagnostics, consumer’s first; a stable empty array when none.
| Name | Type | Default |
|---|---|---|
state | EditorState | |
id | string |
Diagnostic
A problem shown on a node. The consumer’s carry its own message; the
package’s structural ones carry a code UIs can translate.
| Name | Type | Default |
|---|---|---|
severity | DiagnosticSeverity | |
message | string | |
code | (string & {}) | StructuralDiagnosticCode |
DiagnosticSeverity
type DiagnosticSeverity = "error" | "warning" | "info";How serious a Diagnostic is.
StructuralDiagnosticCode
type StructuralDiagnosticCode = "xref-into-conditional" | "note-shared-outside-conditional";The diagnostics the package adds itself: a visible cross-reference points into a conditional, or a note anchor inside a conditional shares its note with one outside.
Reading the document
collectAutomationNodes
Every conditional, repeat, component (by id) and field (by name), in document order, with the repeat items enclosing it. Structure only: what the consumer joins its own logic, data and values onto. Nodes without an id are left out.
| Name | Type | Default |
|---|---|---|
doc | Node |
AutomationNodeEntry
One entry of collectAutomationNodes: a conditional, repeat or
component by id, or a field by name.
Frame
An enclosing repeat item.
| Name | Type | Default |
|---|---|---|
repeatId | string | |
itemKey | string | |
index | number |
getFrames
The repeat items enclosing $pos, outermost first.
| Name | Type | Default |
|---|---|---|
$pos | ResolvedPos |
isInTemplate
Whether $pos sits inside a repeat template.
| Name | Type | Default |
|---|---|---|
$pos | ResolvedPos |
findNodeById
The first node of one of types whose id is id.
| Name | Type | Default |
|---|---|---|
doc | Node | |
id | string | |
types | ReadonlySet<string> |
nodeIdOf
A node’s id attr when it is a non-empty string.
| Name | Type | Default |
|---|---|---|
node | Node |
itemKeyOf
A repeat item’s key; attrs are unvalidated on the JSON/collab path.
| Name | Type | Default |
|---|---|---|
node | Node |
deriveItemNodeId
The id a node copied from a repeat template gets in item key: a pure
function of the template node’s id and the key, in core’s id format (12
[A-Za-z0-9]). Deterministic so a consumer can address the copies of a
template conditional (deriveItemNodeId(templateNodeId, key)) without the
document storing provenance, and so two actors applying the same keys
produce the same steps.
| Name | Type | Default |
|---|---|---|
templateNodeId | string | |
key | string |
templateNodeIdOf
The id derivedId was derived from: a node in its repeat’s template, or the
repeat itself for an item’s own id. One level: for a copy inside nested
repeats, call again on the result. Null when nothing in doc derives it.
| Name | Type | Default |
|---|---|---|
doc | Node | |
derivedId | string |
Node types
Sets of node type names, for findNodeById and for walking a document.
CONDITIONAL_NODES
const CONDITIONAL_NODES: ReadonlySet<string>conditional_block and conditional_inline.
REPEAT_NODES
const REPEAT_NODES: ReadonlySet<string>repeat_block and repeat_inline.
REPEAT_TEMPLATE_NODES
const REPEAT_TEMPLATE_NODES: ReadonlySet<string>The repeat templates: repeat_block_template and repeat_inline_template.
REPEAT_ITEM_NODES
const REPEAT_ITEM_NODES: ReadonlySet<string>The repeat items: repeat_block_item and repeat_inline_item.
COMPONENT_NODES
const COMPONENT_NODES: ReadonlySet<string>component_block and component_inline.
AUTOMATION_NODES
const AUTOMATION_NODES: ReadonlySet<string>Every automation node type name.
AutomationNodeName
type AutomationNodeName =
| "conditional_block"
| "conditional_inline"
| "repeat_block"
| "repeat_block_template"
| "repeat_block_item"
| "repeat_inline"
| "repeat_inline_template"
| "repeat_inline_item"
| "component_block"
| "component_inline";The type name of an automation node.
Your own transactions
markApply
Marks tr as an apply: kept out of undo history and track changes (applied
results are not edits), and forces numbering and cross-references to re-run.
| Name | Type | Default |
|---|---|---|
tr | Transaction |
isAutomationApply
Whether tr was marked with markApply.
| Name | Type | Default |
|---|---|---|
tr | Transaction |
Rendering and export
materialize
Turns an applied automation document into a plain Stesura document, a pure
JSON → JSON pass (like docgen’s substituteVariables):
- hidden subtrees and repeat templates are dropped;
- visible wrappers (conditionals, repeats, items, components) are replaced by their content, inline repeats with their punctuation as text;
- a field with a
valuebecomes that text; one without stays a field (forsubstituteVariables) and is reported; - cross-references into dropped content become their stored text (the label kept when the editor hid their target), else Word’s error text, and the bodies of dropped notes go;
- a container left empty is filled the way the schema requires.
The result is validated against schema, which needs no automation nodes;
it throws when the result is not a valid document.
| Name | Type | Default |
|---|---|---|
doc | JSONContent | |
__1 | { schema: Schema<any, any>; } |
MaterializeWarning
Something materialize could not render cleanly; the output is still valid.
| Name | Type | Default |
|---|---|---|
code | MaterializeWarningCode | |
message | string |
MaterializeWarningCode
type MaterializeWarningCode =
| "field-without-value"
| "reference-to-removed-content"
| "emptied-container";Why materialize warned.
automationDocx
DOCX export of a live (applied) document, for downloadDocx(…, automationDocx).
The document is materialized first: the serializer’s pre-passes see the
whole doc, so hidden footnotes would leave orphan note parts (an invalid
file) and references into hidden content would show Word’s “Reference
source not found”. The node handlers render a raw state for hosts that drop
transformDoc: visible wrappers as their content, hidden content and repeat
templates as nothing.
automationMarkdown
Markdown serialization of a live (applied) document: wrappers unwrap to
their content, hidden content and repeat templates vanish, boundaries are
dropped. One-way: markdown has no syntax for the structure, so parsing
gives plain content. For a final document prefer materialize.
| Name | Type | Default |
|---|---|---|
fenceHandlers | Readonly<Record<string, FenceHandler>> | |
nodeHandlers | Readonly<Record<string, PmNodeHandler>> |