@folijs/view 0.1.0 → 0.2.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/clipboard.d.ts +9 -2
- package/dist/clipboard.d.ts.map +1 -1
- package/dist/clipboard.js +370 -51
- package/dist/clipboard.js.map +1 -1
- package/dist/render.d.ts +17 -4
- package/dist/render.d.ts.map +1 -1
- package/dist/render.js +104 -8
- package/dist/render.js.map +1 -1
- package/dist/view.d.ts +42 -6
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +490 -74
- package/dist/view.js.map +1 -1
- package/package.json +2 -2
- package/src/clipboard.ts +399 -60
- package/src/render.ts +109 -10
- package/src/view.ts +491 -69
package/src/render.ts
CHANGED
|
@@ -1,18 +1,29 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { Mark, Node as ModelNode, SimpleMarkName } from "@folijs/core";
|
|
2
2
|
|
|
3
|
-
// Every
|
|
3
|
+
// Every mark without attributes has a tag. Marks with attributes are rendered by `markElement`;
|
|
4
|
+
// marks of other schemas fall back to <span>.
|
|
4
5
|
const MARK_TAGS: ReadonlyMap<string, string> = new Map(
|
|
5
6
|
Object.entries({
|
|
6
7
|
bold: "strong",
|
|
7
8
|
italic: "em",
|
|
8
9
|
underline: "u",
|
|
9
10
|
strike: "s",
|
|
10
|
-
} satisfies Record<
|
|
11
|
+
} satisfies Record<SimpleMarkName, string>),
|
|
11
12
|
);
|
|
12
13
|
|
|
13
14
|
/** Marks the <br> of an explicit page break. */
|
|
14
15
|
export const PAGE_BREAK_ATTR = "data-page-break";
|
|
15
16
|
|
|
17
|
+
/** Marks a container element (list, list item) the view created around blocks. */
|
|
18
|
+
export const WRAPPER_ATTR = "data-foli-wrapper";
|
|
19
|
+
|
|
20
|
+
/** The element for a container node: lists and list items; anything else is a <div>. */
|
|
21
|
+
const WRAPPER_TAGS: ReadonlyMap<string, string> = new Map([
|
|
22
|
+
["bulletList", "ul"],
|
|
23
|
+
["orderedList", "ol"],
|
|
24
|
+
["listItem", "li"],
|
|
25
|
+
]);
|
|
26
|
+
|
|
16
27
|
/**
|
|
17
28
|
* A rendered inline leaf (a text node or hard break) and where it sits in its block's content.
|
|
18
29
|
* A text node split across fragments is two leaves, one per fragment.
|
|
@@ -36,20 +47,45 @@ export interface BlockDesc {
|
|
|
36
47
|
readonly leaves: readonly LeafDesc[];
|
|
37
48
|
}
|
|
38
49
|
|
|
50
|
+
/** The element for a mark: a tag, a link, or a span with a color. Values come validated. */
|
|
51
|
+
function markElement(mark: Mark, doc: Document): HTMLElement {
|
|
52
|
+
const { name } = mark.type;
|
|
53
|
+
if (name === "link") {
|
|
54
|
+
const a = doc.createElement("a");
|
|
55
|
+
// The schema only lets safe hrefs into a document (isSafeHref); clicks do not navigate.
|
|
56
|
+
a.setAttribute("href", String(mark.attrs.href));
|
|
57
|
+
return a;
|
|
58
|
+
}
|
|
59
|
+
const element = doc.createElement(MARK_TAGS.get(name) ?? "span");
|
|
60
|
+
if (name === "textColor") element.style.color = String(mark.attrs.color);
|
|
61
|
+
if (name === "highlight") element.style.backgroundColor = String(mark.attrs.color);
|
|
62
|
+
return element;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The element for a block: a heading or paragraph tag, or a rule. */
|
|
66
|
+
function blockTag(node: ModelNode): string {
|
|
67
|
+
if (node.type.name === "heading") return `h${String(node.attrs.level)}`;
|
|
68
|
+
if (node.type.name === "horizontalRule") return "hr";
|
|
69
|
+
return "p";
|
|
70
|
+
}
|
|
71
|
+
|
|
39
72
|
/**
|
|
40
|
-
* Renders one textblock
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
73
|
+
* Renders one leaf block (a textblock, or a block leaf such as a rule) with
|
|
74
|
+
* createElement/textContent only (never innerHTML), and records its leaves for position
|
|
75
|
+
* mapping. With `splits`, a textblock's content is rendered as several fragments of the same
|
|
76
|
+
* element type: each continued fragment has no bottom margin and each continuation no top
|
|
77
|
+
* margin, so together they take the same height as the whole block.
|
|
44
78
|
*/
|
|
45
79
|
export function renderBlock(
|
|
46
80
|
node: ModelNode,
|
|
47
81
|
doc: Document,
|
|
48
82
|
splits: readonly number[] = [],
|
|
49
83
|
): BlockDesc {
|
|
50
|
-
const tag =
|
|
84
|
+
const tag = blockTag(node);
|
|
85
|
+
const align = node.attrs.align;
|
|
51
86
|
const fragments = [0, ...splits].map((_, i, all) => {
|
|
52
87
|
const fragment = doc.createElement(tag);
|
|
88
|
+
if (typeof align === "string" && align !== "start") fragment.style.textAlign = align;
|
|
53
89
|
if (all.length > 1) {
|
|
54
90
|
fragment.dataset.fragment = String(i);
|
|
55
91
|
if (i > 0) fragment.style.marginTop = "0";
|
|
@@ -58,6 +94,9 @@ export function renderBlock(
|
|
|
58
94
|
return fragment;
|
|
59
95
|
});
|
|
60
96
|
const leaves: LeafDesc[] = [];
|
|
97
|
+
const dom = fragments[0] as HTMLElement;
|
|
98
|
+
// A block leaf (a rule) has no content: nothing to map, nothing to split.
|
|
99
|
+
if (!node.isTextblock) return { node, dom, fragments, splits, leaves };
|
|
61
100
|
/** The fragment that holds content offset `at` (a split offset starts the next fragment). */
|
|
62
101
|
const fragmentAt = (at: number) => {
|
|
63
102
|
let i = 0;
|
|
@@ -81,8 +120,9 @@ export function renderBlock(
|
|
|
81
120
|
const leaf = doc.createTextNode(text.slice(from - offset, to - offset));
|
|
82
121
|
leaves.push({ dom: leaf, offset: from, size: to - from });
|
|
83
122
|
let outer: Node = leaf;
|
|
123
|
+
// Marks are in canonical order, outermost first (a link wraps the formatting inside it).
|
|
84
124
|
for (const mark of [...child.marks].reverse()) {
|
|
85
|
-
const wrapper =
|
|
125
|
+
const wrapper = markElement(mark, doc);
|
|
86
126
|
wrapper.append(outer);
|
|
87
127
|
outer = wrapper;
|
|
88
128
|
}
|
|
@@ -93,6 +133,65 @@ export function renderBlock(
|
|
|
93
133
|
// An empty block, or one ending in a hard break, needs an extra <br> to show its last line.
|
|
94
134
|
if (!node.lastChild?.isText)
|
|
95
135
|
(fragments[fragments.length - 1] as HTMLElement).append(doc.createElement("br"));
|
|
96
|
-
|
|
136
|
+
// A fragment's last line is not the paragraph's last line: justified text stays justified up
|
|
137
|
+
// to the page break (CSS aligns a block's last line to the start). `text-align-last` also
|
|
138
|
+
// applies to every line before a forced break, which the paragraph shows start-aligned, so it
|
|
139
|
+
// is only set on fragments without one.
|
|
140
|
+
// shortcut: a fragment holding a hard break keeps its last line start-aligned at the page break; fixing that needs a per-line wrapper.
|
|
141
|
+
if (align === "justify")
|
|
142
|
+
for (const fragment of fragments.slice(0, -1))
|
|
143
|
+
if (!fragment.querySelector("br")) fragment.style.textAlignLast = "justify";
|
|
97
144
|
return { node, dom, fragments, splits, leaves };
|
|
98
145
|
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Creates the element for a container (list, list item). It takes no vertical space of its own
|
|
149
|
+
* (block margins and paddings 0): the page flow treats the blocks inside as siblings, whose
|
|
150
|
+
* margins collapse through it (ADR-0004).
|
|
151
|
+
*/
|
|
152
|
+
/** Every container element the view created (a set: checked for every block on every render). */
|
|
153
|
+
const wrappers = new WeakSet<Node>();
|
|
154
|
+
|
|
155
|
+
export function createWrapper(node: ModelNode, doc: Document): HTMLElement {
|
|
156
|
+
const element = doc.createElement(WRAPPER_TAGS.get(node.type.name) ?? "div");
|
|
157
|
+
element.setAttribute(WRAPPER_ATTR, "");
|
|
158
|
+
wrappers.add(element);
|
|
159
|
+
const { style } = element;
|
|
160
|
+
style.marginTop = style.marginBottom = style.paddingTop = style.paddingBottom = "0";
|
|
161
|
+
return element;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Whether `element` is the container element `createWrapper` makes for `node`. */
|
|
165
|
+
export function isWrapperFor(element: Node | null, node: ModelNode): element is HTMLElement {
|
|
166
|
+
return (
|
|
167
|
+
element !== null &&
|
|
168
|
+
wrappers.has(element) &&
|
|
169
|
+
(element as HTMLElement).localName === (WRAPPER_TAGS.get(node.type.name) ?? "div")
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Whether `node` is a container element the view created. */
|
|
174
|
+
export function isWrapper(node: Node | null): node is HTMLElement {
|
|
175
|
+
return node !== null && wrappers.has(node);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Renders a fragment of blocks as one DOM tree, for the clipboard: containers become their
|
|
180
|
+
* elements (with the list's start number), leaf blocks as in the editor, unsplit.
|
|
181
|
+
*/
|
|
182
|
+
export function renderTree(content: Iterable<ModelNode>, parent: Node, doc: Document): void {
|
|
183
|
+
for (const node of content) {
|
|
184
|
+
if (node.isTextblock || node.isLeaf) {
|
|
185
|
+
parent.appendChild(renderBlock(node, doc).dom);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
const element = createWrapper(node, doc);
|
|
189
|
+
wrappers.delete(element);
|
|
190
|
+
element.removeAttribute(WRAPPER_ATTR);
|
|
191
|
+
element.removeAttribute("style");
|
|
192
|
+
const start = node.attrs.start;
|
|
193
|
+
if (typeof start === "number" && start !== 1) element.setAttribute("start", String(start));
|
|
194
|
+
renderTree(node.content.content, element, doc);
|
|
195
|
+
parent.appendChild(element);
|
|
196
|
+
}
|
|
197
|
+
}
|