@folijs/core 0.4.0 → 0.5.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 (91) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/dist/model/schema.d.ts +9 -1
  3. package/dist/model/schema.d.ts.map +1 -1
  4. package/dist/model/schema.js +13 -3
  5. package/dist/model/schema.js.map +1 -1
  6. package/dist/state/commands.d.ts +8 -1
  7. package/dist/state/commands.d.ts.map +1 -1
  8. package/dist/state/commands.js +87 -34
  9. package/dist/state/commands.js.map +1 -1
  10. package/dist/state/comments.js +3 -3
  11. package/dist/state/comments.js.map +1 -1
  12. package/dist/state/editor.d.ts.map +1 -1
  13. package/dist/state/editor.js +2 -0
  14. package/dist/state/editor.js.map +1 -1
  15. package/dist/state/index.d.ts +4 -1
  16. package/dist/state/index.d.ts.map +1 -1
  17. package/dist/state/index.js +4 -1
  18. package/dist/state/index.js.map +1 -1
  19. package/dist/state/links.d.ts.map +1 -1
  20. package/dist/state/links.js +30 -19
  21. package/dist/state/links.js.map +1 -1
  22. package/dist/state/pictures.d.ts +51 -0
  23. package/dist/state/pictures.d.ts.map +1 -0
  24. package/dist/state/pictures.js +140 -0
  25. package/dist/state/pictures.js.map +1 -0
  26. package/dist/state/review.d.ts +1 -1
  27. package/dist/state/review.d.ts.map +1 -1
  28. package/dist/state/review.js +92 -13
  29. package/dist/state/review.js.map +1 -1
  30. package/dist/state/selection.d.ts +12 -1
  31. package/dist/state/selection.d.ts.map +1 -1
  32. package/dist/state/selection.js +12 -0
  33. package/dist/state/selection.js.map +1 -1
  34. package/dist/state/tablecommands.d.ts +77 -0
  35. package/dist/state/tablecommands.d.ts.map +1 -0
  36. package/dist/state/tablecommands.js +675 -0
  37. package/dist/state/tablecommands.js.map +1 -0
  38. package/dist/state/tables.d.ts +110 -0
  39. package/dist/state/tables.d.ts.map +1 -0
  40. package/dist/state/tables.js +729 -0
  41. package/dist/state/tables.js.map +1 -0
  42. package/dist/state/track.js +55 -6
  43. package/dist/state/track.js.map +1 -1
  44. package/dist/state/word.js +1 -1
  45. package/dist/state/word.js.map +1 -1
  46. package/dist/state/wordclip.d.ts.map +1 -1
  47. package/dist/state/wordclip.js +26 -9
  48. package/dist/state/wordclip.js.map +1 -1
  49. package/dist/state/wordformat.d.ts +9 -1
  50. package/dist/state/wordformat.d.ts.map +1 -1
  51. package/dist/state/wordformat.js +38 -16
  52. package/dist/state/wordformat.js.map +1 -1
  53. package/dist/v3/document.d.ts +114 -7
  54. package/dist/v3/document.d.ts.map +1 -1
  55. package/dist/v3/document.js +170 -13
  56. package/dist/v3/document.js.map +1 -1
  57. package/dist/v3/index.d.ts +2 -2
  58. package/dist/v3/index.d.ts.map +1 -1
  59. package/dist/v3/index.js +2 -2
  60. package/dist/v3/index.js.map +1 -1
  61. package/dist/v3/migrate.d.ts +2 -2
  62. package/dist/v3/migrate.d.ts.map +1 -1
  63. package/dist/v3/resolve.d.ts +50 -4
  64. package/dist/v3/resolve.d.ts.map +1 -1
  65. package/dist/v3/resolve.js +205 -15
  66. package/dist/v3/resolve.js.map +1 -1
  67. package/dist/v3/schema.d.ts +4 -0
  68. package/dist/v3/schema.d.ts.map +1 -1
  69. package/dist/v3/schema.js +166 -6
  70. package/dist/v3/schema.js.map +1 -1
  71. package/package.json +1 -1
  72. package/src/model/schema.ts +18 -2
  73. package/src/state/commands.ts +80 -28
  74. package/src/state/comments.ts +3 -3
  75. package/src/state/editor.ts +2 -0
  76. package/src/state/index.ts +34 -0
  77. package/src/state/links.ts +27 -17
  78. package/src/state/pictures.ts +168 -0
  79. package/src/state/review.ts +96 -14
  80. package/src/state/selection.ts +15 -1
  81. package/src/state/tablecommands.ts +741 -0
  82. package/src/state/tables.ts +760 -0
  83. package/src/state/track.ts +63 -6
  84. package/src/state/word.ts +1 -1
  85. package/src/state/wordclip.ts +28 -11
  86. package/src/state/wordformat.ts +35 -13
  87. package/src/v3/document.ts +285 -14
  88. package/src/v3/index.ts +14 -0
  89. package/src/v3/migrate.ts +2 -2
  90. package/src/v3/resolve.ts +259 -15
  91. package/src/v3/schema.ts +187 -7
