@docen/docx 0.3.1 → 0.3.2
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/dist/converters/docx.d.mts +199 -1
- package/dist/converters/docx.mjs +1273 -1
- package/dist/converters/html.d.mts +28 -1
- package/dist/converters/html.mjs +103 -1
- package/dist/converters/markdown.d.mts +12 -1
- package/dist/converters/markdown.mjs +17 -1
- package/dist/converters/patch.d.mts +52 -1
- package/dist/converters/patch.mjs +35 -1
- package/dist/converters/prepare.d.mts +62 -1
- package/dist/converters/prepare.mjs +121 -1
- package/dist/converters/styles.d.mts +84 -1
- package/dist/converters/styles.mjs +284 -1
- package/dist/core-C9VunJ8o.d.mts +174 -0
- package/dist/core-DT9IrTzN.mjs +670 -0
- package/dist/core.d.mts +1 -1
- package/dist/core.mjs +1 -1
- package/dist/editor.d.mts +20 -1
- package/dist/editor.mjs +20 -1
- package/dist/extensions/blockquote.d.mts +1 -1
- package/dist/extensions/blockquote.mjs +30 -2
- package/dist/extensions/code-block.d.mts +1 -1
- package/dist/extensions/code-block.mjs +22 -1
- package/dist/extensions/column-break.d.mts +1 -1
- package/dist/extensions/column-break.mjs +1 -1
- package/dist/extensions/details.d.mts +1 -1
- package/dist/extensions/details.mjs +17 -2
- package/dist/extensions/document.mjs +49 -1
- package/dist/extensions/extensions.d.mts +3 -4
- package/dist/extensions/extensions.mjs +13 -14
- package/dist/extensions/formatting-marks.d.mts +7 -1
- package/dist/extensions/formatting-marks.mjs +1 -1
- package/dist/extensions/heading.mjs +165 -1
- package/dist/extensions/image.d.mts +1 -1
- package/dist/extensions/image.mjs +395 -1
- package/dist/extensions/index.d.mts +3 -2
- package/dist/extensions/index.mjs +4 -3
- package/dist/extensions/mention.d.mts +1 -1
- package/dist/extensions/mention.mjs +43 -2
- package/dist/extensions/ordered-list.d.mts +1 -1
- package/dist/extensions/ordered-list.mjs +40 -2
- package/dist/extensions/page-break.d.mts +1 -33
- package/dist/extensions/page-break.mjs +1 -1
- package/dist/extensions/paragraph.mjs +150 -1
- package/dist/extensions/passthrough.d.mts +1 -1
- package/dist/extensions/passthrough.mjs +1 -1
- package/dist/extensions/section-break.d.mts +19 -2
- package/dist/extensions/section-break.mjs +1 -1
- package/dist/extensions/strike.mjs +49 -1
- package/dist/extensions/table-cell.mjs +111 -1
- package/dist/extensions/table-header.mjs +111 -1
- package/dist/extensions/table-row.mjs +83 -1
- package/dist/extensions/table.mjs +159 -1
- package/dist/extensions/task-item.d.mts +1 -1
- package/dist/extensions/task-item.mjs +36 -2
- package/dist/extensions/text-style.mjs +150 -1
- package/dist/extensions/tiptap.d.mts +2 -2
- package/dist/extensions/tiptap.mjs +31 -2
- package/dist/extensions/utils.d.mts +2 -62
- package/dist/extensions/utils.mjs +499 -2
- package/dist/extensions/wpg-group.d.mts +2 -0
- package/dist/extensions/wpg-group.mjs +2 -0
- package/dist/extensions/wps-shape.d.mts +2 -0
- package/dist/extensions/wps-shape.mjs +2 -0
- package/dist/index.d.mts +86 -27
- package/dist/index.mjs +13 -10
- package/dist/{tiptap-TErPjuNJ.d.mts → tiptap-pZsNPsvV.d.mts} +1 -3
- package/dist/utils-D87vukzp.d.mts +128 -0
- package/package.json +39 -40
- package/dist/blockquote-Ky9xD_Q-.mjs +0 -31
- package/dist/code-block-BgNjhADy.mjs +0 -23
- package/dist/core-DEqBHRzE.mjs +0 -406
- package/dist/core-fBMCCj4D.d.mts +0 -97
- package/dist/details-Dh8luWb2.mjs +0 -18
- package/dist/dist-B6B3HnVZ.mjs +0 -1838
- package/dist/document-CvTym-FM.mjs +0 -50
- package/dist/docx-C5wyjI-0.d.mts +0 -200
- package/dist/docx-CONyAedl.mjs +0 -1231
- package/dist/editor-Bf3CxQnY.mjs +0 -21
- package/dist/editor-DReY3Rb9.d.mts +0 -21
- package/dist/emoji-BZ1dJD7d.d.mts +0 -17
- package/dist/emoji-C2OsTWR7.mjs +0 -27
- package/dist/extensions/emoji.d.mts +0 -2
- package/dist/extensions/emoji.mjs +0 -2
- package/dist/extensions/image-group.d.mts +0 -6
- package/dist/extensions/image-group.mjs +0 -2
- package/dist/heading-DAubgJJN.mjs +0 -166
- package/dist/html-DCC_DSHv.d.mts +0 -18
- package/dist/html-DedDWPoO.mjs +0 -66
- package/dist/image-CuEIXoZl.mjs +0 -334
- package/dist/markdown-BzJmRsBs.mjs +0 -18
- package/dist/markdown-D0k0sRYS.d.mts +0 -13
- package/dist/mention-C-dTR6MK.mjs +0 -44
- package/dist/ordered-list-V-3xFQmV.mjs +0 -41
- package/dist/paragraph-BrWH-BOV.mjs +0 -147
- package/dist/patch-Cdp5WKJz.d.mts +0 -53
- package/dist/patch-IZ13tXTu.mjs +0 -36
- package/dist/prepare-Bdk8Cjkf.mjs +0 -122
- package/dist/prepare-CPAvcv6q.d.mts +0 -63
- package/dist/strike-hWxShOgb.mjs +0 -50
- package/dist/styles-QSX3RhxK.mjs +0 -285
- package/dist/styles-ugspkrxz.d.mts +0 -85
- package/dist/table-DrA12lxX.mjs +0 -159
- package/dist/table-cell-DmkfYwDc.mjs +0 -112
- package/dist/table-header-Bwr5GKWg.mjs +0 -112
- package/dist/table-row-vew1y7WH.mjs +0 -84
- package/dist/task-item-BnlXGrIG.mjs +0 -37
- package/dist/text-style-tMeBq5XQ.mjs +0 -151
- package/dist/tiptap-D7pl4I5U.mjs +0 -33
- package/dist/utils-BPT4tjcn.mjs +0 -333
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { t as encodeBase64 } from "./dist-B6B3HnVZ.mjs";
|
|
2
|
-
import { imageMeta } from "image-meta";
|
|
3
|
-
//#region src/converters/prepare.ts
|
|
4
|
-
/**
|
|
5
|
-
* Default fetch handler using the global `fetch` API (Node 18+ and browsers).
|
|
6
|
-
*/
|
|
7
|
-
async function fetchImageHandler(url) {
|
|
8
|
-
const response = await fetch(url);
|
|
9
|
-
if (!response.ok) throw new Error(`Failed to fetch image from ${url}: ${response.status} ${response.statusText}`);
|
|
10
|
-
const buffer = await response.arrayBuffer();
|
|
11
|
-
return new Uint8Array(buffer);
|
|
12
|
-
}
|
|
13
|
-
/**
|
|
14
|
-
* Create a prepare step that fetches external image URLs and converts them to data URLs.
|
|
15
|
-
*
|
|
16
|
-
* @param handler - Custom fetch handler (defaults to `fetchImageHandler`)
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* // Use with defaults
|
|
21
|
-
* await prepareDocument(json);
|
|
22
|
-
*
|
|
23
|
-
* // Custom handler
|
|
24
|
-
* await prepareDocument(json, [prepareImages(myHandler)]);
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
function prepareImages(handler = fetchImageHandler) {
|
|
28
|
-
return async (json) => {
|
|
29
|
-
await walkImages(json, handler);
|
|
30
|
-
};
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Create a prepare step that fills in missing image dimensions by reading image
|
|
34
|
-
* metadata. Intended to run after {@link prepareImages} so HTTP sources are
|
|
35
|
-
* already embedded as data URLs.
|
|
36
|
-
*
|
|
37
|
-
* Only images lacking `width` or `height` are probed — the DOCX round-trip path
|
|
38
|
-
* already carries `transformation.width/height`, so this mainly serves images
|
|
39
|
-
* entering via HTML/Markdown (which carry no intrinsic size). Images whose
|
|
40
|
-
* metadata can't be read are left untouched; `renderDocx` falls back to 600×400
|
|
41
|
-
* (see extensions/image.ts).
|
|
42
|
-
*/
|
|
43
|
-
function prepareImageSizes() {
|
|
44
|
-
return async (json) => {
|
|
45
|
-
await walkImageSizes(json);
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
/** Built-in prepare steps, run when no custom steps are provided. */
|
|
49
|
-
const DEFAULT_STEPS = [prepareImages(), prepareImageSizes()];
|
|
50
|
-
/**
|
|
51
|
-
* Run prepare steps on a Tiptap JSON document before compilation.
|
|
52
|
-
*
|
|
53
|
-
* Each step receives the JSON and may mutate it in place (e.g. replace external
|
|
54
|
-
* URLs with embedded data). Steps run sequentially in order.
|
|
55
|
-
*
|
|
56
|
-
* Defaults to `[prepareImages(), prepareImageSizes()]` when no steps are provided.
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```ts
|
|
60
|
-
* const json = parseHTML(html);
|
|
61
|
-
* await prepareDocument(json); // default: fetch images
|
|
62
|
-
* const docOpts = compileDocument(json);
|
|
63
|
-
* ```
|
|
64
|
-
*/
|
|
65
|
-
async function prepareDocument(json, steps = DEFAULT_STEPS) {
|
|
66
|
-
for (const step of steps) await step(json);
|
|
67
|
-
}
|
|
68
|
-
async function toDataUrl(src, handler) {
|
|
69
|
-
const data = await handler(src);
|
|
70
|
-
return `data:${{
|
|
71
|
-
jpg: "image/jpeg",
|
|
72
|
-
jpeg: "image/jpeg",
|
|
73
|
-
png: "image/png",
|
|
74
|
-
gif: "image/gif",
|
|
75
|
-
bmp: "image/bmp",
|
|
76
|
-
svg: "image/svg+xml",
|
|
77
|
-
webp: "image/webp"
|
|
78
|
-
}[src.split(".").pop()?.toLowerCase() ?? "png"] ?? "image/png"};base64,${encodeBase64(data)}`;
|
|
79
|
-
}
|
|
80
|
-
async function walkImages(node, handler) {
|
|
81
|
-
if (node.type === "image" && node.attrs) {
|
|
82
|
-
const src = node.attrs.src;
|
|
83
|
-
if (src && (src.startsWith("http://") || src.startsWith("https://"))) try {
|
|
84
|
-
node.attrs.src = await toDataUrl(src, handler);
|
|
85
|
-
} catch (error) {
|
|
86
|
-
console.warn(`Failed to fetch image: ${src}`, error instanceof Error ? error.message : error);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
const tasks = [];
|
|
90
|
-
for (const child of node.content ?? []) tasks.push(walkImages(child, handler));
|
|
91
|
-
await Promise.all(tasks);
|
|
92
|
-
}
|
|
93
|
-
async function walkImageSizes(node) {
|
|
94
|
-
if (node.type === "image" && node.attrs) {
|
|
95
|
-
const attrs = node.attrs;
|
|
96
|
-
const needsWidth = attrs.width == null;
|
|
97
|
-
const needsHeight = attrs.height == null;
|
|
98
|
-
if (needsWidth || needsHeight) {
|
|
99
|
-
const bytes = decodeDataUrl(attrs.src);
|
|
100
|
-
if (bytes) try {
|
|
101
|
-
const meta = imageMeta(bytes);
|
|
102
|
-
if (needsWidth && typeof meta.width === "number") attrs.width = meta.width;
|
|
103
|
-
if (needsHeight && typeof meta.height === "number") attrs.height = meta.height;
|
|
104
|
-
} catch {}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
const tasks = [];
|
|
108
|
-
for (const child of node.content ?? []) tasks.push(walkImageSizes(child));
|
|
109
|
-
await Promise.all(tasks);
|
|
110
|
-
}
|
|
111
|
-
/** Decode a `data:image/...;base64,...` URL to bytes; null if not a data URL. */
|
|
112
|
-
function decodeDataUrl(src) {
|
|
113
|
-
if (!src) return null;
|
|
114
|
-
const match = src.match(/^data:image\/[\w.+-]+;base64,(.+)$/);
|
|
115
|
-
if (!match) return null;
|
|
116
|
-
const binary = atob(match[1]);
|
|
117
|
-
const bytes = new Uint8Array(binary.length);
|
|
118
|
-
for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
|
|
119
|
-
return bytes;
|
|
120
|
-
}
|
|
121
|
-
//#endregion
|
|
122
|
-
export { prepareImages as i, prepareDocument as n, prepareImageSizes as r, fetchImageHandler as t };
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { a as JSONContent } from "./core-fBMCCj4D.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/converters/prepare.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* A prepare step that transforms Tiptap JSON in place (e.g. fetch external resources).
|
|
6
|
-
*/
|
|
7
|
-
type PrepareStep = (json: JSONContent) => Promise<void>;
|
|
8
|
-
/**
|
|
9
|
-
* Fetch handler for external image URLs.
|
|
10
|
-
*
|
|
11
|
-
* Receives the URL, returns the image binary data.
|
|
12
|
-
* Override to customize fetching (proxy, auth, caching, etc.).
|
|
13
|
-
*/
|
|
14
|
-
type ImageFetchHandler = (url: string) => Promise<Uint8Array>;
|
|
15
|
-
/**
|
|
16
|
-
* Default fetch handler using the global `fetch` API (Node 18+ and browsers).
|
|
17
|
-
*/
|
|
18
|
-
declare function fetchImageHandler(url: string): Promise<Uint8Array>;
|
|
19
|
-
/**
|
|
20
|
-
* Create a prepare step that fetches external image URLs and converts them to data URLs.
|
|
21
|
-
*
|
|
22
|
-
* @param handler - Custom fetch handler (defaults to `fetchImageHandler`)
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* // Use with defaults
|
|
27
|
-
* await prepareDocument(json);
|
|
28
|
-
*
|
|
29
|
-
* // Custom handler
|
|
30
|
-
* await prepareDocument(json, [prepareImages(myHandler)]);
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
declare function prepareImages(handler?: ImageFetchHandler): PrepareStep;
|
|
34
|
-
/**
|
|
35
|
-
* Create a prepare step that fills in missing image dimensions by reading image
|
|
36
|
-
* metadata. Intended to run after {@link prepareImages} so HTTP sources are
|
|
37
|
-
* already embedded as data URLs.
|
|
38
|
-
*
|
|
39
|
-
* Only images lacking `width` or `height` are probed — the DOCX round-trip path
|
|
40
|
-
* already carries `transformation.width/height`, so this mainly serves images
|
|
41
|
-
* entering via HTML/Markdown (which carry no intrinsic size). Images whose
|
|
42
|
-
* metadata can't be read are left untouched; `renderDocx` falls back to 600×400
|
|
43
|
-
* (see extensions/image.ts).
|
|
44
|
-
*/
|
|
45
|
-
declare function prepareImageSizes(): PrepareStep;
|
|
46
|
-
/**
|
|
47
|
-
* Run prepare steps on a Tiptap JSON document before compilation.
|
|
48
|
-
*
|
|
49
|
-
* Each step receives the JSON and may mutate it in place (e.g. replace external
|
|
50
|
-
* URLs with embedded data). Steps run sequentially in order.
|
|
51
|
-
*
|
|
52
|
-
* Defaults to `[prepareImages(), prepareImageSizes()]` when no steps are provided.
|
|
53
|
-
*
|
|
54
|
-
* @example
|
|
55
|
-
* ```ts
|
|
56
|
-
* const json = parseHTML(html);
|
|
57
|
-
* await prepareDocument(json); // default: fetch images
|
|
58
|
-
* const docOpts = compileDocument(json);
|
|
59
|
-
* ```
|
|
60
|
-
*/
|
|
61
|
-
declare function prepareDocument(json: JSONContent, steps?: readonly PrepareStep[]): Promise<void>;
|
|
62
|
-
//#endregion
|
|
63
|
-
export { prepareImageSizes as a, prepareDocument as i, PrepareStep as n, prepareImages as o, fetchImageHandler as r, ImageFetchHandler as t };
|
package/dist/strike-hWxShOgb.mjs
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { E as Strike$1 } from "./tiptap-D7pl4I5U.mjs";
|
|
2
|
-
//#region src/extensions/strike.ts
|
|
3
|
-
/**
|
|
4
|
-
* Strike mark extension with nested office-open attrs.
|
|
5
|
-
*
|
|
6
|
-
* OOXML represents strikethrough on a run via two mutually exclusive booleans:
|
|
7
|
-
* `strike` (single) and `doubleStrike` (double). The mark itself is "single
|
|
8
|
-
* strikethrough"; the `doubleStrike` attr flips it to double. DOCX round-trip
|
|
9
|
-
* is near-identity for the doubleStrike flag; `strike` itself is implied by the
|
|
10
|
-
* mark's presence and handled in renderDocx/parseDocx.
|
|
11
|
-
*
|
|
12
|
-
* Mark attribute-level renderHTML is delegated to the base Strike extension
|
|
13
|
-
* (renders `<s>`); only the DOCX flag needs custom handling.
|
|
14
|
-
*/
|
|
15
|
-
/**
|
|
16
|
-
* attrs → run properties.
|
|
17
|
-
*
|
|
18
|
-
* The mark presence itself means single strikethrough. When `doubleStrike` is
|
|
19
|
-
* set, emit the OOXML double-strike flag instead (the two are mutually
|
|
20
|
-
* exclusive in OOXML). DocxManager calls this with `mark.attrs`.
|
|
21
|
-
*/
|
|
22
|
-
function renderDocx(attrs) {
|
|
23
|
-
return attrs.doubleStrike ? { doubleStrike: true } : { strike: true };
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* run properties → attrs.
|
|
27
|
-
*
|
|
28
|
-
* Only the `doubleStrike` flag needs to round-trip; single strike is implied by
|
|
29
|
-
* the mark's existence. `strike` is structural/semantic (the mark) and skipped.
|
|
30
|
-
*/
|
|
31
|
-
function parseDocx(runOpts) {
|
|
32
|
-
return { doubleStrike: runOpts.doubleStrike ?? null };
|
|
33
|
-
}
|
|
34
|
-
const attrNative = () => ({
|
|
35
|
-
default: null,
|
|
36
|
-
parseHTML: () => null,
|
|
37
|
-
rendered: false
|
|
38
|
-
});
|
|
39
|
-
const Strike = Strike$1.extend({
|
|
40
|
-
addAttributes() {
|
|
41
|
-
return {
|
|
42
|
-
...this.parent?.(),
|
|
43
|
-
doubleStrike: attrNative()
|
|
44
|
-
};
|
|
45
|
-
},
|
|
46
|
-
renderDocx,
|
|
47
|
-
parseDocx
|
|
48
|
-
});
|
|
49
|
-
//#endregion
|
|
50
|
-
export { parseDocx as n, renderDocx as r, Strike as t };
|
package/dist/styles-QSX3RhxK.mjs
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
import { f as renderParagraphStyles, h as resolveFontName, p as renderRunStyles } from "./utils-BPT4tjcn.mjs";
|
|
2
|
-
//#region src/converters/styles.ts
|
|
3
|
-
/** Escape a style id for safe use in a CSS class selector. OOXML style ids are
|
|
4
|
-
* NCNames so this is rarely needed, but it keeps arbitrary ids safe. */
|
|
5
|
-
function escapeClass(id) {
|
|
6
|
-
return id.replace(/[^A-Za-z0-9_-]/g, (ch) => `\\${ch}`);
|
|
7
|
-
}
|
|
8
|
-
/** The pStyle val (class hook `docx-style-{id}`) for a built-in named style
|
|
9
|
-
* nested under DefaultStylesOptions: the key with its first letter upper-cased
|
|
10
|
-
* ("heading1" → "Heading1", "title" → "Title", "listParagraph" → "ListParagraph").
|
|
11
|
-
* This matches office-open's HeadingLevel literals / pStyle ids, so we derive
|
|
12
|
-
* the id from the key instead of hard-coding a name table. */
|
|
13
|
-
function pStyleIdFromKey(key) {
|
|
14
|
-
return key.charAt(0).toUpperCase() + key.slice(1);
|
|
15
|
-
}
|
|
16
|
-
/** The styleId of the document's default paragraph style (`w:default="1"`
|
|
17
|
-
* type="paragraph") — the implicit style applied to every paragraph WITHOUT an
|
|
18
|
-
* explicit pStyle. OOXML renders a pStyle-less paragraph as this style (usually
|
|
19
|
-
* "Normal"), so the editor's `.docx-default` class — emitted by Paragraph/
|
|
20
|
-
* Heading renderHTML on a styleId-less node — targets it. Searched in
|
|
21
|
-
* `paragraphStyles` and the built-in named styles nested under `default`
|
|
22
|
-
* (key → pStyle id). null when the document declares none. */
|
|
23
|
-
function defaultParagraphStyleId(styles) {
|
|
24
|
-
if (!styles) return null;
|
|
25
|
-
for (const ps of styles.paragraphStyles ?? []) if (ps.default) return ps.id;
|
|
26
|
-
const defaults = styles.default;
|
|
27
|
-
for (const [key, style] of Object.entries(defaults ?? {})) {
|
|
28
|
-
if (key === "document" || !style) continue;
|
|
29
|
-
if (style.default) return pStyleIdFromKey(key);
|
|
30
|
-
}
|
|
31
|
-
return null;
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* Generate scoped CSS from a document's `styles.xml` model (`StylesOptions`) so
|
|
35
|
-
* named paragraph/character styles and the document defaults render correctly
|
|
36
|
-
* in the editor. All rules are scoped to `.docen-page` (the editor's page
|
|
37
|
-
* surface); the editor injects the result into a `<style>` after load.
|
|
38
|
-
*
|
|
39
|
-
* - `default.document` → `.docen-page` base run + paragraph defaults (the doc's
|
|
40
|
-
* default font/size, line spacing, …).
|
|
41
|
-
* - `paragraphStyles[]` → `.docen-page .docx-style-{id}` (run + paragraph CSS).
|
|
42
|
-
* Paragraph nodes carry `class="docx-style-{styleId}"` (see the Paragraph /
|
|
43
|
-
* Heading extensions' pStyle ↔ styleId round-trip).
|
|
44
|
-
* - `characterStyles[]` → `.docen-page .docx-char-{id}` (run CSS).
|
|
45
|
-
*
|
|
46
|
-
* `basedOn` inheritance is deep-merged per-property into each named style's
|
|
47
|
-
* rule (root first, child overrides) — see mergeStyleChain. CSS inheritance
|
|
48
|
-
* follows the DOM tree, not a style's basedOn, so each `.docx-style-{id}` rule
|
|
49
|
-
* carries its full ancestor chain rather than relying on source order.
|
|
50
|
-
*/
|
|
51
|
-
function stylesToCss(styles, scope) {
|
|
52
|
-
if (!styles) return "";
|
|
53
|
-
const rules = [];
|
|
54
|
-
const within = scope ? `${scope} ` : "";
|
|
55
|
-
const doc = styles.default?.document;
|
|
56
|
-
if (doc) {
|
|
57
|
-
const runDecls = doc.run ? renderRunStyles(doc.run) : [];
|
|
58
|
-
if (runDecls.length && scope) rules.push(`${scope} { ${runDecls.join(";")} }`);
|
|
59
|
-
const paraDecls = doc.paragraph ? renderParagraphStyles(doc.paragraph) : [];
|
|
60
|
-
if (paraDecls.length) {
|
|
61
|
-
const paraSel = `${within}p, ${within}h1, ${within}h2, ${within}h3, ${within}h4, ${within}h5, ${within}h6`;
|
|
62
|
-
rules.push(`${paraSel} { ${paraDecls.join(";")} }`);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
const byId = indexParagraphStyles(styles);
|
|
66
|
-
const defaultId = defaultParagraphStyleId(styles);
|
|
67
|
-
for (const id of byId.keys()) {
|
|
68
|
-
const { run, paragraph } = mergeStyleChain(byId, id);
|
|
69
|
-
const decls = [];
|
|
70
|
-
if (Object.keys(run).length) decls.push(...renderRunStyles(run));
|
|
71
|
-
if (Object.keys(paragraph).length) decls.push(...renderParagraphStyles(paragraph));
|
|
72
|
-
if (!decls.length) continue;
|
|
73
|
-
const selector = id === defaultId ? `${within}.docx-default, ${within}.docx-style-${escapeClass(id)}` : `${within}.docx-style-${escapeClass(id)}`;
|
|
74
|
-
rules.push(`${selector} { ${decls.join(";")} }`);
|
|
75
|
-
}
|
|
76
|
-
for (const cs of styles.characterStyles ?? []) {
|
|
77
|
-
const decls = [];
|
|
78
|
-
if (cs.run) decls.push(...renderRunStyles(cs.run));
|
|
79
|
-
if (decls.length) rules.push(`${within}.docx-char-${escapeClass(cs.id)} { ${decls.join(";")} }`);
|
|
80
|
-
}
|
|
81
|
-
return rules.join("\n");
|
|
82
|
-
}
|
|
83
|
-
/** Build an id → style-entry index over every paragraph style: the explicit
|
|
84
|
-
* `paragraphStyles` plus the built-in named styles nested under `default`
|
|
85
|
-
* (key → pStyle id via pStyleIdFromKey). `document` is docDefaults, not a
|
|
86
|
-
* named style, so it is excluded. A built-in that also appears in
|
|
87
|
-
* paragraphStyles is deduped by id (paragraphStyles wins on insertion order). */
|
|
88
|
-
function indexParagraphStyles(styles) {
|
|
89
|
-
const byId = /* @__PURE__ */ new Map();
|
|
90
|
-
for (const ps of styles.paragraphStyles ?? []) byId.set(ps.id, ps);
|
|
91
|
-
const defaults = styles.default;
|
|
92
|
-
for (const [key, style] of Object.entries(defaults ?? {})) {
|
|
93
|
-
if (key === "document" || !style) continue;
|
|
94
|
-
byId.set(pStyleIdFromKey(key), style);
|
|
95
|
-
}
|
|
96
|
-
return byId;
|
|
97
|
-
}
|
|
98
|
-
/** Merge a paragraph style's run/paragraph properties with its `basedOn` chain
|
|
99
|
-
* (root first, child overrides per-property) — the OOXML inheritance model. A
|
|
100
|
-
* flat property-level merge: a child property overrides the parent's; an unset
|
|
101
|
-
* property is inherited. Shared by stylesToCss (rendering) and
|
|
102
|
-
* effectiveRunProps (the caret resolver) so the gallery box and the rendered
|
|
103
|
-
* page resolve identical values. */
|
|
104
|
-
function mergeStyleChain(byId, styleId) {
|
|
105
|
-
const chain = [];
|
|
106
|
-
const visited = /* @__PURE__ */ new Set();
|
|
107
|
-
let curId = styleId || void 0;
|
|
108
|
-
while (curId && !visited.has(curId)) {
|
|
109
|
-
visited.add(curId);
|
|
110
|
-
const style = byId.get(curId);
|
|
111
|
-
if (!style) break;
|
|
112
|
-
chain.unshift(style);
|
|
113
|
-
curId = style.basedOn ?? void 0;
|
|
114
|
-
}
|
|
115
|
-
const run = {};
|
|
116
|
-
const paragraph = {};
|
|
117
|
-
for (const style of chain) {
|
|
118
|
-
const s = style;
|
|
119
|
-
if (s.run) Object.assign(run, s.run);
|
|
120
|
-
if (s.paragraph) Object.assign(paragraph, s.paragraph);
|
|
121
|
-
}
|
|
122
|
-
return {
|
|
123
|
-
run,
|
|
124
|
-
paragraph
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
/** The `DefaultStylesOptions` keys whose values are character styles, not
|
|
128
|
-
* paragraph styles. The Quick Styles gallery is paragraph-only, so these are
|
|
129
|
-
* excluded even when flagged `quickFormat` (the authoritative source is
|
|
130
|
-
* office-open's DefaultStylesOptions interface). */
|
|
131
|
-
const CHARACTER_DEFAULT_KEYS = new Set([
|
|
132
|
-
"hyperlink",
|
|
133
|
-
"footnoteReference",
|
|
134
|
-
"footnoteTextChar",
|
|
135
|
-
"endnoteReference",
|
|
136
|
-
"endnoteTextChar"
|
|
137
|
-
]);
|
|
138
|
-
/**
|
|
139
|
-
* The paragraph styles to list in the Quick Styles gallery, matching Word's
|
|
140
|
-
* default behavior: the gallery is a *paragraph-style* selector (it applies a
|
|
141
|
-
* pStyle), so only paragraph styles appear — never character styles, even
|
|
142
|
-
* those flagged `quickFormat` (those live in the Styles task pane). Among
|
|
143
|
-
* paragraph styles, only those flagged `quickFormat` are listed, ordered by
|
|
144
|
-
* `uiPriority` (Word orders the gallery this way).
|
|
145
|
-
*
|
|
146
|
-
* Reads `quickFormat`/`uiPriority`/`name` straight from office-open's styles
|
|
147
|
-
* model (`StylesOptions`): `paragraphStyles` (Normal + custom) and the built-in
|
|
148
|
-
* named paragraph styles nested under `default` (title/heading1-9/quote/…). The
|
|
149
|
-
* `default` keys that hold character styles are excluded via
|
|
150
|
-
* `CHARACTER_DEFAULT_KEYS`. When a document carries no quickFormat flags at all
|
|
151
|
-
* (e.g. some LibreOffice-generated files), fall back to all paragraph styles so
|
|
152
|
-
* the gallery is never empty.
|
|
153
|
-
*/
|
|
154
|
-
function quickStyles(styles) {
|
|
155
|
-
if (!styles) return [];
|
|
156
|
-
const all = [];
|
|
157
|
-
const seen = /* @__PURE__ */ new Set();
|
|
158
|
-
const push = (id, style) => {
|
|
159
|
-
if (seen.has(id)) return;
|
|
160
|
-
seen.add(id);
|
|
161
|
-
all.push({
|
|
162
|
-
id,
|
|
163
|
-
name: style.name || id,
|
|
164
|
-
uiPriority: style.uiPriority ?? 9999,
|
|
165
|
-
quick: !!style.quickFormat
|
|
166
|
-
});
|
|
167
|
-
};
|
|
168
|
-
for (const ps of styles.paragraphStyles ?? []) push(ps.id, ps);
|
|
169
|
-
const defaults = styles.default;
|
|
170
|
-
for (const [key, style] of Object.entries(defaults)) {
|
|
171
|
-
if (key === "document" || CHARACTER_DEFAULT_KEYS.has(key) || !style) continue;
|
|
172
|
-
push(pStyleIdFromKey(key), style);
|
|
173
|
-
}
|
|
174
|
-
const byPriority = (a, b) => a.uiPriority - b.uiPriority;
|
|
175
|
-
const quick = all.filter((s) => s.quick).sort(byPriority);
|
|
176
|
-
return (quick.length > 0 ? quick : all).map(({ id, name }) => ({
|
|
177
|
-
id,
|
|
178
|
-
name
|
|
179
|
-
}));
|
|
180
|
-
}
|
|
181
|
-
/** Resolve the effective run-level properties (font name, size in points) at the
|
|
182
|
-
* caret, staying in the document's own units — no px conversion. Priority:
|
|
183
|
-
* direct run props (the textStyle mark) → the paragraph style (`styleId`) →
|
|
184
|
-
* its `basedOn` chain → the document defaults. `font` is resolved to a single
|
|
185
|
-
* display name (ascii/hAnsi/eastAsia). Returns null where nothing in the chain
|
|
186
|
-
* sets a property, so the caller can leave the box empty. */
|
|
187
|
-
function effectiveRunProps(styles, styleId, direct) {
|
|
188
|
-
let font = null;
|
|
189
|
-
let size = null;
|
|
190
|
-
if (direct) {
|
|
191
|
-
font = resolveFontName(direct.font);
|
|
192
|
-
if (typeof direct.size === "number" && direct.size > 0) size = direct.size;
|
|
193
|
-
}
|
|
194
|
-
if ((font == null || size == null) && styles) {
|
|
195
|
-
const effStyleId = styleId || defaultParagraphStyleId(styles);
|
|
196
|
-
const { run } = mergeStyleChain(indexParagraphStyles(styles), effStyleId);
|
|
197
|
-
if (font == null) font = resolveFontName(run.font);
|
|
198
|
-
if (size == null && typeof run.size === "number" && run.size > 0) size = run.size;
|
|
199
|
-
if (font == null || size == null) {
|
|
200
|
-
const docRun = styles.default?.document?.run;
|
|
201
|
-
if (docRun) {
|
|
202
|
-
if (font == null) font = resolveFontName(docRun.font);
|
|
203
|
-
if (size == null && typeof docRun.size === "number" && docRun.size > 0) size = docRun.size;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return {
|
|
208
|
-
font,
|
|
209
|
-
size
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
/** Index a document's character styles (explicit `characterStyles` + the
|
|
213
|
-
* built-in character keys under `default`) by pStyle id → run properties. Core
|
|
214
|
-
* phase: a character style's own run only (its basedOn chain is not walked —
|
|
215
|
-
* rare in practice; paragraph styles above already deep-merge). */
|
|
216
|
-
function indexCharacterRunStyles(styles) {
|
|
217
|
-
const byId = /* @__PURE__ */ new Map();
|
|
218
|
-
for (const cs of styles.characterStyles ?? []) if (cs.run) byId.set(cs.id, cs.run);
|
|
219
|
-
const defaults = styles.default;
|
|
220
|
-
for (const [key, style] of Object.entries(defaults ?? {})) {
|
|
221
|
-
if (!style || !style.run || key === "document") continue;
|
|
222
|
-
if (!CHARACTER_DEFAULT_KEYS.has(key)) continue;
|
|
223
|
-
byId.set(pStyleIdFromKey(key), style.run);
|
|
224
|
-
}
|
|
225
|
-
return byId;
|
|
226
|
-
}
|
|
227
|
-
/** Resolve one node's style inheritance: a paragraph/heading absorbs its
|
|
228
|
-
* styleId's basedOn chain (paragraph properties + the default `run`); a
|
|
229
|
-
* textStyle mark absorbs its character style's run. Explicit attrs/marks
|
|
230
|
-
* override the inherited values; styleId is preserved. Pure JSON — no DOM, no
|
|
231
|
-
* marks pushed onto text. */
|
|
232
|
-
function resolveNode(node, paraById, charRunById) {
|
|
233
|
-
const out = { ...node };
|
|
234
|
-
const attrs = node.attrs;
|
|
235
|
-
const type = node.type;
|
|
236
|
-
if ((type === "paragraph" || type === "heading") && attrs?.styleId) {
|
|
237
|
-
const { paragraph, run } = mergeStyleChain(paraById, attrs.styleId);
|
|
238
|
-
const mergedRun = {
|
|
239
|
-
...run,
|
|
240
|
-
...attrs.run
|
|
241
|
-
};
|
|
242
|
-
out.attrs = {
|
|
243
|
-
...paragraph,
|
|
244
|
-
...attrs,
|
|
245
|
-
run: mergedRun
|
|
246
|
-
};
|
|
247
|
-
}
|
|
248
|
-
if (node.marks) out.marks = node.marks.map((m) => {
|
|
249
|
-
if (m.type !== "textStyle") return m;
|
|
250
|
-
const sid = m.attrs?.styleId;
|
|
251
|
-
const crun = sid ? charRunById.get(sid) : void 0;
|
|
252
|
-
return crun ? {
|
|
253
|
-
...m,
|
|
254
|
-
attrs: {
|
|
255
|
-
...crun,
|
|
256
|
-
...m.attrs
|
|
257
|
-
}
|
|
258
|
-
} : m;
|
|
259
|
-
});
|
|
260
|
-
if (node.content) out.content = node.content.map((c) => resolveNode(c, paraById, charRunById));
|
|
261
|
-
return out;
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Inline a document's styles into each node's attrs — a self-contained
|
|
265
|
-
* JSONContent that round-trips and renders without the styles context. A
|
|
266
|
-
* paragraph/heading absorbs its styleId's `basedOn` chain (paragraph properties
|
|
267
|
-
* + the paragraph's default `run`); a textStyle mark absorbs its character
|
|
268
|
-
* style's run. Explicit attrs/marks always override the inherited values;
|
|
269
|
-
* styleId is preserved (the semantic reference is kept). The style definitions
|
|
270
|
-
* themselves are untouched — only properties are copied onto nodes.
|
|
271
|
-
*
|
|
272
|
-
* Styles default to the document's own `attrs.styles` (the round-tripped
|
|
273
|
-
* styles.xml model carried on the doc node), so `inlineStyles(doc)` needs no
|
|
274
|
-
* second argument; pass `styles` explicitly to override. Use cases:
|
|
275
|
-
* cross-document paste (the snippet carries its styling) and self-contained
|
|
276
|
-
* export. The editor still renders via global styles — this only merges JSON
|
|
277
|
-
* properties.
|
|
278
|
-
*/
|
|
279
|
-
function inlineStyles(json, styles) {
|
|
280
|
-
const docStyles = styles ?? json.attrs?.styles;
|
|
281
|
-
if (!docStyles) return json;
|
|
282
|
-
return resolveNode(json, indexParagraphStyles(docStyles), indexCharacterRunStyles(docStyles));
|
|
283
|
-
}
|
|
284
|
-
//#endregion
|
|
285
|
-
export { stylesToCss as a, quickStyles as i, effectiveRunProps as n, inlineStyles as r, defaultParagraphStyleId as t };
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { JSONContent } from "@tiptap/core";
|
|
2
|
-
import { StylesOptions } from "@office-open/docx";
|
|
3
|
-
|
|
4
|
-
//#region src/converters/styles.d.ts
|
|
5
|
-
/** The styleId of the document's default paragraph style (`w:default="1"`
|
|
6
|
-
* type="paragraph") — the implicit style applied to every paragraph WITHOUT an
|
|
7
|
-
* explicit pStyle. OOXML renders a pStyle-less paragraph as this style (usually
|
|
8
|
-
* "Normal"), so the editor's `.docx-default` class — emitted by Paragraph/
|
|
9
|
-
* Heading renderHTML on a styleId-less node — targets it. Searched in
|
|
10
|
-
* `paragraphStyles` and the built-in named styles nested under `default`
|
|
11
|
-
* (key → pStyle id). null when the document declares none. */
|
|
12
|
-
declare function defaultParagraphStyleId(styles: StylesOptions | null | undefined): string | null;
|
|
13
|
-
/**
|
|
14
|
-
* Generate scoped CSS from a document's `styles.xml` model (`StylesOptions`) so
|
|
15
|
-
* named paragraph/character styles and the document defaults render correctly
|
|
16
|
-
* in the editor. All rules are scoped to `.docen-page` (the editor's page
|
|
17
|
-
* surface); the editor injects the result into a `<style>` after load.
|
|
18
|
-
*
|
|
19
|
-
* - `default.document` → `.docen-page` base run + paragraph defaults (the doc's
|
|
20
|
-
* default font/size, line spacing, …).
|
|
21
|
-
* - `paragraphStyles[]` → `.docen-page .docx-style-{id}` (run + paragraph CSS).
|
|
22
|
-
* Paragraph nodes carry `class="docx-style-{styleId}"` (see the Paragraph /
|
|
23
|
-
* Heading extensions' pStyle ↔ styleId round-trip).
|
|
24
|
-
* - `characterStyles[]` → `.docen-page .docx-char-{id}` (run CSS).
|
|
25
|
-
*
|
|
26
|
-
* `basedOn` inheritance is deep-merged per-property into each named style's
|
|
27
|
-
* rule (root first, child overrides) — see mergeStyleChain. CSS inheritance
|
|
28
|
-
* follows the DOM tree, not a style's basedOn, so each `.docx-style-{id}` rule
|
|
29
|
-
* carries its full ancestor chain rather than relying on source order.
|
|
30
|
-
*/
|
|
31
|
-
declare function stylesToCss(styles: StylesOptions | null | undefined, scope: string): string;
|
|
32
|
-
/** A gallery-ready paragraph-style entry for the Styles combobox. */
|
|
33
|
-
interface QuickStyleEntry {
|
|
34
|
-
id: string;
|
|
35
|
-
name: string;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* The paragraph styles to list in the Quick Styles gallery, matching Word's
|
|
39
|
-
* default behavior: the gallery is a *paragraph-style* selector (it applies a
|
|
40
|
-
* pStyle), so only paragraph styles appear — never character styles, even
|
|
41
|
-
* those flagged `quickFormat` (those live in the Styles task pane). Among
|
|
42
|
-
* paragraph styles, only those flagged `quickFormat` are listed, ordered by
|
|
43
|
-
* `uiPriority` (Word orders the gallery this way).
|
|
44
|
-
*
|
|
45
|
-
* Reads `quickFormat`/`uiPriority`/`name` straight from office-open's styles
|
|
46
|
-
* model (`StylesOptions`): `paragraphStyles` (Normal + custom) and the built-in
|
|
47
|
-
* named paragraph styles nested under `default` (title/heading1-9/quote/…). The
|
|
48
|
-
* `default` keys that hold character styles are excluded via
|
|
49
|
-
* `CHARACTER_DEFAULT_KEYS`. When a document carries no quickFormat flags at all
|
|
50
|
-
* (e.g. some LibreOffice-generated files), fall back to all paragraph styles so
|
|
51
|
-
* the gallery is never empty.
|
|
52
|
-
*/
|
|
53
|
-
declare function quickStyles(styles: StylesOptions | null | undefined): QuickStyleEntry[];
|
|
54
|
-
/** Resolve the effective run-level properties (font name, size in points) at the
|
|
55
|
-
* caret, staying in the document's own units — no px conversion. Priority:
|
|
56
|
-
* direct run props (the textStyle mark) → the paragraph style (`styleId`) →
|
|
57
|
-
* its `basedOn` chain → the document defaults. `font` is resolved to a single
|
|
58
|
-
* display name (ascii/hAnsi/eastAsia). Returns null where nothing in the chain
|
|
59
|
-
* sets a property, so the caller can leave the box empty. */
|
|
60
|
-
declare function effectiveRunProps(styles: StylesOptions | null | undefined, styleId: string | null | undefined, direct?: {
|
|
61
|
-
font?: unknown;
|
|
62
|
-
size?: unknown;
|
|
63
|
-
}): {
|
|
64
|
-
font: string | null;
|
|
65
|
-
size: number | null;
|
|
66
|
-
};
|
|
67
|
-
/**
|
|
68
|
-
* Inline a document's styles into each node's attrs — a self-contained
|
|
69
|
-
* JSONContent that round-trips and renders without the styles context. A
|
|
70
|
-
* paragraph/heading absorbs its styleId's `basedOn` chain (paragraph properties
|
|
71
|
-
* + the paragraph's default `run`); a textStyle mark absorbs its character
|
|
72
|
-
* style's run. Explicit attrs/marks always override the inherited values;
|
|
73
|
-
* styleId is preserved (the semantic reference is kept). The style definitions
|
|
74
|
-
* themselves are untouched — only properties are copied onto nodes.
|
|
75
|
-
*
|
|
76
|
-
* Styles default to the document's own `attrs.styles` (the round-tripped
|
|
77
|
-
* styles.xml model carried on the doc node), so `inlineStyles(doc)` needs no
|
|
78
|
-
* second argument; pass `styles` explicitly to override. Use cases:
|
|
79
|
-
* cross-document paste (the snippet carries its styling) and self-contained
|
|
80
|
-
* export. The editor still renders via global styles — this only merges JSON
|
|
81
|
-
* properties.
|
|
82
|
-
*/
|
|
83
|
-
declare function inlineStyles(json: JSONContent, styles?: StylesOptions | null): JSONContent;
|
|
84
|
-
//#endregion
|
|
85
|
-
export { inlineStyles as a, effectiveRunProps as i, StylesOptions as n, quickStyles as o, defaultParagraphStyleId as r, stylesToCss as s, QuickStyleEntry as t };
|