@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/dist/clipboard.js
CHANGED
|
@@ -1,41 +1,74 @@
|
|
|
1
|
-
import { Fragment,
|
|
2
|
-
import { PAGE_BREAK_ATTR
|
|
1
|
+
import { copiedDefinitions, Fragment, isSafeHref, } from "@folijs/core";
|
|
2
|
+
import { PAGE_BREAK_ATTR } from "./render.js";
|
|
3
|
+
import { opaqueParagraphsText, parseOpaque } from "./word/blocks.js";
|
|
4
|
+
import { clipboardBlocks } from "./word/edit.js";
|
|
3
5
|
/**
|
|
4
|
-
* Clipboard type for Foli content, read back first on paste: `{ open, content }`, the
|
|
5
|
-
* blocks as `DocJSON
|
|
6
|
+
* Clipboard type for Foli content, read back first on paste: `{ open, content, word? }`, the
|
|
7
|
+
* copied blocks as `DocJSON`, whether they were cut out of one block (see `Pasted.open`), and
|
|
8
|
+
* from a schema v3 document the definitions they reference (`copiedDefinitions`).
|
|
6
9
|
*/
|
|
7
10
|
export const FOLI_MIME = "application/x-foli+json";
|
|
8
|
-
/**
|
|
9
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Serializes `from..to` of `doc` for the clipboard. Hard breaks and block ends become "\n". The
|
|
13
|
+
* HTML is the document as its Word renderer draws it (`word`: its context and the fonts' line
|
|
14
|
+
* height ratios): resolved formatting as inline CSS, list numbers as Word's HTML has them.
|
|
15
|
+
*/
|
|
16
|
+
export function serializeRange(doc, from, to, ownerDocument, word) {
|
|
10
17
|
// A slice inside one textblock holds only inline content: wrap it back in its ancestors.
|
|
11
18
|
const $from = doc.resolve(from);
|
|
12
19
|
let content = doc.slice(from, to).content;
|
|
13
20
|
for (let depth = $from.sharedDepth(to); depth > 0; depth--)
|
|
14
21
|
content = Fragment.from($from.node(depth).copy(content));
|
|
15
|
-
|
|
22
|
+
// Not validated: the JSON is validated when pasted; the HTML is only markup.
|
|
23
|
+
const blocks = doc.type.createOpen(null, content);
|
|
16
24
|
const container = ownerDocument.createElement("div");
|
|
17
|
-
// Built with createElement/textContent (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
// Built with createElement/textContent (word/edit.ts); reading innerHTML only serializes it.
|
|
26
|
+
// A range inside one paragraph is that paragraph's text: its list number stays out of the HTML
|
|
27
|
+
// (a word copied from a list item must not paste as a one-item list); the JSON keeps it.
|
|
28
|
+
const $to = doc.resolve(to);
|
|
29
|
+
const inline = $from.parent.isTextblock && $from.sameParent($to);
|
|
30
|
+
const htmlBlocks = inline
|
|
31
|
+
? [$from.parent.copy(doc.slice(from, to).content)]
|
|
32
|
+
: blocks.content.content;
|
|
33
|
+
const first = inline ? -1 : $from.index(0);
|
|
34
|
+
container.append(...clipboardBlocks(word.context, doc, htmlBlocks, first, ownerDocument, word.ratio));
|
|
35
|
+
// How other apps (and print) recognize a page break; the editor itself does not need it.
|
|
36
|
+
for (const br of container.querySelectorAll(`br[${PAGE_BREAK_ATTR}]`))
|
|
37
|
+
br.style.cssText = "page-break-before: always; break-before: page";
|
|
26
38
|
return {
|
|
27
|
-
text: doc.textBetween(from, to, "\n",
|
|
39
|
+
text: doc.textBetween(from, to, "\n", (leaf) => leafText(leaf, ownerDocument)),
|
|
28
40
|
html: container.innerHTML,
|
|
29
|
-
json: JSON.stringify({
|
|
41
|
+
json: JSON.stringify({
|
|
42
|
+
open: $from.sharedDepth(to) > 0,
|
|
43
|
+
content: blocks.toJSON(),
|
|
44
|
+
word: copiedDefinitions(word.context, blocks),
|
|
45
|
+
}),
|
|
30
46
|
};
|
|
31
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* A leaf as plain text: breaks and rules as line feeds, a tab as a tab, opaque content as the
|
|
50
|
+
* text it shows (a field's result, a table's paragraphs on their lines), markers as nothing.
|
|
51
|
+
*/
|
|
52
|
+
function leafText(leaf, doc) {
|
|
53
|
+
const name = leaf.type.name;
|
|
54
|
+
if (name === "tab")
|
|
55
|
+
return "\t";
|
|
56
|
+
if (name === "opaqueInline" || name === "opaqueBlock") {
|
|
57
|
+
const xml = parseOpaque(String(leaf.attrs.xml ?? ""), doc);
|
|
58
|
+
const text = xml ? opaqueParagraphsText(xml) : "";
|
|
59
|
+
return leaf.isBlock ? `\n${text}` : text;
|
|
60
|
+
}
|
|
61
|
+
return leaf.isBlock || name === "hardBreak" || name === "pageBreak" || name === "columnBreak"
|
|
62
|
+
? "\n"
|
|
63
|
+
: "";
|
|
64
|
+
}
|
|
32
65
|
/**
|
|
33
66
|
* The content of clipboard data, validated, or null. Priority: Foli JSON, then HTML, then plain
|
|
34
|
-
* text. Every format is validated
|
|
35
|
-
* through to the next. `marks` are given to plain text
|
|
36
|
-
* insertion point).
|
|
67
|
+
* text. Every format is validated and fitted to the document by `parse` (`fitPaste`,
|
|
68
|
+
* `fitPastedV2`); a format that fails falls through to the next. `marks` are given to plain text
|
|
69
|
+
* (the marks typing would get at the insertion point).
|
|
37
70
|
*/
|
|
38
|
-
export function docFromClipboard(data, marks
|
|
71
|
+
export function docFromClipboard(data, marks, parse) {
|
|
39
72
|
const json = data.getData(FOLI_MIME);
|
|
40
73
|
const html = data.getData("text/html");
|
|
41
74
|
const text = data.getData("text/plain");
|
|
@@ -46,10 +79,13 @@ export function docFromClipboard(data, marks = []) {
|
|
|
46
79
|
const value = JSON.parse(json);
|
|
47
80
|
if (typeof value !== "object" || value === null)
|
|
48
81
|
return null;
|
|
49
|
-
const { open, content } = value;
|
|
50
|
-
return { content, open: open === true };
|
|
82
|
+
const { open, content, word } = value;
|
|
83
|
+
return { content, open: open === true, word };
|
|
84
|
+
},
|
|
85
|
+
() => {
|
|
86
|
+
const read = html ? readHTML(html) : null;
|
|
87
|
+
return read && { ...read, html: true };
|
|
51
88
|
},
|
|
52
|
-
() => (html ? readHTML(html) : null),
|
|
53
89
|
() => (text ? { content: textToDoc(text, marks), open: true } : null),
|
|
54
90
|
];
|
|
55
91
|
for (const candidate of candidates) {
|
|
@@ -57,7 +93,7 @@ export function docFromClipboard(data, marks = []) {
|
|
|
57
93
|
const found = candidate();
|
|
58
94
|
if (!found)
|
|
59
95
|
continue;
|
|
60
|
-
return {
|
|
96
|
+
return { ...parse(found), open: found.open };
|
|
61
97
|
}
|
|
62
98
|
catch {
|
|
63
99
|
// Invalid or unreadable in this format: try the next one.
|
|
@@ -150,33 +186,129 @@ const TAG_MARKS = new Map([
|
|
|
150
186
|
["strike", "strike"],
|
|
151
187
|
["del", "strike"],
|
|
152
188
|
]);
|
|
153
|
-
|
|
189
|
+
const NAMED_COLORS = {
|
|
190
|
+
black: "#000000",
|
|
191
|
+
white: "#ffffff",
|
|
192
|
+
red: "#ff0000",
|
|
193
|
+
green: "#008000",
|
|
194
|
+
blue: "#0000ff",
|
|
195
|
+
yellow: "#ffff00",
|
|
196
|
+
gray: "#808080",
|
|
197
|
+
grey: "#808080",
|
|
198
|
+
silver: "#c0c0c0",
|
|
199
|
+
maroon: "#800000",
|
|
200
|
+
navy: "#000080",
|
|
201
|
+
purple: "#800080",
|
|
202
|
+
teal: "#008080",
|
|
203
|
+
olive: "#808000",
|
|
204
|
+
orange: "#ffa500",
|
|
205
|
+
lime: "#00ff00",
|
|
206
|
+
aqua: "#00ffff",
|
|
207
|
+
cyan: "#00ffff",
|
|
208
|
+
fuchsia: "#ff00ff",
|
|
209
|
+
magenta: "#ff00ff",
|
|
210
|
+
};
|
|
211
|
+
const hex2 = (n) => Math.max(0, Math.min(255, Math.round(n)))
|
|
212
|
+
.toString(16)
|
|
213
|
+
.padStart(2, "0");
|
|
214
|
+
/**
|
|
215
|
+
* A CSS color as the schema stores it (lowercase `#rrggbb`), or null: transparent, unknown
|
|
216
|
+
* (system colors such as Word's `windowtext`) or empty. Hex (3 or 6 digits), `rgb()`/`rgba()`
|
|
217
|
+
* and the basic named colors.
|
|
218
|
+
*/
|
|
219
|
+
export function parseColor(value) {
|
|
220
|
+
const color = value.trim().toLowerCase();
|
|
221
|
+
const named = NAMED_COLORS[color];
|
|
222
|
+
if (named)
|
|
223
|
+
return named;
|
|
224
|
+
const short = /^#([0-9a-f])([0-9a-f])([0-9a-f])$/.exec(color);
|
|
225
|
+
if (short)
|
|
226
|
+
return `#${short[1]}${short[1]}${short[2]}${short[2]}${short[3]}${short[3]}`;
|
|
227
|
+
if (/^#[0-9a-f]{6}$/.test(color))
|
|
228
|
+
return color;
|
|
229
|
+
// rgb(r, g, b), rgb(r g b), rgba(r, g, b, a), rgb(r g b / a): split, no backtracking regex.
|
|
230
|
+
const body = /^rgba?\(([^()]*)\)$/.exec(color)?.[1];
|
|
231
|
+
if (body === undefined)
|
|
232
|
+
return null;
|
|
233
|
+
const parts = body.trim().split(/\s*[,/]\s*|\s+/);
|
|
234
|
+
if (parts.length < 3 || parts.length > 4)
|
|
235
|
+
return null;
|
|
236
|
+
if (!parts.every((part) => /^\d+(?:\.\d+)?%?$/.test(part)))
|
|
237
|
+
return null;
|
|
238
|
+
const numbers = parts.map((part) => Number.parseFloat(part));
|
|
239
|
+
if (numbers[3] === 0)
|
|
240
|
+
return null; // fully transparent
|
|
241
|
+
const hex = `#${hex2(numbers[0])}${hex2(numbers[1])}${hex2(numbers[2])}`;
|
|
242
|
+
return hex;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Marks of an element on top of the inherited ones: tags first, then inline styles; a link
|
|
246
|
+
* from `a[href]` when the href is safe (`isSafeHref`; links inside the source page, `#…`, are
|
|
247
|
+
* dropped); text color and highlight from inline elements only (a page's colored containers
|
|
248
|
+
* are layout, not formatting), black text and white highlight being the defaults.
|
|
249
|
+
*/
|
|
154
250
|
function marksOf(element, inherited) {
|
|
155
|
-
const marks = new
|
|
156
|
-
const
|
|
251
|
+
const marks = new Map(inherited);
|
|
252
|
+
const tag = element.localName;
|
|
253
|
+
const tagMark = TAG_MARKS.get(tag);
|
|
157
254
|
if (tagMark)
|
|
158
|
-
marks.
|
|
255
|
+
marks.set(tagMark, { type: tagMark });
|
|
256
|
+
if (tag === "a") {
|
|
257
|
+
const href = element.getAttribute("href")?.trim().replace(/ /g, "%20");
|
|
258
|
+
if (href && !href.startsWith("#") && isSafeHref(href)) {
|
|
259
|
+
marks.set("link", { type: "link", attrs: { href } });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
159
262
|
const style = element.style;
|
|
160
263
|
if (!style)
|
|
161
264
|
return marks;
|
|
162
265
|
const weight = style.fontWeight;
|
|
163
266
|
// Google Docs wraps everything in <b style="font-weight:normal">.
|
|
164
267
|
if (/^(bold|bolder|[6-9]00)$/.test(weight))
|
|
165
|
-
marks.
|
|
268
|
+
marks.set("bold", { type: "bold" });
|
|
166
269
|
else if (/^(normal|lighter|[1-5]00)$/.test(weight))
|
|
167
270
|
marks.delete("bold");
|
|
168
271
|
if (/^(italic|oblique)/.test(style.fontStyle))
|
|
169
|
-
marks.
|
|
272
|
+
marks.set("italic", { type: "italic" });
|
|
170
273
|
else if (style.fontStyle === "normal")
|
|
171
274
|
marks.delete("italic");
|
|
172
275
|
const decoration = `${style.textDecorationLine} ${style.textDecoration}`;
|
|
173
276
|
if (decoration.includes("underline"))
|
|
174
|
-
marks.
|
|
277
|
+
marks.set("underline", { type: "underline" });
|
|
175
278
|
if (decoration.includes("line-through"))
|
|
176
|
-
marks.
|
|
279
|
+
marks.set("strike", { type: "strike" });
|
|
177
280
|
// `text-decoration: none` on a child does not remove a parent's line in CSS: keep it.
|
|
281
|
+
if (!BLOCK_TAGS.has(tag)) {
|
|
282
|
+
const color = parseColor(style.color || (tag === "font" ? (element.getAttribute("color") ?? "") : ""));
|
|
283
|
+
// Black is the default; white text comes from dark pages and would be invisible.
|
|
284
|
+
if (color === "#000000" || color === "#ffffff")
|
|
285
|
+
marks.delete("textColor");
|
|
286
|
+
else if (color)
|
|
287
|
+
marks.set("textColor", { type: "textColor", attrs: { color } });
|
|
288
|
+
const background = parseColor(style.backgroundColor);
|
|
289
|
+
if (background === "#ffffff")
|
|
290
|
+
marks.delete("highlight");
|
|
291
|
+
else if (background) {
|
|
292
|
+
marks.set("highlight", { type: "highlight", attrs: { color: background } });
|
|
293
|
+
}
|
|
294
|
+
}
|
|
178
295
|
return marks;
|
|
179
296
|
}
|
|
297
|
+
/** A block's own alignment (`text-align`, or the old `align` attribute), or undefined. */
|
|
298
|
+
function alignOf(element) {
|
|
299
|
+
const value = (element.style?.textAlign ||
|
|
300
|
+
element.getAttribute("align") ||
|
|
301
|
+
"").toLowerCase();
|
|
302
|
+
if (value === "left" || value === "start")
|
|
303
|
+
return "start";
|
|
304
|
+
if (value === "center" || value === "-webkit-center")
|
|
305
|
+
return "center";
|
|
306
|
+
if (value === "right" || value === "end")
|
|
307
|
+
return "end";
|
|
308
|
+
if (value === "justify")
|
|
309
|
+
return "justify";
|
|
310
|
+
return undefined;
|
|
311
|
+
}
|
|
180
312
|
/**
|
|
181
313
|
* A page break in pasted HTML: Foli's own `<br data-page-break>`, or a `<br>` or `<hr>` styled
|
|
182
314
|
* to break the page (Word writes `<br clear=all style='page-break-before:always'>`, Google Docs
|
|
@@ -201,11 +333,57 @@ function isNoise(element) {
|
|
|
201
333
|
if (element.style?.display === "none" || element.hasAttribute("hidden")) {
|
|
202
334
|
return true;
|
|
203
335
|
}
|
|
204
|
-
return element.localName === "o:p" && !element.textContent?.replace(/[\s
|
|
336
|
+
return element.localName === "o:p" && !element.textContent?.replace(/[\s ]/g, "");
|
|
205
337
|
}
|
|
206
338
|
/**
|
|
207
|
-
*
|
|
208
|
-
* (
|
|
339
|
+
* A Word list paragraph (`style="mso-list:l0 level2 lfo1"`): its list and level, and from its
|
|
340
|
+
* marker (the `mso-list:Ignore` span Word adds for other apps) whether it is numbered and
|
|
341
|
+
* where the numbering starts. Best effort: a marker ending in "." or ")" is a number or
|
|
342
|
+
* letter (numbered); anything else ("·", "o", "§") a bullet.
|
|
343
|
+
*/
|
|
344
|
+
function wordListItem(element) {
|
|
345
|
+
const match = /mso-list:\s*(l\d+)\s+level(\d+)/i.exec(element.getAttribute("style") ?? "");
|
|
346
|
+
if (!match)
|
|
347
|
+
return null;
|
|
348
|
+
let marker = "";
|
|
349
|
+
for (const child of element.querySelectorAll("[style]")) {
|
|
350
|
+
if (/mso-list:\s*ignore/i.test(child.getAttribute("style") ?? "")) {
|
|
351
|
+
marker = (child.textContent ?? "").replace(/[\s ]+/g, "");
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
// "1.", "a)", "(iv)", and multi-level "1.2." or "1.2": numbered; its last part is its number.
|
|
356
|
+
const ordered = /^\(?[0-9a-zA-Z]{1,6}(?:\.[0-9]{1,6})*[.)]$|^\d{1,6}(?:\.\d{1,6})+$/.test(marker);
|
|
357
|
+
const number = /(\d+)[.)]?$/.exec(marker);
|
|
358
|
+
return {
|
|
359
|
+
list: match[1],
|
|
360
|
+
level: Number(match[2]),
|
|
361
|
+
ordered,
|
|
362
|
+
start: ordered && number ? Number(number[1]) : 1,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
/** A list item must start with a paragraph: a heading becomes one, else one is added. */
|
|
366
|
+
function finishItem(item) {
|
|
367
|
+
if (!item.content)
|
|
368
|
+
item.content = [];
|
|
369
|
+
const content = item.content;
|
|
370
|
+
const first = content[0];
|
|
371
|
+
if (first?.type === "heading") {
|
|
372
|
+
first.type = "paragraph";
|
|
373
|
+
const align = first.attrs?.align;
|
|
374
|
+
if (align)
|
|
375
|
+
first.attrs = { align };
|
|
376
|
+
else
|
|
377
|
+
delete first.attrs;
|
|
378
|
+
}
|
|
379
|
+
else if (first?.type !== "paragraph") {
|
|
380
|
+
content.unshift({ type: "paragraph" });
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Own HTML reader: maps known tags and inline styles to the schema (paragraphs, headings,
|
|
385
|
+
* lists, rules; bold, italic, underline, strike, links, colors; alignment), drops everything
|
|
386
|
+
* else. The HTML is parsed into an inert document and never inserted.
|
|
209
387
|
*/
|
|
210
388
|
export function htmlToDoc(html) {
|
|
211
389
|
return readHTML(html)?.content ?? null;
|
|
@@ -213,17 +391,34 @@ export function htmlToDoc(html) {
|
|
|
213
391
|
/** `htmlToDoc`, plus whether the HTML had no block elements (`Pasted.open`). */
|
|
214
392
|
function readHTML(html) {
|
|
215
393
|
const parsed = new DOMParser().parseFromString(html, "text/html");
|
|
216
|
-
const
|
|
394
|
+
const doc = { type: "doc", content: [] };
|
|
395
|
+
const stack = [{ node: doc, kind: "doc" }];
|
|
217
396
|
let inline = null;
|
|
218
397
|
let blockType = "paragraph";
|
|
219
398
|
let level = 1;
|
|
399
|
+
let blockAlign;
|
|
220
400
|
let blockPre = false;
|
|
221
401
|
let sawBlock = false;
|
|
222
402
|
let afterSpace = true; // collapse leading and repeated whitespace like HTML layout does
|
|
223
|
-
const
|
|
403
|
+
const top = () => stack[stack.length - 1];
|
|
404
|
+
/** Where blocks go now: the open item or the document (text directly in a list gets an item). */
|
|
405
|
+
const container = () => {
|
|
406
|
+
if (top().kind === "list") {
|
|
407
|
+
const item = { type: "listItem", content: [] };
|
|
408
|
+
top().node.content.push(item);
|
|
409
|
+
stack.push({ node: item, kind: "item", implicit: true });
|
|
410
|
+
}
|
|
411
|
+
return top().node.content;
|
|
412
|
+
};
|
|
413
|
+
const lastBlock = () => {
|
|
414
|
+
const blocks = top().node.content;
|
|
415
|
+
return blocks[blocks.length - 1];
|
|
416
|
+
};
|
|
417
|
+
const open = (type, headingLevel = 1, pre = false, align = undefined) => {
|
|
224
418
|
inline = [];
|
|
225
419
|
blockType = type;
|
|
226
420
|
level = headingLevel;
|
|
421
|
+
blockAlign = align;
|
|
227
422
|
blockPre = pre;
|
|
228
423
|
afterSpace = true;
|
|
229
424
|
};
|
|
@@ -239,22 +434,33 @@ function readHTML(html) {
|
|
|
239
434
|
// Non-breaking spaces survive the trimming above, then become ordinary spaces.
|
|
240
435
|
for (const node of inline) {
|
|
241
436
|
if (node.type === "text")
|
|
242
|
-
node.text = node.text.replace(
|
|
437
|
+
node.text = node.text.replace(/ /g, " ");
|
|
243
438
|
}
|
|
244
439
|
const content = inline.filter((node) => node.type !== "text" || node.text);
|
|
245
|
-
if (content.length || keepEmpty) {
|
|
246
|
-
blocks.push(blockType === "heading"
|
|
247
|
-
? { type: "heading", attrs: { level }, content }
|
|
248
|
-
: { type: "paragraph", content });
|
|
249
|
-
}
|
|
250
440
|
inline = null;
|
|
441
|
+
if (!content.length && !keepEmpty)
|
|
442
|
+
return;
|
|
443
|
+
const attrs = {};
|
|
444
|
+
if (blockType === "heading")
|
|
445
|
+
attrs.level = level;
|
|
446
|
+
if (blockAlign && blockAlign !== "start")
|
|
447
|
+
attrs.align = blockAlign;
|
|
448
|
+
container().push({
|
|
449
|
+
type: blockType,
|
|
450
|
+
...(Object.keys(attrs).length ? { attrs } : {}),
|
|
451
|
+
...(content.length ? { content } : {}),
|
|
452
|
+
});
|
|
251
453
|
};
|
|
252
454
|
const addText = (raw, marks, pre) => {
|
|
253
455
|
// Source formatting between blocks ("</p>\n<br>\n<p>") is not content.
|
|
254
456
|
if (!inline && !pre && !/[^ \t\n\r\f]/.test(raw))
|
|
255
457
|
return;
|
|
256
|
-
if (!inline)
|
|
458
|
+
if (!inline) {
|
|
459
|
+
// Text of its own after Word list paragraphs ends them (it is not part of an item).
|
|
460
|
+
if (stack.some((entry) => entry.word))
|
|
461
|
+
closeWordLists();
|
|
257
462
|
open("paragraph", 1, pre);
|
|
463
|
+
}
|
|
258
464
|
const lines = pre ? raw.replace(/\r\n?/g, "\n").split("\n") : [raw];
|
|
259
465
|
lines.forEach((line, i) => {
|
|
260
466
|
if (i > 0)
|
|
@@ -265,7 +471,8 @@ function readHTML(html) {
|
|
|
265
471
|
if (!text)
|
|
266
472
|
return;
|
|
267
473
|
afterSpace = !pre && text.endsWith(" ");
|
|
268
|
-
|
|
474
|
+
// A link's own look (Google Docs' blue underline) is the link's, not formatting.
|
|
475
|
+
const markJSON = [...marks.values()].filter((mark) => !marks.has("link") || (mark.type !== "underline" && mark.type !== "textColor"));
|
|
269
476
|
inline.push({
|
|
270
477
|
type: "text",
|
|
271
478
|
text,
|
|
@@ -273,13 +480,14 @@ function readHTML(html) {
|
|
|
273
480
|
});
|
|
274
481
|
});
|
|
275
482
|
};
|
|
276
|
-
/** An explicit page break: in the open block, else at the end of the previous
|
|
483
|
+
/** An explicit page break: in the open block, else at the end of the previous textblock. */
|
|
277
484
|
const addPageBreak = () => {
|
|
278
|
-
const previous =
|
|
485
|
+
const previous = lastBlock();
|
|
279
486
|
if (inline)
|
|
280
487
|
inline.push({ type: "pageBreak" });
|
|
281
|
-
else if (previous)
|
|
488
|
+
else if (previous && (previous.type === "paragraph" || previous.type === "heading")) {
|
|
282
489
|
previous.content = [...(previous.content ?? []), { type: "pageBreak" }];
|
|
490
|
+
}
|
|
283
491
|
else {
|
|
284
492
|
open("paragraph");
|
|
285
493
|
inline?.push({ type: "pageBreak" });
|
|
@@ -289,7 +497,7 @@ function readHTML(html) {
|
|
|
289
497
|
const addBreak = () => {
|
|
290
498
|
// A <br> between blocks is an empty line (Google Docs writes empty paragraphs this way).
|
|
291
499
|
if (!inline) {
|
|
292
|
-
|
|
500
|
+
container().push({ type: "paragraph" });
|
|
293
501
|
return;
|
|
294
502
|
}
|
|
295
503
|
const list = inline;
|
|
@@ -299,10 +507,97 @@ function readHTML(html) {
|
|
|
299
507
|
list.push({ type: "hardBreak" });
|
|
300
508
|
afterSpace = true;
|
|
301
509
|
};
|
|
510
|
+
/** Closes the top container: an item gets its first paragraph, a list goes to its parent. */
|
|
511
|
+
const pop = () => {
|
|
512
|
+
const container = stack.pop();
|
|
513
|
+
if (container.kind === "item" && container.implicit && !container.node.content?.length) {
|
|
514
|
+
// An item opened for content that never came (`<ul><ul></ul></ul>`): not an empty bullet.
|
|
515
|
+
const items = top().node.content;
|
|
516
|
+
items.splice(items.indexOf(container.node), 1);
|
|
517
|
+
return;
|
|
518
|
+
}
|
|
519
|
+
if (container.kind === "item")
|
|
520
|
+
finishItem(container.node);
|
|
521
|
+
if (container.kind === "list" && container.node.content?.length) {
|
|
522
|
+
container.parent?.push(container.node);
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
/** Opens a list; one directly inside a list (sloppy HTML, Google Docs) nests in its last item. */
|
|
526
|
+
const openList = (ordered, start, word) => {
|
|
527
|
+
close(false);
|
|
528
|
+
if (top().kind === "list") {
|
|
529
|
+
const items = top().node.content;
|
|
530
|
+
let item = items[items.length - 1];
|
|
531
|
+
if (!item) {
|
|
532
|
+
item = { type: "listItem", content: [] };
|
|
533
|
+
items.push(item);
|
|
534
|
+
}
|
|
535
|
+
stack.push({ node: item, kind: "item", implicit: true });
|
|
536
|
+
}
|
|
537
|
+
const node = {
|
|
538
|
+
type: ordered ? "orderedList" : "bulletList",
|
|
539
|
+
...(ordered && start !== 1 && Number.isInteger(start) && start >= 0 && start <= 99_999
|
|
540
|
+
? { attrs: { start } }
|
|
541
|
+
: {}),
|
|
542
|
+
content: [],
|
|
543
|
+
};
|
|
544
|
+
stack.push({
|
|
545
|
+
node,
|
|
546
|
+
kind: "list",
|
|
547
|
+
parent: top().node.content,
|
|
548
|
+
...(word && { word }),
|
|
549
|
+
});
|
|
550
|
+
};
|
|
551
|
+
const openItem = (word) => {
|
|
552
|
+
close(false);
|
|
553
|
+
while (top().kind === "item" && (top().implicit || word))
|
|
554
|
+
pop();
|
|
555
|
+
const item = { type: "listItem", content: [] };
|
|
556
|
+
top().node.content.push(item);
|
|
557
|
+
stack.push({ node: item, kind: "item", ...(word && { word }) });
|
|
558
|
+
};
|
|
559
|
+
/** Word lists are flat paragraphs with levels: open and close lists to nest them. */
|
|
560
|
+
const wordItem = (item) => {
|
|
561
|
+
close(false);
|
|
562
|
+
const wordList = () => {
|
|
563
|
+
for (let i = stack.length - 1; i > 0; i--) {
|
|
564
|
+
const entry = stack[i];
|
|
565
|
+
if (entry.kind === "list" && entry.word)
|
|
566
|
+
return { entry, index: i };
|
|
567
|
+
}
|
|
568
|
+
return null;
|
|
569
|
+
};
|
|
570
|
+
for (let found = wordList(); found; found = wordList()) {
|
|
571
|
+
const { word } = found.entry;
|
|
572
|
+
if (!word)
|
|
573
|
+
break;
|
|
574
|
+
const same = word.level === item.level && word.list === item.list && word.ordered === item.ordered;
|
|
575
|
+
if (word.level < item.level || (same && word.level === item.level))
|
|
576
|
+
break;
|
|
577
|
+
while (stack.length > found.index)
|
|
578
|
+
pop();
|
|
579
|
+
}
|
|
580
|
+
const current = wordList();
|
|
581
|
+
const word = { list: item.list, level: item.level, ordered: item.ordered };
|
|
582
|
+
if (!current || current.entry.word?.level !== item.level) {
|
|
583
|
+
openList(item.ordered, item.start, word);
|
|
584
|
+
}
|
|
585
|
+
else {
|
|
586
|
+
// A sibling item: close the previous one (and anything nested in it).
|
|
587
|
+
while (stack.length > current.index + 1)
|
|
588
|
+
pop();
|
|
589
|
+
}
|
|
590
|
+
openItem(word);
|
|
591
|
+
};
|
|
592
|
+
const closeWordLists = () => {
|
|
593
|
+
close(false);
|
|
594
|
+
while (stack.some((entry) => entry.word))
|
|
595
|
+
pop();
|
|
596
|
+
};
|
|
302
597
|
/** Walks `node`; returns whether it contained a block element. */
|
|
303
|
-
const walk = (node,
|
|
598
|
+
const walk = (node, context) => {
|
|
304
599
|
if (node.nodeType === 3) {
|
|
305
|
-
addText(node.data, marks, pre);
|
|
600
|
+
addText(node.data, context.marks, context.pre);
|
|
306
601
|
return false;
|
|
307
602
|
}
|
|
308
603
|
if (node.nodeType !== 1)
|
|
@@ -322,28 +617,80 @@ function readHTML(html) {
|
|
|
322
617
|
addBreak();
|
|
323
618
|
return false;
|
|
324
619
|
}
|
|
325
|
-
const childMarks = marksOf(element, marks);
|
|
326
620
|
const whiteSpace = element.style?.whiteSpace ?? "";
|
|
327
|
-
const
|
|
328
|
-
|
|
621
|
+
const child = {
|
|
622
|
+
marks: marksOf(element, context.marks),
|
|
623
|
+
pre: context.pre || tag === "pre" || whiteSpace.startsWith("pre"),
|
|
624
|
+
align: BLOCK_TAGS.has(tag) ? (alignOf(element) ?? context.align) : context.align,
|
|
625
|
+
};
|
|
626
|
+
const children = () => {
|
|
329
627
|
let hasBlock = false;
|
|
330
|
-
for (const
|
|
331
|
-
hasBlock = walk(
|
|
628
|
+
for (const node of element.childNodes)
|
|
629
|
+
hasBlock = walk(node, child) || hasBlock;
|
|
332
630
|
return hasBlock;
|
|
333
|
-
}
|
|
631
|
+
};
|
|
632
|
+
if (!BLOCK_TAGS.has(tag))
|
|
633
|
+
return children();
|
|
334
634
|
sawBlock = true;
|
|
635
|
+
// Word list paragraphs: <p> only (a numbered Word heading stays a heading), and not inside an
|
|
636
|
+
// HTML list (Outlook writes `<li style="mso-list:…">` in real lists).
|
|
637
|
+
const word = tag === "p" && !stack.some((entry) => entry.kind === "list" && !entry.word)
|
|
638
|
+
? wordListItem(element)
|
|
639
|
+
: null;
|
|
640
|
+
if (word) {
|
|
641
|
+
wordItem(word);
|
|
642
|
+
open("paragraph", 1, child.pre, child.align);
|
|
643
|
+
children();
|
|
644
|
+
close(true);
|
|
645
|
+
return true;
|
|
646
|
+
}
|
|
647
|
+
// Word wraps its content in a <div>: only a block of its own content ends Word lists.
|
|
648
|
+
if (!/^(div|body|section|article|main)$/.test(tag))
|
|
649
|
+
closeWordLists();
|
|
650
|
+
// A list or item closes everything opened inside it, so unbalanced input cannot leak.
|
|
651
|
+
const base = stack.length;
|
|
652
|
+
const closeTo = (height) => {
|
|
653
|
+
close(false);
|
|
654
|
+
while (stack.length > height)
|
|
655
|
+
pop();
|
|
656
|
+
};
|
|
657
|
+
if (tag === "ul" || tag === "ol") {
|
|
658
|
+
const start = Number.parseInt(element.getAttribute("start") ?? "", 10);
|
|
659
|
+
openList(tag === "ol", Number.isNaN(start) ? 1 : start);
|
|
660
|
+
children();
|
|
661
|
+
closeTo(base);
|
|
662
|
+
return true;
|
|
663
|
+
}
|
|
664
|
+
if (tag === "li" && stack.some((entry) => entry.kind === "list")) {
|
|
665
|
+
// Inside a list item that is itself still open, an <li> is a sibling: close down to a list.
|
|
666
|
+
while (top().kind === "item")
|
|
667
|
+
closeTo(stack.length - 1);
|
|
668
|
+
const height = stack.length;
|
|
669
|
+
openItem();
|
|
670
|
+
children();
|
|
671
|
+
closeTo(height);
|
|
672
|
+
return true;
|
|
673
|
+
}
|
|
674
|
+
if (tag === "hr") {
|
|
675
|
+
close(false);
|
|
676
|
+
container().push({ type: "horizontalRule" });
|
|
677
|
+
return true;
|
|
678
|
+
}
|
|
335
679
|
close(false);
|
|
336
680
|
const heading = /^h([1-6])$/.exec(tag);
|
|
337
|
-
open(heading ? "heading" : "paragraph", heading ? Number(heading[1]) : 1,
|
|
338
|
-
|
|
339
|
-
for (const child of element.childNodes)
|
|
340
|
-
hasBlock = walk(child, childMarks, childPre) || hasBlock;
|
|
681
|
+
open(heading ? "heading" : "paragraph", heading ? Number(heading[1]) : 1, child.pre, child.align);
|
|
682
|
+
const hasBlock = children();
|
|
341
683
|
// <p></p> is an empty line; a container such as <div><p>…</p></div> adds none.
|
|
342
|
-
close(!hasBlock &&
|
|
684
|
+
close(!hasBlock && !/^(ol|ul|dl|table|tbody|thead|tfoot|tr|li)$/.test(tag));
|
|
343
685
|
return true;
|
|
344
686
|
};
|
|
345
|
-
walk(parsed.body, new
|
|
687
|
+
walk(parsed.body, { marks: new Map(), pre: false, align: undefined });
|
|
346
688
|
close(false);
|
|
347
|
-
|
|
689
|
+
while (stack.length > 1)
|
|
690
|
+
pop();
|
|
691
|
+
const content = doc.content;
|
|
692
|
+
return content.length
|
|
693
|
+
? { content: { type: "doc", content }, open: !sawBlock }
|
|
694
|
+
: null;
|
|
348
695
|
}
|
|
349
696
|
//# sourceMappingURL=clipboard.js.map
|