@folijs/view 0.0.0-stage → 0.1.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 +18 -0
- package/LICENSE +21 -0
- package/README.md +33 -2
- package/dist/clipboard.d.ts +38 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.js +349 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/layout/flow.d.ts +91 -0
- package/dist/layout/flow.d.ts.map +1 -0
- package/dist/layout/flow.js +200 -0
- package/dist/layout/flow.js.map +1 -0
- package/dist/render.d.ts +32 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +70 -0
- package/dist/render.js.map +1 -0
- package/dist/view.d.ts +225 -0
- package/dist/view.d.ts.map +1 -0
- package/dist/view.js +1394 -0
- package/dist/view.js.map +1 -0
- package/package.json +31 -3
- package/src/clipboard.ts +369 -0
- package/src/index.ts +2 -0
- package/src/layout/flow.ts +281 -0
- package/src/render.ts +98 -0
- package/src/view.ts +1463 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @folijs/view
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- First release: a document editor for the web with its own editing engine, on real pages.
|
|
8
|
+
- **Documents** (`@folijs/core`): a versioned JSON format (`schemaVersion: 1`) with page size and margins in millimetres; `parseDocument` validates untrusted input and never drops document content silently. Schema v1: paragraphs, headings 1–6, line breaks, page breaks; bold, italic, underline, strikethrough.
|
|
9
|
+
- **Engine** (`@folijs/core`): immutable state, steps and transactions, undo/redo history, commands, key bindings, a headless `createEditor`. No DOM.
|
|
10
|
+
- **View** (`@folijs/view`): `EditorView` renders the document on A4 or Letter pages in one `contenteditable` host. Paragraphs split across pages at a line, with widow/orphan and keep-with-next rules, explicit page breaks, incremental reflow, and print with one sheet per page (PDF output checked in Chromium). Typing, IME composition, copy, cut and paste (Foli JSON, HTML mapped to the schema, plain text), read-only mode, placeholder.
|
|
11
|
+
- **React** (`@folijs/react`, React 18.2+ and 19): `FoliEditor` with a formatting toolbar and `onChange`; `useFoliEditor`, `FoliEditorProvider`, `FoliCanvas`, `FoliToolbar`, `useCurrentEditor`, `useEditorState`. Server rendering gives the editor's shell; the document appears on hydration. Default theme in `@folijs/react/styles.css` (CSS variables).
|
|
12
|
+
|
|
13
|
+
Known risks: real IME and dead keys (Windows, macOS, Android, iOS input methods), NVDA and VoiceOver, macOS key bindings, spellcheck at a page break, and pastes from real Word, Google Docs and LibreOffice have not been checked by hand. Automated tests run in Chromium, Firefox and WebKit on Windows (IME only through Chromium's emulation, paste not in WebKit, no screen-reader tests); Safari on macOS and iOS is not tested. See [Known limitations](https://github.com/josep-audenis/foli/blob/main/docs/limitations.md).
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- Updated dependencies
|
|
18
|
+
- @folijs/core@0.1.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Josep Audenis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @folijs/view
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The editable view of [Foli](https://github.com/josep-audenis/foli), without a framework: renders
|
|
4
|
+
a `@folijs/core` editor into an element as real pages and turns keyboard, IME, clipboard and
|
|
5
|
+
mouse input into editor commands. For React, use [`@folijs/react`](https://www.npmjs.com/package/@folijs/react).
|
|
6
|
+
|
|
7
|
+
> Early release: the API may change in any 0.x minor version (changes in `CHANGELOG.md`).
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @folijs/view @folijs/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createDocument, createEditor } from "@folijs/core";
|
|
15
|
+
import { EditorView } from "@folijs/view";
|
|
16
|
+
|
|
17
|
+
const editor = createEditor({ document: createDocument() });
|
|
18
|
+
const host = document.getElementById("editor")!;
|
|
19
|
+
host.classList.add("foli-content"); // the default theme's selection color
|
|
20
|
+
host.setAttribute("aria-label", "Document");
|
|
21
|
+
const view = new EditorView(host, editor, { placeholder: "Start writing…" });
|
|
22
|
+
// later: view.destroy();
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
The view sets page size and margins from the document settings; the page look (background,
|
|
26
|
+
shadow, gap, fonts) comes from your CSS or a copy of
|
|
27
|
+
[`@folijs/react`'s `styles.css`](https://github.com/josep-audenis/foli/blob/main/packages/react/styles.css).
|
|
28
|
+
|
|
29
|
+
- [Getting started](https://github.com/josep-audenis/foli/blob/main/docs/getting-started.md#using-the-view-without-react)
|
|
30
|
+
- [API reference](https://github.com/josep-audenis/foli/blob/main/docs/api.md#foliview)
|
|
31
|
+
- [Styling](https://github.com/josep-audenis/foli/blob/main/docs/styling.md)
|
|
32
|
+
- [Known limitations](https://github.com/josep-audenis/foli/blob/main/docs/limitations.md)
|
|
33
|
+
|
|
34
|
+
MIT
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type DocJSON, type MarkJSON, type Node as ModelNode } from "@folijs/core";
|
|
2
|
+
/**
|
|
3
|
+
* Clipboard type for Foli content, read back first on paste: `{ open, content }`, the copied
|
|
4
|
+
* blocks as `DocJSON` and whether they were cut out of one block (see `Pasted.open`).
|
|
5
|
+
*/
|
|
6
|
+
export declare const FOLI_MIME = "application/x-foli+json";
|
|
7
|
+
/** Validated clipboard content. */
|
|
8
|
+
export interface Pasted {
|
|
9
|
+
doc: ModelNode;
|
|
10
|
+
/**
|
|
11
|
+
* True when no block boundary was copied (text from inside one block, plain text, inline
|
|
12
|
+
* HTML): the content always joins the block it is pasted into, which keeps its type. Closed
|
|
13
|
+
* content (whole blocks) replaces an empty block instead.
|
|
14
|
+
*/
|
|
15
|
+
open: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface ClipboardContent {
|
|
18
|
+
text: string;
|
|
19
|
+
html: string;
|
|
20
|
+
json: string;
|
|
21
|
+
}
|
|
22
|
+
/** Serializes `from..to` of `doc` for the clipboard. Hard breaks and block ends become "\n". */
|
|
23
|
+
export declare function serializeRange(doc: ModelNode, from: number, to: number, ownerDocument: Document): ClipboardContent;
|
|
24
|
+
/**
|
|
25
|
+
* The content of clipboard data, validated, or null. Priority: Foli JSON, then HTML, then plain
|
|
26
|
+
* text. Every format is validated by the schema (`parseContent`); a format that fails falls
|
|
27
|
+
* through to the next. `marks` are given to plain text (the marks typing would get at the
|
|
28
|
+
* insertion point).
|
|
29
|
+
*/
|
|
30
|
+
export declare function docFromClipboard(data: Pick<DataTransfer, "getData">, marks?: readonly MarkJSON[]): Pasted | null;
|
|
31
|
+
/** Plain text: one paragraph per line. */
|
|
32
|
+
export declare function textToDoc(text: string, marks?: readonly MarkJSON[]): DocJSON;
|
|
33
|
+
/**
|
|
34
|
+
* Own HTML reader: maps known tags and inline styles to schema v1, drops everything else
|
|
35
|
+
* (links keep their text). The HTML is parsed into an inert document and never inserted.
|
|
36
|
+
*/
|
|
37
|
+
export declare function htmlToDoc(html: string): DocJSON | null;
|
|
38
|
+
//# sourceMappingURL=clipboard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipboard.d.ts","sourceRoot":"","sources":["../src/clipboard.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,OAAO,EAGZ,KAAK,QAAQ,EAEb,KAAK,IAAI,IAAI,SAAS,EAEvB,MAAM,cAAc,CAAC;AAGtB;;;GAGG;AACH,eAAO,MAAM,SAAS,4BAA4B,CAAC;AAEnD,mCAAmC;AACnC,MAAM,WAAW,MAAM;IACrB,GAAG,EAAE,SAAS,CAAC;IACf;;;;OAIG;IACH,IAAI,EAAE,OAAO,CAAC;CACf;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,gGAAgG;AAChG,wBAAgB,cAAc,CAC5B,GAAG,EAAE,SAAS,EACd,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,EACV,aAAa,EAAE,QAAQ,GACtB,gBAAgB,CAsBlB;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAC9B,IAAI,EAAE,IAAI,CAAC,YAAY,EAAE,SAAS,CAAC,EACnC,KAAK,GAAE,SAAS,QAAQ,EAAO,GAC9B,MAAM,GAAG,IAAI,CAyBf;AAED,0CAA0C;AAC1C,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,GAAE,SAAS,QAAQ,EAAO,GAAG,OAAO,CAahF;AAwHD;;;GAGG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,CAEtD"}
|
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
import { Fragment, parseContent, } from "@folijs/core";
|
|
2
|
+
import { PAGE_BREAK_ATTR, renderBlock } from "./render.js";
|
|
3
|
+
/**
|
|
4
|
+
* Clipboard type for Foli content, read back first on paste: `{ open, content }`, the copied
|
|
5
|
+
* blocks as `DocJSON` and whether they were cut out of one block (see `Pasted.open`).
|
|
6
|
+
*/
|
|
7
|
+
export const FOLI_MIME = "application/x-foli+json";
|
|
8
|
+
/** Serializes `from..to` of `doc` for the clipboard. Hard breaks and block ends become "\n". */
|
|
9
|
+
export function serializeRange(doc, from, to, ownerDocument) {
|
|
10
|
+
// A slice inside one textblock holds only inline content: wrap it back in its ancestors.
|
|
11
|
+
const $from = doc.resolve(from);
|
|
12
|
+
let content = doc.slice(from, to).content;
|
|
13
|
+
for (let depth = $from.sharedDepth(to); depth > 0; depth--)
|
|
14
|
+
content = Fragment.from($from.node(depth).copy(content));
|
|
15
|
+
const blocks = doc.type.create(null, content);
|
|
16
|
+
const container = ownerDocument.createElement("div");
|
|
17
|
+
// Built with createElement/textContent (render.ts); reading innerHTML only serializes it.
|
|
18
|
+
blocks.forEach((block) => {
|
|
19
|
+
const { dom } = renderBlock(block, ownerDocument);
|
|
20
|
+
dom.style.whiteSpace = "pre-wrap"; // keep repeated and leading spaces in other apps
|
|
21
|
+
// How other apps (and print) recognize a page break; the editor itself does not need it.
|
|
22
|
+
for (const br of dom.querySelectorAll(`br[${PAGE_BREAK_ATTR}]`))
|
|
23
|
+
br.style.cssText = "page-break-before: always; break-before: page";
|
|
24
|
+
container.append(dom);
|
|
25
|
+
});
|
|
26
|
+
return {
|
|
27
|
+
text: doc.textBetween(from, to, "\n", "\n"),
|
|
28
|
+
html: container.innerHTML,
|
|
29
|
+
json: JSON.stringify({ open: $from.sharedDepth(to) > 0, content: blocks.toJSON() }),
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The content of clipboard data, validated, or null. Priority: Foli JSON, then HTML, then plain
|
|
34
|
+
* text. Every format is validated by the schema (`parseContent`); a format that fails falls
|
|
35
|
+
* through to the next. `marks` are given to plain text (the marks typing would get at the
|
|
36
|
+
* insertion point).
|
|
37
|
+
*/
|
|
38
|
+
export function docFromClipboard(data, marks = []) {
|
|
39
|
+
const json = data.getData(FOLI_MIME);
|
|
40
|
+
const html = data.getData("text/html");
|
|
41
|
+
const text = data.getData("text/plain");
|
|
42
|
+
const candidates = [
|
|
43
|
+
() => {
|
|
44
|
+
if (!json)
|
|
45
|
+
return null;
|
|
46
|
+
const value = JSON.parse(json);
|
|
47
|
+
if (typeof value !== "object" || value === null)
|
|
48
|
+
return null;
|
|
49
|
+
const { open, content } = value;
|
|
50
|
+
return { content, open: open === true };
|
|
51
|
+
},
|
|
52
|
+
() => (html ? readHTML(html) : null),
|
|
53
|
+
() => (text ? { content: textToDoc(text, marks), open: true } : null),
|
|
54
|
+
];
|
|
55
|
+
for (const candidate of candidates) {
|
|
56
|
+
try {
|
|
57
|
+
const found = candidate();
|
|
58
|
+
if (!found)
|
|
59
|
+
continue;
|
|
60
|
+
return { doc: parseContent(found.content), open: found.open };
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
// Invalid or unreadable in this format: try the next one.
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
/** Plain text: one paragraph per line. */
|
|
69
|
+
export function textToDoc(text, marks = []) {
|
|
70
|
+
const lines = text.replace(/\r\n?/g, "\n").split("\n");
|
|
71
|
+
return {
|
|
72
|
+
type: "doc",
|
|
73
|
+
content: lines.map((line) => ({
|
|
74
|
+
type: "paragraph",
|
|
75
|
+
...(line
|
|
76
|
+
? {
|
|
77
|
+
content: [{ type: "text", text: line, ...(marks.length ? { marks: [...marks] } : {}) }],
|
|
78
|
+
}
|
|
79
|
+
: {}),
|
|
80
|
+
})),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
const BLOCK_TAGS = new Set([
|
|
84
|
+
"address",
|
|
85
|
+
"article",
|
|
86
|
+
"aside",
|
|
87
|
+
"blockquote",
|
|
88
|
+
"dd",
|
|
89
|
+
"div",
|
|
90
|
+
"dl",
|
|
91
|
+
"dt",
|
|
92
|
+
"figcaption",
|
|
93
|
+
"figure",
|
|
94
|
+
"footer",
|
|
95
|
+
"h1",
|
|
96
|
+
"h2",
|
|
97
|
+
"h3",
|
|
98
|
+
"h4",
|
|
99
|
+
"h5",
|
|
100
|
+
"h6",
|
|
101
|
+
"header",
|
|
102
|
+
"hr",
|
|
103
|
+
"li",
|
|
104
|
+
"main",
|
|
105
|
+
"nav",
|
|
106
|
+
"ol",
|
|
107
|
+
"p",
|
|
108
|
+
"pre",
|
|
109
|
+
"section",
|
|
110
|
+
"table",
|
|
111
|
+
"tbody",
|
|
112
|
+
"td",
|
|
113
|
+
"tfoot",
|
|
114
|
+
"th",
|
|
115
|
+
"thead",
|
|
116
|
+
"tr",
|
|
117
|
+
"ul",
|
|
118
|
+
]);
|
|
119
|
+
/** Elements whose content is never document text. */
|
|
120
|
+
const SKIPPED_TAGS = new Set([
|
|
121
|
+
"audio",
|
|
122
|
+
"button",
|
|
123
|
+
"canvas",
|
|
124
|
+
"embed",
|
|
125
|
+
"head",
|
|
126
|
+
"iframe",
|
|
127
|
+
"img",
|
|
128
|
+
"input",
|
|
129
|
+
"link",
|
|
130
|
+
"meta",
|
|
131
|
+
"noscript",
|
|
132
|
+
"object",
|
|
133
|
+
"script",
|
|
134
|
+
"select",
|
|
135
|
+
"style",
|
|
136
|
+
"svg",
|
|
137
|
+
"template",
|
|
138
|
+
"textarea",
|
|
139
|
+
"title",
|
|
140
|
+
"video",
|
|
141
|
+
]);
|
|
142
|
+
const TAG_MARKS = new Map([
|
|
143
|
+
["b", "bold"],
|
|
144
|
+
["strong", "bold"],
|
|
145
|
+
["i", "italic"],
|
|
146
|
+
["em", "italic"],
|
|
147
|
+
["u", "underline"],
|
|
148
|
+
["ins", "underline"],
|
|
149
|
+
["s", "strike"],
|
|
150
|
+
["strike", "strike"],
|
|
151
|
+
["del", "strike"],
|
|
152
|
+
]);
|
|
153
|
+
/** Marks of an element on top of the inherited ones: tags first, then inline styles. */
|
|
154
|
+
function marksOf(element, inherited) {
|
|
155
|
+
const marks = new Set(inherited);
|
|
156
|
+
const tagMark = TAG_MARKS.get(element.localName);
|
|
157
|
+
if (tagMark)
|
|
158
|
+
marks.add(tagMark);
|
|
159
|
+
const style = element.style;
|
|
160
|
+
if (!style)
|
|
161
|
+
return marks;
|
|
162
|
+
const weight = style.fontWeight;
|
|
163
|
+
// Google Docs wraps everything in <b style="font-weight:normal">.
|
|
164
|
+
if (/^(bold|bolder|[6-9]00)$/.test(weight))
|
|
165
|
+
marks.add("bold");
|
|
166
|
+
else if (/^(normal|lighter|[1-5]00)$/.test(weight))
|
|
167
|
+
marks.delete("bold");
|
|
168
|
+
if (/^(italic|oblique)/.test(style.fontStyle))
|
|
169
|
+
marks.add("italic");
|
|
170
|
+
else if (style.fontStyle === "normal")
|
|
171
|
+
marks.delete("italic");
|
|
172
|
+
const decoration = `${style.textDecorationLine} ${style.textDecoration}`;
|
|
173
|
+
if (decoration.includes("underline"))
|
|
174
|
+
marks.add("underline");
|
|
175
|
+
if (decoration.includes("line-through"))
|
|
176
|
+
marks.add("strike");
|
|
177
|
+
// `text-decoration: none` on a child does not remove a parent's line in CSS: keep it.
|
|
178
|
+
return marks;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* A page break in pasted HTML: Foli's own `<br data-page-break>`, or a `<br>` or `<hr>` styled
|
|
182
|
+
* to break the page (Word writes `<br clear=all style='page-break-before:always'>`, Google Docs
|
|
183
|
+
* `<hr style="page-break-before:always;display:none">`).
|
|
184
|
+
*
|
|
185
|
+
* shortcut: a block styled to break before or after it (`<p style="page-break-before:always">`) loses the break; map it when a real capture needs it.
|
|
186
|
+
*/
|
|
187
|
+
function isPageBreak(element) {
|
|
188
|
+
if (element.localName !== "br" && element.localName !== "hr")
|
|
189
|
+
return false;
|
|
190
|
+
if (element.hasAttribute(PAGE_BREAK_ATTR))
|
|
191
|
+
return true;
|
|
192
|
+
const style = element.getAttribute("style") ?? "";
|
|
193
|
+
// Anchored on a property start, so Word's `mso-column-break-before` (a column break) is not one.
|
|
194
|
+
return /(?:^|[;\s])(?:page-)?break-(?:before|after)\s*:\s*(?:always|page)/i.test(style);
|
|
195
|
+
}
|
|
196
|
+
/** Hidden text, Word's list bullets and empty-paragraph fillers. */
|
|
197
|
+
function isNoise(element) {
|
|
198
|
+
const style = element.getAttribute("style") ?? "";
|
|
199
|
+
if (/mso-list:\s*ignore|mso-hide:\s*all/i.test(style))
|
|
200
|
+
return true;
|
|
201
|
+
if (element.style?.display === "none" || element.hasAttribute("hidden")) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
return element.localName === "o:p" && !element.textContent?.replace(/[\s ]/g, "");
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Own HTML reader: maps known tags and inline styles to schema v1, drops everything else
|
|
208
|
+
* (links keep their text). The HTML is parsed into an inert document and never inserted.
|
|
209
|
+
*/
|
|
210
|
+
export function htmlToDoc(html) {
|
|
211
|
+
return readHTML(html)?.content ?? null;
|
|
212
|
+
}
|
|
213
|
+
/** `htmlToDoc`, plus whether the HTML had no block elements (`Pasted.open`). */
|
|
214
|
+
function readHTML(html) {
|
|
215
|
+
const parsed = new DOMParser().parseFromString(html, "text/html");
|
|
216
|
+
const blocks = [];
|
|
217
|
+
let inline = null;
|
|
218
|
+
let blockType = "paragraph";
|
|
219
|
+
let level = 1;
|
|
220
|
+
let blockPre = false;
|
|
221
|
+
let sawBlock = false;
|
|
222
|
+
let afterSpace = true; // collapse leading and repeated whitespace like HTML layout does
|
|
223
|
+
const open = (type, headingLevel = 1, pre = false) => {
|
|
224
|
+
inline = [];
|
|
225
|
+
blockType = type;
|
|
226
|
+
level = headingLevel;
|
|
227
|
+
blockPre = pre;
|
|
228
|
+
afterSpace = true;
|
|
229
|
+
};
|
|
230
|
+
const close = (keepEmpty) => {
|
|
231
|
+
if (!inline)
|
|
232
|
+
return;
|
|
233
|
+
const last = inline[inline.length - 1];
|
|
234
|
+
if (last?.type === "hardBreak")
|
|
235
|
+
inline.pop(); // a trailing <br> adds no line in HTML
|
|
236
|
+
const tail = inline[inline.length - 1];
|
|
237
|
+
if (tail?.type === "text" && !blockPre)
|
|
238
|
+
tail.text = tail.text.replace(/ +$/, "");
|
|
239
|
+
// Non-breaking spaces survive the trimming above, then become ordinary spaces.
|
|
240
|
+
for (const node of inline) {
|
|
241
|
+
if (node.type === "text")
|
|
242
|
+
node.text = node.text.replace(/\u00a0/g, " ");
|
|
243
|
+
}
|
|
244
|
+
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
|
+
inline = null;
|
|
251
|
+
};
|
|
252
|
+
const addText = (raw, marks, pre) => {
|
|
253
|
+
// Source formatting between blocks ("</p>\n<br>\n<p>") is not content.
|
|
254
|
+
if (!inline && !pre && !/[^ \t\n\r\f]/.test(raw))
|
|
255
|
+
return;
|
|
256
|
+
if (!inline)
|
|
257
|
+
open("paragraph", 1, pre);
|
|
258
|
+
const lines = pre ? raw.replace(/\r\n?/g, "\n").split("\n") : [raw];
|
|
259
|
+
lines.forEach((line, i) => {
|
|
260
|
+
if (i > 0)
|
|
261
|
+
addBreak();
|
|
262
|
+
let text = pre ? line : line.replace(/[ \t\n\r\f]+/g, " ");
|
|
263
|
+
if (!pre && afterSpace)
|
|
264
|
+
text = text.replace(/^ /, "");
|
|
265
|
+
if (!text)
|
|
266
|
+
return;
|
|
267
|
+
afterSpace = !pre && text.endsWith(" ");
|
|
268
|
+
const markJSON = [...marks].map((type) => ({ type }));
|
|
269
|
+
inline.push({
|
|
270
|
+
type: "text",
|
|
271
|
+
text,
|
|
272
|
+
...(markJSON.length ? { marks: markJSON } : {}),
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
};
|
|
276
|
+
/** An explicit page break: in the open block, else at the end of the previous one. */
|
|
277
|
+
const addPageBreak = () => {
|
|
278
|
+
const previous = blocks[blocks.length - 1];
|
|
279
|
+
if (inline)
|
|
280
|
+
inline.push({ type: "pageBreak" });
|
|
281
|
+
else if (previous)
|
|
282
|
+
previous.content = [...(previous.content ?? []), { type: "pageBreak" }];
|
|
283
|
+
else {
|
|
284
|
+
open("paragraph");
|
|
285
|
+
inline?.push({ type: "pageBreak" });
|
|
286
|
+
}
|
|
287
|
+
afterSpace = true;
|
|
288
|
+
};
|
|
289
|
+
const addBreak = () => {
|
|
290
|
+
// A <br> between blocks is an empty line (Google Docs writes empty paragraphs this way).
|
|
291
|
+
if (!inline) {
|
|
292
|
+
blocks.push({ type: "paragraph" });
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const list = inline;
|
|
296
|
+
const tail = list[list.length - 1];
|
|
297
|
+
if (tail?.type === "text")
|
|
298
|
+
tail.text = tail.text.replace(/ +$/, "");
|
|
299
|
+
list.push({ type: "hardBreak" });
|
|
300
|
+
afterSpace = true;
|
|
301
|
+
};
|
|
302
|
+
/** Walks `node`; returns whether it contained a block element. */
|
|
303
|
+
const walk = (node, marks, pre) => {
|
|
304
|
+
if (node.nodeType === 3) {
|
|
305
|
+
addText(node.data, marks, pre);
|
|
306
|
+
return false;
|
|
307
|
+
}
|
|
308
|
+
if (node.nodeType !== 1)
|
|
309
|
+
return false; // comments (Word's conditional comments), etc.
|
|
310
|
+
const element = node;
|
|
311
|
+
const tag = element.localName;
|
|
312
|
+
// Before the noise check: Google Docs hides its page-break <hr> (display: none).
|
|
313
|
+
if (isPageBreak(element)) {
|
|
314
|
+
addPageBreak();
|
|
315
|
+
return false;
|
|
316
|
+
}
|
|
317
|
+
if (SKIPPED_TAGS.has(tag) || isNoise(element))
|
|
318
|
+
return false;
|
|
319
|
+
if (tag === "br") {
|
|
320
|
+
// Chrome marks "the copied range ended with a line break" this way; it is not content.
|
|
321
|
+
if (!element.classList.contains("Apple-interchange-newline"))
|
|
322
|
+
addBreak();
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
const childMarks = marksOf(element, marks);
|
|
326
|
+
const whiteSpace = element.style?.whiteSpace ?? "";
|
|
327
|
+
const childPre = pre || tag === "pre" || whiteSpace.startsWith("pre");
|
|
328
|
+
if (!BLOCK_TAGS.has(tag)) {
|
|
329
|
+
let hasBlock = false;
|
|
330
|
+
for (const child of element.childNodes)
|
|
331
|
+
hasBlock = walk(child, childMarks, childPre) || hasBlock;
|
|
332
|
+
return hasBlock;
|
|
333
|
+
}
|
|
334
|
+
sawBlock = true;
|
|
335
|
+
close(false);
|
|
336
|
+
const heading = /^h([1-6])$/.exec(tag);
|
|
337
|
+
open(heading ? "heading" : "paragraph", heading ? Number(heading[1]) : 1, childPre);
|
|
338
|
+
let hasBlock = false;
|
|
339
|
+
for (const child of element.childNodes)
|
|
340
|
+
hasBlock = walk(child, childMarks, childPre) || hasBlock;
|
|
341
|
+
// <p></p> is an empty line; a container such as <div><p>…</p></div> adds none.
|
|
342
|
+
close(!hasBlock && tag !== "hr" && !/^(ol|ul|dl|table|tbody|thead|tfoot|tr)$/.test(tag));
|
|
343
|
+
return true;
|
|
344
|
+
};
|
|
345
|
+
walk(parsed.body, new Set(), false);
|
|
346
|
+
close(false);
|
|
347
|
+
return blocks.length ? { content: { type: "doc", content: blocks }, open: !sawBlock } : null;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OAAO,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;AAC/F,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,KAAK,iBAAiB,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACjE,OAAO,EAAE,UAAU,EAA0B,OAAO,EAAE,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type { DocumentSettings } from "@folijs/core";
|
|
2
|
+
/** CSS pixels per millimetre (CSS fixes 1in = 96px = 25.4mm). */
|
|
3
|
+
export declare const PX_PER_MM: number;
|
|
4
|
+
/** A rendered block's border-box height and vertical margins, in CSS px. */
|
|
5
|
+
export interface BlockBox {
|
|
6
|
+
height: number;
|
|
7
|
+
marginTop: number;
|
|
8
|
+
marginBottom: number;
|
|
9
|
+
/**
|
|
10
|
+
* The bottom of each line box, from the block's top (the last one is `height`). Only called
|
|
11
|
+
* for a block that does not fit where it is, or has page breaks, since measuring lines is
|
|
12
|
+
* expensive.
|
|
13
|
+
*/
|
|
14
|
+
lines(): readonly number[];
|
|
15
|
+
/** Keep on the same page as the start of the next block (headings). */
|
|
16
|
+
keepWithNext?: boolean;
|
|
17
|
+
/** The lines that end with an explicit page break (indices into `lines()`), if any. */
|
|
18
|
+
breaksAfter?(): readonly number[];
|
|
19
|
+
}
|
|
20
|
+
/** The lines `[fromLine, toLine)` of a block, placed on one page (`toLine` Infinity: to the end). */
|
|
21
|
+
export interface Fragment {
|
|
22
|
+
page: number;
|
|
23
|
+
fromLine: number;
|
|
24
|
+
toLine: number;
|
|
25
|
+
}
|
|
26
|
+
export interface FlowRules {
|
|
27
|
+
/** Fewest lines of a split block left at the bottom of a page. */
|
|
28
|
+
orphans: number;
|
|
29
|
+
/** Fewest lines of a split block carried to the top of the next page. */
|
|
30
|
+
widows: number;
|
|
31
|
+
}
|
|
32
|
+
export declare const DEFAULT_RULES: FlowRules;
|
|
33
|
+
/** Height of a page's content box (page height minus top and bottom margins), in CSS px. */
|
|
34
|
+
export declare function contentHeight({ pageSize, margins }: DocumentSettings): number;
|
|
35
|
+
/** Where the flow stands before a block: shared by a full and an incremental flow. */
|
|
36
|
+
export interface FlowState {
|
|
37
|
+
page: number;
|
|
38
|
+
used: number;
|
|
39
|
+
previousMargin: number;
|
|
40
|
+
onPage: number;
|
|
41
|
+
/** An explicit page break ended the previous block: the next one starts a new page. */
|
|
42
|
+
breakPending: boolean;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Page flow: the fragments of each block, in order. Blocks fill a page in order; a block that
|
|
46
|
+
* does not fit is split at a line boundary, the lines that fit staying on the page, the rest
|
|
47
|
+
* continuing at the top of the next one (as many pages as it takes). A page with room for no
|
|
48
|
+
* line of the next block ends; an empty page always takes at least one line.
|
|
49
|
+
*
|
|
50
|
+
* Rules:
|
|
51
|
+
* - Explicit page breaks: the line holding one ends its page.
|
|
52
|
+
* - Widows and orphans: a split leaves at least `orphans` lines at the bottom of a page and
|
|
53
|
+
* carries at least `widows` lines to the next; a block too short for both moves whole. Not
|
|
54
|
+
* applied where only an empty page could hold the lines, nor across an explicit break.
|
|
55
|
+
* - Keep with next: a `keepWithNext` block (and a chain of them) moves to the next page when
|
|
56
|
+
* the block after it would start there, unless it already starts its page or the move would
|
|
57
|
+
* not bring them together (then the flow stays as it was).
|
|
58
|
+
*
|
|
59
|
+
* Spacing follows CSS inside a padded page: the first block keeps its top margin, adjacent
|
|
60
|
+
* margins collapse to the larger, and the last block's bottom margin stays inside the page
|
|
61
|
+
* (it does not collapse into the padding), so it must fit too. Fragments are rendered without
|
|
62
|
+
* the margins at their split edges: a continued fragment has no bottom margin, a continuation
|
|
63
|
+
* no top margin.
|
|
64
|
+
*
|
|
65
|
+
* shortcut: positive margins only (negative margins collapse differently).
|
|
66
|
+
*/
|
|
67
|
+
export declare function flow(boxes: readonly BlockBox[], pageHeight: number, rules?: FlowRules): Fragment[][];
|
|
68
|
+
/** A flow with the state before each block (and after the last), for the next incremental one. */
|
|
69
|
+
export interface FlowResult {
|
|
70
|
+
fragments: Fragment[][];
|
|
71
|
+
states: FlowState[];
|
|
72
|
+
}
|
|
73
|
+
/** What an incremental flow may reuse from the previous one. */
|
|
74
|
+
export interface Reuse {
|
|
75
|
+
previous: FlowResult;
|
|
76
|
+
/** Blocks before this index are the previous flow's first blocks, with the same boxes. */
|
|
77
|
+
from: number;
|
|
78
|
+
/**
|
|
79
|
+
* The previous index of block `i` when it and every block after it are unchanged from the
|
|
80
|
+
* previous flow (the common tail of an edit), else -1.
|
|
81
|
+
*/
|
|
82
|
+
previousIndex(i: number): number;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* `flow`, reusing a previous one: it starts at `reuse.from` (backed up over a chain of
|
|
86
|
+
* keep-with-next blocks, whose placement depends on what follows) with the previous state
|
|
87
|
+
* there, and once the state before an unchanged block equals the previous state before it, the
|
|
88
|
+
* rest is the previous flow's. The result is the same as a full flow.
|
|
89
|
+
*/
|
|
90
|
+
export declare function flowFrom(boxes: readonly BlockBox[], pageHeight: number, rules?: FlowRules, reuse?: Reuse): FlowResult;
|
|
91
|
+
//# sourceMappingURL=flow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flow.d.ts","sourceRoot":"","sources":["../../src/layout/flow.ts"],"names":[],"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