@folijs/view 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -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/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +195 -20
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +790 -102
- 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 +407 -8
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +2956 -263
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +99 -2
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +535 -31
- 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 +32 -11
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +215 -33
- 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 +26 -13
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +439 -203
- 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/dist/word/table.d.ts +79 -0
- package/dist/word/table.d.ts.map +1 -0
- package/dist/word/table.js +210 -0
- package/dist/word/table.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +3 -0
- package/src/index.ts +8 -1
- package/src/layout/flow.ts +948 -111
- package/src/print.ts +48 -0
- package/src/view.ts +3291 -273
- package/src/word/blocks.ts +611 -28
- package/src/word/columns.ts +66 -0
- package/src/word/controls.ts +447 -0
- package/src/word/edit.ts +253 -30
- package/src/word/matches.ts +99 -0
- package/src/word/notes.ts +451 -0
- package/src/word/pages.ts +524 -192
- package/src/word/regions.ts +469 -0
- package/src/word/table.ts +267 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tables in the editable view (10.5, ADR-0006): the document's blocks as the view renders them
|
|
3
|
+
* (top-level paragraphs and opaque blocks, and every block inside table cells, in document
|
|
4
|
+
* order: each a unit with its own position, leaves and fragments), and each table's frame: the
|
|
5
|
+
* table, row and cell elements around its units, built from the model for the rows a page holds.
|
|
6
|
+
* Built with createElement only.
|
|
7
|
+
*/
|
|
8
|
+
import type { CellFormat, Node as ModelNode, Resolver } from "@folijs/core";
|
|
9
|
+
import { borderJsonCss, PX_PER_TWIP } from "./blocks.js";
|
|
10
|
+
|
|
11
|
+
/** A block the view renders: its node, the position before it, and the cell format it is in. */
|
|
12
|
+
export interface Unit {
|
|
13
|
+
readonly node: ModelNode;
|
|
14
|
+
readonly pos: number;
|
|
15
|
+
readonly cell: CellFormat | null;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** What a cell holds, in order: units (by index) and nested tables. */
|
|
19
|
+
export type CellItem = { unit: number } | { frame: Frame };
|
|
20
|
+
|
|
21
|
+
export interface FrameCell {
|
|
22
|
+
readonly node: ModelNode;
|
|
23
|
+
/** The first grid column it covers, and how many. */
|
|
24
|
+
readonly column: number;
|
|
25
|
+
readonly span: number;
|
|
26
|
+
readonly vMerge: "restart" | "continue" | null;
|
|
27
|
+
readonly format: CellFormat;
|
|
28
|
+
readonly items: readonly CellItem[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface FrameRow {
|
|
32
|
+
readonly node: ModelNode;
|
|
33
|
+
readonly header: boolean;
|
|
34
|
+
readonly cantSplit: boolean;
|
|
35
|
+
/** Twips; `exact` is drawn as at least (shortcut: cells never clip their content). */
|
|
36
|
+
readonly height: number | null;
|
|
37
|
+
readonly cells: readonly FrameCell[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** A table as the view draws it. */
|
|
41
|
+
export interface Frame {
|
|
42
|
+
readonly node: ModelNode;
|
|
43
|
+
readonly pos: number;
|
|
44
|
+
readonly rows: readonly FrameRow[];
|
|
45
|
+
/** Twips per grid column. */
|
|
46
|
+
readonly grid: readonly number[];
|
|
47
|
+
/** The leading header rows (repeated on each page the table continues on). */
|
|
48
|
+
readonly headers: number;
|
|
49
|
+
readonly css: { marginLeft: string; justify: string | null };
|
|
50
|
+
/** The units inside it, nested tables' included: `[first, end)`. */
|
|
51
|
+
readonly first: number;
|
|
52
|
+
readonly end: number;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A top-level item of the page flow: a unit (paragraph, opaque block) or a table. */
|
|
56
|
+
export type FlowItem = { unit: number; frame?: undefined } | { frame: Frame; unit?: undefined };
|
|
57
|
+
|
|
58
|
+
/** The units and flow items of `doc`, in document order. */
|
|
59
|
+
export function collect(doc: ModelNode, resolver: Resolver): { units: Unit[]; items: FlowItem[] } {
|
|
60
|
+
const units: Unit[] = [];
|
|
61
|
+
const items: FlowItem[] = [];
|
|
62
|
+
doc.forEach((node, offset) => {
|
|
63
|
+
if (node.type.name === "table") items.push({ frame: frameOf(node, offset, resolver, units) });
|
|
64
|
+
else {
|
|
65
|
+
items.push({ unit: units.length });
|
|
66
|
+
units.push({ node, pos: offset, cell: null });
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
return { units, items };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function frameOf(table: ModelNode, pos: number, resolver: Resolver, units: Unit[]): Frame {
|
|
73
|
+
const first = units.length;
|
|
74
|
+
const attrs = table.attrs as Record<string, unknown>;
|
|
75
|
+
const format = resolver.table(attrs);
|
|
76
|
+
const rows: FrameRow[] = [];
|
|
77
|
+
let rowPos = pos + 1;
|
|
78
|
+
table.forEach((row, _r, r) => {
|
|
79
|
+
const rowAttrs = row.attrs as Record<string, unknown>;
|
|
80
|
+
let column = (rowAttrs.gridBefore as number | null) ?? 0;
|
|
81
|
+
const cells: FrameCell[] = [];
|
|
82
|
+
let cellPos = rowPos + 1;
|
|
83
|
+
row.forEach((cell, _c, c) => {
|
|
84
|
+
const cellAttrs = cell.attrs as Record<string, unknown>;
|
|
85
|
+
const cellFormat = resolver.cell(attrs, rowAttrs, cellAttrs, {
|
|
86
|
+
row: r,
|
|
87
|
+
rows: table.childCount,
|
|
88
|
+
cell: c,
|
|
89
|
+
cells: row.childCount,
|
|
90
|
+
});
|
|
91
|
+
const items: CellItem[] = [];
|
|
92
|
+
cell.forEach((block, offset) => {
|
|
93
|
+
const at = cellPos + 1 + offset;
|
|
94
|
+
if (block.type.name === "table") items.push({ frame: frameOf(block, at, resolver, units) });
|
|
95
|
+
else {
|
|
96
|
+
items.push({ unit: units.length });
|
|
97
|
+
units.push({ node: block, pos: at, cell: cellFormat });
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
const span = (cellAttrs.gridSpan as number | null) ?? 1;
|
|
101
|
+
cells.push({
|
|
102
|
+
node: cell,
|
|
103
|
+
column,
|
|
104
|
+
span,
|
|
105
|
+
vMerge: (cellAttrs.vMerge as FrameCell["vMerge"]) ?? null,
|
|
106
|
+
format: cellFormat,
|
|
107
|
+
items,
|
|
108
|
+
});
|
|
109
|
+
column += span;
|
|
110
|
+
cellPos += cell.nodeSize;
|
|
111
|
+
});
|
|
112
|
+
const height = rowAttrs.height as { value: number } | null;
|
|
113
|
+
rows.push({
|
|
114
|
+
node: row,
|
|
115
|
+
header: !!rowAttrs.header,
|
|
116
|
+
cantSplit: !!rowAttrs.cantSplit,
|
|
117
|
+
height: height?.value ?? null,
|
|
118
|
+
cells,
|
|
119
|
+
});
|
|
120
|
+
rowPos += row.nodeSize;
|
|
121
|
+
});
|
|
122
|
+
let headers = 0;
|
|
123
|
+
while (headers < rows.length && rows[headers]?.header) headers++;
|
|
124
|
+
// Word repeats header rows only while some body row follows them.
|
|
125
|
+
if (headers === rows.length) headers = 0;
|
|
126
|
+
const indent = format.indent?.type === "dxa" ? format.indent.value : 0;
|
|
127
|
+
return {
|
|
128
|
+
node: table,
|
|
129
|
+
pos,
|
|
130
|
+
rows,
|
|
131
|
+
grid: (attrs.grid as number[] | undefined) ?? [],
|
|
132
|
+
headers,
|
|
133
|
+
css: {
|
|
134
|
+
marginLeft: `${indent * PX_PER_TWIP}px`,
|
|
135
|
+
justify:
|
|
136
|
+
format.justification === "center" || format.justification === "right"
|
|
137
|
+
? format.justification
|
|
138
|
+
: null,
|
|
139
|
+
},
|
|
140
|
+
first,
|
|
141
|
+
end: units.length,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Marks a table's elements (wrapper, table, rows, cells): they hold blocks, not content. */
|
|
146
|
+
export const FRAME_ATTR = "data-foli-frame";
|
|
147
|
+
/** Marks a repeated header row: shown, not document content. */
|
|
148
|
+
export const REPEATED_ATTR = "data-foli-repeated";
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* The wrapper and table for `rows` of `frame` (indexes, in order), with `headers` (indexes of
|
|
152
|
+
* header rows drawn again above them, not editable). `content(row, cell)` gives what a cell
|
|
153
|
+
* shows on this page; a cell a vertical merge covers is drawn by the merge's first cell when
|
|
154
|
+
* that is on the page (spanning the rows it covers here), else as an empty cell of its own.
|
|
155
|
+
*/
|
|
156
|
+
export function frameElement(
|
|
157
|
+
frame: Frame,
|
|
158
|
+
rows: readonly number[],
|
|
159
|
+
headers: readonly number[],
|
|
160
|
+
content: (row: number, cell: number) => readonly Node[],
|
|
161
|
+
doc: Document,
|
|
162
|
+
): HTMLElement {
|
|
163
|
+
const wrapper = doc.createElement("div");
|
|
164
|
+
wrapper.setAttribute(FRAME_ATTR, "table");
|
|
165
|
+
wrapper.style.display = "flow-root";
|
|
166
|
+
const table = doc.createElement("table");
|
|
167
|
+
table.setAttribute(FRAME_ATTR, "");
|
|
168
|
+
const { style } = table;
|
|
169
|
+
style.borderCollapse = "collapse";
|
|
170
|
+
// shortcut: autofit tables keep the grid Word saved; widths are not recomputed from content.
|
|
171
|
+
style.tableLayout = "fixed";
|
|
172
|
+
const total = frame.grid.reduce((a, b) => a + b, 0);
|
|
173
|
+
if (total) style.width = `${total * PX_PER_TWIP}px`;
|
|
174
|
+
if (frame.css.justify === "center") style.margin = "0 auto";
|
|
175
|
+
else if (frame.css.justify === "right") style.marginLeft = "auto";
|
|
176
|
+
else style.marginLeft = frame.css.marginLeft;
|
|
177
|
+
const colgroup = doc.createElement("colgroup");
|
|
178
|
+
for (const width of frame.grid) {
|
|
179
|
+
const col = doc.createElement("col");
|
|
180
|
+
col.style.width = `${width * PX_PER_TWIP}px`;
|
|
181
|
+
colgroup.append(col);
|
|
182
|
+
}
|
|
183
|
+
const body = doc.createElement("tbody");
|
|
184
|
+
body.setAttribute(FRAME_ATTR, "");
|
|
185
|
+
table.append(colgroup, body);
|
|
186
|
+
for (const r of headers) {
|
|
187
|
+
const repeated = rowElement(frame, r, [r], () => [], doc);
|
|
188
|
+
repeated.setAttribute(REPEATED_ATTR, "");
|
|
189
|
+
repeated.setAttribute("contenteditable", "false");
|
|
190
|
+
repeated.setAttribute("aria-hidden", "true");
|
|
191
|
+
// The header's text, drawn again: copied from the model's cells, not leaves.
|
|
192
|
+
const source = frame.rows[r] as FrameRow;
|
|
193
|
+
[...repeated.children].forEach((td, c) => {
|
|
194
|
+
td.textContent = source.cells[c]?.node.textContent ?? "";
|
|
195
|
+
});
|
|
196
|
+
body.append(repeated);
|
|
197
|
+
}
|
|
198
|
+
for (const r of rows) body.append(rowElement(frame, r, rows, content, doc));
|
|
199
|
+
wrapper.append(table);
|
|
200
|
+
return wrapper;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function rowElement(
|
|
204
|
+
frame: Frame,
|
|
205
|
+
r: number,
|
|
206
|
+
shown: readonly number[],
|
|
207
|
+
content: (row: number, cell: number) => readonly Node[],
|
|
208
|
+
doc: Document,
|
|
209
|
+
): HTMLElement {
|
|
210
|
+
const row = frame.rows[r] as FrameRow;
|
|
211
|
+
const tr = doc.createElement("tr");
|
|
212
|
+
tr.setAttribute(FRAME_ATTR, "");
|
|
213
|
+
if (row.header) tr.dataset.foliHeader = "";
|
|
214
|
+
if (row.height) tr.style.height = `${row.height * PX_PER_TWIP}px`;
|
|
215
|
+
row.cells.forEach((cell, c) => {
|
|
216
|
+
// Covered by a merge whose first cell is drawn on this page: not shown. Its blocks (the
|
|
217
|
+
// empty paragraph Word keeps) stay in document order, in a cell the table does not lay out.
|
|
218
|
+
if (cell.vMerge === "continue" && mergeStartShown(frame, r, cell.column, shown)) {
|
|
219
|
+
const td = doc.createElement("td");
|
|
220
|
+
td.setAttribute(FRAME_ATTR, "covered");
|
|
221
|
+
td.style.display = "none";
|
|
222
|
+
td.append(...content(r, c));
|
|
223
|
+
tr.append(td);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const td = doc.createElement("td");
|
|
227
|
+
td.setAttribute(FRAME_ATTR, "");
|
|
228
|
+
if (cell.span > 1) td.colSpan = cell.span;
|
|
229
|
+
const rowSpan = covered(frame, r, cell.column, shown);
|
|
230
|
+
if (rowSpan > 1) td.rowSpan = rowSpan;
|
|
231
|
+
const { format } = cell;
|
|
232
|
+
const s = td.style;
|
|
233
|
+
if (format.shading) s.backgroundColor = `#${format.shading}`;
|
|
234
|
+
const m = format.margins;
|
|
235
|
+
s.padding = `${m.top * PX_PER_TWIP}px ${m.right * PX_PER_TWIP}px ${m.bottom * PX_PER_TWIP}px ${m.left * PX_PER_TWIP}px`;
|
|
236
|
+
s.verticalAlign = format.verticalAlign === "center" ? "middle" : format.verticalAlign;
|
|
237
|
+
for (const side of ["top", "left", "bottom", "right"] as const) {
|
|
238
|
+
const css = borderJsonCss(format.borders[side]);
|
|
239
|
+
if (css) s.setProperty(`border-${side}`, css);
|
|
240
|
+
}
|
|
241
|
+
td.append(...content(r, c));
|
|
242
|
+
tr.append(td);
|
|
243
|
+
});
|
|
244
|
+
return tr;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Whether a covered cell at (`r`, `column`) is drawn by a cell above it here: the row above is
|
|
249
|
+
* shown and its cell there is part of the merge (its first cell, or one drawn for it because
|
|
250
|
+
* the first is on an earlier page).
|
|
251
|
+
*/
|
|
252
|
+
function mergeStartShown(frame: Frame, r: number, column: number, shown: readonly number[]) {
|
|
253
|
+
if (!shown.includes(r - 1)) return false;
|
|
254
|
+
const above = frame.rows[r - 1]?.cells.find((x) => x.column === column);
|
|
255
|
+
return above?.vMerge === "restart" || above?.vMerge === "continue";
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/** How many shown rows from `r` a cell at `column` covers (itself and the merge below it). */
|
|
259
|
+
function covered(frame: Frame, r: number, column: number, shown: readonly number[]): number {
|
|
260
|
+
let n = 1;
|
|
261
|
+
for (let below = r + 1; shown.includes(below); below++) {
|
|
262
|
+
const cell = frame.rows[below]?.cells.find((x) => x.column === column);
|
|
263
|
+
if (cell?.vMerge !== "continue") break;
|
|
264
|
+
n++;
|
|
265
|
+
}
|
|
266
|
+
return n;
|
|
267
|
+
}
|