@folijs/core 0.2.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 (136) hide show
  1. package/CHANGELOG.md +45 -0
  2. package/README.md +1 -1
  3. package/dist/document.d.ts +10 -16
  4. package/dist/document.d.ts.map +1 -1
  5. package/dist/document.js +22 -22
  6. package/dist/document.js.map +1 -1
  7. package/dist/index.d.ts +3 -2
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +3 -2
  10. package/dist/index.js.map +1 -1
  11. package/dist/model/fragment.d.ts +5 -2
  12. package/dist/model/fragment.d.ts.map +1 -1
  13. package/dist/model/fragment.js +5 -2
  14. package/dist/model/fragment.js.map +1 -1
  15. package/dist/model/node.d.ts +1 -1
  16. package/dist/model/node.d.ts.map +1 -1
  17. package/dist/model/node.js.map +1 -1
  18. package/dist/schema.d.ts +10 -1
  19. package/dist/schema.d.ts.map +1 -1
  20. package/dist/schema.js +11 -1
  21. package/dist/schema.js.map +1 -1
  22. package/dist/state/commands.d.ts +7 -6
  23. package/dist/state/commands.d.ts.map +1 -1
  24. package/dist/state/commands.js +162 -97
  25. package/dist/state/commands.js.map +1 -1
  26. package/dist/state/comments.d.ts +47 -0
  27. package/dist/state/comments.d.ts.map +1 -0
  28. package/dist/state/comments.js +304 -0
  29. package/dist/state/comments.js.map +1 -0
  30. package/dist/state/editor.d.ts +67 -16
  31. package/dist/state/editor.d.ts.map +1 -1
  32. package/dist/state/editor.js +74 -18
  33. package/dist/state/editor.js.map +1 -1
  34. package/dist/state/format.d.ts +8 -22
  35. package/dist/state/format.d.ts.map +1 -1
  36. package/dist/state/format.js +16 -140
  37. package/dist/state/format.js.map +1 -1
  38. package/dist/state/history.d.ts +1 -1
  39. package/dist/state/history.d.ts.map +1 -1
  40. package/dist/state/history.js +3 -2
  41. package/dist/state/history.js.map +1 -1
  42. package/dist/state/index.d.ts +11 -4
  43. package/dist/state/index.d.ts.map +1 -1
  44. package/dist/state/index.js +11 -4
  45. package/dist/state/index.js.map +1 -1
  46. package/dist/state/links.d.ts.map +1 -1
  47. package/dist/state/links.js +10 -2
  48. package/dist/state/links.js.map +1 -1
  49. package/dist/state/lists.d.ts +19 -32
  50. package/dist/state/lists.d.ts.map +1 -1
  51. package/dist/state/lists.js +23 -372
  52. package/dist/state/lists.js.map +1 -1
  53. package/dist/state/mark.d.ts +28 -0
  54. package/dist/state/mark.d.ts.map +1 -0
  55. package/dist/state/mark.js +57 -0
  56. package/dist/state/mark.js.map +1 -0
  57. package/dist/state/review.d.ts +43 -0
  58. package/dist/state/review.d.ts.map +1 -0
  59. package/dist/state/review.js +282 -0
  60. package/dist/state/review.js.map +1 -0
  61. package/dist/state/state.d.ts +2 -0
  62. package/dist/state/state.d.ts.map +1 -1
  63. package/dist/state/state.js +18 -0
  64. package/dist/state/state.js.map +1 -1
  65. package/dist/state/track.d.ts +47 -0
  66. package/dist/state/track.d.ts.map +1 -0
  67. package/dist/state/track.js +534 -0
  68. package/dist/state/track.js.map +1 -0
  69. package/dist/state/word.d.ts +100 -0
  70. package/dist/state/word.d.ts.map +1 -0
  71. package/dist/state/word.js +253 -0
  72. package/dist/state/word.js.map +1 -0
  73. package/dist/state/wordclip.d.ts +43 -0
  74. package/dist/state/wordclip.d.ts.map +1 -0
  75. package/dist/state/wordclip.js +412 -0
  76. package/dist/state/wordclip.js.map +1 -0
  77. package/dist/state/wordformat.d.ts +136 -0
  78. package/dist/state/wordformat.d.ts.map +1 -0
  79. package/dist/state/wordformat.js +846 -0
  80. package/dist/state/wordformat.js.map +1 -0
  81. package/dist/v3/check.d.ts +33 -0
  82. package/dist/v3/check.d.ts.map +1 -0
  83. package/dist/v3/check.js +90 -0
  84. package/dist/v3/check.js.map +1 -0
  85. package/dist/v3/document.d.ts +217 -0
  86. package/dist/v3/document.d.ts.map +1 -0
  87. package/dist/v3/document.js +225 -0
  88. package/dist/v3/document.js.map +1 -0
  89. package/dist/v3/index.d.ts +6 -0
  90. package/dist/v3/index.d.ts.map +1 -0
  91. package/dist/v3/index.js +6 -0
  92. package/dist/v3/index.js.map +1 -0
  93. package/dist/v3/migrate.d.ts +19 -0
  94. package/dist/v3/migrate.d.ts.map +1 -0
  95. package/dist/v3/migrate.js +227 -0
  96. package/dist/v3/migrate.js.map +1 -0
  97. package/dist/v3/resolve.d.ts +129 -0
  98. package/dist/v3/resolve.d.ts.map +1 -0
  99. package/dist/v3/resolve.js +534 -0
  100. package/dist/v3/resolve.js.map +1 -0
  101. package/dist/v3/schema.d.ts +29 -0
  102. package/dist/v3/schema.d.ts.map +1 -0
  103. package/dist/v3/schema.js +431 -0
  104. package/dist/v3/schema.js.map +1 -0
  105. package/dist/v3/units.d.ts +16 -0
  106. package/dist/v3/units.d.ts.map +1 -0
  107. package/dist/v3/units.js +28 -0
  108. package/dist/v3/units.js.map +1 -0
  109. package/package.json +1 -1
  110. package/src/document.ts +26 -26
  111. package/src/index.ts +3 -6
  112. package/src/model/fragment.ts +11 -3
  113. package/src/model/node.ts +6 -1
  114. package/src/schema.ts +13 -1
  115. package/src/state/commands.ts +173 -91
  116. package/src/state/comments.ts +342 -0
  117. package/src/state/editor.ts +176 -36
  118. package/src/state/format.ts +24 -122
  119. package/src/state/history.ts +3 -2
  120. package/src/state/index.ts +73 -3
  121. package/src/state/links.ts +10 -2
  122. package/src/state/lists.ts +31 -408
  123. package/src/state/mark.ts +64 -0
  124. package/src/state/review.ts +330 -0
  125. package/src/state/state.ts +18 -0
  126. package/src/state/track.ts +602 -0
  127. package/src/state/word.ts +330 -0
  128. package/src/state/wordclip.ts +445 -0
  129. package/src/state/wordformat.ts +982 -0
  130. package/src/v3/check.ts +119 -0
  131. package/src/v3/document.ts +456 -0
  132. package/src/v3/index.ts +33 -0
  133. package/src/v3/migrate.ts +267 -0
  134. package/src/v3/resolve.ts +646 -0
  135. package/src/v3/schema.ts +482 -0
  136. package/src/v3/units.ts +34 -0
