@folijs/core 0.1.0 → 0.2.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 (133) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/dist/document.d.ts +5 -4
  3. package/dist/document.d.ts.map +1 -1
  4. package/dist/document.js +30 -8
  5. package/dist/document.js.map +1 -1
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.d.ts.map +1 -1
  8. package/dist/index.js +1 -1
  9. package/dist/index.js.map +1 -1
  10. package/dist/model/attrs.d.ts +5 -0
  11. package/dist/model/attrs.d.ts.map +1 -1
  12. package/dist/model/attrs.js +17 -0
  13. package/dist/model/attrs.js.map +1 -1
  14. package/dist/model/content.d.ts +10 -0
  15. package/dist/model/content.d.ts.map +1 -1
  16. package/dist/model/content.js +44 -0
  17. package/dist/model/content.js.map +1 -1
  18. package/dist/model/fragment.d.ts +2 -0
  19. package/dist/model/fragment.d.ts.map +1 -1
  20. package/dist/model/fragment.js +8 -0
  21. package/dist/model/fragment.js.map +1 -1
  22. package/dist/model/index.d.ts +1 -1
  23. package/dist/model/index.d.ts.map +1 -1
  24. package/dist/model/index.js +1 -1
  25. package/dist/model/index.js.map +1 -1
  26. package/dist/model/mark.d.ts.map +1 -1
  27. package/dist/model/mark.js +4 -3
  28. package/dist/model/mark.js.map +1 -1
  29. package/dist/model/node.d.ts +12 -0
  30. package/dist/model/node.d.ts.map +1 -1
  31. package/dist/model/node.js +35 -3
  32. package/dist/model/node.js.map +1 -1
  33. package/dist/model/resolvedpos.d.ts +26 -2
  34. package/dist/model/resolvedpos.d.ts.map +1 -1
  35. package/dist/model/resolvedpos.js +54 -2
  36. package/dist/model/resolvedpos.js.map +1 -1
  37. package/dist/model/schema.d.ts +25 -0
  38. package/dist/model/schema.d.ts.map +1 -1
  39. package/dist/model/schema.js +23 -0
  40. package/dist/model/schema.js.map +1 -1
  41. package/dist/model/slice.d.ts +7 -0
  42. package/dist/model/slice.d.ts.map +1 -1
  43. package/dist/model/slice.js +53 -0
  44. package/dist/model/slice.js.map +1 -1
  45. package/dist/schema.d.ts +62 -7
  46. package/dist/schema.d.ts.map +1 -1
  47. package/dist/schema.js +82 -8
  48. package/dist/schema.js.map +1 -1
  49. package/dist/state/attempt.d.ts +4 -0
  50. package/dist/state/attempt.d.ts.map +1 -0
  51. package/dist/state/attempt.js +19 -0
  52. package/dist/state/attempt.js.map +1 -0
  53. package/dist/state/commands.d.ts +18 -3
  54. package/dist/state/commands.d.ts.map +1 -1
  55. package/dist/state/commands.js +199 -44
  56. package/dist/state/commands.js.map +1 -1
  57. package/dist/state/editor.d.ts +24 -0
  58. package/dist/state/editor.d.ts.map +1 -1
  59. package/dist/state/editor.js +14 -1
  60. package/dist/state/editor.js.map +1 -1
  61. package/dist/state/format.d.ts +27 -0
  62. package/dist/state/format.d.ts.map +1 -0
  63. package/dist/state/format.js +144 -0
  64. package/dist/state/format.js.map +1 -0
  65. package/dist/state/history.js +2 -2
  66. package/dist/state/history.js.map +1 -1
  67. package/dist/state/index.d.ts +5 -2
  68. package/dist/state/index.d.ts.map +1 -1
  69. package/dist/state/index.js +5 -2
  70. package/dist/state/index.js.map +1 -1
  71. package/dist/state/links.d.ts +42 -0
  72. package/dist/state/links.d.ts.map +1 -0
  73. package/dist/state/links.js +186 -0
  74. package/dist/state/links.js.map +1 -0
  75. package/dist/state/lists.d.ts +36 -0
  76. package/dist/state/lists.d.ts.map +1 -0
  77. package/dist/state/lists.js +383 -0
  78. package/dist/state/lists.js.map +1 -0
  79. package/dist/state/selection.d.ts +18 -2
  80. package/dist/state/selection.d.ts.map +1 -1
  81. package/dist/state/selection.js +63 -2
  82. package/dist/state/selection.js.map +1 -1
  83. package/dist/state/state.d.ts.map +1 -1
  84. package/dist/state/state.js +12 -2
  85. package/dist/state/state.js.map +1 -1
  86. package/dist/transform/index.d.ts +2 -1
  87. package/dist/transform/index.d.ts.map +1 -1
  88. package/dist/transform/index.js +2 -1
  89. package/dist/transform/index.js.map +1 -1
  90. package/dist/transform/stepmap.d.ts +8 -6
  91. package/dist/transform/stepmap.d.ts.map +1 -1
  92. package/dist/transform/stepmap.js +18 -15
  93. package/dist/transform/stepmap.js.map +1 -1
  94. package/dist/transform/steps.d.ts +27 -2
  95. package/dist/transform/steps.d.ts.map +1 -1
  96. package/dist/transform/steps.js +115 -4
  97. package/dist/transform/steps.js.map +1 -1
  98. package/dist/transform/structure.d.ts +26 -0
  99. package/dist/transform/structure.d.ts.map +1 -0
  100. package/dist/transform/structure.js +116 -0
  101. package/dist/transform/structure.js.map +1 -0
  102. package/dist/transform/transform.d.ts +34 -11
  103. package/dist/transform/transform.d.ts.map +1 -1
  104. package/dist/transform/transform.js +145 -18
  105. package/dist/transform/transform.js.map +1 -1
  106. package/package.json +1 -1
  107. package/src/document.ts +34 -8
  108. package/src/index.ts +14 -0
  109. package/src/model/attrs.ts +19 -0
  110. package/src/model/content.ts +56 -0
  111. package/src/model/fragment.ts +7 -0
  112. package/src/model/index.ts +1 -1
  113. package/src/model/mark.ts +3 -2
  114. package/src/model/node.ts +42 -2
  115. package/src/model/resolvedpos.ts +55 -2
  116. package/src/model/schema.ts +43 -0
  117. package/src/model/slice.ts +81 -1
  118. package/src/schema.ts +135 -12
  119. package/src/state/attempt.ts +22 -0
  120. package/src/state/commands.ts +209 -47
  121. package/src/state/editor.ts +38 -2
  122. package/src/state/format.ts +133 -0
  123. package/src/state/history.ts +3 -3
  124. package/src/state/index.ts +31 -0
  125. package/src/state/links.ts +185 -0
  126. package/src/state/lists.ts +427 -0
  127. package/src/state/selection.ts +72 -3
  128. package/src/state/state.ts +11 -2
  129. package/src/transform/index.ts +8 -0
  130. package/src/transform/stepmap.ts +18 -12
  131. package/src/transform/steps.ts +126 -3
  132. package/src/transform/structure.ts +135 -0
  133. package/src/transform/transform.ts +165 -16
