@folijs/core 0.3.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 (113) hide show
  1. package/CHANGELOG.md +43 -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 +119 -38
  9. package/dist/state/commands.js.map +1 -1
  10. package/dist/state/comments.d.ts +47 -0
  11. package/dist/state/comments.d.ts.map +1 -0
  12. package/dist/state/comments.js +304 -0
  13. package/dist/state/comments.js.map +1 -0
  14. package/dist/state/editor.d.ts +42 -1
  15. package/dist/state/editor.d.ts.map +1 -1
  16. package/dist/state/editor.js +32 -2
  17. package/dist/state/editor.js.map +1 -1
  18. package/dist/state/history.d.ts +1 -1
  19. package/dist/state/history.d.ts.map +1 -1
  20. package/dist/state/history.js +3 -2
  21. package/dist/state/history.js.map +1 -1
  22. package/dist/state/index.d.ts +8 -2
  23. package/dist/state/index.d.ts.map +1 -1
  24. package/dist/state/index.js +8 -2
  25. package/dist/state/index.js.map +1 -1
  26. package/dist/state/links.d.ts.map +1 -1
  27. package/dist/state/links.js +30 -19
  28. package/dist/state/links.js.map +1 -1
  29. package/dist/state/mark.d.ts +5 -2
  30. package/dist/state/mark.d.ts.map +1 -1
  31. package/dist/state/mark.js +20 -3
  32. package/dist/state/mark.js.map +1 -1
  33. package/dist/state/pictures.d.ts +51 -0
  34. package/dist/state/pictures.d.ts.map +1 -0
  35. package/dist/state/pictures.js +140 -0
  36. package/dist/state/pictures.js.map +1 -0
  37. package/dist/state/review.d.ts +43 -0
  38. package/dist/state/review.d.ts.map +1 -0
  39. package/dist/state/review.js +361 -0
  40. package/dist/state/review.js.map +1 -0
  41. package/dist/state/selection.d.ts +12 -1
  42. package/dist/state/selection.d.ts.map +1 -1
  43. package/dist/state/selection.js +12 -0
  44. package/dist/state/selection.js.map +1 -1
  45. package/dist/state/state.d.ts +2 -0
  46. package/dist/state/state.d.ts.map +1 -1
  47. package/dist/state/state.js +8 -1
  48. package/dist/state/state.js.map +1 -1
  49. package/dist/state/tablecommands.d.ts +77 -0
  50. package/dist/state/tablecommands.d.ts.map +1 -0
  51. package/dist/state/tablecommands.js +675 -0
  52. package/dist/state/tablecommands.js.map +1 -0
  53. package/dist/state/tables.d.ts +110 -0
  54. package/dist/state/tables.d.ts.map +1 -0
  55. package/dist/state/tables.js +729 -0
  56. package/dist/state/tables.js.map +1 -0
  57. package/dist/state/track.d.ts +47 -0
  58. package/dist/state/track.d.ts.map +1 -0
  59. package/dist/state/track.js +583 -0
  60. package/dist/state/track.js.map +1 -0
  61. package/dist/state/word.d.ts +30 -2
  62. package/dist/state/word.d.ts.map +1 -1
  63. package/dist/state/word.js +83 -3
  64. package/dist/state/word.js.map +1 -1
  65. package/dist/state/wordclip.d.ts.map +1 -1
  66. package/dist/state/wordclip.js +26 -9
  67. package/dist/state/wordclip.js.map +1 -1
  68. package/dist/state/wordformat.d.ts +9 -1
  69. package/dist/state/wordformat.d.ts.map +1 -1
  70. package/dist/state/wordformat.js +38 -16
  71. package/dist/state/wordformat.js.map +1 -1
  72. package/dist/v3/document.d.ts +114 -7
  73. package/dist/v3/document.d.ts.map +1 -1
  74. package/dist/v3/document.js +170 -13
  75. package/dist/v3/document.js.map +1 -1
  76. package/dist/v3/index.d.ts +2 -2
  77. package/dist/v3/index.d.ts.map +1 -1
  78. package/dist/v3/index.js +2 -2
  79. package/dist/v3/index.js.map +1 -1
  80. package/dist/v3/migrate.d.ts +2 -2
  81. package/dist/v3/migrate.d.ts.map +1 -1
  82. package/dist/v3/resolve.d.ts +50 -4
  83. package/dist/v3/resolve.d.ts.map +1 -1
  84. package/dist/v3/resolve.js +205 -15
  85. package/dist/v3/resolve.js.map +1 -1
  86. package/dist/v3/schema.d.ts +6 -0
  87. package/dist/v3/schema.d.ts.map +1 -1
  88. package/dist/v3/schema.js +168 -6
  89. package/dist/v3/schema.js.map +1 -1
  90. package/package.json +1 -1
  91. package/src/model/schema.ts +18 -2
  92. package/src/state/commands.ts +110 -32
  93. package/src/state/comments.ts +342 -0
  94. package/src/state/editor.ts +97 -1
  95. package/src/state/history.ts +3 -2
  96. package/src/state/index.ts +74 -1
  97. package/src/state/links.ts +27 -17
  98. package/src/state/mark.ts +20 -3
  99. package/src/state/pictures.ts +168 -0
  100. package/src/state/review.ts +412 -0
  101. package/src/state/selection.ts +15 -1
  102. package/src/state/state.ts +8 -1
  103. package/src/state/tablecommands.ts +741 -0
  104. package/src/state/tables.ts +760 -0
  105. package/src/state/track.ts +659 -0
  106. package/src/state/word.ts +89 -4
  107. package/src/state/wordclip.ts +28 -11
  108. package/src/state/wordformat.ts +35 -13
  109. package/src/v3/document.ts +285 -14
  110. package/src/v3/index.ts +14 -0
  111. package/src/v3/migrate.ts +2 -2
  112. package/src/v3/resolve.ts +259 -15
  113. package/src/v3/schema.ts +190 -7