@@ -0,0 +1,267 @@
1
+ import type { FoliDocumentV2 } from "../document.js";
2
+ import type { MarkJSON, NodeJSON } from "../model/index.js";
3
+ import type { BlockJSON, InlineJSON, MarkNameJSON } from "../schema.js";
4
+ import type {
5
+ AbstractNumberingJSON,
6
+ FoliDocument,
7
+ NumberingInstanceJSON,
8
+ SectionJSON,
9
+ StyleJSON,
10
+ } from "./document.js";
11
+ import { mmToTwips } from "./units.js";
12
+
13
+ /**
14
+ * Schema v2 → v3 (ADR-0005): the content keeps every character, mark and break; its structure
15
+ * becomes Word's.
16
+ * - Headings become paragraphs styled `Heading1`–`Heading6`.
17
+ * - Lists become numbered paragraphs (style `ListParagraph`, `numbering` id and level): one
18
+ * numbering instance per list, a nested list of the same kind continuing its parent's at the
19
+ * next level; further blocks of an item are indented to its text. Levels past Word's 9 stay at
20
+ * the ninth.
21
+ * - A horizontal rule becomes an empty paragraph with a bottom border.
22
+ * - Alignment becomes justification; text color `color`; highlight (any color) `shading`, as
23
+ * Word records a background color that is not one of its highlight colors. Linked text gets
24
+ * Word's "Hyperlink" character style (blue, underlined), the look links had.
25
+ * - `settings` becomes the section, in twips (rounded to the twip: under 0.02 mm).
26
+ * The styles and numbering definitions the result references are added.
27
+ */
28
+ export function migrateToV3(doc: FoliDocumentV2): FoliDocument {
29
+ const instances: NumberingInstanceJSON[] = [];
30
+ const content = blocks(doc.content.content, { instances });
31
+ return {
32
+ schemaVersion: 3,
33
+ section: section(doc.settings),
34
+ // The default stylesheet's look, rather than Word's empty-defaults 10 pt: 11 pt Georgia, 1.5
35
+ // lines, and the browser's 1em between paragraphs as space after.
36
+ defaults: {
37
+ paragraph: { spacing: { after: 220, line: 360, lineRule: "auto" } },
38
+ run: [
39
+ { type: "font", attrs: { ascii: "Georgia", hAnsi: "Georgia", cs: "Georgia" } },
40
+ { type: "fontSize", attrs: { value: 22, complexScript: 22 } },
41
+ ],
42
+ },
43
+ // Copies: no two documents share an object.
44
+ styles: structuredClone(STYLES),
45
+ numbering: { abstracts: structuredClone([BULLETS, NUMBERS]), instances },
46
+ comments: [],
47
+ content: { type: "doc", content },
48
+ };
49
+ }
50
+
51
+ interface Context {
52
+ readonly instances: NumberingInstanceJSON[];
53
+ }
54
+
55
+ type Kind = "bullet" | "number";
56
+
57
+ const INDENT = 720; // twips per list level: 0.5 inch, Word's
58
+ const HANGING = 360;
59
+
60
+ /** Millimetres to twips within Word's limits (22 inches: v2 allowed any size). */
61
+ function twips(mm: number): number {
62
+ return Math.min(mmToTwips(mm), 31_680);
63
+ }
64
+
65
+ function section(settings: FoliDocumentV2["settings"]): SectionJSON {
66
+ const { width, height } = settings.pageSize;
67
+ const { top, right, bottom, left } = settings.margins;
68
+ return {
69
+ pageSize: {
70
+ width: Math.max(twips(width), 1),
71
+ height: Math.max(twips(height), 1),
72
+ ...(width > height ? { orientation: "landscape" as const } : {}),
73
+ },
74
+ margins: {
75
+ top: twips(top),
76
+ right: twips(right),
77
+ bottom: twips(bottom),
78
+ left: twips(left),
79
+ },
80
+ };
81
+ }
82
+
83
+ function blocks(list: readonly BlockJSON[], context: Context): NodeJSON[] {
84
+ const out: NodeJSON[] = [];
85
+ for (const block of list) {
86
+ if (block.type === "bulletList" || block.type === "orderedList") {
87
+ listBlocks(block, 0, undefined, context, out);
88
+ } else {
89
+ out.push(paragraph(block, {}));
90
+ }
91
+ }
92
+ return out;
93
+ }
94
+
95
+ /** A v2 list's items as numbered paragraphs, appended to `out`. */
96
+ function listBlocks(
97
+ list: Extract<BlockJSON, { type: "bulletList" | "orderedList" }>,
98
+ depth: number,
99
+ parent: { id: number; kind: Kind; item: { continued: boolean } } | undefined,
100
+ context: Context,
101
+ out: NodeJSON[],
102
+ ): void {
103
+ const level = Math.min(depth, 8);
104
+ const kind: Kind = list.type === "bulletList" ? "bullet" : "number";
105
+ const start = list.type === "orderedList" ? (list.attrs?.start ?? 1) : 1;
106
+ // A nested list of the same kind starting at 1 continues its parent's numbering one level
107
+ // down: Word restarts the deeper level after each item, as v2 numbered it. Only the item's
108
+ // first such list (a second one would continue the first's count) and only while the level
109
+ // goes down (past the ninth, it would continue the parent's).
110
+ const continues =
111
+ parent !== undefined &&
112
+ parent.kind === kind &&
113
+ start === 1 &&
114
+ depth <= 8 &&
115
+ !parent.item.continued;
116
+ let id: number | undefined;
117
+ if (continues) {
118
+ id = parent.id;
119
+ parent.item.continued = true;
120
+ }
121
+ if (id === undefined) {
122
+ id = context.instances.length + 1;
123
+ context.instances.push({
124
+ id,
125
+ abstract: kind === "bullet" ? BULLETS.id : NUMBERS.id,
126
+ // An explicit start restarts the numbering, whatever instances share the definition.
127
+ ...(kind === "number" ? { overrides: [{ level, start }] } : {}),
128
+ });
129
+ }
130
+ for (const item of list.content) {
131
+ const [first, ...rest] = item.content;
132
+ const state = { continued: false };
133
+ out.push(paragraph(first, { style: "ListParagraph", numbering: { id, level } }));
134
+ for (const block of rest) {
135
+ if (block.type === "bulletList" || block.type === "orderedList") {
136
+ listBlocks(block, depth + 1, { id, kind, item: state }, context, out);
137
+ } else {
138
+ // More of the item: aligned with its text.
139
+ const style =
140
+ block.type === "paragraph" || block.type === "horizontalRule"
141
+ ? { style: "ListParagraph" }
142
+ : {};
143
+ out.push(paragraph(block, { ...style, indent: { left: INDENT * (level + 1) } }));
144
+ }
145
+ }
146
+ }
147
+ }
148
+
149
+ function paragraph(
150
+ block: Exclude<BlockJSON, { type: "bulletList" | "orderedList" }>,
151
+ extra: Record<string, unknown>,
152
+ ): NodeJSON {
153
+ if (block.type === "horizontalRule") {
154
+ return {
155
+ type: "paragraph",
156
+ attrs: {
157
+ ...extra,
158
+ borders: { bottom: { style: "single", size: 6, space: 1, color: "auto" } },
159
+ },
160
+ };
161
+ }
162
+ const attrs: Record<string, unknown> = { ...extra };
163
+ if (block.type === "heading") attrs.style = `Heading${block.attrs.level}`;
164
+ const align = block.attrs?.align;
165
+ if (align && align !== "start") attrs.justification = JUSTIFY[align];
166
+ return {
167
+ type: "paragraph",
168
+ ...(Object.keys(attrs).length ? { attrs } : {}),
169
+ ...(block.content?.length ? { content: block.content.map(inline) } : {}),
170
+ };
171
+ }
172
+
173
+ const JUSTIFY = { center: "center", end: "right", justify: "both" } as const;
174
+
175
+ function inline(node: InlineJSON): NodeJSON {
176
+ const marks = node.marks?.flatMap((m) =>
177
+ m.type === "link"
178
+ ? [mark(m), { type: "characterStyle", attrs: { id: "Hyperlink" } }]
179
+ : [mark(m)],
180
+ );
181
+ return { ...node, ...(marks?.length ? { marks } : {}) } as NodeJSON;
182
+ }
183
+
184
+ function mark(m: MarkNameJSON): MarkJSON {
185
+ switch (m.type) {
186
+ case "link":
187
+ return { type: "link", attrs: { href: m.attrs.href } };
188
+ case "textColor":
189
+ return { type: "color", attrs: { value: m.attrs.color.slice(1).toUpperCase() } };
190
+ case "highlight":
191
+ return {
192
+ type: "shading",
193
+ attrs: { pattern: "clear", fill: m.attrs.color.slice(1).toUpperCase() },
194
+ };
195
+ default:
196
+ return { type: m.type };
197
+ }
198
+ }
199
+
200
+ /** The styles migrated documents use, as Word names them. */
201
+ const HEADING_SIZES = [32, 28, 26, 24, 22, 22]; // half-points
202
+
203
+ const STYLES: StyleJSON[] = [
204
+ { id: "Normal", type: "paragraph", name: "Normal", isDefault: true },
205
+ ...HEADING_SIZES.map(
206
+ (size, i): StyleJSON => ({
207
+ id: `Heading${i + 1}`,
208
+ type: "paragraph",
209
+ name: `heading ${i + 1}`,
210
+ basedOn: "Normal",
211
+ next: "Normal",
212
+ paragraph: {
213
+ keepNext: true,
214
+ keepLines: true,
215
+ outlineLevel: i,
216
+ spacing: { before: 240, after: 120 },
217
+ },
218
+ run: [{ type: "bold" }, { type: "fontSize", attrs: { value: size } }],
219
+ }),
220
+ ),
221
+ {
222
+ id: "ListParagraph",
223
+ type: "paragraph",
224
+ name: "List Paragraph",
225
+ basedOn: "Normal",
226
+ paragraph: { indent: { left: INDENT }, contextualSpacing: true },
227
+ },
228
+ {
229
+ id: "DefaultParagraphFont",
230
+ type: "character",
231
+ name: "Default Paragraph Font",
232
+ isDefault: true,
233
+ },
234
+ {
235
+ id: "Hyperlink",
236
+ type: "character",
237
+ name: "Hyperlink",
238
+ basedOn: "DefaultParagraphFont",
239
+ run: [
240
+ { type: "color", attrs: { value: "0563C1", themeColor: "hyperlink" } },
241
+ { type: "underline" },
242
+ ],
243
+ },
244
+ ];
245
+
246
+ function levels(kind: Kind): AbstractNumberingJSON["levels"] {
247
+ return Array.from({ length: 9 }, (_, level) => ({
248
+ level,
249
+ start: 1,
250
+ // v2 showed nested numbers as decimal at every level (the browser's default).
251
+ format: kind === "bullet" ? "bullet" : "decimal",
252
+ text: kind === "bullet" ? (["•", "◦", "▪"][level % 3] as string) : `%${level + 1}.`,
253
+ justification: "left",
254
+ paragraph: { indent: { left: INDENT * (level + 1), hanging: HANGING } },
255
+ }));
256
+ }
257
+
258
+ const BULLETS: AbstractNumberingJSON = {
259
+ id: 0,
260
+ multiLevelType: "hybridMultilevel",
261
+ levels: levels("bullet"),
262
+ };
263
+ const NUMBERS: AbstractNumberingJSON = {
264
+ id: 1,
265
+ multiLevelType: "hybridMultilevel",
266
+ levels: levels("number"),
267
+ };