@@ -3,10 +3,12 @@
3
3
  import { deepEqual } from "../model/attrs.js";
4
4
  import {
5
5
  type Attrs,
6
+ FoliDocumentError,
6
7
  Fragment,
7
8
  type Mark,
8
9
  MarkType,
9
10
  type Node,
11
+ type NodeRange,
10
12
  type NodeType,
11
13
  Slice,
12
14
  } from "../model/index.js";
@@ -16,10 +18,31 @@ import {
16
18
  NodeMarkupBatchStep,
17
19
  NodeMarkupStep,
18
20
  RemoveMarkStep,
21
+ ReplaceAroundStep,
19
22
  ReplaceStep,
20
23
  type Step,
21
24
  type StepResult,
22
25
  } from "./steps.js";
26
+ import type { NodeTypeWithAttrs } from "./structure.js";
27
+
28
+ /** The attributes a block gets when it becomes `type` (see Transform.setBlockType). */
29
+ function blockAttrs(node: Node, type: NodeType, attrs: Attrs | null): Attrs {
30
+ const merged: Record<string, unknown> = {};
31
+ const own = node.type.spec.attrs ?? {};
32
+ for (const name of Object.keys(type.spec.attrs ?? {})) {
33
+ if (attrs && Object.hasOwn(attrs, name)) merged[name] = attrs[name];
34
+ else if (Object.hasOwn(own, name)) merged[name] = node.attrs[name];
35
+ }
36
+ for (const name of Object.keys(attrs ?? {})) {
37
+ if (!Object.hasOwn(merged, name)) merged[name] = attrs?.[name]; // unknown: rejected below
38
+ }
39
+ try {
40
+ return type.createOpen(merged).attrs;
41
+ } catch (error) {
42
+ if (error instanceof FoliDocumentError) throw new TransformError(error.message);
43
+ throw error;
44
+ }
45
+ }
23
46
 
24
47
  /** A step could not be applied (the change would break the schema or the range is invalid). */
