@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
@@ -4,6 +4,7 @@ import { TransformError } from "../transform/transform.js";
4
4
  import { attempt } from "./attempt.js";
5
5
  import { AllSelection, marksAt, TextSelection } from "./selection.js";
6
6
  import type { Command, EditorState, Transaction } from "./state.js";
7
+ import { CellSelection } from "./tables.js";
7
8
  import { styleLink, unstyleLink } from "./wordformat.js";
8
9
 
9
10
  const EMAIL = /^[^\s@/:]+@[^\s@/:]+\.\p{L}{2,}$/iu;
@@ -78,10 +79,11 @@ function canLink(state: EditorState, type: MarkType): boolean {
78
79
  return parent.inlineContent && parent.type.allowsMarkType(type);
79
80
  }
80
81
  let found = false;
81
- state.doc.nodesBetween(selection.from, selection.to, (node) => {
82
- if (node.isTextblock && node.type.allowsMarkType(type)) found = true;
83
- return !found;
84
- });
82
+ for (const { from, to } of selection.ranges)
83
+ state.doc.nodesBetween(from, to, (node) => {
84
+ if (node.isTextblock && node.type.allowsMarkType(type)) found = true;
85
+ return !found;
86
+ });
85
87
  return found;
86
88
  }
87
89
 
@@ -97,13 +99,14 @@ export function linkAt(state: EditorState): string | null {
97
99
  return (linkAround(selection.$from, type)?.mark.attrs.href as string) ?? null;
98
100
  // undefined: no text seen yet; null: text without a link, or with another one.
99
101
  let href = undefined as string | null | undefined;
100
- state.doc.nodesBetween(selection.from, selection.to, (node, _pos, parent) => {
101
- if (href === null) return false;
102
- if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
103
- const value = (type.isInSet(node.marks)?.attrs.href as string | undefined) ?? null;
104
- href = href === undefined || href === value ? value : null;
105
- return false;
106
- });
102
+ for (const { from, to } of selection.ranges)
103
+ state.doc.nodesBetween(from, to, (node, _pos, parent) => {
104
+ if (href === null) return false;
105
+ if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
106
+ const value = (type.isInSet(node.marks)?.attrs.href as string | undefined) ?? null;
107
+ href = href === undefined || href === value ? value : null;
108
+ return false;
109
+ });
107
110
  return href ?? null;
108
111
  }
109
112
 
