@a200888213/gbdocx 0.5.2 → 0.6.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.
Files changed (80) hide show
  1. package/README.md +215 -207
  2. package/index.html +3 -2
  3. package/package.json +48 -48
  4. package/serve.mjs +3 -2
  5. package/src/app/file-io.js +122 -122
  6. package/src/app/main.js +9 -9
  7. package/src/app/word/catalog.js +52 -52
  8. package/src/app/word/comments.js +112 -112
  9. package/src/app/word/doc.js +113 -113
  10. package/src/app/word/edit.js +72 -72
  11. package/src/app/word/file.js +30 -1
  12. package/src/app/word/hf.js +76 -76
  13. package/src/app/word/layout.js +155 -155
  14. package/src/app/word/notes.js +110 -110
  15. package/src/app/word/shared.js +22 -22
  16. package/src/app/word/view.js +6 -2
  17. package/src/app/word-app.js +486 -469
  18. package/src/app/word-sheets.js +101 -101
  19. package/src/kernel/docx/codec/blocks.js +123 -123
  20. package/src/kernel/docx/codec/carry.js +129 -129
  21. package/src/kernel/docx/codec/comments.js +25 -25
  22. package/src/kernel/docx/codec/docx.js +198 -198
  23. package/src/kernel/docx/codec/docxStyles.js +177 -177
  24. package/src/kernel/docx/codec/fonts.js +195 -195
  25. package/src/kernel/docx/codec/helpers.js +161 -161
  26. package/src/kernel/docx/codec/hfHtml.js +152 -152
  27. package/src/kernel/docx/codec/html.js +739 -739
  28. package/src/kernel/docx/codec/losses.js +420 -420
  29. package/src/kernel/docx/codec/markdown.js +170 -170
  30. package/src/kernel/docx/codec/parts.js +249 -249
  31. package/src/kernel/docx/codec/read.js +603 -603
  32. package/src/kernel/docx/codec/render.js +1156 -1156
  33. package/src/kernel/docx/codec/roundtrip.js +236 -236
  34. package/src/kernel/docx/codec/write.js +833 -833
  35. package/src/kernel/docx/comments.js +14 -14
  36. package/src/kernel/docx/pageGeometry.js +123 -123
  37. package/src/kernel/docx/pagination.js +481 -481
  38. package/src/kernel/docx/watermark.js +45 -45
  39. package/src/kernel/editor/constants.js +8 -8
  40. package/src/kernel/editor/engine/blocks.js +594 -594
  41. package/src/kernel/editor/engine/blocksEnter.js +109 -109
  42. package/src/kernel/editor/engine/blocksInsert.js +254 -254
  43. package/src/kernel/editor/engine/blocksStructure.js +226 -226
  44. package/src/kernel/editor/engine/commands.js +181 -181
  45. package/src/kernel/editor/engine/dom.js +646 -646
  46. package/src/kernel/editor/engine/history.js +149 -149
  47. package/src/kernel/editor/engine/inline.js +656 -656
  48. package/src/kernel/editor/engine/input.js +554 -554
  49. package/src/kernel/editor/engine/interaction.js +137 -137
  50. package/src/kernel/editor/engine/pointer.js +39 -39
  51. package/src/kernel/editor/engine/revision.js +138 -138
  52. package/src/kernel/editor/engine/schema.js +417 -417
  53. package/src/kernel/editor/engine/selection.js +119 -119
  54. package/src/kernel/editor/engine/state.js +118 -118
  55. package/src/kernel/editor/engine/tableClip.js +154 -154
  56. package/src/kernel/editor/engine/tableGrid.js +55 -55
  57. package/src/kernel/editor/engine/tableInteraction.js +388 -388
  58. package/src/kernel/editor/engine/tableMenu.js +250 -250
  59. package/src/kernel/editor/engine/tableOps.js +174 -174
  60. package/src/kernel/editor/engine/tables.js +732 -732
  61. package/src/kernel/editor/overlayGeometry.js +20 -20
  62. package/src/kernel/editor/utils.js +48 -48
  63. package/src/kernel/keyboard.js +114 -114
  64. package/src/kernel/textStyles.js +84 -84
  65. package/src/kernel/util/imageFile.js +91 -91
  66. package/src/kernel/util/shortcutKey.js +33 -33
  67. package/src/surface/editor-surface.js +600 -600
  68. package/src/ui/color-panel.js +69 -69
  69. package/src/ui/context-menu.js +133 -133
  70. package/src/ui/find-bar.js +151 -151
  71. package/src/ui/icons.js +137 -135
  72. package/src/ui/menubar.js +28 -28
  73. package/src/ui/panels.js +115 -115
  74. package/src/ui/prompt.js +89 -89
  75. package/src/ui/ribbon.js +485 -485
  76. package/src/ui/special-chars.js +39 -39
  77. package/src/ui/statusbar.js +81 -81
  78. package/src/ui/toolbar.js +32 -32
  79. package/styles/editor-content.css +440 -440
  80. package/styles/tokens.css +154 -154
