@folijs/core 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/README.md +1 -1
  3. package/dist/document.d.ts +10 -15
  4. package/dist/document.d.ts.map +1 -1
  5. package/dist/document.js +41 -19
  6. package/dist/document.js.map +1 -1
  7. package/dist/index.d.ts +3 -2
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +3 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/model/attrs.d.ts +5 -0
  12. package/dist/model/attrs.d.ts.map +1 -1
  13. package/dist/model/attrs.js +17 -0
  14. package/dist/model/attrs.js.map +1 -1
  15. package/dist/model/content.d.ts +10 -0
  16. package/dist/model/content.d.ts.map +1 -1
  17. package/dist/model/content.js +44 -0
  18. package/dist/model/content.js.map +1 -1
  19. package/dist/model/fragment.d.ts +7 -2
  20. package/dist/model/fragment.d.ts.map +1 -1
  21. package/dist/model/fragment.js +13 -2
  22. package/dist/model/fragment.js.map +1 -1
  23. package/dist/model/index.d.ts +1 -1
  24. package/dist/model/index.d.ts.map +1 -1
  25. package/dist/model/index.js +1 -1
  26. package/dist/model/index.js.map +1 -1
  27. package/dist/model/mark.d.ts.map +1 -1
  28. package/dist/model/mark.js +4 -3
  29. package/dist/model/mark.js.map +1 -1
  30. package/dist/model/node.d.ts +13 -1
  31. package/dist/model/node.d.ts.map +1 -1
  32. package/dist/model/node.js +35 -3
  33. package/dist/model/node.js.map +1 -1
  34. package/dist/model/resolvedpos.d.ts +26 -2
  35. package/dist/model/resolvedpos.d.ts.map +1 -1
  36. package/dist/model/resolvedpos.js +54 -2
  37. package/dist/model/resolvedpos.js.map +1 -1
  38. package/dist/model/schema.d.ts +25 -0
  39. package/dist/model/schema.d.ts.map +1 -1
  40. package/dist/model/schema.js +23 -0
  41. package/dist/model/schema.js.map +1 -1
  42. package/dist/model/slice.d.ts +7 -0
  43. package/dist/model/slice.d.ts.map +1 -1
  44. package/dist/model/slice.js +53 -0
  45. package/dist/model/slice.js.map +1 -1
  46. package/dist/schema.d.ts +72 -8
  47. package/dist/schema.d.ts.map +1 -1
  48. package/dist/schema.js +93 -9
  49. package/dist/schema.js.map +1 -1
  50. package/dist/state/attempt.d.ts +4 -0
  51. package/dist/state/attempt.d.ts.map +1 -0
  52. package/dist/state/attempt.js +19 -0
  53. package/dist/state/attempt.js.map +1 -0
  54. package/dist/state/commands.d.ts +21 -5
  55. package/dist/state/commands.d.ts.map +1 -1
  56. package/dist/state/commands.js +260 -68
  57. package/dist/state/commands.js.map +1 -1
  58. package/dist/state/editor.d.ts +41 -7
  59. package/dist/state/editor.d.ts.map +1 -1
  60. package/dist/state/editor.js +51 -10
  61. package/dist/state/editor.js.map +1 -1
  62. package/dist/state/format.d.ts +13 -0
  63. package/dist/state/format.d.ts.map +1 -0
  64. package/dist/state/format.js +20 -0
  65. package/dist/state/format.js.map +1 -0
  66. package/dist/state/history.js +2 -2
  67. package/dist/state/history.js.map +1 -1
  68. package/dist/state/index.d.ts +10 -3
  69. package/dist/state/index.d.ts.map +1 -1
  70. package/dist/state/index.js +10 -3
  71. package/dist/state/index.js.map +1 -1
  72. package/dist/state/links.d.ts +42 -0
  73. package/dist/state/links.d.ts.map +1 -0
  74. package/dist/state/links.js +194 -0
  75. package/dist/state/links.js.map +1 -0
  76. package/dist/state/lists.d.ts +23 -0
  77. package/dist/state/lists.d.ts.map +1 -0
  78. package/dist/state/lists.js +34 -0
  79. package/dist/state/lists.js.map +1 -0
  80. package/dist/state/mark.d.ts +25 -0
  81. package/dist/state/mark.d.ts.map +1 -0
  82. package/dist/state/mark.js +40 -0
  83. package/dist/state/mark.js.map +1 -0
  84. package/dist/state/selection.d.ts +18 -2
  85. package/dist/state/selection.d.ts.map +1 -1
  86. package/dist/state/selection.js +63 -2
  87. package/dist/state/selection.js.map +1 -1
  88. package/dist/state/state.d.ts.map +1 -1
  89. package/dist/state/state.js +23 -2
  90. package/dist/state/state.js.map +1 -1
  91. package/dist/state/word.d.ts +72 -0
  92. package/dist/state/word.d.ts.map +1 -0
  93. package/dist/state/word.js +173 -0
  94. package/dist/state/word.js.map +1 -0
  95. package/dist/state/wordclip.d.ts +43 -0
  96. package/dist/state/wordclip.d.ts.map +1 -0
  97. package/dist/state/wordclip.js +412 -0
  98. package/dist/state/wordclip.js.map +1 -0
  99. package/dist/state/wordformat.d.ts +136 -0
  100. package/dist/state/wordformat.d.ts.map +1 -0
  101. package/dist/state/wordformat.js +846 -0
  102. package/dist/state/wordformat.js.map +1 -0
  103. package/dist/transform/index.d.ts +2 -1
  104. package/dist/transform/index.d.ts.map +1 -1
  105. package/dist/transform/index.js +2 -1
  106. package/dist/transform/index.js.map +1 -1
  107. package/dist/transform/stepmap.d.ts +8 -6
  108. package/dist/transform/stepmap.d.ts.map +1 -1
  109. package/dist/transform/stepmap.js +18 -15
  110. package/dist/transform/stepmap.js.map +1 -1
  111. package/dist/transform/steps.d.ts +27 -2
  112. package/dist/transform/steps.d.ts.map +1 -1
  113. package/dist/transform/steps.js +115 -4
  114. package/dist/transform/steps.js.map +1 -1
  115. package/dist/transform/structure.d.ts +26 -0
  116. package/dist/transform/structure.d.ts.map +1 -0
  117. package/dist/transform/structure.js +116 -0
  118. package/dist/transform/structure.js.map +1 -0
  119. package/dist/transform/transform.d.ts +34 -11
  120. package/dist/transform/transform.d.ts.map +1 -1
  121. package/dist/transform/transform.js +145 -18
  122. package/dist/transform/transform.js.map +1 -1
  123. package/dist/v3/check.d.ts +33 -0
  124. package/dist/v3/check.d.ts.map +1 -0
  125. package/dist/v3/check.js +90 -0
  126. package/dist/v3/check.js.map +1 -0
  127. package/dist/v3/document.d.ts +217 -0
  128. package/dist/v3/document.d.ts.map +1 -0
  129. package/dist/v3/document.js +225 -0
  130. package/dist/v3/document.js.map +1 -0
  131. package/dist/v3/index.d.ts +6 -0
  132. package/dist/v3/index.d.ts.map +1 -0
  133. package/dist/v3/index.js +6 -0
  134. package/dist/v3/index.js.map +1 -0
  135. package/dist/v3/migrate.d.ts +19 -0
  136. package/dist/v3/migrate.d.ts.map +1 -0
  137. package/dist/v3/migrate.js +227 -0
  138. package/dist/v3/migrate.js.map +1 -0
  139. package/dist/v3/resolve.d.ts +129 -0
  140. package/dist/v3/resolve.d.ts.map +1 -0
  141. package/dist/v3/resolve.js +534 -0
  142. package/dist/v3/resolve.js.map +1 -0
  143. package/dist/v3/schema.d.ts +27 -0
  144. package/dist/v3/schema.d.ts.map +1 -0
  145. package/dist/v3/schema.js +429 -0
  146. package/dist/v3/schema.js.map +1 -0
  147. package/dist/v3/units.d.ts +16 -0
  148. package/dist/v3/units.d.ts.map +1 -0
  149. package/dist/v3/units.js +28 -0
  150. package/dist/v3/units.js.map +1 -0
  151. package/package.json +1 -1
  152. package/src/document.ts +49 -23
  153. package/src/index.ts +17 -6
  154. package/src/model/attrs.ts +19 -0
  155. package/src/model/content.ts +56 -0
  156. package/src/model/fragment.ts +18 -3
  157. package/src/model/index.ts +1 -1
  158. package/src/model/mark.ts +3 -2
  159. package/src/model/node.ts +48 -3
  160. package/src/model/resolvedpos.ts +55 -2
  161. package/src/model/schema.ts +43 -0
  162. package/src/model/slice.ts +81 -1
  163. package/src/schema.ts +148 -13
  164. package/src/state/attempt.ts +22 -0
  165. package/src/state/commands.ts +287 -69
  166. package/src/state/editor.ts +103 -21
  167. package/src/state/format.ts +35 -0
  168. package/src/state/history.ts +3 -3
  169. package/src/state/index.ts +63 -1
  170. package/src/state/links.ts +193 -0
  171. package/src/state/lists.ts +50 -0
  172. package/src/state/mark.ts +47 -0
  173. package/src/state/selection.ts +72 -3
  174. package/src/state/state.ts +22 -2
  175. package/src/state/word.ts +245 -0
  176. package/src/state/wordclip.ts +445 -0
  177. package/src/state/wordformat.ts +982 -0
  178. package/src/transform/index.ts +8 -0
  179. package/src/transform/stepmap.ts +18 -12
  180. package/src/transform/steps.ts +126 -3
  181. package/src/transform/structure.ts +135 -0
  182. package/src/transform/transform.ts +165 -16
  183. package/src/v3/check.ts +119 -0
  184. package/src/v3/document.ts +456 -0
  185. package/src/v3/index.ts +33 -0
  186. package/src/v3/migrate.ts +267 -0
  187. package/src/v3/resolve.ts +646 -0
  188. package/src/v3/schema.ts +479 -0
  189. package/src/v3/units.ts +34 -0
