prosemirror-model
Advanced tools
Comparing version 1.17.0 to 1.18.0
@@ -0,1 +1,9 @@ | ||
## 1.18.0 (2022-06-07) | ||
### New features | ||
Node specs for leaf nodes now support a property `leafText` which, when given, will be used by `textContent` and `textBetween` to serialize the node. | ||
Add optional type parameters to `Schema` for the node and mark names. Clarify Schema type parameters | ||
## 1.17.0 (2022-05-30) | ||
@@ -2,0 +10,0 @@ |
@@ -838,3 +838,3 @@ import OrderedMap from 'orderedmap'; | ||
*/ | ||
interface SchemaSpec { | ||
interface SchemaSpec<Nodes extends string = any, Marks extends string = any> { | ||
/** | ||
@@ -849,3 +849,3 @@ The node types in this schema. Maps names to | ||
nodes: { | ||
[name: string]: NodeSpec; | ||
[name in Nodes]: NodeSpec; | ||
} | OrderedMap<NodeSpec>; | ||
@@ -859,3 +859,3 @@ /** | ||
marks?: { | ||
[name: string]: MarkSpec; | ||
[name in Marks]: MarkSpec; | ||
} | OrderedMap<MarkSpec>; | ||
@@ -992,2 +992,9 @@ /** | ||
/** | ||
Defines the default way a [leaf node](https://prosemirror.net/docs/ref/#model.NodeType.isLeaf) of | ||
this type should be serialized to a string (as used by | ||
[`Node.textBetween`](https://prosemirror.net/docs/ref/#model.Node^textBetween) and | ||
[`Node.textContent`](https://prosemirror.net/docs/ref/#model.Node^textContent)). | ||
*/ | ||
leafText?: (node: Node) => string; | ||
/** | ||
Node specs may include arbitrary properties that can be read by | ||
@@ -1077,4 +1084,7 @@ other code via [`NodeType.spec`](https://prosemirror.net/docs/ref/#model.NodeType.spec). | ||
creating and deserializing such documents. | ||
When given, the type parameters provide the names of the nodes and | ||
marks in this schema. | ||
*/ | ||
declare class Schema { | ||
declare class Schema<Nodes extends string = any, Marks extends string = any> { | ||
/** | ||
@@ -1096,3 +1106,5 @@ The [spec](https://prosemirror.net/docs/ref/#model.SchemaSpec) on which the schema is based, | ||
nodes: { | ||
readonly [name: string]: NodeType; | ||
readonly [name in Nodes]: NodeType; | ||
} & { | ||
readonly [key: string]: NodeType; | ||
}; | ||
@@ -1103,3 +1115,5 @@ /** | ||
marks: { | ||
readonly [name: string]: MarkType; | ||
readonly [name in Marks]: MarkType; | ||
} & { | ||
readonly [key: string]: MarkType; | ||
}; | ||
@@ -1109,3 +1123,3 @@ /** | ||
*/ | ||
constructor(spec: SchemaSpec); | ||
constructor(spec: SchemaSpec<Nodes, Marks>); | ||
/** | ||
@@ -1144,3 +1158,3 @@ The type of the [default top node](https://prosemirror.net/docs/ref/#model.SchemaSpec.topNode) | ||
*/ | ||
nodeFromJSON(json: any): Node; | ||
nodeFromJSON(json: any): any; | ||
/** | ||
@@ -1438,4 +1452,5 @@ Deserialize a mark from its JSON representation. This method is | ||
`blockSeparator` is given, it will be inserted to separate text | ||
from different block nodes. When `leafText` is given, it'll be | ||
inserted for every non-text leaf node encountered. | ||
from different block nodes. If `leafText` is given, it'll be | ||
inserted for every non-text leaf node encountered, otherwise | ||
[`leafText`](https://prosemirror.net/docs/ref/#model.NodeSpec^leafText) will be used. | ||
*/ | ||
@@ -1442,0 +1457,0 @@ textBetween(from: number, to: number, blockSeparator?: string | null, leafText?: null | string | ((leafNode: Node) => string)): string; |
{ | ||
"name": "prosemirror-model", | ||
"version": "1.17.0", | ||
"version": "1.18.0", | ||
"description": "ProseMirror's document model", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -60,5 +60,9 @@ import {findDiffStart, findDiffEnd} from "./diff" | ||
separated = !blockSeparator | ||
} else if (node.isLeaf && leafText) { | ||
text += typeof leafText === 'function' ? leafText(node): leafText | ||
separated = !blockSeparator | ||
} else if (node.isLeaf) { | ||
if (leafText) { | ||
text += typeof leafText === "function" ? leafText(node) : leafText; | ||
} else if (node.type.spec.leafText) { | ||
text += node.type.spec.leafText(node); | ||
} | ||
separated = !blockSeparator; | ||
} else if (!separated && node.isBlock) { | ||
@@ -65,0 +69,0 @@ text += blockSeparator |
@@ -88,8 +88,13 @@ import {Fragment} from "./fragment" | ||
/// children. | ||
get textContent() { return this.textBetween(0, this.content.size, "") } | ||
get textContent() { | ||
return (this.isLeaf && this.type.spec.leafText) | ||
? this.type.spec.leafText(this) | ||
: this.textBetween(0, this.content.size, "") | ||
} | ||
/// Get all text between positions `from` and `to`. When | ||
/// `blockSeparator` is given, it will be inserted to separate text | ||
/// from different block nodes. When `leafText` is given, it'll be | ||
/// inserted for every non-text leaf node encountered. | ||
/// from different block nodes. If `leafText` is given, it'll be | ||
/// inserted for every non-text leaf node encountered, otherwise | ||
/// [`leafText`](#model.NodeSpec^leafText) will be used. | ||
textBetween(from: number, to: number, blockSeparator?: string | null, | ||
@@ -96,0 +101,0 @@ leafText?: null | string | ((leafNode: Node) => string)) { |
@@ -208,3 +208,3 @@ import OrderedMap from "orderedmap" | ||
/// @internal | ||
static compile(nodes: OrderedMap<NodeSpec>, schema: Schema): {readonly [name: string]: NodeType} { | ||
static compile<Nodes extends string>(nodes: OrderedMap<NodeSpec>, schema: Schema<Nodes>): {readonly [name in Nodes]: NodeType} { | ||
let result = Object.create(null) | ||
@@ -309,3 +309,3 @@ nodes.forEach((name, spec) => result[name] = new NodeType(name, schema, spec)) | ||
/// constructor. | ||
export interface SchemaSpec { | ||
export interface SchemaSpec<Nodes extends string = any, Marks extends string = any> { | ||
/// The node types in this schema. Maps names to | ||
@@ -317,3 +317,3 @@ /// [`NodeSpec`](#model.NodeSpec) objects that describe the node type | ||
/// [group](#model.NodeSpec.group). | ||
nodes: {[name: string]: NodeSpec} | OrderedMap<NodeSpec>, | ||
nodes: {[name in Nodes]: NodeSpec} | OrderedMap<NodeSpec>, | ||
@@ -324,3 +324,3 @@ /// The mark types that exist in this schema. The order in which they | ||
/// rules](#model.MarkSpec.parseDOM) are tried. | ||
marks?: {[name: string]: MarkSpec} | OrderedMap<MarkSpec> | ||
marks?: {[name in Marks]: MarkSpec} | OrderedMap<MarkSpec> | ||
@@ -434,2 +434,8 @@ /// The name of the default top-level node for the schema. Defaults | ||
/// Defines the default way a [leaf node](#model.NodeType.isLeaf) of | ||
/// this type should be serialized to a string (as used by | ||
/// [`Node.textBetween`](#model.Node^textBetween) and | ||
/// [`Node.textContent`](#model.Node^textContent)). | ||
leafText?: (node: Node) => string | ||
/// Node specs may include arbitrary properties that can be read by | ||
@@ -503,3 +509,6 @@ /// other code via [`NodeType.spec`](#model.NodeType.spec). | ||
/// creating and deserializing such documents. | ||
export class Schema { | ||
/// | ||
/// When given, the type parameters provide the names of the nodes and | ||
/// marks in this schema. | ||
export class Schema<Nodes extends string = any, Marks extends string = any> { | ||
/// The [spec](#model.SchemaSpec) on which the schema is based, | ||
@@ -517,9 +526,9 @@ /// with the added guarantee that its `nodes` and `marks` | ||
/// An object mapping the schema's node names to node type objects. | ||
nodes: {readonly [name: string]: NodeType} | ||
nodes: {readonly [name in Nodes]: NodeType} & {readonly [key: string]: NodeType} | ||
/// A map from mark names to mark type objects. | ||
marks: {readonly [name: string]: MarkType} | ||
marks: {readonly [name in Marks]: MarkType} & {readonly [key: string]: MarkType} | ||
/// Construct a schema from a schema [specification](#model.SchemaSpec). | ||
constructor(spec: SchemaSpec) { | ||
constructor(spec: SchemaSpec<Nodes, Marks>) { | ||
this.spec = { | ||
@@ -526,0 +535,0 @@ nodes: OrderedMap.from(spec.nodes), |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
444075
10891