@folijs/core 0.5.0 → 1.0.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 +187 -0
- package/README.md +1 -1
- package/dist/document.d.ts +0 -7
- package/dist/document.d.ts.map +1 -1
- package/dist/document.js +5 -1
- package/dist/document.js.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -4
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +5 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +7 -0
- package/dist/internal.js.map +1 -0
- package/dist/model/attrs.d.ts +0 -10
- package/dist/model/attrs.d.ts.map +1 -1
- package/dist/model/attrs.js +10 -2
- package/dist/model/attrs.js.map +1 -1
- package/dist/model/content.d.ts +1 -1
- package/dist/model/content.d.ts.map +1 -1
- package/dist/model/content.js +1 -1
- package/dist/model/content.js.map +1 -1
- package/dist/model/errors.d.ts +0 -2
- package/dist/model/errors.d.ts.map +1 -1
- package/dist/model/errors.js +4 -1
- package/dist/model/errors.js.map +1 -1
- package/dist/model/fragment.d.ts +1 -1
- package/dist/model/fragment.d.ts.map +1 -1
- package/dist/model/fragment.js +3 -1
- package/dist/model/fragment.js.map +1 -1
- package/dist/model/mark.d.ts +1 -1
- package/dist/model/mark.d.ts.map +1 -1
- package/dist/model/mark.js +1 -1
- package/dist/model/mark.js.map +1 -1
- package/dist/model/node.d.ts +4 -4
- package/dist/model/node.d.ts.map +1 -1
- package/dist/model/node.js +8 -4
- package/dist/model/node.js.map +1 -1
- package/dist/model/schema.d.ts +8 -8
- package/dist/model/schema.d.ts.map +1 -1
- package/dist/model/schema.js +4 -3
- package/dist/model/schema.js.map +1 -1
- package/dist/schema.d.ts +0 -11
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +2 -0
- package/dist/schema.js.map +1 -1
- package/dist/state/attempt.d.ts +1 -3
- package/dist/state/attempt.d.ts.map +1 -1
- package/dist/state/commands.d.ts +2 -0
- package/dist/state/commands.d.ts.map +1 -1
- package/dist/state/commands.js +38 -6
- package/dist/state/commands.js.map +1 -1
- package/dist/state/comments.d.ts +1 -7
- package/dist/state/comments.d.ts.map +1 -1
- package/dist/state/comments.js +13 -3
- package/dist/state/comments.js.map +1 -1
- package/dist/state/controls.d.ts +110 -0
- package/dist/state/controls.d.ts.map +1 -0
- package/dist/state/controls.js +1473 -0
- package/dist/state/controls.js.map +1 -0
- package/dist/state/editor.d.ts +188 -68
- package/dist/state/editor.d.ts.map +1 -1
- package/dist/state/editor.js +265 -79
- package/dist/state/editor.js.map +1 -1
- package/dist/state/fields.d.ts +68 -0
- package/dist/state/fields.d.ts.map +1 -0
- package/dist/state/fields.js +410 -0
- package/dist/state/fields.js.map +1 -0
- package/dist/state/fieldupdate.d.ts +132 -0
- package/dist/state/fieldupdate.d.ts.map +1 -0
- package/dist/state/fieldupdate.js +1575 -0
- package/dist/state/fieldupdate.js.map +1 -0
- package/dist/state/find.d.ts +66 -0
- package/dist/state/find.d.ts.map +1 -0
- package/dist/state/find.js +405 -0
- package/dist/state/find.js.map +1 -0
- package/dist/state/history.d.ts +2 -0
- package/dist/state/history.d.ts.map +1 -1
- package/dist/state/history.js +44 -7
- package/dist/state/history.js.map +1 -1
- package/dist/state/index.d.ts +13 -6
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +12 -5
- package/dist/state/index.js.map +1 -1
- package/dist/state/lists.d.ts +3 -3
- package/dist/state/lists.d.ts.map +1 -1
- package/dist/state/lists.js.map +1 -1
- package/dist/state/mark.d.ts +1 -17
- package/dist/state/mark.d.ts.map +1 -1
- package/dist/state/mark.js +10 -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 +84 -0
- package/dist/state/notes.d.ts.map +1 -0
- package/dist/state/notes.js +451 -0
- package/dist/state/notes.js.map +1 -0
- package/dist/state/review.d.ts +8 -4
- package/dist/state/review.d.ts.map +1 -1
- package/dist/state/review.js +134 -18
- package/dist/state/review.js.map +1 -1
- package/dist/state/sections.d.ts +91 -0
- package/dist/state/sections.d.ts.map +1 -0
- package/dist/state/sections.js +479 -0
- package/dist/state/sections.js.map +1 -0
- package/dist/state/state.d.ts +13 -7
- package/dist/state/state.d.ts.map +1 -1
- package/dist/state/state.js +9 -2
- package/dist/state/state.js.map +1 -1
- package/dist/state/stories.d.ts +2 -0
- package/dist/state/stories.d.ts.map +1 -0
- package/dist/state/stories.js +259 -0
- package/dist/state/stories.js.map +1 -0
- package/dist/state/tables.d.ts +1 -36
- package/dist/state/tables.d.ts.map +1 -1
- package/dist/state/tables.js +25 -6
- package/dist/state/tables.js.map +1 -1
- package/dist/state/track.d.ts +1 -6
- package/dist/state/track.d.ts.map +1 -1
- package/dist/state/track.js +65 -9
- package/dist/state/track.js.map +1 -1
- package/dist/state/word.d.ts +47 -55
- package/dist/state/word.d.ts.map +1 -1
- package/dist/state/word.js +306 -19
- package/dist/state/word.js.map +1 -1
- package/dist/state/wordclip.d.ts +17 -6
- package/dist/state/wordclip.d.ts.map +1 -1
- package/dist/state/wordclip.js +254 -17
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +1 -86
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +114 -12
- package/dist/state/wordformat.js.map +1 -1
- package/dist/transform/stepmap.d.ts +0 -2
- package/dist/transform/stepmap.d.ts.map +1 -1
- package/dist/v3/check.d.ts +0 -25
- package/dist/v3/check.d.ts.map +1 -1
- package/dist/v3/check.js +35 -7
- package/dist/v3/check.js.map +1 -1
- package/dist/v3/document.d.ts +135 -10
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +192 -38
- package/dist/v3/document.js.map +1 -1
- package/dist/v3/extensions.d.ts +43 -0
- package/dist/v3/extensions.d.ts.map +1 -0
- package/dist/v3/extensions.js +154 -0
- package/dist/v3/extensions.js.map +1 -0
- package/dist/v3/index.d.ts +3 -2
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +3 -2
- package/dist/v3/index.js.map +1 -1
- package/dist/v3/resolve.d.ts +9 -3
- package/dist/v3/resolve.d.ts.map +1 -1
- package/dist/v3/resolve.js +28 -4
- package/dist/v3/resolve.js.map +1 -1
- package/dist/v3/schema.d.ts +5 -16
- package/dist/v3/schema.d.ts.map +1 -1
- package/dist/v3/schema.js +167 -7
- package/dist/v3/schema.js.map +1 -1
- package/package.json +5 -1
- package/src/document.ts +5 -1
- package/src/index.ts +330 -4
- package/src/internal.ts +50 -0
- package/src/model/attrs.ts +10 -2
- package/src/model/content.ts +1 -1
- package/src/model/errors.ts +4 -1
- package/src/model/fragment.ts +2 -1
- package/src/model/mark.ts +1 -1
- package/src/model/node.ts +6 -4
- package/src/model/schema.ts +9 -3
- package/src/schema.ts +2 -0
- package/src/state/commands.ts +41 -4
- package/src/state/comments.ts +12 -3
- package/src/state/controls.ts +1612 -0
- package/src/state/editor.ts +508 -143
- package/src/state/fields.ts +444 -0
- package/src/state/fieldupdate.ts +1771 -0
- package/src/state/find.ts +518 -0
- package/src/state/history.ts +53 -7
- package/src/state/index.ts +125 -1
- package/src/state/lists.ts +2 -2
- package/src/state/mark.ts +10 -1
- package/src/state/navigation.ts +305 -0
- package/src/state/notes.ts +529 -0
- package/src/state/numeric-pictures.word.json +172 -0
- package/src/state/review.ts +172 -19
- package/src/state/sections.ts +578 -0
- package/src/state/state.ts +21 -3
- package/src/state/stories.ts +312 -0
- package/src/state/tables.ts +29 -7
- package/src/state/track.ts +72 -10
- package/src/state/word.ts +360 -24
- package/src/state/wordclip.ts +282 -32
- package/src/state/wordformat.ts +116 -12
- package/src/v3/check.ts +35 -7
- package/src/v3/document.ts +358 -50
- package/src/v3/extensions.ts +201 -0
- package/src/v3/index.ts +16 -0
- package/src/v3/resolve.ts +38 -9
- package/src/v3/schema.ts +174 -7
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,19 +29,32 @@ import { type DocJSON, isSafeLink } from "../schema.js";
|
|
|
24
29
|
import {
|
|
25
30
|
type AbstractNumberingJSON,
|
|
26
31
|
type CommentJSON,
|
|
32
|
+
type ControlJSON,
|
|
27
33
|
migrateToV4,
|
|
34
|
+
migrateToV5,
|
|
28
35
|
type NumberingInstanceJSON,
|
|
29
36
|
type NumberingLevelJSON,
|
|
30
37
|
type ParagraphPropertiesJSON,
|
|
31
38
|
parseContent,
|
|
32
39
|
parseDocument,
|
|
40
|
+
type ResourceJSON,
|
|
33
41
|
SCHEMA_VERSION,
|
|
42
|
+
type StoryJSON,
|
|
34
43
|
type StyleJSON,
|
|
35
44
|
} from "../v3/document.js";
|
|
36
45
|
import { migrateToV3 } from "../v3/migrate.js";
|
|
37
46
|
import { Resolver } from "../v3/resolve.js";
|
|
47
|
+
import { controlsIn, copiedControl, freshControlId } from "./controls.js";
|
|
48
|
+
import { noteIds, noteKindOf } from "./notes.js";
|
|
38
49
|
import type { EditorState } from "./state.js";
|
|
39
|
-
import {
|
|
50
|
+
import {
|
|
51
|
+
otherTrees,
|
|
52
|
+
type Story,
|
|
53
|
+
storyBody,
|
|
54
|
+
type WordContext,
|
|
55
|
+
type WordEnvelope,
|
|
56
|
+
wordContext,
|
|
57
|
+
} from "./word.js";
|
|
40
58
|
import { builtinStyle, levels } from "./wordformat.js";
|
|
41
59
|
|
|
42
60
|
/** What a v3 copy carries beside its content: the definitions the content references. */
|
|
@@ -46,6 +64,18 @@ export interface WordDefinitions {
|
|
|
46
64
|
styles: StyleJSON[];
|
|
47
65
|
numbering: { abstracts: AbstractNumberingJSON[]; instances: NumberingInstanceJSON[] };
|
|
48
66
|
comments: CommentJSON[];
|
|
67
|
+
/** The notes the content refers to, by story id. */
|
|
68
|
+
notes?: Record<string, StoryJSON>;
|
|
69
|
+
/** The resources (pictures' images) the content uses, by key. */
|
|
70
|
+
resources?: Record<string, ResourceJSON>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** What a paste brings: the content, the envelope it needs, and new notes. */
|
|
74
|
+
export interface FittedPaste {
|
|
75
|
+
doc: Node;
|
|
76
|
+
envelope: WordEnvelope;
|
|
77
|
+
/** The stories of the notes the references in `doc` now name, to add with it. */
|
|
78
|
+
notes?: { id: string; story: Story }[];
|
|
49
79
|
}
|
|
50
80
|
|
|
51
81
|
type Json = Record<string, unknown>;
|
|
@@ -68,7 +98,9 @@ type Properties = Pick<ParagraphPropertiesJSON, "style" | "numbering" | "markFor
|
|
|
68
98
|
* comments; with the document's id.
|
|
69
99
|
*/
|
|
70
100
|
export function copiedDefinitions(context: WordContext, content: Node): WordDefinitions {
|
|
71
|
-
const { styles, numbering, comments } = context.envelope;
|
|
101
|
+
const { styles, numbering, comments, stories, resources } = context.envelope;
|
|
102
|
+
const noteStories = new Set<string>();
|
|
103
|
+
const resourceKeys = new Set<string>();
|
|
72
104
|
const styleIds = new Set<string>();
|
|
73
105
|
const numberingIds = new Set<number>();
|
|
74
106
|
const commentIds = new Set<number>();
|
|
@@ -94,6 +126,8 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
|
|
|
94
126
|
if (node.type === "table") properties(attrs);
|
|
95
127
|
if (node.type === "tableRow") properties((attrs as { exceptions?: Properties }).exceptions);
|
|
96
128
|
if (COMMENT_ANCHORS.has(node.type)) commentIds.add(attrs.id as number);
|
|
129
|
+
if (node.type === "noteReference") noteStories.add(String(node.attrs?.story));
|
|
130
|
+
if (node.type === "picture") resourceKeys.add(String(node.attrs?.resource));
|
|
97
131
|
marks(node.marks);
|
|
98
132
|
};
|
|
99
133
|
visit([content.toJSON()], references);
|
|
@@ -102,6 +136,16 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
|
|
|
102
136
|
copied.flatMap((c) => c.content),
|
|
103
137
|
references,
|
|
104
138
|
);
|
|
139
|
+
const notes = Object.fromEntries(
|
|
140
|
+
[...noteStories].flatMap((id) => {
|
|
141
|
+
const story = stories && Object.hasOwn(stories, id) ? stories[id] : undefined;
|
|
142
|
+
return story ? [[id, story] as const] : [];
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
visit(
|
|
146
|
+
Object.values(notes).map((note) => note.content),
|
|
147
|
+
references,
|
|
148
|
+
);
|
|
105
149
|
const instances = () => numbering.instances.filter((n) => numberingIds.has(n.id));
|
|
106
150
|
const abstracts = () => {
|
|
107
151
|
const ids = new Set(instances().map((n) => n.abstract));
|
|
@@ -127,11 +171,18 @@ export function copiedDefinitions(context: WordContext, content: Node): WordDefi
|
|
|
127
171
|
for (const o of instance.overrides ?? [])
|
|
128
172
|
if (o.definition?.style) styleIds.add(o.definition.style);
|
|
129
173
|
}
|
|
174
|
+
const used = Object.fromEntries(
|
|
175
|
+
[...resourceKeys].flatMap((key) =>
|
|
176
|
+
resources && Object.hasOwn(resources, key) ? [[key, resources[key] as ResourceJSON]] : [],
|
|
177
|
+
),
|
|
178
|
+
);
|
|
130
179
|
return {
|
|
131
180
|
document: context.id,
|
|
132
181
|
styles: styles.filter((s) => styleIds.has(s.id)),
|
|
133
182
|
numbering: { abstracts: abstracts(), instances: instances() },
|
|
134
183
|
comments: copied,
|
|
184
|
+
...(Object.keys(notes).length ? { notes } : {}),
|
|
185
|
+
...(Object.keys(used).length ? { resources: used } : {}),
|
|
135
186
|
};
|
|
136
187
|
}
|
|
137
188
|
|
|
@@ -145,12 +196,14 @@ export function fitPastedV2(
|
|
|
145
196
|
state: EditorState,
|
|
146
197
|
content: Node,
|
|
147
198
|
): { doc: Node; envelope: WordEnvelope } {
|
|
148
|
-
const migrated =
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
199
|
+
const migrated = migrateToV5(
|
|
200
|
+
migrateToV4(
|
|
201
|
+
migrateToV3({
|
|
202
|
+
schemaVersion: 2,
|
|
203
|
+
settings: DEFAULT_SETTINGS,
|
|
204
|
+
content: content.toJSON() as DocJSON,
|
|
205
|
+
}),
|
|
206
|
+
),
|
|
154
207
|
);
|
|
155
208
|
const definitions: WordDefinitions = {
|
|
156
209
|
styles: migrated.styles.map((style) => {
|
|
@@ -188,7 +241,7 @@ export function fitPastedV2(
|
|
|
188
241
|
else delete child.marks;
|
|
189
242
|
}
|
|
190
243
|
}
|
|
191
|
-
return { doc: parseContent(json), envelope: fitted.envelope };
|
|
244
|
+
return { doc: parseContent(json, { schema: state.schema }), envelope: fitted.envelope };
|
|
192
245
|
}
|
|
193
246
|
|
|
194
247
|
const equal = (a: unknown, b: unknown) => JSON.stringify(a) === JSON.stringify(b);
|
|
@@ -210,17 +263,19 @@ function safeLinks(json: NodeJSON): NodeJSON {
|
|
|
210
263
|
* text, older copies). Returns the content to insert and the envelope with what it added (the
|
|
211
264
|
* same object when nothing was added). Throws FoliDocumentError on invalid input.
|
|
212
265
|
*/
|
|
213
|
-
export function fitPaste(
|
|
214
|
-
state: EditorState,
|
|
215
|
-
content: unknown,
|
|
216
|
-
definitions?: unknown,
|
|
217
|
-
): { doc: Node; envelope: WordEnvelope } {
|
|
266
|
+
export function fitPaste(state: EditorState, content: unknown, definitions?: unknown): FittedPaste {
|
|
218
267
|
const context = wordContext(state) as WordContext;
|
|
219
268
|
const target = context.envelope;
|
|
220
269
|
// Taken as this document's own, but it cannot be told to be: no unsafe link comes in.
|
|
221
270
|
if (definitions === undefined || definitions === null)
|
|
222
|
-
return fit(
|
|
223
|
-
|
|
271
|
+
return fit(
|
|
272
|
+
state,
|
|
273
|
+
target,
|
|
274
|
+
safeLinks(parseContent(content, { fragment: true, schema: state.schema }).toJSON()),
|
|
275
|
+
true,
|
|
276
|
+
);
|
|
277
|
+
const { document, styles, numbering, comments, notes, resources } =
|
|
278
|
+
definitions as Partial<WordDefinitions>;
|
|
224
279
|
// Validated as a document's envelope: types, ranges, no style based on itself; the content
|
|
225
280
|
// as content to paste (a table may end it).
|
|
226
281
|
const checked = parseDocument({
|
|
@@ -230,10 +285,58 @@ export function fitPaste(
|
|
|
230
285
|
styles,
|
|
231
286
|
numbering,
|
|
232
287
|
comments,
|
|
288
|
+
...(notes ? { stories: notes } : {}),
|
|
289
|
+
...(resources ? { resources } : {}),
|
|
233
290
|
content: { type: "doc", content: [{ type: "paragraph" }] },
|
|
234
291
|
});
|
|
235
|
-
const json = parseContent(content,
|
|
236
|
-
|
|
292
|
+
const json = parseContent(content, { fragment: true, schema: state.schema }).toJSON();
|
|
293
|
+
const source: WordDefinitions = {
|
|
294
|
+
styles: checked.styles,
|
|
295
|
+
numbering: checked.numbering,
|
|
296
|
+
comments: checked.comments,
|
|
297
|
+
...(checked.stories ? { notes: checked.stories } : {}),
|
|
298
|
+
...(checked.resources ? { resources: checked.resources } : {}),
|
|
299
|
+
};
|
|
300
|
+
return fit(state, source, json, document === context.id);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Adds to `out` the field and inline control markers of `nodes` without partners (ADR-0007): a
|
|
305
|
+
* field that lost its end loses its start and code too, so the code never becomes text. What they
|
|
306
|
+
* held stays. `open` carries what is open from one node to the next.
|
|
307
|
+
*/
|
|
308
|
+
function unpairedMarkers(
|
|
309
|
+
nodes: readonly NodeJSON[],
|
|
310
|
+
out: Set<NodeJSON>,
|
|
311
|
+
open: {
|
|
312
|
+
fields: { parts: NodeJSON[]; separated: boolean }[];
|
|
313
|
+
controls: Map<unknown, NodeJSON[]>;
|
|
314
|
+
} = { fields: [], controls: new Map() },
|
|
315
|
+
top = true,
|
|
316
|
+
): void {
|
|
317
|
+
for (const node of nodes) {
|
|
318
|
+
const field = open.fields[open.fields.length - 1];
|
|
319
|
+
const id = node.attrs?.id;
|
|
320
|
+
if (node.type === "fieldStart") open.fields.push({ parts: [node], separated: false });
|
|
321
|
+
else if (node.type === "fieldCode") {
|
|
322
|
+
if (field && !field.separated) field.parts.push(node);
|
|
323
|
+
else out.add(node);
|
|
324
|
+
} else if (node.type === "fieldSeparator") {
|
|
325
|
+
if (field && !field.separated) {
|
|
326
|
+
field.parts.push(node);
|
|
327
|
+
field.separated = true;
|
|
328
|
+
} else out.add(node);
|
|
329
|
+
} else if (node.type === "fieldEnd") {
|
|
330
|
+
if (!open.fields.pop()) out.add(node);
|
|
331
|
+
} else if (node.type === "controlStart")
|
|
332
|
+
open.controls.set(id, [...(open.controls.get(id) ?? []), node]);
|
|
333
|
+
else if (node.type === "controlEnd") {
|
|
334
|
+
if (!open.controls.get(id)?.pop()) out.add(node);
|
|
335
|
+
} else if (node.content) unpairedMarkers(node.content, out, open, false);
|
|
336
|
+
}
|
|
337
|
+
if (!top) return;
|
|
338
|
+
for (const { parts } of open.fields) for (const part of parts) out.add(part);
|
|
339
|
+
for (const starts of open.controls.values()) for (const start of starts) out.add(start);
|
|
237
340
|
}
|
|
238
341
|
|
|
239
342
|
/**
|
|
@@ -245,7 +348,7 @@ function fit(
|
|
|
245
348
|
source: WordDefinitions,
|
|
246
349
|
json: NodeJSON,
|
|
247
350
|
same: boolean,
|
|
248
|
-
):
|
|
351
|
+
): FittedPaste {
|
|
249
352
|
const target = (wordContext(state) as WordContext).envelope;
|
|
250
353
|
let styles = target.styles;
|
|
251
354
|
let { abstracts, instances } = target.numbering;
|
|
@@ -361,15 +464,21 @@ function fit(
|
|
|
361
464
|
for (const c of target.comments) nextId.comment = Math.max(nextId.comment, c.id + 1);
|
|
362
465
|
const names = new Set<string>();
|
|
363
466
|
const { from, to } = state.selection;
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
467
|
+
/** What every tree says about ids and names in use (ids are allocated across them). */
|
|
468
|
+
const scan = (tree: Node, inSelection: (pos: number) => boolean) =>
|
|
469
|
+
tree.descendants((node, pos) => {
|
|
470
|
+
const id = (node.attrs.id as number) + 1;
|
|
471
|
+
if (COMMENT_ANCHORS.has(node.type.name)) nextId.comment = Math.max(nextId.comment, id);
|
|
472
|
+
// A bookmark in the selection goes with it.
|
|
473
|
+
if (node.type.name === "bookmarkStart" && !inSelection(pos))
|
|
474
|
+
names.add(String(node.attrs.name));
|
|
475
|
+
if (node.type.name.startsWith("bookmark")) nextId.bookmark = Math.max(nextId.bookmark, id);
|
|
476
|
+
return node.type.name === "doc" || node.isBlock;
|
|
477
|
+
});
|
|
478
|
+
scan(state.doc, (pos) => pos >= from && pos < to);
|
|
479
|
+
for (const tree of otherTrees(state)) scan(tree, () => false);
|
|
480
|
+
// A story holds no comments, note references or section breaks (ADR-0007).
|
|
481
|
+
const inStory = storyBody(state) !== null;
|
|
373
482
|
const fresh = (kind: keyof typeof nextId, map: Map<number, number>, id: number) => {
|
|
374
483
|
let next = map.get(id);
|
|
375
484
|
if (next === undefined) {
|
|
@@ -383,10 +492,102 @@ function fit(
|
|
|
383
492
|
const bookmarkIds = new Map<number, number>();
|
|
384
493
|
const leftOut = new Set<number>();
|
|
385
494
|
|
|
495
|
+
// Controls that stay in the document: what is pasted of them is a copy, under a new id.
|
|
496
|
+
let controls = target.controls;
|
|
497
|
+
const staying = new Set<string>();
|
|
498
|
+
for (const range of controlsIn(state.doc))
|
|
499
|
+
if (range.from < from || range.to > to) staying.add(range.id);
|
|
500
|
+
for (const tree of otherTrees(state)) for (const range of controlsIn(tree)) staying.add(range.id);
|
|
501
|
+
const copiedControls = new Map<string, string>();
|
|
502
|
+
const mapControl = (id: unknown): string | null => {
|
|
503
|
+
if (!same || typeof id !== "string" || !Object.hasOwn(target.controls ?? {}, id)) return null;
|
|
504
|
+
if (!staying.has(id)) return id;
|
|
505
|
+
let copy = copiedControls.get(id);
|
|
506
|
+
if (!copy) {
|
|
507
|
+
copy = freshControlId(controls);
|
|
508
|
+
copiedControls.set(id, copy);
|
|
509
|
+
controls = { ...controls, [copy]: copiedControl(target.controls?.[id] as ControlJSON) };
|
|
510
|
+
}
|
|
511
|
+
return copy;
|
|
512
|
+
};
|
|
513
|
+
// Pictures from another document bring their images: under a key of their own here (the same
|
|
514
|
+
// image already here is reused), as bytes or a web URL only. A package part's name would name
|
|
515
|
+
// this document's part of that name, and a blob: URL dies with the page it was made in: such a
|
|
516
|
+
// picture comes without its image, under a key no resource has (drawn as a box, as Word shows
|
|
517
|
+
// a missing image; refused by `.docx` export until an image is given).
|
|
518
|
+
let resources = target.resources;
|
|
519
|
+
const resourceKeys = new Map<string, string>();
|
|
520
|
+
const takenKeys = new Set<string>();
|
|
521
|
+
const freeKey = (key: string) => {
|
|
522
|
+
let next = key;
|
|
523
|
+
for (let i = 2; (resources && Object.hasOwn(resources, next)) || takenKeys.has(next); i++)
|
|
524
|
+
next = `${key}-${i}`;
|
|
525
|
+
takenKeys.add(next);
|
|
526
|
+
return next;
|
|
527
|
+
};
|
|
528
|
+
const mapResource = (key: string): string => {
|
|
529
|
+
if (same) return key;
|
|
530
|
+
const known = resourceKeys.get(key);
|
|
531
|
+
if (known !== undefined) return known;
|
|
532
|
+
const carried =
|
|
533
|
+
source.resources && Object.hasOwn(source.resources, key) ? source.resources[key] : undefined;
|
|
534
|
+
const { part: _part, size, url, ...rest } = carried ?? { contentType: "" };
|
|
535
|
+
const web = url !== undefined && !/^\s*blob:/i.test(url) ? url : undefined;
|
|
536
|
+
let next: string;
|
|
537
|
+
if (!carried || (rest.data === undefined && web === undefined))
|
|
538
|
+
next = freeKey(`${key}-missing`);
|
|
539
|
+
else {
|
|
540
|
+
const resource: ResourceJSON = {
|
|
541
|
+
...rest,
|
|
542
|
+
...(web === undefined ? {} : { url: web }),
|
|
543
|
+
...(rest.data === undefined && size !== undefined ? { size } : {}),
|
|
544
|
+
};
|
|
545
|
+
const existing = Object.entries(resources ?? {}).find(([, r]) => equal(r, resource))?.[0];
|
|
546
|
+
next = existing ?? freeKey(key);
|
|
547
|
+
if (!existing) resources = { ...resources, [next]: resource };
|
|
548
|
+
}
|
|
549
|
+
resourceKeys.set(key, next);
|
|
550
|
+
return next;
|
|
551
|
+
};
|
|
552
|
+
// A note has one reference: one still in the document keeps it (cut and paste moves it).
|
|
553
|
+
const referredTo = new Set<string>();
|
|
554
|
+
state.doc.descendants((node, pos) => {
|
|
555
|
+
if (node.type.name === "noteReference" && (pos < from || pos >= to))
|
|
556
|
+
referredTo.add(String(node.attrs.story));
|
|
557
|
+
});
|
|
558
|
+
const noteAvailable = (story: string) =>
|
|
559
|
+
same && Object.hasOwn(target.stories ?? {}, story) && !referredTo.has(story);
|
|
560
|
+
const carried = (story: string) =>
|
|
561
|
+
source.notes && Object.hasOwn(source.notes, story) ? source.notes[story] : undefined;
|
|
562
|
+
const newNoteId = noteIds(state);
|
|
563
|
+
/** The new note of each story a pasted reference was copied with. */
|
|
564
|
+
const pastedNotes = new Map<string, string>();
|
|
565
|
+
|
|
566
|
+
// Half a field or control from elsewhere: its markers go, what it held stays.
|
|
567
|
+
const unpaired = new Set<NodeJSON>();
|
|
568
|
+
unpairedMarkers([json], unpaired);
|
|
569
|
+
for (const comment of source.comments) unpairedMarkers(comment.content, unpaired);
|
|
570
|
+
for (const note of Object.values(source.notes ?? {})) unpairedMarkers([note.content], unpaired);
|
|
571
|
+
|
|
386
572
|
const mapNode = (node: NodeJSON): NodeJSON | null => {
|
|
573
|
+
if (unpaired.has(node)) return null;
|
|
387
574
|
const attrs = node.attrs as Json | undefined;
|
|
575
|
+
if (inStory && (COMMENT_ANCHORS.has(node.type) || node.type === "noteReference")) return null;
|
|
388
576
|
let next: Json | undefined = attrs;
|
|
389
|
-
if (node.type === "
|
|
577
|
+
if (node.type === "noteReference") {
|
|
578
|
+
const story = String(attrs?.story);
|
|
579
|
+
const kind = noteKindOf(story);
|
|
580
|
+
if (kind && carried(story)) {
|
|
581
|
+
const id = pastedNotes.get(story) ?? newNoteId(kind);
|
|
582
|
+
pastedNotes.set(story, id);
|
|
583
|
+
next = { ...attrs, story: id };
|
|
584
|
+
} else if (!noteAvailable(story)) return null;
|
|
585
|
+
} else if (node.type === "controlStart" || node.type === "controlEnd") {
|
|
586
|
+
// From another document their definitions are not here: the content comes without them.
|
|
587
|
+
const id = mapControl(attrs?.id);
|
|
588
|
+
if (!id) return null;
|
|
589
|
+
next = { ...attrs, id };
|
|
590
|
+
} else if (node.type === "paragraph" && attrs) {
|
|
390
591
|
next = paragraphProperties(attrs as ParagraphPropertiesJSON) as Json;
|
|
391
592
|
const change = attrs.formatChange as { previous: ParagraphPropertiesJSON } | null | undefined;
|
|
392
593
|
if (change) next.formatChange = { ...change, previous: paragraphProperties(change.previous) };
|
|
@@ -398,6 +599,8 @@ function fit(
|
|
|
398
599
|
if (Object.keys(rest).length) next.attributes = rest;
|
|
399
600
|
else delete next.attributes;
|
|
400
601
|
}
|
|
602
|
+
} else if (node.type === "picture" && attrs) {
|
|
603
|
+
next = { ...attrs, resource: mapResource(String(attrs.resource)) };
|
|
401
604
|
} else if (node.type === "table" && attrs?.style) {
|
|
402
605
|
next = { ...attrs, style: mapStyle(String(attrs.style)) };
|
|
403
606
|
} else if (node.type === "tableRow" && attrs) {
|
|
@@ -417,6 +620,16 @@ function fit(
|
|
|
417
620
|
if (node.type === "bookmarkStart") names.add(String(attrs?.name));
|
|
418
621
|
next = { ...attrs, id: fresh("bookmark", bookmarkIds, id) };
|
|
419
622
|
}
|
|
623
|
+
if (inStory && next?.section) {
|
|
624
|
+
next = { ...next };
|
|
625
|
+
delete next.section;
|
|
626
|
+
}
|
|
627
|
+
if (next?.controls) {
|
|
628
|
+
const ids = (next.controls as string[]).flatMap((id) => mapControl(id) ?? []);
|
|
629
|
+
next = { ...next };
|
|
630
|
+
if (ids.length) next.controls = ids;
|
|
631
|
+
else delete next.controls;
|
|
632
|
+
}
|
|
420
633
|
const marks = node.marks && runMarks(node.marks);
|
|
421
634
|
const children = node.content
|
|
422
635
|
?.map(mapNode)
|
|
@@ -429,7 +642,34 @@ function fit(
|
|
|
429
642
|
} as NodeJSON;
|
|
430
643
|
};
|
|
431
644
|
|
|
432
|
-
|
|
645
|
+
// Pasted into a block control, the new paragraphs are in it as the one at the caret is.
|
|
646
|
+
const around = (
|
|
647
|
+
from === to || state.selection.$from.parent === state.selection.$to.parent
|
|
648
|
+
? state.selection.$from.parent.attrs.controls
|
|
649
|
+
: null
|
|
650
|
+
) as string[] | null | undefined;
|
|
651
|
+
const mapped = mapNode(json) as NodeJSON;
|
|
652
|
+
const inPlace = around?.length
|
|
653
|
+
? {
|
|
654
|
+
...mapped,
|
|
655
|
+
content: mapped.content?.map((block) => {
|
|
656
|
+
const attrs = block.attrs as Json | undefined;
|
|
657
|
+
const own = (attrs?.controls as string[] | undefined) ?? [];
|
|
658
|
+
return { ...block, attrs: { ...attrs, controls: [...around, ...own] } };
|
|
659
|
+
}),
|
|
660
|
+
}
|
|
661
|
+
: mapped;
|
|
662
|
+
const doc = parseContent(inPlace, { fragment: true, schema: state.schema });
|
|
663
|
+
const notes = [...pastedNotes].map(([from, id]) => {
|
|
664
|
+
const note = carried(from) as StoryJSON;
|
|
665
|
+
return {
|
|
666
|
+
id,
|
|
667
|
+
story: {
|
|
668
|
+
kind: note.kind,
|
|
669
|
+
doc: parseContent(mapNode(note.content), { schema: state.schema }),
|
|
670
|
+
},
|
|
671
|
+
};
|
|
672
|
+
});
|
|
433
673
|
let comments = target.comments;
|
|
434
674
|
const copiedComments = source.comments.filter((c) => commentIds.has(c.id));
|
|
435
675
|
if (copiedComments.length) {
|
|
@@ -452,11 +692,21 @@ function fit(
|
|
|
452
692
|
styles !== target.styles ||
|
|
453
693
|
abstracts !== target.numbering.abstracts ||
|
|
454
694
|
instances !== target.numbering.instances ||
|
|
455
|
-
comments !== target.comments
|
|
695
|
+
comments !== target.comments ||
|
|
696
|
+
controls !== target.controls ||
|
|
697
|
+
resources !== target.resources;
|
|
456
698
|
return {
|
|
457
699
|
doc,
|
|
458
700
|
envelope: changed
|
|
459
|
-
? {
|
|
701
|
+
? {
|
|
702
|
+
...target,
|
|
703
|
+
styles,
|
|
704
|
+
numbering: { abstracts, instances },
|
|
705
|
+
comments,
|
|
706
|
+
...(controls ? { controls } : {}),
|
|
707
|
+
...(resources ? { resources } : {}),
|
|
708
|
+
}
|
|
460
709
|
: target,
|
|
710
|
+
...(notes.length ? { notes } : {}),
|
|
461
711
|
};
|
|
462
712
|
}
|