@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/index.ts
CHANGED
|
@@ -6,6 +6,7 @@ export {
|
|
|
6
6
|
deleteForward,
|
|
7
7
|
deleteSelection,
|
|
8
8
|
deleteSelectionKeepingTables,
|
|
9
|
+
insertColumnBreak,
|
|
9
10
|
insertHardBreak,
|
|
10
11
|
insertPageBreak,
|
|
11
12
|
insertTab,
|
|
@@ -30,13 +31,85 @@ export {
|
|
|
30
31
|
resolveComment,
|
|
31
32
|
} from "./comments.js";
|
|
32
33
|
export {
|
|
34
|
+
addRepeatingItem,
|
|
35
|
+
type ControlInsertOptions,
|
|
36
|
+
type ControlPropertiesPatch,
|
|
37
|
+
type ControlRange,
|
|
38
|
+
type ControlValue,
|
|
39
|
+
type ControlValueInput,
|
|
40
|
+
controlAt,
|
|
41
|
+
controlIsReadOnly,
|
|
42
|
+
controlProperties,
|
|
43
|
+
controlSpot,
|
|
44
|
+
controlsIn,
|
|
45
|
+
controlValues,
|
|
46
|
+
insertControl,
|
|
47
|
+
insertControlText,
|
|
48
|
+
setControlProperties,
|
|
49
|
+
setControlValue,
|
|
50
|
+
} from "./controls.js";
|
|
51
|
+
export {
|
|
52
|
+
type BuiltInCommands,
|
|
33
53
|
createEditor,
|
|
54
|
+
type DocumentTarget,
|
|
34
55
|
type Editor,
|
|
35
56
|
type EditorDocument,
|
|
36
57
|
type EditorListener,
|
|
37
58
|
type EditorOptions,
|
|
59
|
+
editorCommand,
|
|
60
|
+
type FoliCommands,
|
|
61
|
+
type FoundMatches,
|
|
62
|
+
type NavigationRequest,
|
|
63
|
+
type OpenStory,
|
|
64
|
+
type StoryView,
|
|
38
65
|
sectionSettings,
|
|
66
|
+
viewKeys,
|
|
39
67
|
} from "./editor.js";
|
|
68
|
+
export {
|
|
69
|
+
caretStop,
|
|
70
|
+
type Field,
|
|
71
|
+
type FieldDisplay,
|
|
72
|
+
fieldAround,
|
|
73
|
+
fieldCodesShown,
|
|
74
|
+
fieldDisplay,
|
|
75
|
+
fieldsIn,
|
|
76
|
+
sameSpot,
|
|
77
|
+
toggleFieldCodes,
|
|
78
|
+
} from "./fields.js";
|
|
79
|
+
export {
|
|
80
|
+
type CrossReference,
|
|
81
|
+
type CrossReferenceTargets,
|
|
82
|
+
crossReferenceTargets,
|
|
83
|
+
type FieldInstruction,
|
|
84
|
+
type FieldLayout,
|
|
85
|
+
type FieldPage,
|
|
86
|
+
type FieldSkip,
|
|
87
|
+
type FieldSkipReason,
|
|
88
|
+
type FieldsReport,
|
|
89
|
+
formatDatePicture,
|
|
90
|
+
formatNumericPicture,
|
|
91
|
+
type InsertFieldOptions,
|
|
92
|
+
insertCrossReference,
|
|
93
|
+
insertDate,
|
|
94
|
+
insertField,
|
|
95
|
+
insertPageNumber,
|
|
96
|
+
insertTableOfContents,
|
|
97
|
+
parseFieldInstruction,
|
|
98
|
+
type TableOfContentsOptions,
|
|
99
|
+
type UpdateFieldsOptions,
|
|
100
|
+
updateFields,
|
|
101
|
+
} from "./fieldupdate.js";
|
|
102
|
+
export {
|
|
103
|
+
type FindMatch,
|
|
104
|
+
type FindOptions,
|
|
105
|
+
findAll,
|
|
106
|
+
findInText,
|
|
107
|
+
nextMatch,
|
|
108
|
+
type ReplaceRefusal,
|
|
109
|
+
type ReplaceReport,
|
|
110
|
+
replace,
|
|
111
|
+
replaceAll,
|
|
112
|
+
} from "./find.js";
|
|
40
113
|
export {
|
|
41
114
|
alignAt,
|
|
42
115
|
colorAt,
|
|
@@ -48,6 +121,7 @@ export {
|
|
|
48
121
|
closeHistory,
|
|
49
122
|
type HistoryOptions,
|
|
50
123
|
history,
|
|
124
|
+
joinHistory,
|
|
51
125
|
redo,
|
|
52
126
|
redoDepth,
|
|
53
127
|
undo,
|
|
@@ -75,6 +149,33 @@ export {
|
|
|
75
149
|
wrapInList,
|
|
76
150
|
} from "./lists.js";
|
|
77
151
|
export { typedMarks } from "./mark.js";
|
|
152
|
+
export {
|
|
153
|
+
type BookmarkInfo,
|
|
154
|
+
type BookmarkResult,
|
|
155
|
+
deleteBookmark,
|
|
156
|
+
documentOutline,
|
|
157
|
+
goTo,
|
|
158
|
+
insertBookmark,
|
|
159
|
+
isBookmarkName,
|
|
160
|
+
listBookmarks,
|
|
161
|
+
type OutlineEntry,
|
|
162
|
+
renameBookmark,
|
|
163
|
+
} from "./navigation.js";
|
|
164
|
+
export {
|
|
165
|
+
convertNotes,
|
|
166
|
+
type InsertNoteOptions,
|
|
167
|
+
insertNote,
|
|
168
|
+
type NoteKind,
|
|
169
|
+
type NoteNumber,
|
|
170
|
+
type NoteNumberOptions,
|
|
171
|
+
noteAtSelection,
|
|
172
|
+
noteKindOf,
|
|
173
|
+
noteNumbers,
|
|
174
|
+
noteProperties,
|
|
175
|
+
noteTexts,
|
|
176
|
+
referencesIn,
|
|
177
|
+
setNoteProperties,
|
|
178
|
+
} from "./notes.js";
|
|
78
179
|
export {
|
|
79
180
|
insertPicture,
|
|
80
181
|
type NewPicture,
|
|
@@ -94,6 +195,26 @@ export {
|
|
|
94
195
|
rejectChange,
|
|
95
196
|
trackedChanges,
|
|
96
197
|
} from "./review.js";
|
|
198
|
+
export {
|
|
199
|
+
documentSections,
|
|
200
|
+
ensureStory,
|
|
201
|
+
insertSectionBreak,
|
|
202
|
+
pageVariant,
|
|
203
|
+
removeSectionBreak,
|
|
204
|
+
type SectionAt,
|
|
205
|
+
type SectionBreakKind,
|
|
206
|
+
type SectionColumns,
|
|
207
|
+
type SectionSetup,
|
|
208
|
+
type StorySide,
|
|
209
|
+
type StoryVariant,
|
|
210
|
+
sectionAt,
|
|
211
|
+
sectionColumns,
|
|
212
|
+
setDifferentEvenPages,
|
|
213
|
+
setDifferentFirstPage,
|
|
214
|
+
setSectionSetup,
|
|
215
|
+
setStoryLinked,
|
|
216
|
+
storyOfSection,
|
|
217
|
+
} from "./sections.js";
|
|
97
218
|
export {
|
|
98
219
|
AllSelection,
|
|
99
220
|
marksAt,
|
|
@@ -104,7 +225,6 @@ export {
|
|
|
104
225
|
} from "./selection.js";
|
|
105
226
|
export {
|
|
106
227
|
type Command,
|
|
107
|
-
defaultTextblockType,
|
|
108
228
|
EditorState,
|
|
109
229
|
type EditorStateConfig,
|
|
110
230
|
Plugin,
|
|
@@ -149,6 +269,9 @@ export {
|
|
|
149
269
|
} from "./track.js";
|
|
150
270
|
export {
|
|
151
271
|
CommentStep,
|
|
272
|
+
documentJSON,
|
|
273
|
+
EnvelopeStep,
|
|
274
|
+
StoryStep,
|
|
152
275
|
setComments,
|
|
153
276
|
type WordContext,
|
|
154
277
|
type WordEnvelope,
|
|
@@ -157,6 +280,7 @@ export {
|
|
|
157
280
|
} from "./word.js";
|
|
158
281
|
export {
|
|
159
282
|
copiedDefinitions,
|
|
283
|
+
type FittedPaste,
|
|
160
284
|
fitPaste,
|
|
161
285
|
fitPastedV2,
|
|
162
286
|
type WordDefinitions,
|
package/src/state/lists.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Command, Plugin } from "./state.js";
|
|
1
|
+
import { type Command, type EditorState, Plugin } from "./state.js";
|
|
2
2
|
import {
|
|
3
3
|
endWordList,
|
|
4
4
|
type ListKind,
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
} from "./wordformat.js";
|
|
12
12
|
|
|
13
13
|
/** The kind of list all selected paragraphs are in ("bulletList", "orderedList"), or null. */
|
|
14
|
-
export const listKindAt = wordListKindAt;
|
|
14
|
+
export const listKindAt: (state: EditorState) => ListKind | null = wordListKindAt;
|
|
15
15
|
|
|
16
16
|
/** Numbers the selected paragraphs as a list of `kind` (one right before goes on). */
|
|
17
17
|
export const wrapInList: (kind: ListKind) => Command = wrapInWordList;
|
package/src/state/mark.ts
CHANGED
|
@@ -10,11 +10,19 @@ import type { Transform } from "../transform/transform.js";
|
|
|
10
10
|
* Attributes the paragraph mark carries in Word (schema v3): the section break, the mark's
|
|
11
11
|
* tracked change and the paragraph's own attributes (`w14:paraId`). Whichever paragraph keeps
|
|
12
12
|
* the mark keeps them.
|
|
13
|
+
* @internal
|
|
13
14
|
*/
|
|
14
15
|
export const MARK_ATTRS = ["section", "revision", "formatChange", "attributes"] as const;
|
|
15
16
|
|
|
16
17
|
/** Inline nodes that stand between runs: bookmarks and comment ranges. */
|
|
17
|
-
const MARKERS = new Set([
|
|
18
|
+
const MARKERS = new Set([
|
|
19
|
+
"bookmarkStart",
|
|
20
|
+
"bookmarkEnd",
|
|
21
|
+
"commentStart",
|
|
22
|
+
"commentEnd",
|
|
23
|
+
"controlStart",
|
|
24
|
+
"controlEnd",
|
|
25
|
+
]);
|
|
18
26
|
|
|
19
27
|
/**
|
|
20
28
|
* After a join, or a deletion across paragraphs, the merged paragraph at `pos` ends with the
|
|
@@ -23,6 +31,7 @@ const MARKERS = new Set(["bookmarkStart", "bookmarkEnd", "commentStart", "commen
|
|
|
23
31
|
* (it was empty, or deleted from its start), it is gone: the last one stays whole, as in Word.
|
|
24
32
|
* `tail` is where the content kept of the last one starts in it (a deletion into it). Nothing for
|
|
25
33
|
* a schema without them (v2).
|
|
34
|
+
* @internal
|
|
26
35
|
*/
|
|
27
36
|
export function keepLastMark(tr: Transform, pos: number, last: Node, tail = 0): void {
|
|
28
37
|
const merged = tr.doc.nodeAt(pos);
|
|
@@ -0,0 +1,305 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Going places in a document (11.11): bookmarks with Word's rules for their names, and the outline.
|
|
3
|
+
* Bookmarks are not tracked (Word does not track them) and their ids are unique over the body and
|
|
4
|
+
* every story.
|
|
5
|
+
*/
|
|
6
|
+
import { Fragment, type Mark, type Node, type NodeType, Slice } from "../model/index.js";
|
|
7
|
+
import { formatNumber } from "../v3/resolve.js";
|
|
8
|
+
import type { DocumentTarget, Editor } from "./editor.js";
|
|
9
|
+
import { fieldsIn } from "./fields.js";
|
|
10
|
+
import {
|
|
11
|
+
bookmarkContent,
|
|
12
|
+
bookmarksIn,
|
|
13
|
+
entryTitle,
|
|
14
|
+
type FieldLayout,
|
|
15
|
+
headingLevel,
|
|
16
|
+
nextBookmarkId,
|
|
17
|
+
paragraphsOf,
|
|
18
|
+
} from "./fieldupdate.js";
|
|
19
|
+
import { storyOfEditor } from "./find.js";
|
|
20
|
+
import { TextSelection } from "./selection.js";
|
|
21
|
+
import type { EditorState, Transaction } from "./state.js";
|
|
22
|
+
import { editTrees } from "./stories.js";
|
|
23
|
+
import { storyBody, wordContext } from "./word.js";
|
|
24
|
+
|
|
25
|
+
/** A letter, then letters, digits and `_`; at most 40 (what Word takes). */
|
|
26
|
+
const NAME = /^\p{L}[\p{L}\p{N}_]{0,39}$/u;
|
|
27
|
+
|
|
28
|
+
/** Whether Word takes `name` for a bookmark made by hand. */
|
|
29
|
+
export const isBookmarkName = (name: string): boolean => NAME.test(name);
|
|
30
|
+
|
|
31
|
+
export interface BookmarkInfo {
|
|
32
|
+
name: string;
|
|
33
|
+
/** The story it is in; null: the body. */
|
|
34
|
+
story: string | null;
|
|
35
|
+
/** After its start marker, before its end marker. */
|
|
36
|
+
from: number;
|
|
37
|
+
to: number;
|
|
38
|
+
/** The text it holds. */
|
|
39
|
+
text: string;
|
|
40
|
+
/** Word's own (`_Toc…`, `_Ref…`, `_GoBack`): not listed unless asked for. */
|
|
41
|
+
hidden: boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export type BookmarkResult =
|
|
45
|
+
| { ok: true; bookmark: BookmarkInfo }
|
|
46
|
+
| {
|
|
47
|
+
ok: false;
|
|
48
|
+
/**
|
|
49
|
+
* `invalid-name`: not a name Word takes. `no-place`: the selection is not in text.
|
|
50
|
+
* `missing`: no bookmark has that name. `exists`: another one does. `refused`: the content
|
|
51
|
+
* there cannot change (a locked control).
|
|
52
|
+
*/
|
|
53
|
+
reason: "invalid-name" | "no-place" | "missing" | "exists" | "refused";
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const bodyOf = (state: EditorState) => storyBody(state)?.state ?? state;
|
|
57
|
+
|
|
58
|
+
function treesOf(state: EditorState): { id: string | null; tree: Node }[] {
|
|
59
|
+
const body = bodyOf(state);
|
|
60
|
+
return [
|
|
61
|
+
{ id: null, tree: body.doc },
|
|
62
|
+
...[...(wordContext(body)?.stories ?? [])].map(([id, story]) => ({ id, tree: story.doc })),
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The document's bookmarks, the body's first; Word's hidden ones only with `hidden`. */
|
|
67
|
+
export function listBookmarks(
|
|
68
|
+
target: Editor | EditorState,
|
|
69
|
+
{ hidden = false }: { hidden?: boolean } = {},
|
|
70
|
+
): BookmarkInfo[] {
|
|
71
|
+
return treesOf("getDocument" in target ? target.state : target).flatMap(({ id, tree }) =>
|
|
72
|
+
[...bookmarksIn(tree).values()].flatMap((mark) => {
|
|
73
|
+
const secret = mark.name.startsWith("_");
|
|
74
|
+
if (secret && !hidden) return [];
|
|
75
|
+
const text = bookmarkContent(tree, mark)
|
|
76
|
+
.map((node) => node.text ?? " ")
|
|
77
|
+
.join("");
|
|
78
|
+
return [{ ...mark, story: id, text, hidden: secret }];
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const same = (a: string, b: string) => a.toLowerCase() === b.toLowerCase();
|
|
84
|
+
|
|
85
|
+
/** The positions of `name`'s markers in `tree`, either end. */
|
|
86
|
+
function markersOf(tree: Node, name: string): number[] {
|
|
87
|
+
const ids = new Set<unknown>();
|
|
88
|
+
const out: number[] = [];
|
|
89
|
+
tree.descendants((node, pos) => {
|
|
90
|
+
if (node.type.name === "bookmarkStart" && same(String(node.attrs.name), name)) {
|
|
91
|
+
ids.add(node.attrs.id);
|
|
92
|
+
out.push(pos);
|
|
93
|
+
} else if (node.type.name === "bookmarkEnd" && ids.has(node.attrs.id)) out.push(pos);
|
|
94
|
+
return !node.isInline;
|
|
95
|
+
});
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const find = (editor: Editor, name: string) =>
|
|
100
|
+
listBookmarks(editor, { hidden: true }).find((b) => same(b.name, name));
|
|
101
|
+
|
|
102
|
+
const result = (editor: Editor, name: string, failed: Set<unknown>): BookmarkResult => {
|
|
103
|
+
const bookmark = failed.size ? undefined : find(editor, name);
|
|
104
|
+
return bookmark ? { ok: true, bookmark } : { ok: false, reason: "refused" };
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A bookmark over the selection or `range` of `editor`'s tree (a caret: a point). As in Word, a
|
|
109
|
+
* name already used, in any case and any story, moves that bookmark here.
|
|
110
|
+
*/
|
|
111
|
+
export function insertBookmark(
|
|
112
|
+
editor: Editor,
|
|
113
|
+
name: string,
|
|
114
|
+
range?: { from: number; to: number },
|
|
115
|
+
): BookmarkResult {
|
|
116
|
+
if (!isBookmarkName(name)) return { ok: false, reason: "invalid-name" };
|
|
117
|
+
const { state } = editor;
|
|
118
|
+
const { from, to } = range ?? state.selection;
|
|
119
|
+
const size = state.doc.content.size;
|
|
120
|
+
if (from < 0 || to > size || from > to) return { ok: false, reason: "no-place" };
|
|
121
|
+
if (![from, to].every((pos) => state.doc.resolve(pos).parent.inlineContent))
|
|
122
|
+
return { ok: false, reason: "no-place" };
|
|
123
|
+
const here = storyOfEditor(editor);
|
|
124
|
+
const edits = new Map<string | null, (tr: Transaction) => void>();
|
|
125
|
+
for (const { id, tree } of treesOf(state)) {
|
|
126
|
+
const old = markersOf(tree, name);
|
|
127
|
+
if (id !== here && !old.length) continue;
|
|
128
|
+
edits.set(id, (tr) => {
|
|
129
|
+
const { bookmarkStart, bookmarkEnd } = tr.doc.type.schema.nodes as Record<string, NodeType>;
|
|
130
|
+
const mark = id === here ? nextBookmarkId(state, tr) : 0;
|
|
131
|
+
// From the end back, so positions hold; at one place the old goes first, the start ends first.
|
|
132
|
+
const steps: { pos: number; rank: number; run: () => void }[] = old.map((pos) => ({
|
|
133
|
+
pos,
|
|
134
|
+
rank: 0,
|
|
135
|
+
run: () => tr.delete(pos, pos + 1),
|
|
136
|
+
}));
|
|
137
|
+
if (id === here)
|
|
138
|
+
steps.push(
|
|
139
|
+
{
|
|
140
|
+
pos: to,
|
|
141
|
+
rank: 1,
|
|
142
|
+
run: () => tr.insert(to, (bookmarkEnd as NodeType).create({ id: mark })),
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
pos: from,
|
|
146
|
+
rank: 2,
|
|
147
|
+
run: () => tr.insert(from, (bookmarkStart as NodeType).create({ id: mark, name })),
|
|
148
|
+
},
|
|
149
|
+
);
|
|
150
|
+
steps.sort((a, b) => b.pos - a.pos || a.rank - b.rank);
|
|
151
|
+
for (const step of steps) step.run();
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return result(editor, name, editTrees(editor, edits, { tracked: false }));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const escaped = (text: string) => text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
158
|
+
|
|
159
|
+
/** Word's field names: a code starting with one is that field, never a bookmark standing for `REF`. */
|
|
160
|
+
const FIELD_NAMES = new Set(
|
|
161
|
+
(
|
|
162
|
+
"ADDRESSBLOCK ADVANCE ASK AUTHOR AUTONUM AUTONUMLGL AUTONUMOUT AUTOTEXT AUTOTEXTLIST BARCODE " +
|
|
163
|
+
"BIBLIOGRAPHY BIDIOUTLINE CITATION COMMENTS COMPARE CREATEDATE DATABASE DATE DISPLAYBARCODE " +
|
|
164
|
+
"DOCPROPERTY DOCVARIABLE EDITTIME EQ FILENAME FILESIZE FILLIN FORMCHECKBOX FORMDROPDOWN " +
|
|
165
|
+
"FORMTEXT GOTOBUTTON GREETINGLINE HYPERLINK IF INCLUDEPICTURE INCLUDETEXT INDEX INFO KEYWORDS " +
|
|
166
|
+
"LASTSAVEDBY LINK LISTNUM MACROBUTTON MERGEBARCODE MERGEFIELD MERGEREC MERGESEQ NEXT NEXTIF " +
|
|
167
|
+
"NOTEREF NUMCHARS NUMPAGES NUMWORDS PAGE PAGEREF PRINT PRINTDATE PRIVATE QUOTE RD REF REVNUM " +
|
|
168
|
+
"SAVEDATE SECTION SECTIONPAGES SEQ SET SKIPIF STYLEREF SUBJECT SYMBOL TA TC TEMPLATE TIME " +
|
|
169
|
+
"TITLE TOA TOC USERADDRESS USERINITIALS USERNAME XE"
|
|
170
|
+
).split(" "),
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
/** Renames a bookmark, and what refers to it: `REF`, `PAGEREF`, `NOTEREF` and links. */
|
|
174
|
+
export function renameBookmark(editor: Editor, name: string, next: string): BookmarkResult {
|
|
175
|
+
if (!isBookmarkName(next)) return { ok: false, reason: "invalid-name" };
|
|
176
|
+
const current = find(editor, name);
|
|
177
|
+
if (!current) return { ok: false, reason: "missing" };
|
|
178
|
+
const taken = find(editor, next);
|
|
179
|
+
if (taken && !same(taken.name, name)) return { ok: false, reason: "exists" };
|
|
180
|
+
const old = escaped(current.name);
|
|
181
|
+
// shortcut: a reference whose name is split over two `w:instrText` runs is not followed.
|
|
182
|
+
// A bare name stands for `REF` only when it is no field's name: `{ TITLE }` is the title.
|
|
183
|
+
const bare = FIELD_NAMES.has(current.name.toUpperCase()) ? "" : "?";
|
|
184
|
+
const reference = new RegExp(`^(\\s*(?:(?:REF|PAGEREF|NOTEREF)\\s+)${bare})${old}(?=\\s|$)`, "i");
|
|
185
|
+
const edits = new Map<string | null, (tr: Transaction) => void>();
|
|
186
|
+
for (const { id, tree } of treesOf(editor.state))
|
|
187
|
+
edits.set(id, (tr) => {
|
|
188
|
+
const swap = (pos: number, node: Node, attrs: Record<string, unknown>) =>
|
|
189
|
+
tr.replace(
|
|
190
|
+
pos,
|
|
191
|
+
pos + 1,
|
|
192
|
+
new Slice(
|
|
193
|
+
Fragment.from(node.type.create({ ...node.attrs, ...attrs }, null, node.marks)),
|
|
194
|
+
0,
|
|
195
|
+
0,
|
|
196
|
+
),
|
|
197
|
+
);
|
|
198
|
+
const codes = new Set(fieldsIn(tree).map((field) => field.from));
|
|
199
|
+
let inField = false;
|
|
200
|
+
tree.descendants((node, pos) => {
|
|
201
|
+
if (!node.isInline) return true;
|
|
202
|
+
const type = node.type.name;
|
|
203
|
+
if (type === "fieldStart") inField = codes.has(pos);
|
|
204
|
+
if (type === "bookmarkStart" && same(String(node.attrs.name), name))
|
|
205
|
+
swap(pos, node, { name: next });
|
|
206
|
+
if (type === "fieldCode" && inField) {
|
|
207
|
+
const text = String(node.attrs.text ?? "");
|
|
208
|
+
const changed = text.replace(reference, `$1${next}`);
|
|
209
|
+
if (changed !== text) swap(pos, node, { text: changed });
|
|
210
|
+
inField = false;
|
|
211
|
+
}
|
|
212
|
+
const link = node.marks.find(
|
|
213
|
+
(m: Mark) => m.type.name === "link" && m.attrs.anchor === current.name,
|
|
214
|
+
);
|
|
215
|
+
if (link) {
|
|
216
|
+
const end = pos + node.nodeSize;
|
|
217
|
+
tr.removeMark(pos, end, link);
|
|
218
|
+
tr.addMark(pos, end, link.type.create({ ...link.attrs, anchor: next }));
|
|
219
|
+
}
|
|
220
|
+
return false;
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
return result(editor, next, editTrees(editor, edits, { tracked: false }));
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Deletes a bookmark, in whichever tree it is. What refers to it keeps its last result. */
|
|
227
|
+
export function deleteBookmark(editor: Editor, name: string): boolean {
|
|
228
|
+
const edits = new Map<string | null, (tr: Transaction) => void>();
|
|
229
|
+
for (const { id, tree } of treesOf(editor.state)) {
|
|
230
|
+
const markers = markersOf(tree, name);
|
|
231
|
+
if (markers.length)
|
|
232
|
+
edits.set(id, (tr) => {
|
|
233
|
+
for (const pos of markers.reverse()) tr.delete(pos, pos + 1);
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return edits.size > 0 && !editTrees(editor, edits, { tracked: false }).size;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Selects `target` and shows it: through the view when there is one (which opens a story for it),
|
|
241
|
+
* else in the editor of its tree. Whether it could.
|
|
242
|
+
*/
|
|
243
|
+
export function goTo(
|
|
244
|
+
editor: Editor,
|
|
245
|
+
target: DocumentTarget,
|
|
246
|
+
options: { focus?: boolean } = {},
|
|
247
|
+
): boolean {
|
|
248
|
+
const view = editor.storyView;
|
|
249
|
+
if (view) return view.reveal(target, options);
|
|
250
|
+
const host = target.story === null ? null : editor.story(target.story);
|
|
251
|
+
if (target.story !== null && !host) return false;
|
|
252
|
+
const state = (host ?? editor).state;
|
|
253
|
+
const tr = state.tr.setSelection(TextSelection.create(state.doc, target.from, target.to));
|
|
254
|
+
(host ?? storyBody(editor.state) ?? editor).dispatch(tr);
|
|
255
|
+
return true;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export interface OutlineEntry {
|
|
259
|
+
/** Before the paragraph. */
|
|
260
|
+
pos: number;
|
|
261
|
+
/** 1 to 9. */
|
|
262
|
+
level: number;
|
|
263
|
+
text: string;
|
|
264
|
+
/** Its list number, if any. */
|
|
265
|
+
number: string;
|
|
266
|
+
/** Its page as the document numbers it; null without a layout. */
|
|
267
|
+
page: string | null;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* The body's headings, by the rule of a table of contents with `\o` and `\u`: a heading style's
|
|
272
|
+
* level, else the paragraph's outline level. Empty ones and a table of contents' own lines are
|
|
273
|
+
* left out. `layout` (`editor.storyView.fieldLayout`) gives their pages.
|
|
274
|
+
*/
|
|
275
|
+
export function documentOutline(
|
|
276
|
+
state: EditorState,
|
|
277
|
+
layout: FieldLayout | null = null,
|
|
278
|
+
): OutlineEntry[] {
|
|
279
|
+
const body = bodyOf(state);
|
|
280
|
+
const word = wordContext(body);
|
|
281
|
+
if (!word) return [];
|
|
282
|
+
const tree = body.doc;
|
|
283
|
+
const paragraphs = paragraphsOf(tree);
|
|
284
|
+
const markers = word.resolver.markers(paragraphs.map((p) => p.node.attrs));
|
|
285
|
+
const contents = fieldsIn(tree).filter((field) => field.name === "TOC");
|
|
286
|
+
return paragraphs.flatMap((paragraph, i) => {
|
|
287
|
+
const { node, pos } = paragraph;
|
|
288
|
+
const outline = word.resolver.paragraph(node.attrs).paragraph.outlineLevel;
|
|
289
|
+
const level =
|
|
290
|
+
headingLevel(word, node) ?? (outline !== null && outline < 9 ? outline + 1 : null);
|
|
291
|
+
if (level === null || contents.some((f) => pos + 1 >= f.from && pos < f.to)) return [];
|
|
292
|
+
const text = entryTitle(tree, word, paragraph);
|
|
293
|
+
if (!text.trim()) return [];
|
|
294
|
+
const at = layout?.pageAt(pos + 1);
|
|
295
|
+
return [
|
|
296
|
+
{
|
|
297
|
+
pos,
|
|
298
|
+
level,
|
|
299
|
+
text,
|
|
300
|
+
number: markers[i]?.text ?? "",
|
|
301
|
+
page: at ? formatNumber(at.number, at.format) : null,
|
|
302
|
+
},
|
|
303
|
+
];
|
|
304
|
+
});
|
|
305
|
+
}
|