@folijs/view 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 +33 -0
- package/dist/clipboard.d.ts +3 -1
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js.map +1 -1
- package/dist/layout/flow.d.ts +193 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +789 -101
- package/dist/layout/flow.js.map +1 -1
- package/dist/print.d.ts +22 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +31 -0
- package/dist/print.js.map +1 -0
- package/dist/view.d.ts +220 -7
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1753 -208
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +30 -1
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +193 -40
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/columns.d.ts +23 -0
- package/dist/word/columns.d.ts.map +1 -0
- package/dist/word/columns.js +51 -0
- package/dist/word/columns.js.map +1 -0
- package/dist/word/controls.d.ts +63 -0
- package/dist/word/controls.d.ts.map +1 -0
- package/dist/word/controls.js +411 -0
- package/dist/word/controls.js.map +1 -0
- package/dist/word/edit.d.ts +17 -5
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +157 -26
- package/dist/word/edit.js.map +1 -1
- package/dist/word/matches.d.ts +23 -0
- package/dist/word/matches.d.ts.map +1 -0
- package/dist/word/matches.js +97 -0
- package/dist/word/matches.js.map +1 -0
- package/dist/word/notes.d.ts +105 -0
- package/dist/word/notes.d.ts.map +1 -0
- package/dist/word/notes.js +392 -0
- package/dist/word/notes.js.map +1 -0
- package/dist/word/pages.d.ts +18 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +420 -229
- package/dist/word/pages.js.map +1 -1
- package/dist/word/regions.d.ts +113 -0
- package/dist/word/regions.d.ts.map +1 -0
- package/dist/word/regions.js +374 -0
- package/dist/word/regions.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/layout/flow.ts +945 -110
- package/src/print.ts +48 -0
- package/src/view.ts +1957 -229
- package/src/word/blocks.ts +219 -36
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +175 -23
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +501 -220
- package/src/word/regions.ts +469 -0
package/dist/view.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
import { AllSelection, arrowAcrossLeaf, autolinkHref, CellSelection, cellsAsTable, closeHistory, deleteBackward, deleteForward, deleteSelection, deleteSelectionKeepingTables, extendCellSelection, fitPaste, fitPastedV2, goToCellVertically, insertHardBreak, insertLink, insertPicture, insertText, isSafeHref, keepTablesApart, marksAt, NodeSelection, parseContentV2, pasteCells, redo, Selection, Slice, sectionSettings, setColumnWidth, setComments, setLink, setPictureSize, splitBlock, TextSelection, TransformError, toggleMark, trackState, typedMarks, undo, withEnvelope, wordContext, } from "@folijs/core";
|
|
1
|
+
import { AllSelection, arrowAcrossLeaf, autolinkHref, CellSelection, caretStop, cellsAsTable, closeHistory, controlAt, controlProperties, controlSpot, deleteBackward, deleteForward, deleteSelection, deleteSelectionKeepingTables, ensureStory, extendCellSelection, fieldCodesShown, fieldsIn, fitPaste, fitPastedV2, goToCellVertically, insertControlText, insertHardBreak, insertLink, insertNote, insertPicture, insertText, isSafeHref, keepTablesApart, marksAt, NodeSelection, noteAtSelection, noteNumbers, noteProperties, noteTexts, pageVariant, parseContentV2, pasteCells, redo, referencesIn, Selection, Slice, StoryStep, sameSpot, sectionSettings, setColumnWidth, setComments, setLink, setPictureSize, splitBlock, storyOfSection, TextSelection, TransformError, toggleFieldCodes, toggleMark, trackState, typedMarks, undo, updateFields, withEnvelope, wordContext, } from "@folijs/core";
|
|
2
2
|
import { docFromClipboard, FOLI_MIME, serializeRange } from "./clipboard.js";
|
|
3
|
-
import { contentHeight, DEFAULT_RULES, flowFrom, PX_PER_MM, } from "./layout/flow.js";
|
|
3
|
+
import { contentHeight, DEFAULT_RULES, flowFrom, notePieces, PX_PER_MM, pagesOf, spaceAfterText, } from "./layout/flow.js";
|
|
4
|
+
import { printStyles, SHEET_ATTR } from "./print.js";
|
|
4
5
|
import { PAGE_BREAK_ATTR } from "./render.js";
|
|
5
|
-
import { EMU_PER_PX, imageSource, PICTURE_ATTR } from "./word/blocks.js";
|
|
6
|
+
import { EMU_PER_PX, imageSource, NOTE_NUMBER_ATTR, NOTE_REF_ATTR, PICTURE_ATTR, WORD_COLUMN_BREAK_ATTR, } from "./word/blocks.js";
|
|
7
|
+
import { BAND_CLASS, bandElement, COLUMN_CLASS, columnsKey, columnsPx, } from "./word/columns.js";
|
|
8
|
+
import { boxOf, CONTROL_STYLES, CONTROL_UI_ATTR, ControlChrome, } from "./word/controls.js";
|
|
6
9
|
import { FLOAT_ANCHORS, spacing, WordRenderer, } from "./word/edit.js";
|
|
10
|
+
import { MATCH_STYLES, MatchMarks, rangesInText } from "./word/matches.js";
|
|
11
|
+
import { NOTE_ATTR, PageNotes } from "./word/notes.js";
|
|
7
12
|
import { measureLineRatio } from "./word/pages.js";
|
|
13
|
+
import { PageRegions, REGION_CONTENT_CLASS } from "./word/regions.js";
|
|
8
14
|
import { collect, FRAME_ATTR, frameElement, REPEATED_ATTR, } from "./word/table.js";
|
|
9
15
|
/** What the flow needs of a table: no paragraph spacing or keep rules of its own. */
|
|
10
16
|
const TABLE_WORD = {
|
|
@@ -15,6 +21,7 @@ const TABLE_WORD = {
|
|
|
15
21
|
pageBreakBefore: false,
|
|
16
22
|
style: null,
|
|
17
23
|
contextual: false,
|
|
24
|
+
empty: false,
|
|
18
25
|
};
|
|
19
26
|
/** Word's paragraph spacing for a schema v3 document (`flow`'s `word` spacing). */
|
|
20
27
|
const WORD_RULES = { ...DEFAULT_RULES, spacing: "word" };
|
|
@@ -27,11 +34,13 @@ const CELLS_ATTR = "data-foli-cells";
|
|
|
27
34
|
const PLACEHOLDER_ATTR = "data-foli-placeholder";
|
|
28
35
|
/** On the span of a selected picture (it holds the resize handle). */
|
|
29
36
|
const PICTURE_SELECTED_ATTR = "data-foli-picture-selected";
|
|
37
|
+
/** On what shows a field the selection is in: Word shades it. */
|
|
38
|
+
const FIELD_SELECTED_ATTR = "data-foli-field-selected";
|
|
30
39
|
/** The smallest a picture gets by dragging its handle, in px. */
|
|
31
40
|
const MIN_PICTURE = 8;
|
|
32
41
|
// Generated content is not document text: no caret stops inside it, a zero-height float keeps
|
|
33
42
|
// the caret at the line start, and the empty alt text keeps screen readers on aria-placeholder.
|
|
34
|
-
const STYLES =
|
|
43
|
+
const STYLES = `[${FIELD_SELECTED_ATTR}]{background-color:rgba(0,0,0,.14)}@media print{[${FIELD_SELECTED_ATTR}]{background-color:transparent}}.foli-in-story>.foli-page>:not(.foli-region){opacity:.45}.foli-story-mount{pointer-events:auto;outline:1px dashed GrayText;outline-offset:2px}.foli-story-mount>[contenteditable=true]:focus{outline:none}@media print{.foli-in-story>.foli-page>:not(.foli-region){opacity:1}.foli-story-mount{outline:none}}.${CELL_CLASS}{box-shadow:inset 0 0 0 9999px rgba(0,120,215,.22)}[${CELLS_ATTR}] ::selection{background:transparent}[data-foli-no-alt]{outline:2px dashed #b45309;outline-offset:-2px}@media print{[data-foli-no-alt]{outline:none}}[${PICTURE_SELECTED_ATTR}]{position:relative;outline:2px solid Highlight}[${PICTURE_SELECTED_ATTR}] [data-foli-resize]{position:absolute;right:-5px;bottom:-5px;width:10px;height:10px;box-sizing:border-box;border:1px solid Canvas;background:Highlight;cursor:nwse-resize;forced-color-adjust:none}@media print{[${PICTURE_SELECTED_ATTR}]{outline:none}[${PICTURE_SELECTED_ATTR}] [data-foli-resize]{display:none}}[${PLACEHOLDER_ATTR}]::before{content:attr(${PLACEHOLDER_ATTR});content:attr(${PLACEHOLDER_ATTR}) / "";float:inline-start;height:0;pointer-events:none;color:GrayText}${CONTROL_STYLES}${MATCH_STYLES}`;
|
|
35
44
|
/**
|
|
36
45
|
* Adds the view's stylesheet once per document or shadow root, when a placeholder is first shown
|
|
37
46
|
* (by then the host is usually mounted where it will stay).
|
|
@@ -53,31 +62,16 @@ function injectStyles(host) {
|
|
|
53
62
|
const VIEW_ATTR = "data-foli-view";
|
|
54
63
|
let viewCount = 0;
|
|
55
64
|
/**
|
|
56
|
-
* Print rules for the page settings of the view whose host has `VIEW_ATTR` = `id
|
|
57
|
-
*
|
|
58
|
-
* margins (the page's padding holds them). A page is exactly as tall as a sheet (the flow keeps content inside it; the exact height absorbs its
|
|
59
|
-
* half-pixel slack), with no gap after it. Content taller than a page (a line taller than a
|
|
60
|
-
* page) is not cut: it prints over the next sheet.
|
|
65
|
+
* Print rules for the page settings of the view whose host has `VIEW_ATTR` = `id` (`print.ts`,
|
|
66
|
+
* shared with the read-only renderer and so with `@folijs/pdf`).
|
|
61
67
|
* shortcut: `@page` is per document: with several editors of different page sizes on one web page, the last one's sheet size wins (each keeps its own page rules); print one editor per document until that is needed.
|
|
62
68
|
*/
|
|
63
|
-
function
|
|
64
|
-
const { width, height }
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const named = sheets.every((sheet) => sheet.css === last)
|
|
70
|
-
? ""
|
|
71
|
-
: sheets
|
|
72
|
-
.map(({ settings: { pageSize: size }, index }) => {
|
|
73
|
-
const name = `foli-${id}-${index}`;
|
|
74
|
-
return (`@page ${name}{size:${size.width}mm ${size.height}mm;margin:0}` +
|
|
75
|
-
`${pages}[${SHEET_ATTR}="${index}"]{page:${name};height:${size.height}mm!important}`);
|
|
76
|
-
})
|
|
77
|
-
.join("");
|
|
78
|
-
return (`@media print{@page{size:${width}mm ${height}mm;margin:0}` +
|
|
79
|
-
`${pages}{height:${height}mm!important;margin-block:0!important;` +
|
|
80
|
-
`break-after:page;break-inside:avoid}${pages}:last-child{break-after:auto}${named}}`);
|
|
69
|
+
function viewPrintStyles(settings, sheets, id) {
|
|
70
|
+
const mm = ({ width, height }) => ({
|
|
71
|
+
width: `${width}mm`,
|
|
72
|
+
height: `${height}mm`,
|
|
73
|
+
});
|
|
74
|
+
return printStyles(`[${VIEW_ATTR}="${id}"]>.foli-page`, mm(settings.pageSize), sheets.map(({ settings: { pageSize }, index }) => ({ ...mm(pageSize), index })), id);
|
|
81
75
|
}
|
|
82
76
|
/** Whether `node` is inside an element of `stop` that is not displayed (hidden deleted text). */
|
|
83
77
|
function hiddenIn(node, stop) {
|
|
@@ -86,8 +80,8 @@ function hiddenIn(node, stop) {
|
|
|
86
80
|
return true;
|
|
87
81
|
return false;
|
|
88
82
|
}
|
|
89
|
-
/** The
|
|
90
|
-
const
|
|
83
|
+
/** The fields whose results depend on where things fall on the pages. */
|
|
84
|
+
const PAGE_DEPENDENT = new Set(["PAGE", "NUMPAGES", "SECTIONPAGES", "PAGEREF"]);
|
|
91
85
|
function sheetOf(settings, index) {
|
|
92
86
|
const { pageSize, margins } = settings;
|
|
93
87
|
const css =
|
|
@@ -100,62 +94,143 @@ function sheetOf(settings, index) {
|
|
|
100
94
|
}
|
|
101
95
|
/** The style each page element was last given (its `cssText` reads back normalized). */
|
|
102
96
|
const pageSheets = new WeakMap();
|
|
97
|
+
/** The margins a header or footer gave each page element, as last written. */
|
|
98
|
+
const pagePads = new WeakMap();
|
|
99
|
+
/** What a view without pages gives its one flow of content. */
|
|
100
|
+
const UNPAGED_CSS = "display: flow-root; position: relative;";
|
|
101
|
+
const slotOf = (fragment) => ({
|
|
102
|
+
band: fragment.band ?? 0,
|
|
103
|
+
column: fragment.column ?? 0,
|
|
104
|
+
lead: fragment.lead,
|
|
105
|
+
mark: !!fragment.mark,
|
|
106
|
+
});
|
|
107
|
+
/** The document's sections and the blocks of each. */
|
|
108
|
+
function sectionSpans(doc, last) {
|
|
109
|
+
const spans = [];
|
|
110
|
+
let from = 0;
|
|
111
|
+
doc.forEach((node, _, i) => {
|
|
112
|
+
const own = node.attrs.section;
|
|
113
|
+
if (!own)
|
|
114
|
+
return;
|
|
115
|
+
spans.push({ setup: own, from, to: i + 1 });
|
|
116
|
+
from = i + 1;
|
|
117
|
+
});
|
|
118
|
+
if (from < doc.childCount || !spans.length)
|
|
119
|
+
spans.push({ setup: last, from, to: doc.childCount });
|
|
120
|
+
return spans;
|
|
121
|
+
}
|
|
103
122
|
/**
|
|
104
123
|
* The section of each top-level block (the section a paragraph's `section` ends; the
|
|
105
|
-
* envelope's for the blocks after the last such paragraph)
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* add a blank page), as `renderDocument` lays sections out.
|
|
124
|
+
* envelope's for the blocks after the last such paragraph), as `renderDocument` lays sections
|
|
125
|
+
* out. By position: two paragraphs may end sections with one object. `box` gives a page's text
|
|
126
|
+
* height: null for a view without pages.
|
|
109
127
|
*/
|
|
110
|
-
function sectionLayouts(
|
|
128
|
+
function sectionLayouts(spans, box) {
|
|
111
129
|
const sheets = new Map();
|
|
112
130
|
const bySection = new Map();
|
|
113
131
|
const sheet = (section) => {
|
|
114
132
|
let found = bySection.get(section);
|
|
115
133
|
if (!found) {
|
|
116
134
|
const settings = sectionSettings(section);
|
|
117
|
-
const css = sheetOf(settings, 0).css;
|
|
118
|
-
found = sheets.get(css) ?? sheetOf(settings, sheets.size);
|
|
135
|
+
const css = box ? sheetOf(settings, 0).css : UNPAGED_CSS;
|
|
136
|
+
found = sheets.get(css) ?? { ...sheetOf(settings, sheets.size), css };
|
|
119
137
|
sheets.set(css, found);
|
|
120
138
|
bySection.set(section, found);
|
|
121
139
|
}
|
|
122
140
|
return found;
|
|
123
141
|
};
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
142
|
+
const sheetByFlow = new Map();
|
|
143
|
+
const out = [];
|
|
144
|
+
spans.forEach(({ setup, from: start, to }, s) => {
|
|
145
|
+
// Indexes in document order: the first page setup is 0.
|
|
146
|
+
const own = sheet(setup);
|
|
147
|
+
const next = spans[s + 1]?.setup;
|
|
148
|
+
const columns = columnsPx(setup);
|
|
149
|
+
const count = columns.widths.length;
|
|
150
|
+
const balanced = !!next && count > 1 && next.type === "continuous" && sheet(next).css === own.css;
|
|
151
|
+
const flow = {
|
|
152
|
+
index: s,
|
|
153
|
+
start: setup.type ?? "nextPage",
|
|
154
|
+
columns: count,
|
|
155
|
+
balanced,
|
|
156
|
+
pageStart: setup.pageNumbers?.start,
|
|
157
|
+
box: (page) => ({ height: box ? box(s, page) : Number.POSITIVE_INFINITY }),
|
|
158
|
+
samePage: (other) => sheetByFlow.get(other)?.css === own.css,
|
|
159
|
+
};
|
|
160
|
+
sheetByFlow.set(flow, own);
|
|
161
|
+
const section = {
|
|
162
|
+
sheet: own,
|
|
163
|
+
columns,
|
|
164
|
+
unequal: new Set(columns.widths).size > 1,
|
|
165
|
+
flow,
|
|
166
|
+
key: `${own.css}|${flow.start}|${flow.pageStart ?? ""}|${balanced}|${columnsKey(columns)}`,
|
|
167
|
+
};
|
|
168
|
+
for (let i = start; i < to; i++)
|
|
169
|
+
out[i] = { sheet: own, section, first: i === start };
|
|
140
170
|
});
|
|
171
|
+
return out;
|
|
141
172
|
}
|
|
142
173
|
/**
|
|
143
|
-
*
|
|
144
|
-
* page setup
|
|
174
|
+
* Where blocks are before a flow: where they were (`known`), moved where needed so that each
|
|
175
|
+
* page holds blocks of one page setup and each section has a band of its own: a block is
|
|
176
|
+
* measured in a column of its own section's width.
|
|
145
177
|
*/
|
|
146
|
-
function
|
|
147
|
-
|
|
148
|
-
let
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
178
|
+
function initialPlaces(pages, layout, known) {
|
|
179
|
+
let page = 0;
|
|
180
|
+
let band = 0;
|
|
181
|
+
let column = 0;
|
|
182
|
+
let section;
|
|
183
|
+
const slots = [];
|
|
184
|
+
const placed = pages.map((list, i) => {
|
|
185
|
+
const own = layout[i].section;
|
|
186
|
+
const out = [];
|
|
187
|
+
slots[i] = out;
|
|
188
|
+
return list.map((p, f) => {
|
|
189
|
+
let target = Math.max(p, page);
|
|
190
|
+
if (target === page && section && section !== own) {
|
|
191
|
+
// Only the last page used so far has a setup: the next one is free.
|
|
192
|
+
if (section.sheet.css !== own.sheet.css)
|
|
193
|
+
target++;
|
|
194
|
+
else {
|
|
195
|
+
band++;
|
|
196
|
+
column = 0;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if (target > page) {
|
|
200
|
+
page = target;
|
|
201
|
+
band = 0;
|
|
202
|
+
column = 0;
|
|
203
|
+
}
|
|
204
|
+
section = own;
|
|
205
|
+
const was = known[i]?.[f];
|
|
206
|
+
column = Math.min(Math.max(column, was?.column ?? 0), own.columns.widths.length - 1);
|
|
207
|
+
out.push({
|
|
208
|
+
band,
|
|
209
|
+
column,
|
|
210
|
+
lead: was?.band === band ? was.lead : undefined,
|
|
211
|
+
mark: was?.mark ?? false,
|
|
212
|
+
});
|
|
213
|
+
return page;
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
return { pages: placed, slots };
|
|
217
|
+
}
|
|
218
|
+
/** Makes `list` the children of `parent`, moving as little as possible; returns what it removed. */
|
|
219
|
+
function fillElement(parent, list, removed) {
|
|
220
|
+
let at = parent.firstChild;
|
|
221
|
+
for (const dom of list) {
|
|
222
|
+
if (dom === at)
|
|
223
|
+
at = at.nextSibling;
|
|
224
|
+
else
|
|
225
|
+
parent.insertBefore(dom, at);
|
|
226
|
+
}
|
|
227
|
+
// What is left after the last placed one: strays, and blocks of later pages.
|
|
228
|
+
while (at) {
|
|
229
|
+
const next = at.nextSibling;
|
|
230
|
+
at.remove();
|
|
231
|
+
removed.add(at);
|
|
232
|
+
at = next;
|
|
233
|
+
}
|
|
159
234
|
}
|
|
160
235
|
/**
|
|
161
236
|
* The editable view of an `Editor` from `@folijs/core`: renders its state into one
|
|
@@ -185,6 +260,10 @@ export class EditorView {
|
|
|
185
260
|
cellCaret = null;
|
|
186
261
|
/** The pages of each top-level item (a paragraph's fragments, a table's wrappers), by node. */
|
|
187
262
|
itemPages = new Map();
|
|
263
|
+
/** Where on its page each of those is (band and column). */
|
|
264
|
+
itemSlots = new Map();
|
|
265
|
+
/** Each page's band elements, as last placed. */
|
|
266
|
+
bands = new WeakMap();
|
|
188
267
|
/** Each table as last rendered, by node. */
|
|
189
268
|
frames = new Map();
|
|
190
269
|
/** The table each wrapper element draws (part of). */
|
|
@@ -236,9 +315,12 @@ export class EditorView {
|
|
|
236
315
|
resizeObserver;
|
|
237
316
|
observed = new Set();
|
|
238
317
|
relayoutPending = false;
|
|
318
|
+
/** A composition runs in this view. */
|
|
239
319
|
composing = false;
|
|
240
320
|
/** Set by Ctrl/Cmd+Shift+V for the paste event it triggers. */
|
|
241
321
|
plainPaste = false;
|
|
322
|
+
/** How deep `renumberNotes` has rendered again. */
|
|
323
|
+
renumbering = 0;
|
|
242
324
|
/** Blocks whose DOM the browser changed: the next render rebuilds them. */
|
|
243
325
|
dirty = new Set();
|
|
244
326
|
/** DOM mutations not yet read back (kept while a composition is running). */
|
|
@@ -267,14 +349,97 @@ export class EditorView {
|
|
|
267
349
|
resource;
|
|
268
350
|
/** How tracked changes show. */
|
|
269
351
|
review = "markup";
|
|
352
|
+
/** Whether fields show their codes (`toggleFieldCodes`). */
|
|
353
|
+
fieldCodes = false;
|
|
354
|
+
/** The elements shaded for the fields the selection is in. */
|
|
355
|
+
shadedFields = [];
|
|
270
356
|
/** Fonts' normal line height ratios, measured once per family (Word's single spacing). */
|
|
271
357
|
ratios = new Map();
|
|
272
358
|
/** Web fonts loaded since the v3 blocks were drawn: the next render draws them again. */
|
|
273
359
|
fontsChanged = false;
|
|
360
|
+
/** Whether content goes on pages (`EditorViewOptions.paged`). */
|
|
361
|
+
paged;
|
|
362
|
+
/** The headers and footers of the pages; none without pages. */
|
|
363
|
+
regions;
|
|
364
|
+
/** Each page as last flowed. */
|
|
365
|
+
facts = [];
|
|
366
|
+
/** The header and footer element of each page, as last drawn. */
|
|
367
|
+
pageRegions = [];
|
|
368
|
+
/** Each page's margins, when a header or footer pushes them. */
|
|
369
|
+
pageMargins = [];
|
|
370
|
+
/** What the header and footer heights were when last laid out (`PageRegions.key`). */
|
|
371
|
+
regionsKey = "";
|
|
372
|
+
/** What `regionsKey` was computed from. */
|
|
373
|
+
regionsFor = null;
|
|
374
|
+
/** Footnotes and endnotes on the pages; none without pages. */
|
|
375
|
+
notes;
|
|
376
|
+
/** What each page shows of them. */
|
|
377
|
+
pageNotes = [];
|
|
378
|
+
/** Their elements by what they show. */
|
|
379
|
+
noteElements = new Map();
|
|
380
|
+
/** The page each note starts on, as last flowed: numbers can restart on each. */
|
|
381
|
+
notePages = new Map();
|
|
382
|
+
/** What the note references showed when last drawn. */
|
|
383
|
+
shownNotes = "";
|
|
384
|
+
/** The layout and boxes of the last flow, to draw notes again from. */
|
|
385
|
+
flowLayout = [];
|
|
386
|
+
flowBoxes = [];
|
|
387
|
+
/** What this view's note mark shows (`EditorViewOptions.noteMark`). */
|
|
388
|
+
noteMark;
|
|
389
|
+
/** Puts everything on its page again, as the last render left it. */
|
|
390
|
+
placeAgain = null;
|
|
391
|
+
/** The story being edited on its page, if any (ADR-0007). */
|
|
392
|
+
mount = null;
|
|
393
|
+
/** What find shows, and which tree this view draws of it (a mounted story's view: its own). */
|
|
394
|
+
found = null;
|
|
395
|
+
matchStory = null;
|
|
396
|
+
matchMarks;
|
|
397
|
+
/** The pages marked, as a key; the frame waiting to look again. */
|
|
398
|
+
matchPages = "";
|
|
399
|
+
matchFrame = 0;
|
|
400
|
+
/** The body's selection before the press of a double click in a page's margin. */
|
|
401
|
+
pressSelection = null;
|
|
402
|
+
/** The listeners on the host, to remove them. */
|
|
403
|
+
listeners = [];
|
|
404
|
+
/** The frames, titles and pop-ups of content controls. */
|
|
405
|
+
controlChrome;
|
|
274
406
|
constructor(host, editor, options = {}) {
|
|
275
407
|
this.host = host;
|
|
276
408
|
this.editor = editor;
|
|
277
409
|
this.state = editor.state;
|
|
410
|
+
this.matchMarks = new MatchMarks(host.ownerDocument);
|
|
411
|
+
this.paged = options.paged !== false;
|
|
412
|
+
this.noteMark = options.noteMark;
|
|
413
|
+
const view = this;
|
|
414
|
+
this.controlChrome = new ControlChrome({
|
|
415
|
+
host,
|
|
416
|
+
editor,
|
|
417
|
+
state: () => view.state,
|
|
418
|
+
editable: () => view.editable && !view.mount,
|
|
419
|
+
pages: () => view.pages,
|
|
420
|
+
scale: () => view.scale(),
|
|
421
|
+
rects: (range) => view.controlRects(range),
|
|
422
|
+
contentRects: (range) => view.controlContentRects(range),
|
|
423
|
+
posFromDOM: (node, offset) => view.posFromDOM(node, offset),
|
|
424
|
+
focus: () => view.focus(),
|
|
425
|
+
injectStyles: () => injectStyles(host),
|
|
426
|
+
});
|
|
427
|
+
const storyOptions = {
|
|
428
|
+
get review() {
|
|
429
|
+
return view.review;
|
|
430
|
+
},
|
|
431
|
+
get fieldCodes() {
|
|
432
|
+
return view.fieldCodes;
|
|
433
|
+
},
|
|
434
|
+
lineRatio: (family) => this.lineRatio(family),
|
|
435
|
+
resource: (key, resource) => this.resource?.(key, resource),
|
|
436
|
+
measureIn: () => this.pages[0] ?? host,
|
|
437
|
+
scale: () => this.scale(),
|
|
438
|
+
};
|
|
439
|
+
this.regions = this.paged
|
|
440
|
+
? new PageRegions(host.ownerDocument, storyOptions, new WeakMap())
|
|
441
|
+
: null;
|
|
442
|
+
this.notes = this.paged ? new PageNotes(host.ownerDocument, storyOptions, new WeakMap()) : null;
|
|
278
443
|
host.setAttribute("role", "textbox");
|
|
279
444
|
host.setAttribute("aria-multiline", "true");
|
|
280
445
|
host.setAttribute(VIEW_ATTR, this.id);
|
|
@@ -286,6 +451,7 @@ export class EditorView {
|
|
|
286
451
|
host.style.overflowWrap = "break-word";
|
|
287
452
|
host.replaceChildren();
|
|
288
453
|
this.review = trackState(this.state)?.view ?? "markup";
|
|
454
|
+
this.fieldCodes = fieldCodesShown(this.state);
|
|
289
455
|
this.resizeObserver =
|
|
290
456
|
typeof ResizeObserver === "undefined" ? null : new ResizeObserver(this.onResize);
|
|
291
457
|
this.observer = new MutationObserver(this.onMutations);
|
|
@@ -294,27 +460,43 @@ export class EditorView {
|
|
|
294
460
|
this.resource = options.resource ?? undefined;
|
|
295
461
|
this.render();
|
|
296
462
|
this.setOptions(options);
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
463
|
+
this.listen("beforeinput", this.onBeforeInput);
|
|
464
|
+
this.listen("focus", this.onFocus);
|
|
465
|
+
this.listen("keydown", this.onKeyDown);
|
|
466
|
+
this.listen("input", this.onInput);
|
|
467
|
+
this.listen("compositionstart", this.onCompositionStart);
|
|
468
|
+
this.listen("compositionend", this.onCompositionEnd);
|
|
469
|
+
this.listen("blur", this.onBlur);
|
|
470
|
+
this.listen("mousedown", this.onPress);
|
|
471
|
+
this.listen("mousemove", this.onMouseMove);
|
|
472
|
+
this.listen("mouseleave", () => this.controlChrome.hover(null));
|
|
473
|
+
this.listen("copy", this.onCopy);
|
|
474
|
+
this.listen("cut", this.onCut);
|
|
475
|
+
this.listen("paste", this.onPaste);
|
|
476
|
+
this.listen("dragover", this.onDragOver);
|
|
477
|
+
this.listen("drop", this.onDrop);
|
|
478
|
+
this.listen("click", this.onClick);
|
|
479
|
+
this.listen("auxclick", this.onClick);
|
|
480
|
+
this.claimStories();
|
|
313
481
|
host.ownerDocument.addEventListener("selectionchange", this.onSelectionChange);
|
|
314
482
|
host.ownerDocument.fonts?.addEventListener("loadingdone", this.onFontsLoaded);
|
|
315
483
|
this.unsubscribe = editor.subscribe((state) => this.update(state));
|
|
316
484
|
}
|
|
485
|
+
/** Adds a listener for the body's events: a story open on a page has its own view. */
|
|
486
|
+
listen(type, handler) {
|
|
487
|
+
const listener = (event) => {
|
|
488
|
+
if (!this.mount?.wrapper.contains(event.target))
|
|
489
|
+
handler(event);
|
|
490
|
+
};
|
|
491
|
+
this.host.addEventListener(type, listener);
|
|
492
|
+
this.listeners.push([type, listener]);
|
|
493
|
+
}
|
|
317
494
|
destroy() {
|
|
495
|
+
this.closeStory(false);
|
|
496
|
+
this.showFound(null);
|
|
497
|
+
this.controlChrome.destroy();
|
|
498
|
+
if (this.editor.storyView === this.stories)
|
|
499
|
+
this.editor.storyView = null;
|
|
318
500
|
this.destroyed = true;
|
|
319
501
|
this.unsubscribe();
|
|
320
502
|
this.host.ownerDocument.fonts?.removeEventListener("loadingdone", this.onFontsLoaded);
|
|
@@ -323,24 +505,10 @@ export class EditorView {
|
|
|
323
505
|
for (const style of this.printStyles)
|
|
324
506
|
style.remove();
|
|
325
507
|
const { host } = this;
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
host.removeEventListener("keydown", this.onKeyDown);
|
|
329
|
-
host.removeEventListener("input", this.onInput);
|
|
330
|
-
host.removeEventListener("compositionstart", this.onCompositionStart);
|
|
331
|
-
host.removeEventListener("compositionend", this.onCompositionEnd);
|
|
332
|
-
host.removeEventListener("blur", this.onBlur);
|
|
333
|
-
host.removeEventListener("mousedown", this.onMouseDown);
|
|
334
|
-
host.removeEventListener("mousemove", this.onMouseMove);
|
|
508
|
+
for (const [type, listener] of this.listeners)
|
|
509
|
+
host.removeEventListener(type, listener);
|
|
335
510
|
this.endDrag();
|
|
336
|
-
host.removeEventListener("copy", this.onCopy);
|
|
337
|
-
host.removeEventListener("cut", this.onCut);
|
|
338
|
-
host.removeEventListener("paste", this.onPaste);
|
|
339
|
-
host.removeEventListener("dragover", this.onDragOver);
|
|
340
|
-
host.removeEventListener("drop", this.onDrop);
|
|
341
511
|
this.endResize();
|
|
342
|
-
host.removeEventListener("click", this.onClick);
|
|
343
|
-
host.removeEventListener("auxclick", this.onClick);
|
|
344
512
|
host.ownerDocument.removeEventListener("selectionchange", this.onSelectionChange);
|
|
345
513
|
const owned = [
|
|
346
514
|
"contenteditable",
|
|
@@ -362,6 +530,10 @@ export class EditorView {
|
|
|
362
530
|
}
|
|
363
531
|
/** Focuses the host, shows the model selection in it and scrolls it into view. */
|
|
364
532
|
focus() {
|
|
533
|
+
if (this.mount) {
|
|
534
|
+
this.mount.view.focus();
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
365
537
|
this.host.focus();
|
|
366
538
|
this.writeSelection(true);
|
|
367
539
|
// The focus handler scrolls only when it writes the selection itself, and a DOM selection
|
|
@@ -376,6 +548,8 @@ export class EditorView {
|
|
|
376
548
|
if (!options.editable && this.composing)
|
|
377
549
|
this.onCompositionEnd();
|
|
378
550
|
this.editable = options.editable;
|
|
551
|
+
if (!this.editable)
|
|
552
|
+
this.closeStory(false);
|
|
379
553
|
// A selected picture's handle only while it can be resized.
|
|
380
554
|
this.endResize();
|
|
381
555
|
this.showSelectedPicture();
|
|
@@ -384,6 +558,10 @@ export class EditorView {
|
|
|
384
558
|
this.placeholder = options.placeholder;
|
|
385
559
|
if (options.upload !== undefined)
|
|
386
560
|
this.upload = options.upload ?? undefined;
|
|
561
|
+
this.mount?.view.setOptions({
|
|
562
|
+
...(options.upload !== undefined ? { upload: options.upload } : {}),
|
|
563
|
+
...(options.resource !== undefined ? { resource: options.resource } : {}),
|
|
564
|
+
});
|
|
387
565
|
// null clears the resolver (stored as none): the same as none is no change.
|
|
388
566
|
const resource = options.resource ?? undefined;
|
|
389
567
|
if (options.resource !== undefined && resource !== this.resource) {
|
|
@@ -399,8 +577,8 @@ export class EditorView {
|
|
|
399
577
|
}
|
|
400
578
|
}
|
|
401
579
|
// Read-only: no caret and no browser editing; the text stays selectable and copyable.
|
|
402
|
-
host.setAttribute("contenteditable", String(this.editable));
|
|
403
|
-
if (this.editable)
|
|
580
|
+
host.setAttribute("contenteditable", String(this.editable && !this.mount));
|
|
581
|
+
if (this.editable && !this.mount)
|
|
404
582
|
host.removeAttribute("aria-readonly");
|
|
405
583
|
else
|
|
406
584
|
host.setAttribute("aria-readonly", "true");
|
|
@@ -438,11 +616,34 @@ export class EditorView {
|
|
|
438
616
|
this.review = review;
|
|
439
617
|
this.word = null;
|
|
440
618
|
}
|
|
441
|
-
|
|
619
|
+
// Codes shown or hidden: every block and story drawn again.
|
|
620
|
+
const codes = fieldCodesShown(state);
|
|
621
|
+
const codesChanged = codes !== this.fieldCodes;
|
|
622
|
+
if (codesChanged) {
|
|
623
|
+
this.fieldCodes = codes;
|
|
624
|
+
this.word = null;
|
|
625
|
+
}
|
|
626
|
+
const before = wordContext(this.state)?.envelope;
|
|
627
|
+
const envelope = wordContext(state)?.envelope;
|
|
628
|
+
// The last section's setup and the settings live in the envelope: changing them leaves the
|
|
629
|
+
// tree as it is. So does a story's change, which only its occurrences show.
|
|
630
|
+
const docChanged = state.doc !== this.state.doc ||
|
|
631
|
+
reviewChanged ||
|
|
632
|
+
codesChanged ||
|
|
633
|
+
envelope?.section !== before?.section ||
|
|
634
|
+
envelope?.settings !== before?.settings;
|
|
635
|
+
const storiesChanged = envelope?.stories !== before?.stories;
|
|
442
636
|
const selectionChanged = !state.selection.eq(this.state.selection);
|
|
637
|
+
// Another document: the story being edited closes.
|
|
638
|
+
const lost = !!this.mount && wordContext(state)?.id !== wordContext(this.state)?.id;
|
|
443
639
|
this.state = state;
|
|
444
|
-
if (docChanged)
|
|
640
|
+
if (docChanged || (storiesChanged && !this.redrawRegions()))
|
|
445
641
|
this.render();
|
|
642
|
+
if (lost)
|
|
643
|
+
this.closeStory(true);
|
|
644
|
+
// The open story has its own editor, which hears nothing of this.
|
|
645
|
+
if (codesChanged)
|
|
646
|
+
this.mount?.view.update(this.mount.view.editor.state);
|
|
446
647
|
this.showSelectedNode();
|
|
447
648
|
this.writeSelection(docChanged);
|
|
448
649
|
// shortcut: scrolls for every change while focused; once remote or plugin changes exist, scroll only for local input (a transaction flag).
|
|
@@ -485,32 +686,67 @@ export class EditorView {
|
|
|
485
686
|
* Measurements are cached by node and the flow restarts at the first changed block (5.4).
|
|
486
687
|
*/
|
|
487
688
|
render() {
|
|
689
|
+
try {
|
|
690
|
+
this.renderPages();
|
|
691
|
+
}
|
|
692
|
+
catch (error) {
|
|
693
|
+
// What it had changed is not input: read back, it would render and fail again, for ever.
|
|
694
|
+
this.observer.takeRecords();
|
|
695
|
+
this.pending = [];
|
|
696
|
+
throw error;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
renderPages() {
|
|
488
700
|
const { doc } = this.state;
|
|
489
701
|
const ownerDocument = this.host.ownerDocument;
|
|
490
702
|
// A new document (envelope) gets its renderer; nothing rendered before is reused then. An
|
|
491
703
|
// editor's state always has one (`createEditor`).
|
|
492
704
|
const context = wordContext(this.state);
|
|
493
705
|
// Fonts loaded: a new renderer (new line heights), but not while a composition runs in a
|
|
494
|
-
// block it would redraw.
|
|
495
|
-
const
|
|
706
|
+
// block it would redraw. What only stories or the settings changed leaves blocks as they are.
|
|
707
|
+
const drawn = this.word?.context;
|
|
708
|
+
const renewed = !drawn ||
|
|
709
|
+
drawn.id !== context.id ||
|
|
710
|
+
drawn.resolver !== context.resolver ||
|
|
711
|
+
drawn.envelope.section !== context.envelope.section ||
|
|
712
|
+
drawn.envelope.resources !== context.envelope.resources ||
|
|
713
|
+
drawn.envelope.comments !== context.envelope.comments ||
|
|
714
|
+
(this.fontsChanged && !this.composing);
|
|
496
715
|
if (renewed)
|
|
497
716
|
this.fontsChanged = false;
|
|
498
717
|
let word = this.word;
|
|
718
|
+
if (!renewed)
|
|
719
|
+
word.context = context;
|
|
499
720
|
if (renewed) {
|
|
500
721
|
// A document with pictures: the mark of one without alt text is the view's stylesheet's.
|
|
501
722
|
if (context.envelope.resources)
|
|
502
723
|
injectStyles(this.host);
|
|
503
|
-
word = this.word = new WordRenderer(context, ownerDocument, this.lineRatio, false, this.review, imageSource(context.envelope.resources, this.resource ?? undefined));
|
|
724
|
+
word = this.word = new WordRenderer(context, ownerDocument, this.lineRatio, false, this.review, imageSource(context.envelope.resources, this.resource ?? undefined), this.fieldCodes);
|
|
504
725
|
this.invalidateLayout();
|
|
505
726
|
}
|
|
727
|
+
const references = this.noteReferences();
|
|
728
|
+
word.setNotes(noteTexts(references), this.noteMark?.());
|
|
506
729
|
// The blocks the view renders (paragraphs in table cells among them) and the page flow's
|
|
507
730
|
// items (top-level paragraphs and opaque blocks, and tables).
|
|
508
731
|
const { units, items } = collect(doc, context.resolver);
|
|
509
|
-
word.prepare(units.map((unit) => unit.node))
|
|
510
|
-
|
|
732
|
+
word.prepare(units.map((unit) => unit.node), (i) => {
|
|
733
|
+
const unit = units[i];
|
|
734
|
+
const previous = units[i - 1];
|
|
735
|
+
// Siblings: one ends where the other starts.
|
|
736
|
+
return unit.cell && previous && previous.pos + previous.node.nodeSize === unit.pos
|
|
737
|
+
? { node: previous.node, cell: unit.cell }
|
|
738
|
+
: null;
|
|
739
|
+
});
|
|
740
|
+
// A story's view has none of the body's sections: its one flow is not a page's.
|
|
741
|
+
const spans = sectionSpans(doc, this.paged ? context.envelope.section : {});
|
|
742
|
+
const { regions } = this;
|
|
743
|
+
regions?.setDocument(context.envelope, spans.map((span) => span.setup), context.resolver);
|
|
744
|
+
const { notes } = this;
|
|
745
|
+
notes?.setDocument(context.envelope, spans.map((span) => span.setup), context.resolver, references);
|
|
746
|
+
const layout = sectionLayouts(spans, regions && ((section, page) => regions.boxHeight(section, page)));
|
|
511
747
|
doc.forEach((node, _, i) => {
|
|
512
|
-
const {
|
|
513
|
-
const key = `${
|
|
748
|
+
const { section, first } = layout[i];
|
|
749
|
+
const key = `${section.key}${first ? "|first" : ""}`;
|
|
514
750
|
const before = this.layoutKeys.get(node);
|
|
515
751
|
if (before !== undefined && before !== key)
|
|
516
752
|
this.stale.add(node);
|
|
@@ -630,23 +866,30 @@ export class EditorView {
|
|
|
630
866
|
const desc = blocks[item.unit];
|
|
631
867
|
return desc.fragments.map((_, f) => (last = Math.max(last, before?.[f] ?? last)));
|
|
632
868
|
});
|
|
633
|
-
|
|
869
|
+
const initial = initialPlaces(pages, layout, items.map((_, i) => this.itemSlots.get(doc.child(i))));
|
|
870
|
+
pages = initial.pages;
|
|
871
|
+
let slots = initial.slots;
|
|
634
872
|
const elementsOf = (item, i) => item.frame
|
|
635
873
|
? frames.get(doc.child(i)).wrappers
|
|
636
874
|
: blocks[item.unit].fragments;
|
|
637
|
-
this.place(items.map((item, i) => elementsOf(item, i)), pages, layout);
|
|
875
|
+
this.place(items.map((item, i) => elementsOf(item, i)), pages, slots, layout);
|
|
638
876
|
if (!this.composing) {
|
|
639
877
|
// DOM reads only for blocks without a valid measurement (new, changed or stale ones).
|
|
640
878
|
let scale;
|
|
641
879
|
const layoutPx = () => {
|
|
642
|
-
scale ??=
|
|
880
|
+
scale ??= this.scale();
|
|
643
881
|
return scale;
|
|
644
882
|
};
|
|
883
|
+
// A header or footer of another height moves every page's text: no flow is reused.
|
|
884
|
+
if (regions && this.regionsChanged(regions, spans))
|
|
885
|
+
this.flowed = null;
|
|
645
886
|
if (dropLines)
|
|
646
887
|
for (const desc of blocks) {
|
|
647
888
|
const measure = this.measures.get(desc.node);
|
|
648
|
-
if (measure)
|
|
889
|
+
if (measure) {
|
|
649
890
|
delete measure.lines;
|
|
891
|
+
delete measure.widths;
|
|
892
|
+
}
|
|
650
893
|
}
|
|
651
894
|
const nodes = items.map((_, i) => doc.child(i));
|
|
652
895
|
// A block's spacing depends on its neighbours' styles: reuse stops a block earlier.
|
|
@@ -665,6 +908,39 @@ export class EditorView {
|
|
|
665
908
|
found.lines ??= measureLines(desc, layoutPx());
|
|
666
909
|
return found.lines;
|
|
667
910
|
};
|
|
911
|
+
/** A block's height and lines at a column's width, wherever it is now. */
|
|
912
|
+
const atWidth = (desc, width, kept) => {
|
|
913
|
+
const measure = measureOf(desc);
|
|
914
|
+
const widths = measure.widths ?? new Map();
|
|
915
|
+
measure.widths = widths;
|
|
916
|
+
const known = widths.get(width);
|
|
917
|
+
if (known)
|
|
918
|
+
return known;
|
|
919
|
+
const read = (measure) => {
|
|
920
|
+
for (const { style } of desc.fragments) {
|
|
921
|
+
style.boxSizing = "border-box";
|
|
922
|
+
style.width = `${width}px`;
|
|
923
|
+
}
|
|
924
|
+
const value = measure();
|
|
925
|
+
for (const { style } of desc.fragments) {
|
|
926
|
+
style.boxSizing = "";
|
|
927
|
+
style.width = "";
|
|
928
|
+
}
|
|
929
|
+
return value;
|
|
930
|
+
};
|
|
931
|
+
const { height } = read(() => measureBox(desc, layoutPx()));
|
|
932
|
+
let all;
|
|
933
|
+
const found = {
|
|
934
|
+
height,
|
|
935
|
+
all: () => {
|
|
936
|
+
all ??= read(() => measureLines(desc, layoutPx()));
|
|
937
|
+
return all;
|
|
938
|
+
},
|
|
939
|
+
lines: () => (kept ? [height] : found.all().bottoms),
|
|
940
|
+
};
|
|
941
|
+
widths.set(width, found);
|
|
942
|
+
return found;
|
|
943
|
+
};
|
|
668
944
|
const tableOf = (frame, i) => {
|
|
669
945
|
let found = this.measures.get(frame.node);
|
|
670
946
|
if (!found?.table || this.stale.delete(frame.node)) {
|
|
@@ -681,22 +957,55 @@ export class EditorView {
|
|
|
681
957
|
return found;
|
|
682
958
|
};
|
|
683
959
|
const words = items.map((item) => item.frame ? TABLE_WORD : blocks[item.unit].word);
|
|
960
|
+
const endnotes = notes?.endnotes();
|
|
684
961
|
const boxes = items.map((item, i) => {
|
|
685
|
-
const
|
|
962
|
+
const own = layout[i];
|
|
963
|
+
const pageHeight = contentHeight(own.sheet.settings);
|
|
964
|
+
const index = own.section.flow.index ?? 0;
|
|
965
|
+
const noteOf = (story) => notes?.footnote(story, index) ?? null;
|
|
966
|
+
// Endnotes follow the last block of their section, or of the document.
|
|
967
|
+
const after = layout[i + 1]?.section !== own.section
|
|
968
|
+
? { endnotes: endnotes?.get(index) }
|
|
969
|
+
: {};
|
|
686
970
|
if (item.frame) {
|
|
687
971
|
const frame = item.frame;
|
|
688
972
|
const measure = tableOf(frame, i);
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
return
|
|
973
|
+
const breaks = () => breaksOf(frame, measure, blocks, pageHeight, lines);
|
|
974
|
+
// A page break before the next paragraph ends its page.
|
|
975
|
+
return {
|
|
976
|
+
...tableBox(frame, measure, pageHeight, !!words[i + 1]?.pageBreakBefore, breaks),
|
|
977
|
+
section: own.section.flow,
|
|
978
|
+
notes: tableNotes(frame, measure, blocks, lines, breaks, noteOf),
|
|
979
|
+
...after,
|
|
980
|
+
};
|
|
692
981
|
}
|
|
693
982
|
const desc = blocks[item.unit];
|
|
694
|
-
return
|
|
983
|
+
return {
|
|
984
|
+
...wordBox(words, i, desc, measureOf(desc), () => lines(desc), own, atWidth, context.resolver.addsParagraphSpacing, noteOf),
|
|
985
|
+
...after,
|
|
986
|
+
};
|
|
695
987
|
});
|
|
696
|
-
const
|
|
988
|
+
const rules = {
|
|
989
|
+
...WORD_RULES,
|
|
990
|
+
addSpacing: context.resolver.addsParagraphSpacing,
|
|
991
|
+
evenAndOddHeaders: context.envelope.settings?.evenAndOddHeaders === true,
|
|
992
|
+
notes: notes?.flowRules(),
|
|
993
|
+
};
|
|
994
|
+
const flowHeight = contentHeight(this.editor.settings);
|
|
995
|
+
let result = flowFrom(boxes, flowHeight, rules, reuse);
|
|
996
|
+
let facts = pagesOf(boxes, result);
|
|
997
|
+
// The page count is known once flowed; a header holding it can change what fits.
|
|
998
|
+
for (let pass = 0; regions && pass < 2 && regions.settle(facts); pass++) {
|
|
999
|
+
result = flowFrom(boxes, flowHeight, rules);
|
|
1000
|
+
facts = pagesOf(boxes, result);
|
|
1001
|
+
}
|
|
1002
|
+
this.facts = facts;
|
|
697
1003
|
this.flowed = { nodes, result };
|
|
1004
|
+
this.flowLayout = layout;
|
|
1005
|
+
this.flowBoxes = boxes;
|
|
698
1006
|
this.stale.clear();
|
|
699
1007
|
const flowed = result.fragments;
|
|
1008
|
+
slots = flowed.map((fragments) => fragments.map(slotOf));
|
|
700
1009
|
// Paragraphs: split where their fragments start. Tables: the rows each fragment shows,
|
|
701
1010
|
// and their blocks split where a row is.
|
|
702
1011
|
const splitsOf = new Map();
|
|
@@ -706,8 +1015,13 @@ export class EditorView {
|
|
|
706
1015
|
if (item.unit !== undefined) {
|
|
707
1016
|
const desc = blocks[item.unit];
|
|
708
1017
|
const contentStart = units[item.unit].pos + 1;
|
|
1018
|
+
const { section } = layout[i];
|
|
709
1019
|
splitsOf.set(item.unit, fragments.slice(1).map((fragment) => {
|
|
710
|
-
|
|
1020
|
+
// Line numbers are those of the column the fragment is in.
|
|
1021
|
+
const measured = section.unequal
|
|
1022
|
+
? atWidth(desc, section.columns.widths[fragment.column ?? 0], false).all()
|
|
1023
|
+
: lines(desc);
|
|
1024
|
+
const offset = measured.starts[fragment.fromLine];
|
|
711
1025
|
// Never split a grapheme cluster across pages.
|
|
712
1026
|
return clusterBoundary(doc, contentStart + offset, false) - contentStart;
|
|
713
1027
|
}));
|
|
@@ -748,9 +1062,16 @@ export class EditorView {
|
|
|
748
1062
|
const firsts = items.map((item, i) => item.frame
|
|
749
1063
|
? frames.get(doc.child(i)).wrappers[0]
|
|
750
1064
|
: blocks[item.unit].dom);
|
|
751
|
-
applyGaps(firsts,
|
|
752
|
-
this.
|
|
1065
|
+
applyGaps(firsts, result.gaps);
|
|
1066
|
+
this.drawRegions();
|
|
1067
|
+
this.drawNotes();
|
|
1068
|
+
this.place(items.map((item, i) => elementsOf(item, i)), pages, slots, layout);
|
|
1069
|
+
this.afterPlace();
|
|
753
1070
|
}
|
|
1071
|
+
const placed = items.map((item, i) => elementsOf(item, i));
|
|
1072
|
+
const placedPages = pages;
|
|
1073
|
+
const placedSlots = slots;
|
|
1074
|
+
this.placeAgain = () => this.place(placed, placedPages, placedSlots, layout);
|
|
754
1075
|
// (Wrappers no longer drawn are not in a page: `place` removed them.)
|
|
755
1076
|
this.frames = frames;
|
|
756
1077
|
this.frameAt = items.map((item, i) => {
|
|
@@ -768,6 +1089,7 @@ export class EditorView {
|
|
|
768
1089
|
this.starts = units.map((unit) => unit.pos);
|
|
769
1090
|
this.indexOf = new Map(blocks.map((desc, i) => [desc, i]));
|
|
770
1091
|
this.itemPages = new Map(items.map((_, i) => [doc.child(i), pages[i] ?? []]));
|
|
1092
|
+
this.itemSlots = new Map(items.map((_, i) => [doc.child(i), slots[i] ?? []]));
|
|
771
1093
|
this.owner = new Map(blocks.flatMap((desc) => desc.fragments.map((fragment) => [fragment, desc])));
|
|
772
1094
|
this.unflowed = this.composing;
|
|
773
1095
|
this.observeSizes();
|
|
@@ -778,6 +1100,1013 @@ export class EditorView {
|
|
|
778
1100
|
const records = this.observer.takeRecords();
|
|
779
1101
|
if (this.composing)
|
|
780
1102
|
this.pending.push(...records);
|
|
1103
|
+
this.renumberNotes();
|
|
1104
|
+
}
|
|
1105
|
+
/** The note references shown, with their numbers as the last flow's pages make them. */
|
|
1106
|
+
noteReferences() {
|
|
1107
|
+
if (!this.paged)
|
|
1108
|
+
return [];
|
|
1109
|
+
const { doc } = this.state;
|
|
1110
|
+
const { envelope } = wordContext(this.state);
|
|
1111
|
+
const hideDeleted = this.review !== "markup";
|
|
1112
|
+
const all = noteNumbers(doc, envelope, {
|
|
1113
|
+
hideDeleted,
|
|
1114
|
+
pageOf: (_pos, story) => this.notePages.get(story),
|
|
1115
|
+
});
|
|
1116
|
+
this.shownNotes = JSON.stringify(all.map((n) => n.text));
|
|
1117
|
+
return hideDeleted ? all.filter((n) => !n.deleted) : all;
|
|
1118
|
+
}
|
|
1119
|
+
/**
|
|
1120
|
+
* Numbers that restart on each page depend on the flow just made: when they came out otherwise,
|
|
1121
|
+
* once more with them (their width can move a line), at most twice.
|
|
1122
|
+
*/
|
|
1123
|
+
renumberNotes() {
|
|
1124
|
+
if (!this.notes || this.composing || this.renumbering > 1)
|
|
1125
|
+
return;
|
|
1126
|
+
const { envelope } = wordContext(this.state);
|
|
1127
|
+
const each = this.sections().some((section) => noteProperties(envelope, "footnote", section).restart === "eachPage");
|
|
1128
|
+
if (!each)
|
|
1129
|
+
return;
|
|
1130
|
+
const shown = this.shownNotes;
|
|
1131
|
+
this.noteReferences();
|
|
1132
|
+
if (this.shownNotes === shown)
|
|
1133
|
+
return;
|
|
1134
|
+
this.renumbering++;
|
|
1135
|
+
try {
|
|
1136
|
+
this.renderPages();
|
|
1137
|
+
}
|
|
1138
|
+
finally {
|
|
1139
|
+
this.renumbering = 0;
|
|
1140
|
+
}
|
|
1141
|
+
}
|
|
1142
|
+
/** The section of each page's bands in the last flow, and each note's place on the pages. */
|
|
1143
|
+
drawNotes() {
|
|
1144
|
+
const { notes, flowed, facts, mount } = this;
|
|
1145
|
+
if (!notes)
|
|
1146
|
+
return;
|
|
1147
|
+
const pieces = flowed ? notePieces(flowed.result) : [];
|
|
1148
|
+
const notePages = new Map();
|
|
1149
|
+
for (const piece of pieces)
|
|
1150
|
+
if (!piece.fromLine && !notePages.has(piece.id))
|
|
1151
|
+
notePages.set(piece.id, piece.page);
|
|
1152
|
+
this.notePages = notePages;
|
|
1153
|
+
const bandSections = new Map();
|
|
1154
|
+
flowed?.result.fragments.forEach((fragments, i) => {
|
|
1155
|
+
const index = this.flowLayout[i]?.section.flow.index ?? 0;
|
|
1156
|
+
for (const f of fragments)
|
|
1157
|
+
bandSections.set(`${f.page}|${f.band ?? 0}`, index);
|
|
1158
|
+
});
|
|
1159
|
+
const groups = notes.groups(pieces, (page, band) => bandSections.get(`${page}|${band}`) ??
|
|
1160
|
+
bandSections.get(`${page}|0`) ??
|
|
1161
|
+
facts[page]?.section ??
|
|
1162
|
+
0);
|
|
1163
|
+
const foot = groups.filter((group) => !group.inFlow);
|
|
1164
|
+
const known = this.noteElements;
|
|
1165
|
+
const elements = new Map();
|
|
1166
|
+
const shown = [];
|
|
1167
|
+
const note = mount?.side === "note" ? mount : null;
|
|
1168
|
+
for (const group of groups) {
|
|
1169
|
+
const { settings } = this.sheetOfPage(group.page);
|
|
1170
|
+
const fact = facts[group.page];
|
|
1171
|
+
const bottom = fact?.sectionPages
|
|
1172
|
+
? this.regions.margins(fact.section, fact).bottom
|
|
1173
|
+
: settings.margins.bottom * PX_PER_MM;
|
|
1174
|
+
const place = group.inFlow
|
|
1175
|
+
? null
|
|
1176
|
+
: notes.footPlace({ left: settings.margins.left * PX_PER_MM, bottom }, notes.stacked(group, foot));
|
|
1177
|
+
// Footnotes beneath the text keep the space after the paragraph above them.
|
|
1178
|
+
const lead = group.inFlow && !group.pieces[0]?.end && flowed
|
|
1179
|
+
? spaceAfterText(this.flowBoxes, flowed.result, group.page, group.band, group.column)
|
|
1180
|
+
: 0;
|
|
1181
|
+
const key = `${group.page}|${group.band}|${group.column}|${group.inFlow}|${place?.left}|${place?.bottom}|${lead}|${notes.keyOf(group)}`;
|
|
1182
|
+
let element = known.get(key);
|
|
1183
|
+
if (!element) {
|
|
1184
|
+
element = notes.groupElement(group, place, (piece) => piece.id === note?.id && !piece.fromLine
|
|
1185
|
+
? note.region.element
|
|
1186
|
+
: notes.pieceElement(piece, group), lead);
|
|
1187
|
+
if (note && element.contains(note.wrapper))
|
|
1188
|
+
note.moved = true;
|
|
1189
|
+
}
|
|
1190
|
+
elements.set(key, element);
|
|
1191
|
+
const page = shown[group.page] ?? { foot: [], flow: new Map() };
|
|
1192
|
+
shown[group.page] = page;
|
|
1193
|
+
if (!group.inFlow)
|
|
1194
|
+
page.foot.push(element);
|
|
1195
|
+
else {
|
|
1196
|
+
const slot = `${group.band}|${group.column}`;
|
|
1197
|
+
page.flow.set(slot, [...(page.flow.get(slot) ?? []), element]);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
this.noteElements = elements;
|
|
1201
|
+
this.pageNotes = Array.from({ length: shown.length }, (_, p) => shown[p] ?? { foot: [], flow: new Map() });
|
|
1202
|
+
if (!note)
|
|
1203
|
+
return;
|
|
1204
|
+
// Its reference went (undone, or hidden with the changes): the note closes.
|
|
1205
|
+
const page = notePages.get(note.id);
|
|
1206
|
+
if (page === undefined)
|
|
1207
|
+
queueMicrotask(() => this.closeStory(true));
|
|
1208
|
+
else
|
|
1209
|
+
note.page = page;
|
|
1210
|
+
}
|
|
1211
|
+
/** Layout px per px on screen: what a scaled ancestor (`transform`, `zoom`) multiplies by. */
|
|
1212
|
+
scale() {
|
|
1213
|
+
const page = this.pages[0];
|
|
1214
|
+
if (!page)
|
|
1215
|
+
return 1;
|
|
1216
|
+
return this.paged
|
|
1217
|
+
? layoutScale(page, this.sheets[0].settings)
|
|
1218
|
+
: page.getBoundingClientRect().width / page.offsetWidth || 1;
|
|
1219
|
+
}
|
|
1220
|
+
/** Whether the headers' and footers' heights differ from when the pages were last flowed. */
|
|
1221
|
+
regionsChanged(regions, spans) {
|
|
1222
|
+
const { stories, settings } = wordContext(this.state).envelope;
|
|
1223
|
+
const sections = spans.map((span) => span.setup);
|
|
1224
|
+
const known = this.regionsFor;
|
|
1225
|
+
if (known &&
|
|
1226
|
+
known.stories === stories &&
|
|
1227
|
+
known.settings === settings &&
|
|
1228
|
+
known.sections.length === sections.length &&
|
|
1229
|
+
known.sections.every((section, i) => section === sections[i]))
|
|
1230
|
+
return false;
|
|
1231
|
+
this.regionsFor = { stories, settings, sections };
|
|
1232
|
+
const key = `${regions.key()}|${this.notes?.key() ?? ""}`;
|
|
1233
|
+
// Even and odd headers also decide the blank pages: the flow depends on them.
|
|
1234
|
+
const changed = key !== this.regionsKey ||
|
|
1235
|
+
(known?.settings !== settings &&
|
|
1236
|
+
known?.settings?.evenAndOddHeaders !==
|
|
1237
|
+
settings?.evenAndOddHeaders);
|
|
1238
|
+
this.regionsKey = key;
|
|
1239
|
+
return changed;
|
|
1240
|
+
}
|
|
1241
|
+
/**
|
|
1242
|
+
* Gives every page the header and footer the last flow says, redrawing those that changed. The
|
|
1243
|
+
* one holding the story being edited stays; it follows the story if its page no longer shows it.
|
|
1244
|
+
*/
|
|
1245
|
+
drawRegions() {
|
|
1246
|
+
const { regions, facts } = this;
|
|
1247
|
+
// A note open for editing is `drawNotes`' to look after.
|
|
1248
|
+
const mount = this.mount?.side === "note" ? null : this.mount;
|
|
1249
|
+
if (!regions)
|
|
1250
|
+
return;
|
|
1251
|
+
const sides = ["header", "footer"];
|
|
1252
|
+
const wanted = facts.map((page) =>
|
|
1253
|
+
// Word's blank page before an even or odd page section has neither.
|
|
1254
|
+
page.sectionPages
|
|
1255
|
+
? {
|
|
1256
|
+
header: regions.drawing(page.section, "header", page),
|
|
1257
|
+
footer: regions.drawing(page.section, "footer", page),
|
|
1258
|
+
}
|
|
1259
|
+
: { header: null, footer: null });
|
|
1260
|
+
const shows = (p) => !!mount && wanted[p]?.[mount.side]?.id === mount.id;
|
|
1261
|
+
// Its page, while it shows it. Replaced there (the section unlinked, another variant turned
|
|
1262
|
+
// on) it does not follow the old story to another page: the page's own opens.
|
|
1263
|
+
const replaced = !!mount && facts[mount.page]?.section === mount.section;
|
|
1264
|
+
const home = !mount
|
|
1265
|
+
? -1
|
|
1266
|
+
: shows(mount.page)
|
|
1267
|
+
? mount.page
|
|
1268
|
+
: replaced
|
|
1269
|
+
? -1
|
|
1270
|
+
: wanted.findIndex((_, p) => shows(p));
|
|
1271
|
+
this.pageRegions = facts.map((page, p) => {
|
|
1272
|
+
const own = {};
|
|
1273
|
+
for (const side of sides) {
|
|
1274
|
+
const drawing = wanted[p]?.[side];
|
|
1275
|
+
if (!drawing)
|
|
1276
|
+
continue;
|
|
1277
|
+
if (mount && p === home && side === mount.side) {
|
|
1278
|
+
own[side] = mount.region;
|
|
1279
|
+
if (mount.region.drawing !== drawing.content) {
|
|
1280
|
+
mount.region.drawing = drawing.content;
|
|
1281
|
+
const copy = drawing.content.content.cloneNode(true);
|
|
1282
|
+
copy.style.display = "none";
|
|
1283
|
+
mount.region.element
|
|
1284
|
+
.querySelector(`:scope > .${REGION_CONTENT_CLASS}`)
|
|
1285
|
+
?.replaceWith(copy);
|
|
1286
|
+
mount.wrapper.style.paddingBottom = copy.style.paddingBottom;
|
|
1287
|
+
}
|
|
1288
|
+
continue;
|
|
1289
|
+
}
|
|
1290
|
+
const known = this.pageRegions[p]?.[side];
|
|
1291
|
+
if (known?.drawing === drawing.content && known.section === page.section) {
|
|
1292
|
+
own[side] = known;
|
|
1293
|
+
continue;
|
|
1294
|
+
}
|
|
1295
|
+
const element = regions.element(page.section, side);
|
|
1296
|
+
element.append(drawing.content.content.cloneNode(true));
|
|
1297
|
+
own[side] = { element, drawing: drawing.content, section: page.section, id: drawing.id };
|
|
1298
|
+
}
|
|
1299
|
+
return own;
|
|
1300
|
+
});
|
|
1301
|
+
this.pageMargins = facts.map((page, p) => {
|
|
1302
|
+
if (!page.sectionPages)
|
|
1303
|
+
return { top: "", bottom: "" };
|
|
1304
|
+
const { top, bottom } = regions.margins(page.section, page);
|
|
1305
|
+
const { margins } = this.sheetOfPage(p).settings;
|
|
1306
|
+
return {
|
|
1307
|
+
top: Math.abs(top - margins.top * PX_PER_MM) < 0.01 ? "" : `${top}px`,
|
|
1308
|
+
bottom: Math.abs(bottom - margins.bottom * PX_PER_MM) < 0.01 ? "" : `${bottom}px`,
|
|
1309
|
+
};
|
|
1310
|
+
});
|
|
1311
|
+
if (!mount)
|
|
1312
|
+
return;
|
|
1313
|
+
// Not shown anywhere any more (a variant turned on or off): the page's own story opens.
|
|
1314
|
+
if (home < 0)
|
|
1315
|
+
queueMicrotask(this.reopenStory);
|
|
1316
|
+
else if (home !== mount.page) {
|
|
1317
|
+
mount.page = home;
|
|
1318
|
+
mount.moved = true;
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
/** The page setup page `p` has, or will have once placed. */
|
|
1322
|
+
sheetOfPage(p) {
|
|
1323
|
+
return this.sheets[p] ?? this.sheets[this.sheets.length - 1] ?? this.lastSheet();
|
|
1324
|
+
}
|
|
1325
|
+
/**
|
|
1326
|
+
* After a story changed: its occurrences redrawn. False when a height changed, which takes a
|
|
1327
|
+
* new flow (`render`), or during a composition.
|
|
1328
|
+
*/
|
|
1329
|
+
redrawRegions() {
|
|
1330
|
+
const { regions } = this;
|
|
1331
|
+
if (!regions)
|
|
1332
|
+
return true;
|
|
1333
|
+
if (this.composing)
|
|
1334
|
+
return true;
|
|
1335
|
+
const context = wordContext(this.state);
|
|
1336
|
+
const spans = sectionSpans(this.state.doc, context.envelope.section);
|
|
1337
|
+
regions.setDocument(context.envelope, spans.map((span) => span.setup), context.resolver);
|
|
1338
|
+
this.notes?.setDocument(context.envelope, spans.map((span) => span.setup), context.resolver, this.noteReferences());
|
|
1339
|
+
if (this.regionsChanged(regions, spans)) {
|
|
1340
|
+
this.flowed = null;
|
|
1341
|
+
return false;
|
|
1342
|
+
}
|
|
1343
|
+
this.drawRegions();
|
|
1344
|
+
this.drawNotes();
|
|
1345
|
+
this.placeAgain?.();
|
|
1346
|
+
this.afterPlace();
|
|
1347
|
+
this.observer.takeRecords();
|
|
1348
|
+
return true;
|
|
1349
|
+
}
|
|
1350
|
+
/** The story being edited, moved with its region: the focus goes back to it. */
|
|
1351
|
+
afterPlace() {
|
|
1352
|
+
this.drawControls();
|
|
1353
|
+
this.drawMatches();
|
|
1354
|
+
if (!this.mount?.moved)
|
|
1355
|
+
return;
|
|
1356
|
+
this.mount.moved = false;
|
|
1357
|
+
this.mount.view.focus();
|
|
1358
|
+
}
|
|
1359
|
+
// ---------- stories on their pages (ADR-0007) ----------
|
|
1360
|
+
/** What the toolbar and code open stories through (`Editor.storyView`). */
|
|
1361
|
+
stories = ((view) => ({
|
|
1362
|
+
get open() {
|
|
1363
|
+
const { mount } = view;
|
|
1364
|
+
return mount
|
|
1365
|
+
? { id: mount.id, side: mount.side, variant: mount.variant, section: mount.section }
|
|
1366
|
+
: null;
|
|
1367
|
+
},
|
|
1368
|
+
edit: (side, page) => view.openStory(side, page),
|
|
1369
|
+
editNote: (id) => view.openNote(id),
|
|
1370
|
+
toReference: () => view.toReference(),
|
|
1371
|
+
close: () => view.closeStory(),
|
|
1372
|
+
get fieldLayout() {
|
|
1373
|
+
return view.fieldLayout();
|
|
1374
|
+
},
|
|
1375
|
+
updateFields: (options, editor) => view.updateFields(options, editor),
|
|
1376
|
+
showMatches: (found) => view.showMatches(found),
|
|
1377
|
+
reveal: (target, options) => view.reveal(target, options),
|
|
1378
|
+
goToPage: (page, options) => view.goToPage(page, options),
|
|
1379
|
+
focus: () => view.focus(),
|
|
1380
|
+
}))(this);
|
|
1381
|
+
/** Makes this view the one `Editor.storyView` speaks for, telling listeners if it was not. */
|
|
1382
|
+
claimStories() {
|
|
1383
|
+
if (!this.paged || this.editor.storyView === this.stories)
|
|
1384
|
+
return;
|
|
1385
|
+
const first = !this.editor.storyView;
|
|
1386
|
+
this.editor.storyView = this.stories;
|
|
1387
|
+
if (!first)
|
|
1388
|
+
this.editor.storyChanged();
|
|
1389
|
+
}
|
|
1390
|
+
// ---------- find, and going places (11.11) ----------
|
|
1391
|
+
/**
|
|
1392
|
+
* Marks what find found: on the pages, in the story open, and in the drawn copies of the others.
|
|
1393
|
+
* Nothing of it is in the document or the pages' DOM, and it does not print. The current match is
|
|
1394
|
+
* scrolled to when it is where the view can show it.
|
|
1395
|
+
*/
|
|
1396
|
+
showMatches(found) {
|
|
1397
|
+
this.showFound(found);
|
|
1398
|
+
const { current, scroll } = found ?? {};
|
|
1399
|
+
if (!current || !scroll)
|
|
1400
|
+
return;
|
|
1401
|
+
if (current.story === this.matchStory)
|
|
1402
|
+
this.scrollToRange(current.from, current.to);
|
|
1403
|
+
else if (current.story === this.mount?.id)
|
|
1404
|
+
this.mount?.view.scrollToRange(current.from, current.to);
|
|
1405
|
+
}
|
|
1406
|
+
/** The screen ranges of `from..to` in one paragraph: one per text node it covers. */
|
|
1407
|
+
rangesOf(from, to) {
|
|
1408
|
+
const { doc } = this.state;
|
|
1409
|
+
if (to > doc.content.size)
|
|
1410
|
+
return [];
|
|
1411
|
+
const $from = doc.resolve(from);
|
|
1412
|
+
if (!$from.parent.isTextblock)
|
|
1413
|
+
return [];
|
|
1414
|
+
const desc = this.blocks[this.blockFrom($from.before())];
|
|
1415
|
+
if (!desc)
|
|
1416
|
+
return [];
|
|
1417
|
+
const start = $from.start();
|
|
1418
|
+
const out = [];
|
|
1419
|
+
for (const leaf of desc.leaves) {
|
|
1420
|
+
const at = start + leaf.offset;
|
|
1421
|
+
const a = Math.max(from, at);
|
|
1422
|
+
const b = Math.min(to, at + leaf.size);
|
|
1423
|
+
if (a >= b || hiddenIn(leaf.dom, desc.dom))
|
|
1424
|
+
continue;
|
|
1425
|
+
const range = this.host.ownerDocument.createRange();
|
|
1426
|
+
if (leaf.dom.nodeType === Node.TEXT_NODE) {
|
|
1427
|
+
range.setStart(leaf.dom, a - at);
|
|
1428
|
+
range.setEnd(leaf.dom, b - at);
|
|
1429
|
+
}
|
|
1430
|
+
else
|
|
1431
|
+
range.selectNodeContents(leaf.dom);
|
|
1432
|
+
out.push(range);
|
|
1433
|
+
}
|
|
1434
|
+
return out;
|
|
1435
|
+
}
|
|
1436
|
+
/** The pages on the screen and a screen around it: only these carry marks. */
|
|
1437
|
+
pagesNearView() {
|
|
1438
|
+
const height = this.host.ownerDocument.defaultView?.innerHeight ?? 0;
|
|
1439
|
+
const near = new Set();
|
|
1440
|
+
this.pages.forEach((page, i) => {
|
|
1441
|
+
const { top, bottom } = page.getBoundingClientRect();
|
|
1442
|
+
if (bottom >= -height && top <= 2 * height)
|
|
1443
|
+
near.add(i);
|
|
1444
|
+
});
|
|
1445
|
+
return near;
|
|
1446
|
+
}
|
|
1447
|
+
drawMatches() {
|
|
1448
|
+
const { found, mount } = this;
|
|
1449
|
+
if (mount) {
|
|
1450
|
+
mount.view.matchStory = mount.id;
|
|
1451
|
+
mount.view.showFound(found);
|
|
1452
|
+
}
|
|
1453
|
+
if (!found) {
|
|
1454
|
+
this.matchMarks.set([], []);
|
|
1455
|
+
this.matchPages = "";
|
|
1456
|
+
return;
|
|
1457
|
+
}
|
|
1458
|
+
injectStyles(this.host);
|
|
1459
|
+
const { doc } = this.state;
|
|
1460
|
+
const near = this.pagesNearView();
|
|
1461
|
+
this.matchPages = [...near].join();
|
|
1462
|
+
// What the near pages hold, as positions: of the body's top-level nodes on them.
|
|
1463
|
+
let low = 0;
|
|
1464
|
+
let high = doc.content.size;
|
|
1465
|
+
if (this.paged) {
|
|
1466
|
+
low = high + 1;
|
|
1467
|
+
high = -1;
|
|
1468
|
+
doc.forEach((node, offset) => {
|
|
1469
|
+
const pages = this.itemPages.get(node);
|
|
1470
|
+
if (pages && !pages.some((page) => near.has(page)))
|
|
1471
|
+
return;
|
|
1472
|
+
low = Math.min(low, offset);
|
|
1473
|
+
high = Math.max(high, offset + node.nodeSize);
|
|
1474
|
+
});
|
|
1475
|
+
}
|
|
1476
|
+
// A tree's matches stand together, in order: those near are found by halving.
|
|
1477
|
+
const { matches } = found;
|
|
1478
|
+
let start = matches.findIndex((m) => m.story === this.matchStory);
|
|
1479
|
+
if (start < 0)
|
|
1480
|
+
start = matches.length;
|
|
1481
|
+
let end = start;
|
|
1482
|
+
while (end < matches.length && matches[end]?.story === this.matchStory)
|
|
1483
|
+
end++;
|
|
1484
|
+
let first = start;
|
|
1485
|
+
for (let last = end; first < last;) {
|
|
1486
|
+
const mid = (first + last) >> 1;
|
|
1487
|
+
if (matches[mid].to <= low)
|
|
1488
|
+
first = mid + 1;
|
|
1489
|
+
else
|
|
1490
|
+
last = mid;
|
|
1491
|
+
}
|
|
1492
|
+
const all = [];
|
|
1493
|
+
for (let i = first; i < end && matches[i].from < high; i++) {
|
|
1494
|
+
const { from, to } = matches[i];
|
|
1495
|
+
all.push(...this.rangesOf(from, to));
|
|
1496
|
+
}
|
|
1497
|
+
const others = new Set();
|
|
1498
|
+
for (const [i, { story }] of matches.entries())
|
|
1499
|
+
if ((i < start || i >= end) && story !== null && story !== mount?.id)
|
|
1500
|
+
others.add(story);
|
|
1501
|
+
const { current, query, matchCase, wholeWord } = found;
|
|
1502
|
+
const here = current?.story === this.matchStory ? current : null;
|
|
1503
|
+
if (others.size) {
|
|
1504
|
+
const options = { matchCase, wholeWord };
|
|
1505
|
+
const add = (root) => {
|
|
1506
|
+
if (root)
|
|
1507
|
+
all.push(...rangesInText(root, query, options));
|
|
1508
|
+
};
|
|
1509
|
+
for (const p of near) {
|
|
1510
|
+
const { header, footer } = this.pageRegions[p] ?? {};
|
|
1511
|
+
for (const region of [header, footer])
|
|
1512
|
+
if (region && others.has(region.id))
|
|
1513
|
+
add(region.element.querySelector(`:scope > .${REGION_CONTENT_CLASS}`));
|
|
1514
|
+
for (const note of this.pages[p]?.querySelectorAll(`[${NOTE_ATTR}]`) ?? [])
|
|
1515
|
+
if (others.has(note.getAttribute(NOTE_ATTR)))
|
|
1516
|
+
add(note);
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
this.matchMarks.set(all, here ? this.rangesOf(here.from, here.to) : []);
|
|
1520
|
+
}
|
|
1521
|
+
/** Scrolling brings other pages near: their marks are drawn, once per frame. */
|
|
1522
|
+
onMatchScroll = () => {
|
|
1523
|
+
const win = this.host.ownerDocument.defaultView;
|
|
1524
|
+
if (this.matchFrame || !win)
|
|
1525
|
+
return;
|
|
1526
|
+
this.matchFrame = win.requestAnimationFrame(() => {
|
|
1527
|
+
this.matchFrame = 0;
|
|
1528
|
+
if (this.found && [...this.pagesNearView()].join() !== this.matchPages)
|
|
1529
|
+
this.drawMatches();
|
|
1530
|
+
});
|
|
1531
|
+
};
|
|
1532
|
+
/** What find shows here, marked; a story's view is told by its page's view. */
|
|
1533
|
+
showFound(found) {
|
|
1534
|
+
const doc = this.host.ownerDocument;
|
|
1535
|
+
if (this.paged && !this.found !== !found) {
|
|
1536
|
+
if (found)
|
|
1537
|
+
doc.addEventListener("scroll", this.onMatchScroll, SCROLL_OPTIONS);
|
|
1538
|
+
else
|
|
1539
|
+
doc.removeEventListener("scroll", this.onMatchScroll, SCROLL_OPTIONS);
|
|
1540
|
+
}
|
|
1541
|
+
this.found = found;
|
|
1542
|
+
this.drawMatches();
|
|
1543
|
+
}
|
|
1544
|
+
scrollToRange(from, to) {
|
|
1545
|
+
let rect = this.rangesOf(from, to)[0]?.getClientRects?.()[0];
|
|
1546
|
+
if (!rect) {
|
|
1547
|
+
// A place, not a stretch of text: what holds it.
|
|
1548
|
+
const { node } = this.domFromPos(from);
|
|
1549
|
+
rect = (node instanceof Element ? node : node.parentElement)?.getBoundingClientRect();
|
|
1550
|
+
}
|
|
1551
|
+
// With room around it: a panel may float over the top of the window.
|
|
1552
|
+
if (rect)
|
|
1553
|
+
scrollRectIntoView(this.host, { top: rect.top - REVEAL_ABOVE, bottom: rect.bottom + 48 });
|
|
1554
|
+
}
|
|
1555
|
+
/** Opens story `id` where a page shows it, leaving the focus where it is. */
|
|
1556
|
+
openForReveal(id) {
|
|
1557
|
+
const kind = wordContext(this.state)?.stories.get(id)?.kind;
|
|
1558
|
+
if (kind === "footnote" || kind === "endnote")
|
|
1559
|
+
return this.openNote(id, false);
|
|
1560
|
+
if (kind !== "header" && kind !== "footer")
|
|
1561
|
+
return false;
|
|
1562
|
+
const page = this.pageRegions.findIndex((regions) => regions[kind]?.id === id);
|
|
1563
|
+
return page >= 0 && this.openStory(kind, page, false, false);
|
|
1564
|
+
}
|
|
1565
|
+
/**
|
|
1566
|
+
* Selects `target` and scrolls to it: in the body (leaving a story that is open), or in its story,
|
|
1567
|
+
* opened where it is. `focus`: the document takes the focus. False when no page shows it.
|
|
1568
|
+
*/
|
|
1569
|
+
reveal({ story, from, to }, { focus = false } = {}) {
|
|
1570
|
+
if (story === null) {
|
|
1571
|
+
this.closeStory(false);
|
|
1572
|
+
const { doc } = this.state;
|
|
1573
|
+
if (to > doc.content.size)
|
|
1574
|
+
return false;
|
|
1575
|
+
this.editor.dispatch(this.state.tr.setSelection(TextSelection.create(doc, from, to)));
|
|
1576
|
+
if (focus)
|
|
1577
|
+
this.focus();
|
|
1578
|
+
this.scrollToRange(from, to);
|
|
1579
|
+
return true;
|
|
1580
|
+
}
|
|
1581
|
+
if (this.mount?.id !== story && !this.openForReveal(story))
|
|
1582
|
+
return false;
|
|
1583
|
+
const { mount } = this;
|
|
1584
|
+
if (!mount)
|
|
1585
|
+
return false;
|
|
1586
|
+
mount.view.editor.select(from, to);
|
|
1587
|
+
if (focus)
|
|
1588
|
+
mount.view.focus();
|
|
1589
|
+
mount.view.scrollToRange(from, to);
|
|
1590
|
+
return true;
|
|
1591
|
+
}
|
|
1592
|
+
/** The caret to the start of page `page` (1-based) and the page into view. */
|
|
1593
|
+
goToPage(page, { focus = false } = {}) {
|
|
1594
|
+
const element = this.pages[page - 1];
|
|
1595
|
+
if (!element)
|
|
1596
|
+
return false;
|
|
1597
|
+
this.closeStory(false);
|
|
1598
|
+
search: for (const [i, desc] of this.blocks.entries()) {
|
|
1599
|
+
for (const [f, fragment] of desc.fragments.entries()) {
|
|
1600
|
+
if (fragment.closest(".foli-page") !== element)
|
|
1601
|
+
continue;
|
|
1602
|
+
const start = this.starts[i] + 1 + (f ? desc.splits[f - 1] : 0);
|
|
1603
|
+
const { doc } = this.state;
|
|
1604
|
+
this.editor.dispatch(this.state.tr.setSelection(Selection.near(doc.resolve(Math.min(start, doc.content.size)))));
|
|
1605
|
+
break search;
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
if (focus)
|
|
1609
|
+
this.focus();
|
|
1610
|
+
scrollRectIntoView(this.host, element.getBoundingClientRect());
|
|
1611
|
+
return true;
|
|
1612
|
+
}
|
|
1613
|
+
/** Word's keys for finding and going places, when the app shows something for them. */
|
|
1614
|
+
navigationKey(name) {
|
|
1615
|
+
const request = Object.hasOwn(NAVIGATION_KEYS, name) ? NAVIGATION_KEYS[name] : undefined;
|
|
1616
|
+
return !!request && this.editor.navigation?.(request) === true;
|
|
1617
|
+
}
|
|
1618
|
+
// ---------- content controls ----------
|
|
1619
|
+
/** The frames of content controls, drawn again (a render takes them off the pages). */
|
|
1620
|
+
drawControls() {
|
|
1621
|
+
if (!this.composing)
|
|
1622
|
+
this.controlChrome.draw(!this.mount);
|
|
1623
|
+
}
|
|
1624
|
+
/** Where a control's content is on the screen: its lines, or its blocks by page. */
|
|
1625
|
+
controlRects(range) {
|
|
1626
|
+
if (range.level !== "inline") {
|
|
1627
|
+
const byPage = new Map();
|
|
1628
|
+
for (let i = this.blockFrom(range.from); i < this.blocks.length; i++) {
|
|
1629
|
+
if (this.starts[i] >= range.to)
|
|
1630
|
+
break;
|
|
1631
|
+
for (const fragment of this.blocks[i].fragments) {
|
|
1632
|
+
if (!this.host.contains(fragment))
|
|
1633
|
+
continue;
|
|
1634
|
+
const rect = fragment.getBoundingClientRect();
|
|
1635
|
+
const page = fragment.closest(".foli-page,.foli-story-flow");
|
|
1636
|
+
const known = byPage.get(page);
|
|
1637
|
+
const left = Math.min(known?.left ?? rect.left, rect.left);
|
|
1638
|
+
const top = Math.min(known?.top ?? rect.top, rect.top);
|
|
1639
|
+
byPage.set(page, boxOf(left, top, Math.max(known?.right ?? rect.right, rect.right) - left, Math.max(known?.bottom ?? rect.bottom, rect.bottom) - top));
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
return [...byPage.values()];
|
|
1643
|
+
}
|
|
1644
|
+
return this.controlContentRects(range);
|
|
1645
|
+
}
|
|
1646
|
+
/** Where what a control's content shows is on the screen. */
|
|
1647
|
+
controlContentRects(range) {
|
|
1648
|
+
const dom = this.host.ownerDocument.createRange();
|
|
1649
|
+
const start = this.domFromPos(range.contentFrom);
|
|
1650
|
+
const end = this.domFromPos(range.contentTo);
|
|
1651
|
+
dom.setStart(start.node, start.offset);
|
|
1652
|
+
dom.setEnd(end.node, end.offset);
|
|
1653
|
+
const rects = [...(dom.getClientRects?.() ?? [])].filter((rect) => rect.width > 0);
|
|
1654
|
+
if (rects.length)
|
|
1655
|
+
return rects;
|
|
1656
|
+
// Nothing in it: a thin box where it is.
|
|
1657
|
+
const element = start.node.nodeType === Node.ELEMENT_NODE
|
|
1658
|
+
? start.node
|
|
1659
|
+
: start.node.parentElement;
|
|
1660
|
+
const rect = dom.getBoundingClientRect?.();
|
|
1661
|
+
const box = rect?.height ? rect : element?.getBoundingClientRect();
|
|
1662
|
+
return box ? [boxOf(box.left, box.top, 4, box.height)] : [];
|
|
1663
|
+
}
|
|
1664
|
+
/**
|
|
1665
|
+
* The keys of content controls: Alt+Down opens a list or date picker, and Left and Right stop on
|
|
1666
|
+
* each side of a control's edge, as Word's caret does (browsers cannot see the difference).
|
|
1667
|
+
*/
|
|
1668
|
+
controlKey(name) {
|
|
1669
|
+
if (name === "Alt-ArrowDown")
|
|
1670
|
+
return this.controlChrome.open();
|
|
1671
|
+
if (name !== "ArrowLeft" && name !== "ArrowRight")
|
|
1672
|
+
return false;
|
|
1673
|
+
const { selection, doc } = this.state;
|
|
1674
|
+
if (!(selection instanceof TextSelection) || !wordContext(this.state)?.envelope.controls)
|
|
1675
|
+
return false;
|
|
1676
|
+
const forward = name === "ArrowRight";
|
|
1677
|
+
const range = controlAt(doc, selection.from);
|
|
1678
|
+
if (!selection.empty) {
|
|
1679
|
+
// A placeholder is one thing: the arrows go past it.
|
|
1680
|
+
if (range?.level !== "inline" ||
|
|
1681
|
+
!controlProperties(this.state, range.id)?.placeholder?.showing ||
|
|
1682
|
+
selection.from !== range.contentFrom ||
|
|
1683
|
+
selection.to !== range.contentTo)
|
|
1684
|
+
return false;
|
|
1685
|
+
this.dispatch(this.state.tr.setSelection(TextSelection.create(doc, forward ? range.to : range.from)));
|
|
1686
|
+
return true;
|
|
1687
|
+
}
|
|
1688
|
+
const { $from } = selection;
|
|
1689
|
+
const next = forward ? $from.nodeAfter : $from.nodeBefore;
|
|
1690
|
+
if (next?.type.name !== "controlStart" && next?.type.name !== "controlEnd")
|
|
1691
|
+
return false;
|
|
1692
|
+
this.dispatch(this.state.tr.setSelection(TextSelection.create(doc, $from.pos + (forward ? 1 : -1))));
|
|
1693
|
+
return true;
|
|
1694
|
+
}
|
|
1695
|
+
// ---------- fields ----------
|
|
1696
|
+
/** The pages as last flowed, as fields need them; null without pages. */
|
|
1697
|
+
fieldLayout() {
|
|
1698
|
+
if (!this.regions || !this.facts.length)
|
|
1699
|
+
return null;
|
|
1700
|
+
return {
|
|
1701
|
+
pages: this.facts.length,
|
|
1702
|
+
pageAt: (pos) => {
|
|
1703
|
+
const { node } = this.domFromPos(Math.min(pos, this.state.doc.content.size));
|
|
1704
|
+
const element = node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
|
|
1705
|
+
const page = this.facts[this.pages.indexOf(element?.closest(".foli-page"))];
|
|
1706
|
+
if (!page)
|
|
1707
|
+
return null;
|
|
1708
|
+
const format = this.sections()[page.section]?.pageNumbers?.format;
|
|
1709
|
+
return { number: page.number, sectionPages: page.sectionPages, format };
|
|
1710
|
+
},
|
|
1711
|
+
};
|
|
1712
|
+
}
|
|
1713
|
+
/**
|
|
1714
|
+
* Updates fields from the pages shown: all of them, or those `options.only` says, of the body or
|
|
1715
|
+
* of `editor` (a story's). What that does to the pages is taken in by a second pass over what
|
|
1716
|
+
* depends on them, in the same undo step.
|
|
1717
|
+
*/
|
|
1718
|
+
updateFields(options = {}, editor = this.editor) {
|
|
1719
|
+
// A story's view has no pages: the view of the body has.
|
|
1720
|
+
const main = this.regions ? null : this.editor.storyView;
|
|
1721
|
+
if (main)
|
|
1722
|
+
return main.updateFields(options, editor);
|
|
1723
|
+
if (this.composing)
|
|
1724
|
+
this.onCompositionEnd();
|
|
1725
|
+
const report = updateFields(editor, { ...options, layout: this.fieldLayout() });
|
|
1726
|
+
const { only } = options;
|
|
1727
|
+
const range = !!only && typeof only !== "function";
|
|
1728
|
+
if (!report.updated || (range && !report.rebuilt))
|
|
1729
|
+
return report;
|
|
1730
|
+
const follows = (field) => range
|
|
1731
|
+
? field.parent?.name === "TOC"
|
|
1732
|
+
: PAGE_DEPENDENT.has(field.name) && (typeof only !== "function" || only(field));
|
|
1733
|
+
const again = updateFields(editor, {
|
|
1734
|
+
...options,
|
|
1735
|
+
layout: this.fieldLayout(),
|
|
1736
|
+
only: follows,
|
|
1737
|
+
continues: true,
|
|
1738
|
+
});
|
|
1739
|
+
return { ...report, updated: report.updated + again.updated };
|
|
1740
|
+
}
|
|
1741
|
+
/**
|
|
1742
|
+
* Prints what the document is now: fields updated (one undo step), then the browser's dialog.
|
|
1743
|
+
*/
|
|
1744
|
+
print() {
|
|
1745
|
+
try {
|
|
1746
|
+
this.updateFields();
|
|
1747
|
+
}
|
|
1748
|
+
finally {
|
|
1749
|
+
this.host.ownerDocument.defaultView?.print();
|
|
1750
|
+
}
|
|
1751
|
+
}
|
|
1752
|
+
/**
|
|
1753
|
+
* Left and Right next to a field's markers: the browser's move starts where something can be
|
|
1754
|
+
* seen, or the markers (empty elements) cost WebKit a key press each. With codes shown the
|
|
1755
|
+
* view makes the move itself, over the whole field; whether it did.
|
|
1756
|
+
*/
|
|
1757
|
+
skipFieldParts(event) {
|
|
1758
|
+
const forward = event.key === "ArrowRight";
|
|
1759
|
+
if ((!forward && event.key !== "ArrowLeft") || event.ctrlKey || event.altKey || event.metaKey)
|
|
1760
|
+
return false;
|
|
1761
|
+
const { selection, doc } = this.state;
|
|
1762
|
+
// Shift extends by itself: an extended DOM selection would lose its anchor to the same spot.
|
|
1763
|
+
if (!(selection instanceof TextSelection) || event.shiftKey || !selection.empty)
|
|
1764
|
+
return false;
|
|
1765
|
+
const stop = caretStop(this.state, selection.head, forward);
|
|
1766
|
+
const sel = this.host.ownerDocument.getSelection();
|
|
1767
|
+
if (stop === selection.head || !sel)
|
|
1768
|
+
return false;
|
|
1769
|
+
if (this.fieldCodes) {
|
|
1770
|
+
this.dispatch(this.state.tr.setSelection(TextSelection.create(doc, stop)));
|
|
1771
|
+
return true;
|
|
1772
|
+
}
|
|
1773
|
+
const { node, offset } = this.domFromPos(stop);
|
|
1774
|
+
sel.collapse(node, offset);
|
|
1775
|
+
return false;
|
|
1776
|
+
}
|
|
1777
|
+
/** F9 and Alt+F9, as in Word. */
|
|
1778
|
+
fieldKey(name) {
|
|
1779
|
+
if (name === "Alt-F9") {
|
|
1780
|
+
this.run(toggleFieldCodes);
|
|
1781
|
+
return true;
|
|
1782
|
+
}
|
|
1783
|
+
if (name !== "F9")
|
|
1784
|
+
return false;
|
|
1785
|
+
if (!this.editable)
|
|
1786
|
+
return true;
|
|
1787
|
+
const { selection } = this.state;
|
|
1788
|
+
this.updateFields(selection instanceof AllSelection ? {} : { only: { from: selection.from, to: selection.to } });
|
|
1789
|
+
return true;
|
|
1790
|
+
}
|
|
1791
|
+
/** Shades what shows the fields the selection is in (not printed): the outermost of each. */
|
|
1792
|
+
showSelectedFields() {
|
|
1793
|
+
const { selection, doc } = this.state;
|
|
1794
|
+
const wanted = new Set();
|
|
1795
|
+
const fields = selection instanceof TextSelection ? fieldsIn(doc) : [];
|
|
1796
|
+
const touched = (field) => selection.empty
|
|
1797
|
+
? field.from < selection.from && selection.from < field.to
|
|
1798
|
+
: field.from < selection.to && field.to > selection.from;
|
|
1799
|
+
for (const field of fields) {
|
|
1800
|
+
if (!touched(field) || (field.parent && touched(field.parent)))
|
|
1801
|
+
continue;
|
|
1802
|
+
const [from, to] = this.fieldCodes
|
|
1803
|
+
? [field.from, field.codeEnd + 1]
|
|
1804
|
+
: [field.resultFrom, field.resultTo];
|
|
1805
|
+
const first = Math.max(this.blockFrom(from) - 1, 0);
|
|
1806
|
+
for (let i = first; i < this.blocks.length; i++) {
|
|
1807
|
+
const start = this.starts[i] + 1;
|
|
1808
|
+
if (start > to)
|
|
1809
|
+
break;
|
|
1810
|
+
const desc = this.blocks[i];
|
|
1811
|
+
for (const leaf of desc.leaves) {
|
|
1812
|
+
const at = start + leaf.offset;
|
|
1813
|
+
if (at + leaf.size <= from || at >= to || !leaf.size)
|
|
1814
|
+
continue;
|
|
1815
|
+
if (hiddenIn(leaf.dom, desc.dom))
|
|
1816
|
+
continue;
|
|
1817
|
+
const element = leaf.dom.nodeType === Node.TEXT_NODE ? leaf.dom.parentElement : leaf.dom;
|
|
1818
|
+
if (element instanceof Element && element.textContent)
|
|
1819
|
+
wanted.add(element);
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
for (const element of this.shadedFields)
|
|
1824
|
+
if (!wanted.has(element))
|
|
1825
|
+
element.removeAttribute(FIELD_SELECTED_ATTR);
|
|
1826
|
+
if (wanted.size)
|
|
1827
|
+
injectStyles(this.host);
|
|
1828
|
+
for (const element of wanted)
|
|
1829
|
+
element.setAttribute(FIELD_SELECTED_ATTR, "");
|
|
1830
|
+
this.shadedFields = [...wanted];
|
|
1831
|
+
}
|
|
1832
|
+
/** The sections of the document, in order. */
|
|
1833
|
+
sections() {
|
|
1834
|
+
const { envelope } = wordContext(this.state);
|
|
1835
|
+
return sectionSpans(this.state.doc, envelope.section).map((span) => span.setup);
|
|
1836
|
+
}
|
|
1837
|
+
/** The page the selection's head is on. */
|
|
1838
|
+
selectionPage() {
|
|
1839
|
+
const { node } = this.domFromPos(this.state.selection.head);
|
|
1840
|
+
const element = node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement;
|
|
1841
|
+
return Math.max(this.pages.indexOf(element?.closest(".foli-page")), 0);
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1844
|
+
* Opens the header or footer of page `at` (by default the selection's) for editing where it is;
|
|
1845
|
+
* made first when the page has none. Whether it is open now.
|
|
1846
|
+
*/
|
|
1847
|
+
openStory(side, at = this.selectionPage(), create = true, focus = true) {
|
|
1848
|
+
if (!this.regions || !this.editable || this.composing)
|
|
1849
|
+
return false;
|
|
1850
|
+
// Word's blank page has none: the next page's.
|
|
1851
|
+
let p = at;
|
|
1852
|
+
while (this.facts[p] && !this.facts[p]?.sectionPages)
|
|
1853
|
+
p++;
|
|
1854
|
+
const page = this.facts[p];
|
|
1855
|
+
if (!page)
|
|
1856
|
+
return false;
|
|
1857
|
+
const context = wordContext(this.state);
|
|
1858
|
+
const setup = this.sections()[page.section];
|
|
1859
|
+
const even = context.envelope.settings?.evenAndOddHeaders === true;
|
|
1860
|
+
const variant = pageVariant(setup, page, even);
|
|
1861
|
+
const before = storyOfSection(this.sections(), page.section, side, variant);
|
|
1862
|
+
if (this.mount?.id === before?.id && this.mount?.side === side && this.mount?.page === p) {
|
|
1863
|
+
if (focus)
|
|
1864
|
+
this.mount?.view.focus();
|
|
1865
|
+
return true;
|
|
1866
|
+
}
|
|
1867
|
+
this.closeStory(false);
|
|
1868
|
+
if (!before && (!create || !this.editor.run(ensureStory(side, variant, page.section))))
|
|
1869
|
+
return false;
|
|
1870
|
+
const found = storyOfSection(this.sections(), page.section, side, variant);
|
|
1871
|
+
const editor = found && this.editor.story(found.id);
|
|
1872
|
+
const region = this.pageRegions[p]?.[side];
|
|
1873
|
+
if (!found || !editor || !region)
|
|
1874
|
+
return false;
|
|
1875
|
+
this.mountStory(editor, { id: found.id, side, variant, section: page.section, page: p, region }, side === "header" ? "Header" : "Footer", undefined, focus);
|
|
1876
|
+
return true;
|
|
1877
|
+
}
|
|
1878
|
+
/**
|
|
1879
|
+
* Opens note `id`, or the one whose reference is at the selection, in place: shown whole at its
|
|
1880
|
+
* reference's page. Whether it is open now.
|
|
1881
|
+
*/
|
|
1882
|
+
openNote(id, focus = true) {
|
|
1883
|
+
const { notes } = this;
|
|
1884
|
+
if (!notes || !this.editable || this.composing)
|
|
1885
|
+
return false;
|
|
1886
|
+
const story = id ?? noteAtSelection(this.state)?.story;
|
|
1887
|
+
if (!story)
|
|
1888
|
+
return false;
|
|
1889
|
+
if (this.mount?.id === story) {
|
|
1890
|
+
if (focus)
|
|
1891
|
+
this.mount.view.focus();
|
|
1892
|
+
return true;
|
|
1893
|
+
}
|
|
1894
|
+
this.closeStory(false);
|
|
1895
|
+
const editor = this.editor.story(story);
|
|
1896
|
+
if (!editor)
|
|
1897
|
+
return false;
|
|
1898
|
+
notes.open = story;
|
|
1899
|
+
this.flowed = null;
|
|
1900
|
+
this.render();
|
|
1901
|
+
const element = this.host.querySelector(`[${NOTE_ATTR}="${CSS.escape(story)}"]`);
|
|
1902
|
+
const page = this.notePages.get(story);
|
|
1903
|
+
if (!element || page === undefined) {
|
|
1904
|
+
notes.open = null;
|
|
1905
|
+
this.flowed = null;
|
|
1906
|
+
this.render();
|
|
1907
|
+
return false;
|
|
1908
|
+
}
|
|
1909
|
+
const region = { element, drawing: {}, section: 0, id: story };
|
|
1910
|
+
const texts = () => noteTexts(noteNumbers(this.state.doc, wordContext(this.state).envelope));
|
|
1911
|
+
this.mountStory(editor, { id: story, side: "note", variant: "default", section: 0, page, region }, "Note", () => texts().get(story) ?? "", focus);
|
|
1912
|
+
editor.select(Selection.atEnd(editor.state.doc).from);
|
|
1913
|
+
return true;
|
|
1914
|
+
}
|
|
1915
|
+
/** From the note being edited back to its reference, the caret after it. */
|
|
1916
|
+
toReference() {
|
|
1917
|
+
const { mount } = this;
|
|
1918
|
+
if (mount?.side !== "note")
|
|
1919
|
+
return false;
|
|
1920
|
+
const reference = noteNumbers(this.state.doc, wordContext(this.state).envelope).find((n) => n.story === mount.id);
|
|
1921
|
+
this.closeStory(false);
|
|
1922
|
+
if (reference)
|
|
1923
|
+
this.dispatch(this.state.tr.setSelection(TextSelection.create(this.state.doc, reference.pos + 1)));
|
|
1924
|
+
this.focus();
|
|
1925
|
+
return true;
|
|
1926
|
+
}
|
|
1927
|
+
/** Puts `editor`'s story on its page, in `place`, the body read-only and dimmed around it. */
|
|
1928
|
+
mountStory(editor, place, label, noteMark, focus = true) {
|
|
1929
|
+
const { region } = place;
|
|
1930
|
+
injectStyles(this.host);
|
|
1931
|
+
this.observer.disconnect();
|
|
1932
|
+
this.host.setAttribute("contenteditable", "false");
|
|
1933
|
+
this.host.setAttribute("aria-readonly", "true");
|
|
1934
|
+
this.host.classList.add("foli-in-story");
|
|
1935
|
+
const doc = this.host.ownerDocument;
|
|
1936
|
+
const wrapper = doc.createElement("div");
|
|
1937
|
+
wrapper.className = "foli-story-mount";
|
|
1938
|
+
wrapper.setAttribute("contenteditable", "false");
|
|
1939
|
+
wrapper.style.userSelect = "text";
|
|
1940
|
+
wrapper.style.setProperty("-webkit-user-select", "text");
|
|
1941
|
+
const inner = doc.createElement("div");
|
|
1942
|
+
inner.setAttribute("aria-label", label);
|
|
1943
|
+
wrapper.append(inner);
|
|
1944
|
+
const shown = region.element.querySelector(`:scope > .${REGION_CONTENT_CLASS}`);
|
|
1945
|
+
if (shown) {
|
|
1946
|
+
shown.style.display = "none";
|
|
1947
|
+
wrapper.style.paddingBottom = shown.style.paddingBottom;
|
|
1948
|
+
}
|
|
1949
|
+
region.element.append(wrapper);
|
|
1950
|
+
const view = new EditorView(inner, editor, {
|
|
1951
|
+
paged: false,
|
|
1952
|
+
upload: this.upload ?? null,
|
|
1953
|
+
resource: this.resource ?? null,
|
|
1954
|
+
...(noteMark ? { noteMark } : {}),
|
|
1955
|
+
});
|
|
1956
|
+
wrapper.addEventListener("keydown", (event) => {
|
|
1957
|
+
if (event.isComposing || view.composing)
|
|
1958
|
+
return;
|
|
1959
|
+
if (isNoteKey(event) && this.toReference()) {
|
|
1960
|
+
// Not on to the body, which would open the note again.
|
|
1961
|
+
event.preventDefault();
|
|
1962
|
+
event.stopPropagation();
|
|
1963
|
+
return;
|
|
1964
|
+
}
|
|
1965
|
+
if (event.key !== "Escape")
|
|
1966
|
+
return;
|
|
1967
|
+
event.preventDefault();
|
|
1968
|
+
this.closeStory(true);
|
|
1969
|
+
});
|
|
1970
|
+
this.mount = { ...place, wrapper, view, moved: false };
|
|
1971
|
+
// With several views of the editor, the one in use answers for its story.
|
|
1972
|
+
wrapper.addEventListener("focusin", () => this.claimStories());
|
|
1973
|
+
this.claimStories();
|
|
1974
|
+
if (focus)
|
|
1975
|
+
view.focus();
|
|
1976
|
+
this.drawMatches();
|
|
1977
|
+
this.editor.storyChanged();
|
|
1978
|
+
}
|
|
1979
|
+
/** Finishes a running composition, its text going into the model. */
|
|
1980
|
+
endComposition() {
|
|
1981
|
+
if (this.composing)
|
|
1982
|
+
this.onCompositionEnd();
|
|
1983
|
+
}
|
|
1984
|
+
/** Leaves the story being edited; `focus`: the body takes the focus, with its selection back. */
|
|
1985
|
+
closeStory(focus = true) {
|
|
1986
|
+
const { mount } = this;
|
|
1987
|
+
if (!mount)
|
|
1988
|
+
return;
|
|
1989
|
+
mount.view.endComposition();
|
|
1990
|
+
this.mount = null;
|
|
1991
|
+
mount.view.destroy();
|
|
1992
|
+
mount.wrapper.remove();
|
|
1993
|
+
const shown = mount.region.element.querySelector(`:scope > .${REGION_CONTENT_CLASS}`);
|
|
1994
|
+
if (shown)
|
|
1995
|
+
shown.style.display = "";
|
|
1996
|
+
if (mount.side === "note" && this.notes) {
|
|
1997
|
+
this.notes.open = null;
|
|
1998
|
+
this.flowed = null;
|
|
1999
|
+
if (!this.destroyed)
|
|
2000
|
+
this.render();
|
|
2001
|
+
}
|
|
2002
|
+
this.host.classList.remove("foli-in-story");
|
|
2003
|
+
this.host.setAttribute("contenteditable", String(this.editable));
|
|
2004
|
+
if (this.editable)
|
|
2005
|
+
this.host.removeAttribute("aria-readonly");
|
|
2006
|
+
this.observer.takeRecords();
|
|
2007
|
+
this.observer.observe(this.host, { childList: true, characterData: true, subtree: true });
|
|
2008
|
+
if (focus && !this.destroyed)
|
|
2009
|
+
this.focus();
|
|
2010
|
+
if (!this.destroyed)
|
|
2011
|
+
this.drawMatches();
|
|
2012
|
+
this.editor.storyChanged();
|
|
2013
|
+
}
|
|
2014
|
+
/** The page's own story, after what was open is shown nowhere any more. */
|
|
2015
|
+
reopenStory = () => {
|
|
2016
|
+
const { mount } = this;
|
|
2017
|
+
if (!mount || mount.side === "note" || this.facts.length === 0)
|
|
2018
|
+
return;
|
|
2019
|
+
const { side, page, id } = mount;
|
|
2020
|
+
// A story that went (undone) is not made again.
|
|
2021
|
+
const create = !!wordContext(this.state)?.stories.has(id);
|
|
2022
|
+
this.closeStory(false);
|
|
2023
|
+
if (!this.openStory(side, Math.min(page, this.facts.length - 1), create))
|
|
2024
|
+
this.focus();
|
|
2025
|
+
};
|
|
2026
|
+
/** The margin of a page a mouse event is in, if it is on the page itself. */
|
|
2027
|
+
marginAt(event) {
|
|
2028
|
+
const element = event.target;
|
|
2029
|
+
const page = this.pages.indexOf(element);
|
|
2030
|
+
if (page < 0 || !this.regions)
|
|
2031
|
+
return null;
|
|
2032
|
+
const box = element.getBoundingClientRect();
|
|
2033
|
+
const scale = this.scale();
|
|
2034
|
+
const style = getComputedStyle(element);
|
|
2035
|
+
const y = (event.clientY - box.top) / scale;
|
|
2036
|
+
if (y < Number.parseFloat(style.paddingTop))
|
|
2037
|
+
return { side: "header", page };
|
|
2038
|
+
return y > box.height / scale - Number.parseFloat(style.paddingBottom)
|
|
2039
|
+
? { side: "footer", page }
|
|
2040
|
+
: null;
|
|
2041
|
+
}
|
|
2042
|
+
/**
|
|
2043
|
+
* A press outside the story being edited leaves it. The second press of a double click in a
|
|
2044
|
+
* page's margin opens its header or footer, the body's selection kept: on the press, since the
|
|
2045
|
+
* first click may have scrolled the page (Firefox) and the double click would land elsewhere.
|
|
2046
|
+
*/
|
|
2047
|
+
onPress = (event) => {
|
|
2048
|
+
this.closeStory(false);
|
|
2049
|
+
// A note on the page, or the number of its reference: the second press opens the note.
|
|
2050
|
+
const target = event.target instanceof Element ? event.target : null;
|
|
2051
|
+
const onNote = target?.closest(`[${NOTE_ATTR}]`);
|
|
2052
|
+
const number = target?.closest(`[${NOTE_REF_ATTR}]`);
|
|
2053
|
+
const story = onNote?.getAttribute(NOTE_ATTR) ?? number?.getAttribute(NOTE_REF_ATTR);
|
|
2054
|
+
if (story && event.button === 0 && !event.shiftKey) {
|
|
2055
|
+
const opens = event.detail === 2 && this.editable && isNoteStory(this.state, story);
|
|
2056
|
+
if (onNote || opens)
|
|
2057
|
+
event.preventDefault();
|
|
2058
|
+
if (opens) {
|
|
2059
|
+
if (this.openNote(story) && onNote)
|
|
2060
|
+
this.mount?.view.placeCaret(event.clientX, event.clientY);
|
|
2061
|
+
return;
|
|
2062
|
+
}
|
|
2063
|
+
if (onNote)
|
|
2064
|
+
return;
|
|
2065
|
+
}
|
|
2066
|
+
const margin = event.button === 0 && !event.shiftKey ? this.marginAt(event) : null;
|
|
2067
|
+
if (margin && event.detail > 1) {
|
|
2068
|
+
event.preventDefault();
|
|
2069
|
+
if (event.detail > 2 || !this.editable)
|
|
2070
|
+
return;
|
|
2071
|
+
const selection = this.pressSelection;
|
|
2072
|
+
this.pressSelection = null;
|
|
2073
|
+
if (selection && !selection.eq(this.state.selection))
|
|
2074
|
+
this.editor.dispatch(this.state.tr.setSelection(selection));
|
|
2075
|
+
if (this.openStory(margin.side, margin.page))
|
|
2076
|
+
this.mount?.view.placeCaret(event.clientX, event.clientY);
|
|
2077
|
+
return;
|
|
2078
|
+
}
|
|
2079
|
+
// What the body shows now is what leaving the story comes back to (`selectionchange` comes
|
|
2080
|
+
// late, and the browser may select something on the way to the second press).
|
|
2081
|
+
if (margin && this.hasFocus())
|
|
2082
|
+
this.syncSelection();
|
|
2083
|
+
this.pressSelection = margin ? this.state.selection : null;
|
|
2084
|
+
// On a header or footer a press is not for the body: it places no caret.
|
|
2085
|
+
if (margin && this.pageRegions[margin.page]?.[margin.side]) {
|
|
2086
|
+
event.preventDefault();
|
|
2087
|
+
if (!this.hasFocus()) {
|
|
2088
|
+
this.pointerFocus = true;
|
|
2089
|
+
this.host.focus({ preventScroll: true });
|
|
2090
|
+
this.writeSelection(true);
|
|
2091
|
+
}
|
|
2092
|
+
return;
|
|
2093
|
+
}
|
|
2094
|
+
this.onMouseDown(event);
|
|
2095
|
+
};
|
|
2096
|
+
/** Puts the caret at a point of the window, if it is on this view's content. */
|
|
2097
|
+
placeCaret(x, y) {
|
|
2098
|
+
const pos = this.posAtPoint(x, y);
|
|
2099
|
+
if (pos !== null)
|
|
2100
|
+
this.editor.dispatch(this.state.tr.setSelection(TextSelection.near(this.state.doc.resolve(pos))));
|
|
2101
|
+
}
|
|
2102
|
+
/** The model position at a point of the window, or null. */
|
|
2103
|
+
posAtPoint(x, y) {
|
|
2104
|
+
const doc = this.host.ownerDocument;
|
|
2105
|
+
const point = doc.caretPositionFromPoint?.(x, y);
|
|
2106
|
+
const range = point ? null : doc.caretRangeFromPoint?.(x, y);
|
|
2107
|
+
const node = point?.offsetNode ?? range?.startContainer;
|
|
2108
|
+
const offset = point?.offset ?? range?.startOffset ?? 0;
|
|
2109
|
+
return node && this.host.contains(node) ? this.posFromDOM(node, offset) : null;
|
|
781
2110
|
}
|
|
782
2111
|
/**
|
|
783
2112
|
* A floating picture reaches into the blocks after its paragraph (their lines go around it):
|
|
@@ -829,20 +2158,30 @@ export class EditorView {
|
|
|
829
2158
|
};
|
|
830
2159
|
/**
|
|
831
2160
|
* Puts each fragment of each block into its page (pages are non-decreasing in document
|
|
832
|
-
* order)
|
|
833
|
-
*
|
|
834
|
-
* or a page (stray DOM) is removed.
|
|
2161
|
+
* order) and, on a page with several sections or columns, into its band's column, creating
|
|
2162
|
+
* and removing pages as needed, and gives each page the page setup of its first section.
|
|
2163
|
+
* Anything else in the host or a page (stray DOM) is removed.
|
|
835
2164
|
*/
|
|
836
|
-
place(elements, pages, layout) {
|
|
2165
|
+
place(elements, pages, slots, layout) {
|
|
837
2166
|
const { host } = this;
|
|
838
|
-
const items = elements.flatMap((list, i) => list.map((dom, f) => ({
|
|
839
|
-
|
|
2167
|
+
const items = elements.flatMap((list, i) => list.map((dom, f) => ({
|
|
2168
|
+
dom,
|
|
2169
|
+
page: pages[i]?.[f] ?? 0,
|
|
2170
|
+
slot: slots[i]?.[f],
|
|
2171
|
+
section: layout[i].section,
|
|
2172
|
+
})));
|
|
2173
|
+
// A note can reach pages no text is on.
|
|
2174
|
+
const count = Math.max((items[items.length - 1]?.page ?? 0) + 1, this.pageNotes.length);
|
|
840
2175
|
const sheets = [];
|
|
841
2176
|
pages.forEach((list, i) => {
|
|
842
2177
|
for (const p of list)
|
|
843
2178
|
sheets[p] ??= layout[i].sheet;
|
|
844
2179
|
});
|
|
845
|
-
// A page no block
|
|
2180
|
+
// A page no block is on has the setup of the page after it (Word's blank page before an
|
|
2181
|
+
// even or odd page section); the last, one with no blocks yet, of the page before it.
|
|
2182
|
+
for (let p = count - 1; p >= 0; p--)
|
|
2183
|
+
if (sheets[p + 1])
|
|
2184
|
+
sheets[p] ??= sheets[p + 1];
|
|
846
2185
|
for (let p = 0; p < count; p++)
|
|
847
2186
|
sheets[p] ??=
|
|
848
2187
|
sheets[p - 1] ?? layout[0]?.sheet ?? this.lastSheet();
|
|
@@ -852,11 +2191,19 @@ export class EditorView {
|
|
|
852
2191
|
this.sheets.forEach((sheet, p) => {
|
|
853
2192
|
const page = this.pages[p];
|
|
854
2193
|
const key = `${sheet.index} ${sheet.css}`;
|
|
855
|
-
if (pageSheets.get(page)
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
2194
|
+
if (pageSheets.get(page) !== key) {
|
|
2195
|
+
pageSheets.set(page, key);
|
|
2196
|
+
pagePads.delete(page);
|
|
2197
|
+
page.style.cssText = sheet.css;
|
|
2198
|
+
page.setAttribute(SHEET_ATTR, String(sheet.index));
|
|
2199
|
+
}
|
|
2200
|
+
const { top = "", bottom = "" } = this.pageMargins[p] ?? {};
|
|
2201
|
+
const pads = `${top} ${bottom}`;
|
|
2202
|
+
if ((pagePads.get(page) ?? " ") !== pads) {
|
|
2203
|
+
pagePads.set(page, pads);
|
|
2204
|
+
page.style.paddingTop = top;
|
|
2205
|
+
page.style.paddingBottom = bottom;
|
|
2206
|
+
}
|
|
860
2207
|
});
|
|
861
2208
|
// Pages first, in order; anything else in the host ends up after them.
|
|
862
2209
|
let cursor = host.firstChild;
|
|
@@ -869,21 +2216,60 @@ export class EditorView {
|
|
|
869
2216
|
let i = 0;
|
|
870
2217
|
for (let p = 0; p < count; p++) {
|
|
871
2218
|
const page = this.pages[p];
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
2219
|
+
const first = i;
|
|
2220
|
+
while (i < items.length && items[i]?.page === p)
|
|
2221
|
+
i++;
|
|
2222
|
+
const onPage = items.slice(first, i);
|
|
2223
|
+
const { header, footer } = this.pageRegions[p] ?? {};
|
|
2224
|
+
const notes = this.pageNotes[p];
|
|
2225
|
+
/** What follows the text of a column: its endnotes. */
|
|
2226
|
+
const after = (band, column) => notes?.flow.get(`${band}|${column}`) ?? [];
|
|
2227
|
+
/** A page's children: its header, what it holds, its notes, its footer. */
|
|
2228
|
+
const framed = (list) => [
|
|
2229
|
+
...(header ? [header.element] : []),
|
|
2230
|
+
...list,
|
|
2231
|
+
...(notes?.foot ?? []),
|
|
2232
|
+
...(footer ? [footer.element] : []),
|
|
2233
|
+
];
|
|
2234
|
+
for (const { dom, slot } of onPage) {
|
|
2235
|
+
const position = slot.mark ? "absolute" : "";
|
|
2236
|
+
if (dom.style.position !== position)
|
|
2237
|
+
dom.style.position = position;
|
|
879
2238
|
}
|
|
880
|
-
//
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
this.
|
|
885
|
-
|
|
2239
|
+
// One section in one column: the blocks are the page's children.
|
|
2240
|
+
const last = onPage[onPage.length - 1];
|
|
2241
|
+
if (!last || (!last.slot.band && last.section.columns.widths.length === 1)) {
|
|
2242
|
+
fillElement(page, framed([...onPage.map((item) => item.dom), ...after(0, 0)]), this.viewRemoved);
|
|
2243
|
+
this.bands.delete(page);
|
|
2244
|
+
continue;
|
|
2245
|
+
}
|
|
2246
|
+
const known = this.bands.get(page) ?? [];
|
|
2247
|
+
const bands = [];
|
|
2248
|
+
for (const { section, slot } of onPage) {
|
|
2249
|
+
if (bands[slot.band])
|
|
2250
|
+
continue;
|
|
2251
|
+
const key = columnsKey(section.columns);
|
|
2252
|
+
const reused = known[slot.band];
|
|
2253
|
+
bands[slot.band] =
|
|
2254
|
+
reused?.key === key
|
|
2255
|
+
? reused
|
|
2256
|
+
: { key, ...bandElement(host.ownerDocument, section.columns) };
|
|
886
2257
|
}
|
|
2258
|
+
this.bands.set(page, bands);
|
|
2259
|
+
fillElement(page, framed(bands.map((b) => b.band)), this.viewRemoved);
|
|
2260
|
+
bands.forEach((band, b) => {
|
|
2261
|
+
const inBand = onPage.filter((item) => item.slot.band === b);
|
|
2262
|
+
const lead = inBand[0]?.slot.lead;
|
|
2263
|
+
const margin = lead ? `${lead}px` : "";
|
|
2264
|
+
if (band.band.style.marginTop !== margin)
|
|
2265
|
+
band.band.style.marginTop = margin;
|
|
2266
|
+
band.columns.forEach((column, c) => {
|
|
2267
|
+
fillElement(column, [
|
|
2268
|
+
...inBand.filter((item) => item.slot.column === c).map((item) => item.dom),
|
|
2269
|
+
...after(b, c),
|
|
2270
|
+
], this.viewRemoved);
|
|
2271
|
+
});
|
|
2272
|
+
});
|
|
887
2273
|
}
|
|
888
2274
|
// Pages no longer needed and stray DOM in the host: all after the last page by now.
|
|
889
2275
|
let rest = this.pages[count - 1].nextSibling;
|
|
@@ -917,7 +2303,7 @@ export class EditorView {
|
|
|
917
2303
|
}
|
|
918
2304
|
createPage() {
|
|
919
2305
|
const page = this.host.ownerDocument.createElement("section");
|
|
920
|
-
page.className = "foli-page";
|
|
2306
|
+
page.className = this.paged ? "foli-page" : "foli-story-flow";
|
|
921
2307
|
return page;
|
|
922
2308
|
}
|
|
923
2309
|
/** The last section's page setup (the envelope's), for an empty document. */
|
|
@@ -926,6 +2312,8 @@ export class EditorView {
|
|
|
926
2312
|
}
|
|
927
2313
|
/** The print rules for the document's page setups, written when they change. */
|
|
928
2314
|
applyPrintStyles(layout) {
|
|
2315
|
+
if (!this.paged)
|
|
2316
|
+
return;
|
|
929
2317
|
const sheets = [...new Set(layout.map((block) => block.sheet))];
|
|
930
2318
|
const key = [this.lastSheet(), ...sheets].map((sheet) => sheet.css).join("\n");
|
|
931
2319
|
if (key === this.printKey)
|
|
@@ -942,7 +2330,7 @@ export class EditorView {
|
|
|
942
2330
|
this.printStyles = parents.map((parent) => {
|
|
943
2331
|
const style = doc.createElement("style");
|
|
944
2332
|
style.setAttribute("data-foli-print", "");
|
|
945
|
-
style.textContent =
|
|
2333
|
+
style.textContent = viewPrintStyles(this.editor.settings, sheets, this.id);
|
|
946
2334
|
parent.append(style);
|
|
947
2335
|
return style;
|
|
948
2336
|
});
|
|
@@ -951,6 +2339,11 @@ export class EditorView {
|
|
|
951
2339
|
invalidateLayout() {
|
|
952
2340
|
this.measures = new WeakMap();
|
|
953
2341
|
this.flowed = null;
|
|
2342
|
+
this.regionsFor = null;
|
|
2343
|
+
if (this.regions)
|
|
2344
|
+
this.regions.cache = new WeakMap();
|
|
2345
|
+
if (this.notes)
|
|
2346
|
+
this.notes.cache = new WeakMap();
|
|
954
2347
|
}
|
|
955
2348
|
/** Re-flows the pages once web fonts have loaded: line heights and wrapping change. */
|
|
956
2349
|
onFontsLoaded = () => {
|
|
@@ -991,7 +2384,7 @@ export class EditorView {
|
|
|
991
2384
|
// A hidden host (display: none) reports every block as 0x0: nothing to re-flow.
|
|
992
2385
|
if (!entry.contentRect.width)
|
|
993
2386
|
continue;
|
|
994
|
-
scale ??=
|
|
2387
|
+
scale ??= this.scale();
|
|
995
2388
|
let height = 0;
|
|
996
2389
|
for (const fragment of desc.fragments)
|
|
997
2390
|
height += fragment.getBoundingClientRect().height;
|
|
@@ -1020,7 +2413,7 @@ export class EditorView {
|
|
|
1020
2413
|
if (this.destroyed)
|
|
1021
2414
|
return;
|
|
1022
2415
|
// compositionend flows what a composition kept from moving.
|
|
1023
|
-
if (this.composing) {
|
|
2416
|
+
if (this.composing || this.mount?.view.composing) {
|
|
1024
2417
|
this.unflowed = true;
|
|
1025
2418
|
return;
|
|
1026
2419
|
}
|
|
@@ -1039,15 +2432,18 @@ export class EditorView {
|
|
|
1039
2432
|
return null;
|
|
1040
2433
|
}
|
|
1041
2434
|
/**
|
|
1042
|
-
* Whether `node` holds blocks rather than content: the host, a page, or
|
|
1043
|
-
* table, rows and cells.
|
|
2435
|
+
* Whether `node` holds blocks rather than content: the host, a page, a band or column of one,
|
|
2436
|
+
* or a table's wrapper, table, rows and cells.
|
|
1044
2437
|
*/
|
|
1045
2438
|
holdsBlocks(node) {
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
2439
|
+
if (node === this.host || this.pages.includes(node))
|
|
2440
|
+
return true;
|
|
2441
|
+
if (node.nodeType !== Node.ELEMENT_NODE)
|
|
2442
|
+
return false;
|
|
2443
|
+
const element = node;
|
|
2444
|
+
return (element.classList.contains(BAND_CLASS) ||
|
|
2445
|
+
element.classList.contains(COLUMN_CLASS) ||
|
|
2446
|
+
(element.hasAttribute(FRAME_ATTR) && !element.closest(`[${REPEATED_ATTR}]`)));
|
|
1051
2447
|
}
|
|
1052
2448
|
/** Index of the first block that starts at or after `pos` (the block count if none). */
|
|
1053
2449
|
blockFrom(pos) {
|
|
@@ -1176,6 +2572,8 @@ export class EditorView {
|
|
|
1176
2572
|
const sel = this.host.ownerDocument.getSelection();
|
|
1177
2573
|
if (!sel?.anchorNode || !sel.focusNode)
|
|
1178
2574
|
return null;
|
|
2575
|
+
if (this.mount?.wrapper.contains(sel.anchorNode))
|
|
2576
|
+
return null;
|
|
1179
2577
|
// A range around exactly one block leaf element (a rule): the node selection that wrote it.
|
|
1180
2578
|
// (Its end, mapped alone, would be the start of the next block, not the end of the rule.)
|
|
1181
2579
|
if (sel.anchorNode === sel.focusNode && Math.abs(sel.anchorOffset - sel.focusOffset) === 1) {
|
|
@@ -1299,6 +2697,8 @@ export class EditorView {
|
|
|
1299
2697
|
showSelectedNode() {
|
|
1300
2698
|
this.showSelectedCells();
|
|
1301
2699
|
this.showSelectedPicture();
|
|
2700
|
+
this.showSelectedFields();
|
|
2701
|
+
this.drawControls();
|
|
1302
2702
|
const dom = this.selectedBlock()?.dom ?? this.selectedTable();
|
|
1303
2703
|
if (dom === this.selectedDom)
|
|
1304
2704
|
return;
|
|
@@ -1326,11 +2726,19 @@ export class EditorView {
|
|
|
1326
2726
|
const shown = selection instanceof AllSelection
|
|
1327
2727
|
? TextSelection.between(selection.$anchor, selection.$head)
|
|
1328
2728
|
: selection;
|
|
1329
|
-
|
|
2729
|
+
if (dom.anchor === shown.anchor && dom.head === shown.head)
|
|
2730
|
+
return true;
|
|
2731
|
+
// Field markers take no room, and a control's edge is one place to the browser: the DOM
|
|
2732
|
+
// cannot tell their two sides apart.
|
|
2733
|
+
const { doc } = this.state;
|
|
2734
|
+
const texts = shown instanceof TextSelection && dom instanceof TextSelection;
|
|
2735
|
+
const same = (a, b) => controlSpot(doc, a, b) ||
|
|
2736
|
+
(texts && doc.resolve(a).parent.inlineContent && sameSpot(doc, a, b));
|
|
2737
|
+
return same(dom.anchor, shown.anchor) && same(dom.head, shown.head);
|
|
1330
2738
|
}
|
|
1331
2739
|
hasFocus() {
|
|
1332
2740
|
const active = this.host.ownerDocument.activeElement;
|
|
1333
|
-
return active === this.host || this.host.contains(active);
|
|
2741
|
+
return ((active === this.host || this.host.contains(active)) && !this.mount?.wrapper.contains(active));
|
|
1334
2742
|
}
|
|
1335
2743
|
/** Writes the model selection to the DOM unless the DOM already shows it (keeps native affinity). */
|
|
1336
2744
|
writeSelection(force) {
|
|
@@ -1379,7 +2787,7 @@ export class EditorView {
|
|
|
1379
2787
|
*/
|
|
1380
2788
|
dispatch(tr) {
|
|
1381
2789
|
const marksAdded = tr.storedMarks !== null && tr.storedMarks !== this.state.storedMarks;
|
|
1382
|
-
if (!this.editable && (tr.docChanged || marksAdded))
|
|
2790
|
+
if ((!this.editable || this.mount) && (tr.docChanged || marksAdded))
|
|
1383
2791
|
return false;
|
|
1384
2792
|
this.editor.dispatch(tr);
|
|
1385
2793
|
return true;
|
|
@@ -1532,6 +2940,8 @@ export class EditorView {
|
|
|
1532
2940
|
onMouseMove = (event) => {
|
|
1533
2941
|
if (this.drag)
|
|
1534
2942
|
return;
|
|
2943
|
+
if (event.buttons === 0)
|
|
2944
|
+
this.controlChrome.hover(event.target);
|
|
1535
2945
|
const near = event.buttons === 0 && this.columnBorder(event) !== null;
|
|
1536
2946
|
if (near === this.resizeCursor)
|
|
1537
2947
|
return;
|
|
@@ -1772,12 +3182,7 @@ export class EditorView {
|
|
|
1772
3182
|
if (!files.length || this.composing)
|
|
1773
3183
|
return;
|
|
1774
3184
|
// The place the files were dropped.
|
|
1775
|
-
const
|
|
1776
|
-
const point = doc.caretPositionFromPoint?.(event.clientX, event.clientY);
|
|
1777
|
-
const range = point ? null : doc.caretRangeFromPoint?.(event.clientX, event.clientY);
|
|
1778
|
-
const node = point?.offsetNode ?? range?.startContainer;
|
|
1779
|
-
const offset = point?.offset ?? range?.startOffset ?? 0;
|
|
1780
|
-
const pos = node ? this.posFromDOM(node, offset) : null;
|
|
3185
|
+
const pos = this.posAtPoint(event.clientX, event.clientY);
|
|
1781
3186
|
if (pos !== null)
|
|
1782
3187
|
this.dispatch(this.state.tr.setSelection(TextSelection.near(this.state.doc.resolve(pos))));
|
|
1783
3188
|
void this.insertFiles(files);
|
|
@@ -1902,6 +3307,20 @@ export class EditorView {
|
|
|
1902
3307
|
}
|
|
1903
3308
|
return;
|
|
1904
3309
|
}
|
|
3310
|
+
// Word's blank page before an even or odd page section holds nothing: the caret goes to the
|
|
3311
|
+
// section after it (the browser would put it at the document's start).
|
|
3312
|
+
const blank = event.target;
|
|
3313
|
+
if (!event.shiftKey && this.pages.includes(blank) && !blank.firstChild) {
|
|
3314
|
+
const pos = this.posFromDOM(blank, 0);
|
|
3315
|
+
if (pos !== null) {
|
|
3316
|
+
event.preventDefault();
|
|
3317
|
+
if (!this.hasFocus())
|
|
3318
|
+
this.host.focus({ preventScroll: true });
|
|
3319
|
+
this.dispatch(this.state.tr.setSelection(Selection.near(this.state.doc.resolve(pos))));
|
|
3320
|
+
this.writeSelection(true);
|
|
3321
|
+
}
|
|
3322
|
+
return;
|
|
3323
|
+
}
|
|
1905
3324
|
const element = this.blockElement(event.target);
|
|
1906
3325
|
const owner = element ? this.owner.get(element) : undefined;
|
|
1907
3326
|
const index = owner ? (this.indexOf.get(owner) ?? -1) : -1;
|
|
@@ -1934,6 +3353,8 @@ export class EditorView {
|
|
|
1934
3353
|
*/
|
|
1935
3354
|
onClick = (event) => {
|
|
1936
3355
|
const target = event.target;
|
|
3356
|
+
if (event.type === "click" && !event.shiftKey && this.controlChrome.click(event))
|
|
3357
|
+
return;
|
|
1937
3358
|
const element = target instanceof Element ? target.closest("a") : null;
|
|
1938
3359
|
if (!element || !this.host.contains(element))
|
|
1939
3360
|
return;
|
|
@@ -1956,6 +3377,15 @@ export class EditorView {
|
|
|
1956
3377
|
return isSafeHref(href) ? href : null;
|
|
1957
3378
|
}
|
|
1958
3379
|
onKeyDown = (event) => {
|
|
3380
|
+
// The host itself focused while a story is open (Shift+Tab): it is read-only, and its selection
|
|
3381
|
+
// is what leaving the story comes back to.
|
|
3382
|
+
if (this.mount) {
|
|
3383
|
+
if (event.key === "Escape") {
|
|
3384
|
+
event.preventDefault();
|
|
3385
|
+
this.closeStory(true);
|
|
3386
|
+
}
|
|
3387
|
+
return;
|
|
3388
|
+
}
|
|
1959
3389
|
if (event.isComposing || this.composing)
|
|
1960
3390
|
return;
|
|
1961
3391
|
this.syncSelection();
|
|
@@ -1963,7 +3393,19 @@ export class EditorView {
|
|
|
1963
3393
|
event.preventDefault();
|
|
1964
3394
|
return;
|
|
1965
3395
|
}
|
|
3396
|
+
if (this.skipFieldParts(event)) {
|
|
3397
|
+
event.preventDefault();
|
|
3398
|
+
return;
|
|
3399
|
+
}
|
|
1966
3400
|
const name = keyName(event);
|
|
3401
|
+
if (this.documentEdgeKey(name) ||
|
|
3402
|
+
this.fieldKey(name) ||
|
|
3403
|
+
this.controlKey(name) ||
|
|
3404
|
+
this.noteKey(event, name) ||
|
|
3405
|
+
this.navigationKey(name)) {
|
|
3406
|
+
event.preventDefault();
|
|
3407
|
+
return;
|
|
3408
|
+
}
|
|
1967
3409
|
// Ctrl/Cmd+Shift+V: the paste event that follows uses only the plain text.
|
|
1968
3410
|
this.plainPaste = name === "Mod-Shift-v";
|
|
1969
3411
|
// Tab and Shift+Tab move the focus on in read-only mode, and right after Escape where Tab
|
|
@@ -2016,6 +3458,32 @@ export class EditorView {
|
|
|
2016
3458
|
event.preventDefault();
|
|
2017
3459
|
}
|
|
2018
3460
|
};
|
|
3461
|
+
/** Word's keys for notes, and Shift+Alt+N from a reference to its note. */
|
|
3462
|
+
noteKey(event, name) {
|
|
3463
|
+
if (isNoteKey(event))
|
|
3464
|
+
return this.openNote();
|
|
3465
|
+
const kind = name === "Mod-Alt-f" ? "footnote" : name === "Mod-Alt-d" ? "endnote" : null;
|
|
3466
|
+
if (!kind || !this.editable || !this.notes)
|
|
3467
|
+
return false;
|
|
3468
|
+
return this.run(insertNote(kind)) && this.openNote();
|
|
3469
|
+
}
|
|
3470
|
+
/**
|
|
3471
|
+
* Ctrl+Home and Ctrl+End (with Shift, extending) on pages with headers and footers: the browser
|
|
3472
|
+
* stops at the first or last place before what it cannot edit, so the model says where.
|
|
3473
|
+
*/
|
|
3474
|
+
documentEdgeKey(name) {
|
|
3475
|
+
const shift = name.startsWith("Mod-Shift-");
|
|
3476
|
+
const key = name.replace("Mod-Shift-", "Mod-");
|
|
3477
|
+
if ((key !== "Mod-Home" && key !== "Mod-End") || !this.editable)
|
|
3478
|
+
return false;
|
|
3479
|
+
if (!this.pageRegions.some((page) => page.header || page.footer))
|
|
3480
|
+
return false;
|
|
3481
|
+
const { doc, selection } = this.state;
|
|
3482
|
+
const edge = key === "Mod-Home" ? Selection.atStart(doc) : Selection.atEnd(doc);
|
|
3483
|
+
const next = shift ? TextSelection.between(selection.$anchor, edge.$head) : edge;
|
|
3484
|
+
this.dispatch(this.state.tr.setSelection(next).setStoredMarks(null));
|
|
3485
|
+
return true;
|
|
3486
|
+
}
|
|
2019
3487
|
/**
|
|
2020
3488
|
* Whether the caret is on the first (`dir` -1) or last (1) line of its textblock, in the
|
|
2021
3489
|
* block's first or last fragment (a block split across pages has several).
|
|
@@ -2159,9 +3627,10 @@ export class EditorView {
|
|
|
2159
3627
|
* and so is the scroll position.
|
|
2160
3628
|
*/
|
|
2161
3629
|
onFocus = () => {
|
|
3630
|
+
this.claimStories();
|
|
2162
3631
|
const pointer = this.pointerFocus;
|
|
2163
3632
|
this.pointerFocus = false;
|
|
2164
|
-
if (pointer)
|
|
3633
|
+
if (pointer || this.mount)
|
|
2165
3634
|
return;
|
|
2166
3635
|
const current = this.readSelection();
|
|
2167
3636
|
if (current && this.shows(current))
|
|
@@ -2407,6 +3876,10 @@ export class EditorView {
|
|
|
2407
3876
|
*/
|
|
2408
3877
|
paste(data, autolink = true) {
|
|
2409
3878
|
const { state } = this;
|
|
3879
|
+
// A text control takes the text alone.
|
|
3880
|
+
const plainText = data.getData("text/plain");
|
|
3881
|
+
if (plainText && this.run(insertControlText(plainText)))
|
|
3882
|
+
return true;
|
|
2410
3883
|
const text = autolink && !data.getData(FOLI_MIME) ? data.getData("text/plain") : "";
|
|
2411
3884
|
const href = text ? autolinkHref(text) : null;
|
|
2412
3885
|
if (href) {
|
|
@@ -2432,7 +3905,7 @@ export class EditorView {
|
|
|
2432
3905
|
const only = pasted?.doc.childCount === 1 ? pasted.doc.firstChild : null;
|
|
2433
3906
|
if (!pasted || (only?.isTextblock && !only.content.size))
|
|
2434
3907
|
return false;
|
|
2435
|
-
const { doc, open, envelope } = pasted;
|
|
3908
|
+
const { doc, open, envelope, notes } = pasted;
|
|
2436
3909
|
// As a slice (text joins the text around the caret); where that cannot be (an opaque block
|
|
2437
3910
|
// at an end), as whole blocks at the caret.
|
|
2438
3911
|
const asSlice = (tr) => {
|
|
@@ -2476,6 +3949,8 @@ export class EditorView {
|
|
|
2476
3949
|
withEnvelope(tr, envelope);
|
|
2477
3950
|
setComments(tr, current.comments, envelope.comments);
|
|
2478
3951
|
}
|
|
3952
|
+
for (const { id, story } of notes ?? [])
|
|
3953
|
+
tr.step(new StoryStep(id, null, story));
|
|
2479
3954
|
}
|
|
2480
3955
|
catch (error) {
|
|
2481
3956
|
if (error instanceof TransformError)
|
|
@@ -2487,7 +3962,7 @@ export class EditorView {
|
|
|
2487
3962
|
return false;
|
|
2488
3963
|
}
|
|
2489
3964
|
onSelectionChange = () => {
|
|
2490
|
-
if (!this.composing && this.hasFocus())
|
|
3965
|
+
if (!this.composing && !this.mount && this.hasFocus())
|
|
2491
3966
|
this.syncSelection();
|
|
2492
3967
|
};
|
|
2493
3968
|
}
|
|
@@ -2720,59 +4195,108 @@ function measureBox(desc, scale) {
|
|
|
2720
4195
|
/**
|
|
2721
4196
|
* What the flow needs of a block: its measurement, its lines (on demand), Word's spacing before
|
|
2722
4197
|
* and after (next to its neighbours), keep with next, keep lines together (not for a block
|
|
2723
|
-
* taller than a page, or one with a
|
|
2724
|
-
* and a page break before the next block ending this one's page.
|
|
4198
|
+
* taller than a page, or one with a break in it), page and column breaks ending their line's
|
|
4199
|
+
* page or column, and a page break before the next block ending this one's page.
|
|
2725
4200
|
*/
|
|
2726
|
-
function wordBox(words, i, desc, measure, lines, layout) {
|
|
4201
|
+
function wordBox(words, i, desc, measure, lines, layout, atWidth, adds, noteOf) {
|
|
2727
4202
|
const pageHeight = contentHeight(layout.sheet.settings);
|
|
4203
|
+
const { section } = layout;
|
|
4204
|
+
// Without pages nothing is kept off the next one, and a page break ends nothing.
|
|
4205
|
+
const paged = layout.sheet.css !== UNPAGED_CSS;
|
|
2728
4206
|
const next = words[i + 1] ? { word: words[i + 1] } : undefined;
|
|
2729
|
-
const { before, after } = spacing(words[i - 1], desc.word, next?.word);
|
|
2730
|
-
const
|
|
2731
|
-
const
|
|
2732
|
-
const
|
|
4207
|
+
const { before, after } = spacing(words[i - 1], desc.word, next?.word, adds);
|
|
4208
|
+
const leavesWith = (attr) => desc.leaves.filter((leaf) => leaf.dom.nodeType !== Node.TEXT_NODE && leaf.dom.hasAttribute(attr));
|
|
4209
|
+
const pageBreaks = leavesWith(PAGE_BREAK_ATTR);
|
|
4210
|
+
const columnBreaks = leavesWith(WORD_COLUMN_BREAK_ATTR);
|
|
4211
|
+
const hasBreak = pageBreaks.length + columnBreaks.length > 0;
|
|
4212
|
+
const keep = desc.word.keepLines && !hasBreak;
|
|
4213
|
+
const kept = keep && measure.height <= pageHeight;
|
|
2733
4214
|
const bottoms = () => (kept ? [measure.height] : lines().bottoms);
|
|
4215
|
+
/** The block as it is in `column`, when the columns' widths differ. */
|
|
4216
|
+
const inColumn = (column) => atWidth(desc, section.columns.widths[column], keep);
|
|
4217
|
+
// The line holding a break, among the column's lines: the last starting at or before it.
|
|
4218
|
+
const linesOf = (leaves, column = 0) => {
|
|
4219
|
+
if (!leaves.length)
|
|
4220
|
+
return [];
|
|
4221
|
+
const { starts } = section.unequal ? inColumn(column).all() : lines();
|
|
4222
|
+
return leaves.map((leaf) => {
|
|
4223
|
+
let line = starts.length - 1;
|
|
4224
|
+
while (line > 0 && starts[line] > leaf.offset)
|
|
4225
|
+
line--;
|
|
4226
|
+
return line;
|
|
4227
|
+
});
|
|
4228
|
+
};
|
|
4229
|
+
const references = referencesIn(desc.node);
|
|
4230
|
+
const notes = references.flatMap((ref) => {
|
|
4231
|
+
const note = noteOf(ref.story);
|
|
4232
|
+
return note ? [{ note, offset: ref.offset - 1 }] : [];
|
|
4233
|
+
});
|
|
2734
4234
|
return {
|
|
4235
|
+
notes: notes.length
|
|
4236
|
+
? (column) => {
|
|
4237
|
+
if (kept)
|
|
4238
|
+
return notes.map(({ note }) => ({ line: 0, note }));
|
|
4239
|
+
const { starts } = section.unequal ? inColumn(column).all() : lines();
|
|
4240
|
+
return notes.map(({ note, offset }) => {
|
|
4241
|
+
let line = starts.length - 1;
|
|
4242
|
+
while (line > 0 && starts[line] > offset)
|
|
4243
|
+
line--;
|
|
4244
|
+
return { line, note };
|
|
4245
|
+
});
|
|
4246
|
+
}
|
|
4247
|
+
: undefined,
|
|
2735
4248
|
height: measure.height,
|
|
2736
4249
|
marginTop: before,
|
|
2737
4250
|
marginBottom: after,
|
|
2738
4251
|
keepWithNext: desc.word.keepNext,
|
|
2739
4252
|
pageHeight,
|
|
4253
|
+
section: section.flow,
|
|
4254
|
+
sectionMark: !!desc.node.attrs.section && desc.word.empty,
|
|
4255
|
+
inColumn: section.unequal ? inColumn : undefined,
|
|
2740
4256
|
lines: bottoms,
|
|
2741
|
-
|
|
2742
|
-
|
|
2743
|
-
|
|
2744
|
-
|
|
2745
|
-
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
return line;
|
|
2749
|
-
});
|
|
2750
|
-
if (next?.word.pageBreakBefore || layout.breakAfter)
|
|
2751
|
-
breaks.push(bottoms().length - 1);
|
|
4257
|
+
columnBreaksAfter: (column) => linesOf(columnBreaks, column),
|
|
4258
|
+
breaksAfter: (column = 0) => {
|
|
4259
|
+
if (!paged)
|
|
4260
|
+
return [];
|
|
4261
|
+
const breaks = linesOf(pageBreaks, column);
|
|
4262
|
+
if (next?.word.pageBreakBefore)
|
|
4263
|
+
breaks.push((section.unequal ? inColumn(column).lines() : bottoms()).length - 1);
|
|
2752
4264
|
return breaks;
|
|
2753
4265
|
},
|
|
2754
4266
|
};
|
|
2755
4267
|
}
|
|
4268
|
+
/** The footnotes a table refers to, each on the break at or after its reference's line. */
|
|
4269
|
+
function tableNotes(frame, measure, blocks, lines, breaks, noteOf) {
|
|
4270
|
+
const t = measure.table;
|
|
4271
|
+
const found = [];
|
|
4272
|
+
for (let unit = frame.first; unit < frame.end; unit++)
|
|
4273
|
+
for (const ref of referencesIn(blocks[unit].node)) {
|
|
4274
|
+
const note = noteOf(ref.story);
|
|
4275
|
+
if (note && t.tops.get(unit) != null)
|
|
4276
|
+
found.push({ note, unit, offset: ref.offset - 1 });
|
|
4277
|
+
}
|
|
4278
|
+
if (!found.length)
|
|
4279
|
+
return undefined;
|
|
4280
|
+
return () => {
|
|
4281
|
+
const list = breaks();
|
|
4282
|
+
return found.map(({ note, unit, offset }) => {
|
|
4283
|
+
const { starts, bottoms } = lines(blocks[unit]);
|
|
4284
|
+
let k = starts.length - 1;
|
|
4285
|
+
while (k > 0 && starts[k] > offset)
|
|
4286
|
+
k--;
|
|
4287
|
+
const y = t.tops.get(unit) + bottoms[k];
|
|
4288
|
+
const line = list.findIndex((b) => b >= y - 0.5);
|
|
4289
|
+
return { line: line < 0 ? list.length - 1 : line, note };
|
|
4290
|
+
});
|
|
4291
|
+
};
|
|
4292
|
+
}
|
|
2756
4293
|
/**
|
|
2757
|
-
* Writes Word's space above each v3 block as
|
|
2758
|
-
*
|
|
2759
|
-
* and only on the first page or after an explicit break (a page the text flowed onto drops it).
|
|
2760
|
-
* Written only when it changes.
|
|
4294
|
+
* Writes Word's space above each v3 block, as the flow worked it out, as its top margin. Written
|
|
4295
|
+
* only when it changes.
|
|
2761
4296
|
*/
|
|
2762
|
-
function applyGaps(firsts,
|
|
4297
|
+
function applyGaps(firsts, spaces) {
|
|
2763
4298
|
firsts.forEach((dom, i) => {
|
|
2764
|
-
const
|
|
2765
|
-
const above = pages[i - 1];
|
|
2766
|
-
const box = boxes[i];
|
|
2767
|
-
let gap;
|
|
2768
|
-
if (above && above[above.length - 1] === page)
|
|
2769
|
-
gap = boxes[i - 1].marginBottom + box.marginTop;
|
|
2770
|
-
else {
|
|
2771
|
-
const prior = boxes[i - 1];
|
|
2772
|
-
const breaks = prior?.breaksAfter?.() ?? [];
|
|
2773
|
-
const forced = page === 0 || (!!prior && breaks.length > 0 && breaks.includes(prior.lines().length - 1));
|
|
2774
|
-
gap = forced ? box.marginTop : 0;
|
|
2775
|
-
}
|
|
4299
|
+
const gap = spaces[i] ?? 0;
|
|
2776
4300
|
if (gaps.get(dom) === gap)
|
|
2777
4301
|
return;
|
|
2778
4302
|
gaps.set(dom, gap);
|
|
@@ -2829,6 +4353,9 @@ function measureLines(desc, scale) {
|
|
|
2829
4353
|
continue;
|
|
2830
4354
|
if (leaf.dom.nodeType !== Node.TEXT_NODE) {
|
|
2831
4355
|
const br = leaf.dom;
|
|
4356
|
+
// A raised number says nothing about where its line is.
|
|
4357
|
+
if (br.hasAttribute(NOTE_NUMBER_ATTR))
|
|
4358
|
+
continue;
|
|
2832
4359
|
items.push({ offset: leaf.offset, rect: () => br.getBoundingClientRect() });
|
|
2833
4360
|
continue;
|
|
2834
4361
|
}
|
|
@@ -2957,6 +4484,22 @@ function scrollRectIntoView(from, rect) {
|
|
|
2957
4484
|
if (move)
|
|
2958
4485
|
win.scrollBy(0, move);
|
|
2959
4486
|
}
|
|
4487
|
+
const SCROLL_OPTIONS = { capture: true, passive: true };
|
|
4488
|
+
/** Room kept above what is scrolled to for find, in px. */
|
|
4489
|
+
const REVEAL_ABOVE = 200;
|
|
4490
|
+
/** Word's keys for finding and going places. */
|
|
4491
|
+
const NAVIGATION_KEYS = {
|
|
4492
|
+
"Mod-f": "find",
|
|
4493
|
+
"Mod-h": "replace",
|
|
4494
|
+
"Mod-g": "goTo",
|
|
4495
|
+
F3: "next",
|
|
4496
|
+
"Shift-F3": "previous",
|
|
4497
|
+
"Mod-Shift-F5": "bookmarks",
|
|
4498
|
+
};
|
|
4499
|
+
/** Shift+Alt+N: from a note's reference to the note, and back. */
|
|
4500
|
+
const isNoteKey = (event) => event.altKey && event.shiftKey && !event.ctrlKey && !event.metaKey && event.code === "KeyN";
|
|
4501
|
+
/** Whether story `id` of `state`'s document exists (a number can outlive its note on screen). */
|
|
4502
|
+
const isNoteStory = (state, id) => !!wordContext(state)?.stories.has(id);
|
|
2960
4503
|
function before(node) {
|
|
2961
4504
|
const parent = node.parentNode;
|
|
2962
4505
|
return { node: parent, offset: Array.prototype.indexOf.call(parent.childNodes, node) };
|
|
@@ -3050,11 +4593,13 @@ function holdsFloat(node) {
|
|
|
3050
4593
|
}
|
|
3051
4594
|
return found;
|
|
3052
4595
|
}
|
|
3053
|
-
/** A change that only adds or removes
|
|
4596
|
+
/** A change that only adds or removes the view's own marks (a resize handle, a control's frame). */
|
|
3054
4597
|
function handleOnly(record) {
|
|
3055
4598
|
const nodes = [...record.addedNodes, ...record.removedNodes];
|
|
3056
4599
|
return (record.type === "childList" &&
|
|
3057
4600
|
nodes.length > 0 &&
|
|
3058
|
-
nodes.every((n) => n.nodeType === 1 &&
|
|
4601
|
+
nodes.every((n) => n.nodeType === 1 &&
|
|
4602
|
+
(n.hasAttribute("data-foli-resize") ||
|
|
4603
|
+
n.hasAttribute(CONTROL_UI_ATTR))));
|
|
3059
4604
|
}
|
|
3060
4605
|
//# sourceMappingURL=view.js.map
|