ai-word-editor 0.0.3 → 0.0.5
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/wordDocxExport.js +84 -80
- package/package.json +1 -1
package/dist/wordDocxExport.js
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
import R from "jszip";
|
|
2
2
|
import { saveAs as F } from "file-saver";
|
|
3
|
-
async function
|
|
4
|
-
const
|
|
5
|
-
|
|
3
|
+
async function O(t, o, e = "文档") {
|
|
4
|
+
const r = new DOMParser().parseFromString(t, "text/html"), n = /* @__PURE__ */ new Map(), l = [], i = new Map((o || []).map((p) => [p.id, p]));
|
|
5
|
+
r.body.querySelectorAll("span[data-comment-id]").forEach((p) => {
|
|
6
6
|
const c = p.getAttribute("data-comment-id");
|
|
7
7
|
if (c && i.has(c) && !n.has(c)) {
|
|
8
8
|
const d = l.length;
|
|
9
9
|
n.set(c, d), l.push({ ...i.get(c), ooxmlId: d });
|
|
10
10
|
}
|
|
11
11
|
});
|
|
12
|
-
const a = k(
|
|
13
|
-
w.file("[Content_Types].xml",
|
|
12
|
+
const a = k(r.body, n), u = l.length > 0, w = new R();
|
|
13
|
+
w.file("[Content_Types].xml", U(u)), w.file("_rels/.rels", W()), w.file("word/document.xml", Z(a)), w.file("word/styles.xml", J()), w.file("word/_rels/document.xml.rels", X(u)), u && w.file("word/comments.xml", K(l));
|
|
14
14
|
const m = await w.generateAsync({
|
|
15
15
|
type: "blob",
|
|
16
16
|
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
|
17
17
|
});
|
|
18
18
|
F(m, `${e}.docx`);
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
async function Q(t, o = "文档") {
|
|
21
|
+
return O(t, [], o);
|
|
22
|
+
}
|
|
23
|
+
const q = /* @__PURE__ */ new Set(["p", "div", "h1", "h2", "h3", "h4", "h5", "h6", "li", "blockquote", "pre"]), M = /* @__PURE__ */ new Set(["script", "style", "head"]), H = {
|
|
21
24
|
signle: { val: "single", sz: "6" },
|
|
22
25
|
"signle-bold": { val: "thick", sz: "12" },
|
|
23
26
|
double: { val: "double", sz: "6" },
|
|
@@ -28,49 +31,49 @@ const O = /* @__PURE__ */ new Set(["p", "div", "h1", "h2", "h3", "h4", "h5", "h6
|
|
|
28
31
|
"double-bold-bottom": { val: "thickThinSmallGap", sz: "24" },
|
|
29
32
|
wavy: { val: "wave", sz: "6" }
|
|
30
33
|
};
|
|
31
|
-
function k(t,
|
|
34
|
+
function k(t, o) {
|
|
32
35
|
let e = "";
|
|
33
36
|
for (const s of t.childNodes)
|
|
34
|
-
e += P(s,
|
|
37
|
+
e += P(s, o);
|
|
35
38
|
return e.includes("<w:p") || (e = "<w:p><w:r><w:t></w:t></w:r></w:p>"), e;
|
|
36
39
|
}
|
|
37
|
-
function P(t,
|
|
40
|
+
function P(t, o) {
|
|
38
41
|
if (t.nodeType === 3) {
|
|
39
|
-
const
|
|
40
|
-
return !
|
|
42
|
+
const r = t.textContent;
|
|
43
|
+
return !r || !r.trim() ? "" : `<w:p><w:r><w:t xml:space="preserve">${y(r)}</w:t></w:r></w:p>`;
|
|
41
44
|
}
|
|
42
45
|
if (t.nodeType !== 1) return "";
|
|
43
46
|
const e = t.tagName.toLowerCase();
|
|
44
|
-
if (
|
|
45
|
-
if (
|
|
46
|
-
const
|
|
47
|
-
return e === "p" ? `<w:p>${
|
|
47
|
+
if (M.has(e)) return "";
|
|
48
|
+
if (q.has(e)) {
|
|
49
|
+
const r = G(e, t), n = f(t, o, {});
|
|
50
|
+
return e === "p" ? `<w:p>${r}${n || "<w:r><w:t></w:t></w:r>"}</w:p>` : n ? `<w:p>${r}${n}</w:p>` : "";
|
|
48
51
|
}
|
|
49
52
|
if (e === "table") {
|
|
50
|
-
const
|
|
51
|
-
return
|
|
53
|
+
const r = t.querySelector("colgroup[data-word-col-layout]"), n = t.querySelectorAll("tbody tr, tr"), l = n.length === 1, i = n[0] ? [...n[0].querySelectorAll("td, th")] : [];
|
|
54
|
+
return r || l && i.length >= 2 ? _(t, o) : I(t, o);
|
|
52
55
|
}
|
|
53
56
|
if (["ul", "ol", "tbody", "thead", "tfoot", "tr", "td", "th"].includes(e)) {
|
|
54
|
-
let
|
|
57
|
+
let r = "";
|
|
55
58
|
for (const n of t.childNodes)
|
|
56
|
-
|
|
57
|
-
return
|
|
59
|
+
r += P(n, o);
|
|
60
|
+
return r;
|
|
58
61
|
}
|
|
59
62
|
if (e === "hr") {
|
|
60
|
-
const
|
|
63
|
+
const r = t.getAttribute("data-type") || "signle", l = (t.getAttribute("data-color") || "#000000").replace(/^#/, ""), i = H[r] || { val: "single", sz: "6" };
|
|
61
64
|
return `<w:p><w:pPr><w:pBdr><w:bottom w:val="${i.val}" w:color="${l}" w:sz="${i.sz}" w:space="1"/></w:pBdr></w:pPr></w:p>`;
|
|
62
65
|
}
|
|
63
|
-
const s = f(t,
|
|
66
|
+
const s = f(t, o, {});
|
|
64
67
|
return s ? `<w:p>${s}</w:p>` : "";
|
|
65
68
|
}
|
|
66
|
-
function
|
|
69
|
+
function I(t, o) {
|
|
67
70
|
const e = t.querySelectorAll("tr");
|
|
68
71
|
if (!e.length) return "";
|
|
69
|
-
const s = t.querySelector("colgroup"),
|
|
72
|
+
const s = t.querySelector("colgroup"), r = s ? [...s.querySelectorAll("col")] : [], n = e[0], l = n ? [...n.querySelectorAll("td, th")] : [], i = Math.max(r.length, l.length, 1), a = 8306, u = (d, h) => {
|
|
70
73
|
const g = (d && d.getAttribute("style") || "").match(/width\s*:\s*([\d.]+)%/);
|
|
71
74
|
return g ? parseFloat(g[1]) : 100 / h;
|
|
72
75
|
}, w = Array.from({ length: i }).map((d, h) => {
|
|
73
|
-
const
|
|
76
|
+
const x = r[h], g = u(x, i);
|
|
74
77
|
return Math.round(g / 100 * a);
|
|
75
78
|
}), m = w.map((d) => `<w:gridCol w:w="${d}"/>`).join(""), p = `
|
|
76
79
|
<w:tblPr>
|
|
@@ -89,33 +92,33 @@ function H(t, r) {
|
|
|
89
92
|
return e.forEach((d) => {
|
|
90
93
|
const h = [...d.querySelectorAll("td, th")];
|
|
91
94
|
if (!h.length) return;
|
|
92
|
-
let
|
|
95
|
+
let x = "";
|
|
93
96
|
h.forEach((g, N) => {
|
|
94
|
-
const $ = parseInt(g.getAttribute("colspan"), 10),
|
|
97
|
+
const $ = parseInt(g.getAttribute("colspan"), 10), D = !Number.isNaN($) && $ > 1, S = D ? $ : 1;
|
|
95
98
|
let T = 0;
|
|
96
|
-
for (let
|
|
97
|
-
const z = w[N +
|
|
99
|
+
for (let b = 0; b < S; b++) {
|
|
100
|
+
const z = w[N + b];
|
|
98
101
|
T += Number.isFinite(z) ? z : Math.round(a / i);
|
|
99
102
|
}
|
|
100
103
|
const C = [];
|
|
101
|
-
|
|
104
|
+
D && C.push(`<w:gridSpan w:val="${S}"/>`), T > 0 && C.push(`<w:tcW w:w="${T}" w:type="dxa"/>`);
|
|
102
105
|
const A = `<w:tcPr>${C.join("")}</w:tcPr>`;
|
|
103
106
|
let v = "";
|
|
104
|
-
for (const
|
|
105
|
-
v += P(
|
|
106
|
-
v || (v = "<w:p><w:r><w:t></w:t></w:r></w:p>"),
|
|
107
|
-
}), c += `<w:tr>${
|
|
107
|
+
for (const b of g.childNodes)
|
|
108
|
+
v += P(b, o);
|
|
109
|
+
v || (v = "<w:p><w:r><w:t></w:t></w:r></w:p>"), x += `<w:tc>${A}${v}</w:tc>`;
|
|
110
|
+
}), c += `<w:tr>${x}</w:tr>`;
|
|
108
111
|
}), c ? `<w:tbl>${p}<w:tblGrid>${m}</w:tblGrid>${c}</w:tbl>` : "";
|
|
109
112
|
}
|
|
110
|
-
function
|
|
111
|
-
const e = t.querySelector("colgroup"), s = e ? [...e.querySelectorAll("col")] : [],
|
|
113
|
+
function _(t, o) {
|
|
114
|
+
const e = t.querySelector("colgroup"), s = e ? [...e.querySelectorAll("col")] : [], r = t.querySelectorAll("tbody tr, tr"), n = r[0] ? [...r[0].querySelectorAll("td, th")] : [], l = Math.max(s.length, n.length, 1), i = 8306, a = 720, u = (c) => {
|
|
112
115
|
const h = (c && c.getAttribute("style") || "").match(/width\s*:\s*([\d.]+)%/);
|
|
113
116
|
return h ? parseFloat(h[1]) : 100 / l;
|
|
114
117
|
}, w = s.length > 0 ? s.map((c) => Math.round(u(c) / 100 * i)) : Array.from({ length: l }, () => Math.round((i - a * (l - 1)) / l));
|
|
115
118
|
let m = "";
|
|
116
119
|
n.forEach((c, d) => {
|
|
117
120
|
for (const h of c.childNodes)
|
|
118
|
-
m += P(h,
|
|
121
|
+
m += P(h, o);
|
|
119
122
|
d < n.length - 1 && (m += '<w:p><w:r><w:br w:type="column"/></w:r></w:p>');
|
|
120
123
|
});
|
|
121
124
|
const p = w.map((c) => `<w:col w:w="${c}" w:space="${a}"/>`).join("");
|
|
@@ -125,70 +128,70 @@ function I(t, r) {
|
|
|
125
128
|
<w:pgMar w:top="1800" w:right="1800" w:bottom="1800" w:left="1800" w:header="851" w:footer="992" w:gutter="0"/>
|
|
126
129
|
</w:sectPr></w:pPr></w:p>`, m;
|
|
127
130
|
}
|
|
128
|
-
function f(t,
|
|
131
|
+
function f(t, o, e) {
|
|
129
132
|
let s = "";
|
|
130
|
-
for (const
|
|
131
|
-
s +=
|
|
133
|
+
for (const r of t.childNodes)
|
|
134
|
+
s += E(r, o, e);
|
|
132
135
|
return s;
|
|
133
136
|
}
|
|
134
|
-
function
|
|
137
|
+
function E(t, o, e) {
|
|
135
138
|
if (t.nodeType === 3) {
|
|
136
|
-
const
|
|
137
|
-
return
|
|
139
|
+
const r = t.textContent;
|
|
140
|
+
return r ? B(r, e) : "";
|
|
138
141
|
}
|
|
139
142
|
if (t.nodeType !== 1) return "";
|
|
140
143
|
const s = t.tagName.toLowerCase();
|
|
141
144
|
if (s === "span" && t.hasAttribute("data-comment-id")) {
|
|
142
|
-
const
|
|
145
|
+
const r = t.getAttribute("data-comment-id"), n = o.get(r);
|
|
143
146
|
if (n !== void 0) {
|
|
144
|
-
const l = f(t,
|
|
147
|
+
const l = f(t, o, e);
|
|
145
148
|
return `<w:commentRangeStart w:id="${n}"/>` + l + `<w:commentRangeEnd w:id="${n}"/><w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="${n}"/></w:r>`;
|
|
146
149
|
}
|
|
147
|
-
return f(t,
|
|
150
|
+
return f(t, o, e);
|
|
148
151
|
}
|
|
149
152
|
if (s === "span") {
|
|
150
|
-
const
|
|
151
|
-
return f(t,
|
|
153
|
+
const r = j(t, e);
|
|
154
|
+
return f(t, o, r);
|
|
152
155
|
}
|
|
153
|
-
return s === "strong" || s === "b" ? f(t,
|
|
156
|
+
return s === "strong" || s === "b" ? f(t, o, { ...e, bold: !0 }) : s === "em" || s === "i" ? f(t, o, { ...e, italic: !0 }) : s === "u" ? f(t, o, { ...e, underline: !0 }) : s === "s" || s === "del" || s === "strike" ? f(t, o, { ...e, strike: !0 }) : s === "br" ? "<w:r><w:br/></w:r>" : f(t, o, e);
|
|
154
157
|
}
|
|
155
|
-
function
|
|
158
|
+
function j(t, o) {
|
|
156
159
|
const e = t.getAttribute("style") || "";
|
|
157
160
|
t.className;
|
|
158
|
-
const s = e.match(/font-family\s*:\s*([^;]+)/i),
|
|
161
|
+
const s = e.match(/font-family\s*:\s*([^;]+)/i), r = s ? s[1].trim().replace(/["']/g, "") : null, n = e.match(/font-size\s*:\s*([\d.]+)(px|pt)/i);
|
|
159
162
|
let l = null;
|
|
160
163
|
if (n) {
|
|
161
164
|
const u = parseFloat(n[1]);
|
|
162
165
|
l = n[2].toLowerCase() === "pt" ? Math.round(u * 2) : Math.round(u * 1.5);
|
|
163
166
|
}
|
|
164
|
-
const i = e.match(/(?:^|;)\s*color\s*:\s*([^;]+)/i), a = i ?
|
|
167
|
+
const i = e.match(/(?:^|;)\s*color\s*:\s*([^;]+)/i), a = i ? L(i[1].trim()) : null;
|
|
165
168
|
return {
|
|
166
|
-
...
|
|
167
|
-
bold:
|
|
168
|
-
italic:
|
|
169
|
-
underline:
|
|
170
|
-
strike:
|
|
171
|
-
fontFamily:
|
|
172
|
-
fontSize: l ??
|
|
173
|
-
color: a ||
|
|
169
|
+
...o,
|
|
170
|
+
bold: o.bold || /font-weight\s*:\s*(bold|[789]\d\d)/i.test(e),
|
|
171
|
+
italic: o.italic || /font-style\s*:\s*italic/i.test(e),
|
|
172
|
+
underline: o.underline || /text-decoration[^;]*underline/i.test(e),
|
|
173
|
+
strike: o.strike || /text-decoration[^;]*line-through/i.test(e),
|
|
174
|
+
fontFamily: r || o.fontFamily,
|
|
175
|
+
fontSize: l ?? o.fontSize,
|
|
176
|
+
color: a || o.color
|
|
174
177
|
};
|
|
175
178
|
}
|
|
176
|
-
function
|
|
179
|
+
function B(t, { bold: o, italic: e, underline: s, strike: r, fontFamily: n, fontSize: l, color: i } = {}) {
|
|
177
180
|
let a = "";
|
|
178
181
|
if (n) {
|
|
179
182
|
const w = y(n);
|
|
180
183
|
a += `<w:rFonts w:ascii="${w}" w:eastAsia="${w}" w:hAnsi="${w}" w:cs="${w}"/>`;
|
|
181
184
|
}
|
|
182
|
-
return l && (a += `<w:sz w:val="${l}"/><w:szCs w:val="${l}"/>`), i && (a += `<w:color w:val="${i}"/>`),
|
|
185
|
+
return l && (a += `<w:sz w:val="${l}"/><w:szCs w:val="${l}"/>`), i && (a += `<w:color w:val="${i}"/>`), o && (a += "<w:b/><w:bCs/>"), e && (a += "<w:i/><w:iCs/>"), s && (a += '<w:u w:val="single"/>'), r && (a += "<w:strike/>"), `<w:r>${a ? `<w:rPr>${a}</w:rPr>` : ""}<w:t xml:space="preserve">${y(t)}</w:t></w:r>`;
|
|
183
186
|
}
|
|
184
|
-
function
|
|
187
|
+
function G(t, o) {
|
|
185
188
|
let e = "";
|
|
186
189
|
const s = { h1: "Heading1", h2: "Heading2", h3: "Heading3", h4: "Heading4", h5: "Heading5", h6: "Heading6" };
|
|
187
190
|
s[t] && (e += `<w:pStyle w:val="${s[t]}"/>`), (t === "li" || t === "blockquote" || t === "pre") && (e = '<w:ind w:left="720"/>');
|
|
188
|
-
const
|
|
191
|
+
const r = o.getAttribute("style") || "", n = r.match(/text-align\s*:\s*(left|center|right|justify)/i);
|
|
189
192
|
n && (e += `<w:jc w:val="${{ left: "left", center: "center", right: "right", justify: "both" }[n[1].toLowerCase()]}"/>`);
|
|
190
193
|
let l = "";
|
|
191
|
-
const i =
|
|
194
|
+
const i = r.match(/line-height\s*:\s*([^;]+)/i), a = r.match(/margin-top\s*:\s*([\d.]+)px/i), u = r.match(/margin-bottom\s*:\s*([\d.]+)px/i);
|
|
192
195
|
let w = "";
|
|
193
196
|
if (i) {
|
|
194
197
|
const m = i[1].trim(), p = parseFloat(m);
|
|
@@ -225,17 +228,17 @@ function B(t, r) {
|
|
|
225
228
|
function y(t) {
|
|
226
229
|
return String(t).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
227
230
|
}
|
|
228
|
-
function
|
|
231
|
+
function L(t) {
|
|
229
232
|
if (!t || typeof t != "string") return null;
|
|
230
|
-
const
|
|
231
|
-
if (/^#[0-9a-f]{3,6}$/i.test(
|
|
232
|
-
let s =
|
|
233
|
-
return s.length === 3 && (s = s.split("").map((
|
|
233
|
+
const o = t.trim();
|
|
234
|
+
if (/^#[0-9a-f]{3,6}$/i.test(o)) {
|
|
235
|
+
let s = o.slice(1);
|
|
236
|
+
return s.length === 3 && (s = s.split("").map((r) => r + r).join("")), s.toUpperCase();
|
|
234
237
|
}
|
|
235
|
-
const e =
|
|
238
|
+
const e = o.match(/rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/);
|
|
236
239
|
return e ? [e[1], e[2], e[3]].map((s) => (+s).toString(16).padStart(2, "0")).join("").toUpperCase() : null;
|
|
237
240
|
}
|
|
238
|
-
function
|
|
241
|
+
function U(t) {
|
|
239
242
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
240
243
|
<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
|
|
241
244
|
<Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
|
|
@@ -245,20 +248,20 @@ function L(t) {
|
|
|
245
248
|
<Override PartName="/word/comments.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml"/>` : ""}
|
|
246
249
|
</Types>`;
|
|
247
250
|
}
|
|
248
|
-
function
|
|
251
|
+
function W() {
|
|
249
252
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
250
253
|
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
251
254
|
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
|
|
252
255
|
</Relationships>`;
|
|
253
256
|
}
|
|
254
|
-
function
|
|
257
|
+
function X(t) {
|
|
255
258
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
256
259
|
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
|
257
260
|
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>${t ? `
|
|
258
261
|
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" Target="comments.xml"/>` : ""}
|
|
259
262
|
</Relationships>`;
|
|
260
263
|
}
|
|
261
|
-
function
|
|
264
|
+
function Z(t) {
|
|
262
265
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
263
266
|
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas"
|
|
264
267
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
|
@@ -280,12 +283,12 @@ ${t}
|
|
|
280
283
|
</w:body>
|
|
281
284
|
</w:document>`;
|
|
282
285
|
}
|
|
283
|
-
function
|
|
286
|
+
function K(t) {
|
|
284
287
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
285
288
|
<w:comments xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
|
286
289
|
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
|
287
|
-
${t.map(({ ooxmlId: e, author: s, createdAt:
|
|
288
|
-
const l = y(s || "作者"), i = y((s || "作").charAt(0)), a =
|
|
290
|
+
${t.map(({ ooxmlId: e, author: s, createdAt: r, note: n }) => {
|
|
291
|
+
const l = y(s || "作者"), i = y((s || "作").charAt(0)), a = r ? new Date(r).toISOString().slice(0, 19) + "Z" : (/* @__PURE__ */ new Date()).toISOString().slice(0, 19) + "Z", u = y(n || "");
|
|
289
292
|
return ` <w:comment w:id="${e}" w:author="${l}" w:date="${a}" w:initials="${i}">
|
|
290
293
|
<w:p>
|
|
291
294
|
<w:pPr><w:pStyle w:val="CommentText"/></w:pPr>
|
|
@@ -297,7 +300,7 @@ ${t.map(({ ooxmlId: e, author: s, createdAt: o, note: n }) => {
|
|
|
297
300
|
`)}
|
|
298
301
|
</w:comments>`;
|
|
299
302
|
}
|
|
300
|
-
function
|
|
303
|
+
function J() {
|
|
301
304
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
302
305
|
<w:styles xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
|
303
306
|
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
|
|
@@ -356,5 +359,6 @@ function K() {
|
|
|
356
359
|
</w:styles>`;
|
|
357
360
|
}
|
|
358
361
|
export {
|
|
359
|
-
|
|
362
|
+
Q as exportHtmlToDocx,
|
|
363
|
+
O as exportToDocx
|
|
360
364
|
};
|