25
48
  export class TransformError extends Error {
@@ -150,35 +173,142 @@ export class Transform {
150
173
  }
151
174
 
152
175
  /**
153
- * Splits the textblock around `pos` in two. The second half gets the same markup unless
154
- * `after` says otherwise. shortcut: splits one level only; nested splits arrive with lists (Phase 7).
176
+ * Splits the node around `pos` and `depth - 1` of its ancestors in two (depth 2 in a list
177
+ * item's paragraph splits the item). The new nodes after the split get the types in
178
+ * `typesAfter`: entry 0 for the outermost split node, entry `depth - 1` for the innermost;
179
+ * missing or null entries keep the original type and attributes. Check with `canSplit`
180
+ * first: an invalid split (content or attributes) throws TransformError.
155
181
  */
156
- split(pos: number, after?: { type: NodeType; attrs?: Attrs | null }): this {
157
- const parent = this.doc.resolve(pos).parent;
158
- const second = after ? after.type.create(after.attrs) : parent.copy();
159
- return this.replace(pos, pos, new Slice(Fragment.from([parent.copy(), second]), 1, 1));
182
+ split(
183
+ pos: number,
184
+ depth = 1,
185
+ typesAfter?: readonly (NodeTypeWithAttrs | null | undefined)[],
186
+ ): this {
187
+ const $pos = this.doc.resolve(pos);
188
+ if (depth < 1 || depth > $pos.depth)
189
+ throw new TransformError(`Cannot split ${depth} levels at ${pos}`);
190
+ let before = Fragment.empty;
191
+ let after = Fragment.empty;
192
+ for (let d = $pos.depth, i = depth - 1; d > $pos.depth - depth; d--, i--) {
193
+ before = Fragment.from($pos.node(d).copy(before));
194
+ const typeAfter = typesAfter?.[i];
195
+ let next: Node;
196
+ try {
197
+ next = typeAfter
198
+ ? typeAfter.type.createOpen(typeAfter.attrs, after)
199
+ : $pos.node(d).copy(after);
200
+ } catch (error) {
201
+ if (error instanceof FoliDocumentError) throw new TransformError(error.message);
202
+ throw error;
203
+ }
204
+ after = Fragment.from(next);
205
+ }
206
+ return this.step(
207
+ new ReplaceStep(pos, pos, new Slice(before.append(after), depth, depth), true),
208
+ );
160
209
  }
161
210
 
162
211
  /**
163
- * Joins the two textblocks on either side of `pos`. Throws TransformError for any other position.
164
- * shortcut: textblocks only; joining other compatible blocks (lists, Phase 7) needs a content check.
212
+ * Joins the nodes on either side of `pos` (and, with `depth` > 1, their last and first
213
+ * descendants down to that depth). Throws TransformError when they cannot be joined; check
214
+ * with `canJoin` first.
165
215
  */
166
- join(pos: number): this {
167
- // Without this check a position inside text would silently delete the two characters around it.
216
+ join(pos: number, depth = 1): this {
217
+ // Without this check a position inside text would silently delete the characters around it.
168
218
  const $pos = pos > 0 && pos < this.doc.content.size ? this.doc.resolve(pos) : null;
169
- if (!$pos?.nodeBefore?.isTextblock || !$pos.nodeAfter?.isTextblock) {
170
- throw new TransformError(`Position ${pos} is not between two textblocks`);
219
+ const before = $pos?.nodeBefore;
220
+ const after = $pos?.nodeAfter;
221
+ if (!before || !after || before.isLeaf || after.isLeaf || $pos?.textOffset) {
222
+ throw new TransformError(`Position ${pos} is not between two joinable nodes`);
171
223
  }
172
- return this.delete(pos - 1, pos + 1);
224
+ return this.step(new ReplaceStep(pos - depth, pos + depth, Slice.empty, true));
173
225
  }
174
226
 
175
- /** Changes every textblock touching `from..to` to `type` with `attrs`, keeping content. */
227
+ /**
228
+ * Wraps the blocks of `range` in `wrappers` (outermost first, as `findWrapping` returns
229
+ * them). The blocks move inside without being re-created, so positions in them map through.
230
+ */
231
+ wrap(range: NodeRange, wrappers: readonly NodeTypeWithAttrs[]): this {
232
+ let content = Fragment.empty;
233
+ for (let i = wrappers.length - 1; i >= 0; i--) {
234
+ const wrapper = wrappers[i] as NodeTypeWithAttrs;
235
+ if (content.size && !wrapper.type.contentMatch.matchFragment(content)?.validEnd) {
236
+ throw new TransformError(`${wrapper.type.name} cannot hold the next wrapper`);
237
+ }
238
+ content = Fragment.from(wrapper.type.createOpen(wrapper.attrs, content));
239
+ }
240
+ const { start, end } = range;
241
+ return this.step(
242
+ new ReplaceAroundStep(
243
+ start,
244
+ end,
245
+ start,
246
+ end,
247
+ new Slice(content, 0, 0),
248
+ wrappers.length,
249
+ true,
250
+ ),
251
+ );
252
+ }
253
+
254
+ /**
255
+ * Moves the blocks of `range` up to `target` depth (from `liftTarget`), splitting the
256
+ * ancestors they leave around them; ancestors left empty on a side are removed.
257
+ */
258
+ lift(range: NodeRange, target: number): this {
259
+ const { $from, $to, depth } = range;
260
+ const gapStart = $from.before(depth + 1);
261
+ const gapEnd = $to.after(depth + 1);
262
+ let start = gapStart;
263
+ let end = gapEnd;
264
+ let before = Fragment.empty;
265
+ let openStart = 0;
266
+ for (let d = depth, splitting = false; d > target; d--) {
267
+ if (splitting || $from.index(d) > 0) {
268
+ splitting = true;
269
+ before = Fragment.from($from.node(d).copy(before));
270
+ openStart++;
271
+ } else {
272
+ start--;
273
+ }
274
+ }
275
+ let after = Fragment.empty;
276
+ let openEnd = 0;
277
+ for (let d = depth, splitting = false; d > target; d--) {
278
+ if (splitting || $to.after(d + 1) < $to.end(d)) {
279
+ splitting = true;
280
+ after = Fragment.from($to.node(d).copy(after));
281
+ openEnd++;
282
+ } else {
283
+ end++;
284
+ }
285
+ }
286
+ return this.step(
287
+ new ReplaceAroundStep(
288
+ start,
289
+ end,
290
+ gapStart,
291
+ gapEnd,
292
+ new Slice(before.append(after), openStart, openEnd),
293
+ before.size - openStart,
294
+ true,
295
+ ),
296
+ );
297
+ }
298
+
299
+ /**
300
+ * Changes every textblock touching `from..to` to `type`, keeping content. Attributes come from
301
+ * `attrs`, then from the block itself for attributes both types declare (a centered paragraph
302
+ * stays centered as a heading), then from defaults. Blocks already like that are untouched.
303
+ * Invalid attributes throw TransformError.
304
+ */
176
305
  setBlockType(from: number, to: number, type: NodeType, attrs: Attrs | null = null): this {
177
306
  const changed: NodeMarkupStep[] = [];
178
307
  this.doc.nodesBetween(from, to, (node, pos) => {
179
308
  if (!node.isTextblock) return undefined;
180
- if (node.type !== type || !deepEqual(node.attrs, attrs ?? {})) {
181
- changed.push(new NodeMarkupStep(pos, type, attrs));
309
+ const next = blockAttrs(node, type, attrs);
310
+ if (node.type !== type || !deepEqual(node.attrs, next)) {
311
+ changed.push(new NodeMarkupStep(pos, type, next));
182
312
  }
183
313
  return false;
184
314
  });
@@ -186,6 +316,25 @@ export class Transform {
186
316
  return changed.length ? this.step(new NodeMarkupBatchStep(changed)) : this;
187
317
  }
188
318
 
319
+ /**
320
+ * Sets `attrs` on every textblock touching `from..to` whose type declares them (alignment on
321
+ * paragraphs and headings), keeping each block's type. Blocks already like that are untouched.
322
+ * Invalid attributes throw TransformError.
323
+ */
324
+ setBlockAttrs(from: number, to: number, attrs: Attrs): this {
325
+ const changed: NodeMarkupStep[] = [];
326
+ this.doc.nodesBetween(from, to, (node, pos) => {
327
+ if (!node.isTextblock) return undefined;
328
+ const declared = node.type.spec.attrs ?? {};
329
+ const own = Object.fromEntries(Object.entries(attrs).filter(([name]) => name in declared));
330
+ if (!Object.keys(own).length) return false;
331
+ const next = { ...node.attrs, ...own };
332
+ if (!deepEqual(node.attrs, next)) changed.push(new NodeMarkupStep(pos, node.type, next));
333
+ return false;
334
+ });
335
+ return changed.length ? this.step(new NodeMarkupBatchStep(changed)) : this;
336
+ }
337
+
189
338
  /** Changes the markup of the node at `pos` (see NodeMarkupStep). */
190
339
  setNodeMarkup(
191
340
  pos: number,