@folijs/core 0.4.0 → 0.6.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.
- package/CHANGELOG.md +49 -0
- package/dist/model/schema.d.ts +9 -1
- package/dist/model/schema.d.ts.map +1 -1
- package/dist/model/schema.js +13 -3
- package/dist/model/schema.js.map +1 -1
- package/dist/state/commands.d.ts +10 -1
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +125 -40
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.d.ts.map +1 -1
- package/dist/state/comments.js +15 -6
- package/dist/state/comments.js.map +1 -1
- package/dist/state/controls.d.ts +128 -0
- package/dist/state/controls.d.ts.map +1 -0
- package/dist/state/controls.js +1459 -0
- package/dist/state/controls.js.map +1 -0
- package/dist/state/editor.d.ts +86 -1
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +169 -41
- package/dist/state/editor.js.map +1 -1
- package/dist/state/fields.d.ts +79 -0
- package/dist/state/fields.d.ts.map +1 -0
- package/dist/state/fields.js +398 -0
- package/dist/state/fields.js.map +1 -0
- package/dist/state/fieldupdate.d.ts +178 -0
- package/dist/state/fieldupdate.d.ts.map +1 -0
- package/dist/state/fieldupdate.js +1574 -0
- package/dist/state/fieldupdate.js.map +1 -0
- package/dist/state/find.d.ts +68 -0
- package/dist/state/find.d.ts.map +1 -0
- package/dist/state/find.js +397 -0
- package/dist/state/find.js.map +1 -0
- package/dist/state/history.d.ts +12 -1
- package/dist/state/history.d.ts.map +1 -1
- package/dist/state/history.js +43 -7
- package/dist/state/history.js.map +1 -1
- package/dist/state/index.d.ts +15 -5
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +13 -3
- package/dist/state/index.js.map +1 -1
- package/dist/state/links.d.ts.map +1 -1
- package/dist/state/links.js +30 -19
- package/dist/state/links.js.map +1 -1
- package/dist/state/mark.d.ts.map +1 -1
- package/dist/state/mark.js +8 -1
- package/dist/state/mark.js.map +1 -1
- package/dist/state/navigation.d.ts +70 -0
- package/dist/state/navigation.d.ts.map +1 -0
- package/dist/state/navigation.js +226 -0
- package/dist/state/navigation.js.map +1 -0
- package/dist/state/notes.d.ts +94 -0
- package/dist/state/notes.d.ts.map +1 -0
- package/dist/state/notes.js +444 -0
- package/dist/state/notes.js.map +1 -0
- package/dist/state/pictures.d.ts +51 -0
- package/dist/state/pictures.d.ts.map +1 -0
- package/dist/state/pictures.js +140 -0
- package/dist/state/pictures.js.map +1 -0
- package/dist/state/review.d.ts +13 -4
- package/dist/state/review.d.ts.map +1 -1
- package/dist/state/review.js +223 -29
- package/dist/state/review.js.map +1 -1
- package/dist/state/sections.d.ts +101 -0
- package/dist/state/sections.d.ts.map +1 -0
- package/dist/state/sections.js +472 -0
- package/dist/state/sections.js.map +1 -0
- package/dist/state/selection.d.ts +12 -1
- package/dist/state/selection.d.ts.map +1 -1
- package/dist/state/selection.js +12 -0
- package/dist/state/selection.js.map +1 -1
- package/dist/state/state.d.ts +2 -0
- package/dist/state/state.d.ts.map +1 -1
- package/dist/state/state.js +4 -0
- package/dist/state/state.js.map +1 -1
- package/dist/state/stories.d.ts +41 -0
- package/dist/state/stories.d.ts.map +1 -0
- package/dist/state/stories.js +243 -0
- package/dist/state/stories.js.map +1 -0
- package/dist/state/tablecommands.d.ts +77 -0
- package/dist/state/tablecommands.d.ts.map +1 -0
- package/dist/state/tablecommands.js +675 -0
- package/dist/state/tablecommands.js.map +1 -0
- package/dist/state/tables.d.ts +110 -0
- package/dist/state/tables.d.ts.map +1 -0
- package/dist/state/tables.js +730 -0
- package/dist/state/tables.js.map +1 -0
- package/dist/state/track.d.ts +23 -0
- package/dist/state/track.d.ts.map +1 -1
- package/dist/state/track.js +113 -15
- package/dist/state/track.js.map +1 -1
- package/dist/state/word.d.ts +78 -16
- package/dist/state/word.d.ts.map +1 -1
- package/dist/state/word.js +231 -11
- package/dist/state/word.js.map +1 -1
- package/dist/state/wordclip.d.ts +15 -6
- package/dist/state/wordclip.d.ts.map +1 -1
- package/dist/state/wordclip.js +213 -18
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +10 -2
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +98 -17
- package/dist/state/wordformat.js.map +1 -1
- package/dist/v3/document.d.ts +237 -7
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +314 -15
- package/dist/v3/document.js.map +1 -1
- package/dist/v3/index.d.ts +2 -2
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +2 -2
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/migrate.d.ts +2 -2
- package/dist/v3/migrate.d.ts.map +1 -1
- package/dist/v3/resolve.d.ts +57 -5
- package/dist/v3/resolve.d.ts.map +1 -1
- package/dist/v3/resolve.js +224 -16
- package/dist/v3/resolve.js.map +1 -1
- package/dist/v3/schema.d.ts +6 -0
- package/dist/v3/schema.d.ts.map +1 -1
- package/dist/v3/schema.js +254 -8
- package/dist/v3/schema.js.map +1 -1
- package/package.json +1 -1
- package/src/model/schema.ts +18 -2
- package/src/state/commands.ts +121 -32
- package/src/state/comments.ts +14 -6
- package/src/state/controls.ts +1596 -0
- package/src/state/editor.ts +292 -46
- package/src/state/fields.ts +432 -0
- package/src/state/fieldupdate.ts +1770 -0
- package/src/state/find.ts +510 -0
- package/src/state/history.ts +51 -7
- package/src/state/index.ts +157 -0
- package/src/state/links.ts +27 -17
- package/src/state/mark.ts +8 -1
- package/src/state/navigation.ts +305 -0
- package/src/state/notes.ts +519 -0
- package/src/state/numeric-pictures.word.json +172 -0
- package/src/state/pictures.ts +168 -0
- package/src/state/review.ts +265 -31
- package/src/state/sections.ts +571 -0
- package/src/state/selection.ts +15 -1
- package/src/state/state.ts +5 -0
- package/src/state/stories.ts +285 -0
- package/src/state/tablecommands.ts +741 -0
- package/src/state/tables.ts +761 -0
- package/src/state/track.ts +128 -16
- package/src/state/word.ts +276 -13
- package/src/state/wordclip.ts +231 -32
- package/src/state/wordformat.ts +96 -14
- package/src/v3/document.ts +572 -16
- package/src/v3/index.ts +21 -0
- package/src/v3/migrate.ts +2 -2
- package/src/v3/resolve.ts +289 -21
- package/src/v3/schema.ts +278 -9
package/src/state/wordclip.ts
CHANGED
|
@@ -14,6 +14,11 @@
|
|
|
14
14
|
* - From another document, a style or list the copy does not define is not one: the text is in
|
|
15
15
|
* the default style, not numbered (not the target's style or list of that id).
|
|
16
16
|
* - Paragraph ids (`w14:paraId`, `w14:textId`) are left out: export makes the ones it needs.
|
|
17
|
+
* - A note reference brings its note, carried by the copy, as a new story with a new id: a copy
|
|
18
|
+
* is a second note, and what was cut was removed with its reference (ADR-0007).
|
|
19
|
+
* - A content control copied inside its document is a new control with the same properties while
|
|
20
|
+
* the original stays (cut and pasted, it is the same one). From elsewhere its content comes
|
|
21
|
+
* without it.
|
|
17
22
|
*
|
|
18
23
|
* shortcut: ids inside opaque content (a table's bookmarks and paragraph ids) and tracked-change
|
|
19
24
|
* ids are pasted as they are; Word tolerates repeats of both. Map them when a file shows otherwise.
|
|
@@ -24,17 +29,31 @@ import { type DocJSON, isSafeLink } from "../schema.js";
|
|
|
24
29
|
import {
|
|
25
30
|
type AbstractNumberingJSON,
|
|
26
31
|
type CommentJSON,
|
|
32
|
+
type ControlJSON,
|
|
33
|
+
migrateToV4,
|
|
34
|
+
migrateToV5,
|
|
27
35
|
type NumberingInstanceJSON,
|
|
28
36
|
type NumberingLevelJSON,
|
|
29
37
|
type ParagraphPropertiesJSON,
|
|
30
38
|
parseContent,
|
|
31
39
|
parseDocument,
|
|
40
|
+
SCHEMA_VERSION,
|
|
41
|
+
type StoryJSON,
|
|
32
42
|
type StyleJSON,
|
|
33
43
|
} from "../v3/document.js";
|
|
34
44
|
import { migrateToV3 } from "../v3/migrate.js";
|
|
35
45
|
import { Resolver } from "../v3/resolve.js";
|
|
46
|
+
import { controlsIn, copiedControl, freshControlId } from "./controls.js";
|
|
47
|
+
import { noteIds, noteKindOf } from "./notes.js";
|
|
36
48
|
import type { EditorState } from "./state.js";
|
|
37
|
-
import {
|
|
49
|
+
import {
|
|
50
|
+
otherTrees,
|
|
51
|
+
type Story,
|
|
52
|
+
storyBody,
|
|
53
|
+
type WordContext,
|
|
54
|
+
type WordEnvelope,
|
|
55
|
+
wordContext,
|
|
56
|
+
} from "./word.js";
|
|
38
57
|
import { builtinStyle, levels } from "./wordformat.js";
|
|
39
58
|
|
|
40
59
|
/** What a v3 copy carries beside its content: the definitions the content references. */
|
|
@@ -44,6 +63,16 @@ export interface WordDefinitions {
|
|
|
44
63
|
styles: StyleJSON[];
|
|
45
64
|
numbering: { abstracts: AbstractNumberingJSON[]; instances: NumberingInstanceJSON[] };
|
|
46
65
|
comments: CommentJSON[];
|
|
66
|
+
/** The notes the content refers to, by story id. */
|
|
67
|
+
notes?: Record<string, StoryJSON>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** What a paste brings: the content, the envelope it needs, and new notes. */
|
|
71
|
+
export interface FittedPaste {
|
|
72
|
+
doc: Node;
|
|
73
|
+
envelope: WordEnvelope;
|
|
74
|
+
/** The stories of the notes the references in `doc` now name, to add with it. */
|
|
75
|
+
notes?: { id: string; story: Story }[];
|
|
47
76
|
}
|
|
48
77
|
|
|
49
78
|
type Json = Record<string, unknown>;
|
|
@@ -66,7 +95,8 @@ type Properties = Pick<ParagraphPropertiesJSON, "style" | "numbering" | "markFor
|
|
|
66
95
|
* comments; with the document's id.
|
|
67
96
|
*/
|
|
68
97
|
export function copiedDefinitions(context: WordContext, content: Node): WordDefinitions {
|
|
69
|
-
const { styles, numbering, comments } = context.envelope;
|
|
98
|
+
const { styles, numbering, comments, stories } = context.envelope;
|
|
99
|
+
const noteStories = new Set<string>();
|
|
70
100
|
const styleIds = new Set<string>();
|
|
71
101
|
const numberingIds = new Set<number>();
|
|
72
102
|
const commentIds = new Set<number>();
|
|
@@ -88,7 +118,11 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
|
|
|
88
118
|
properties(attrs);
|
|
89
119
|
properties(attrs.formatChange?.previous);
|
|
90
120
|
}
|
|
121
|
+
// A table's style, and a row's exceptions' style.
|
|
122
|
+
if (node.type === "table") properties(attrs);
|
|
123
|
+
if (node.type === "tableRow") properties((attrs as { exceptions?: Properties }).exceptions);
|
|
91
124
|
if (COMMENT_ANCHORS.has(node.type)) commentIds.add(attrs.id as number);
|
|
125
|
+
if (node.type === "noteReference") noteStories.add(String(node.attrs?.story));
|
|
92
126
|
marks(node.marks);
|
|
93
127
|
};
|
|
94
128
|
visit([content.toJSON()], references);
|
|
@@ -97,6 +131,16 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
|
|
|
97
131
|
copied.flatMap((c) => c.content),
|
|
98
132
|
references,
|
|
99
133
|
);
|
|
134
|
+
const notes = Object.fromEntries(
|
|
135
|
+
[...noteStories].flatMap((id) => {
|
|
136
|
+
const story = stories && Object.hasOwn(stories, id) ? stories[id] : undefined;
|
|
137
|
+
return story ? [[id, story] as const] : [];
|
|
138
|
+
}),
|
|
139
|
+
);
|
|
140
|
+
visit(
|
|
141
|
+
Object.values(notes).map((note) => note.content),
|
|
142
|
+
references,
|
|
143
|
+
);
|
|
100
144
|
const instances = () => numbering.instances.filter((n) => numberingIds.has(n.id));
|
|
101
145
|
const abstracts = () => {
|
|
102
146
|
const ids = new Set(instances().map((n) => n.abstract));
|
|
@@ -127,6 +171,7 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
|
|
|
127
171
|
styles: styles.filter((s) => styleIds.has(s.id)),
|
|
128
172
|
numbering: { abstracts: abstracts(), instances: instances() },
|
|
129
173
|
comments: copied,
|
|
174
|
+
...(Object.keys(notes).length ? { notes } : {}),
|
|
130
175
|
};
|
|
131
176
|
}
|
|
132
177
|
|
|
@@ -140,11 +185,15 @@ export function fitPastedV2(
|
|
|
140
185
|
state: EditorState,
|
|
141
186
|
content: Node,
|
|
142
187
|
): { doc: Node; envelope: WordEnvelope } {
|
|
143
|
-
const migrated =
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
188
|
+
const migrated = migrateToV5(
|
|
189
|
+
migrateToV4(
|
|
190
|
+
migrateToV3({
|
|
191
|
+
schemaVersion: 2,
|
|
192
|
+
settings: DEFAULT_SETTINGS,
|
|
193
|
+
content: content.toJSON() as DocJSON,
|
|
194
|
+
}),
|
|
195
|
+
),
|
|
196
|
+
);
|
|
148
197
|
const definitions: WordDefinitions = {
|
|
149
198
|
styles: migrated.styles.map((style) => {
|
|
150
199
|
// Based on the paste's Normal, which is the target's by name.
|
|
@@ -203,28 +252,72 @@ function safeLinks(json: NodeJSON): NodeJSON {
|
|
|
203
252
|
* text, older copies). Returns the content to insert and the envelope with what it added (the
|
|
204
253
|
* same object when nothing was added). Throws FoliDocumentError on invalid input.
|
|
205
254
|
*/
|
|
206
|
-
export function fitPaste(
|
|
207
|
-
state: EditorState,
|
|
208
|
-
content: unknown,
|
|
209
|
-
definitions?: unknown,
|
|
210
|
-
): { doc: Node; envelope: WordEnvelope } {
|
|
255
|
+
export function fitPaste(state: EditorState, content: unknown, definitions?: unknown): FittedPaste {
|
|
211
256
|
const context = wordContext(state) as WordContext;
|
|
212
257
|
const target = context.envelope;
|
|
213
258
|
// Taken as this document's own, but it cannot be told to be: no unsafe link comes in.
|
|
214
259
|
if (definitions === undefined || definitions === null)
|
|
215
|
-
return fit(state, target, safeLinks(parseContent(content).toJSON()), true);
|
|
216
|
-
const { document, styles, numbering, comments } = definitions as Partial<WordDefinitions>;
|
|
217
|
-
// Validated as a document's envelope: types, ranges, no style based on itself
|
|
260
|
+
return fit(state, target, safeLinks(parseContent(content, "content", true).toJSON()), true);
|
|
261
|
+
const { document, styles, numbering, comments, notes } = definitions as Partial<WordDefinitions>;
|
|
262
|
+
// Validated as a document's envelope: types, ranges, no style based on itself; the content
|
|
263
|
+
// as content to paste (a table may end it).
|
|
218
264
|
const checked = parseDocument({
|
|
219
|
-
schemaVersion:
|
|
265
|
+
schemaVersion: SCHEMA_VERSION,
|
|
220
266
|
section: target.section,
|
|
221
267
|
defaults: {},
|
|
222
268
|
styles,
|
|
223
269
|
numbering,
|
|
224
270
|
comments,
|
|
225
|
-
|
|
271
|
+
...(notes ? { stories: notes } : {}),
|
|
272
|
+
content: { type: "doc", content: [{ type: "paragraph" }] },
|
|
226
273
|
});
|
|
227
|
-
|
|
274
|
+
const json = parseContent(content, "content", true).toJSON();
|
|
275
|
+
const source: WordDefinitions = {
|
|
276
|
+
styles: checked.styles,
|
|
277
|
+
numbering: checked.numbering,
|
|
278
|
+
comments: checked.comments,
|
|
279
|
+
...(checked.stories ? { notes: checked.stories } : {}),
|
|
280
|
+
};
|
|
281
|
+
return fit(state, source, json, document === context.id);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Adds to `out` the field and inline control markers of `nodes` without partners (ADR-0007): a
|
|
286
|
+
* field that lost its end loses its start and code too, so the code never becomes text. What they
|
|
287
|
+
* held stays. `open` carries what is open from one node to the next.
|
|
288
|
+
*/
|
|
289
|
+
function unpairedMarkers(
|
|
290
|
+
nodes: readonly NodeJSON[],
|
|
291
|
+
out: Set<NodeJSON>,
|
|
292
|
+
open: {
|
|
293
|
+
fields: { parts: NodeJSON[]; separated: boolean }[];
|
|
294
|
+
controls: Map<unknown, NodeJSON[]>;
|
|
295
|
+
} = { fields: [], controls: new Map() },
|
|
296
|
+
top = true,
|
|
297
|
+
): void {
|
|
298
|
+
for (const node of nodes) {
|
|
299
|
+
const field = open.fields[open.fields.length - 1];
|
|
300
|
+
const id = node.attrs?.id;
|
|
301
|
+
if (node.type === "fieldStart") open.fields.push({ parts: [node], separated: false });
|
|
302
|
+
else if (node.type === "fieldCode") {
|
|
303
|
+
if (field && !field.separated) field.parts.push(node);
|
|
304
|
+
else out.add(node);
|
|
305
|
+
} else if (node.type === "fieldSeparator") {
|
|
306
|
+
if (field && !field.separated) {
|
|
307
|
+
field.parts.push(node);
|
|
308
|
+
field.separated = true;
|
|
309
|
+
} else out.add(node);
|
|
310
|
+
} else if (node.type === "fieldEnd") {
|
|
311
|
+
if (!open.fields.pop()) out.add(node);
|
|
312
|
+
} else if (node.type === "controlStart")
|
|
313
|
+
open.controls.set(id, [...(open.controls.get(id) ?? []), node]);
|
|
314
|
+
else if (node.type === "controlEnd") {
|
|
315
|
+
if (!open.controls.get(id)?.pop()) out.add(node);
|
|
316
|
+
} else if (node.content) unpairedMarkers(node.content, out, open, false);
|
|
317
|
+
}
|
|
318
|
+
if (!top) return;
|
|
319
|
+
for (const { parts } of open.fields) for (const part of parts) out.add(part);
|
|
320
|
+
for (const starts of open.controls.values()) for (const start of starts) out.add(start);
|
|
228
321
|
}
|
|
229
322
|
|
|
230
323
|
/**
|
|
@@ -236,7 +329,7 @@ function fit(
|
|
|
236
329
|
source: WordDefinitions,
|
|
237
330
|
json: NodeJSON,
|
|
238
331
|
same: boolean,
|
|
239
|
-
):
|
|
332
|
+
): FittedPaste {
|
|
240
333
|
const target = (wordContext(state) as WordContext).envelope;
|
|
241
334
|
let styles = target.styles;
|
|
242
335
|
let { abstracts, instances } = target.numbering;
|
|
@@ -352,15 +445,21 @@ function fit(
|
|
|
352
445
|
for (const c of target.comments) nextId.comment = Math.max(nextId.comment, c.id + 1);
|
|
353
446
|
const names = new Set<string>();
|
|
354
447
|
const { from, to } = state.selection;
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
448
|
+
/** What every tree says about ids and names in use (ids are allocated across them). */
|
|
449
|
+
const scan = (tree: Node, inSelection: (pos: number) => boolean) =>
|
|
450
|
+
tree.descendants((node, pos) => {
|
|
451
|
+
const id = (node.attrs.id as number) + 1;
|
|
452
|
+
if (COMMENT_ANCHORS.has(node.type.name)) nextId.comment = Math.max(nextId.comment, id);
|
|
453
|
+
// A bookmark in the selection goes with it.
|
|
454
|
+
if (node.type.name === "bookmarkStart" && !inSelection(pos))
|
|
455
|
+
names.add(String(node.attrs.name));
|
|
456
|
+
if (node.type.name.startsWith("bookmark")) nextId.bookmark = Math.max(nextId.bookmark, id);
|
|
457
|
+
return node.type.name === "doc" || node.isBlock;
|
|
458
|
+
});
|
|
459
|
+
scan(state.doc, (pos) => pos >= from && pos < to);
|
|
460
|
+
for (const tree of otherTrees(state)) scan(tree, () => false);
|
|
461
|
+
// A story holds no comments, note references or section breaks (ADR-0007).
|
|
462
|
+
const inStory = storyBody(state) !== null;
|
|
364
463
|
const fresh = (kind: keyof typeof nextId, map: Map<number, number>, id: number) => {
|
|
365
464
|
let next = map.get(id);
|
|
366
465
|
if (next === undefined) {
|
|
@@ -374,10 +473,63 @@ function fit(
|
|
|
374
473
|
const bookmarkIds = new Map<number, number>();
|
|
375
474
|
const leftOut = new Set<number>();
|
|
376
475
|
|
|
476
|
+
// Controls that stay in the document: what is pasted of them is a copy, under a new id.
|
|
477
|
+
let controls = target.controls;
|
|
478
|
+
const staying = new Set<string>();
|
|
479
|
+
for (const range of controlsIn(state.doc))
|
|
480
|
+
if (range.from < from || range.to > to) staying.add(range.id);
|
|
481
|
+
for (const tree of otherTrees(state)) for (const range of controlsIn(tree)) staying.add(range.id);
|
|
482
|
+
const copiedControls = new Map<string, string>();
|
|
483
|
+
const mapControl = (id: unknown): string | null => {
|
|
484
|
+
if (!same || typeof id !== "string" || !Object.hasOwn(target.controls ?? {}, id)) return null;
|
|
485
|
+
if (!staying.has(id)) return id;
|
|
486
|
+
let copy = copiedControls.get(id);
|
|
487
|
+
if (!copy) {
|
|
488
|
+
copy = freshControlId(controls);
|
|
489
|
+
copiedControls.set(id, copy);
|
|
490
|
+
controls = { ...controls, [copy]: copiedControl(target.controls?.[id] as ControlJSON) };
|
|
491
|
+
}
|
|
492
|
+
return copy;
|
|
493
|
+
};
|
|
494
|
+
// A note has one reference: one still in the document keeps it (cut and paste moves it).
|
|
495
|
+
const referredTo = new Set<string>();
|
|
496
|
+
state.doc.descendants((node, pos) => {
|
|
497
|
+
if (node.type.name === "noteReference" && (pos < from || pos >= to))
|
|
498
|
+
referredTo.add(String(node.attrs.story));
|
|
499
|
+
});
|
|
500
|
+
const noteAvailable = (story: string) =>
|
|
501
|
+
same && Object.hasOwn(target.stories ?? {}, story) && !referredTo.has(story);
|
|
502
|
+
const carried = (story: string) =>
|
|
503
|
+
source.notes && Object.hasOwn(source.notes, story) ? source.notes[story] : undefined;
|
|
504
|
+
const newNoteId = noteIds(state);
|
|
505
|
+
/** The new note of each story a pasted reference was copied with. */
|
|
506
|
+
const pastedNotes = new Map<string, string>();
|
|
507
|
+
|
|
508
|
+
// Half a field or control from elsewhere: its markers go, what it held stays.
|
|
509
|
+
const unpaired = new Set<NodeJSON>();
|
|
510
|
+
unpairedMarkers([json], unpaired);
|
|
511
|
+
for (const comment of source.comments) unpairedMarkers(comment.content, unpaired);
|
|
512
|
+
for (const note of Object.values(source.notes ?? {})) unpairedMarkers([note.content], unpaired);
|
|
513
|
+
|
|
377
514
|
const mapNode = (node: NodeJSON): NodeJSON | null => {
|
|
515
|
+
if (unpaired.has(node)) return null;
|
|
378
516
|
const attrs = node.attrs as Json | undefined;
|
|
517
|
+
if (inStory && (COMMENT_ANCHORS.has(node.type) || node.type === "noteReference")) return null;
|
|
379
518
|
let next: Json | undefined = attrs;
|
|
380
|
-
if (node.type === "
|
|
519
|
+
if (node.type === "noteReference") {
|
|
520
|
+
const story = String(attrs?.story);
|
|
521
|
+
const kind = noteKindOf(story);
|
|
522
|
+
if (kind && carried(story)) {
|
|
523
|
+
const id = pastedNotes.get(story) ?? newNoteId(kind);
|
|
524
|
+
pastedNotes.set(story, id);
|
|
525
|
+
next = { ...attrs, story: id };
|
|
526
|
+
} else if (!noteAvailable(story)) return null;
|
|
527
|
+
} else if (node.type === "controlStart" || node.type === "controlEnd") {
|
|
528
|
+
// From another document their definitions are not here: the content comes without them.
|
|
529
|
+
const id = mapControl(attrs?.id);
|
|
530
|
+
if (!id) return null;
|
|
531
|
+
next = { ...attrs, id };
|
|
532
|
+
} else if (node.type === "paragraph" && attrs) {
|
|
381
533
|
next = paragraphProperties(attrs as ParagraphPropertiesJSON) as Json;
|
|
382
534
|
const change = attrs.formatChange as { previous: ParagraphPropertiesJSON } | null | undefined;
|
|
383
535
|
if (change) next.formatChange = { ...change, previous: paragraphProperties(change.previous) };
|
|
@@ -389,6 +541,14 @@ function fit(
|
|
|
389
541
|
if (Object.keys(rest).length) next.attributes = rest;
|
|
390
542
|
else delete next.attributes;
|
|
391
543
|
}
|
|
544
|
+
} else if (node.type === "table" && attrs?.style) {
|
|
545
|
+
next = { ...attrs, style: mapStyle(String(attrs.style)) };
|
|
546
|
+
} else if (node.type === "tableRow" && attrs) {
|
|
547
|
+
const exceptions = attrs.exceptions as Json | null | undefined;
|
|
548
|
+
// A copied row is a new row: not its identity (`w14:paraId`…).
|
|
549
|
+
next = { ...attrs, attributes: null };
|
|
550
|
+
if (exceptions?.style)
|
|
551
|
+
next.exceptions = { ...exceptions, style: mapStyle(String(exceptions.style)) };
|
|
392
552
|
} else if (COMMENT_ANCHORS.has(node.type)) {
|
|
393
553
|
const id = attrs?.id as number;
|
|
394
554
|
if (!defined.has(id)) return null;
|
|
@@ -400,6 +560,16 @@ function fit(
|
|
|
400
560
|
if (node.type === "bookmarkStart") names.add(String(attrs?.name));
|
|
401
561
|
next = { ...attrs, id: fresh("bookmark", bookmarkIds, id) };
|
|
402
562
|
}
|
|
563
|
+
if (inStory && next?.section) {
|
|
564
|
+
next = { ...next };
|
|
565
|
+
delete next.section;
|
|
566
|
+
}
|
|
567
|
+
if (next?.controls) {
|
|
568
|
+
const ids = (next.controls as string[]).flatMap((id) => mapControl(id) ?? []);
|
|
569
|
+
next = { ...next };
|
|
570
|
+
if (ids.length) next.controls = ids;
|
|
571
|
+
else delete next.controls;
|
|
572
|
+
}
|
|
403
573
|
const marks = node.marks && runMarks(node.marks);
|
|
404
574
|
const children = node.content
|
|
405
575
|
?.map(mapNode)
|
|
@@ -412,7 +582,28 @@ function fit(
|
|
|
412
582
|
} as NodeJSON;
|
|
413
583
|
};
|
|
414
584
|
|
|
415
|
-
|
|
585
|
+
// Pasted into a block control, the new paragraphs are in it as the one at the caret is.
|
|
586
|
+
const around = (
|
|
587
|
+
from === to || state.selection.$from.parent === state.selection.$to.parent
|
|
588
|
+
? state.selection.$from.parent.attrs.controls
|
|
589
|
+
: null
|
|
590
|
+
) as string[] | null | undefined;
|
|
591
|
+
const mapped = mapNode(json) as NodeJSON;
|
|
592
|
+
const inPlace = around?.length
|
|
593
|
+
? {
|
|
594
|
+
...mapped,
|
|
595
|
+
content: mapped.content?.map((block) => {
|
|
596
|
+
const attrs = block.attrs as Json | undefined;
|
|
597
|
+
const own = (attrs?.controls as string[] | undefined) ?? [];
|
|
598
|
+
return { ...block, attrs: { ...attrs, controls: [...around, ...own] } };
|
|
599
|
+
}),
|
|
600
|
+
}
|
|
601
|
+
: mapped;
|
|
602
|
+
const doc = parseContent(inPlace, "content", true);
|
|
603
|
+
const notes = [...pastedNotes].map(([from, id]) => {
|
|
604
|
+
const note = carried(from) as StoryJSON;
|
|
605
|
+
return { id, story: { kind: note.kind, doc: parseContent(mapNode(note.content)) } };
|
|
606
|
+
});
|
|
416
607
|
let comments = target.comments;
|
|
417
608
|
const copiedComments = source.comments.filter((c) => commentIds.has(c.id));
|
|
418
609
|
if (copiedComments.length) {
|
|
@@ -435,11 +626,19 @@ function fit(
|
|
|
435
626
|
styles !== target.styles ||
|
|
436
627
|
abstracts !== target.numbering.abstracts ||
|
|
437
628
|
instances !== target.numbering.instances ||
|
|
438
|
-
comments !== target.comments
|
|
629
|
+
comments !== target.comments ||
|
|
630
|
+
controls !== target.controls;
|
|
439
631
|
return {
|
|
440
632
|
doc,
|
|
441
633
|
envelope: changed
|
|
442
|
-
? {
|
|
634
|
+
? {
|
|
635
|
+
...target,
|
|
636
|
+
styles,
|
|
637
|
+
numbering: { abstracts, instances },
|
|
638
|
+
comments,
|
|
639
|
+
...(controls ? { controls } : {}),
|
|
640
|
+
}
|
|
443
641
|
: target,
|
|
642
|
+
...(notes.length ? { notes } : {}),
|
|
444
643
|
};
|
|
445
644
|
}
|
package/src/state/wordformat.ts
CHANGED
|
@@ -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 }
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
|
|
@@ -111,7 +111,7 @@ const builtinName = (id: string) =>
|
|
|
111
111
|
|
|
112
112
|
/**
|
|
113
113
|
* Word's definition of built-in style `name` ("heading 1"–"heading 9", "List Paragraph",
|
|
114
|
-
* "Hyperlink"), as Word adds it to a document the first time it is used (the Word 2013–2019
|
|
114
|
+
* "Hyperlink", "Placeholder Text"), as Word adds it to a document the first time it is used (the Word 2013–2019
|
|
115
115
|
* template's values; not checked against newer Word).
|
|
116
116
|
*/
|
|
117
117
|
export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | null {
|
|
@@ -158,6 +158,20 @@ export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | nu
|
|
|
158
158
|
],
|
|
159
159
|
};
|
|
160
160
|
}
|
|
161
|
+
const contents = /^toc ([1-9])$/.exec(name)?.[1];
|
|
162
|
+
if (contents) {
|
|
163
|
+
// Word indents each level by the default font's size (11 pt: 220 twips).
|
|
164
|
+
const size = context.envelope.defaults.run?.find((m) => m.type === "fontSize")?.attrs?.value;
|
|
165
|
+
const indent = (Number(contents) - 1) * (typeof size === "number" ? size * 10 : 220);
|
|
166
|
+
return {
|
|
167
|
+
id: `TOC${contents}`,
|
|
168
|
+
type: "paragraph",
|
|
169
|
+
name,
|
|
170
|
+
...based,
|
|
171
|
+
...(normal ? { next: normal } : {}),
|
|
172
|
+
paragraph: { spacing: { after: 100 }, ...(indent ? { indent: { left: indent } } : {}) },
|
|
173
|
+
};
|
|
174
|
+
}
|
|
161
175
|
if (name === "List Paragraph")
|
|
162
176
|
return {
|
|
163
177
|
id: "ListParagraph",
|
|
@@ -166,6 +180,74 @@ export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | nu
|
|
|
166
180
|
...based,
|
|
167
181
|
paragraph: { indent: { left: 720 }, contextualSpacing: true },
|
|
168
182
|
};
|
|
183
|
+
if (name === "Table Grid") {
|
|
184
|
+
// Word's: single borders all round and between cells, no space after paragraphs.
|
|
185
|
+
const line = { style: "single", size: 4, space: 0, color: "auto" };
|
|
186
|
+
const plain = styleNamed(context, "Normal Table", "table");
|
|
187
|
+
return {
|
|
188
|
+
id: "TableGrid",
|
|
189
|
+
type: "table",
|
|
190
|
+
name,
|
|
191
|
+
...(plain ? { basedOn: plain } : {}),
|
|
192
|
+
paragraph: { spacing: { after: 0, line: 240, lineRule: "auto" } },
|
|
193
|
+
table: {
|
|
194
|
+
borders: {
|
|
195
|
+
top: line,
|
|
196
|
+
left: line,
|
|
197
|
+
bottom: line,
|
|
198
|
+
right: line,
|
|
199
|
+
insideH: line,
|
|
200
|
+
insideV: line,
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
const note = /^(foot|end)note (text|reference)$/.exec(name);
|
|
206
|
+
const textChar = /^(Foot|End)note Text Char$/.exec(name);
|
|
207
|
+
if (note || textChar) {
|
|
208
|
+
// Word's: 10 pt text without space after, and a superscript reference.
|
|
209
|
+
const font = defaultParagraphStyleOf(context, "character");
|
|
210
|
+
const fontBased = font ? { basedOn: font } : {};
|
|
211
|
+
const size = { type: "fontSize", attrs: { value: 20, complexScript: 20 } };
|
|
212
|
+
const stem =
|
|
213
|
+
(note?.[1] ?? textChar?.[1] ?? "").toLowerCase() === "foot" ? "Footnote" : "Endnote";
|
|
214
|
+
if (textChar)
|
|
215
|
+
return {
|
|
216
|
+
id: `${stem}TextChar`,
|
|
217
|
+
type: "character",
|
|
218
|
+
name,
|
|
219
|
+
...fontBased,
|
|
220
|
+
link: `${stem}Text`,
|
|
221
|
+
run: [size],
|
|
222
|
+
};
|
|
223
|
+
if (note?.[2] === "reference")
|
|
224
|
+
return {
|
|
225
|
+
id: `${stem}Reference`,
|
|
226
|
+
type: "character",
|
|
227
|
+
name,
|
|
228
|
+
...fontBased,
|
|
229
|
+
run: [{ type: "verticalAlign", attrs: { value: "superscript" } }],
|
|
230
|
+
};
|
|
231
|
+
return {
|
|
232
|
+
id: `${stem}Text`,
|
|
233
|
+
type: "paragraph",
|
|
234
|
+
name,
|
|
235
|
+
...based,
|
|
236
|
+
link: `${stem}TextChar`,
|
|
237
|
+
paragraph: { spacing: { after: 0, line: 240, lineRule: "auto" } },
|
|
238
|
+
run: [size],
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
if (name === "Placeholder Text") {
|
|
242
|
+
const font = defaultParagraphStyleOf(context, "character");
|
|
243
|
+
return {
|
|
244
|
+
id: "PlaceholderText",
|
|
245
|
+
type: "character",
|
|
246
|
+
name,
|
|
247
|
+
...(font ? { basedOn: font } : {}),
|
|
248
|
+
run: [{ type: "color", attrs: { value: "666666" } }],
|
|
249
|
+
};
|
|
250
|
+
}
|
|
169
251
|
if (name === "Hyperlink") {
|
|
170
252
|
const font = defaultParagraphStyleOf(context, "character");
|
|
171
253
|
return {
|
|
@@ -186,7 +268,7 @@ export function builtinStyle(context: HasEnvelope, name: string): StyleJSON | nu
|
|
|
186
268
|
* The id of the document's style named `name`, adding Word's built-in definition when the
|
|
187
269
|
* document does not have it yet (with the envelope that has it; unchanged if it was there).
|
|
188
270
|
*/
|
|
189
|
-
function ensureStyle(
|
|
271
|
+
export function ensureStyle(
|
|
190
272
|
context: HasEnvelope,
|
|
191
273
|
name: string,
|
|
192
274
|
type: StyleJSON["type"] = "paragraph",
|
|
@@ -285,12 +367,12 @@ function applyStyle(
|
|
|
285
367
|
function runsIn(state: EditorState, type: MarkType) {
|
|
286
368
|
const context = wordContext(state);
|
|
287
369
|
const found: { node: Node; pos: number; parent: Node }[] = [];
|
|
288
|
-
const { from, to }
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
370
|
+
for (const { from, to } of state.selection.ranges)
|
|
371
|
+
state.doc.nodesBetween(from, to, (node, pos, parent) => {
|
|
372
|
+
if (!node.isInline || !parent?.type.allowsMarkType(type)) return undefined;
|
|
373
|
+
if (!isMarker(context, node)) found.push({ node, pos, parent });
|
|
374
|
+
return false;
|
|
375
|
+
});
|
|
294
376
|
return found;
|
|
295
377
|
}
|
|
296
378
|
|