package/src/state/word.ts CHANGED
@@ -10,7 +10,8 @@
10
10
  * become the commands themselves.
11
11
  */
12
12
  import type { Attrs, Mark, MarkType, Node } from "../model/index.js";
13
- import type { FoliDocument } from "../v3/document.js";
13
+ import { Step, StepResult } from "../transform/steps.js";
14
+ import type { CommentJSON, FoliDocument } from "../v3/document.js";
14
15
  import { Resolver, type RunFormat } from "../v3/resolve.js";
15
16
  import { MARK_ATTRS } from "./mark.js";
16
17
  import { NodeSelection, TextSelection } from "./selection.js";
@@ -32,9 +33,85 @@ export interface WordContext {
32
33
 
33
34
  const key = new PluginKey<WordContext>("word");
34
35
 
36
+ /**
37
+ * Sets, adds or removes the envelope's comment `id` (`comment` null removes it). A step, unlike
38
+ * `withEnvelope`, so undo and redo take comments with the text they annotate. It leaves the
39
+ * tree as it is; `index` is where an added comment goes in the list (a removed one's place).
40
+ */
41
+ export class CommentStep extends Step {
42
+ constructor(
43
+ readonly id: number,
44
+ readonly comment: CommentJSON | null,
45
+ readonly previous: CommentJSON | null,
46
+ readonly index: number,
47
+ ) {
48
+ super();
49
+ }
50
+
51
+ apply(doc: Node): StepResult {
52
+ return StepResult.ok(doc);
53
+ }
54
+
55
+ invert(): Step {
56
+ return new CommentStep(this.id, this.previous, this.comment, this.index);
57
+ }
58
+
59
+ map(): Step {
60
+ return this;
61
+ }
62
+ }
63
+
64
+ /** `comments` after the comment steps among `steps`. */
65
+ export function applyCommentSteps(
66
+ comments: readonly CommentJSON[],
67
+ steps: readonly Step[],
68
+ ): readonly CommentJSON[] {
69
+ let next = comments;
70
+ for (const step of steps) {
71
+ if (!(step instanceof CommentStep)) continue;
72
+ const at = next.findIndex((c) => c.id === step.id);
73
+ // A change made since (outside history) wins: undo does not bring back or overwrite it.
74
+ if ((step.previous === null) !== at < 0) continue;
75
+ const rest = at < 0 ? next : [...next.slice(0, at), ...next.slice(at + 1)];
76
+ if (!step.comment) next = rest;
77
+ else {
78
+ const index = at < 0 ? Math.min(step.index, rest.length) : at;
79
+ next = [...rest.slice(0, index), step.comment, ...rest.slice(index)];
80
+ }
81
+ }
82
+ return next;
83
+ }
84
+
85
+ /**
86
+ * Makes `tr` change the comments from `from` (the state's) to `to`, one `CommentStep` per
87
+ * comment added, changed or removed, after any comment steps `tr` already has. Comments kept
88
+ * keep their order (a new one goes at its index in `to`).
89
+ */
90
+ export function setComments(
91
+ tr: Transaction,
92
+ from: readonly CommentJSON[],
93
+ to: readonly CommentJSON[],
94
+ ): Transaction {
95
+ let current = applyCommentSteps(from, tr.steps);
96
+ const wanted = new Map(to.map((c) => [c.id, c]));
97
+ for (const c of current)
98
+ if (!wanted.has(c.id)) {
99
+ tr.step(new CommentStep(c.id, null, c, current.indexOf(c)));
100
+ current = applyCommentSteps(current, tr.steps.slice(-1));
101
+ }
102
+ to.forEach((c, index) => {
103
+ const before = current.find((p) => p.id === c.id) ?? null;
104
+ if (before === c || (before && JSON.stringify(before) === JSON.stringify(c))) return;
105
+ tr.step(new CommentStep(c.id, c, before, index));
106
+ current = applyCommentSteps(current, tr.steps.slice(-1));
107
+ });
108
+ return tr;
109
+ }
110
+
35
111
  /**
36
112
  * The plugin that gives commands a v3 document's envelope. A transaction changes it with
37
113
  * `withEnvelope` (a new list's numbering definition); undo leaves such additions, as Word does.
114
+ * Comments change only through `CommentStep`s, which undo takes back.
38
115
  */
39
116
  export function wordPlugin(envelope: WordEnvelope): Plugin<WordContext> {
40
117
  // Not a secret: only tells documents apart.
@@ -46,13 +123,21 @@ export function wordPlugin(envelope: WordEnvelope): Plugin<WordContext> {
46
123
  init: () => context,
47
124
  apply: (tr, value) => {
48
125
  const next = tr.getMeta(key) as WordEnvelope | undefined;
49
- return next ? { envelope: next, resolver: new Resolver(next), id: value.id } : value;
126
+ const comments = applyCommentSteps(value.envelope.comments, tr.steps);
127
+ if (!next && comments === value.envelope.comments) return value;
128
+ const envelope = { ...(next ?? value.envelope), comments: comments as CommentJSON[] };
129
+ // Only comments changed: they do not affect formatting, the resolver stays.
130
+ const resolver = next ? new Resolver(envelope) : value.resolver;
131
+ return { envelope, resolver, id: value.id };
50
132
  },
51
133
  },
52
134
  });
53
135
  }
54
136
 
55
- /** Makes `tr` change the document's envelope to `envelope` (styles, numbering…). */
137
+ /**
138
+ * Makes `tr` change the document's envelope to `envelope` (styles, numbering…). Its comments
139
+ * are ignored: those change through `setComments`, so undo takes them back.
140
+ */
56
141
  export function withEnvelope(tr: Transaction, envelope: WordEnvelope): Transaction {
57
142
  return tr.setMeta(key, envelope);
58
143
  }
@@ -167,7 +252,7 @@ export function wordToggleActive(
167
252
  ): boolean {
168
253
  const { selection } = state;
169
254
  if (caretMarks) return shows(context, selection.$from.parent, caretMarks, type);
170
- const { judged } = inlineIn(state, selection.from, selection.to, type);
255
+ const judged = selection.ranges.flatMap((r) => inlineIn(state, r.from, r.to, type).judged);
171
256
  return judged.length > 0 && judged.every((f) => shows(context, f.parent, f.node.marks, type));
172
257
  }
173
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