@folijs/core 0.3.0 → 0.4.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 (56) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/state/commands.d.ts.map +1 -1
  3. package/dist/state/commands.js +32 -4
  4. package/dist/state/commands.js.map +1 -1
  5. package/dist/state/comments.d.ts +47 -0
  6. package/dist/state/comments.d.ts.map +1 -0
  7. package/dist/state/comments.js +304 -0
  8. package/dist/state/comments.js.map +1 -0
  9. package/dist/state/editor.d.ts +42 -1
  10. package/dist/state/editor.d.ts.map +1 -1
  11. package/dist/state/editor.js +30 -2
  12. package/dist/state/editor.js.map +1 -1
  13. package/dist/state/history.d.ts +1 -1
  14. package/dist/state/history.d.ts.map +1 -1
  15. package/dist/state/history.js +3 -2
  16. package/dist/state/history.js.map +1 -1
  17. package/dist/state/index.d.ts +4 -1
  18. package/dist/state/index.d.ts.map +1 -1
  19. package/dist/state/index.js +4 -1
  20. package/dist/state/index.js.map +1 -1
  21. package/dist/state/mark.d.ts +5 -2
  22. package/dist/state/mark.d.ts.map +1 -1
  23. package/dist/state/mark.js +20 -3
  24. package/dist/state/mark.js.map +1 -1
  25. package/dist/state/review.d.ts +43 -0
  26. package/dist/state/review.d.ts.map +1 -0
  27. package/dist/state/review.js +282 -0
  28. package/dist/state/review.js.map +1 -0
  29. package/dist/state/state.d.ts +2 -0
  30. package/dist/state/state.d.ts.map +1 -1
  31. package/dist/state/state.js +8 -1
  32. package/dist/state/state.js.map +1 -1
  33. package/dist/state/track.d.ts +47 -0
  34. package/dist/state/track.d.ts.map +1 -0
  35. package/dist/state/track.js +534 -0
  36. package/dist/state/track.js.map +1 -0
  37. package/dist/state/word.d.ts +30 -2
  38. package/dist/state/word.d.ts.map +1 -1
  39. package/dist/state/word.js +82 -2
  40. package/dist/state/word.js.map +1 -1
  41. package/dist/v3/schema.d.ts +2 -0
  42. package/dist/v3/schema.d.ts.map +1 -1
  43. package/dist/v3/schema.js +2 -0
  44. package/dist/v3/schema.js.map +1 -1
  45. package/package.json +1 -1
  46. package/src/state/commands.ts +30 -4
  47. package/src/state/comments.ts +342 -0
  48. package/src/state/editor.ts +95 -1
  49. package/src/state/history.ts +3 -2
  50. package/src/state/index.ts +40 -1
  51. package/src/state/mark.ts +20 -3
  52. package/src/state/review.ts +330 -0
  53. package/src/state/state.ts +8 -1
  54. package/src/state/track.ts +602 -0
  55. package/src/state/word.ts +88 -3
  56. package/src/v3/schema.ts +3 -0
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
  }
package/src/v3/schema.ts CHANGED
@@ -262,6 +262,9 @@ const paragraphProperties: Record<string, AttributeSpec> = {
262
262
  ext: nullableAttr(xml),
263
263
  };
264
264
 
265
+ /** The names of the paragraph properties (`w:pPr`) among a paragraph's attributes. */
266
+ export const PARAGRAPH_PROPERTY_KEYS: readonly string[] = Object.keys(paragraphProperties);
267
+
265
268
  /** Paragraph properties as a style, a numbering level, the defaults or a recorded change give them. */
266
269
  export const checkParagraphProperties: Check = (v) => {
267
270
  if (!isRecord(v)) return "must be an object";