Type Editor / @type-editor/model / schema/Schema / Schema
Class: Schema<Nodes, Marks>
Defined in: packages/model/src/schema/Schema.ts:28
A document schema. Holds node and mark type objects for the nodes and marks that may occur in conforming documents, and provides functionality for creating and deserializing such documents.
When given, the type parameters provide the names of the nodes and marks in this schema.
Type Parameters
| Type Parameter | Default type |
|---|---|
Nodes extends string | string |
Marks extends string | string |
Constructors
Constructor
new Schema<Nodes, Marks>(spec): Schema<Nodes, Marks>;Defined in: packages/model/src/schema/Schema.ts:90
Constructs a schema from a schema specification. Compiles node and mark types, validates the schema structure, and initializes content matching and mark restrictions.
Parameters
| Parameter | Type | Description |
|---|---|---|
spec | SchemaSpec<Nodes, Marks> | The schema specification containing node and mark definitions |
Returns
Schema<Nodes, Marks>
Throws
If the schema is invalid (e.g., duplicate node/mark names, multiple linebreak nodes, invalid linebreak node configuration)
Accessors
cached
Get Signature
get cached(): Record<string, any>;Defined in: packages/model/src/schema/Schema.ts:165
An object for storing cached values computed from the schema. Modules can use this to cache expensive computations. Use unique property names to avoid conflicts.
Returns
Record<string, any>
linebreakReplacement
Get Signature
get linebreakReplacement(): NodeType;Defined in: packages/model/src/schema/Schema.ts:148
The linebreak replacement node type, if one is defined in this schema. This node type is used to replace newline characters in pasted content.
Returns
markFromJSON
Get Signature
get markFromJSON(): (json) => Mark;Defined in: packages/model/src/schema/Schema.ts:183
Deserialize a mark from its JSON representation. This method is bound.
Returns
(json): Mark;Parameters
| Parameter | Type |
|---|---|
json | MarkJSON |
Returns
marks
Get Signature
get marks(): Readonly<Record<Marks, MarkType>> & Readonly<Record<string, MarkType>>;Defined in: packages/model/src/schema/Schema.ts:140
An object mapping mark names to MarkType instances. Contains all mark types defined in this schema.
Returns
Readonly<Record<Marks, MarkType>> & Readonly<Record<string, MarkType>>
nodeFromJSON
Get Signature
get nodeFromJSON(): (json) => Node;Defined in: packages/model/src/schema/Schema.ts:175
Deserialize a node from its JSON representation. This method is bound.
NodeJSON as array for backwards compatibility
Returns
(json): Node;Parameters
| Parameter | Type |
|---|---|
json | | NodeJSON | NodeJSON[] |
Returns
nodes
Get Signature
get nodes(): Readonly<Record<Nodes, NodeType>> & Readonly<Record<string, NodeType>>;Defined in: packages/model/src/schema/Schema.ts:132
An object mapping node names to NodeType instances. Contains all node types defined in this schema.
Returns
Readonly<Record<Nodes, NodeType>> & Readonly<Record<string, NodeType>>
spec
Get Signature
get spec(): BasicSchemaSpec<NodeSpec, MarkSpec>;Defined in: packages/model/src/schema/Schema.ts:124
The schema specification this schema is based on, with nodes and marks converted to OrderedMap instances for consistent ordering.
Returns
BasicSchemaSpec<NodeSpec, MarkSpec>
topNodeType
Get Signature
get topNodeType(): NodeType;Defined in: packages/model/src/schema/Schema.ts:156
The top-level node type for this schema (defaults to 'doc'). This is the root node type that contains the document content.
Returns
Methods
mark()
mark(type, attrs?): Mark;Defined in: packages/model/src/schema/Schema.ts:236
Create a mark with the given type and attributes.
Parameters
| Parameter | Type | Description |
|---|---|---|
type | string | MarkType | The mark type name or MarkType instance |
attrs? | Attrs | Attribute values for the mark, or undefined to use defaults |
Returns
A new Mark instance
Throws
If the mark type is not found in this schema
node()
node(
type,
attrs?,
content?,
marks?): Node;Defined in: packages/model/src/schema/Schema.ts:200
Create a node in this schema. The type may be a string or a NodeType instance. Attributes will be extended with defaults, content may be a Fragment, null, a Node, or an array of nodes.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
type | string | NodeType | undefined | The node type name or NodeType instance |
attrs | Readonly<Record<string, any>> | null | Attribute values for the node, or null to use defaults |
content? | | Fragment | Node | readonly Node[] | undefined | Fragment, single node, array of nodes, or undefined for empty content |
marks? | readonly Mark[] | undefined | Array of marks to apply to the node, or undefined for no marks |
Returns
A new Node instance
Throws
If the type is invalid or from a different schema
nodeType()
nodeType(name): NodeType;Defined in: packages/model/src/schema/Schema.ts:254
Retrieves a node type by name.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | string | The name of the node type to retrieve |
Returns
The NodeType instance
Throws
If the node type is not found in this schema
text()
text(text, marks?): Node;Defined in: packages/model/src/schema/Schema.ts:223
Create a text node in this schema. Empty text nodes are not allowed.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | string | The text content for the node |
marks? | readonly Mark[] | Array of marks to apply to the text node, or undefined for no marks |
Returns
A new text Node instance