@folijs/view 0.6.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.
Files changed (82) hide show
  1. package/CHANGELOG.md +161 -0
  2. package/README.md +1 -1
  3. package/dist/clipboard.d.ts +1 -51
  4. package/dist/clipboard.d.ts.map +1 -1
  5. package/dist/clipboard.js +65 -13
  6. package/dist/clipboard.js.map +1 -1
  7. package/dist/extension.d.ts +90 -0
  8. package/dist/extension.d.ts.map +1 -0
  9. package/dist/extension.js +152 -0
  10. package/dist/extension.js.map +1 -0
  11. package/dist/index.d.ts +2 -1
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/layout/flow.d.ts +1 -282
  15. package/dist/layout/flow.d.ts.map +1 -1
  16. package/dist/layout/flow.js +21 -4
  17. package/dist/layout/flow.js.map +1 -1
  18. package/dist/print.d.ts +1 -17
  19. package/dist/print.d.ts.map +1 -1
  20. package/dist/print.js +5 -1
  21. package/dist/print.js.map +1 -1
  22. package/dist/render.d.ts +1 -24
  23. package/dist/render.d.ts.map +1 -1
  24. package/dist/render.js +4 -1
  25. package/dist/render.js.map +1 -1
  26. package/dist/view.d.ts +34 -67
  27. package/dist/view.d.ts.map +1 -1
  28. package/dist/view.js +209 -24
  29. package/dist/view.js.map +1 -1
  30. package/dist/word/blocks.d.ts +0 -175
  31. package/dist/word/blocks.d.ts.map +1 -1
  32. package/dist/word/blocks.js +106 -24
  33. package/dist/word/blocks.js.map +1 -1
  34. package/dist/word/columns.d.ts +1 -22
  35. package/dist/word/columns.d.ts.map +1 -1
  36. package/dist/word/columns.js +15 -3
  37. package/dist/word/columns.js.map +1 -1
  38. package/dist/word/controls.d.ts +1 -62
  39. package/dist/word/controls.d.ts.map +1 -1
  40. package/dist/word/controls.js +6 -1
  41. package/dist/word/controls.js.map +1 -1
  42. package/dist/word/edit.d.ts +1 -83
  43. package/dist/word/edit.d.ts.map +1 -1
  44. package/dist/word/edit.js +183 -5
  45. package/dist/word/edit.js.map +1 -1
  46. package/dist/word/matches.d.ts +1 -22
  47. package/dist/word/matches.d.ts.map +1 -1
  48. package/dist/word/matches.js +6 -1
  49. package/dist/word/matches.js.map +1 -1
  50. package/dist/word/notes.d.ts +0 -103
  51. package/dist/word/notes.d.ts.map +1 -1
  52. package/dist/word/notes.js +15 -4
  53. package/dist/word/notes.js.map +1 -1
  54. package/dist/word/pages.d.ts +15 -10
  55. package/dist/word/pages.d.ts.map +1 -1
  56. package/dist/word/pages.js +3 -2
  57. package/dist/word/pages.js.map +1 -1
  58. package/dist/word/regions.d.ts +0 -111
  59. package/dist/word/regions.d.ts.map +1 -1
  60. package/dist/word/regions.js +19 -5
  61. package/dist/word/regions.js.map +1 -1
  62. package/dist/word/table.d.ts +1 -78
  63. package/dist/word/table.d.ts.map +1 -1
  64. package/dist/word/table.js +13 -3
  65. package/dist/word/table.js.map +1 -1
  66. package/package.json +6 -3
  67. package/src/clipboard.ts +91 -21
  68. package/src/extension.ts +267 -0
  69. package/src/index.ts +10 -0
  70. package/src/layout/flow.ts +59 -13
  71. package/src/print.ts +9 -2
  72. package/src/render.ts +6 -1
  73. package/src/view.ts +245 -40
  74. package/src/word/blocks.ts +124 -30
  75. package/src/word/columns.ts +19 -4
  76. package/src/word/controls.ts +13 -3
  77. package/src/word/edit.ts +207 -9
  78. package/src/word/matches.ts +6 -1
  79. package/src/word/notes.ts +19 -6
  80. package/src/word/pages.ts +19 -3
  81. package/src/word/regions.ts +27 -8
  82. package/src/word/table.ts +33 -8