@@ -294,6 +294,10 @@ function rebuild(
294
294
  (mark ??= adjacent(deletion, from, to) ?? deletion.create({ id: newId(), author, date }));
295
295
  const real: [number, number][] = [];
296
296
  const marked: [number, number][] = [];
297
+ /** Whole rows deleted: recorded as the rows' deletion. */
298
+ const rows: { pos: number; node: Node }[] = [];
299
+ /** One's own inserted rows deleted, by table: they go (the table, when they are all its rows). */
300
+ const ownRows = new Map<number, [number, number][]>();
297
301
  // Paragraphs joined for real (one's own inserted mark removed) become one, which starts
298
302
  // where the first starts and ends with the last one's mark (`keepLastMark`), as an
299
303
  // untracked join does.
@@ -334,9 +338,22 @@ function rebuild(
334
338
  pending.push({ at: here(start), kind: "deletion", revision: revisionOf(markOf()) });
335
339
  return true;
336
340
  }
337
- // shortcut: blocks Foli does not model (tables…) cannot be recorded as changed, so such an
338
- // edit is not made while tracking; Phase 10 models tables and tracks them.
339
- if (!node.isInline) throw new TransformError("A table cannot be deleted as a tracked change");
341
+ // A whole row: its deletion is the row's (`w:trPr/w:del`), its content kept as Word keeps
342
+ // it; one's own inserted row goes for real.
343
+ if (node.type.name === "tableRow" && from <= pos && to >= pos + node.nodeSize) {
344
+ const revision = node.attrs.revision as ParagraphRevision | null;
345
+ if (revision?.insertion?.author === author && !revision.deletion) {
346
+ const table = doc.resolve(pos).before();
347
+ ownRows.set(table, [...(ownRows.get(table) ?? []), [pos, pos + node.nodeSize]]);
348
+ } else if (!revision?.deletion) rows.push({ pos, node });
349
+ return false;
350
+ }
351
+ // Tables, rows and cells partly covered: the text in them, cell by cell.
352
+ if (["table", "tableRow", "tableCell"].includes(node.type.name)) return true;
353
+ // shortcut: other blocks Foli does not model cannot be recorded as changed, so such an
354
+ // edit is not made while tracking.
355
+ if (!node.isInline)
356
+ throw new TransformError("This content cannot be deleted as a tracked change");
340
357
  const s = Math.max(pos, from);
341
358
  const e = Math.min(pos + node.nodeSize, to);
342
359
  if (s >= e || MARKERS.has(node.type.name) || deletion.isInSet(node.marks)) return false;
@@ -344,7 +361,24 @@ function rebuild(
344
361
  (own && own.attrs.author === author ? real : marked).push([s, e]);
345
362
  return false;
346
363
  });
364
+ for (const [table, list] of ownRows) {
365
+ const node = doc.nodeAt(table);
366
+ if (node && list.length === node.childCount) real.push([table, table + node.nodeSize]);
367
+ else real.push(...list);
368
+ }
347
369
  for (const [s, e] of joined(marked)) out.addMark(s, e, markOf());