@@ -37,6 +37,25 @@ export function computeAttrs(
37
37
  return attrs;
38
38
  }
39
39
 
40
+ /**
41
+ * The persisted form of `attrs`: only attributes that differ from their default (required ones
42
+ * always). Undefined when none remain, so a paragraph with default alignment has no `attrs`.
43
+ */
44
+ export function attrsToJSON(
45
+ specs: AttributeSpecs | undefined,
46
+ attrs: Attrs,
47
+ ): Record<string, unknown> | undefined {
48
+ if (!specs) return undefined;
49
+ let json: Record<string, unknown> | undefined;
50
+ for (const [name, spec] of Object.entries(specs)) {
51
+ const value = attrs[name];
52
+ if ("default" in spec && deepEqual(value, spec.default)) continue;
53
+ json ??= {};
54
+ json[name] = value;
55
+ }
56
+ return json;
57
+ }
58
+
40
59
  /** Structural equality for JSON-like attribute values. */
41
60
  export function deepEqual(a: unknown, b: unknown): boolean {
42
61
  if (a === b) return true;
@@ -79,6 +79,62 @@ export class ContentMatch {
79
79
  return match;
80
80
  }
81
81
 
82
+ /** The node types that may come next, in expression order, each once. */
83
+ get nextTypes(): readonly NodeType[] {
84
+ const types: NodeType[] = [];
85
+ for (const state of this.states) {
86
+ for (const type of this.program.elements[state]?.types ?? []) {
87
+ if (!types.includes(type)) types.push(type);
88
+ }
89
+ }
90
+ return types;
91
+ }
92
+
93
+ /** The first type that may come next and can be created without attributes or content. */
94
+ get defaultType(): NodeType | null {
95
+ return (
96
+ this.nextTypes.find(
97
+ (type) => !type.isText && !type.hasRequiredAttrs() && type.contentMatch.validEnd,
98
+ ) ?? null
99
+ );
100
+ }
101
+
102
+ /**
103
+ * The shortest chain of wrapper types (outermost first) that makes a node of `target` fit
104
+ * here: `[]` when it fits as is, null when no chain of creatable types does. Wrappers must
105
+ * be creatable without attributes; inner wrappers must leave their parent complete.
106
+ */
107
+ findWrapping(target: NodeType): readonly NodeType[] | null {
108
+ interface Step {
109
+ match: ContentMatch;
110
+ type: NodeType | null;
111
+ via: Step | null;
112
+ }
113
+ const seen = new Set<NodeType>();
114
+ const active: Step[] = [{ match: this, type: null, via: null }];
115
+ while (active.length) {
116
+ const current = active.shift() as Step;
117
+ if (current.match.matchType(target)) {
118
+ const result: NodeType[] = [];
119
+ for (let step: Step | null = current; step?.type; step = step.via) result.push(step.type);
120
+ return result.reverse();
121
+ }
122
+ for (const type of current.match.nextTypes) {
123
+ const next = current.match.matchType(type);
124
+ if (
125
+ !type.isLeaf &&
126
+ !type.hasRequiredAttrs() &&
127
+ !seen.has(type) &&
128
+ (!current.type || next?.validEnd)
129
+ ) {
130
+ seen.add(type);
131
+ active.push({ match: type.contentMatch, type, via: current });
132
+ }
133
+ }
134
+ }
135
+ return null;
136
+ }
137
+
82
138
  /** True when the expression's node types are inline (all are, or none are). */
83
139
  get inlineContent(): boolean {
84
140
  return this.program.elements[0]?.types[0]?.isInline ?? false;
@@ -97,8 +97,16 @@ export class Fragment {
97
97
  }
98
98
  }
99
99
 
100
- /** Text between two positions; `blockSeparator` goes between textblocks, `leafText` for leaves. */
101
- textBetween(from: number, to: number, blockSeparator = "", leafText = ""): string {
100
+ /**
101
+ * Text between two positions; `blockSeparator` goes between textblocks, `leafText` for leaves
102
+ * (a string, or one per leaf).
103
+ */
104
+ textBetween(
105
+ from: number,
106
+ to: number,
107
+ blockSeparator = "",
108
+ leafText: string | ((leaf: Node) => string) = "",
109
+ ): string {
102
110
  let text = "";
103
111
  let first = true;
104
112
  this.nodesBetween(from, to, (node, pos) => {
@@ -108,7 +116,7 @@ export class Fragment {
108
116
  } else if (node.isText) {
109
117
  text += node.textContent.slice(Math.max(from, pos) - pos, to - pos);
110
118
  } else if (node.isLeaf) {
111
- text += leafText;
119
+ text += typeof leafText === "string" ? leafText : leafText(node);
112
120
  }
113
121
  return undefined;
114
122
  });
@@ -145,6 +153,13 @@ export class Fragment {
145
153
  return new Fragment(result, size);
146
154
  }
147
155
 
156
+ /** The children from index `from` to `to`. */
157
+ cutByIndex(from: number, to: number): Fragment {
158
+ if (from === to) return Fragment.empty;
159
+ if (from === 0 && to === this.content.length) return this;
160
+ return Fragment.fromArray(this.content.slice(from, to));
161
+ }
162
+
148
163
  /** A copy with the child at `index` replaced. Does not merge text; callers keep normal form. */
149
164
  replaceChild(index: number, node: Node): Fragment {
150
165
  const current = this.child(index);
@@ -4,7 +4,7 @@ export { FoliDocumentError, ReplaceError } from "./errors.js";
4
4
  export { Fragment } from "./fragment.js";
5
5
  export { Mark, type MarkJSON } from "./mark.js";
6
6
  export { Node, type NodeJSON, TextNode } from "./node.js";
7
- export { ResolvedPos } from "./resolvedpos.js";
7
+ export { NodeRange, ResolvedPos } from "./resolvedpos.js";
8
8
  export {
9
9
  type MarkSpec,
10
10
  MarkType,
package/src/model/mark.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Attrs, deepEqual } from "./attrs.js";
1
+ import { type Attrs, attrsToJSON, deepEqual } from "./attrs.js";
2
2
  import type { MarkType } from "./schema.js";
3
3
 
4
4
  export interface MarkJSON {
@@ -60,7 +60,8 @@ export class Mark {
60
60
 
61
61
  toJSON(): MarkJSON {
62
62
  const json: MarkJSON = { type: this.type.name };
63
- if (this.type.spec.attrs) json.attrs = { ...this.attrs };
63
+ const attrs = attrsToJSON(this.type.spec.attrs, this.attrs);
64
+ if (attrs) json.attrs = attrs;
64
65
  return json;
65
66
  }
66
67
 
package/src/model/node.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { type Attrs, deepEqual, NO_ATTRS } from "./attrs.js";
1
+ import { type Attrs, attrsToJSON, deepEqual, NO_ATTRS } from "./attrs.js";
2
+ import type { ContentMatch } from "./content.js";
2
3
  import { FoliDocumentError } from "./errors.js";
3
4
  import { Fragment } from "./fragment.js";
4
5
  import { Mark, type MarkJSON } from "./mark.js";
@@ -71,6 +72,11 @@ export class Node {
71
72
  return this.type.isLeaf;
72
73
  }
73
74
 
75
+ /** Edited as one unit: leaf nodes and `atom` node types. */
76
+ get isAtom(): boolean {
77
+ return this.type.isAtom;
78
+ }
79
+
74
80
  /** True if this node holds inline content (a caret can sit directly inside it). */
75
81
  get inlineContent(): boolean {
76
82
  return this.type.inlineContent;
@@ -109,7 +115,12 @@ export class Node {
109
115
  this.nodesBetween(0, this.content.size, f);
110
116
  }
111
117
 
112
- textBetween(from: number, to: number, blockSeparator?: string, leafText?: string): string {
118
+ textBetween(
119
+ from: number,
120
+ to: number,
121
+ blockSeparator?: string,
122
+ leafText?: string | ((leaf: Node) => string),
123
+ ): string {
113
124
  return this.content.textBetween(from, to, blockSeparator, leafText);
114
125
  }
115
126
 
@@ -132,6 +143,39 @@ export class Node {
132
143
  return content === this.content ? this : new Node(this.type, this.attrs, content, this.marks);
133
144
  }
134
145
 
146
+ /** The content match after the first `index` children. */
147
+ contentMatchAt(index: number): ContentMatch {
148
+ const match = this.type.contentMatch.matchFragment(this.content, 0, index);
149
+ if (!match) throw new RangeError(`Invalid content in ${this.type.name}`);
150
+ return match;
151
+ }
152
+
153
+ /**
154
+ * Whether replacing children `from..to` with `replacement` (its children `start..end`) leaves
155
+ * valid content: allowed types in a valid order, allowed marks.
156
+ */
157
+ canReplace(
158
+ from: number,
159
+ to: number,
160
+ replacement: Fragment = Fragment.empty,
161
+ start = 0,
162
+ end = replacement.childCount,
163
+ ): boolean {
164
+ const one = this.contentMatchAt(from).matchFragment(replacement, start, end);
165
+ const two = one?.matchFragment(this.content, to);
166
+ if (!two?.validEnd) return false;
167
+ for (let i = start; i < end; i++) {
168
+ if (!this.type.allowsMarks(replacement.child(i).marks)) return false;
169
+ }
170
+ return true;
171
+ }
172
+
173
+ /** Whether replacing children `from..to` with one node of `type` leaves valid content. */
174
+ canReplaceWith(from: number, to: number, type: NodeType): boolean {
175
+ const start = this.contentMatchAt(from).matchType(type);
176
+ return !!start?.matchFragment(this.content, to)?.validEnd;
177
+ }
178
+
135
179
  /** Same node with the given (canonical) mark set. */
136
180
  mark(marks: readonly Mark[]): Node {
137
181
  return Mark.sameSet(marks, this.marks)
@@ -195,7 +239,8 @@ export class Node {
195
239
 
196
240
  toJSON(): NodeJSON {
197
241
  const json: NodeJSON = { type: this.type.name };
198
- if (this.type.spec.attrs) json.attrs = { ...this.attrs };
242
+ const attrs = attrsToJSON(this.type.spec.attrs, this.attrs);
243
+ if (attrs) json.attrs = attrs;
199
244
  if (this.content.size) json.content = this.content.toJSON();
200
245
  if (this.marks.length) json.marks = this.marks.map((mark) => mark.toJSON());
201
246
  return json;
@@ -83,15 +83,20 @@ export class ResolvedPos {
83
83
  return this.start(d) + this.node(d).content.size;
84
84
  }
85
85
 
86
- /** Absolute position before the ancestor at `depth` (not defined for the root). */
86
+ /**
87
+ * Absolute position before the ancestor at `depth` (not defined for the root). At
88
+ * `depth + 1`, the position itself: it is between two children, before the one after it.
89
+ */
87
90
  before(depth = this.depth): number {
91
+ if (depth === this.depth + 1) return this.pos;
88
92
  const d = this.at(depth);
89
93
  if (!d) throw new RangeError("There is no position before the root node");
90
94
  return this.start(d) - 1;
91
95
  }
92
96
 
93
- /** Absolute position after the ancestor at `depth` (not defined for the root). */
97
+ /** Absolute position after the ancestor at `depth` (not defined for the root); see `before`. */
94
98
  after(depth = this.depth): number {
99
+ if (depth === this.depth + 1) return this.pos;
95
100
  const d = this.at(depth);
96
101
  if (!d) throw new RangeError("There is no position after the root node");
97
102
  return this.end(d) + 1;
@@ -127,6 +132,22 @@ export class ResolvedPos {
127
132
  return 0;
128
133
  }
129
134
 
135
+ /**
136
+ * The range of sibling blocks around this position and `other`: the deepest depth (below the
137
+ * textblock, if the positions are in one) whose node holds both and satisfies `pred`.
138
+ * Null when no ancestor qualifies.
139
+ */
140
+ blockRange(other: ResolvedPos = this, pred?: (node: Node) => boolean): NodeRange | null {
141
+ if (other.pos < this.pos) return other.blockRange(this, pred);
142
+ const top = this.depth - (this.parent.inlineContent || this.pos === other.pos ? 1 : 0);
143
+ for (let depth = top; depth >= 0; depth--) {
144
+ if (other.pos <= this.end(depth) && (!pred || pred(this.node(depth)))) {
145
+ return new NodeRange(this, other, depth);
146
+ }
147
+ }
148
+ return null;
149
+ }
150
+
130
151
  sameParent(other: ResolvedPos): boolean {
131
152
  return this.pos - this.parentOffset === other.pos - other.parentOffset;
132
153
  }
@@ -142,3 +163,35 @@ export class ResolvedPos {
142
163
  return d;
143
164
  }
144
165
  }
166
+
167
+ /** A flat range of sibling nodes: the children of the node at `depth` from `$from` to `$to`. */
168
+ export class NodeRange {
169
+ constructor(
170
+ readonly $from: ResolvedPos,
171
+ readonly $to: ResolvedPos,
172
+ readonly depth: number,
173
+ ) {}
174
+
175
+ /** Position before the first node of the range. */
176
+ get start(): number {
177
+ return this.$from.before(this.depth + 1);
178
+ }
179
+
180
+ /** Position after the last node of the range. */
181
+ get end(): number {
182
+ return this.$to.after(this.depth + 1);
183
+ }
184
+
185
+ /** The node that holds the range. */
186
+ get parent(): Node {
187
+ return this.$from.node(this.depth);
188
+ }
189
+
190
+ get startIndex(): number {
191
+ return this.$from.index(this.depth);
192
+ }
193
+
194
+ get endIndex(): number {
195
+ return this.$to.indexAfter(this.depth);
196
+ }
197
+ }
@@ -19,6 +19,13 @@ export interface NodeSpec {
19
19
  */
20
20
  marks?: string;
21
21
  attrs?: AttributeSpecs;
22
+ /**
23
+ * Edited as one unit: the caret never enters it. Leaf nodes are atoms whatever this says;
24
+ * set it on a non-leaf node whose content is managed by code.
25
+ */
26
+ atom?: boolean;
27
+ /** Whether a NodeSelection may select this node (default true; text is never selectable). */
28
+ selectable?: boolean;
22
29
  }
23
30
 
24
31
  export interface MarkSpec {
@@ -30,6 +37,11 @@ export interface MarkSpec {
30
37
  * Defaults to the mark itself (at most one mark of a type per node).
31
38
  */
32
39
  excludes?: string;
40
+ /**
41
+ * Whether text typed at the end of this mark gets it too (default true). A link is not
42
+ * inclusive: typing right after it continues as plain text.
43
+ */
44
+ inclusive?: boolean;
33
45
  }
34
46
 
35
47
  export interface SchemaSpec {
@@ -77,6 +89,16 @@ export class NodeType {
77
89
  return this.isBlock && this.inlineContent;
78
90
  }
79
91
 
92
+ /** Edited as one unit (leaf nodes, and nodes whose spec says `atom`). */
93
+ get isAtom(): boolean {
94
+ return this.isLeaf || !!this.spec.atom;
95
+ }
96
+
97
+ /** True when some attribute has no default (the type cannot be created without attrs). */
98
+ hasRequiredAttrs(): boolean {
99
+ return Object.values(this.spec.attrs ?? {}).some((spec) => !("default" in spec));
100
+ }
101
+
80
102
  /** Creates a validated node. Throws FoliDocumentError on bad attrs or content. */
81
103
  create(
82
104
  attrs?: Attrs | null,
@@ -94,6 +116,23 @@ export class NodeType {
94
116
  );
95
117
  }
96
118
 
119
+ /**
120
+ * Creates a node without checking its content. Attributes are still validated.
121
+ * @internal For transforms whose node ends up complete: a wrapper the wrapped blocks fill
122
+ * (validated when they are inserted) or the open half of a split (validated when the replace
123
+ * joins it). Steps do not validate closed nodes inside a slice: a node left incomplete here
124
+ * would make an invalid document.
125
+ */
126
+ createOpen(attrs?: Attrs | null, content?: Fragment | Node | readonly Node[] | null): Node {
127
+ if (this.isText) throw new RangeError("Use Schema.text to create text nodes");
128
+ return new Node(
129
+ this,
130
+ computeAttrs(this.spec.attrs, attrs ?? undefined, `${this.name}.attrs`),
131
+ Fragment.from(content),
132
+ Mark.none,
133
+ );
134
+ }
135
+
97
136
  /** True if `content` matches the content expression and only carries allowed marks. */
98
137
  validContent(content: Fragment): boolean {
99
138
  if (!this.contentMatch.matchFragment(content)?.validEnd) return false;
@@ -159,6 +198,10 @@ export class MarkType {
159
198
  return new Mark(this, computeAttrs(this.spec.attrs, attrs ?? undefined, `${this.name}.attrs`));
160
199
  }
161
200
 
201
+ get inclusive(): boolean {
202
+ return this.spec.inclusive ?? true;
203
+ }
204
+
162
205
  excludes(other: MarkType): boolean {
163
206
  return this.excluded.includes(other);
164
207
  }
@@ -1,5 +1,5 @@
1
1
  import { Fragment } from "./fragment.js";
2
- import type { NodeJSON } from "./node.js";
2
+ import type { Node, NodeJSON } from "./node.js";
3
3
 
4
4
  export interface SliceJSON {
5
5
  content: NodeJSON[];
@@ -26,6 +26,31 @@ export class Slice {
26
26
  return this.content.size - this.openStart - this.openEnd;
27
27
  }
28
28
 
29
+ /**
30
+ * This slice with `fragment` inserted at `pos` (in slice positions): into the node that holds
31
+ * `pos`, which must accept it. Null when the content would not be valid there.
32
+ */
33
+ insertAt(pos: number, fragment: Fragment): Slice | null {
34
+ const content = insertInto(this.content, pos + this.openStart, fragment, {
35
+ parent: null,
36
+ level: 0,
37
+ onStart: true,
38
+ onEnd: true,
39
+ openStart: this.openStart,
40
+ openEnd: this.openEnd,
41
+ });
42
+ return content && new Slice(content, this.openStart, this.openEnd);
43
+ }
44
+
45
+ /** This slice without the flat range `from..to` (slice positions). */
46
+ removeBetween(from: number, to: number): Slice {
47
+ return new Slice(
48
+ removeRange(this.content, from + this.openStart, to + this.openStart),
49
+ this.openStart,
50
+ this.openEnd,
51
+ );
52
+ }
53
+
29
54
  eq(other: Slice): boolean {
30
55
  return (
31
56
  this.content.eq(other.content) &&
@@ -38,3 +63,58 @@ export class Slice {
38
63
  return { content: this.content.toJSON(), openStart: this.openStart, openEnd: this.openEnd };
39
64
  }
40
65
  }
66
+
67
+ interface InsertPath {
68
+ /** The node holding `content`, null at the top of the slice. */
69
+ parent: Node | null;
70
+ /** Depth of `parent` in the slice (1 for a top-level node). */
71
+ level: number;
72
+ /** Whether `parent` is on the slice's first-child chain, and on its last-child chain. */
73
+ onStart: boolean;
74
+ onEnd: boolean;
75
+ openStart: number;
76
+ openEnd: number;
77
+ }
78
+
79
+ function insertInto(
80
+ content: Fragment,
81
+ dist: number,
82
+ insert: Fragment,
83
+ path: InsertPath,
84
+ ): Fragment | null {
85
+ const { index, offset } = content.findIndex(dist);
86
+ const child = content.maybeChild(index);
87
+ if (offset === dist || !child || child.isText) {
88
+ const result = content.cut(0, dist).append(insert).append(content.cut(dist));
89
+ const { parent, level } = path;
90
+ // An open node of the slice is completed by the document around the replaced range;
91
+ // replace validates it once joined. Checking it here would reject valid content.
92
+ const open = (path.onStart && level <= path.openStart) || (path.onEnd && level <= path.openEnd);
93
+ return parent && !open && !parent.type.validContent(result) ? null : result;
94
+ }
95
+ const inner = insertInto(child.content, dist - offset - 1, insert, {
96
+ ...path,
97
+ parent: child,
98
+ level: path.level + 1,
99
+ onStart: path.onStart && index === 0,
100
+ onEnd: path.onEnd && index === content.childCount - 1,
101
+ });
102
+ return inner && content.replaceChild(index, child.copy(inner));
103
+ }
104
+
105
+ function removeRange(content: Fragment, from: number, to: number): Fragment {
106
+ const { index, offset } = content.findIndex(from);
107
+ const child = content.maybeChild(index);
108
+ const { index: indexTo, offset: offsetTo } = content.findIndex(to);
109
+ if (offset === from || !child || child.isText) {
110
+ if (offsetTo !== to && !content.child(indexTo).isText) {
111
+ throw new RangeError("Removing a non-flat range");
112
+ }
113
+ return content.cut(0, from).append(content.cut(to));
114
+ }
115
+ if (index !== indexTo) throw new RangeError("Removing a non-flat range");
116
+ return content.replaceChild(
117
+ index,
118
+ child.copy(removeRange(child.content, from - offset - 1, to - offset - 1)),
119
+ );
120
+ }