@folijs/view 0.4.0 → 0.5.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 +26 -0
- 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 +2 -0
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +1 -1
- package/dist/layout/flow.js.map +1 -1
- package/dist/view.d.ts +190 -4
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +1243 -95
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +70 -2
- package/dist/word/blocks.d.ts.map +1 -1
- package/dist/word/blocks.js +356 -5
- package/dist/word/blocks.js.map +1 -1
- package/dist/word/edit.d.ts +18 -9
- package/dist/word/edit.d.ts.map +1 -1
- package/dist/word/edit.js +67 -16
- package/dist/word/edit.js.map +1 -1
- package/dist/word/pages.d.ts +8 -0
- package/dist/word/pages.d.ts.map +1 -1
- package/dist/word/pages.js +47 -2
- package/dist/word/pages.js.map +1 -1
- 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/index.ts +8 -1
- package/src/layout/flow.ts +3 -1
- package/src/view.ts +1385 -95
- package/src/word/blocks.ts +404 -4
- package/src/word/edit.ts +85 -14
- package/src/word/pages.ts +53 -2
- package/src/word/table.ts +267 -0
package/src/word/edit.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
* opaque inline node a leaf element.
|
|
11
11
|
*/
|
|
12
12
|
import {
|
|
13
|
+
type CellFormat,
|
|
13
14
|
isSafeHref,
|
|
14
15
|
type ListMarker,
|
|
15
16
|
type Mark,
|
|
@@ -22,7 +23,12 @@ import type { BlockDesc, LeafDesc } from "../render.js";
|
|
|
22
23
|
import {
|
|
23
24
|
CHANGE_BAR,
|
|
24
25
|
changeElement,
|
|
26
|
+
type FloatGeometry,
|
|
27
|
+
floatElements,
|
|
28
|
+
floatGeometry,
|
|
25
29
|
fontStack,
|
|
30
|
+
type ImageSource,
|
|
31
|
+
imageSource,
|
|
26
32
|
latin,
|
|
27
33
|
lineHeight,
|
|
28
34
|
markerElement,
|
|
@@ -30,6 +36,7 @@ import {
|
|
|
30
36
|
opaqueText,
|
|
31
37
|
PX_PER_TWIP,
|
|
32
38
|
parseOpaque,
|
|
39
|
+
pictureElement,
|
|
33
40
|
type ReviewView,
|
|
34
41
|
styleParagraph,
|
|
35
42
|
styleRun,
|
|
@@ -69,6 +76,12 @@ export interface WordBlockDesc extends BlockDesc {
|
|
|
69
76
|
readonly key: string;
|
|
70
77
|
}
|
|
71
78
|
|
|
79
|
+
/**
|
|
80
|
+
* A floating picture's holder (drawn first in its paragraph, the view's own DOM) to its anchor:
|
|
81
|
+
* the empty, non-editable span at the picture's place in the text, which positions map to.
|
|
82
|
+
*/
|
|
83
|
+
export const FLOAT_ANCHORS = new WeakMap<Element, HTMLElement>();
|
|
84
|
+
|
|
72
85
|
/**
|
|
73
86
|
* Renders the blocks of one v3 document for the editable view. `prepare` runs before each
|
|
74
87
|
* render: list numbers and comment ranges depend on the blocks before.
|
|
@@ -81,6 +94,8 @@ export class WordRenderer {
|
|
|
81
94
|
readonly #clipboard: boolean;
|
|
82
95
|
/** How tracked changes show. */
|
|
83
96
|
readonly #review: ReviewView;
|
|
97
|
+
/** Where pictures' images are shown from. */
|
|
98
|
+
readonly #image: ImageSource | undefined;
|
|
84
99
|
#markers: (ListMarker | null)[] = [];
|
|
85
100
|
/** Comment ranges open at the start of each block. */
|
|
86
101
|
#comments: number[][] = [];
|
|
@@ -91,24 +106,29 @@ export class WordRenderer {
|
|
|
91
106
|
ratio: (family: string) => number,
|
|
92
107
|
clipboard = false,
|
|
93
108
|
review: ReviewView = "markup",
|
|
109
|
+
image?: ImageSource,
|
|
94
110
|
) {
|
|
95
111
|
this.#context = context;
|
|
96
112
|
this.#doc = doc;
|
|
97
113
|
this.#ratio = ratio;
|
|
98
114
|
this.#clipboard = clipboard;
|
|
99
115
|
this.#review = review;
|
|
116
|
+
this.#image = image ?? imageSource(context.envelope.resources);
|
|
100
117
|
}
|
|
101
118
|
|
|
102
119
|
get context(): WordContext {
|
|
103
120
|
return this.#context;
|
|
104
121
|
}
|
|
105
122
|
|
|
106
|
-
/**
|
|
107
|
-
|
|
123
|
+
/**
|
|
124
|
+
* Numbering and comment state for `blocks`, the blocks rendered, in document order (the
|
|
125
|
+
* paragraphs in table cells among them: Word numbers lists through tables).
|
|
126
|
+
*/
|
|
127
|
+
prepare(blocks: readonly ModelNode[]): void {
|
|
108
128
|
const attrs: Record<string, unknown>[] = [];
|
|
109
129
|
const comments: number[][] = [];
|
|
110
130
|
const open = new Set<number>();
|
|
111
|
-
|
|
131
|
+
for (const node of blocks) {
|
|
112
132
|
attrs.push(node.type.name === "paragraph" ? node.attrs : { numbering: { id: 0 } });
|
|
113
133
|
comments.push([...open]);
|
|
114
134
|
node.forEach((child) => {
|
|
@@ -116,25 +136,32 @@ export class WordRenderer {
|
|
|
116
136
|
if (child.type.name === "commentStart") open.add(id);
|
|
117
137
|
else if (child.type.name === "commentEnd") open.delete(id);
|
|
118
138
|
});
|
|
119
|
-
}
|
|
139
|
+
}
|
|
120
140
|
this.#markers = this.#context.resolver.markers(attrs);
|
|
121
141
|
this.#comments = comments;
|
|
122
142
|
}
|
|
123
143
|
|
|
124
|
-
/** Whether the
|
|
144
|
+
/** Whether the block at `index` (of those prepared) shows a list number. */
|
|
125
145
|
hasMarker(index: number): boolean {
|
|
126
146
|
return !!this.#markers[index];
|
|
127
147
|
}
|
|
128
148
|
|
|
129
|
-
/** What a block's rendering depends on at `index`, besides its node. */
|
|
130
|
-
key(index: number): string {
|
|
131
|
-
|
|
149
|
+
/** What a block's rendering depends on at `index`, besides its node (and its cell's format). */
|
|
150
|
+
key(index: number, cell?: CellFormat | null): string {
|
|
151
|
+
const inCell = cell ? `|${cell.style ?? ""}:${cell.conditions.join(",")}` : "";
|
|
152
|
+
return `${this.#markers[index]?.text ?? ""}|${(this.#comments[index] ?? []).join(" ")}${inCell}`;
|
|
132
153
|
}
|
|
133
154
|
|
|
134
|
-
|
|
155
|
+
/** A block; `cell` is the format of the table cell it is in, if any. */
|
|
156
|
+
render(
|
|
157
|
+
node: ModelNode,
|
|
158
|
+
index: number,
|
|
159
|
+
splits: readonly number[] = [],
|
|
160
|
+
cell?: CellFormat | null,
|
|
161
|
+
): WordBlockDesc {
|
|
135
162
|
if (node.type.name !== "paragraph") return this.#opaque(node, index);
|
|
136
163
|
const { resolver } = this.#context;
|
|
137
|
-
const { paragraph, run } = resolver.paragraph(node.attrs);
|
|
164
|
+
const { paragraph, run } = resolver.paragraph(node.attrs, cell ?? undefined);
|
|
138
165
|
const line = (r: RunFormat) => lineHeight(paragraph, r, this.#ratio(fontStack(latin(r))));
|
|
139
166
|
const doc = this.#doc;
|
|
140
167
|
const changed = this.#review !== "final" && hasChanges(node);
|
|
@@ -170,6 +197,9 @@ export class WordRenderer {
|
|
|
170
197
|
return fragments[i] as HTMLElement;
|
|
171
198
|
};
|
|
172
199
|
const leaves: LeafDesc[] = [];
|
|
200
|
+
// Floating pictures go first in the fragment their anchor is in, in order.
|
|
201
|
+
const floats = new Map<HTMLElement, HTMLElement[]>();
|
|
202
|
+
let geometry: FloatGeometry | undefined;
|
|
173
203
|
const comments = new Set(this.#comments[index] ?? []);
|
|
174
204
|
node.forEach((child, offset) => {
|
|
175
205
|
const name = child.type.name;
|
|
@@ -201,6 +231,23 @@ export class WordRenderer {
|
|
|
201
231
|
fragmentAt(offset).append(this.#wrap(span, child.marks, comments));
|
|
202
232
|
return;
|
|
203
233
|
}
|
|
234
|
+
case "picture": {
|
|
235
|
+
// Not editable inside (no browser resize handles); one position, like opaque content.
|
|
236
|
+
const span = doc.createElement("span");
|
|
237
|
+
span.setAttribute("contenteditable", "false");
|
|
238
|
+
if (child.attrs.float && !this.#clipboard) {
|
|
239
|
+
// Floating: drawn at the fragment's start (ADR-0006), its place in the text an
|
|
240
|
+
// empty anchor.
|
|
241
|
+
geometry ??= floatGeometry(this.#context.envelope.section);
|
|
242
|
+
const { nodes, holder } = floatElements(child.attrs, doc, this.#image, geometry);
|
|
243
|
+
for (const node of nodes) node.setAttribute("contenteditable", "false");
|
|
244
|
+
FLOAT_ANCHORS.set(holder, span);
|
|
245
|
+
const fragment = fragmentAt(offset);
|
|
246
|
+
floats.set(fragment, [...(floats.get(fragment) ?? []), ...nodes]);
|
|
247
|
+
} else span.append(pictureElement(child.attrs, doc, this.#image));
|
|
248
|
+
leafElement(span);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
204
251
|
case "opaqueInline": {
|
|
205
252
|
const span = this.#run(run, child.marks, line);
|
|
206
253
|
const xml = parseOpaque(String(child.attrs.xml ?? ""), doc);
|
|
@@ -232,12 +279,17 @@ export class WordRenderer {
|
|
|
232
279
|
from = to;
|
|
233
280
|
}
|
|
234
281
|
});
|
|
282
|
+
for (const [fragment, nodes] of floats) fragment.prepend(...nodes);
|
|
235
283
|
// An empty paragraph, or one ending in a line break, shows its last line; after a page
|
|
236
284
|
// break the paragraph mark stays on the break's line, as in Word.
|
|
237
285
|
let last: ModelNode | null = null;
|
|
238
286
|
for (let i = node.childCount - 1; i >= 0 && !last; i--) {
|
|
239
287
|
const child = node.child(i);
|
|
240
|
-
|
|
288
|
+
// A floating picture is not in the line: a paragraph holding only one shows its empty line.
|
|
289
|
+
if (
|
|
290
|
+
!["bookmarkStart", "bookmarkEnd", "commentStart", "commentEnd"].includes(child.type.name) &&
|
|
291
|
+
!(child.type.name === "picture" && child.attrs.float)
|
|
292
|
+
)
|
|
241
293
|
last = child;
|
|
242
294
|
}
|
|
243
295
|
if (!last || last.type.name === "hardBreak" || last.type.name === "columnBreak")
|
|
@@ -253,6 +305,13 @@ export class WordRenderer {
|
|
|
253
305
|
style: paragraph.style,
|
|
254
306
|
contextual: paragraph.contextualSpacing,
|
|
255
307
|
};
|
|
308
|
+
if (cell && !this.#clipboard) {
|
|
309
|
+
// In a cell its spacing is its margins (the page flow spaces top-level blocks); a
|
|
310
|
+
// fragment continued on the next page has none at the split.
|
|
311
|
+
// shortcut: contextual spacing between paragraphs in a cell is not applied.
|
|
312
|
+
dom.style.marginTop = `${word.before}px`;
|
|
313
|
+
(fragments[fragments.length - 1] as HTMLElement).style.marginBottom = `${word.after}px`;
|
|
314
|
+
}
|
|
256
315
|
if (this.#clipboard) {
|
|
257
316
|
// The flow places the spacing in the editor; elsewhere it is the paragraph's margins.
|
|
258
317
|
dom.style.marginTop = `${word.before}px`;
|
|
@@ -264,7 +323,7 @@ export class WordRenderer {
|
|
|
264
323
|
dom.setAttribute("style", `${styleOf(dom)}mso-list:${list}`);
|
|
265
324
|
}
|
|
266
325
|
}
|
|
267
|
-
return { node, dom, fragments, splits, leaves, word, key: this.key(index) };
|
|
326
|
+
return { node, dom, fragments, splits, leaves, word, key: this.key(index, cell) };
|
|
268
327
|
}
|
|
269
328
|
|
|
270
329
|
/** A span for text with `marks` over its paragraph's run formatting. */
|
|
@@ -313,7 +372,15 @@ export class WordRenderer {
|
|
|
313
372
|
|
|
314
373
|
/** An opaque block, shown from its XML: a block leaf, not editable inside. */
|
|
315
374
|
#opaque(node: ModelNode, index: number): WordBlockDesc {
|
|
316
|
-
const shown = opaqueBlock(
|
|
375
|
+
const shown = opaqueBlock(
|
|
376
|
+
node.toJSON(),
|
|
377
|
+
index,
|
|
378
|
+
this.#context.resolver,
|
|
379
|
+
this.#doc,
|
|
380
|
+
this.#ratio,
|
|
381
|
+
this.#image,
|
|
382
|
+
floatGeometry(this.#context.envelope.section),
|
|
383
|
+
);
|
|
317
384
|
const dom = shown.element;
|
|
318
385
|
dom.setAttribute("contenteditable", "false");
|
|
319
386
|
return {
|
|
@@ -356,7 +423,11 @@ export function clipboardBlocks(
|
|
|
356
423
|
ratio: (family: string) => number,
|
|
357
424
|
): HTMLElement[] {
|
|
358
425
|
const renderer = new WordRenderer(context, ownerDocument, ratio, true);
|
|
359
|
-
|
|
426
|
+
const top: ModelNode[] = [];
|
|
427
|
+
doc.forEach((node) => {
|
|
428
|
+
top.push(node);
|
|
429
|
+
});
|
|
430
|
+
renderer.prepare(top);
|
|
360
431
|
return nodes.map((node, i) => {
|
|
361
432
|
const { dom } = renderer.render(node, first < 0 ? -1 : first + i);
|
|
362
433
|
for (const element of [dom, ...dom.querySelectorAll("*")])
|
package/src/word/pages.ts
CHANGED
|
@@ -29,6 +29,11 @@ export interface DocumentPages {
|
|
|
29
29
|
* where the top-level node at `index` starts, as Word numbers lines; undefined if not shown.
|
|
30
30
|
*/
|
|
31
31
|
lineOf(index: number): number | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Every paragraph in table cells, in document order (nested tables' included; none for a cell
|
|
34
|
+
* a vertical merge covers, as Word lists none), with the page (0-based) it starts on.
|
|
35
|
+
*/
|
|
36
|
+
cellParagraphs(): { page: number; text: string }[];
|
|
32
37
|
/** Removes the pages. */
|
|
33
38
|
destroy(): void;
|
|
34
39
|
}
|
|
@@ -73,6 +78,8 @@ function createPage(doc: Document, g: Geometry): { page: HTMLElement; body: HTML
|
|
|
73
78
|
const { style } = page;
|
|
74
79
|
style.boxSizing = "border-box";
|
|
75
80
|
style.position = "relative";
|
|
81
|
+
// The stacking context pictures behind text are drawn in (over the page's background).
|
|
82
|
+
style.isolation = "isolate";
|
|
76
83
|
style.width = `${g.width}px`;
|
|
77
84
|
style.height = `${g.height}px`;
|
|
78
85
|
style.padding = `${g.top}px ${g.right}px ${g.bottom}px ${g.left}px`;
|
|
@@ -155,6 +162,23 @@ export function lineBottoms(element: HTMLElement): number[] {
|
|
|
155
162
|
return bottoms;
|
|
156
163
|
}
|
|
157
164
|
|
|
165
|
+
/**
|
|
166
|
+
* A table block's line ends without those inside a row that may not split (`cantSplit`), unless
|
|
167
|
+
* the row is taller than a page.
|
|
168
|
+
*/
|
|
169
|
+
function keepRows(element: HTMLElement, bottoms: number[], pageHeight: number): number[] {
|
|
170
|
+
const rows = element.querySelectorAll(":scope > table > tbody > tr[data-foli-cant-split]");
|
|
171
|
+
if (!rows.length) return bottoms;
|
|
172
|
+
const top = element.getBoundingClientRect().top;
|
|
173
|
+
const kept = [...rows]
|
|
174
|
+
.map((row) => {
|
|
175
|
+
const box = row.getBoundingClientRect();
|
|
176
|
+
return [box.top - top, box.bottom - top] as const;
|
|
177
|
+
})
|
|
178
|
+
.filter(([from, to]) => to - from <= pageHeight);
|
|
179
|
+
return bottoms.filter((y) => !kept.some(([from, to]) => y > from + 0.5 && y < to - 0.5));
|
|
180
|
+
}
|
|
181
|
+
|
|
158
182
|
/** The height of a table's leading header rows (repeated on each page it continues on), or 0. */
|
|
159
183
|
function headerHeight(element: HTMLElement): number {
|
|
160
184
|
let bottom = 0;
|
|
@@ -214,6 +238,7 @@ export function renderDocument(
|
|
|
214
238
|
const firstPage = new Map<number, number>();
|
|
215
239
|
/** Every fragment placed, in order: its page and how many lines it shows (measured lazily). */
|
|
216
240
|
const placements: { page: number; nodes: number[] | null; lines: () => number }[] = [];
|
|
241
|
+
const cellPages: { page: number; text: string }[] = [];
|
|
217
242
|
for (const group of groups) {
|
|
218
243
|
const g = group.geometry;
|
|
219
244
|
// Measure in a page of the section's width, out of view.
|
|
@@ -224,16 +249,30 @@ export function renderDocument(
|
|
|
224
249
|
container.append(measuring.page);
|
|
225
250
|
for (const block of group.blocks) measuring.body.append(block.element);
|
|
226
251
|
const lines = new Map<WordBlock, number[]>();
|
|
252
|
+
const pageHeight = g.height - g.top - g.bottom;
|
|
227
253
|
const linesOf = (block: WordBlock) => {
|
|
228
254
|
let value = lines.get(block);
|
|
229
255
|
if (!value) {
|
|
230
|
-
value = lineBottoms(block.element);
|
|
256
|
+
value = keepRows(block.element, lineBottoms(block.element), pageHeight);
|
|
231
257
|
lines.set(block, value);
|
|
232
258
|
}
|
|
233
259
|
return value;
|
|
234
260
|
};
|
|
261
|
+
// Each cell paragraph's top in its block, measured while the blocks are laid out whole.
|
|
262
|
+
const cellTops = new Map<WordBlock, { top: number; text: string }[]>();
|
|
263
|
+
for (const block of group.blocks) {
|
|
264
|
+
const cells = block.element.querySelectorAll("[data-foli-cell]");
|
|
265
|
+
if (!cells.length) continue;
|
|
266
|
+
const top = block.element.getBoundingClientRect().top;
|
|
267
|
+
cellTops.set(
|
|
268
|
+
block,
|
|
269
|
+
[...cells].map((cell) => ({
|
|
270
|
+
top: cell.getBoundingClientRect().top - top,
|
|
271
|
+
text: cell.textContent ?? "",
|
|
272
|
+
})),
|
|
273
|
+
);
|
|
274
|
+
}
|
|
235
275
|
const headers = new Map<WordBlock, number>();
|
|
236
|
-
const pageHeight = g.height - g.top - g.bottom;
|
|
237
276
|
const boxes: BlockBox[] = group.blocks.map((block, i) => {
|
|
238
277
|
const height = block.element.getBoundingClientRect().height;
|
|
239
278
|
const repeat = block.element.querySelector("tr") ? headerHeight(block.element) : 0;
|
|
@@ -248,6 +287,8 @@ export function renderDocument(
|
|
|
248
287
|
marginTop: block.before,
|
|
249
288
|
marginBottom: block.after,
|
|
250
289
|
repeat,
|
|
290
|
+
// A table splits at any line: no widow and orphan control across its rows.
|
|
291
|
+
anyLine: !!block.element.querySelector(":scope > table"),
|
|
251
292
|
keepWithNext: block.keepNext,
|
|
252
293
|
lines: () => (kept ? [height] : linesOf(block)),
|
|
253
294
|
breaksAfter: () => {
|
|
@@ -337,6 +378,15 @@ export function renderDocument(
|
|
|
337
378
|
: fragment.toLine) - fragment.fromLine,
|
|
338
379
|
});
|
|
339
380
|
});
|
|
381
|
+
// Its cell paragraphs: on the page of the fragment their top is in.
|
|
382
|
+
for (const cell of cellTops.get(block) ?? []) {
|
|
383
|
+
let at = placed[0]?.page ?? 0;
|
|
384
|
+
for (const fragment of placed) {
|
|
385
|
+
const start = fragment.fromLine ? (bottoms[fragment.fromLine - 1] ?? 0) : 0;
|
|
386
|
+
if (cell.top + 0.5 >= start) at = fragment.page;
|
|
387
|
+
}
|
|
388
|
+
cellPages.push({ page: pages.length + at, text: cell.text });
|
|
389
|
+
}
|
|
340
390
|
const first = placed[0];
|
|
341
391
|
if (first) for (const index of block.nodes) firstPage.set(index, pages.length + first.page);
|
|
342
392
|
});
|
|
@@ -365,6 +415,7 @@ export function renderDocument(
|
|
|
365
415
|
}
|
|
366
416
|
return firstLine.get(index);
|
|
367
417
|
},
|
|
418
|
+
cellParagraphs: () => cellPages,
|
|
368
419
|
destroy: () => {
|
|
369
420
|
for (const page of pages) page.remove();
|
|
370
421
|
},
|
|
@@ -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
|
+
}
|