370
+ for (const { pos, node } of rows) {
371
+ const revision = (node.attrs.revision as ParagraphRevision | null) ?? {};
372
+ const deleted = revisionOf(markOf());
373
+ out.step(
374
+ new NodeMarkupStep(
375
+ pos,
376
+ node.type,
377
+ { ...node.attrs, revision: { ...revision, deletion: deleted } },
378
+ node.marks,
379
+ ),
380
+ );
381
+ }
348
382
  for (const [s, e] of joined(real).reverse()) out.delete(s, e);
349
383
  for (const { at: where, node, live } of merged) {
350
384
  const pos = at(where, 1);
@@ -405,13 +439,31 @@ function rebuild(
405
439
  out.replace(pos, pos, new Slice(content, slice.openStart, slice.openEnd));
406
440
  inserted = here(pos);
407
441
  const size = slice.size;
442
+ // Whole rows inserted: the rows' insertion (`w:trPr/w:ins`), beside their text's marks.
443
+ const newRows: number[] = [];
408
444
  out.doc.nodesBetween(pos, pos + size, (node, p) => {
445
+ if (node.type.name === "tableRow" && p >= pos && p + node.nodeSize <= pos + size)
446
+ newRows.push(p);
409
447
  if (!node.isTextblock) return !node.isInline;
410
448
  const close = p + node.nodeSize - 1;
411
449
  if (close >= pos && close < pos + size)
412
450
  pending.push({ at: here(p), kind: "insertion", revision: revisionOf(mark) });
413
451
  return false;
414
452
  });
453
+ for (const p of newRows) {
454
+ const row = out.doc.nodeAt(p);
455
+ if (!row) continue;
456
+ const revision = (row.attrs.revision as ParagraphRevision | null) ?? {};
457
+ if (revision.insertion) continue;
458
+ out.step(
459
+ new NodeMarkupStep(
460
+ p,
461
+ row.type,
462
+ { ...row.attrs, revision: { ...revision, insertion: revisionOf(mark) } },
463
+ row.marks,
464
+ ),
465
+ );
466
+ }
415
467
  // An open end: the paragraph there keeps the original mark with the inserted content's
416
468
  // properties (Enter at the end of a heading: the next style), a change of its formatting.
417
469
  const $end = out.doc.resolve(pos + size);
@@ -555,6 +607,9 @@ function rebuild(
555
607
  out.setSelection(NodeSelection.create(out.doc, anchor));
556
608
  else if (selection instanceof TextSelection)
557
609
  out.setSelection(TextSelection.create(out.doc, anchor, head));
610
+ // Cells stay selected (`Selection.fromJSON` falls back to a caret when they are not cells).
611
+ else if (selection.toJSON().type === "cell")
612
+ out.setSelection(Selection.fromJSON(out.doc, { type: "cell", anchor, head }));
558
613
  else out.setSelection(Selection.near(out.doc.resolve(head)));
559
614
  if (tr.storedMarks)
560
615
  out.setStoredMarks(tr.storedMarks.filter((m) => m.type !== insertion && m.type !== deletion));
@@ -593,9 +648,11 @@ function markFragment(
593
648
  return;
594
649
  }
595
650
  // A paragraph keeps its revision: an open end's is the original mark's, and the marks
596
- // inserted get theirs afterwards.
597
- if (!node.isTextblock)
598
- throw new TransformError("A table cannot be inserted as a tracked change");
651
+ // inserted get theirs afterwards. Tables, rows and cells: their content marked (whole rows
652
+ // get their insertion afterwards too).
653
+ const container = ["table", "tableRow", "tableCell"].includes(node.type.name);
654
+ if (!node.isTextblock && !container)
655
+ throw new TransformError("This content cannot be inserted as a tracked change");
599
656
  nodes.push(node.copy(markFragment(node.content, mark, insertion, deletion)));
600
657
  });
601
658
  return Fragment.fromArray(nodes);
package/src/state/word.ts CHANGED
@@ -252,7 +252,7 @@ export function wordToggleActive(
252
252
  ): boolean {
253
253
  const { selection } = state;
254
254
  if (caretMarks) return shows(context, selection.$from.parent, caretMarks, type);
255
- const { judged } = inlineIn(state, selection.from, selection.to, type);
255
+ const judged = selection.ranges.flatMap((r) => inlineIn(state, r.from, r.to, type).judged);
256
256
  return judged.length > 0 && judged.every((f) => shows(context, f.parent, f.node.marks, type));
257
257
  }
258
258
 
@@ -24,11 +24,13 @@ import { type DocJSON, isSafeLink } from "../schema.js";
24
24
  import {
25
25
  type AbstractNumberingJSON,
26
26
  type CommentJSON,
27
+ migrateToV4,
27
28
  type NumberingInstanceJSON,
28
29
  type NumberingLevelJSON,
29
30
  type ParagraphPropertiesJSON,
30
31
  parseContent,
31
32
  parseDocument,
33
+ SCHEMA_VERSION,
32
34
  type StyleJSON,
33
35
  } from "../v3/document.js";
34
36
  import { migrateToV3 } from "../v3/migrate.js";
@@ -88,6 +90,9 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
88
90
  properties(attrs);
89
91
  properties(attrs.formatChange?.previous);
90
92
  }