@@ -118,6 +121,8 @@ export function insertLink(href: string, text?: string): Command {
118
121
  const label = text ?? normalized;
119
122
  const { selection } = state;
120
123
  if (!type || !label || !isSafeHref(normalized)) return false;
124
+ // Selected cells are linked by `setLink`, never replaced by one link.
125
+ if (selection instanceof CellSelection) return false;
121
126
  if (!(selection instanceof AllSelection) && !canLink(state, type)) return false;
122
127
  const link = type.create({ href: normalized });
123
128
  return attempt(state, dispatch, (tr) => {
@@ -150,8 +155,10 @@ export function setLink(href: string): Command {
150
155
  const { selection } = state;
151
156
  if (!selection.empty) {
152
157
  return attempt(state, dispatch, (tr) => {
153
- tr.addMark(selection.from, selection.to, mark);
154
- styleLink(tr, state, selection.from, selection.to);
158
+ for (const { from, to } of selection.ranges) {
159
+ tr.addMark(from, to, mark);
160
+ styleLink(tr, state, from, to);
161
+ }
155
162
  });
156
163
  }
157
164
  const around = linkAround(selection.$from, type);
@@ -165,11 +172,14 @@ export const removeLink: Command = (state, dispatch) => {
165
172
  const type = linkType(state);
166
173
  if (!type) return false;
167
174
  const { selection } = state;
168
- const range = selection.empty ? linkAround(selection.$from, type) : selection;
169
- if (!range) return false;
175
+ const around = selection.empty && linkAround(selection.$from, type);
176
+ const ranges = selection.empty ? (around ? [around] : []) : selection.ranges;
177
+ if (!ranges.length) return false;
170
178
  return attempt(state, dispatch, (tr) => {
171
- tr.removeMark(range.from, range.to, type);
172
- unstyleLink(tr, state, range.from, range.to);
179
+ for (const range of ranges) {
180
+ tr.removeMark(range.from, range.to, type);
181
+ unstyleLink(tr, state, range.from, range.to);
182
+ }
173
183
  });
174
184
  };
175
185
 
package/src/state/mark.ts CHANGED
@@ -13,14 +13,31 @@ import type { Transform } from "../transform/transform.js";
13
13
  */
14
14
  export const MARK_ATTRS = ["section", "revision", "formatChange", "attributes"] as const;
15
15
 
16
+ /** Inline nodes that stand between runs: bookmarks and comment ranges. */
17
+ const MARKERS = new Set(["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"]);
18
+
16
19
  /**
17
20
  * After a join, or a deletion across paragraphs, the merged paragraph at `pos` ends with the
18
21
  * last paragraph's mark, as in Word: it takes that paragraph's mark attributes (deleting a
19
- * section break merges its section into the next one). Nothing for a schema without them (v2).
22
+ * section break merges its section into the next one). When the first paragraph left nothing
23
+ * (it was empty, or deleted from its start), it is gone: the last one stays whole, as in Word.
24
+ * `tail` is where the content kept of the last one starts in it (a deletion into it). Nothing for
25
+ * a schema without them (v2).
20
26
  */
21
- export function keepLastMark(tr: Transform, pos: number, last: Node): void {
27
+ export function keepLastMark(tr: Transform, pos: number, last: Node, tail = 0): void {
22
28
  const merged = tr.doc.nodeAt(pos);
23
- if (!merged?.isTextblock) return;
29
+ if (!merged?.isTextblock || !Object.hasOwn(merged.attrs, "revision")) return;
30
+ // What the first paragraph left, bookmarks and comment ranges aside (headings often start
31
+ // with a bookmark).
32
+ let left = false;
33
+ merged.content.cut(0, merged.content.size - (last.content.size - tail)).forEach((child) => {
34
+ if (!MARKERS.has(child.type.name)) left = true;
35
+ });
36
+ if (!left) {
37
+ if (merged.type !== last.type || JSON.stringify(merged.attrs) !== JSON.stringify(last.attrs))
38
+ tr.setNodeMarkup(pos, last.type, last.attrs);
39
+ return;
40
+ }
24
41
  const attrs = { ...merged.attrs };
25
42
  let changed = false;
26
43
  for (const key of MARK_ATTRS) {
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Picture commands (10.8, ADR-0006): inserting an inline picture (its image a new resource of
3
+ * the envelope; alt text required, or the picture marked decorative), and changing a picture's
4
+ * size and alt text. Inserting and deleting a picture is tracked like text while tracking; its
5
+ * size does not change while tracking (Foli does not record it yet); alt text, which Word does
6
+ * not track either, changes any time.
7
+ */
8
+ import type { Node } from "../model/index.js";
9
+ import { TransformError } from "../transform/index.js";
10
+ import type { ResourceJSON } from "../v3/document.js";
11
+ import { checkResource } from "../v3/document.js";
12
+ import { attempt } from "./attempt.js";
13
+ import { deleteSelectionKeepingTables } from "./commands.js";
14
+ import { closeHistory } from "./history.js";
15
+ import { typedMarks } from "./mark.js";
16
+ import { marksAt, NodeSelection, TextSelection } from "./selection.js";
17
+ import type { Command, EditorState } from "./state.js";
18
+ import { trackState, untracked } from "./track.js";
19
+ import { withEnvelope, wordContext } from "./word.js";
20
+
21
+ /** The largest size the schema takes, in EMU. */
22
+ const MAX_EMU = 2_147_483_647;
23
+
24
+ /** A picture to insert: its image (a `data:` URL or a URL the app gave) and its size in EMU. */
25
+ export interface NewPicture {
26
+ /** The image's media type (`image/png`…). */
27
+ contentType: string;
28
+ /** Where the image is: a URL the app gave (an upload's) … */
29
+ url?: string;
30
+ /** … or, opted into, a `data:` URL holding it. */
31
+ data?: string;
32
+ width: number;
33
+ height: number;
34
+ /** Required unless `decorative`. */
35
+ alt?: string;
36
+ title?: string;
37
+ /** Marked decorative: no alt text needed (a screen reader passes it by). */
38
+ decorative?: boolean;
39
+ }
40
+
41
+ const validSize = (n: number) => Number.isInteger(n) && n >= 1 && n <= MAX_EMU;
42
+ /** Alt text and titles as the schema holds them. */
43
+ const validText = (value: unknown) =>
44
+ value === undefined || (typeof value === "string" && value.length <= 65_535);
45
+ /** A `data:` URL's media type, lower case. */
46
+ const dataType = (data: string) => /^data:([^;,]*)/i.exec(data)?.[1]?.toLowerCase();
47
+
48
+ /**
49
+ * Inserts `picture` at the selection (replacing it), its image added to the document's
50
+ * resources under a new key. Does not apply without alt text (unless decorative, or
51
+ * `requireAlt` is false: a picture pasted or dropped, asked for later and marked), with an image
52
+ * the document cannot hold (not an image type, no URL or `data:` URL, an unsafe URL), or where
53
+ * the selection is not in text.
54
+ */
55
+ export function insertPicture(
56
+ picture: NewPicture,
57
+ { requireAlt = true }: { requireAlt?: boolean } = {},
58
+ ): Command {
59
+ return (state, dispatch) => {
60
+ const context = wordContext(state);
61
+ const type = state.schema.nodes.picture;
62
+ if (!context || !type) return false;
63
+ if (!validText(picture.alt) || !validText(picture.title)) return false;
64
+ const alt = picture.decorative ? "" : (picture.alt?.trim() ?? "");
65
+ if (!alt && !picture.decorative && requireAlt) return false;
66
+ if (!validSize(picture.width) || !validSize(picture.height)) return false;
67
+ // The bytes are of the type the document says (a part is named from it on export).
68
+ if (
69
+ picture.data !== undefined &&
70
+ dataType(String(picture.data)) !== String(picture.contentType).toLowerCase()
71
+ )
72
+ return false;
73
+ const resource: ResourceJSON = {
74
+ contentType: picture.contentType,
75
+ ...(picture.url !== undefined ? { url: picture.url } : {}),
76
+ ...(picture.data !== undefined ? { data: picture.data } : {}),
77
+ };
78
+ if (checkResource(resource) !== undefined) return false;
79
+ const resources = context.envelope.resources ?? {};
80
+ let n = Object.keys(resources).length + 1;
81
+ while (Object.hasOwn(resources, `foli-picture-${n}`)) n++;
82
+ const key = `foli-picture-${n}`;
83
+ const node = type.create({
84
+ resource: key,
85
+ width: picture.width,
86
+ height: picture.height,
87
+ alt: alt || null,
88
+ title: picture.title || null,
89
+ decorative: picture.decorative || null,
90
+ });
91
+ return attempt(state, dispatch, (tr) => {
92
+ tr.setMeta(closeHistory, true);
93
+ // Over selected cells, into the anchor cell (as typing goes).
94
+ if (!tr.selection.empty) deleteSelectionKeepingTables(tr, true);
95
+ const $pos = tr.selection.$from;
96
+ if (!$pos.parent.inlineContent || !$pos.parent.type.contentMatch.matchType(type))
97
+ throw new TransformError("No text where the picture would go");
98
+ // The marks typing would get there (its run's properties, a link), never a tracked
99
+ // change's: tracking marks it as its own.
100
+ const marks = typedMarks(state.storedMarks ?? marksAt($pos)).filter((m) =>
101
+ $pos.parent.type.allowsMarkType(m.type),
102
+ );
103
+ tr.insert($pos.pos, node.mark(marks));
104
+ withEnvelope(tr, { ...context.envelope, resources: { ...resources, [key]: resource } });
105
+ tr.setSelection(TextSelection.create(tr.doc, $pos.pos + 1));
106
+ });
107
+ };
108
+ }
109
+
110
+ /** The selected picture (a node selection of one) and its position, if any. */
111
+ export function selectedPicture(state: EditorState): { node: Node; pos: number } | null {
112
+ const { selection } = state;
113
+ return selection instanceof NodeSelection && selection.node.type.name === "picture"
114
+ ? { node: selection.node, pos: selection.from }
115
+ : null;
116
+ }
117
+
118
+ /** The picture at `pos`, if there is one. */
119
+ const pictureAt = (state: EditorState, pos: number) => {
120
+ const node = pos >= 0 && pos < state.doc.content.size ? state.doc.nodeAt(pos) : null;
121
+ return node?.type.name === "picture" ? node : null;
122
+ };
123
+
124
+ /**
125
+ * Sets the size (EMU) of the picture at `pos`. Not while tracking (Foli does not record a
126
+ * picture's size change yet).
127
+ */
128
+ export function setPictureSize(pos: number, width: number, height: number): Command {
129
+ return (state, dispatch) => {
130
+ const node = pictureAt(state, pos);
131
+ if (!node || trackState(state)?.on === true) return false;
132
+ if (!validSize(width) || !validSize(height)) return false;
133
+ if (node.attrs.width === width && node.attrs.height === height) return false;
134
+ return attempt(state, dispatch, (tr) => {
135
+ tr.setMeta(closeHistory, true);
136
+ tr.setNodeMarkup(pos, node.type, { ...node.attrs, width, height });
137
+ // The picture stays selected (a resize keeps working on it).
138
+ if (state.selection instanceof NodeSelection && state.selection.from === pos)
139
+ tr.setSelection(NodeSelection.create(tr.doc, pos));
140
+ });
141
+ };
142
+ }
143
+
144
+ /**
145
+ * Sets the alt text of the picture at `pos`, or marks it decorative (no alt text). Does not
146
+ * apply with empty alt text unless decorative.
147
+ */
148
+ export function setPictureAlt(pos: number, alt: string, decorative = false): Command {
149
+ return (state, dispatch) => {
150
+ const node = pictureAt(state, pos);
151
+ if (!node || !validText(alt)) return false;
152
+ // A decorative picture has no alt text (a screen reader passes it by).
153
+ const text = decorative ? null : alt.trim() || null;
154
+ if (!text && !decorative) return false;
155
+ if (node.attrs.alt === text && (node.attrs.decorative === true) === decorative) return false;
156
+ return attempt(state, dispatch, (tr) => {
157
+ // Not a tracked change, as in Word.
158
+ tr.setMeta(closeHistory, true).setMeta(untracked, true);
159
+ tr.setNodeMarkup(pos, node.type, {
160
+ ...node.attrs,
161
+ alt: text,
162
+ decorative: decorative || null,
163
+ });
164
+ if (state.selection instanceof NodeSelection && state.selection.from === pos)
165
+ tr.setSelection(NodeSelection.create(tr.doc, pos));
166
+ });
167
+ };
168
+ }