@folijs/view 0.0.0-stage → 0.1.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.
@@ -0,0 +1,200 @@
1
+ /** CSS pixels per millimetre (CSS fixes 1in = 96px = 25.4mm). */
2
+ export const PX_PER_MM = 96 / 25.4;
3
+ export const DEFAULT_RULES = { orphans: 2, widows: 2 };
4
+ /** Height of a page's content box (page height minus top and bottom margins), in CSS px. */
5
+ export function contentHeight({ pageSize, margins }) {
6
+ return (pageSize.height - margins.top - margins.bottom) * PX_PER_MM;
7
+ }
8
+ // Half a pixel of slack: measured heights are fractional and rounding must not add a page.
9
+ const SLACK = 0.5;
10
+ /**
11
+ * Page flow: the fragments of each block, in order. Blocks fill a page in order; a block that
12
+ * does not fit is split at a line boundary, the lines that fit staying on the page, the rest
13
+ * continuing at the top of the next one (as many pages as it takes). A page with room for no
14
+ * line of the next block ends; an empty page always takes at least one line.
15
+ *
16
+ * Rules:
17
+ * - Explicit page breaks: the line holding one ends its page.
18
+ * - Widows and orphans: a split leaves at least `orphans` lines at the bottom of a page and
19
+ * carries at least `widows` lines to the next; a block too short for both moves whole. Not
20
+ * applied where only an empty page could hold the lines, nor across an explicit break.
21
+ * - Keep with next: a `keepWithNext` block (and a chain of them) moves to the next page when
22
+ * the block after it would start there, unless it already starts its page or the move would
23
+ * not bring them together (then the flow stays as it was).
24
+ *
25
+ * Spacing follows CSS inside a padded page: the first block keeps its top margin, adjacent
26
+ * margins collapse to the larger, and the last block's bottom margin stays inside the page
27
+ * (it does not collapse into the padding), so it must fit too. Fragments are rendered without
28
+ * the margins at their split edges: a continued fragment has no bottom margin, a continuation
29
+ * no top margin.
30
+ *
31
+ * shortcut: positive margins only (negative margins collapse differently).
32
+ */
33
+ export function flow(boxes, pageHeight, rules = DEFAULT_RULES) {
34
+ return flowFrom(boxes, pageHeight, rules).fragments;
35
+ }
36
+ /**
37
+ * `flow`, reusing a previous one: it starts at `reuse.from` (backed up over a chain of
38
+ * keep-with-next blocks, whose placement depends on what follows) with the previous state
39
+ * there, and once the state before an unchanged block equals the previous state before it, the
40
+ * rest is the previous flow's. The result is the same as a full flow.
41
+ */
42
+ export function flowFrom(boxes, pageHeight, rules = DEFAULT_RULES, reuse) {
43
+ let start = reuse ? Math.min(reuse.from, boxes.length, reuse.previous.fragments.length) : 0;
44
+ while (start > 0 && boxes[start - 1]?.keepWithNext)
45
+ start--;
46
+ const result = reuse ? reuse.previous.fragments.slice(0, start) : [];
47
+ const snapshots = reuse ? reuse.previous.states.slice(0, start) : [];
48
+ let state = (reuse && reuse.previous.states[start]
49
+ ? { ...reuse.previous.states[start] }
50
+ : { page: 0, used: 0, previousMargin: 0, onPage: 0, breakPending: false });
51
+ /** Blocks that start a page because the keep-with-next rule moved them. */
52
+ const breakBefore = new Set();
53
+ const newPage = () => {
54
+ state.page++;
55
+ state.used = 0;
56
+ state.onPage = 0;
57
+ };
58
+ const place = (box, index) => {
59
+ if ((state.breakPending || breakBefore.has(index)) && state.onPage)
60
+ newPage();
61
+ state.breakPending = false;
62
+ const above = state.previousMargin;
63
+ state.previousMargin = box.marginBottom;
64
+ const gap = () => (state.onPage ? Math.max(above, box.marginTop) : box.marginTop);
65
+ const forced = box.breaksAfter?.() ?? [];
66
+ if (!forced.length &&
67
+ state.used + gap() + box.height + box.marginBottom <= pageHeight + SLACK) {
68
+ state.used += gap() + box.height;
69
+ state.onPage++;
70
+ return [{ page: state.page, fromLine: 0, toLine: Infinity }];
71
+ }
72
+ const lines = box.lines();
73
+ const count = lines.length;
74
+ const breaks = new Set(forced.filter((line) => line >= 0 && line < count));
75
+ const fragments = [];
76
+ let line = 0;
77
+ while (line < count) {
78
+ // The lines up to the next explicit break (or the end) can be split freely.
79
+ let segmentEnd = line;
80
+ while (segmentEnd < count && !breaks.has(segmentEnd))
81
+ segmentEnd++;
82
+ segmentEnd = Math.min(segmentEnd + 1, count);
83
+ const top = line ? lines[line - 1] : 0;
84
+ // Only the block's first fragment has the gap above it.
85
+ const spacing = line === 0 ? gap() : 0;
86
+ let end = line;
87
+ while (end < segmentEnd) {
88
+ const bottom = lines[end] - top + (end + 1 === count ? box.marginBottom : 0);
89
+ if (state.used + spacing + bottom > pageHeight + SLACK)
90
+ break;
91
+ end++;
92
+ }
93
+ if (end < segmentEnd && end > line) {
94
+ // A natural split: keep `widows` lines for the next page and `orphans` on this one.
95
+ const kept = Math.min(end, segmentEnd - rules.widows);
96
+ if (kept - line >= rules.orphans)
97
+ end = kept;
98
+ else if (state.onPage)
99
+ end = line;
100
+ }
101
+ if (end === line) {
102
+ if (state.onPage) {
103
+ newPage();
104
+ continue;
105
+ }
106
+ end = line + 1; // a line taller than a page is placed anyway
107
+ }
108
+ fragments.push({ page: state.page, fromLine: line, toLine: end });
109
+ state.used += spacing + lines[end - 1] - top;
110
+ state.onPage++;
111
+ line = end;
112
+ const broken = breaks.has(end - 1);
113
+ if (line < count)
114
+ newPage();
115
+ else if (broken)
116
+ state.breakPending = true;
117
+ }
118
+ // Moved whole to a later page: not split.
119
+ const [only] = fragments;
120
+ if (fragments.length === 1 && only && !forced.length)
121
+ only.toLine = Infinity;
122
+ return fragments;
123
+ };
124
+ /** The flow as it was before a keep-with-next move, by the block that triggered it. */
125
+ const attempts = new Map();
126
+ let index = start;
127
+ while (index < boxes.length) {
128
+ // Realigned with the previous flow: the rest is unchanged (no keep-with-next decision
129
+ // about this block, made with the block before it, is pending). The block before it must
130
+ // be unchanged too: had it been keep-with-next before the edit, the previous flow from
131
+ // here holds a decision about a chain that no longer exists.
132
+ const was = reuse && index > start && !attempts.size && reuse.previousIndex(index - 1) >= 0
133
+ ? reuse.previousIndex(index)
134
+ : -1;
135
+ const before = was >= 0 ? reuse?.previous.states[was] : undefined;
136
+ if (before && !boxes[index - 1]?.keepWithNext && sameState(before, state)) {
137
+ const previous = reuse;
138
+ result.length = index;
139
+ snapshots.length = index;
140
+ result.push(...previous.previous.fragments.slice(was));
141
+ snapshots.push(...previous.previous.states.slice(was, -1));
142
+ return {
143
+ fragments: result,
144
+ states: [...snapshots, { ...previous.previous.states.at(-1) }],
145
+ };
146
+ }
147
+ snapshots[index] = { ...state };
148
+ const fragments = place(boxes[index], index);
149
+ result[index] = fragments;
150
+ const previous = result[index - 1];
151
+ const startsLater = previous !== undefined &&
152
+ (fragments[0]?.page ?? 0) > (previous[previous.length - 1]?.page ?? 0);
153
+ const attempt = attempts.get(index);
154
+ if (attempt) {
155
+ attempts.delete(index);
156
+ // The move did not bring them together (the block does not fit after the heading on a
157
+ // page of its own either): keep the flow as it was.
158
+ if (startsLater) {
159
+ result.splice(attempt.first, result.length - attempt.first, ...attempt.result);
160
+ snapshots.splice(attempt.first, snapshots.length - attempt.first, ...attempt.snapshots);
161
+ state = attempt.state;
162
+ index++;
163
+ continue;
164
+ }
165
+ }
166
+ if (startsLater && boxes[index - 1]?.keepWithNext) {
167
+ // The chain of keep-with-next blocks before this one, each whole on one page.
168
+ let first = index - 1;
169
+ while (first > 0 &&
170
+ boxes[first - 1]?.keepWithNext &&
171
+ result[first - 1]?.length === 1 &&
172
+ result[first - 1]?.[0]?.page === result[first]?.[0]?.page)
173
+ first--;
174
+ const beforeFirst = snapshots[first];
175
+ if (!breakBefore.has(first) && beforeFirst.onPage > 0 && result[first]?.length === 1) {
176
+ attempts.set(index, {
177
+ first,
178
+ result: result.slice(first),
179
+ snapshots: snapshots.slice(first),
180
+ state: { ...state },
181
+ });
182
+ breakBefore.add(first);
183
+ state = { ...beforeFirst };
184
+ result.length = first;
185
+ index = first;
186
+ continue;
187
+ }
188
+ }
189
+ index++;
190
+ }
191
+ return { fragments: result, states: [...snapshots.slice(0, boxes.length), { ...state }] };
192
+ }
193
+ function sameState(a, b) {
194
+ return (a.page === b.page &&
195
+ a.used === b.used &&
196
+ a.previousMargin === b.previousMargin &&
197
+ a.onPage === b.onPage &&
198
+ a.breakPending === b.breakPending);
199
+ }
200
+ //# sourceMappingURL=flow.js.map
@@ -0,0 +1 @@
1
+ {"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K,CAAC,KAAK,CAAC;oBAC3B,SAAS,EAAE,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC;oBACjC,KAAK,EAAE,EAAE,GAAG,KAAK,EAAE;iBACpB,CAAC,CAAC;gBACH,WAAW,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBACvB,KAAK,GAAG,EAAE,GAAG,WAAW,EAAE,CAAC;gBAC3B,MAAM,CAAC,MAAM,GAAG,KAAK,CAAC;gBACtB,KAAK,GAAG,KAAK,CAAC;gBACd,SAAS;YACX,CAAC;QACH,CAAC;QACD,KAAK,EAAE,CAAC;IACV,CAAC;IACD,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,EAAE,EAAE,GAAG,KAAK,EAAE,CAAC,EAAE,CAAC;AAC5F,CAAC;AAED,SAAS,SAAS,CAAC,CAAY,EAAE,CAAY;IAC3C,OAAO,CACL,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI;QACjB,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI;QACjB,CAAC,CAAC,cAAc,KAAK,CAAC,CAAC,cAAc;QACrC,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QACrB,CAAC,CAAC,YAAY,KAAK,CAAC,CAAC,YAAY,CAClC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,32 @@
1
+ import type { Node as ModelNode } from "@folijs/core";
2
+ /** Marks the <br> of an explicit page break. */
3
+ export declare const PAGE_BREAK_ATTR = "data-page-break";
4
+ /**
5
+ * A rendered inline leaf (a text node or hard break) and where it sits in its block's content.
6
+ * A text node split across fragments is two leaves, one per fragment.
7
+ */
8
+ export interface LeafDesc {
9
+ readonly dom: Node;
10
+ readonly offset: number;
11
+ readonly size: number;
12
+ }
13
+ /**
14
+ * View descriptor of one block: the model node, its elements and its leaves. A block split
15
+ * across pages has one element per page (its fragments, in order); `dom` is the first.
16
+ */
17
+ export interface BlockDesc {
18
+ readonly node: ModelNode;
19
+ readonly dom: HTMLElement;
20
+ readonly fragments: readonly HTMLElement[];
21
+ /** Content offsets where each fragment after the first starts (sorted, inside the content). */
22
+ readonly splits: readonly number[];
23
+ readonly leaves: readonly LeafDesc[];
24
+ }
25
+ /**
26
+ * Renders one textblock with createElement/textContent only (never innerHTML), and records its
27
+ * leaves for position mapping. With `splits`, the content is rendered as several fragments of
28
+ * the same element type: each continued fragment has no bottom margin and each continuation no
29
+ * top margin, so together they take the same height as the whole block.
30
+ */
31
+ export declare function renderBlock(node: ModelNode, doc: Document, splits?: readonly number[]): BlockDesc;
32
+ //# sourceMappingURL=render.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAY,IAAI,IAAI,SAAS,EAAE,MAAM,cAAc,CAAC;AAYhE,gDAAgD;AAChD,eAAO,MAAM,eAAe,oBAAoB,CAAC;AAEjD;;;GAGG;AACH,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,GAAG,EAAE,IAAI,CAAC;IACnB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,IAAI,EAAE,SAAS,CAAC;IACzB,QAAQ,CAAC,GAAG,EAAE,WAAW,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,SAAS,WAAW,EAAE,CAAC;IAC3C,+FAA+F;IAC/F,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,QAAQ,CAAC,MAAM,EAAE,SAAS,QAAQ,EAAE,CAAC;CACtC;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,IAAI,EAAE,SAAS,EACf,GAAG,EAAE,QAAQ,EACb,MAAM,GAAE,SAAS,MAAM,EAAO,GAC7B,SAAS,CAiDX"}
package/dist/render.js ADDED
@@ -0,0 +1,70 @@
1
+ // Every schema v1 mark must have a tag; marks of other schemas fall back to <span>.
2
+ const MARK_TAGS = new Map(Object.entries({
3
+ bold: "strong",
4
+ italic: "em",
5
+ underline: "u",
6
+ strike: "s",
7
+ }));
8
+ /** Marks the <br> of an explicit page break. */
9
+ export const PAGE_BREAK_ATTR = "data-page-break";
10
+ /**
11
+ * Renders one textblock with createElement/textContent only (never innerHTML), and records its
12
+ * leaves for position mapping. With `splits`, the content is rendered as several fragments of
13
+ * the same element type: each continued fragment has no bottom margin and each continuation no
14
+ * top margin, so together they take the same height as the whole block.
15
+ */
16
+ export function renderBlock(node, doc, splits = []) {
17
+ const tag = node.type.name === "heading" ? `h${String(node.attrs.level)}` : "p";
18
+ const fragments = [0, ...splits].map((_, i, all) => {
19
+ const fragment = doc.createElement(tag);
20
+ if (all.length > 1) {
21
+ fragment.dataset.fragment = String(i);
22
+ if (i > 0)
23
+ fragment.style.marginTop = "0";
24
+ if (i < all.length - 1)
25
+ fragment.style.marginBottom = "0";
26
+ }
27
+ return fragment;
28
+ });
29
+ const leaves = [];
30
+ /** The fragment that holds content offset `at` (a split offset starts the next fragment). */
31
+ const fragmentAt = (at) => {
32
+ let i = 0;
33
+ while (i < splits.length && splits[i] <= at)
34
+ i++;
35
+ return fragments[i];
36
+ };
37
+ node.forEach((child, offset) => {
38
+ if (!child.isText) {
39
+ // A hard break or a page break: both end their line (the flow ends the page at a page break).
40
+ const br = doc.createElement("br");
41
+ if (child.type.name === "pageBreak")
42
+ br.setAttribute(PAGE_BREAK_ATTR, "");
43
+ leaves.push({ dom: br, offset, size: child.nodeSize });
44
+ // Text only: a mark wrapper around <br> would add nothing visible.
45
+ fragmentAt(offset).append(br);
46
+ return;
47
+ }
48
+ const text = child.text ?? "";
49
+ const cuts = splits.filter((split) => split > offset && split < offset + text.length);
50
+ let from = offset;
51
+ for (const to of [...cuts, offset + text.length]) {
52
+ const leaf = doc.createTextNode(text.slice(from - offset, to - offset));
53
+ leaves.push({ dom: leaf, offset: from, size: to - from });
54
+ let outer = leaf;
55
+ for (const mark of [...child.marks].reverse()) {
56
+ const wrapper = doc.createElement(MARK_TAGS.get(mark.type.name) ?? "span");
57
+ wrapper.append(outer);
58
+ outer = wrapper;
59
+ }
60
+ fragmentAt(from).append(outer);
61
+ from = to;
62
+ }
63
+ });
64
+ // An empty block, or one ending in a hard break, needs an extra <br> to show its last line.
65
+ if (!node.lastChild?.isText)
66
+ fragments[fragments.length - 1].append(doc.createElement("br"));
67
+ const dom = fragments[0];
68
+ return { node, dom, fragments, splits, leaves };
69
+ }
70
+ //# sourceMappingURL=render.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAEA,oFAAoF;AACpF,MAAM,SAAS,GAAgC,IAAI,GAAG,CACpD,MAAM,CAAC,OAAO,CAAC;IACb,IAAI,EAAE,QAAQ;IACd,MAAM,EAAE,IAAI;IACZ,SAAS,EAAE,GAAG;IACd,MAAM,EAAE,GAAG;CACuB,CAAC,CACtC,CAAC;AAEF,gDAAgD;AAChD,MAAM,CAAC,MAAM,eAAe,GAAG,iBAAiB,CAAC;AAyBjD;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CACzB,IAAe,EACf,GAAa,EACb,MAAM,GAAsB,EAAE;IAE9B,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC;IAChF,MAAM,SAAS,GAAG,CAAC,CAAC,EAAE,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,EAAE;QACjD,MAAM,QAAQ,GAAG,GAAG,CAAC,aAAa,CA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M,GAAG,CAAC,CAAiB,CAAC,MAAM,CAAC,GAAG,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC;IACnF,MAAM,GAAG,GAAG,SAAS,CAAC,CAAC,CAAgB,CAAC;IACxC,OAAO,EAAE,IAAI,EAAE,GAAG,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAClD,CAAC"}
package/dist/view.d.ts ADDED
@@ -0,0 +1,225 @@
1
+ import { type Editor } from "@folijs/core";
2
+ interface DomPoint {
3
+ node: Node;
4
+ offset: number;
5
+ }
6
+ export interface EditorViewOptions {
7
+ /** Whether the user can change the document (default true). Code can always change it. */
8
+ editable?: boolean;
9
+ /** Shown while the document is one empty block (default: none). */
10
+ placeholder?: string;
11
+ }
12
+ /**
13
+ * The editable view of an `Editor` from `@folijs/core`: renders its state into one
14
+ * `contenteditable` host and turns DOM input into core commands. The model is the source of
15
+ * truth; the DOM is rebuilt from it (unchanged blocks are reused by node identity).
16
+ *
17
+ * The browser edits the DOM itself only during composition (IME, dead keys) and for changes the
18
+ * view cannot prevent (Android keyboards, extensions). A MutationObserver records those; the view
19
+ * then reads the changed text back into a transaction (`readBack`) and re-renders.
20
+ *
21
+ * shortcut: assumes schema v1 (doc > textblocks > inline); nested blocks arrive with lists in Phase 7.
22
+ */
23
+ export declare class EditorView {
24
+ readonly host: HTMLElement;
25
+ readonly editor: Editor;
26
+ private state;
27
+ private blocks;
28
+ /** Position before each block, parallel to `blocks`. */
29
+ private starts;
30
+ /** The page of each fragment of each block. */
31
+ private fragmentPages;
32
+ /** The block each fragment element belongs to. */
33
+ private owner;
34
+ /** The page elements, in order; the host's only children. */
35
+ private pages;
36
+ private pageStyle;
37
+ /** Marks the host for this view's print rules (`printStyles`). */
38
+ private readonly id;
39
+ /**
40
+ * This view's print rules: in the document's head (`@page` only applies there) and, for a host
41
+ * in a shadow root, there too (page rules do not cross into it).
42
+ */
43
+ private printStyles;
44
+ private destroyed;
45
+ /** Set by a render during a composition, which places blocks but does not flow them. */
46
+ private unflowed;
47
+ /**
48
+ * Measurements by model node: a node's rendered height, margins and lines only depend on
49
+ * its content (unchanged node, same identity), the page width and the CSS. Cleared when the
50
+ * page settings or the fonts change; single entries go stale when a block's size changes
51
+ * without an edit (`ResizeObserver`: CSS, fonts, a container).
52
+ */
53
+ private measures;
54
+ private readonly stale;
55
+ /**
56
+ * Set when a block resized without an edit: the CSS or a font changed, and may have rewrapped
57
+ * other blocks without changing their height. The next render drops every cached line list
58
+ * (heights stay; lines are measured again only for blocks that cross a page end).
59
+ */
60
+ private linesStale;
61
+ /** The last flow and the nodes it was made for, for the next incremental one. */
62
+ private flowed;
63
+ private readonly resizeObserver;
64
+ private readonly observed;
65
+ private relayoutPending;
66
+ private composing;
67
+ /** Set by Ctrl/Cmd+Shift+V for the paste event it triggers. */
68
+ private plainPaste;
69
+ /** Blocks whose DOM the browser changed: the next render rebuilds them. */
70
+ private readonly dirty;
71
+ /** DOM mutations not yet read back (kept while a composition is running). */
72
+ private pending;
73
+ private readonly observer;
74
+ private readonly unsubscribe;
75
+ private editable;
76
+ private placeholder;
77
+ /** The block that shows the placeholder, if any. */
78
+ private placeholderDom;
79
+ /** Set by a mousedown that focuses the host: the click places the caret, not the view. */
80
+ private pointerFocus;
81
+ /** Whether the view added the host's tabindex (a read-only host must stay focusable). */
82
+ private readonly addedTabIndex;
83
+ constructor(host: HTMLElement, editor: Editor, options?: EditorViewOptions);
84
+ destroy(): void;
85
+ /** Focuses the host, shows the model selection in it and scrolls it into view. */
86
+ focus(): void;
87
+ /** Changes the given options; the others keep their value. */
88
+ setOptions(options: EditorViewOptions): void;
89
+ /**
90
+ * Marks the only block while the document is one empty textblock; CSS shows the text. Hidden
91
+ * during a composition, whose text the model does not have yet.
92
+ */
93
+ private showPlaceholder;
94
+ private update;
95
+ /**
96
+ * Scrolls the selection head into view. Browsers do it for their own edits and caret moves,
97
+ * not when the view changes the DOM and sets the selection (typing, Enter, undo, paste).
98
+ */
99
+ private scrollToSelection;
100
+ /**
101
+ * Renders the document into pages, reusing the DOM of blocks whose model node is unchanged.
102
+ * Blocks are first placed where they were (a new block joins the page of the block before
103
+ * it), then measured and flowed: a block that does not fit is split at a line boundary into
104
+ * fragments on consecutive pages. During a composition nothing moves, since moving or
105
+ * re-splitting the DOM the browser is composing in would break the IME.
106
+ *
107
+ * Measurements are cached by node and the flow restarts at the first changed block (5.4).
108
+ */
109
+ private render;
110
+ /**
111
+ * Puts each fragment of each block into its page (pages are non-decreasing in document
112
+ * order), creating and removing pages as needed. Anything else in the host or a page (stray
113
+ * DOM) is removed.
114
+ */
115
+ private place;
116
+ private createPage;
117
+ /** Page size and margins from the document settings, applied to every page when they change. */
118
+ private applyGeometry;
119
+ /** Forgets every measurement: the next flow measures the whole document again. */
120
+ private invalidateLayout;
121
+ /** Re-flows the pages once web fonts have loaded: line heights and wrapping change. */
122
+ private onFontsLoaded;
123
+ /** Observes the size of every rendered block element (and only those). */
124
+ private observeSizes;
125
+ /**
126
+ * A block whose height no longer matches its measurement changed without an edit (CSS, a web
127
+ * font, the content width): measure it again and re-flow, once per frame.
128
+ */
129
+ private onResize;
130
+ /** Re-flows in the next animation frame (several requests in one frame make one pass). */
131
+ private scheduleRelayout;
132
+ /** The block element that contains `node` (the child of a page it sits in), or null. */
133
+ private blockElement;
134
+ /** Model position of a DOM point inside the host, or null if it is outside. */
135
+ posFromDOM(node: Node, offset: number): number | null;
136
+ /** DOM point for a model position. At a mark boundary the caret stays in the text before it. */
137
+ domFromPos(pos: number): DomPoint;
138
+ /** The live DOM selection as a model selection, or null if it is not inside the host. */
139
+ private readSelection;
140
+ /**
141
+ * Brings the model selection up to date with the DOM. `selectionchange` is asynchronous, so
142
+ * every input handler calls this first instead of trusting the last event.
143
+ */
144
+ private syncSelection;
145
+ /**
146
+ * Whether a selection read from the DOM already shows the model selection. An AllSelection is
147
+ * shown as the text range between its snapped ends, so reading it back must not replace it.
148
+ */
149
+ private shows;
150
+ private hasFocus;
151
+ /** Writes the model selection to the DOM unless the DOM already shows it (keeps native affinity). */
152
+ private writeSelection;
153
+ private run;
154
+ /**
155
+ * Every change from user input goes through here. Read-only mode drops document changes and
156
+ * newly stored marks (Mod+B on a caret); selection changes, and clearing stored marks, still
157
+ * apply. Code can always change the document through the editor.
158
+ */
159
+ private dispatch;
160
+ /** Selects the event's target range when the browser gives a non-empty one (spellcheck, deletes). */
161
+ private selectTargetRange;
162
+ private onBeforeInput;
163
+ /**
164
+ * Triple-click selects the clicked block's content. Chromium and WebKit natively extend the
165
+ * selection to the start of the next block, so typing over it would merge the two blocks.
166
+ */
167
+ private onMouseDown;
168
+ private onKeyDown;
169
+ /**
170
+ * Caret affinity at a page break. The end of one fragment and the start of the next are two
171
+ * DOM caret stops for one model position, so a native Left/Right (or word move) there would
172
+ * seem to do nothing. Step over the edge first, then let the browser make the move.
173
+ *
174
+ * shortcut: left-to-right only (ArrowRight is forward); bidi text needs visual order here and in the line measurement.
175
+ */
176
+ private crossFragmentEdge;
177
+ /** Safety net: a DOM change that was not prevented is read back into the model. */
178
+ private onInput;
179
+ private onCompositionStart;
180
+ private onCompositionEnd;
181
+ /**
182
+ * Focus from the keyboard or from code shows the model selection: once the page selection has
183
+ * moved elsewhere, browsers put the caret at the start of the host instead. A DOM selection
184
+ * that already shows the model selection (the window or tab regaining focus) is left alone,
185
+ * and so is the scroll position.
186
+ */
187
+ private onFocus;
188
+ /** Some browsers drop compositionend when focus leaves mid-composition; never stay stuck in it. */
189
+ private onBlur;
190
+ private onMutations;
191
+ /** Reads back every DOM change recorded so far. */
192
+ private flush;
193
+ /**
194
+ * Turns DOM changes into a transaction: diffs the DOM text with the model text (blocks joined
195
+ * by a separator, hard breaks as one character) and replaces the minimal changed range. The
196
+ * DOM caret only breaks ties, so typing "a" after "a" lands where the browser put it. Blocks
197
+ * whose DOM was touched are re-rendered even if their text is unchanged.
198
+ *
199
+ * shortcut: diffs the whole document text (O(document) per composition); limit it to the
200
+ * touched blocks if it shows in profiles.
201
+ */
202
+ private readBack;
203
+ /** Rebuilds every block that is not reused and puts the selection back. */
204
+ private rerender;
205
+ /** Copy writes the model's content (plain text, HTML, Foli JSON), never the browser's DOM. */
206
+ private onCopy;
207
+ private onCut;
208
+ private onPaste;
209
+ /**
210
+ * Replaces the selection with clipboard content (Foli JSON, else HTML, else plain text). The
211
+ * paste starts a new undo step (typing right after it joins that step). Closed content (whole
212
+ * blocks) goes into an empty textblock whole, so a pasted heading stays a heading; open
213
+ * content, and anything pasted into a non-empty block, joins the text around the selection,
214
+ * so the target block keeps its type. Returns whether anything was inserted.
215
+ */
216
+ paste(data: Pick<DataTransfer, "getData">): boolean;
217
+ private onSelectionChange;
218
+ }
219
+ /**
220
+ * Normalizes a key event to a keymap name: modifiers in the order `Mod-Ctrl-Meta-Alt-Shift-`,
221
+ * then the key (single characters lower-cased). `Mod` is Cmd on Apple platforms, Ctrl elsewhere.
222
+ */
223
+ export declare function keyName(event: KeyboardEvent): string;
224
+ export {};
225
+ //# sourceMappingURL=view.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"view.d.ts","sourceRoot":"","sources":["../src/view.ts"],"names":[],"mappings":"AAAA,OAAO,EAQL,KAAK,MAAM,EAeZ,MAAM,cAAc,CAAC;AAatB,UAAU,QAAQ;IAChB,IAAI,EAAE,IAAI,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,iBAAiB;IAChC,0FAA0F;IAC1F,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,mEAAmE;IACnE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AA+CD;;;;;;;;;;GAUG;AACH,qBAAa,UAAU;IA4DnB,QAAQ,CAAC,IAAI,EAAE,WAAW;IAC1B,QAAQ,CAAC,MAAM,EAAE,MAAM;IA5DzB,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,MAAM,CAAmB;IACjC,wDAAwD;IACxD,OAAO,CAAC,MAAM,CAAgB;IAC9B,+C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