93
+ // A table's style, and a row's exceptions' style.
94
+ if (node.type === "table") properties(attrs);
95
+ if (node.type === "tableRow") properties((attrs as { exceptions?: Properties }).exceptions);
91
96
  if (COMMENT_ANCHORS.has(node.type)) commentIds.add(attrs.id as number);
92
97
  marks(node.marks);
93
98
  };
@@ -140,11 +145,13 @@ export function fitPastedV2(
140
145
  state: EditorState,
141
146
  content: Node,
142
147
  ): { doc: Node; envelope: WordEnvelope } {
143
- const migrated = migrateToV3({
144
- schemaVersion: 2,
145
- settings: DEFAULT_SETTINGS,
146
- content: content.toJSON() as DocJSON,
147
- });
148
+ const migrated = migrateToV4(
149
+ migrateToV3({
150
+ schemaVersion: 2,
151
+ settings: DEFAULT_SETTINGS,
152
+ content: content.toJSON() as DocJSON,
153
+ }),
154
+ );
148
155
  const definitions: WordDefinitions = {
149
156
  styles: migrated.styles.map((style) => {
150
157
  // Based on the paste's Normal, which is the target's by name.
@@ -212,19 +219,21 @@ export function fitPaste(
212
219
  const target = context.envelope;
213
220
  // Taken as this document's own, but it cannot be told to be: no unsafe link comes in.
214
221
  if (definitions === undefined || definitions === null)
215
- return fit(state, target, safeLinks(parseContent(content).toJSON()), true);
222
+ return fit(state, target, safeLinks(parseContent(content, "content", true).toJSON()), true);
216
223
  const { document, styles, numbering, comments } = definitions as Partial<WordDefinitions>;
217
- // Validated as a document's envelope: types, ranges, no style based on itself.
224
+ // Validated as a document's envelope: types, ranges, no style based on itself; the content
225
+ // as content to paste (a table may end it).
218
226
  const checked = parseDocument({
219
- schemaVersion: 3,
227
+ schemaVersion: SCHEMA_VERSION,
220
228
  section: target.section,
221
229
  defaults: {},
222
230
  styles,
223
231
  numbering,
224
232
  comments,
225
- content,
233
+ content: { type: "doc", content: [{ type: "paragraph" }] },
226
234
  });
227
- return fit(state, checked, checked.content, document === context.id);
235
+ const json = parseContent(content, "content", true).toJSON();
236
+ return fit(state, checked, json, document === context.id);
228
237
  }
229
238
 
230
239
  /**
@@ -389,6 +398,14 @@ function fit(
389
398
  if (Object.keys(rest).length) next.attributes = rest;
390
399
  else delete next.attributes;
391
400
  }
401
+ } else if (node.type === "table" && attrs?.style) {
402
+ next = { ...attrs, style: mapStyle(String(attrs.style)) };
403
+ } else if (node.type === "tableRow" && attrs) {
404
+ const exceptions = attrs.exceptions as Json | null | undefined;
405
+ // A copied row is a new row: not its identity (`w14:paraId`…).
406
+ next = { ...attrs, attributes: null };
407
+ if (exceptions?.style)
408
+ next.exceptions = { ...exceptions, style: mapStyle(String(exceptions.style)) };
392
409
  } else if (COMMENT_ANCHORS.has(node.type)) {
393
410
  const id = attrs?.id as number;
394
411
  if (!defined.has(id)) return null;
@@ -412,7 +429,7 @@ function fit(
412
429
  } as NodeJSON;
413
430
  };
414
431
 
415
- const doc = parseContent(mapNode(json));
432
+ const doc = parseContent(mapNode(json), "content", true);
416
433
  let comments = target.comments;
417
434
  const copiedComments = source.comments.filter((c) => commentIds.has(c.id));
418
435
  if (copiedComments.length) {
@@ -36,12 +36,12 @@ const json = (value: unknown) => JSON.stringify(value ?? null);
36
36
  /** The paragraphs the selection touches, with their positions. */
37
37
  function paragraphsIn(state: EditorState): { node: Node; pos: number }[] {
38
38
  const found: { node: Node; pos: number }[] = [];
39
- const { from, to } = state.selection;
40
- state.doc.nodesBetween(from, to, (node, pos) => {
41
- if (node.type.name !== "paragraph") return true;
42
- found.push({ node, pos });
43
- return false;
44
- });
39
+ for (const { from, to } of state.selection.ranges)
40
+ state.doc.nodesBetween(from, to, (node, pos) => {
41
+ if (node.type.name !== "paragraph") return true;
42
+ found.push({ node, pos });
43
+ return false;
44
+ });
45
45
  return found;
46
46
  }
47
47
 
@@ -166,6 +166,28 @@ export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | nu
166
166
  ...based,
167
167
  paragraph: { indent: { left: 720 }, contextualSpacing: true },
168
168
  };
169
+ if (name === "Table Grid") {
170
+ // Word's: single borders all round and between cells, no space after paragraphs.
171
+ const line = { style: "single", size: 4, space: 0, color: "auto" };
172
+ const plain = styleNamed(context, "Normal Table", "table");
173
+ return {
174
+ id: "TableGrid",
175
+ type: "table",
176
+ name,
177
+ ...(plain ? { basedOn: plain } : {}),
178
+ paragraph: { spacing: { after: 0, line: 240, lineRule: "auto" } },
179
+ table: {
180
+ borders: {
181
+ top: line,
182
+ left: line,
183
+ bottom: line,
184
+ right: line,
185
+ insideH: line,
186
+ insideV: line,
187
+ },
188
+ },
189
+ };
190
+ }
169
191
  if (name === "Hyperlink") {
170
192
  const font = defaultParagraphStyleOf(context, "character");
171
193
  return {
@@ -186,7 +208,7 @@ export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | nu
186
208
  * The id of the document's style named `name`, adding Word's built-in definition when the
187
209
  * document does not have it yet (with the envelope that has it; unchanged if it was there).
188
210
  */
189
- function ensureStyle(
211
+ export function ensureStyle(
190
212
  context: HasEnvelope,
191
213
  name: string,
192
214
  type: StyleJSON["type"] = "paragraph",
@@ -285,12 +307,12 @@ function applyStyle(
285
307
  function runsIn(state: EditorState, type: MarkType) {
286
308
  const context = wordContext(state);
287
309
  const found: { node: Node; pos: number; parent: Node }[] = [];
288
- const { from, to } = state.selection;
289
- state.doc.nodesBetween(from, to, (node, pos, parent) => {
290
- if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
291
- if (!isMarker(context, node)) found.push({ node, pos, parent });
292
- return false;
293
- });
310
+ for (const { from, to } of state.selection.ranges)
311
+ state.doc.nodesBetween(from, to, (node, pos, parent) => {
312
+ if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
313
+ if (!isMarker(context, node)) found.push({ node, pos, parent });
314
+ return false;
315
+ });
294
316
  return found;
295
317
  }
296
318