package/src/view.ts CHANGED
@@ -5,18 +5,15 @@ import {
5
5
  CellSelection,
6
6
  type Command,
7
7
  type ControlRange,
8
- caretStop,
9
8
  cellsAsTable,
10
9
  closeHistory,
11
10
  controlAt,
12
11
  controlProperties,
13
- controlSpot,
14
12
  type DocumentSettings,
15
13
  type DocumentTarget,
16
14
  deleteBackward,
17
15
  deleteForward,
18
16
  deleteSelection,
19
- deleteSelectionKeepingTables,
20
17
  type Editor,
21
18
  type EditorState,
22
19
  ensureStory,
@@ -28,8 +25,6 @@ import {
28
25
  type FoundMatches,
29
26
  fieldCodesShown,
30
27
  fieldsIn,
31
- fitPaste,
32
- fitPastedV2,
33
28
  goToCellVertically,
34
29
  insertControlText,
35
30
  insertHardBreak,
@@ -38,7 +33,6 @@ import {
38
33
  insertPicture,
39
34
  insertText,
40
35
  isSafeHref,
41
- keepTablesApart,
42
36
  type Node as ModelNode,
43
37
  marksAt,
44
38
  type NavigationRequest,
@@ -55,17 +49,13 @@ import {
55
49
  type ResourceJSON,
56
50
  type ReviewView,
57
51
  redo,
58
- referencesIn,
59
52
  type SectionJSON,
60
53
  Selection,
61
54
  Slice,
62
55
  type StorySide,
63
- StoryStep,
64
56
  type StoryView,
65
- sameSpot,
66
57
  sectionSettings,
67
58
  setColumnWidth,
68
- setComments,
69
59
  setLink,
70
60
  setPictureSize,
71
61
  splitBlock,
@@ -76,16 +66,40 @@ import {
76
66
  toggleFieldCodes,
77
67
  toggleMark,
78
68
  trackState,
79
- typedMarks,
80
69
  type UpdateFieldsOptions,
81
70
  undo,
82
71
  updateFields,
83
- type WordContext,
84
72
  type WordSettingsJSON,
73
+ } from "@folijs/core";
74
+ import {
75
+ caretStop,
76
+ controlSpot,
77
+ deleteSelectionKeepingTables,
78
+ fitPaste,
79
+ fitPastedV2,
80
+ keepTablesApart,
81
+ referencesIn,
82
+ StoryStep,
83
+ sameSpot,
84
+ setComments,
85
+ typedMarks,
86
+ viewKeys,
87
+ type WordContext,
85
88
  withEnvelope,
86
89
  wordContext,
87
- } from "@folijs/core";
90
+ } from "@folijs/core/internal";
88
91
  import { docFromClipboard, FOLI_MIME, serializeRange } from "./clipboard.js";
92
+ import {
93
+ checkAttrs,
94
+ type Decoration,
95
+ type Drawn,
96
+ decorationsByBlock,
97
+ type PluginView,
98
+ reportError,
99
+ type ViewExtension,
100
+ type Views,
101
+ viewsOf,
102
+ } from "./extension.js";
89
103
  import {
90
104
  type BlockBox,
91
105
  contentHeight,
@@ -176,7 +190,10 @@ interface FrameState {
176
190
  key: string;
177
191
  }
178
192
 
179
- /** A table's measurement, in layout px from the table's top (drawn whole). */
193
+ /**
194
+ * A table's measurement, in layout px from the table's top (drawn whole).
195
+ * @internal
196
+ */
180
197
  export interface TableMeasure {
181
198
  rows: { top: number; bottom: number }[];
182
199
  /** The top of each of its units (by index into the units), null for one not shown (covered). */
@@ -700,6 +717,14 @@ export class EditorView {
700
717
  private readonly listeners: [string, EventListener][] = [];
701
718
  /** The frames, titles and pop-ups of content controls. */
702
719
  private readonly controlChrome: ControlChrome;
720
+ /** The editor's view extensions' node and mark views, decorations and plugin views. */
721
+ private readonly views: Views;
722
+ private readonly decorators: ViewExtension["decorations"][];
723
+ private decorations: readonly Decoration[] = [];
724
+ private decorationKey = "";
725
+ private pluginViews: PluginView[] = [];
726
+ /** What node views drew and the view still shows (their `destroy` when it no longer does). */
727
+ private readonly drawn: Drawn[] = [];
703
728
 
704
729
  constructor(
705
730
  readonly host: HTMLElement,
@@ -757,6 +782,10 @@ export class EditorView {
757
782
  this.observer.observe(host, { childList: true, characterData: true, subtree: true });
758
783
  // The picture resolver before the first render: no image drawn from a URL the app refuses.
759
784
  this.resource = options.resource ?? undefined;
785
+ const extensions = editor.extensions as readonly ViewExtension[];
786
+ this.views = viewsOf(extensions);
787
+ this.decorators = extensions.flatMap((e) => (e.decorations ? [e.decorations.bind(e)] : []));
788
+ this.decorate(this.state);
760
789
  this.render();
761
790
  this.setOptions(options);
762
791
 
@@ -781,6 +810,68 @@ export class EditorView {
781
810
  host.ownerDocument.addEventListener("selectionchange", this.onSelectionChange);
782
811
  host.ownerDocument.fonts?.addEventListener("loadingdone", this.onFontsLoaded);
783
812
  this.unsubscribe = editor.subscribe((state) => this.update(state));
813
+ for (const extension of extensions) {
814
+ try {
815
+ if (extension.view) this.pluginViews.push(extension.view(this));
816
+ } catch (error) {
817
+ reportError(error);
818
+ }
819
+ }
820
+ }
821
+
822
+ /** Calls `hook` for each plugin view, reporting what throws. */
823
+ private eachPluginView(hook: (view: PluginView) => void): void {
824
+ for (const view of this.pluginViews) {
825
+ try {
826
+ hook(view);
827
+ } catch (error) {
828
+ reportError(error);
829
+ }
830
+ }
831
+ }
832
+
833
+ /** Destroys node views' drawings the host no longer holds (all of them: `all`). */
834
+ private dropDrawn(all: boolean): void {
835
+ let kept = 0;
836
+ for (const drawn of this.drawn) {
837
+ if (!all && this.host.contains(drawn.box)) {
838
+ this.drawn[kept++] = drawn;
839
+ continue;
840
+ }
841
+ try {
842
+ drawn.view.destroy?.(drawn.root);
843
+ } catch (error) {
844
+ reportError(error);
845
+ }
846
+ }
847
+ this.drawn.length = kept;
848
+ }
849
+
850
+ /** Asks the extensions for `state`'s decorations; whether they changed. */
851
+ private decorate(state: EditorState): boolean {
852
+ if (!this.decorators.length) return false;
853
+ // A failing extension, or a decoration with a refused attribute, is reported and left out.
854
+ const decorations = this.decorators.flatMap((decorate) => {
855
+ try {
856
+ return (decorate?.(state) ?? []).filter((decoration) => {
857
+ try {
858
+ checkAttrs(decoration.attrs);
859
+ return true;
860
+ } catch (error) {
861
+ reportError(error);
862
+ return false;
863
+ }
864
+ });
865
+ } catch (error) {
866
+ reportError(error);
867
+ return [];
868
+ }
869
+ });
870
+ const key = JSON.stringify(decorations);
871
+ if (key === this.decorationKey) return false;
872
+ this.decorations = decorations;
873
+ this.decorationKey = key;
874
+ return true;
784
875
  }
785
876
 
786
877
  /** Adds a listener for the body's events: a story open on a page has its own view. */
@@ -793,6 +884,8 @@ export class EditorView {
793
884
  }
794
885
 
795
886
  destroy(): void {
887
+ this.eachPluginView((view) => view.destroy?.());
888
+ this.pluginViews = [];
796
889
  this.closeStory(false);
797
890
  this.showFound(null);
798
891
  this.controlChrome.destroy();
@@ -822,6 +915,7 @@ export class EditorView {
822
915
  host.style.removeProperty("overflow-wrap");
823
916
  if (!host.getAttribute("style")) host.removeAttribute("style");
824
917
  host.replaceChildren();
918
+ this.dropDrawn(true);
825
919
  }
826
920
 
827
921
  /** Focuses the host, shows the model selection in it and scrolls it into view. */
@@ -929,15 +1023,20 @@ export class EditorView {
929
1023
  const selectionChanged = !state.selection.eq(this.state.selection);
930
1024
  // Another document: the story being edited closes.
931
1025
  const lost = !!this.mount && wordContext(state)?.id !== wordContext(this.state)?.id;
1026
+ const previous = this.state;
932
1027
  this.state = state;
933
- if (docChanged || (storiesChanged && !this.redrawRegions())) this.render();
1028
+ // Decorations that changed redraw their blocks (the others are reused).
1029
+ const decorated = this.decorate(state);
1030
+ if (docChanged || decorated || (storiesChanged && !this.redrawRegions())) this.render();
934
1031
  if (lost) this.closeStory(true);
935
1032
  // The open story has its own editor, which hears nothing of this.
936
1033
  if (codesChanged) this.mount?.view.update(this.mount.view.editor.state);
937
1034
  this.showSelectedNode();
938
- this.writeSelection(docChanged);
1035
+ // Redrawn blocks may have held the DOM selection.
1036
+ this.writeSelection(docChanged || decorated);
939
1037
  // shortcut: scrolls for every change while focused; once remote or plugin changes exist, scroll only for local input (a transaction flag).
940
1038
  if (docChanged || selectionChanged) this.scrollToSelection();
1039
+ this.eachPluginView((view) => view.update?.(this, previous));
941
1040
  }
942
1041
 
943
1042
  /**
@@ -977,6 +1076,7 @@ export class EditorView {
977
1076
  private render(): void {
978
1077
  try {
979
1078
  this.renderPages();
1079
+ if (this.drawn.length) this.dropDrawn(false);
980
1080
  } catch (error) {
981
1081
  // What it had changed is not input: read back, it would render and fail again, for ever.
982
1082
  this.observer.takeRecords();
@@ -1017,6 +1117,8 @@ export class EditorView {
1017
1117
  imageSource(context.envelope.resources, this.resource ?? undefined),
1018
1118
  this.fieldCodes,
1019
1119
  );
1120
+ word.views = this.views;
1121
+ word.drawn = this.drawn;
1020
1122
  this.invalidateLayout();
1021
1123
  }
1022
1124
  const references = this.noteReferences();
@@ -1024,6 +1126,7 @@ export class EditorView {
1024
1126
  // The blocks the view renders (paragraphs in table cells among them) and the page flow's
1025
1127
  // items (top-level paragraphs and opaque blocks, and tables).
1026
1128
  const { units, items } = collect(doc, context.resolver);
1129
+ word.decorations = this.decorations.length ? decorationsByBlock(units, this.decorations) : [];
1027
1130
  word.prepare(
1028
1131
  units.map((unit) => unit.node),
1029
1132
  (i) => {
@@ -1346,6 +1449,15 @@ export class EditorView {
1346
1449
  const desc = blocks[item.unit] as WordBlockDesc;
1347
1450
  const contentStart = ((units[item.unit] as Unit).pos as number) + 1;
1348
1451
  const { section } = layout[i] as BlockLayout;
1452
+ // A node view splits where its part's line ends (layout px).
1453
+ if (desc.parts) {
1454
+ const { bottoms } = lines(desc);
1455
+ splitsOf.set(
1456
+ item.unit,
1457
+ fragments.slice(1).map((fragment) => bottoms[fragment.fromLine - 1] as number),
1458
+ );
1459
+ return;
1460
+ }
1349
1461
  splitsOf.set(
1350
1462
  item.unit,
1351
1463
  fragments.slice(1).map((fragment) => {
@@ -1952,7 +2064,10 @@ export class EditorView {
1952
2064
  search: for (const [i, desc] of this.blocks.entries()) {
1953
2065
  for (const [f, fragment] of desc.fragments.entries()) {
1954
2066
  if (fragment.closest(".foli-page") !== element) continue;
1955
- const start = (this.starts[i] as number) + 1 + (f ? (desc.splits[f - 1] as number) : 0);
2067
+ // A block leaf's parts (a node view split across pages) are all before it.
2068
+ const start = desc.node.isTextblock
2069
+ ? (this.starts[i] as number) + 1 + (f ? (desc.splits[f - 1] as number) : 0)
2070
+ : (this.starts[i] as number);
1956
2071
  const { doc } = this.state;
1957
2072
  this.editor.dispatch(
1958
2073
  this.state.tr.setSelection(
@@ -2029,11 +2144,10 @@ export class EditorView {
2029
2144
  }
2030
2145
 
2031
2146
  /**
2032
- * The keys of content controls: Alt+Down opens a list or date picker, and Left and Right stop on
2033
- * each side of a control's edge, as Word's caret does (browsers cannot see the difference).
2147
+ * Left and Right stop on each side of a content control's edge, as Word's caret does (browsers
2148
+ * cannot see the difference). Alt+Down, which opens a list or date picker, is in `viewKey`.
2034
2149
  */
2035
2150
  private controlKey(name: string): boolean {
2036
- if (name === "Alt-ArrowDown") return this.controlChrome.open();
2037
2151
  if (name !== "ArrowLeft" && name !== "ArrowRight") return false;
2038
2152
  const { selection, doc } = this.state;
2039
2153
  if (!(selection instanceof TextSelection) || !wordContext(this.state)?.envelope.controls)
@@ -2339,7 +2453,8 @@ export class EditorView {
2339
2453
  });
2340
2454
  wrapper.addEventListener("keydown", (event) => {
2341
2455
  if (event.isComposing || view.composing) return;
2342
- if (isNoteKey(event) && this.toReference()) {
2456
+ // Unless the story's view ran a user's binding of the key.
2457
+ if (!event.defaultPrevented && keyName(event) === NOTE_KEY && this.toReference()) {
2343
2458
  // Not on to the body, which would open the note again.
2344
2459
  event.preventDefault();
2345
2460
  event.stopPropagation();
@@ -2872,7 +2987,7 @@ export class EditorView {
2872
2987
  const range = host.ownerDocument.createRange();
2873
2988
  range.setStart(node, offset);
2874
2989
  const posOf = (i: number, f: number) =>
2875
- f === 0
2990
+ f === 0 || !(this.blocks[i] as BlockDesc).node.isTextblock
2876
2991
  ? (this.starts[i] as number)
2877
2992
  : (this.starts[i] as number) +
2878
2993
  1 +
@@ -3266,14 +3381,34 @@ export class EditorView {
3266
3381
  this.run(type.endsWith("Forward") ? deleteForward(unit) : deleteBackward(unit));
3267
3382
  };
3268
3383
 
3269
- /** Runs the first command bound to `name` that applies, through the view's dispatch. */
3384
+ /**
3385
+ * Runs the first command bound to `name` that applies, through the view's dispatch; the view's
3386
+ * own keys where core's `viewKeys` plugin stands (after the user's plugins, before the
3387
+ * built-in keymaps).
3388
+ */
3270
3389
  private runKey(name: string): boolean {
3271
3390
  return this.state.plugins.some((plugin) => {
3391
+ if (plugin.key === viewKeys) return this.viewKey(name);
3272
3392
  const command = plugin.spec.keymap?.[name];
3273
3393
  return command ? this.run(command) : false;
3274
3394
  });
3275
3395
  }
3276
3396
 
3397
+ /**
3398
+ * The view's own overridable keys (ADR-0008, `docs/keyboard.md`): they need the pages, the
3399
+ * stories or the app's navigation UI, so they are not core commands. A user keymap binding the
3400
+ * same name runs first.
3401
+ */
3402
+ private viewKey(name: string): boolean {
3403
+ return (
3404
+ this.documentEdgeKey(name) ||
3405
+ this.fieldKey(name) ||
3406
+ (name === "Alt-ArrowDown" && this.controlChrome.open()) ||
3407
+ this.noteKey(name) ||
3408
+ this.navigationKey(name)
3409
+ );
3410
+ }
3411
+
3277
3412
  // ---------- column borders (10.7c) ----------
3278
3413
 
3279
3414
  /** A column border being dragged: its table and column, the column's width, the press, the guide. */
@@ -3763,13 +3898,7 @@ export class EditorView {
3763
3898
  return;
3764
3899
  }
3765
3900
  const name = keyName(event);
3766
- if (
3767
- this.documentEdgeKey(name) ||
3768
- this.fieldKey(name) ||
3769
- this.controlKey(name) ||
3770
- this.noteKey(event, name) ||
3771
- this.navigationKey(name)
3772
- ) {
3901
+ if (this.controlKey(name)) {
3773
3902
  event.preventDefault();
3774
3903
  return;
3775
3904
  }
@@ -3826,8 +3955,8 @@ export class EditorView {
3826
3955
  };
3827
3956
 
3828
3957
  /** Word's keys for notes, and Shift+Alt+N from a reference to its note. */
3829
- private noteKey(event: KeyboardEvent, name: string): boolean {
3830
- if (isNoteKey(event)) return this.openNote();
3958
+ private noteKey(name: string): boolean {
3959
+ if (name === NOTE_KEY) return this.openNote();
3831
3960
  const kind = name === "Mod-Alt-f" ? "footnote" : name === "Mod-Alt-d" ? "endnote" : null;
3832
3961
  if (!kind || !this.editable || !this.notes) return false;
3833
3962
  return this.run(insertNote(kind)) && this.openNote();
@@ -4000,13 +4129,16 @@ export class EditorView {
4000
4129
  };
4001
4130
 
4002
4131
  private onMutations = (records: MutationRecord[]): void => {
4003
- this.pending.push(...records.filter((r) => !handleOnly(r)));
4132
+ this.pending.push(...records.filter((r) => !handleOnly(r) && !inNodeView(r)));
4004
4133
  if (!this.composing && this.pending.length) this.flush();
4005
4134
  };
4006
4135
 
4007
4136
  /** Reads back every DOM change recorded so far. */
4008
4137
  private flush(): void {
4009
- const records = [...this.pending, ...this.observer.takeRecords().filter((r) => !handleOnly(r))];
4138
+ const records = [
4139
+ ...this.pending,
4140
+ ...this.observer.takeRecords().filter((r) => !handleOnly(r) && !inNodeView(r)),
4141
+ ];
4010
4142
  this.pending = [];
4011
4143
  if (records.length) this.readBack(records);
4012
4144
  }
@@ -4166,6 +4298,37 @@ export class EditorView {
4166
4298
 
4167
4299
  // ---------- clipboard ----------
4168
4300
 
4301
+ /**
4302
+ * A copied picture's image as another document can show and save it: its own `data:` or web
4303
+ * URL, else the image this view shows, as a `data:` URL (read from the drawn image when it is
4304
+ * an object URL: its bytes live in this page only). Nothing lasting known (an image not loaded
4305
+ * yet): as it is, and the paste brings the picture without its image.
4306
+ */
4307
+ private portableResource(context: WordContext) {
4308
+ const shown = imageSource(context.envelope.resources, this.resource ?? undefined);
4309
+ return (key: string, resource: ResourceJSON): ResourceJSON => {
4310
+ if (resource.data !== undefined || /^https?:/i.test(resource.url ?? "")) return resource;
4311
+ const url = shown(key);
4312
+ if (url?.startsWith("data:image/")) return { contentType: resource.contentType, data: url };
4313
+ const img = [...this.host.querySelectorAll(`img[${PICTURE_ATTR}]`)].find(
4314
+ (element) => element.getAttribute(PICTURE_ATTR) === key,
4315
+ ) as HTMLImageElement | undefined;
4316
+ if (img?.complete && img.naturalWidth > 0) {
4317
+ try {
4318
+ const canvas = this.host.ownerDocument.createElement("canvas");
4319
+ canvas.width = img.naturalWidth;
4320
+ canvas.height = img.naturalHeight;
4321
+ canvas.getContext("2d")?.drawImage(img, 0, 0);
4322
+ // PNG: the browser's encoding, the same pixels (JPEG's own bytes cannot be read here).
4323
+ return { contentType: "image/png", data: canvas.toDataURL("image/png") };
4324
+ } catch {} // a cross-origin image taints the canvas: keep what is known
4325
+ }
4326
+ // An image the page shows over the web lasts; an object URL does not (the paste then comes
4327
+ // without its image).
4328
+ return url && /^https?:/i.test(url) ? { contentType: resource.contentType, url } : resource;
4329
+ };
4330
+ }
4331
+
4169
4332
  /** Copy writes the model's content (plain text, HTML, Foli JSON), never the browser's DOM. */
4170
4333
  private onCopy = (event: ClipboardEvent): void => {
4171
4334
  this.syncSelection();
@@ -4181,7 +4344,7 @@ export class EditorView {
4181
4344
  table ? 0 : selection.from,
4182
4345
  table ? source.content.size : selection.to,
4183
4346
  this.host.ownerDocument,
4184
- { context, ratio: this.lineRatio },
4347
+ { context, ratio: this.lineRatio, portable: this.portableResource(context) },
4185
4348
  );
4186
4349
  event.clipboardData.setData("text/plain", table ? cellsText(table) : text);
4187
4350
  event.clipboardData.setData("text/html", html);
@@ -4356,7 +4519,10 @@ function layoutScale(page: HTMLElement, settings: DocumentSettings): number {
4356
4519
  return page.getBoundingClientRect().width / (settings.pageSize.width * PX_PER_MM) || 1;
4357
4520
  }
4358
4521
 
4359
- /** A block's measurement: border-box height across its fragments (layout px), margins, lines. */
4522
+ /**
4523
+ * A block's measurement: border-box height across its fragments (layout px), margins, lines.
4524
+ * @internal
4525
+ */
4360
4526
  export interface Measure {
4361
4527
  height: number;
4362
4528
  marginTop: number;
@@ -4437,6 +4603,7 @@ function measureTable(
4437
4603
  * at its cells' line ends, unless the row may not split (`cantSplit`, unless taller than a
4438
4604
  * page), is part of a vertical merge or holds a nested table (shortcut: those split only
4439
4605
  * between rows; a nested table stays whole within its row).
4606
+ * @internal
4440
4607
  */
4441
4608
  export function breaksOf(
4442
4609
  frame: Frame,
@@ -4498,6 +4665,7 @@ function tableBox(
4498
4665
  * A flowed table cut into its fragments: the rows each shows (a row split across pages in
4499
4666
  * both), the header rows drawn again above a continuation, the fragment of the table each
4500
4667
  * block's fragments go in, and where a block of a split row splits (content offsets).
4668
+ * @internal
4501
4669
  */
4502
4670
  export function cutTable(
4503
4671
  frame: Frame,
@@ -4651,6 +4819,8 @@ function wordBox(
4651
4819
  marginTop: before,
4652
4820
  marginBottom: after,
4653
4821
  keepWithNext: desc.word.keepNext,
4822
+ // A node view's parts are not lines: no widow or orphan control.
4823
+ anyLine: !!desc.parts,
4654
4824
  pageHeight,
4655
4825
  section: section.flow,
4656
4826
  sectionMark: !!desc.node.attrs.section && desc.word.empty,
@@ -4746,6 +4916,7 @@ function reuseOf(
4746
4916
  };
4747
4917
  }
4748
4918
 
4919
+ /** @internal */
4749
4920
  export interface Lines {
4750
4921
  /** Content offset where each line starts. */
4751
4922
  starts: number[];
@@ -4807,6 +4978,16 @@ function measureLines(desc: BlockDesc, scale: number): Lines {
4807
4978
  )
4808
4979
  items.push({ offset: desc.node.content.size, rect: () => tail.getBoundingClientRect() });
4809
4980
  const from = f ? (desc.splits[f - 1] as number) : 0;
4981
+ const parts = (desc as WordBlockDesc).parts;
4982
+ if (!items.length && parts && desc.fragments.length === 1) {
4983
+ // A node view drawn whole: it may break after each of its root's children.
4984
+ for (const bottom of parts()) {
4985
+ starts.push(0);
4986
+ bottoms.push(bottom / scale);
4987
+ }
4988
+ base += box.height;
4989
+ continue;
4990
+ }
4810
4991
  if (!items.length) {
4811
4992
  starts.push(from);
4812
4993
  bottoms.push((base + box.height) / scale);
@@ -4929,9 +5110,8 @@ const NAVIGATION_KEYS: Readonly<Record<string, NavigationRequest>> = {
4929
5110
  "Mod-Shift-F5": "bookmarks",
4930
5111
  };
4931
5112
 
4932
- /** Shift+Alt+N: from a note's reference to the note, and back. */
4933
- const isNoteKey = (event: KeyboardEvent) =>
4934
- event.altKey && event.shiftKey && !event.ctrlKey && !event.metaKey && event.code === "KeyN";
5113
+ /** Shift+Alt+N (Shift+Option+N): from a note's reference to the note, and back. */
5114
+ const NOTE_KEY = "Alt-Shift-n";
4935
5115
 
4936
5116
  /** Whether story `id` of `state`'s document exists (a number can outlive its note on screen). */
4937
5117
  const isNoteStory = (state: EditorState, id: string) => !!wordContext(state)?.stories.has(id);
@@ -4966,7 +5146,22 @@ export function keyName(event: KeyboardEvent): string {
4966
5146
  const mod = IS_MAC ? event.metaKey : event.ctrlKey;
4967
5147
  // Non-Latin layouts: Ctrl+B still means "b" (use the physical key when the character is not a-z);
4968
5148
  // with Shift, Ctrl+Shift+7 still means "7" (the character is "&" or "/" depending on layout).
4969
- if (mod && !/^[a-z0-9]$/.test(key) && /^(Key[A-Z]|Digit[0-9])$/.test(event.code))
5149
+ // Option on macOS types a character (Option+N is a dead key): Alt+N still means "n". But AltGr
5150
+ // (Ctrl+Alt on Windows) typing a character is that character: "[" is AltGr+F on a Hungarian
5151
+ // layout, and must not be read as Ctrl+Alt+F (a footnote).
5152
+ // (macOS has no AltGr: some browsers report Option as AltGraph, and Option is handled above.)
5153
+ // shortcut: Ctrl+Alt giving any other character counts as AltGr (not all browsers report its
5154
+ // state), so Ctrl+Alt+F on a non-Latin layout is not Mod-Alt-f; drop the guess if they all do.
5155
+ const altGraph =
5156
+ !IS_MAC &&
5157
+ (event.getModifierState?.("AltGraph") ||
5158
+ (event.ctrlKey && event.altKey && key.length === 1 && !/^[a-z0-9]$/i.test(key)));
5159
+ if (
5160
+ (mod || event.altKey) &&
5161
+ !altGraph &&
5162
+ !/^[a-z0-9]$/.test(key) &&
5163
+ /^(Key[A-Z]|Digit[0-9])$/.test(event.code)
5164
+ )
4970
5165
  key = event.code.slice(-1).toLowerCase();
4971
5166
  let name = key;
4972
5167
  if (event.shiftKey && key !== "Shift") name = `Shift-${name}`;
@@ -5039,6 +5234,16 @@ function holdsFloat(node: ModelNode): boolean {
5039
5234
  return found;
5040
5235
  }
5041
5236
 
5237
+ /**
5238
+ * A change a node view made to its own drawing (content loaded later, a chart drawn): not input,
5239
+ * never read back. Inside a custom node's box, which is not editable.
5240
+ */
5241
+ function inNodeView(record: MutationRecord): boolean {
5242
+ const { target } = record;
5243
+ const element = target.nodeType === 1 ? (target as Element) : target.parentElement;
5244
+ return !!element?.closest('[data-foli-custom][contenteditable="false"]');
5245
+ }
5246
+
5042
5247
  /** A change that only adds or removes the view's own marks (a resize handle, a control's frame). */
5043
5248
  function handleOnly(record: MutationRecord): boolean {
5044
5249
  const nodes = [...record.addedNodes, ...record.removedNodes];