@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.
Files changed (153) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/model/schema.d.ts +9 -1
  3. package/dist/model/schema.d.ts.map +1 -1
  4. package/dist/model/schema.js +13 -3
  5. package/dist/model/schema.js.map +1 -1
  6. package/dist/state/commands.d.ts +10 -1
  7. package/dist/state/commands.d.ts.map +1 -1
  8. package/dist/state/commands.js +125 -40
  9. package/dist/state/commands.js.map +1 -1
  10. package/dist/state/comments.d.ts.map +1 -1
  11. package/dist/state/comments.js +15 -6
  12. package/dist/state/comments.js.map +1 -1
  13. package/dist/state/controls.d.ts +128 -0
  14. package/dist/state/controls.d.ts.map +1 -0
  15. package/dist/state/controls.js +1459 -0
  16. package/dist/state/controls.js.map +1 -0
  17. package/dist/state/editor.d.ts +86 -1
  18. package/dist/state/editor.d.ts.map +1 -1
  19. package/dist/state/editor.js +169 -41
  20. package/dist/state/editor.js.map +1 -1
  21. package/dist/state/fields.d.ts +79 -0
  22. package/dist/state/fields.d.ts.map +1 -0
  23. package/dist/state/fields.js +398 -0
  24. package/dist/state/fields.js.map +1 -0
  25. package/dist/state/fieldupdate.d.ts +178 -0
  26. package/dist/state/fieldupdate.d.ts.map +1 -0
  27. package/dist/state/fieldupdate.js +1574 -0
  28. package/dist/state/fieldupdate.js.map +1 -0
  29. package/dist/state/find.d.ts +68 -0
  30. package/dist/state/find.d.ts.map +1 -0
  31. package/dist/state/find.js +397 -0
  32. package/dist/state/find.js.map +1 -0
  33. package/dist/state/history.d.ts +12 -1
  34. package/dist/state/history.d.ts.map +1 -1
  35. package/dist/state/history.js +43 -7
  36. package/dist/state/history.js.map +1 -1
  37. package/dist/state/index.d.ts +15 -5
  38. package/dist/state/index.d.ts.map +1 -1
  39. package/dist/state/index.js +13 -3
  40. package/dist/state/index.js.map +1 -1
  41. package/dist/state/links.d.ts.map +1 -1
  42. package/dist/state/links.js +30 -19
  43. package/dist/state/links.js.map +1 -1
  44. package/dist/state/mark.d.ts.map +1 -1
  45. package/dist/state/mark.js +8 -1
  46. package/dist/state/mark.js.map +1 -1
  47. package/dist/state/navigation.d.ts +70 -0
  48. package/dist/state/navigation.d.ts.map +1 -0
  49. package/dist/state/navigation.js +226 -0
  50. package/dist/state/navigation.js.map +1 -0
  51. package/dist/state/notes.d.ts +94 -0
  52. package/dist/state/notes.d.ts.map +1 -0
  53. package/dist/state/notes.js +444 -0
  54. package/dist/state/notes.js.map +1 -0
  55. package/dist/state/pictures.d.ts +51 -0
  56. package/dist/state/pictures.d.ts.map +1 -0
  57. package/dist/state/pictures.js +140 -0
  58. package/dist/state/pictures.js.map +1 -0
  59. package/dist/state/review.d.ts +13 -4
  60. package/dist/state/review.d.ts.map +1 -1
  61. package/dist/state/review.js +223 -29
  62. package/dist/state/review.js.map +1 -1
  63. package/dist/state/sections.d.ts +101 -0
  64. package/dist/state/sections.d.ts.map +1 -0
  65. package/dist/state/sections.js +472 -0
  66. package/dist/state/sections.js.map +1 -0
  67. package/dist/state/selection.d.ts +12 -1
  68. package/dist/state/selection.d.ts.map +1 -1
  69. package/dist/state/selection.js +12 -0
  70. package/dist/state/selection.js.map +1 -1
  71. package/dist/state/state.d.ts +2 -0
  72. package/dist/state/state.d.ts.map +1 -1
  73. package/dist/state/state.js +4 -0
  74. package/dist/state/state.js.map +1 -1
  75. package/dist/state/stories.d.ts +41 -0
  76. package/dist/state/stories.d.ts.map +1 -0
  77. package/dist/state/stories.js +243 -0
  78. package/dist/state/stories.js.map +1 -0
  79. package/dist/state/tablecommands.d.ts +77 -0
  80. package/dist/state/tablecommands.d.ts.map +1 -0
  81. package/dist/state/tablecommands.js +675 -0
  82. package/dist/state/tablecommands.js.map +1 -0
  83. package/dist/state/tables.d.ts +110 -0
  84. package/dist/state/tables.d.ts.map +1 -0
  85. package/dist/state/tables.js +730 -0
  86. package/dist/state/tables.js.map +1 -0
  87. package/dist/state/track.d.ts +23 -0
  88. package/dist/state/track.d.ts.map +1 -1
  89. package/dist/state/track.js +113 -15
  90. package/dist/state/track.js.map +1 -1
  91. package/dist/state/word.d.ts +78 -16
  92. package/dist/state/word.d.ts.map +1 -1
  93. package/dist/state/word.js +231 -11
  94. package/dist/state/word.js.map +1 -1
  95. package/dist/state/wordclip.d.ts +15 -6
  96. package/dist/state/wordclip.d.ts.map +1 -1
  97. package/dist/state/wordclip.js +213 -18
  98. package/dist/state/wordclip.js.map +1 -1
  99. package/dist/state/wordformat.d.ts +10 -2
  100. package/dist/state/wordformat.d.ts.map +1 -1
  101. package/dist/state/wordformat.js +98 -17
  102. package/dist/state/wordformat.js.map +1 -1
  103. package/dist/v3/document.d.ts +237 -7
  104. package/dist/v3/document.d.ts.map +1 -1
  105. package/dist/v3/document.js +314 -15
  106. package/dist/v3/document.js.map +1 -1
  107. package/dist/v3/index.d.ts +2 -2
  108. package/dist/v3/index.d.ts.map +1 -1
  109. package/dist/v3/index.js +2 -2
  110. package/dist/v3/index.js.map +1 -1
  111. package/dist/v3/migrate.d.ts +2 -2
  112. package/dist/v3/migrate.d.ts.map +1 -1
  113. package/dist/v3/resolve.d.ts +57 -5
  114. package/dist/v3/resolve.d.ts.map +1 -1
  115. package/dist/v3/resolve.js +224 -16
  116. package/dist/v3/resolve.js.map +1 -1
  117. package/dist/v3/schema.d.ts +6 -0
  118. package/dist/v3/schema.d.ts.map +1 -1
  119. package/dist/v3/schema.js +254 -8
  120. package/dist/v3/schema.js.map +1 -1
  121. package/package.json +1 -1
  122. package/src/model/schema.ts +18 -2
  123. package/src/state/commands.ts +121 -32
  124. package/src/state/comments.ts +14 -6
  125. package/src/state/controls.ts +1596 -0
  126. package/src/state/editor.ts +292 -46
  127. package/src/state/fields.ts +432 -0
  128. package/src/state/fieldupdate.ts +1770 -0
  129. package/src/state/find.ts +510 -0
  130. package/src/state/history.ts +51 -7
  131. package/src/state/index.ts +157 -0
  132. package/src/state/links.ts +27 -17
  133. package/src/state/mark.ts +8 -1
  134. package/src/state/navigation.ts +305 -0
  135. package/src/state/notes.ts +519 -0
  136. package/src/state/numeric-pictures.word.json +172 -0
  137. package/src/state/pictures.ts +168 -0
  138. package/src/state/review.ts +265 -31
  139. package/src/state/sections.ts +571 -0
  140. package/src/state/selection.ts +15 -1
  141. package/src/state/state.ts +5 -0
  142. package/src/state/stories.ts +285 -0
  143. package/src/state/tablecommands.ts +741 -0
  144. package/src/state/tables.ts +761 -0
  145. package/src/state/track.ts +128 -16
  146. package/src/state/word.ts +276 -13
  147. package/src/state/wordclip.ts +231 -32
  148. package/src/state/wordformat.ts +96 -14
  149. package/src/v3/document.ts +572 -16
  150. package/src/v3/index.ts +21 -0
  151. package/src/v3/migrate.ts +2 -2
  152. package/src/v3/resolve.ts +289 -21
  153. package/src/v3/schema.ts +278 -9
