@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.
- package/CHANGELOG.md +161 -0
- package/README.md +1 -1
- package/dist/clipboard.d.ts +1 -51
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +65 -13
- package/dist/clipboard.js.map +1 -1
- package/dist/extension.d.ts +90 -0
- package/dist/extension.d.ts.map +1 -0
- package/dist/extension.js +152 -0
- package/dist/extension.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +1 -282
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +21 -4
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +1 -17
- package/dist/print.d.ts.map +1 -1
- package/dist/print.js +5 -1
- package/dist/print.js.map +1 -1
- package/dist/render.d.ts +1 -24
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +4 -1
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +34 -67
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +209 -24
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +0 -175
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +106 -24
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +1 -22
- package/dist/word/columns.d.ts.map +1 -1
- package/dist/word/columns.js +15 -3
- package/dist/word/columns.js.map +1 -1
- package/dist/word/controls.d.ts +1 -62
- package/dist/word/controls.d.ts.map +1 -1
- package/dist/word/controls.js +6 -1
- package/dist/word/controls.js.map +1 -1
- package/dist/word/edit.d.ts +1 -83
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +183 -5
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +1 -22
- package/dist/word/matches.d.ts.map +1 -1
- package/dist/word/matches.js +6 -1
- package/dist/word/matches.js.map +1 -1
- package/dist/word/notes.d.ts +0 -103
- package/dist/word/notes.d.ts.map +1 -1
- package/dist/word/notes.js +15 -4
- package/dist/word/notes.js.map +1 -1
- package/dist/word/pages.d.ts +15 -10
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +3 -2
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +0 -111
- package/dist/word/regions.d.ts.map +1 -1
- package/dist/word/regions.js +19 -5
- package/dist/word/regions.js.map +1 -1
- package/dist/word/table.d.ts +1 -78
- package/dist/word/table.d.ts.map +1 -1
- package/dist/word/table.js +13 -3
- package/dist/word/table.js.map +1 -1
- package/package.json +6 -3
- package/src/clipboard.ts +91 -21
- package/src/extension.ts +267 -0
- package/src/index.ts +10 -0
- package/src/layout/flow.ts +59 -13
- package/src/print.ts +9 -2
- package/src/render.ts +6 -1
- package/src/view.ts +245 -40
- package/src/word/blocks.ts +124 -30
- package/src/word/columns.ts +19 -4
- package/src/word/controls.ts +13 -3
- package/src/word/edit.ts +207 -9
- package/src/word/matches.ts +6 -1
- package/src/word/notes.ts +19 -6
- package/src/word/pages.ts +19 -3
- package/src/word/regions.ts +27 -8
- package/src/word/table.ts +33 -8
package/dist/view.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import { AllSelection, arrowAcrossLeaf, autolinkHref, CellSelection,
|
|
1
|
+
import { AllSelection, arrowAcrossLeaf, autolinkHref, CellSelection, cellsAsTable, closeHistory, controlAt, controlProperties, deleteBackward, deleteForward, deleteSelection, ensureStory, extendCellSelection, fieldCodesShown, fieldsIn, goToCellVertically, insertControlText, insertHardBreak, insertLink, insertNote, insertPicture, insertText, isSafeHref, marksAt, NodeSelection, noteAtSelection, noteNumbers, noteProperties, noteTexts, pageVariant, parseContentV2, pasteCells, redo, Selection, Slice, sectionSettings, setColumnWidth, setLink, setPictureSize, splitBlock, storyOfSection, TextSelection, TransformError, toggleFieldCodes, toggleMark, trackState, undo, updateFields, } from "@folijs/core";
|
|
2
|
+
import { caretStop, controlSpot, deleteSelectionKeepingTables, fitPaste, fitPastedV2, keepTablesApart, referencesIn, StoryStep, sameSpot, setComments, typedMarks, viewKeys, withEnvelope, wordContext, } from "@folijs/core/internal";
|
|
2
3
|
import { docFromClipboard, FOLI_MIME, serializeRange } from "./clipboard.js";
|
|
4
|
+
import { checkAttrs, decorationsByBlock, reportError, viewsOf, } from "./extension.js";
|
|
3
5
|
import { contentHeight, DEFAULT_RULES, flowFrom, notePieces, PX_PER_MM, pagesOf, spaceAfterText, } from "./layout/flow.js";
|
|
4
6
|
import { printStyles, SHEET_ATTR } from "./print.js";
|
|
5
7
|
import { PAGE_BREAK_ATTR } from "./render.js";
|
|
@@ -403,6 +405,14 @@ export class EditorView {
|
|
|
403
405
|
listeners = [];
|
|
404
406
|
/** The frames, titles and pop-ups of content controls. */
|
|
405
407
|
controlChrome;
|
|
408
|
+
/** The editor's view extensions' node and mark views, decorations and plugin views. */
|
|
409
|
+
views;
|
|
410
|
+
decorators;
|
|
411
|
+
decorations = [];
|
|
412
|
+
decorationKey = "";
|
|
413
|
+
pluginViews = [];
|
|
414
|
+
/** What node views drew and the view still shows (their `destroy` when it no longer does). */
|
|
415
|
+
drawn = [];
|
|
406
416
|
constructor(host, editor, options = {}) {
|
|
407
417
|
this.host = host;
|
|
408
418
|
this.editor = editor;
|
|
@@ -458,6 +468,10 @@ export class EditorView {
|
|
|
458
468
|
this.observer.observe(host, { childList: true, characterData: true, subtree: true });
|
|
459
469
|
// The picture resolver before the first render: no image drawn from a URL the app refuses.
|
|
460
470
|
this.resource = options.resource ?? undefined;
|
|
471
|
+
const extensions = editor.extensions;
|
|
472
|
+
this.views = viewsOf(extensions);
|
|
473
|
+
this.decorators = extensions.flatMap((e) => (e.decorations ? [e.decorations.bind(e)] : []));
|
|
474
|
+
this.decorate(this.state);
|
|
461
475
|
this.render();
|
|
462
476
|
this.setOptions(options);
|
|
463
477
|
this.listen("beforeinput", this.onBeforeInput);
|
|
@@ -481,6 +495,73 @@ export class EditorView {
|
|
|
481
495
|
host.ownerDocument.addEventListener("selectionchange", this.onSelectionChange);
|
|
482
496
|
host.ownerDocument.fonts?.addEventListener("loadingdone", this.onFontsLoaded);
|
|
483
497
|
this.unsubscribe = editor.subscribe((state) => this.update(state));
|
|
498
|
+
for (const extension of extensions) {
|
|
499
|
+
try {
|
|
500
|
+
if (extension.view)
|
|
501
|
+
this.pluginViews.push(extension.view(this));
|
|
502
|
+
}
|
|
503
|
+
catch (error) {
|
|
504
|
+
reportError(error);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
/** Calls `hook` for each plugin view, reporting what throws. */
|
|
509
|
+
eachPluginView(hook) {
|
|
510
|
+
for (const view of this.pluginViews) {
|
|
511
|
+
try {
|
|
512
|
+
hook(view);
|
|
513
|
+
}
|
|
514
|
+
catch (error) {
|
|
515
|
+
reportError(error);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/** Destroys node views' drawings the host no longer holds (all of them: `all`). */
|
|
520
|
+
dropDrawn(all) {
|
|
521
|
+
let kept = 0;
|
|
522
|
+
for (const drawn of this.drawn) {
|
|
523
|
+
if (!all && this.host.contains(drawn.box)) {
|
|
524
|
+
this.drawn[kept++] = drawn;
|
|
525
|
+
continue;
|
|
526
|
+
}
|
|
527
|
+
try {
|
|
528
|
+
drawn.view.destroy?.(drawn.root);
|
|
529
|
+
}
|
|
530
|
+
catch (error) {
|
|
531
|
+
reportError(error);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
this.drawn.length = kept;
|
|
535
|
+
}
|
|
536
|
+
/** Asks the extensions for `state`'s decorations; whether they changed. */
|
|
537
|
+
decorate(state) {
|
|
538
|
+
if (!this.decorators.length)
|
|
539
|
+
return false;
|
|
540
|
+
// A failing extension, or a decoration with a refused attribute, is reported and left out.
|
|
541
|
+
const decorations = this.decorators.flatMap((decorate) => {
|
|
542
|
+
try {
|
|
543
|
+
return (decorate?.(state) ?? []).filter((decoration) => {
|
|
544
|
+
try {
|
|
545
|
+
checkAttrs(decoration.attrs);
|
|
546
|
+
return true;
|
|
547
|
+
}
|
|
548
|
+
catch (error) {
|
|
549
|
+
reportError(error);
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
}
|
|
554
|
+
catch (error) {
|
|
555
|
+
reportError(error);
|
|
556
|
+
return [];
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
const key = JSON.stringify(decorations);
|
|
560
|
+
if (key === this.decorationKey)
|
|
561
|
+
return false;
|
|
562
|
+
this.decorations = decorations;
|
|
563
|
+
this.decorationKey = key;
|
|
564
|
+
return true;
|
|
484
565
|
}
|
|
485
566
|
/** Adds a listener for the body's events: a story open on a page has its own view. */
|
|
486
567
|
listen(type, handler) {
|
|
@@ -492,6 +573,8 @@ export class EditorView {
|
|
|
492
573
|
this.listeners.push([type, listener]);
|
|
493
574
|
}
|
|
494
575
|
destroy() {
|
|
576
|
+
this.eachPluginView((view) => view.destroy?.());
|
|
577
|
+
this.pluginViews = [];
|
|
495
578
|
this.closeStory(false);
|
|
496
579
|
this.showFound(null);
|
|
497
580
|
this.controlChrome.destroy();
|
|
@@ -527,6 +610,7 @@ export class EditorView {
|
|
|
527
610
|
if (!host.getAttribute("style"))
|
|
528
611
|
host.removeAttribute("style");
|
|
529
612
|
host.replaceChildren();
|
|
613
|
+
this.dropDrawn(true);
|
|
530
614
|
}
|
|
531
615
|
/** Focuses the host, shows the model selection in it and scrolls it into view. */
|
|
532
616
|
focus() {
|
|
@@ -636,8 +720,11 @@ export class EditorView {
|
|
|
636
720
|
const selectionChanged = !state.selection.eq(this.state.selection);
|
|
637
721
|
// Another document: the story being edited closes.
|
|
638
722
|
const lost = !!this.mount && wordContext(state)?.id !== wordContext(this.state)?.id;
|
|
723
|
+
const previous = this.state;
|
|
639
724
|
this.state = state;
|
|
640
|
-
|
|
725
|
+
// Decorations that changed redraw their blocks (the others are reused).
|
|
726
|
+
const decorated = this.decorate(state);
|
|
727
|
+
if (docChanged || decorated || (storiesChanged && !this.redrawRegions()))
|
|
641
728
|
this.render();
|
|
642
729
|
if (lost)
|
|
643
730
|
this.closeStory(true);
|
|
@@ -645,10 +732,12 @@ export class EditorView {
|
|
|
645
732
|
if (codesChanged)
|
|
646
733
|
this.mount?.view.update(this.mount.view.editor.state);
|
|
647
734
|
this.showSelectedNode();
|
|
648
|
-
|
|
735
|
+
// Redrawn blocks may have held the DOM selection.
|
|
736
|
+
this.writeSelection(docChanged || decorated);
|
|
649
737
|
// shortcut: scrolls for every change while focused; once remote or plugin changes exist, scroll only for local input (a transaction flag).
|
|
650
738
|
if (docChanged || selectionChanged)
|
|
651
739
|
this.scrollToSelection();
|
|
740
|
+
this.eachPluginView((view) => view.update?.(this, previous));
|
|
652
741
|
}
|
|
653
742
|
/**
|
|
654
743
|
* Scrolls the selection head into view. Browsers do it for their own edits and caret moves,
|
|
@@ -688,6 +777,8 @@ export class EditorView {
|
|
|
688
777
|
render() {
|
|
689
778
|
try {
|
|
690
779
|
this.renderPages();
|
|
780
|
+
if (this.drawn.length)
|
|
781
|
+
this.dropDrawn(false);
|
|
691
782
|
}
|
|
692
783
|
catch (error) {
|
|
693
784
|
// What it had changed is not input: read back, it would render and fail again, for ever.
|
|
@@ -722,6 +813,8 @@ export class EditorView {
|
|
|
722
813
|
if (context.envelope.resources)
|
|
723
814
|
injectStyles(this.host);
|
|
724
815
|
word = this.word = new WordRenderer(context, ownerDocument, this.lineRatio, false, this.review, imageSource(context.envelope.resources, this.resource ?? undefined), this.fieldCodes);
|
|
816
|
+
word.views = this.views;
|
|
817
|
+
word.drawn = this.drawn;
|
|
725
818
|
this.invalidateLayout();
|
|
726
819
|
}
|
|
727
820
|
const references = this.noteReferences();
|
|
@@ -729,6 +822,7 @@ export class EditorView {
|
|
|
729
822
|
// The blocks the view renders (paragraphs in table cells among them) and the page flow's
|
|
730
823
|
// items (top-level paragraphs and opaque blocks, and tables).
|
|
731
824
|
const { units, items } = collect(doc, context.resolver);
|
|
825
|
+
word.decorations = this.decorations.length ? decorationsByBlock(units, this.decorations) : [];
|
|
732
826
|
word.prepare(units.map((unit) => unit.node), (i) => {
|
|
733
827
|
const unit = units[i];
|
|
734
828
|
const previous = units[i - 1];
|
|
@@ -1016,6 +1110,12 @@ export class EditorView {
|
|
|
1016
1110
|
const desc = blocks[item.unit];
|
|
1017
1111
|
const contentStart = units[item.unit].pos + 1;
|
|
1018
1112
|
const { section } = layout[i];
|
|
1113
|
+
// A node view splits where its part's line ends (layout px).
|
|
1114
|
+
if (desc.parts) {
|
|
1115
|
+
const { bottoms } = lines(desc);
|
|
1116
|
+
splitsOf.set(item.unit, fragments.slice(1).map((fragment) => bottoms[fragment.fromLine - 1]));
|
|
1117
|
+
return;
|
|
1118
|
+
}
|
|
1019
1119
|
splitsOf.set(item.unit, fragments.slice(1).map((fragment) => {
|
|
1020
1120
|
// Line numbers are those of the column the fragment is in.
|
|
1021
1121
|
const measured = section.unequal
|
|
@@ -1599,7 +1699,10 @@ export class EditorView {
|
|
|
1599
1699
|
for (const [f, fragment] of desc.fragments.entries()) {
|
|
1600
1700
|
if (fragment.closest(".foli-page") !== element)
|
|
1601
1701
|
continue;
|
|
1602
|
-
|
|
1702
|
+
// A block leaf's parts (a node view split across pages) are all before it.
|
|
1703
|
+
const start = desc.node.isTextblock
|
|
1704
|
+
? this.starts[i] + 1 + (f ? desc.splits[f - 1] : 0)
|
|
1705
|
+
: this.starts[i];
|
|
1603
1706
|
const { doc } = this.state;
|
|
1604
1707
|
this.editor.dispatch(this.state.tr.setSelection(Selection.near(doc.resolve(Math.min(start, doc.content.size)))));
|
|
1605
1708
|
break search;
|
|
@@ -1662,12 +1765,10 @@ export class EditorView {
|
|
|
1662
1765
|
return box ? [boxOf(box.left, box.top, 4, box.height)] : [];
|
|
1663
1766
|
}
|
|
1664
1767
|
/**
|
|
1665
|
-
*
|
|
1666
|
-
*
|
|
1768
|
+
* Left and Right stop on each side of a content control's edge, as Word's caret does (browsers
|
|
1769
|
+
* cannot see the difference). Alt+Down, which opens a list or date picker, is in `viewKey`.
|
|
1667
1770
|
*/
|
|
1668
1771
|
controlKey(name) {
|
|
1669
|
-
if (name === "Alt-ArrowDown")
|
|
1670
|
-
return this.controlChrome.open();
|
|
1671
1772
|
if (name !== "ArrowLeft" && name !== "ArrowRight")
|
|
1672
1773
|
return false;
|
|
1673
1774
|
const { selection, doc } = this.state;
|
|
@@ -1956,7 +2057,8 @@ export class EditorView {
|
|
|
1956
2057
|
wrapper.addEventListener("keydown", (event) => {
|
|
1957
2058
|
if (event.isComposing || view.composing)
|
|
1958
2059
|
return;
|
|
1959
|
-
|
|
2060
|
+
// Unless the story's view ran a user's binding of the key.
|
|
2061
|
+
if (!event.defaultPrevented && keyName(event) === NOTE_KEY && this.toReference()) {
|
|
1960
2062
|
// Not on to the body, which would open the note again.
|
|
1961
2063
|
event.preventDefault();
|
|
1962
2064
|
event.stopPropagation();
|
|
@@ -2473,7 +2575,7 @@ export class EditorView {
|
|
|
2473
2575
|
// where a continuation starts), else the document end.
|
|
2474
2576
|
const range = host.ownerDocument.createRange();
|
|
2475
2577
|
range.setStart(node, offset);
|
|
2476
|
-
const posOf = (i, f) => f === 0
|
|
2578
|
+
const posOf = (i, f) => f === 0 || !this.blocks[i].node.isTextblock
|
|
2477
2579
|
? this.starts[i]
|
|
2478
2580
|
: this.starts[i] +
|
|
2479
2581
|
1 +
|
|
@@ -2875,13 +2977,31 @@ export class EditorView {
|
|
|
2875
2977
|
return;
|
|
2876
2978
|
this.run(type.endsWith("Forward") ? deleteForward(unit) : deleteBackward(unit));
|
|
2877
2979
|
};
|
|
2878
|
-
/**
|
|
2980
|
+
/**
|
|
2981
|
+
* Runs the first command bound to `name` that applies, through the view's dispatch; the view's
|
|
2982
|
+
* own keys where core's `viewKeys` plugin stands (after the user's plugins, before the
|
|
2983
|
+
* built-in keymaps).
|
|
2984
|
+
*/
|
|
2879
2985
|
runKey(name) {
|
|
2880
2986
|
return this.state.plugins.some((plugin) => {
|
|
2987
|
+
if (plugin.key === viewKeys)
|
|
2988
|
+
return this.viewKey(name);
|
|
2881
2989
|
const command = plugin.spec.keymap?.[name];
|
|
2882
2990
|
return command ? this.run(command) : false;
|
|
2883
2991
|
});
|
|
2884
2992
|
}
|
|
2993
|
+
/**
|
|
2994
|
+
* The view's own overridable keys (ADR-0008, `docs/keyboard.md`): they need the pages, the
|
|
2995
|
+
* stories or the app's navigation UI, so they are not core commands. A user keymap binding the
|
|
2996
|
+
* same name runs first.
|
|
2997
|
+
*/
|
|
2998
|
+
viewKey(name) {
|
|
2999
|
+
return (this.documentEdgeKey(name) ||
|
|
3000
|
+
this.fieldKey(name) ||
|
|
3001
|
+
(name === "Alt-ArrowDown" && this.controlChrome.open()) ||
|
|
3002
|
+
this.noteKey(name) ||
|
|
3003
|
+
this.navigationKey(name));
|
|
3004
|
+
}
|
|
2885
3005
|
// ---------- column borders (10.7c) ----------
|
|
2886
3006
|
/** A column border being dragged: its table and column, the column's width, the press, the guide. */
|
|
2887
3007
|
drag = null;
|
|
@@ -3398,11 +3518,7 @@ export class EditorView {
|
|
|
3398
3518
|
return;
|
|
3399
3519
|
}
|
|
3400
3520
|
const name = keyName(event);
|
|
3401
|
-
if (this.
|
|
3402
|
-
this.fieldKey(name) ||
|
|
3403
|
-
this.controlKey(name) ||
|
|
3404
|
-
this.noteKey(event, name) ||
|
|
3405
|
-
this.navigationKey(name)) {
|
|
3521
|
+
if (this.controlKey(name)) {
|
|
3406
3522
|
event.preventDefault();
|
|
3407
3523
|
return;
|
|
3408
3524
|
}
|
|
@@ -3459,8 +3575,8 @@ export class EditorView {
|
|
|
3459
3575
|
}
|
|
3460
3576
|
};
|
|
3461
3577
|
/** Word's keys for notes, and Shift+Alt+N from a reference to its note. */
|
|
3462
|
-
noteKey(
|
|
3463
|
-
if (
|
|
3578
|
+
noteKey(name) {
|
|
3579
|
+
if (name === NOTE_KEY)
|
|
3464
3580
|
return this.openNote();
|
|
3465
3581
|
const kind = name === "Mod-Alt-f" ? "footnote" : name === "Mod-Alt-d" ? "endnote" : null;
|
|
3466
3582
|
if (!kind || !this.editable || !this.notes)
|
|
@@ -3645,13 +3761,16 @@ export class EditorView {
|
|
|
3645
3761
|
this.onCompositionEnd();
|
|
3646
3762
|
};
|
|
3647
3763
|
onMutations = (records) => {
|
|
3648
|
-
this.pending.push(...records.filter((r) => !handleOnly(r)));
|
|
3764
|
+
this.pending.push(...records.filter((r) => !handleOnly(r) && !inNodeView(r)));
|
|
3649
3765
|
if (!this.composing && this.pending.length)
|
|
3650
3766
|
this.flush();
|
|
3651
3767
|
};
|
|
3652
3768
|
/** Reads back every DOM change recorded so far. */
|
|
3653
3769
|
flush() {
|
|
3654
|
-
const records = [
|
|
3770
|
+
const records = [
|
|
3771
|
+
...this.pending,
|
|
3772
|
+
...this.observer.takeRecords().filter((r) => !handleOnly(r) && !inNodeView(r)),
|
|
3773
|
+
];
|
|
3655
3774
|
this.pending = [];
|
|
3656
3775
|
if (records.length)
|
|
3657
3776
|
this.readBack(records);
|
|
@@ -3823,6 +3942,37 @@ export class EditorView {
|
|
|
3823
3942
|
this.writeSelection(true);
|
|
3824
3943
|
}
|
|
3825
3944
|
// ---------- clipboard ----------
|
|
3945
|
+
/**
|
|
3946
|
+
* A copied picture's image as another document can show and save it: its own `data:` or web
|
|
3947
|
+
* URL, else the image this view shows, as a `data:` URL (read from the drawn image when it is
|
|
3948
|
+
* an object URL: its bytes live in this page only). Nothing lasting known (an image not loaded
|
|
3949
|
+
* yet): as it is, and the paste brings the picture without its image.
|
|
3950
|
+
*/
|
|
3951
|
+
portableResource(context) {
|
|
3952
|
+
const shown = imageSource(context.envelope.resources, this.resource ?? undefined);
|
|
3953
|
+
return (key, resource) => {
|
|
3954
|
+
if (resource.data !== undefined || /^https?:/i.test(resource.url ?? ""))
|
|
3955
|
+
return resource;
|
|
3956
|
+
const url = shown(key);
|
|
3957
|
+
if (url?.startsWith("data:image/"))
|
|
3958
|
+
return { contentType: resource.contentType, data: url };
|
|
3959
|
+
const img = [...this.host.querySelectorAll(`img[${PICTURE_ATTR}]`)].find((element) => element.getAttribute(PICTURE_ATTR) === key);
|
|
3960
|
+
if (img?.complete && img.naturalWidth > 0) {
|
|
3961
|
+
try {
|
|
3962
|
+
const canvas = this.host.ownerDocument.createElement("canvas");
|
|
3963
|
+
canvas.width = img.naturalWidth;
|
|
3964
|
+
canvas.height = img.naturalHeight;
|
|
3965
|
+
canvas.getContext("2d")?.drawImage(img, 0, 0);
|
|
3966
|
+
// PNG: the browser's encoding, the same pixels (JPEG's own bytes cannot be read here).
|
|
3967
|
+
return { contentType: "image/png", data: canvas.toDataURL("image/png") };
|
|
3968
|
+
}
|
|
3969
|
+
catch { } // a cross-origin image taints the canvas: keep what is known
|
|
3970
|
+
}
|
|
3971
|
+
// An image the page shows over the web lasts; an object URL does not (the paste then comes
|
|
3972
|
+
// without its image).
|
|
3973
|
+
return url && /^https?:/i.test(url) ? { contentType: resource.contentType, url } : resource;
|
|
3974
|
+
};
|
|
3975
|
+
}
|
|
3826
3976
|
/** Copy writes the model's content (plain text, HTML, Foli JSON), never the browser's DOM. */
|
|
3827
3977
|
onCopy = (event) => {
|
|
3828
3978
|
this.syncSelection();
|
|
@@ -3834,7 +3984,7 @@ export class EditorView {
|
|
|
3834
3984
|
// Selected cells are copied as a table of their own (ADR-0006).
|
|
3835
3985
|
const table = selection instanceof CellSelection ? cellsAsTable(selection) : null;
|
|
3836
3986
|
const source = table ? doc.type.create(null, table) : doc;
|
|
3837
|
-
const { text, html, json } = serializeRange(source, table ? 0 : selection.from, table ? source.content.size : selection.to, this.host.ownerDocument, { context, ratio: this.lineRatio });
|
|
3987
|
+
const { text, html, json } = serializeRange(source, table ? 0 : selection.from, table ? source.content.size : selection.to, this.host.ownerDocument, { context, ratio: this.lineRatio, portable: this.portableResource(context) });
|
|
3838
3988
|
event.clipboardData.setData("text/plain", table ? cellsText(table) : text);
|
|
3839
3989
|
event.clipboardData.setData("text/html", html);
|
|
3840
3990
|
event.clipboardData.setData(FOLI_MIME, json);
|
|
@@ -4060,6 +4210,7 @@ function measureTable(frame, wrapper, blocks, scale) {
|
|
|
4060
4210
|
* at its cells' line ends, unless the row may not split (`cantSplit`, unless taller than a
|
|
4061
4211
|
* page), is part of a vertical merge or holds a nested table (shortcut: those split only
|
|
4062
4212
|
* between rows; a nested table stays whole within its row).
|
|
4213
|
+
* @internal
|
|
4063
4214
|
*/
|
|
4064
4215
|
export function breaksOf(frame, measure, blocks, pageHeight, lines) {
|
|
4065
4216
|
const t = measure.table;
|
|
@@ -4111,6 +4262,7 @@ function tableBox(frame, measure, pageHeight, breakAfter, breaks) {
|
|
|
4111
4262
|
* A flowed table cut into its fragments: the rows each shows (a row split across pages in
|
|
4112
4263
|
* both), the header rows drawn again above a continuation, the fragment of the table each
|
|
4113
4264
|
* block's fragments go in, and where a block of a split row splits (content offsets).
|
|
4265
|
+
* @internal
|
|
4114
4266
|
*/
|
|
4115
4267
|
export function cutTable(frame, measure, fragments, blocks, lines) {
|
|
4116
4268
|
const t = measure.table;
|
|
@@ -4249,6 +4401,8 @@ function wordBox(words, i, desc, measure, lines, layout, atWidth, adds, noteOf)
|
|
|
4249
4401
|
marginTop: before,
|
|
4250
4402
|
marginBottom: after,
|
|
4251
4403
|
keepWithNext: desc.word.keepNext,
|
|
4404
|
+
// A node view's parts are not lines: no widow or orphan control.
|
|
4405
|
+
anyLine: !!desc.parts,
|
|
4252
4406
|
pageHeight,
|
|
4253
4407
|
section: section.flow,
|
|
4254
4408
|
sectionMark: !!desc.node.attrs.section && desc.word.empty,
|
|
@@ -4378,6 +4532,16 @@ function measureLines(desc, scale) {
|
|
|
4378
4532
|
!desc.leaves.some((l) => l.dom === tail))
|
|
4379
4533
|
items.push({ offset: desc.node.content.size, rect: () => tail.getBoundingClientRect() });
|
|
4380
4534
|
const from = f ? desc.splits[f - 1] : 0;
|
|
4535
|
+
const parts = desc.parts;
|
|
4536
|
+
if (!items.length && parts && desc.fragments.length === 1) {
|
|
4537
|
+
// A node view drawn whole: it may break after each of its root's children.
|
|
4538
|
+
for (const bottom of parts()) {
|
|
4539
|
+
starts.push(0);
|
|
4540
|
+
bottoms.push(bottom / scale);
|
|
4541
|
+
}
|
|
4542
|
+
base += box.height;
|
|
4543
|
+
continue;
|
|
4544
|
+
}
|
|
4381
4545
|
if (!items.length) {
|
|
4382
4546
|
starts.push(from);
|
|
4383
4547
|
bottoms.push((base + box.height) / scale);
|
|
@@ -4496,8 +4660,8 @@ const NAVIGATION_KEYS = {
|
|
|
4496
4660
|
"Shift-F3": "previous",
|
|
4497
4661
|
"Mod-Shift-F5": "bookmarks",
|
|
4498
4662
|
};
|
|
4499
|
-
/** Shift+Alt+N: from a note's reference to the note, and back. */
|
|
4500
|
-
const
|
|
4663
|
+
/** Shift+Alt+N (Shift+Option+N): from a note's reference to the note, and back. */
|
|
4664
|
+
const NOTE_KEY = "Alt-Shift-n";
|
|
4501
4665
|
/** Whether story `id` of `state`'s document exists (a number can outlive its note on screen). */
|
|
4502
4666
|
const isNoteStory = (state, id) => !!wordContext(state)?.stories.has(id);
|
|
4503
4667
|
function before(node) {
|
|
@@ -4525,7 +4689,19 @@ export function keyName(event) {
|
|
|
4525
4689
|
const mod = IS_MAC ? event.metaKey : event.ctrlKey;
|
|
4526
4690
|
// Non-Latin layouts: Ctrl+B still means "b" (use the physical key when the character is not a-z);
|
|
4527
4691
|
// with Shift, Ctrl+Shift+7 still means "7" (the character is "&" or "/" depending on layout).
|
|
4528
|
-
|
|
4692
|
+
// Option on macOS types a character (Option+N is a dead key): Alt+N still means "n". But AltGr
|
|
4693
|
+
// (Ctrl+Alt on Windows) typing a character is that character: "[" is AltGr+F on a Hungarian
|
|
4694
|
+
// layout, and must not be read as Ctrl+Alt+F (a footnote).
|
|
4695
|
+
// (macOS has no AltGr: some browsers report Option as AltGraph, and Option is handled above.)
|
|
4696
|
+
// shortcut: Ctrl+Alt giving any other character counts as AltGr (not all browsers report its
|
|
4697
|
+
// state), so Ctrl+Alt+F on a non-Latin layout is not Mod-Alt-f; drop the guess if they all do.
|
|
4698
|
+
const altGraph = !IS_MAC &&
|
|
4699
|
+
(event.getModifierState?.("AltGraph") ||
|
|
4700
|
+
(event.ctrlKey && event.altKey && key.length === 1 && !/^[a-z0-9]$/i.test(key)));
|
|
4701
|
+
if ((mod || event.altKey) &&
|
|
4702
|
+
!altGraph &&
|
|
4703
|
+
!/^[a-z0-9]$/.test(key) &&
|
|
4704
|
+
/^(Key[A-Z]|Digit[0-9])$/.test(event.code))
|
|
4529
4705
|
key = event.code.slice(-1).toLowerCase();
|
|
4530
4706
|
let name = key;
|
|
4531
4707
|
if (event.shiftKey && key !== "Shift")
|
|
@@ -4593,6 +4769,15 @@ function holdsFloat(node) {
|
|
|
4593
4769
|
}
|
|
4594
4770
|
return found;
|
|
4595
4771
|
}
|
|
4772
|
+
/**
|
|
4773
|
+
* A change a node view made to its own drawing (content loaded later, a chart drawn): not input,
|
|
4774
|
+
* never read back. Inside a custom node's box, which is not editable.
|
|
4775
|
+
*/
|
|
4776
|
+
function inNodeView(record) {
|
|
4777
|
+
const { target } = record;
|
|
4778
|
+
const element = target.nodeType === 1 ? target : target.parentElement;
|
|
4779
|
+
return !!element?.closest('[data-foli-custom][contenteditable="false"]');
|
|
4780
|
+
}
|
|
4596
4781
|
/** A change that only adds or removes the view's own marks (a resize handle, a control's frame). */
|
|
4597
4782
|
function handleOnly(record) {
|
|
4598
4783
|
const nodes = [...record.addedNodes, ...record.removedNodes];
|