@@ -1,249 +1,249 @@
1
- // [文件] web·docx·codec·parts —— 静态部件:包内路径/内容类型/关系类型常量,styles.xml / numbering.xml / comments.xml 生成
2
- import { REL_BASE, xmlEscape, xmlEscapeAttr } from 'gbooxml';
3
-
4
- /* ---------- 部件包内路径 ---------- */
5
- export const PART_DOCUMENT = 'word/document.xml';
6
- export const PART_STYLES = 'word/styles.xml';
7
- export const PART_NUMBERING = 'word/numbering.xml';
8
- export const PART_COMMENTS = 'word/comments.xml';
9
- export const PART_SETTINGS = 'word/settings.xml';
10
- export const PART_FOOTNOTES = 'word/footnotes.xml';
11
- export const PART_ENDNOTES = 'word/endnotes.xml';
12
- export const PART_PROPS = 'docProps/core.xml';
13
-
14
- /* ---------- 部件内容类型 ---------- */
15
- export const CT_DOCUMENT = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml';
16
- export const CT_STYLES = 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml';
17
- export const CT_NUMBERING = 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml';
18
- export const CT_HEADER = 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml';
19
- export const CT_FOOTER = 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml';
20
- export const CT_COMMENTS = 'application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml';
21
- export const CT_SETTINGS = 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml';
22
- export const CT_FOOTNOTES = 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml';
23
- export const CT_ENDNOTES = 'application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml';
24
- export const CT_PROPS = 'application/vnd.openxmlformats-package.core-properties+xml';
25
-
26
- /* ---------- 关系类型 ---------- */
27
- export const REL_DOC = `${REL_BASE}officeDocument`;
28
- export const REL_STYLES = `${REL_BASE}styles`;
29
- export const REL_NUMBERING = `${REL_BASE}numbering`;
30
- export const REL_HEADER = `${REL_BASE}header`;
31
- export const REL_FOOTER = `${REL_BASE}footer`;
32
- export const REL_COMMENTS = `${REL_BASE}comments`;
33
- export const REL_SETTINGS = `${REL_BASE}settings`;
34
- export const REL_HYPERLINK = `${REL_BASE}hyperlink`;
35
- export const REL_IMAGE = `${REL_BASE}image`;
36
- export const REL_FOOTNOTES = `${REL_BASE}footnotes`;
37
- export const REL_ENDNOTES = `${REL_BASE}endnotes`;
38
- /** 核心属性关系挂在包根关系表(_rels/.rels)上,不是 document 的 */
39
- export const REL_PROPS = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties';
40
-
41
- /* ---------- 命名空间 ---------- */
42
- export const NS_WORDML = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
43
- const NS_VML = 'urn:schemas-microsoft-com:vml';
44
- const NS_OFFICE = 'urn:schemas-microsoft-com:office:office';
45
- const NS_W10 = 'urn:schemas-microsoft-com:office:word';
46
- const NS_REL_ATTR = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
47
- const NS_WP_DRAW = 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing';
48
- const NS_DRAWING_M = 'http://schemas.openxmlformats.org/drawingml/2006/main';
49
- const NS_PIC_DRAW = 'http://schemas.openxmlformats.org/drawingml/2006/picture';
50
-
51
- /** 正文类部件要声明的命名空间集合:w 正文、r 关系、wp 图片锚点、a 绘图、pic 图片 */
52
- export const DOCX_NS = `xmlns:w="${NS_WORDML}" xmlns:r="${NS_REL_ATTR}" xmlns:wp="${NS_WP_DRAW}" xmlns:a="${NS_DRAWING_M}" xmlns:pic="${NS_PIC_DRAW}"`;
53
-
54
- /** 水印组件(VML 要 v/o/w10 三套前缀,不带它们 Word 会把整个图形当无效 XML) */
55
- export const VML_NS = `${DOCX_NS} xmlns:v="${NS_VML}" xmlns:o="${NS_OFFICE}" xmlns:w10="${NS_W10}"`;
56
-
57
- export const XML_HEAD = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n`;
58
-
59
- /* ---------- 装饰取值(只有我们写出去、不会被读回来的外观选择) ---------- */
60
- /** [坑] 链接色必须带 "#":颜色规整只认 #rrggbb / rgb() 两种写法,写裸十六进制会被判成不可用而回退 */
61
- export const RULE_COLOR = 'd0d7de'; // 分隔线
62
- export const GRID_COLOR = 'd0d7de'; // 表格网格线
63
- /** 正文字体:与编辑器 .rich-text-content 的默认字体同一份值(中文字形走 w:eastAsia) */
64
- export const BODY_FONT = 'Microsoft YaHei';
65
-
66
- /** 正文字号与行距(半点 / 240 分之一行):14px = 10.5pt;1.25 行 = 175(见 docx.js 的 WORD_LINE_RATIO) */
67
- export const BODY_FONT_HALF_PT = 21;
68
- const BODY_LINE = 175;
69
- /** 段后(twips):编辑器 p 的 2px 段间距 */
70
- const BODY_AFTER = 30;
71
-
72
- /** 引用块左缩进(twips):编辑器 blockquote 的 14px 内边距 + 3px 左边框 */
73
- export const QUOTE_INDENT_TWIPS = 255;
74
-
75
- /** 代码块识别(写出端铺底色 + 等宽字体,回读端按这两个值认出代码行) */
76
- export const CODE_FILL = 'F1F3F4';
77
- export const CODE_FONT = 'Consolas';
78
-
79
- /* ---------- 样式表 ---------- */
80
-
81
- /**
82
- * 各级标题样式的排版数值(字号半点 / 段前后 twips / 行距 240 分之一行)。
83
- * [设计] 这些数值是排版契约:导出后的分页要与编辑器页面预览对得上,靠的就是把它们显式钉死,
84
- * 挡住 Word 拿自带 Normal 样式替换。数值逐项对应 editor-content.css 的标题规则:
85
- * 字号 = 1.875/1.5/1.25/1.125/1/0.875 em × 14px,行距 = 1.3 × 140,段前 18px、段后 3px。
86
- * 改动数值等于改变已导出文档的排版,属于功能变更不是重构。
87
- */
88
- const HEADING_STYLES = [
89
- { id: 'Title', name: 'Title', size: 39, bold: true, before: 270, after: 45, line: 182 },
90
- { id: 'Heading1', name: 'heading 1', size: 39, bold: true, before: 270, after: 45, line: 182 },
91
- { id: 'Heading2', name: 'heading 2', size: 32, bold: true, before: 270, after: 45, line: 182 },
92
- { id: 'Heading3', name: 'heading 3', size: 26, bold: true, before: 270, after: 45, line: 182 },
93
- { id: 'Heading4', name: 'heading 4', size: 24, bold: true, before: 270, after: 45, line: 182 },
94
- { id: 'Heading5', name: 'heading 5', size: 21, bold: true, before: 270, after: 45, line: 182 },
95
- { id: 'Heading6', name: 'heading 6', size: 18, bold: true, before: 270, after: 45, line: 182 }
96
- ];
97
-
98
- /** spacingXML 段前/段后(二十分之一点)与行距(240 分之一行) */
99
- const spacingXml = (before, after, line) =>
100
- `<w:spacing w:before="${before}" w:after="${after}" w:line="${line}" w:lineRule="auto"/>`;
101
-
102
- export const stylesPart = () => {
103
- let xml = XML_HEAD;
104
- xml += `<w:styles xmlns:w="${NS_WORDML}">`;
105
- xml += `<w:docDefaults><w:rPrDefault><w:rPr>`;
106
- xml += `<w:rFonts w:ascii="${BODY_FONT}" w:hAnsi="${BODY_FONT}" w:eastAsia="${BODY_FONT}" w:cs="${BODY_FONT}"/>`;
107
- xml += `<w:sz w:val="${BODY_FONT_HALF_PT}"/><w:szCs w:val="${BODY_FONT_HALF_PT}"/></w:rPr></w:rPrDefault>`;
108
- xml += `<w:pPrDefault><w:pPr>${spacingXml(0, BODY_AFTER, BODY_LINE)}</w:pPr></w:pPrDefault></w:docDefaults>`;
109
- xml += `<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/><w:pPr>${spacingXml(0, BODY_AFTER, BODY_LINE)}</w:pPr></w:style>`;
110
- for (const h of HEADING_STYLES) {
111
- xml += `<w:style w:type="paragraph" w:styleId="${h.id}">`;
112
- xml += `<w:name w:val="${h.name}"/>`;
113
- xml += `<w:basedOn w:val="Normal"/>`;
114
- xml += `<w:pPr>${spacingXml(h.before, h.after, h.line)}</w:pPr>`;
115
- xml += '<w:rPr>';
116
- if (h.bold) xml += '<w:b/>';
117
- xml += `<w:sz w:val="${h.size}"/></w:rPr></w:style>`;
118
- }
119
- // 引用块样式:我们写出 pStyle="Quote" + 同样的左缩进,读回端按样式识别;
120
- // 样式表里显式定义一份,Word 不再按自带 Quote 样式替换排版
121
- xml += '<w:style w:type="paragraph" w:styleId="Quote"><w:name w:val="Quote"/><w:basedOn w:val="Normal"/>';
122
- xml += `<w:pPr><w:ind w:left="${QUOTE_INDENT_TWIPS}"/></w:pPr></w:style>`;
123
- xml += `</w:styles>`;
124
- return xml;
125
- };
126
-
127
- /* ---------- 编号表 ---------- */
128
-
129
- /** numId 1 = 项目符号,2 = 有序号,3/4 = 已勾选/未勾选待办(与正文里 numId 的取值对应)。
130
- * 每级左缩进 1.6em × 14px = 22.4px = 336 twips,悬挂同宽:标记顶到栏左、正文从 22.4px 起,
131
- * 与编辑器 ul/ol 的 padding-left 同一视觉(Word 的 720/360 会让列表正文比预览窄一截,提前折行) */
132
- const LEVEL_INDENT = 336;
133
- const LEVEL_HANGING = 336;
134
-
135
- /** 四级项目符号字形与有序号格式(numbering.xml 只定义到 ilvl 3) */
136
- const BULLET_GLYPHS = ['•', '◦', '-', '•'];
137
- const ORDERED_FORMATS = ['decimal', 'lowerLetter', 'lowerRoman', 'decimal'];
138
- /** 待办勾选字形(同一字形铺满四级;回读端据此把编号识别回待办列表) */
139
- const TASK_CHECKED = '☑';
140
- const TASK_UNCHECKED = '☐';
141
-
142
- /** 一个编号层级。 [坑] lvl 子元素顺序固定:numFmt → lvlText → pPr */
143
- const levelXml = (level, format, text) => {
144
- const indent = LEVEL_INDENT * (level + 1);
145
- return `<w:lvl w:ilvl="${level}"><w:numFmt w:val="${format}"/><w:lvlText w:val="${text}"/><w:pPr><w:ind w:left="${indent}" w:hanging="${LEVEL_HANGING}"/></w:pPr></w:lvl>`;
146
- };
147
-
148
- export const numberingPart = () => {
149
- let xml = XML_HEAD;
150
- xml += `<w:numbering xmlns:w="${NS_WORDML}">`;
151
- xml += `<w:abstractNum w:abstractNumId="0">`;
152
- BULLET_GLYPHS.forEach((glyph, level) => {
153
- xml += levelXml(level, 'bullet', glyph);
154
- });
155
- xml += `</w:abstractNum>`;
156
- xml += `<w:abstractNum w:abstractNumId="1">`;
157
- ORDERED_FORMATS.forEach((format, level) => {
158
- xml += levelXml(level, format, `%${level + 1}.`);
159
- });
160
- xml += `</w:abstractNum>`;
161
- xml += `<w:abstractNum w:abstractNumId="2">`;
162
- for (let level = 0; level <= 3; level += 1) xml += levelXml(level, 'bullet', TASK_CHECKED);
163
- xml += `</w:abstractNum>`;
164
- xml += `<w:abstractNum w:abstractNumId="3">`;
165
- for (let level = 0; level <= 3; level += 1) xml += levelXml(level, 'bullet', TASK_UNCHECKED);
166
- xml += `</w:abstractNum>`;
167
- xml += `<w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num>`;
168
- xml += `<w:num w:numId="2"><w:abstractNumId w:val="1"/></w:num>`;
169
- xml += `<w:num w:numId="3"><w:abstractNumId w:val="2"/></w:num>`;
170
- xml += `<w:num w:numId="4"><w:abstractNumId w:val="3"/></w:num>`;
171
- xml += `</w:numbering>`;
172
- return xml;
173
- };
174
-
175
- /* ---------- 批注 ---------- */
176
-
177
- /**
178
- * commentsPart 生成 word/comments.xml(按 id 升序输出,保证 Word 里批注顺序稳定)
179
- * 批注子集:一条批注就是一段纯文本——编辑器侧的回复/解决状态不进 docx,往返口径固定为「一条批注一段文字」。
180
- * 数字 id 按正文出现顺序分配,comments.xml 只输出正文真正引用到的条目。
181
- */
182
- export const commentsPart = (comments, used) => {
183
- const byKey = new Map((comments || []).map((c) => [c.key, c]));
184
- const entries = [...used.entries()].map(([key, id]) => [id, key]).sort((a, b) => a[0] - b[0]);
185
- let xml = XML_HEAD;
186
- xml += `<w:comments ${DOCX_NS}>`;
187
- for (const [id, key] of entries) {
188
- const c = byKey.get(key);
189
- if (!c) continue; // 正文引用了但清单里没有:跳过,不写一条空批注
190
- const author = c.author || '匿名';
191
- const date = c.date ? ` w:date="${xmlEscapeAttr(c.date)}"` : '';
192
- xml += `<w:comment w:id="${id}" w:author="${xmlEscapeAttr(author)}"${date}>`;
193
- xml += `<w:p><w:r><w:t xml:space="preserve">${xmlEscape(c.text || '')}</w:t></w:r></w:p>`;
194
- xml += `</w:comment>`;
195
- }
196
- xml += `</w:comments>`;
197
- return xml;
198
- };
199
-
200
- /* ---------- 脚注 / 尾注 ---------- */
201
-
202
- /**
203
- * notesPart 生成 word/footnotes.xml 或 word/endnotes.xml(tag = footnote | endnote)
204
- * [设计] separators 两条占位(id -1/0)必须带:Word 每份文档都有,缺了它打开时不报错但脚注分隔线行为怪异。
205
- * [设计] 条目全量写出、不过滤「没被引用」的:批注可以只写引用到的(键是编辑器自己分配的),
206
- * 脚注不行——条目在部件里的位置就是 Word 的编号,过滤会整体改变序号;且引用可能藏在没解析到的角落里,
207
- * 丢条目等于丢内容。内容是纯文本保底——富文本脚注导出后只剩文字。
208
- */
209
- export const notesPart = (entries, tag) => {
210
- const ref = `${tag}Ref`;
211
- let xml = XML_HEAD;
212
- xml += `<w:${tag}s ${DOCX_NS}>`;
213
- xml += `<w:${tag} w:type="separator" w:id="-1"><w:p><w:pPr><w:spacing w:after="0" w:line="240" w:lineRule="auto"/></w:pPr>` +
214
- `<w:r><w:separator/></w:r></w:p></w:${tag}>`;
215
- xml += `<w:${tag} w:type="continuationSeparator" w:id="0"><w:p><w:pPr><w:spacing w:after="0" w:line="240" w:lineRule="auto"/></w:pPr>` +
216
- `<w:r><w:continuationSeparator/></w:r></w:p></w:${tag}>`;
217
- for (const n of entries) {
218
- xml += `<w:${tag} w:id="${xmlEscapeAttr(n.id)}"><w:p>` +
219
- `<w:r><w:rPr><w:vertAlign w:val="superscript"/></w:rPr><w:${ref}/></w:r>` +
220
- `<w:r><w:t xml:space="preserve">${xmlEscape(n.text || '')}</w:t></w:r></w:p></w:${tag}>`;
221
- }
222
- xml += `</w:${tag}s>`;
223
- return xml;
224
- };
225
-
226
- /* ---------- 文档属性 ---------- */
227
-
228
- const PROPS_NS = 'xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" ' +
229
- 'xmlns:dc="http://purl.org/dc/elements/1.1/" ' +
230
- 'xmlns:dcterms="http://purl.org/dc/terms/" ' +
231
- 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
232
-
233
- /** propsPart 生成 docProps/core.xml(标题/主题/作者/备注/最后修改者;全空则调用方不建部件) */
234
- export const propsPart = (p) => {
235
- const item = (tag, ns, v) => (String(v ?? '').trim() === '' ? '' : `<${ns}:${tag}>${xmlEscape(String(v))}</${ns}:${tag}>`);
236
- let xml = XML_HEAD;
237
- xml += `<cp:coreProperties ${PROPS_NS}>`;
238
- xml += item('title', 'dc', p.title);
239
- xml += item('subject', 'dc', p.subject);
240
- xml += item('creator', 'dc', p.creator);
241
- xml += item('description', 'dc', p.description);
242
- xml += item('lastModifiedBy', 'cp', p.lastModifiedBy);
243
- xml += '</cp:coreProperties>';
244
- return xml;
245
- };
246
-
247
- /** propsHasValue 文档属性是否有可写内容(空 props 不建部件,免得每个新文档都挂一份空 core.xml) */
248
- export const propsHasValue = (p) =>
249
- Boolean(p && ['title', 'subject', 'creator', 'description', 'lastModifiedBy'].some((k) => String(p[k] ?? '').trim() !== ''));
1
+ // [文件] web·docx·codec·parts —— 静态部件:包内路径/内容类型/关系类型常量,styles.xml / numbering.xml / comments.xml 生成
2
+ import { REL_BASE, xmlEscape, xmlEscapeAttr } from 'gbooxml';
3
+
4
+ /* ---------- 部件包内路径 ---------- */
5
+ export const PART_DOCUMENT = 'word/document.xml';
6
+ export const PART_STYLES = 'word/styles.xml';
7
+ export const PART_NUMBERING = 'word/numbering.xml';
8
+ export const PART_COMMENTS = 'word/comments.xml';
9
+ export const PART_SETTINGS = 'word/settings.xml';
10
+ export const PART_FOOTNOTES = 'word/footnotes.xml';
11
+ export const PART_ENDNOTES = 'word/endnotes.xml';
12
+ export const PART_PROPS = 'docProps/core.xml';
13
+
14
+ /* ---------- 部件内容类型 ---------- */
15
+ export const CT_DOCUMENT = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml';
16
+ export const CT_STYLES = 'application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml';
17
+ export const CT_NUMBERING = 'application/vnd.openxmlformats-officedocument.wordprocessingml.numbering+xml';
18
+ export const CT_HEADER = 'application/vnd.openxmlformats-officedocument.wordprocessingml.header+xml';
19
+ export const CT_FOOTER = 'application/vnd.openxmlformats-officedocument.wordprocessingml.footer+xml';
20
+ export const CT_COMMENTS = 'application/vnd.openxmlformats-officedocument.wordprocessingml.comments+xml';
21
+ export const CT_SETTINGS = 'application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml';
22
+ export const CT_FOOTNOTES = 'application/vnd.openxmlformats-officedocument.wordprocessingml.footnotes+xml';
23
+ export const CT_ENDNOTES = 'application/vnd.openxmlformats-officedocument.wordprocessingml.endnotes+xml';
24
+ export const CT_PROPS = 'application/vnd.openxmlformats-package.core-properties+xml';
25
+
26
+ /* ---------- 关系类型 ---------- */
27
+ export const REL_DOC = `${REL_BASE}officeDocument`;
28
+ export const REL_STYLES = `${REL_BASE}styles`;
29
+ export const REL_NUMBERING = `${REL_BASE}numbering`;
30
+ export const REL_HEADER = `${REL_BASE}header`;
31
+ export const REL_FOOTER = `${REL_BASE}footer`;
32
+ export const REL_COMMENTS = `${REL_BASE}comments`;
33
+ export const REL_SETTINGS = `${REL_BASE}settings`;
34
+ export const REL_HYPERLINK = `${REL_BASE}hyperlink`;
35
+ export const REL_IMAGE = `${REL_BASE}image`;
36
+ export const REL_FOOTNOTES = `${REL_BASE}footnotes`;
37
+ export const REL_ENDNOTES = `${REL_BASE}endnotes`;
38
+ /** 核心属性关系挂在包根关系表(_rels/.rels)上,不是 document 的 */
39
+ export const REL_PROPS = 'http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties';
40
+
41
+ /* ---------- 命名空间 ---------- */
42
+ export const NS_WORDML = 'http://schemas.openxmlformats.org/wordprocessingml/2006/main';
43
+ const NS_VML = 'urn:schemas-microsoft-com:vml';
44
+ const NS_OFFICE = 'urn:schemas-microsoft-com:office:office';
45
+ const NS_W10 = 'urn:schemas-microsoft-com:office:word';
46
+ const NS_REL_ATTR = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships';
47
+ const NS_WP_DRAW = 'http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing';
48
+ const NS_DRAWING_M = 'http://schemas.openxmlformats.org/drawingml/2006/main';
49
+ const NS_PIC_DRAW = 'http://schemas.openxmlformats.org/drawingml/2006/picture';
50
+
51
+ /** 正文类部件要声明的命名空间集合:w 正文、r 关系、wp 图片锚点、a 绘图、pic 图片 */
52
+ export const DOCX_NS = `xmlns:w="${NS_WORDML}" xmlns:r="${NS_REL_ATTR}" xmlns:wp="${NS_WP_DRAW}" xmlns:a="${NS_DRAWING_M}" xmlns:pic="${NS_PIC_DRAW}"`;
53
+
54
+ /** 水印组件(VML 要 v/o/w10 三套前缀,不带它们 Word 会把整个图形当无效 XML) */
55
+ export const VML_NS = `${DOCX_NS} xmlns:v="${NS_VML}" xmlns:o="${NS_OFFICE}" xmlns:w10="${NS_W10}"`;
56
+
57
+ export const XML_HEAD = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n`;
58
+
59
+ /* ---------- 装饰取值(只有我们写出去、不会被读回来的外观选择) ---------- */
60
+ /** [坑] 链接色必须带 "#":颜色规整只认 #rrggbb / rgb() 两种写法,写裸十六进制会被判成不可用而回退 */
61
+ export const RULE_COLOR = 'd0d7de'; // 分隔线
62
+ export const GRID_COLOR = 'd0d7de'; // 表格网格线
63
+ /** 正文字体:与编辑器 .rich-text-content 的默认字体同一份值(中文字形走 w:eastAsia) */
64
+ export const BODY_FONT = 'Microsoft YaHei';
65
+
66
+ /** 正文字号与行距(半点 / 240 分之一行):14px = 10.5pt;1.25 行 = 175(见 docx.js 的 WORD_LINE_RATIO) */
67
+ export const BODY_FONT_HALF_PT = 21;
68
+ const BODY_LINE = 175;
69
+ /** 段后(twips):编辑器 p 的 2px 段间距 */
70
+ const BODY_AFTER = 30;
71
+
72
+ /** 引用块左缩进(twips):编辑器 blockquote 的 14px 内边距 + 3px 左边框 */
73
+ export const QUOTE_INDENT_TWIPS = 255;
74
+
75
+ /** 代码块识别(写出端铺底色 + 等宽字体,回读端按这两个值认出代码行) */
76
+ export const CODE_FILL = 'F1F3F4';
77
+ export const CODE_FONT = 'Consolas';
78
+
79
+ /* ---------- 样式表 ---------- */
80
+
81
+ /**
82
+ * 各级标题样式的排版数值(字号半点 / 段前后 twips / 行距 240 分之一行)。
83
+ * [设计] 这些数值是排版契约:导出后的分页要与编辑器页面预览对得上,靠的就是把它们显式钉死,
84
+ * 挡住 Word 拿自带 Normal 样式替换。数值逐项对应 editor-content.css 的标题规则:
85
+ * 字号 = 1.875/1.5/1.25/1.125/1/0.875 em × 14px,行距 = 1.3 × 140,段前 18px、段后 3px。
86
+ * 改动数值等于改变已导出文档的排版,属于功能变更不是重构。
87
+ */
88
+ const HEADING_STYLES = [
89
+ { id: 'Title', name: 'Title', size: 39, bold: true, before: 270, after: 45, line: 182 },
90
+ { id: 'Heading1', name: 'heading 1', size: 39, bold: true, before: 270, after: 45, line: 182 },
91
+ { id: 'Heading2', name: 'heading 2', size: 32, bold: true, before: 270, after: 45, line: 182 },
92
+ { id: 'Heading3', name: 'heading 3', size: 26, bold: true, before: 270, after: 45, line: 182 },
93
+ { id: 'Heading4', name: 'heading 4', size: 24, bold: true, before: 270, after: 45, line: 182 },
94
+ { id: 'Heading5', name: 'heading 5', size: 21, bold: true, before: 270, after: 45, line: 182 },
95
+ { id: 'Heading6', name: 'heading 6', size: 18, bold: true, before: 270, after: 45, line: 182 }
96
+ ];
97
+
98
+ /** spacingXML 段前/段后(二十分之一点)与行距(240 分之一行) */
99
+ const spacingXml = (before, after, line) =>
100
+ `<w:spacing w:before="${before}" w:after="${after}" w:line="${line}" w:lineRule="auto"/>`;
101
+
102
+ export const stylesPart = () => {
103
+ let xml = XML_HEAD;
104
+ xml += `<w:styles xmlns:w="${NS_WORDML}">`;
105
+ xml += `<w:docDefaults><w:rPrDefault><w:rPr>`;
106
+ xml += `<w:rFonts w:ascii="${BODY_FONT}" w:hAnsi="${BODY_FONT}" w:eastAsia="${BODY_FONT}" w:cs="${BODY_FONT}"/>`;
107
+ xml += `<w:sz w:val="${BODY_FONT_HALF_PT}"/><w:szCs w:val="${BODY_FONT_HALF_PT}"/></w:rPr></w:rPrDefault>`;
108
+ xml += `<w:pPrDefault><w:pPr>${spacingXml(0, BODY_AFTER, BODY_LINE)}</w:pPr></w:pPrDefault></w:docDefaults>`;
109
+ xml += `<w:style w:type="paragraph" w:default="1" w:styleId="Normal"><w:name w:val="Normal"/><w:pPr>${spacingXml(0, BODY_AFTER, BODY_LINE)}</w:pPr></w:style>`;
110
+ for (const h of HEADING_STYLES) {
111
+ xml += `<w:style w:type="paragraph" w:styleId="${h.id}">`;
112
+ xml += `<w:name w:val="${h.name}"/>`;
113
+ xml += `<w:basedOn w:val="Normal"/>`;
114
+ xml += `<w:pPr>${spacingXml(h.before, h.after, h.line)}</w:pPr>`;
115
+ xml += '<w:rPr>';
116
+ if (h.bold) xml += '<w:b/>';
117
+ xml += `<w:sz w:val="${h.size}"/></w:rPr></w:style>`;
118
+ }
119
+ // 引用块样式:我们写出 pStyle="Quote" + 同样的左缩进,读回端按样式识别;
120
+ // 样式表里显式定义一份,Word 不再按自带 Quote 样式替换排版
121
+ xml += '<w:style w:type="paragraph" w:styleId="Quote"><w:name w:val="Quote"/><w:basedOn w:val="Normal"/>';
122
+ xml += `<w:pPr><w:ind w:left="${QUOTE_INDENT_TWIPS}"/></w:pPr></w:style>`;
123
+ xml += `</w:styles>`;
124
+ return xml;
125
+ };
126
+
127
+ /* ---------- 编号表 ---------- */
128
+
129
+ /** numId 1 = 项目符号,2 = 有序号,3/4 = 已勾选/未勾选待办(与正文里 numId 的取值对应)。
130
+ * 每级左缩进 1.6em × 14px = 22.4px = 336 twips,悬挂同宽:标记顶到栏左、正文从 22.4px 起,
131
+ * 与编辑器 ul/ol 的 padding-left 同一视觉(Word 的 720/360 会让列表正文比预览窄一截,提前折行) */
132
+ const LEVEL_INDENT = 336;
133
+ const LEVEL_HANGING = 336;
134
+
135
+ /** 四级项目符号字形与有序号格式(numbering.xml 只定义到 ilvl 3) */
136
+ const BULLET_GLYPHS = ['•', '◦', '-', '•'];
137
+ const ORDERED_FORMATS = ['decimal', 'lowerLetter', 'lowerRoman', 'decimal'];
138
+ /** 待办勾选字形(同一字形铺满四级;回读端据此把编号识别回待办列表) */
139
+ const TASK_CHECKED = '☑';
140
+ const TASK_UNCHECKED = '☐';
141
+
142
+ /** 一个编号层级。 [坑] lvl 子元素顺序固定:numFmt → lvlText → pPr */
143
+ const levelXml = (level, format, text) => {
144
+ const indent = LEVEL_INDENT * (level + 1);
145
+ return `<w:lvl w:ilvl="${level}"><w:numFmt w:val="${format}"/><w:lvlText w:val="${text}"/><w:pPr><w:ind w:left="${indent}" w:hanging="${LEVEL_HANGING}"/></w:pPr></w:lvl>`;
146
+ };
147
+
148
+ export const numberingPart = () => {
149
+ let xml = XML_HEAD;
150
+ xml += `<w:numbering xmlns:w="${NS_WORDML}">`;
151
+ xml += `<w:abstractNum w:abstractNumId="0">`;
152
+ BULLET_GLYPHS.forEach((glyph, level) => {
153
+ xml += levelXml(level, 'bullet', glyph);
154
+ });
155
+ xml += `</w:abstractNum>`;
156
+ xml += `<w:abstractNum w:abstractNumId="1">`;
157
+ ORDERED_FORMATS.forEach((format, level) => {
158
+ xml += levelXml(level, format, `%${level + 1}.`);
159
+ });
160
+ xml += `</w:abstractNum>`;
161
+ xml += `<w:abstractNum w:abstractNumId="2">`;
162
+ for (let level = 0; level <= 3; level += 1) xml += levelXml(level, 'bullet', TASK_CHECKED);
163
+ xml += `</w:abstractNum>`;
164
+ xml += `<w:abstractNum w:abstractNumId="3">`;
165
+ for (let level = 0; level <= 3; level += 1) xml += levelXml(level, 'bullet', TASK_UNCHECKED);
166
+ xml += `</w:abstractNum>`;
167
+ xml += `<w:num w:numId="1"><w:abstractNumId w:val="0"/></w:num>`;
168
+ xml += `<w:num w:numId="2"><w:abstractNumId w:val="1"/></w:num>`;
169
+ xml += `<w:num w:numId="3"><w:abstractNumId w:val="2"/></w:num>`;
170
+ xml += `<w:num w:numId="4"><w:abstractNumId w:val="3"/></w:num>`;
171
+ xml += `</w:numbering>`;
172
+ return xml;
173
+ };
174
+
175
+ /* ---------- 批注 ---------- */
176
+
177
+ /**
178
+ * commentsPart 生成 word/comments.xml(按 id 升序输出,保证 Word 里批注顺序稳定)
179
+ * 批注子集:一条批注就是一段纯文本——编辑器侧的回复/解决状态不进 docx,往返口径固定为「一条批注一段文字」。
180
+ * 数字 id 按正文出现顺序分配,comments.xml 只输出正文真正引用到的条目。
181
+ */
182
+ export const commentsPart = (comments, used) => {
183
+ const byKey = new Map((comments || []).map((c) => [c.key, c]));
184
+ const entries = [...used.entries()].map(([key, id]) => [id, key]).sort((a, b) => a[0] - b[0]);
185
+ let xml = XML_HEAD;
186
+ xml += `<w:comments ${DOCX_NS}>`;
187
+ for (const [id, key] of entries) {
188
+ const c = byKey.get(key);
189
+ if (!c) continue; // 正文引用了但清单里没有:跳过,不写一条空批注
190
+ const author = c.author || '匿名';
191
+ const date = c.date ? ` w:date="${xmlEscapeAttr(c.date)}"` : '';
192
+ xml += `<w:comment w:id="${id}" w:author="${xmlEscapeAttr(author)}"${date}>`;
193
+ xml += `<w:p><w:r><w:t xml:space="preserve">${xmlEscape(c.text || '')}</w:t></w:r></w:p>`;
194
+ xml += `</w:comment>`;
195
+ }
196
+ xml += `</w:comments>`;
197
+ return xml;
198
+ };
199
+
200
+ /* ---------- 脚注 / 尾注 ---------- */
201
+
202
+ /**
203
+ * notesPart 生成 word/footnotes.xml 或 word/endnotes.xml(tag = footnote | endnote)
204
+ * [设计] separators 两条占位(id -1/0)必须带:Word 每份文档都有,缺了它打开时不报错但脚注分隔线行为怪异。
205
+ * [设计] 条目全量写出、不过滤「没被引用」的:批注可以只写引用到的(键是编辑器自己分配的),
206
+ * 脚注不行——条目在部件里的位置就是 Word 的编号,过滤会整体改变序号;且引用可能藏在没解析到的角落里,
207
+ * 丢条目等于丢内容。内容是纯文本保底——富文本脚注导出后只剩文字。
208
+ */
209
+ export const notesPart = (entries, tag) => {
210
+ const ref = `${tag}Ref`;
211
+ let xml = XML_HEAD;
212
+ xml += `<w:${tag}s ${DOCX_NS}>`;
213
+ xml += `<w:${tag} w:type="separator" w:id="-1"><w:p><w:pPr><w:spacing w:after="0" w:line="240" w:lineRule="auto"/></w:pPr>` +
214
+ `<w:r><w:separator/></w:r></w:p></w:${tag}>`;
215
+ xml += `<w:${tag} w:type="continuationSeparator" w:id="0"><w:p><w:pPr><w:spacing w:after="0" w:line="240" w:lineRule="auto"/></w:pPr>` +
216
+ `<w:r><w:continuationSeparator/></w:r></w:p></w:${tag}>`;
217
+ for (const n of entries) {
218
+ xml += `<w:${tag} w:id="${xmlEscapeAttr(n.id)}"><w:p>` +
219
+ `<w:r><w:rPr><w:vertAlign w:val="superscript"/></w:rPr><w:${ref}/></w:r>` +
220
+ `<w:r><w:t xml:space="preserve">${xmlEscape(n.text || '')}</w:t></w:r></w:p></w:${tag}>`;
221
+ }
222
+ xml += `</w:${tag}s>`;
223
+ return xml;
224
+ };
225
+
226
+ /* ---------- 文档属性 ---------- */
227
+
228
+ const PROPS_NS = 'xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" ' +
229
+ 'xmlns:dc="http://purl.org/dc/elements/1.1/" ' +
230
+ 'xmlns:dcterms="http://purl.org/dc/terms/" ' +
231
+ 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"';
232
+
233
+ /** propsPart 生成 docProps/core.xml(标题/主题/作者/备注/最后修改者;全空则调用方不建部件) */
234
+ export const propsPart = (p) => {
235
+ const item = (tag, ns, v) => (String(v ?? '').trim() === '' ? '' : `<${ns}:${tag}>${xmlEscape(String(v))}</${ns}:${tag}>`);
236
+ let xml = XML_HEAD;
237
+ xml += `<cp:coreProperties ${PROPS_NS}>`;
238
+ xml += item('title', 'dc', p.title);
239
+ xml += item('subject', 'dc', p.subject);
240
+ xml += item('creator', 'dc', p.creator);
241
+ xml += item('description', 'dc', p.description);
242
+ xml += item('lastModifiedBy', 'cp', p.lastModifiedBy);
243
+ xml += '</cp:coreProperties>';
244
+ return xml;
245
+ };
246
+
247
+ /** propsHasValue 文档属性是否有可写内容(空 props 不建部件,免得每个新文档都挂一份空 core.xml) */
248
+ export const propsHasValue = (p) =>
249
+ Boolean(p && ['title', 'subject', 'creator', 'description', 'lastModifiedBy'].some((k) => String(p[k] ?? '').trim() !== ''));