@@ -1,5 +1,5 @@
1
1
  import type { DocumentSettings, FoliDocumentV2 } from "../document.js";
2
- import type { Attrs, Node } from "../model/index.js";
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,11 @@ import {
49
61
  rejectAllChanges,
50
62
  rejectChange,
51
63
  } from "./review.js";
52
- import { TextSelection } from "./selection.js";
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";
68
+ import { tableKeymap } from "./tables.js";
54
69
  import {
55
70
  type ReviewView,
56
71
  setReviewView,
@@ -60,7 +75,7 @@ import {
60
75
  trackState,
61
76
  trackTransaction,
62
77
  } from "./track.js";
63
- import { type WordEnvelope, wordContext, wordPlugin } from "./word.js";
78
+ import { storyBody, type WordContext, type WordEnvelope, wordContext, wordPlugin } from "./word.js";
64
79
  import type { ListKind } from "./wordformat.js";
65
80
 
66
81
  /**
@@ -87,6 +102,72 @@ export interface EditorOptions {
87
102
 
88
103
  export type EditorListener = (state: EditorState, transactions: readonly Transaction[]) => void;
89
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
+
90
171
  /** A headless editor: state, commands, history and change notifications, no DOM. */
91
172
  export interface Editor {
92
173
  readonly state: EditorState;
@@ -168,7 +249,14 @@ export interface Editor {
168
249
  deleteComment(id: number): boolean;
169
250
  /** Deletes every comment. */
170
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;
171
257
  };
258
+ /** The content controls' values, the stories' after the body's (from the body's editor). */
259
+ controlValues(): ControlValue[];
172
260
  /** Whether edits are recorded as tracked changes. */
173
261
  readonly trackingChanges: boolean;
174
262
  isMarkActive(type: string): boolean;
@@ -177,6 +265,22 @@ export interface Editor {
177
265
  /** Replaces the whole document (an older one migrated) and resets history. */
178
266
  setDocument(document: EditorDocument): void;
179
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;
180
284
  }
181
285
 
182
286
  /** Word's page when a section leaves it out: Letter, 1 inch margins. */
@@ -211,57 +315,55 @@ function load(document: EditorDocument): Loaded {
211
315
  return { doc: parseContent(content), settings: sectionSettings(envelope.section), envelope };
212
316
  }
213
317
 
214
- export function createEditor({
215
- document = createDocument(),
216
- plugins = [],
217
- author = "Author",
218
- trackChanges = false,
219
- reviewView = "markup",
220
- }: EditorOptions = {}): Editor {
221
- let loaded = load(document);
222
- let track: TrackState = { on: trackChanges, author, view: reviewView };
223
- const pluginsFor = ({ envelope }: Loaded) => [
224
- ...plugins,
225
- history(),
226
- wordPlugin(envelope),
227
- trackPlugin(track),
228
- commentsPlugin(),
229
- listKeymap(),
230
- baseKeymap(),
231
- ];
232
- let state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
233
- 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
+ }
234
331
 
235
- const notify = (transactions: readonly Transaction[]) => {
236
- for (const listener of listeners) listener(state, transactions);
237
- };
238
- const dispatch = (tr: Transaction) => {
239
- // With tracking on, the edit is rebuilt as tracked changes; one that cannot be is not made.
240
- const tracked = trackTransaction(state, tr);
241
- if (!tracked) return;
242
- const result = state.applyTransaction(tracked);
243
- state = result.state;
244
- if (result.transactions.length) notify(result.transactions);
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
+ ];
245
347
  };
246
- const run = (command: Command) => command(state, dispatch);
247
-
248
- return {
348
+ const self: Editor = {
249
349
  get settings() {
250
- return loaded.settings;
350
+ return parts.settings();
251
351
  },
252
352
  get state() {
253
- return state;
353
+ return parts.state();
254
354
  },
255
355
  dispatch,
256
356
  run,
257
- can: (command) => command(state),
357
+ can: (command) => command(parts.state()),
258
358
  handleKey(key) {
359
+ const state = parts.state();
259
360
  return state.plugins.some((plugin) => {
260
361
  const command = plugin.spec.keymap?.[key];
261
362
  return command ? command(state, dispatch) : false;
262
363
  });
263
364
  },
264
365
  select(anchor, head = anchor) {
366
+ const state = parts.state();
265
367
  const size = state.doc.content.size;
266
368
  const clamp = (pos: number) => Math.max(0, Math.min(pos, size));
267
369
  dispatch(state.tr.setSelection(TextSelection.create(state.doc, clamp(anchor), clamp(head))));
@@ -304,24 +406,168 @@ export function createEditor({
304
406
  resolveComment: (id, done) => run(resolveComment(id, done)),
305
407
  deleteComment: (id) => run(deleteComment(id)),
306
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
+ },
307
415
  },
416
+ controlValues: () =>
417
+ withStories().flatMap(({ editor, story }) =>
418
+ controlValues(editor.state).map((value) => (story ? { ...value, story } : value)),
419
+ ),
308
420
  get trackingChanges() {
309
- 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;
310
430
  },
311
- isMarkActive: (type) => isMarkActive(state, type),
312
- getDocument: () => ({
313
- ...structuredClone(wordContext(state)?.envelope ?? loaded.envelope),
314
- content: state.doc.toJSON(),
315
- }),
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,
316
557
  setDocument(next) {
317
558
  track = trackState(state) ?? track;
318
559
  loaded = load(next);
319
560
  state = EditorState.create({ doc: loaded.doc, plugins: pluginsFor(loaded) });
561
+ for (const { editing } of stories.values()) editing.retire();
562
+ stories.clear();
320
563
  notify([]);
321
564
  },
322
565
  subscribe(listener) {
323
566
  listeners.add(listener);
324
567
  return () => listeners.delete(listener);
325
568
  },
326
- };
569
+ story,
570
+ views,
571
+ storyChanged,
572
+ });
327
573
  }