@folijs/core 0.5.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 +28 -0
- 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.map +1 -1
- package/dist/state/comments.js +12 -3
- 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 +168 -42
- 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 +12 -5
- package/dist/state/index.d.ts.map +1 -1
- package/dist/state/index.js +10 -3
- package/dist/state/index.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/review.d.ts +13 -4
- package/dist/state/review.d.ts.map +1 -1
- package/dist/state/review.js +133 -18
- 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/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/tables.d.ts.map +1 -1
- package/dist/state/tables.js +2 -1
- package/dist/state/tables.js.map +1 -1
- package/dist/state/track.d.ts +23 -0
- package/dist/state/track.d.ts.map +1 -1
- package/dist/state/track.js +58 -9
- 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 +230 -10
- 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 +192 -14
- package/dist/state/wordclip.js.map +1 -1
- package/dist/state/wordformat.d.ts +1 -1
- package/dist/state/wordformat.d.ts.map +1 -1
- package/dist/state/wordformat.js +60 -1
- package/dist/state/wordformat.js.map +1 -1
- package/dist/v3/document.d.ts +130 -7
- package/dist/v3/document.d.ts.map +1 -1
- package/dist/v3/document.js +172 -30
- package/dist/v3/document.js.map +1 -1
- package/dist/v3/index.d.ts +1 -1
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +1 -1
- 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 +22 -4
- package/dist/v3/resolve.js.map +1 -1
- package/dist/v3/schema.d.ts +2 -0
- package/dist/v3/schema.d.ts.map +1 -1
- package/dist/v3/schema.js +89 -3
- package/dist/v3/schema.js.map +1 -1
- package/package.json +1 -1
- package/src/state/commands.ts +41 -4
- package/src/state/comments.ts +11 -3
- package/src/state/controls.ts +1596 -0
- package/src/state/editor.ts +291 -47
- 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 +123 -0
- 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/review.ts +171 -19
- package/src/state/sections.ts +571 -0
- package/src/state/state.ts +5 -0
- package/src/state/stories.ts +285 -0
- package/src/state/tables.ts +2 -1
- package/src/state/track.ts +65 -10
- package/src/state/word.ts +275 -12
- package/src/state/wordclip.ts +211 -29
- package/src/state/wordformat.ts +61 -1
- package/src/v3/document.ts +326 -41
- package/src/v3/index.ts +7 -0
- package/src/v3/resolve.ts +33 -9
- package/src/v3/schema.ts +92 -3
package/src/state/editor.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { DocumentSettings, FoliDocumentV2 } from "../document.js";
|
|
2
|
-
import type
|
|
2
|
+
import { type Attrs, type Node, Slice } from "../model/index.js";
|
|
3
3
|
import type { Alignment } from "../schema.js";
|
|
4
4
|
import {
|
|
5
5
|
createDocument,
|
|
@@ -37,10 +37,22 @@ import {
|
|
|
37
37
|
replyToComment,
|
|
38
38
|
resolveComment,
|
|
39
39
|
} from "./comments.js";
|
|
40
|
+
import {
|
|
41
|
+
type ControlValue,
|
|
42
|
+
type ControlValueInput,
|
|
43
|
+
controlsIn,
|
|
44
|
+
controlsPlugin,
|
|
45
|
+
controlValues,
|
|
46
|
+
setControlValue,
|
|
47
|
+
} from "./controls.js";
|
|
48
|
+
import { fieldsPlugin } from "./fields.js";
|
|
49
|
+
import type { FieldLayout, FieldsReport, UpdateFieldsOptions } from "./fieldupdate.js";
|
|
50
|
+
import type { FindMatch, FindOptions } from "./find.js";
|
|
40
51
|
import { setAlign, setHighlight, setTextColor } from "./format.js";
|
|
41
52
|
import { history, redo, undo } from "./history.js";
|
|
42
53
|
import { removeLink, setLink } from "./links.js";
|
|
43
54
|
import { liftListItem, listKeymap, sinkListItem, toggleList, wrapInList } from "./lists.js";
|
|
55
|
+
import { notesPlugin } from "./notes.js";
|
|
44
56
|
import {
|
|
45
57
|
acceptAllChanges,
|
|
46
58
|
acceptChange,
|
|
@@ -49,8 +61,10 @@ import {
|
|
|
49
61
|
rejectAllChanges,
|
|
50
62
|
rejectChange,
|
|
51
63
|
} from "./review.js";
|
|
52
|
-
import {
|
|
64
|
+
import type { StorySide, StoryVariant } from "./sections.js";
|
|
65
|
+
import { Selection, TextSelection } from "./selection.js";
|
|
53
66
|
import { type Command, EditorState, type Plugin, type Transaction } from "./state.js";
|
|
67
|
+
import { StoryEditing } from "./stories.js";
|
|
54
68
|
import { tableKeymap } from "./tables.js";
|
|
55
69
|
import {
|
|
56
70
|
type ReviewView,
|
|
@@ -61,7 +75,7 @@ import {
|
|
|
61
75
|
trackState,
|
|
62
76
|
trackTransaction,
|
|
63
77
|
} from "./track.js";
|
|
64
|
-
import { type WordEnvelope, wordContext, wordPlugin } from "./word.js";
|
|
78
|
+
import { storyBody, type WordContext, type WordEnvelope, wordContext, wordPlugin } from "./word.js";
|
|
65
79
|
import type { ListKind } from "./wordformat.js";
|
|
66
80
|
|
|
67
81
|
/**
|
|
@@ -88,6 +102,72 @@ export interface EditorOptions {
|
|
|
88
102
|
|
|
89
103
|
export type EditorListener = (state: EditorState, transactions: readonly Transaction[]) => void;
|
|
90
104
|
|
|
105
|
+
/** A header, footer or note open for editing on its page. */
|
|
106
|
+
export interface OpenStory {
|
|
107
|
+
id: string;
|
|
108
|
+
side: StorySide | "note";
|
|
109
|
+
variant: StoryVariant;
|
|
110
|
+
/** The section it was opened from (a linked one shows an earlier section's story). */
|
|
111
|
+
section: number;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** A place to show: `from..to` of the body (`story` null) or of a story. */
|
|
115
|
+
export interface DocumentTarget {
|
|
116
|
+
story: string | null;
|
|
117
|
+
from: number;
|
|
118
|
+
to: number;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** What find shows on the pages. */
|
|
122
|
+
export interface FoundMatches extends FindOptions {
|
|
123
|
+
query: string;
|
|
124
|
+
matches: readonly FindMatch[];
|
|
125
|
+
/** The one marked apart, if any. */
|
|
126
|
+
current: FindMatch | null;
|
|
127
|
+
/** Scroll to `current`: on a search or a step, not when the document changed under it. */
|
|
128
|
+
scroll?: boolean;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The commands Word has keys for, answered by what the app shows for them. */
|
|
132
|
+
export type NavigationRequest =
|
|
133
|
+
| "find"
|
|
134
|
+
| "replace"
|
|
135
|
+
| "goTo"
|
|
136
|
+
| "next"
|
|
137
|
+
| "previous"
|
|
138
|
+
| "bookmarks"
|
|
139
|
+
| "outline";
|
|
140
|
+
|
|
141
|
+
/** What opens stories on the pages: the view of the editor's document. */
|
|
142
|
+
export interface StoryView {
|
|
143
|
+
readonly open: OpenStory | null;
|
|
144
|
+
/** Opens the header or footer of a page (default: the selection's). Whether it is open now. */
|
|
145
|
+
edit(side: StorySide, page?: number): boolean;
|
|
146
|
+
/** Opens a note where it is: `id`, or the one whose reference is at the selection. */
|
|
147
|
+
editNote(id?: string): boolean;
|
|
148
|
+
/** Back from the note being edited to its reference, the caret after it. */
|
|
149
|
+
toReference(): boolean;
|
|
150
|
+
close(): void;
|
|
151
|
+
/** The pages as last laid out, for fields; null when there are none. */
|
|
152
|
+
readonly fieldLayout: FieldLayout | null;
|
|
153
|
+
/**
|
|
154
|
+
* Updates fields with the pages in view (of the body, or of `editor`, a story's), again when
|
|
155
|
+
* that moved them. One undo step.
|
|
156
|
+
*/
|
|
157
|
+
updateFields(options?: UpdateFieldsOptions, editor?: Editor): FieldsReport;
|
|
158
|
+
/** Marks what find found on the pages (never in the document, not printed); null: nothing. */
|
|
159
|
+
showMatches(found: FoundMatches | null): void;
|
|
160
|
+
/**
|
|
161
|
+
* Selects `target` and scrolls to it, opening its story where it is, or leaving the open one.
|
|
162
|
+
* `focus`: the document takes the focus. False when no page shows it.
|
|
163
|
+
*/
|
|
164
|
+
reveal(target: DocumentTarget, options?: { focus?: boolean }): boolean;
|
|
165
|
+
/** The caret to the start of page `page` (1-based), scrolled to. False when there is none. */
|
|
166
|
+
goToPage(page: number, options?: { focus?: boolean }): boolean;
|
|
167
|
+
/** Focuses the document, or the story open in it. */
|
|
168
|
+
focus(): void;
|
|
169
|
+
}
|
|
170
|
+
|
|
91
171
|
/** A headless editor: state, commands, history and change notifications, no DOM. */
|
|
92
172
|
export interface Editor {
|
|
93
173
|
readonly state: EditorState;
|
|
@@ -169,7 +249,14 @@ export interface Editor {
|
|
|
169
249
|
deleteComment(id: number): boolean;
|
|
170
250
|
/** Deletes every comment. */
|
|
171
251
|
deleteAllComments(): boolean;
|
|
252
|
+
/**
|
|
253
|
+
* Fills the content controls with this id or tag, here and in the stories (from the body's
|
|
254
|
+
* editor): one undo step, tracked when tracking is on. False when none could take it.
|
|
255
|
+
*/
|
|
256
|
+
setControlValue(target: string, value: ControlValueInput): boolean;
|
|
172
257
|
};
|
|
258
|
+
/** The content controls' values, the stories' after the body's (from the body's editor). */
|
|
259
|
+
controlValues(): ControlValue[];
|
|
173
260
|
/** Whether edits are recorded as tracked changes. */
|
|
174
261
|
readonly trackingChanges: boolean;
|
|
175
262
|
isMarkActive(type: string): boolean;
|
|
@@ -178,6 +265,22 @@ export interface Editor {
|
|
|
178
265
|
/** Replaces the whole document (an older one migrated) and resets history. */
|
|
179
266
|
setDocument(document: EditorDocument): void;
|
|
180
267
|
subscribe(listener: EditorListener): () => void;
|
|
268
|
+
/**
|
|
269
|
+
* An editor over a story (a header, footer or note) by id, null when there is none. It has its
|
|
270
|
+
* own selection, and its changes go through this editor: one undo history, in `state.doc` order
|
|
271
|
+
* of the changes. On a story's editor `getDocument` gives the story as a document's content and
|
|
272
|
+
* `setDocument` replaces its content.
|
|
273
|
+
*/
|
|
274
|
+
story(id: string): Editor | null;
|
|
275
|
+
/** Set by the view showing the pages: what opens a story there, shared with stories' editors. */
|
|
276
|
+
storyView: StoryView | null;
|
|
277
|
+
/** The view opened or closed a story: tells the editor's listeners (no transaction). */
|
|
278
|
+
storyChanged(): void;
|
|
279
|
+
/**
|
|
280
|
+
* Set by the app's navigation UI: what Word's keys for it do. The view asks it first and leaves
|
|
281
|
+
* a key to the browser when it answers false, or when there is none.
|
|
282
|
+
*/
|
|
283
|
+
navigation: ((request: NavigationRequest) => boolean) | null;
|
|
181
284
|
}
|
|
182
285
|
|
|
183
286
|
/** Word's page when a section leaves it out: Letter, 1 inch margins. */
|
|
@@ -212,58 +315,55 @@ function load(document: EditorDocument): Loaded {
|
|
|
212
315
|
return { doc: parseContent(content), settings: sectionSettings(envelope.section), envelope };
|
|
213
316
|
}
|
|
214
317
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
trackPlugin(track),
|
|
229
|
-
commentsPlugin(),
|
|
230
|
-
tableKeymap(),
|
|
231
|
-
listKeymap(),
|
|
232
|
-
baseKeymap(),
|
|
233
|
-
];
|
|
234
|
-
let state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
|
|
235
|
-
const listeners = new Set<EditorListener>();
|
|
318
|
+
/** What differs between the editor of the body and of a story. */
|
|
319
|
+
interface EditorParts {
|
|
320
|
+
state(): EditorState;
|
|
321
|
+
dispatch(tr: Transaction): void;
|
|
322
|
+
settings(): DocumentSettings;
|
|
323
|
+
getDocument(): FoliDocument;
|
|
324
|
+
setDocument(document: EditorDocument): void;
|
|
325
|
+
subscribe(listener: EditorListener): () => void;
|
|
326
|
+
story(id: string): Editor | null;
|
|
327
|
+
/** The view opening stories, and the navigation UI: one for the body and its stories. */
|
|
328
|
+
views: { current: StoryView | null; navigation: Editor["navigation"] };
|
|
329
|
+
storyChanged(): void;
|
|
330
|
+
}
|
|
236
331
|
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
const
|
|
243
|
-
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
|
|
332
|
+
function makeEditor(parts: EditorParts): Editor {
|
|
333
|
+
const { dispatch } = parts;
|
|
334
|
+
const run = (command: Command) => command(parts.state(), dispatch);
|
|
335
|
+
/** This editor and, from the body's, those of the stories holding controls. */
|
|
336
|
+
const withStories = (): { editor: Editor; story?: string }[] => {
|
|
337
|
+
const state = parts.state();
|
|
338
|
+
const context = storyBody(state) ? null : wordContext(state);
|
|
339
|
+
const held = [...(context?.stories ?? [])].filter(([, s]) => controlsIn(s.doc).length);
|
|
340
|
+
return [
|
|
341
|
+
{ editor: self },
|
|
342
|
+
...held.flatMap(([story]) => {
|
|
343
|
+
const editor = parts.story(story);
|
|
344
|
+
return editor ? [{ editor, story }] : [];
|
|
345
|
+
}),
|
|
346
|
+
];
|
|
247
347
|
};
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
return {
|
|
348
|
+
const self: Editor = {
|
|
251
349
|
get settings() {
|
|
252
|
-
return
|
|
350
|
+
return parts.settings();
|
|
253
351
|
},
|
|
254
352
|
get state() {
|
|
255
|
-
return state;
|
|
353
|
+
return parts.state();
|
|
256
354
|
},
|
|
257
355
|
dispatch,
|
|
258
356
|
run,
|
|
259
|
-
can: (command) => command(state),
|
|
357
|
+
can: (command) => command(parts.state()),
|
|
260
358
|
handleKey(key) {
|
|
359
|
+
const state = parts.state();
|
|
261
360
|
return state.plugins.some((plugin) => {
|
|
262
361
|
const command = plugin.spec.keymap?.[key];
|
|
263
362
|
return command ? command(state, dispatch) : false;
|
|
264
363
|
});
|
|
265
364
|
},
|
|
266
365
|
select(anchor, head = anchor) {
|
|
366
|
+
const state = parts.state();
|
|
267
367
|
const size = state.doc.content.size;
|
|
268
368
|
const clamp = (pos: number) => Math.max(0, Math.min(pos, size));
|
|
269
369
|
dispatch(state.tr.setSelection(TextSelection.create(state.doc, clamp(anchor), clamp(head))));
|
|
@@ -306,24 +406,168 @@ export function createEditor({
|
|
|
306
406
|
resolveComment: (id, done) => run(resolveComment(id, done)),
|
|
307
407
|
deleteComment: (id) => run(deleteComment(id)),
|
|
308
408
|
deleteAllComments: () => run(deleteAllComments),
|
|
409
|
+
setControlValue(target, value) {
|
|
410
|
+
let done = false;
|
|
411
|
+
for (const { editor } of withStories())
|
|
412
|
+
if (editor.run(setControlValue(target, value, done))) done = true;
|
|
413
|
+
return done;
|
|
414
|
+
},
|
|
309
415
|
},
|
|
416
|
+
controlValues: () =>
|
|
417
|
+
withStories().flatMap(({ editor, story }) =>
|
|
418
|
+
controlValues(editor.state).map((value) => (story ? { ...value, story } : value)),
|
|
419
|
+
),
|
|
310
420
|
get trackingChanges() {
|
|
311
|
-
return trackState(state)?.on ?? false;
|
|
421
|
+
return trackState(parts.state())?.on ?? false;
|
|
422
|
+
},
|
|
423
|
+
isMarkActive: (type) => isMarkActive(parts.state(), type),
|
|
424
|
+
getDocument: parts.getDocument,
|
|
425
|
+
setDocument: parts.setDocument,
|
|
426
|
+
subscribe: parts.subscribe,
|
|
427
|
+
story: parts.story,
|
|
428
|
+
get storyView() {
|
|
429
|
+
return parts.views.current;
|
|
312
430
|
},
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
431
|
+
set storyView(view) {
|
|
432
|
+
parts.views.current = view;
|
|
433
|
+
},
|
|
434
|
+
storyChanged: parts.storyChanged,
|
|
435
|
+
get navigation() {
|
|
436
|
+
return parts.views.navigation;
|
|
437
|
+
},
|
|
438
|
+
set navigation(handler) {
|
|
439
|
+
parts.views.navigation = handler;
|
|
440
|
+
},
|
|
441
|
+
};
|
|
442
|
+
return self;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
export function createEditor({
|
|
446
|
+
document = createDocument(),
|
|
447
|
+
plugins = [],
|
|
448
|
+
author = "Author",
|
|
449
|
+
trackChanges = false,
|
|
450
|
+
reviewView = "markup",
|
|
451
|
+
}: EditorOptions = {}): Editor {
|
|
452
|
+
let loaded = load(document);
|
|
453
|
+
let track: TrackState = { on: trackChanges, author, view: reviewView };
|
|
454
|
+
const pluginsFor = ({ envelope }: Loaded) => [
|
|
455
|
+
...plugins,
|
|
456
|
+
history(),
|
|
457
|
+
wordPlugin(envelope),
|
|
458
|
+
trackPlugin(track),
|
|
459
|
+
fieldsPlugin(),
|
|
460
|
+
controlsPlugin(),
|
|
461
|
+
commentsPlugin(),
|
|
462
|
+
notesPlugin(),
|
|
463
|
+
tableKeymap(),
|
|
464
|
+
listKeymap(),
|
|
465
|
+
baseKeymap(),
|
|
466
|
+
];
|
|
467
|
+
let state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
|
|
468
|
+
const listeners = new Set<EditorListener>();
|
|
469
|
+
const views: EditorParts["views"] = { current: null, navigation: null };
|
|
470
|
+
/** The stories being edited, by id. */
|
|
471
|
+
const stories = new Map<string, { editing: StoryEditing; editor: Editor }>();
|
|
472
|
+
|
|
473
|
+
const notify = (transactions: readonly Transaction[]) => {
|
|
474
|
+
for (const listener of listeners) listener(state, transactions);
|
|
475
|
+
};
|
|
476
|
+
const storyChanged = () => notify([]);
|
|
477
|
+
const dispatch = (tr: Transaction) => {
|
|
478
|
+
// With tracking on, the edit is rebuilt as tracked changes; one that cannot be is not made.
|
|
479
|
+
const tracked = trackTransaction(state, tr);
|
|
480
|
+
if (!tracked) return;
|
|
481
|
+
const result = state.applyTransaction(tracked);
|
|
482
|
+
state = result.state;
|
|
483
|
+
if (!result.transactions.length) return;
|
|
484
|
+
// The stories first: a view of the body redraws them from what they now are.
|
|
485
|
+
for (const [id, { editing }] of stories) {
|
|
486
|
+
editing.sync(result.transactions);
|
|
487
|
+
if (!editing.alive) stories.delete(id);
|
|
488
|
+
}
|
|
489
|
+
notify(result.transactions);
|
|
490
|
+
};
|
|
491
|
+
const run = (command: Command) => command(state, dispatch);
|
|
492
|
+
let current = { section: loaded.envelope.section, settings: loaded.settings };
|
|
493
|
+
const settings = () => {
|
|
494
|
+
// The last section's page setup, as it is now (`setSectionSetup` changes it).
|
|
495
|
+
const section = wordContext(state)?.envelope.section ?? current.section;
|
|
496
|
+
if (section !== current.section) current = { section, settings: sectionSettings(section) };
|
|
497
|
+
return current.settings;
|
|
498
|
+
};
|
|
499
|
+
const getDocument = (): FoliDocument => ({
|
|
500
|
+
...structuredClone(wordContext(state)?.envelope ?? loaded.envelope),
|
|
501
|
+
content: state.doc.toJSON(),
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
const storyEditor = (id: string): Editor => {
|
|
505
|
+
const editing = new StoryEditing(id, {
|
|
506
|
+
get state() {
|
|
507
|
+
return state;
|
|
508
|
+
},
|
|
509
|
+
run,
|
|
510
|
+
can: (command) => command(state),
|
|
511
|
+
dispatch,
|
|
512
|
+
plugins: (context: WordContext, value) => [
|
|
513
|
+
...plugins,
|
|
514
|
+
wordPlugin(context.envelope, context),
|
|
515
|
+
trackPlugin(value ?? track),
|
|
516
|
+
fieldsPlugin(),
|
|
517
|
+
controlsPlugin(),
|
|
518
|
+
commentsPlugin(),
|
|
519
|
+
tableKeymap(),
|
|
520
|
+
listKeymap(),
|
|
521
|
+
baseKeymap(),
|
|
522
|
+
],
|
|
523
|
+
});
|
|
524
|
+
const editor = makeEditor({
|
|
525
|
+
state: () => editing.state,
|
|
526
|
+
dispatch: editing.dispatch,
|
|
527
|
+
settings,
|
|
528
|
+
getDocument: () => ({ ...getDocument(), content: editing.state.doc.toJSON() }),
|
|
529
|
+
setDocument(next) {
|
|
530
|
+
const { doc } = load(next);
|
|
531
|
+
const tr = editing.state.tr.replace(
|
|
532
|
+
0,
|
|
533
|
+
editing.state.doc.content.size,
|
|
534
|
+
new Slice(doc.content, 0, 0),
|
|
535
|
+
);
|
|
536
|
+
editing.dispatch(tr.setSelection(Selection.atStart(tr.doc)));
|
|
537
|
+
},
|
|
538
|
+
subscribe(listener) {
|
|
539
|
+
editing.listeners.add(listener);
|
|
540
|
+
return () => editing.listeners.delete(listener);
|
|
541
|
+
},
|
|
542
|
+
story,
|
|
543
|
+
views,
|
|
544
|
+
storyChanged,
|
|
545
|
+
});
|
|
546
|
+
stories.set(id, { editing, editor });
|
|
547
|
+
return editor;
|
|
548
|
+
};
|
|
549
|
+
const story = (id: string): Editor | null =>
|
|
550
|
+
wordContext(state)?.stories.has(id) ? (stories.get(id)?.editor ?? storyEditor(id)) : null;
|
|
551
|
+
|
|
552
|
+
return makeEditor({
|
|
553
|
+
state: () => state,
|
|
554
|
+
dispatch,
|
|
555
|
+
settings,
|
|
556
|
+
getDocument,
|
|
318
557
|
setDocument(next) {
|
|
319
558
|
track = trackState(state) ?? track;
|
|
320
559
|
loaded = load(next);
|
|
321
560
|
state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
|
|
561
|
+
for (const { editing } of stories.values()) editing.retire();
|
|
562
|
+
stories.clear();
|
|
322
563
|
notify([]);
|
|
323
564
|
},
|
|
324
565
|
subscribe(listener) {
|
|
325
566
|
listeners.add(listener);
|
|
326
567
|
return () => listeners.delete(listener);
|
|
327
568
|
},
|
|
328
|
-
|
|
569
|
+
story,
|
|
570
|
+
views,
|
|
571
|
+
storyChanged,
|
|
572
|
+
});
|
|
329
573
|
}
|