@folijs/view 0.1.0 → 0.3.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 +54 -0
- package/README.md +4 -3
- package/dist/clipboard.d.ts +37 -11
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +413 -66
- package/dist/clipboard.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/layout/flow.d.ts +18 -1
- package/dist/layout/flow.d.ts.map +1 -1
- package/dist/layout/flow.js +20 -11
- package/dist/layout/flow.js.map +1 -1
- package/dist/render.d.ts +0 -7
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +0 -67
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +67 -10
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +605 -114
- package/dist/view.js.map +1 -1
- package/dist/word/blocks.d.ts +72 -0
- package/dist/word/blocks.d.ts.map +1 -0
- package/dist/word/blocks.js +718 -0
- package/dist/word/blocks.js.map +1 -0
- package/dist/word/edit.d.ts +61 -0
- package/dist/word/edit.d.ts.map +1 -0
- package/dist/word/edit.js +296 -0
- package/dist/word/edit.js.map +1 -0
- package/dist/word/pages.d.ts +45 -0
- package/dist/word/pages.d.ts.map +1 -0
- package/dist/word/pages.js +335 -0
- package/dist/word/pages.js.map +1 -0
- package/package.json +2 -2
- package/src/clipboard.ts +465 -76
- package/src/index.ts +6 -0
- package/src/layout/flow.ts +36 -13
- package/src/render.ts +1 -72
- package/src/view.ts +648 -113
- package/src/word/blocks.ts +777 -0
- package/src/word/edit.ts +360 -0
- package/src/word/pages.ts +372 -0
package/src/index.ts
CHANGED
package/src/layout/flow.ts
CHANGED
|
@@ -18,6 +18,14 @@ export interface BlockBox {
|
|
|
18
18
|
keepWithNext?: boolean;
|
|
19
19
|
/** The lines that end with an explicit page break (indices into `lines()`), if any. */
|
|
20
20
|
breaksAfter?(): readonly number[];
|
|
21
|
+
/** Height shown again at the top of each fragment after the first (a table's header rows). */
|
|
22
|
+
repeat?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Content height of the pages this block goes on, when not the flow's (a section's page). A
|
|
25
|
+
* block with another page height than the one before it must start a page (an explicit
|
|
26
|
+
* break before it): a page has one height.
|
|
27
|
+
*/
|
|
28
|
+
pageHeight?: number;
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
/** The lines `[fromLine, toLine)` of a block, placed on one page (`toLine` Infinity: to the end). */
|
|
@@ -32,6 +40,13 @@ export interface FlowRules {
|
|
|
32
40
|
orphans: number;
|
|
33
41
|
/** Fewest lines of a split block carried to the top of the next page. */
|
|
34
42
|
widows: number;
|
|
43
|
+
/**
|
|
44
|
+
* How margins (paragraph spacing) combine. `css` (the default): as CSS in a padded page.
|
|
45
|
+
* `word`: as Word spaces paragraphs. The space after one and the space before the next add up.
|
|
46
|
+
* A page that a block flowed onto (not one an explicit break started, nor the first) drops
|
|
47
|
+
* the space before its first block. The space after the last block on a page need not fit.
|
|
48
|
+
*/
|
|
49
|
+
spacing?: "css" | "word";
|
|
35
50
|
}
|
|
36
51
|
|
|
37
52
|
export const DEFAULT_RULES: FlowRules = { orphans: 2, widows: 2 };
|
|
@@ -52,6 +67,8 @@ export interface FlowState {
|
|
|
52
67
|
onPage: number;
|
|
53
68
|
/** An explicit page break ended the previous block: the next one starts a new page. */
|
|
54
69
|
breakPending: boolean;
|
|
70
|
+
/** The page is the first or an explicit break started it (Word keeps its first space before). */
|
|
71
|
+
forcedPage: boolean;
|
|
55
72
|
}
|
|
56
73
|
|
|
57
74
|
/**
|
|
@@ -111,7 +128,7 @@ export interface Reuse {
|
|
|
111
128
|
*/
|
|
112
129
|
export function flowFrom(
|
|
113
130
|
boxes: readonly BlockBox[],
|
|
114
|
-
|
|
131
|
+
flowHeight: number,
|
|
115
132
|
rules: FlowRules = DEFAULT_RULES,
|
|
116
133
|
reuse?: Reuse,
|
|
117
134
|
): FlowResult {
|
|
@@ -122,27 +139,32 @@ export function flowFrom(
|
|
|
122
139
|
let state: FlowState = (
|
|
123
140
|
reuse && reuse.previous.states[start]
|
|
124
141
|
? { ...reuse.previous.states[start] }
|
|
125
|
-
: { page: 0, used: 0, previousMargin: 0, onPage: 0, breakPending: false }
|
|
142
|
+
: { page: 0, used: 0, previousMargin: 0, onPage: 0, breakPending: false, forcedPage: true }
|
|
126
143
|
) as FlowState;
|
|
127
144
|
/** Blocks that start a page because the keep-with-next rule moved them. */
|
|
128
145
|
const breakBefore = new Set<number>();
|
|
129
|
-
const
|
|
146
|
+
const word = rules.spacing === "word";
|
|
147
|
+
const newPage = (forced = false) => {
|
|
130
148
|
state.page++;
|
|
131
149
|
state.used = 0;
|
|
132
150
|
state.onPage = 0;
|
|
151
|
+
state.forcedPage = forced;
|
|
133
152
|
};
|
|
134
153
|
|
|
135
154
|
const place = (box: BlockBox, index: number): Fragment[] => {
|
|
136
|
-
|
|
155
|
+
const pageHeight = box.pageHeight ?? flowHeight;
|
|
156
|
+
if ((state.breakPending || breakBefore.has(index)) && state.onPage) newPage(state.breakPending);
|
|
137
157
|
state.breakPending = false;
|
|
138
158
|
const above = state.previousMargin;
|
|
139
159
|
state.previousMargin = box.marginBottom;
|
|
140
|
-
const gap = () =>
|
|
160
|
+
const gap = () => {
|
|
161
|
+
if (!state.onPage) return word && !state.forcedPage ? 0 : box.marginTop;
|
|
162
|
+
return word ? above + box.marginTop : Math.max(above, box.marginTop);
|
|
163
|
+
};
|
|
164
|
+
// The bottom margin that must fit on the page (Word lets the space after hang).
|
|
165
|
+
const below = word ? 0 : box.marginBottom;
|
|
141
166
|
const forced = box.breaksAfter?.() ?? [];
|
|
142
|
-
if (
|
|
143
|
-
!forced.length &&
|
|
144
|
-
state.used + gap() + box.height + box.marginBottom <= pageHeight + SLACK
|
|
145
|
-
) {
|
|
167
|
+
if (!forced.length && state.used + gap() + box.height + below <= pageHeight + SLACK) {
|
|
146
168
|
state.used += gap() + box.height;
|
|
147
169
|
state.onPage++;
|
|
148
170
|
return [{ page: state.page, fromLine: 0, toLine: Infinity }];
|
|
@@ -158,11 +180,11 @@ export function flowFrom(
|
|
|
158
180
|
while (segmentEnd < count && !breaks.has(segmentEnd)) segmentEnd++;
|
|
159
181
|
segmentEnd = Math.min(segmentEnd + 1, count);
|
|
160
182
|
const top = line ? (lines[line - 1] as number) : 0;
|
|
161
|
-
// Only the block's first fragment has the gap above it.
|
|
162
|
-
const spacing = line === 0 ? gap() : 0;
|
|
183
|
+
// Only the block's first fragment has the gap above it; the others repeat its header.
|
|
184
|
+
const spacing = line === 0 ? gap() : (box.repeat ?? 0);
|
|
163
185
|
let end = line;
|
|
164
186
|
while (end < segmentEnd) {
|
|
165
|
-
const bottom = (lines[end] as number) - top + (end + 1 === count ?
|
|
187
|
+
const bottom = (lines[end] as number) - top + (end + 1 === count ? below : 0);
|
|
166
188
|
if (state.used + spacing + bottom > pageHeight + SLACK) break;
|
|
167
189
|
end++;
|
|
168
190
|
}
|
|
@@ -276,6 +298,7 @@ function sameState(a: FlowState, b: FlowState): boolean {
|
|
|
276
298
|
a.used === b.used &&
|
|
277
299
|
a.previousMargin === b.previousMargin &&
|
|
278
300
|
a.onPage === b.onPage &&
|
|
279
|
-
a.breakPending === b.breakPending
|
|
301
|
+
a.breakPending === b.breakPending &&
|
|
302
|
+
a.forcedPage === b.forcedPage
|
|
280
303
|
);
|
|
281
304
|
}
|
package/src/render.ts
CHANGED
|
@@ -1,14 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
// Every schema v1 mark must have a tag; marks of other schemas fall back to <span>.
|
|
4
|
-
const MARK_TAGS: ReadonlyMap<string, string> = new Map(
|
|
5
|
-
Object.entries({
|
|
6
|
-
bold: "strong",
|
|
7
|
-
italic: "em",
|
|
8
|
-
underline: "u",
|
|
9
|
-
strike: "s",
|
|
10
|
-
} satisfies Record<MarkName, string>),
|
|
11
|
-
);
|
|
1
|
+
import type { Node as ModelNode } from "@folijs/core";
|
|
12
2
|
|
|
13
3
|
/** Marks the <br> of an explicit page break. */
|
|
14
4
|
export const PAGE_BREAK_ATTR = "data-page-break";
|
|
@@ -35,64 +25,3 @@ export interface BlockDesc {
|
|
|
35
25
|
readonly splits: readonly number[];
|
|
36
26
|
readonly leaves: readonly LeafDesc[];
|
|
37
27
|
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Renders one textblock with createElement/textContent only (never innerHTML), and records its
|
|
41
|
-
* leaves for position mapping. With `splits`, the content is rendered as several fragments of
|
|
42
|
-
* the same element type: each continued fragment has no bottom margin and each continuation no
|
|
43
|
-
* top margin, so together they take the same height as the whole block.
|
|
44
|
-
*/
|
|
45
|
-
export function renderBlock(
|
|
46
|
-
node: ModelNode,
|
|
47
|
-
doc: Document,
|
|
48
|
-
splits: readonly number[] = [],
|
|
49
|
-
): BlockDesc {
|
|
50
|
-
const tag = node.type.name === "heading" ? `h${String(node.attrs.level)}` : "p";
|
|
51
|
-
const fragments = [0, ...splits].map((_, i, all) => {
|
|
52
|
-
const fragment = doc.createElement(tag);
|
|
53
|
-
if (all.length > 1) {
|
|
54
|
-
fragment.dataset.fragment = String(i);
|
|
55
|
-
if (i > 0) fragment.style.marginTop = "0";
|
|
56
|
-
if (i < all.length - 1) fragment.style.marginBottom = "0";
|
|
57
|
-
}
|
|
58
|
-
return fragment;
|
|
59
|
-
});
|
|
60
|
-
const leaves: LeafDesc[] = [];
|
|
61
|
-
/** The fragment that holds content offset `at` (a split offset starts the next fragment). */
|
|
62
|
-
const fragmentAt = (at: number) => {
|
|
63
|
-
let i = 0;
|
|
64
|
-
while (i < splits.length && (splits[i] as number) <= at) i++;
|
|
65
|
-
return fragments[i] as HTMLElement;
|
|
66
|
-
};
|
|
67
|
-
node.forEach((child, offset) => {
|
|
68
|
-
if (!child.isText) {
|
|
69
|
-
// A hard break or a page break: both end their line (the flow ends the page at a page break).
|
|
70
|
-
const br = doc.createElement("br");
|
|
71
|
-
if (child.type.name === "pageBreak") br.setAttribute(PAGE_BREAK_ATTR, "");
|
|
72
|
-
leaves.push({ dom: br, offset, size: child.nodeSize });
|
|
73
|
-
// Text only: a mark wrapper around <br> would add nothing visible.
|
|
74
|
-
fragmentAt(offset).append(br);
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
const text = child.text ?? "";
|
|
78
|
-
const cuts = splits.filter((split) => split > offset && split < offset + text.length);
|
|
79
|
-
let from = offset;
|
|
80
|
-
for (const to of [...cuts, offset + text.length]) {
|
|
81
|
-
const leaf = doc.createTextNode(text.slice(from - offset, to - offset));
|
|
82
|
-
leaves.push({ dom: leaf, offset: from, size: to - from });
|
|
83
|
-
let outer: Node = leaf;
|
|
84
|
-
for (const mark of [...child.marks].reverse()) {
|
|
85
|
-
const wrapper = doc.createElement(MARK_TAGS.get(mark.type.name) ?? "span");
|
|
86
|
-
wrapper.append(outer);
|
|
87
|
-
outer = wrapper;
|
|
88
|
-
}
|
|
89
|
-
fragmentAt(from).append(outer);
|
|
90
|
-
from = to;
|
|
91
|
-
}
|
|
92
|
-
});
|
|
93
|
-
// An empty block, or one ending in a hard break, needs an extra <br> to show its last line.
|
|
94
|
-
if (!node.lastChild?.isText)
|
|
95
|
-
(fragments[fragments.length - 1] as HTMLElement).append(doc.createElement("br"));
|
|
96
|
-
const dom = fragments[0] as HTMLElement;
|
|
97
|
-
return { node, dom, fragments, splits, leaves };
|
|
98
|
-
}
|