@a200888213/gbdocx 0.6.0 → 0.6.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.
Files changed (76) hide show
  1. package/package.json +1 -1
  2. package/src/app/file-io.js +122 -122
  3. package/src/app/main.js +9 -9
  4. package/src/app/word/catalog.js +52 -52
  5. package/src/app/word/comments.js +112 -112
  6. package/src/app/word/doc.js +113 -113
  7. package/src/app/word/edit.js +72 -72
  8. package/src/app/word/hf.js +76 -76
  9. package/src/app/word/layout.js +155 -155
  10. package/src/app/word/notes.js +110 -110
  11. package/src/app/word/shared.js +22 -22
  12. package/src/app/word/view.js +3 -0
  13. package/src/app/word-app.js +1 -0
  14. package/src/app/word-sheets.js +101 -101
  15. package/src/kernel/docx/codec/blocks.js +123 -123
  16. package/src/kernel/docx/codec/carry.js +129 -129
  17. package/src/kernel/docx/codec/comments.js +25 -25
  18. package/src/kernel/docx/codec/docx.js +198 -198
  19. package/src/kernel/docx/codec/docxStyles.js +177 -177
  20. package/src/kernel/docx/codec/fonts.js +195 -195
  21. package/src/kernel/docx/codec/helpers.js +161 -161
  22. package/src/kernel/docx/codec/hfHtml.js +152 -152
  23. package/src/kernel/docx/codec/html.js +739 -739
  24. package/src/kernel/docx/codec/losses.js +420 -420
  25. package/src/kernel/docx/codec/markdown.js +170 -170
  26. package/src/kernel/docx/codec/parts.js +249 -249
  27. package/src/kernel/docx/codec/read.js +603 -603
  28. package/src/kernel/docx/codec/render.js +1156 -1156
  29. package/src/kernel/docx/codec/roundtrip.js +236 -236
  30. package/src/kernel/docx/codec/write.js +833 -833
  31. package/src/kernel/docx/comments.js +14 -14
  32. package/src/kernel/docx/pageGeometry.js +123 -123
  33. package/src/kernel/docx/pagination.js +481 -481
  34. package/src/kernel/docx/watermark.js +45 -45
  35. package/src/kernel/editor/constants.js +8 -8
  36. package/src/kernel/editor/engine/blocks.js +594 -594
  37. package/src/kernel/editor/engine/blocksEnter.js +109 -109
  38. package/src/kernel/editor/engine/blocksInsert.js +254 -254
  39. package/src/kernel/editor/engine/blocksStructure.js +226 -226
  40. package/src/kernel/editor/engine/commands.js +181 -181
  41. package/src/kernel/editor/engine/dom.js +646 -646
  42. package/src/kernel/editor/engine/history.js +149 -149
  43. package/src/kernel/editor/engine/inline.js +656 -656
  44. package/src/kernel/editor/engine/input.js +554 -554
  45. package/src/kernel/editor/engine/interaction.js +137 -137
  46. package/src/kernel/editor/engine/pointer.js +39 -39
  47. package/src/kernel/editor/engine/revision.js +138 -138
  48. package/src/kernel/editor/engine/schema.js +417 -417
  49. package/src/kernel/editor/engine/selection.js +119 -119
  50. package/src/kernel/editor/engine/state.js +118 -118
  51. package/src/kernel/editor/engine/tableClip.js +154 -154
  52. package/src/kernel/editor/engine/tableGrid.js +55 -55
  53. package/src/kernel/editor/engine/tableInteraction.js +388 -388
  54. package/src/kernel/editor/engine/tableMenu.js +250 -250
  55. package/src/kernel/editor/engine/tableOps.js +174 -174
  56. package/src/kernel/editor/engine/tables.js +732 -732
  57. package/src/kernel/editor/overlayGeometry.js +20 -20
  58. package/src/kernel/editor/utils.js +48 -48
  59. package/src/kernel/keyboard.js +114 -114
  60. package/src/kernel/textStyles.js +84 -84
  61. package/src/kernel/util/imageFile.js +91 -91
  62. package/src/kernel/util/shortcutKey.js +33 -33
  63. package/src/surface/editor-surface.js +600 -600
  64. package/src/ui/color-panel.js +69 -69
  65. package/src/ui/context-menu.js +133 -133
  66. package/src/ui/find-bar.js +151 -151
  67. package/src/ui/menubar.js +28 -28
  68. package/src/ui/panels.js +115 -115
  69. package/src/ui/prompt.js +89 -89
  70. package/src/ui/ribbon.js +485 -485
  71. package/src/ui/special-chars.js +39 -39
  72. package/src/ui/statusbar.js +81 -81
  73. package/src/ui/toolbar.js +32 -32
  74. package/styles/editor-content.css +440 -440
  75. package/styles/tokens.css +154 -154
  76. package/styles/word-panel.css +1 -1
@@ -1,198 +1,198 @@
1
- // [文件] web·docx·codec·docx —— Document 模型、页面几何、单位换算与读写编排(前端 .docx 唯一出入口)
2
- /*
3
- 本模块把编辑器内存态的正文 HTML 子集与 WordprocessingML 互转,是 Word 文档以 .docx 读写的唯一入口。
4
- 转换分两段:先归一到中间的块模型(paragraph/run/table 一套结构,见 blocks.js),再由两个方向各自消费——
5
- 写出走「块模型 → OOXML 部件」,读回走「OOXML 部件 → 块模型 → HTML」。中间层把 HTML 的
6
- 语义翻译与 OOXML 的拼装拆开,顺带让两个方向共用同一套块概念。
7
-
8
- 支持的子集:段落(对齐/缩进/行距)、标题 H1~H6、粗斜下划线删除线、字体颜色/字号/字体族与文字底色、
9
- 等宽代码段、超链接、无序与有序列表、表格(列宽/单元格底色/横向与纵向合并)、内嵌图片(data URL)、
10
- 分隔线、引用块、分页符、页眉页脚与页码、纸张尺寸/方向/页边距/分栏、批注、修订标记(含作者/日期)、
11
- 脚注与尾注(锚点 + 纯文本)、文档属性(docProps 标题/作者等)、整部件透传(theme/glossary/customXml 等)。
12
-
13
- 不支持(读时忽略、写时丢弃):文本框与形状、域代码、SmartArt 等。
14
- 这些内容读入时盘点成 losses 清单,保存前弹确认告知用户;写出的字节另做一次读回自检(codec/roundtrip.js)。
15
- 旧版二进制 .doc 是 OLE 复合文档,按魔数明确拒收。
16
- */
17
-
18
- import { readDocument } from './read.js';
19
- import { writeDocument } from './write.js';
20
- import { PAGE_SIZES, defaultPageConf } from '../constants.js';
21
-
22
- /** 页眉页脚重复模式。空串等价 HF_MODE.ALL——早于该设置存在的文档读回来就是空串 */
23
- export const HF_MODE = {
24
- ALL: 'all', // 每页显示同一段文字
25
- EXCEPT_FIRST: 'except-first', // 除首页外每页显示
26
- ODD_EVEN: 'odd-even', // 奇数页/偶数页各一份
27
- ODD_EVEN_FIRST: 'odd-even-first', // 奇偶页各一份,且首页再单独一份(Word 的 titlePg + evenAndOddHeaders)
28
- NONE: 'none' // 完全不显示
29
- };
30
-
31
- /** 该重复模式是否使用偶数页部件 */
32
- export const hfUsesEven = (mode) => mode === HF_MODE.ODD_EVEN || mode === HF_MODE.ODD_EVEN_FIRST;
33
-
34
- /** 该重复模式是否使用首页部件 */
35
- export const hfUsesFirst = (mode) => mode === HF_MODE.EXCEPT_FIRST || mode === HF_MODE.ODD_EVEN_FIRST;
36
-
37
- /** 页码的中文编号(w:pgNumType 的 w:fmt 取值;其余取值与缺省都是阿拉伯数字) */
38
- export const PAGE_NUM_CHINESE = 'chinese';
39
-
40
- /** 页码的短横格式「- 1 -」(w:pgNumType w:fmt="numberInDash",公文页码的标准写法) */
41
- export const PAGE_NUM_DASH = 'dash';
42
-
43
- /** 段落样式 id。这些 id 同时是前端编辑器的样式名与 OOXML 的 pStyle 值,改动会同时影响两端渲染 */
44
- export const STYLE_TITLE = 'Title';
45
- /**
46
- * 富文本文档:HTML 是编辑器正文,其余按节(w:sectPr)组织
47
- * @typedef {Object} Document
48
- * @property {string} html 编辑器正文 HTML 子集(节边界是 <div class="doc-sectionbreak"> 块)
49
- * @property {SectionConf[]} [sections] 节序列(文档序、末节收尾;渲染几何与写出都以它为准)
50
- * @property {Comment[]} [comments] 批注
51
- * @property {Array<{id:string,text:string}>} [footnotes] 脚注(正文锚点是 <sup class="doc-footnote" data-fn="id">)
52
- * @property {Array<{id:string,text:string}>} [endnotes] 尾注(锚点 class 为 doc-endnote)
53
- * @property {object} [props] 文档属性(docProps/core.xml 的标题/主题/作者/备注/最后修改者)
54
- * @property {number} [defaultTabStop] 默认制表位(twips;缺省 720,非缺省值随 settings.xml 往返)
55
- * @property {boolean} [compressPunctuation] 标点挤压(settings 的 characterSpacingControl=compressPunctuation)
56
- * @property {Array} [carry] 整部件透传清单(theme/glossary/customXml 等,字节 + 关系定义;见 codec/carry.js)
57
- * @property {Array<{kind:string,count:number}>} [losses] 读入时盘点的保存必丢清单(kind 标签表见 codec/losses.js)
58
- */
59
-
60
- /**
61
- * 一节的设置(w:sectPr):纸张几何 + 页眉页脚 + 页码 + 水印
62
- * @typedef {Object} SectionConf
63
- * @property {string} [type] 节切换类型(continuous/nextPage/evenPage/oddPage;空 = 缺省 nextPage)
64
- * @property {PageConf} [page] 该节的页面几何
65
- * @property {string} [hfMode] 该节的页眉页脚重复模式
66
- * @property {string} [header] 该节页眉
67
- * @property {string} [footer] 该节页脚
68
- * @property {string} [headerEven] 该节偶数页页眉(仅奇偶模式写部件)
69
- * @property {string} [footerEven] 该节偶数页页脚(仅奇偶模式写部件)
70
- * @property {string} [headerFirst] 该节首页页眉(仅首页不同模式写部件)
71
- * @property {string} [footerFirst] 该节首页页脚(仅首页不同模式写部件)
72
- * @property {PageNumberConf} [pageNumbers] 该节页码设置
73
- * @property {WatermarkConf} [watermark] 该节水印
74
- */
75
-
76
- /**
77
- * @typedef {Object} Comment 一条批注。正文锚点是 <span class="doc-comment" data-cmt="键">,Key 进 docx 时映射为数字 id
78
- * @property {string} key
79
- * @property {string} text
80
- * @property {string} [author]
81
- * @property {string} [date]
82
- */
83
-
84
- /**
85
- * 页码设置。落进页脚部件的 PAGE 域 + 节属性的 w:pgNumType
86
- * @typedef {Object} PageNumberConf
87
- * @property {boolean} enabled
88
- * @property {string} [align] left | center | right;空串 = left
89
- * @property {string} [format] arabic | chinese;空串 = arabic
90
- * @property {number} [start] 起始页码;<=0 视为 1
91
- */
92
-
93
- /**
94
- * 页面几何(页边距单位毫米)
95
- * @typedef {Object} PageConf
96
- * @property {string} [size] A4 | Letter | Legal
97
- * @property {string} [orientation] portrait | landscape
98
- * @property {{top:number,right:number,bottom:number,left:number}} [margins]
99
- * @property {number} [columns] 分栏数(0/1 = 单栏)
100
- * @property {number} [colGap] 栏间距,毫米
101
- * @property {boolean} [colSeparator] 栏间分隔线(w:cols 的 w:sep)
102
- * @property {number} [headerTop] 页眉距纸张上边缘,毫米(0 = Word 默认 12.7)
103
- * @property {number} [footerBottom] 页脚距纸张下边缘,毫米(0 = Word 默认 12.7)
104
- */
105
-
106
- /**
107
- * 页面水印(落进页眉部件的 VML 图形——Word 的水印就是页眉里的一张倾斜文字图)
108
- * [设计] 不设「平铺」字段:Word 的水印靠页眉每页重复,一页就一个图形,OOXML 侧没有平铺表达,
109
- * 留着它只能是一次会话内生效、存盘即丢的假开关。
110
- * @typedef {Object} WatermarkConf
111
- * @property {string} [data] 水印文字
112
- * @property {string} [color] #rrggbb
113
- * @property {number} [size] 字号(pt)
114
- * @property {number} [opacity] 透明度与 Word 的 VML 口径一致(0~1)
115
- */
116
-
117
- /**
118
- * 解析 .docx 为 Document(读回方向总入口,走 read.js)
119
- * @param {Uint8Array|ArrayBuffer} data
120
- * @returns {Promise<Document>}
121
- */
122
- export const parseDocx = (data) => readDocument(data);
123
-
124
- /**
125
- * 把 Document 打成 .docx(写出方向总入口,走 write.js)
126
- * @param {Document} doc
127
- * @returns {Promise<Uint8Array>}
128
- */
129
- export const buildDocx = (doc) => writeDocument(doc);
130
-
131
- /* ---------- 单位换算 ---------- */
132
-
133
- /** twips:二十分之一点。1 英寸 = 25.4mm = 1440 twips,这一串比例是换算的全部依据 */
134
- const TWIPS_PER_MM = 1440 / 25.4;
135
-
136
- /** 96dpi 下 1px = 1/96 英寸 */
137
- const TWIPS_PER_PX = 15;
138
-
139
- /** 保留一位小数(页面几何回读要毫米级的稳定值,多出来的浮点尾巴会让页面设置反复漂移) */
140
- export const round1 = (v) => Math.round(v * 10) / 10;
141
-
142
- export const mmToTwips = (mm) => Math.round(mm * TWIPS_PER_MM);
143
- export const twipsToMM = (tw) => tw / TWIPS_PER_MM;
144
-
145
- /** 纸张尺寸(twips,纵向)。Word 的 pgSz 就用这套数,回读时按最近者归名
146
- * [设计] 由 constants.js 的 mm 表经 mmToTwips 派生,不另立一份 twips 表(同纸两套单位,改一处必漏一处) */
147
- export const PAGE_SIZES_TWIPS = Object.fromEntries(
148
- Object.entries(PAGE_SIZES).map(([name, { w, h }]) => [name, [mmToTwips(w), mmToTwips(h)]])
149
- );
150
- export const pxToTwips = (px) => Math.round(px * TWIPS_PER_PX);
151
- export const twipsToPx = (tw) => tw / TWIPS_PER_PX;
152
-
153
- /** 1px = 0.75pt,w:sz 以半点计 */
154
- export const pxToHalfPoints = (px) => Math.round(px * 1.5);
155
- export const halfPointsToPx = (hp) => hp / 1.5;
156
-
157
- /**
158
- * Word 单倍行高相对字号的倍数:Microsoft YaHei 10.5pt 的单倍行高实测 18pt(24px),即 24/14。
159
- * 行距 auto 是「相对于单倍行高」的倍数,而 CSS 的 line-height 是「相对于字号」的倍数——
160
- * 两侧换算必须过这个比值,否则编辑器里的 1.25 行会被 Word 当成 1.25 × 24px 排成两倍高。
161
- * [约束] Word 的单倍行高随字体自带度量走:本值只对正文默认字体(导出侧写的就是 Microsoft YaHei)成立;
162
- * 整篇换用其他字体后,Word 的行高会按该字体度量变化,编辑器则始终按字号 × 倍数排版。
163
- */
164
- const WORD_LINE_RATIO = 12 / 7;
165
-
166
- /** CSS 行距倍数 → w:line(lineRule=auto):1.0 倍 = 140 */
167
- export const cssLineToWord = (cssLine) => Math.round((cssLine * 240) / WORD_LINE_RATIO);
168
-
169
- /** w:line(lineRule=auto)→ CSS 行距倍数 */
170
- export const wordLineToCss = (wordLine) => (wordLine * WORD_LINE_RATIO) / 240;
171
-
172
- /** 保留两位小数:twips 折 px、字号半点折 px 这类值取整会让整篇宽度/高度累积出可见误差 */
173
- export const round2 = (v) => Math.round(v * 100) / 100;
174
-
175
- /** 百分比写成 "25" 而不是 "25.00"(OOXML 的百分比属性对冗余小数不友好) */
176
- export const pctText = (v) => String(round2(v));
177
-
178
- /* ---------- 纸张几何 ---------- */
179
-
180
- /** 纸张尺寸(毫米,纵向)——尺寸表唯一源是 constants.js 的 PAGE_SIZES(未知档回落 A4) */
181
- const paperMM = (size, orientation) => {
182
- const { w, h } = PAGE_SIZES[size] || PAGE_SIZES.A4;
183
- return orientation === 'landscape' ? [h, w] : [w, h];
184
- };
185
-
186
- /** 正文栏宽(纸宽减左右页边距) */
187
- export const textWidthMM = (pc) => {
188
- const p = pc || defaultPageConf();
189
- let left = 25.4;
190
- let right = 25.4;
191
- if (p.margins) {
192
- left = p.margins.left;
193
- right = p.margins.right;
194
- }
195
- const [w] = paperMM(p.size, p.orientation);
196
- const width = w - left - right;
197
- return width > 10 ? width : 210 - 50.8;
198
- };
1
+ // [文件] web·docx·codec·docx —— Document 模型、页面几何、单位换算与读写编排(前端 .docx 唯一出入口)
2
+ /*
3
+ 本模块把编辑器内存态的正文 HTML 子集与 WordprocessingML 互转,是 Word 文档以 .docx 读写的唯一入口。
4
+ 转换分两段:先归一到中间的块模型(paragraph/run/table 一套结构,见 blocks.js),再由两个方向各自消费——
5
+ 写出走「块模型 → OOXML 部件」,读回走「OOXML 部件 → 块模型 → HTML」。中间层把 HTML 的
6
+ 语义翻译与 OOXML 的拼装拆开,顺带让两个方向共用同一套块概念。
7
+
8
+ 支持的子集:段落(对齐/缩进/行距)、标题 H1~H6、粗斜下划线删除线、字体颜色/字号/字体族与文字底色、
9
+ 等宽代码段、超链接、无序与有序列表、表格(列宽/单元格底色/横向与纵向合并)、内嵌图片(data URL)、
10
+ 分隔线、引用块、分页符、页眉页脚与页码、纸张尺寸/方向/页边距/分栏、批注、修订标记(含作者/日期)、
11
+ 脚注与尾注(锚点 + 纯文本)、文档属性(docProps 标题/作者等)、整部件透传(theme/glossary/customXml 等)。
12
+
13
+ 不支持(读时忽略、写时丢弃):文本框与形状、域代码、SmartArt 等。
14
+ 这些内容读入时盘点成 losses 清单,保存前弹确认告知用户;写出的字节另做一次读回自检(codec/roundtrip.js)。
15
+ 旧版二进制 .doc 是 OLE 复合文档,按魔数明确拒收。
16
+ */
17
+
18
+ import { readDocument } from './read.js';
19
+ import { writeDocument } from './write.js';
20
+ import { PAGE_SIZES, defaultPageConf } from '../constants.js';
21
+
22
+ /** 页眉页脚重复模式。空串等价 HF_MODE.ALL——早于该设置存在的文档读回来就是空串 */
23
+ export const HF_MODE = {
24
+ ALL: 'all', // 每页显示同一段文字
25
+ EXCEPT_FIRST: 'except-first', // 除首页外每页显示
26
+ ODD_EVEN: 'odd-even', // 奇数页/偶数页各一份
27
+ ODD_EVEN_FIRST: 'odd-even-first', // 奇偶页各一份,且首页再单独一份(Word 的 titlePg + evenAndOddHeaders)
28
+ NONE: 'none' // 完全不显示
29
+ };
30
+
31
+ /** 该重复模式是否使用偶数页部件 */
32
+ export const hfUsesEven = (mode) => mode === HF_MODE.ODD_EVEN || mode === HF_MODE.ODD_EVEN_FIRST;
33
+
34
+ /** 该重复模式是否使用首页部件 */
35
+ export const hfUsesFirst = (mode) => mode === HF_MODE.EXCEPT_FIRST || mode === HF_MODE.ODD_EVEN_FIRST;
36
+
37
+ /** 页码的中文编号(w:pgNumType 的 w:fmt 取值;其余取值与缺省都是阿拉伯数字) */
38
+ export const PAGE_NUM_CHINESE = 'chinese';
39
+
40
+ /** 页码的短横格式「- 1 -」(w:pgNumType w:fmt="numberInDash",公文页码的标准写法) */
41
+ export const PAGE_NUM_DASH = 'dash';
42
+
43
+ /** 段落样式 id。这些 id 同时是前端编辑器的样式名与 OOXML 的 pStyle 值,改动会同时影响两端渲染 */
44
+ export const STYLE_TITLE = 'Title';
45
+ /**
46
+ * 富文本文档:HTML 是编辑器正文,其余按节(w:sectPr)组织
47
+ * @typedef {Object} Document
48
+ * @property {string} html 编辑器正文 HTML 子集(节边界是 <div class="doc-sectionbreak"> 块)
49
+ * @property {SectionConf[]} [sections] 节序列(文档序、末节收尾;渲染几何与写出都以它为准)
50
+ * @property {Comment[]} [comments] 批注
51
+ * @property {Array<{id:string,text:string}>} [footnotes] 脚注(正文锚点是 <sup class="doc-footnote" data-fn="id">)
52
+ * @property {Array<{id:string,text:string}>} [endnotes] 尾注(锚点 class 为 doc-endnote)
53
+ * @property {object} [props] 文档属性(docProps/core.xml 的标题/主题/作者/备注/最后修改者)
54
+ * @property {number} [defaultTabStop] 默认制表位(twips;缺省 720,非缺省值随 settings.xml 往返)
55
+ * @property {boolean} [compressPunctuation] 标点挤压(settings 的 characterSpacingControl=compressPunctuation)
56
+ * @property {Array} [carry] 整部件透传清单(theme/glossary/customXml 等,字节 + 关系定义;见 codec/carry.js)
57
+ * @property {Array<{kind:string,count:number}>} [losses] 读入时盘点的保存必丢清单(kind 标签表见 codec/losses.js)
58
+ */
59
+
60
+ /**
61
+ * 一节的设置(w:sectPr):纸张几何 + 页眉页脚 + 页码 + 水印
62
+ * @typedef {Object} SectionConf
63
+ * @property {string} [type] 节切换类型(continuous/nextPage/evenPage/oddPage;空 = 缺省 nextPage)
64
+ * @property {PageConf} [page] 该节的页面几何
65
+ * @property {string} [hfMode] 该节的页眉页脚重复模式
66
+ * @property {string} [header] 该节页眉
67
+ * @property {string} [footer] 该节页脚
68
+ * @property {string} [headerEven] 该节偶数页页眉(仅奇偶模式写部件)
69
+ * @property {string} [footerEven] 该节偶数页页脚(仅奇偶模式写部件)
70
+ * @property {string} [headerFirst] 该节首页页眉(仅首页不同模式写部件)
71
+ * @property {string} [footerFirst] 该节首页页脚(仅首页不同模式写部件)
72
+ * @property {PageNumberConf} [pageNumbers] 该节页码设置
73
+ * @property {WatermarkConf} [watermark] 该节水印
74
+ */
75
+
76
+ /**
77
+ * @typedef {Object} Comment 一条批注。正文锚点是 <span class="doc-comment" data-cmt="键">,Key 进 docx 时映射为数字 id
78
+ * @property {string} key
79
+ * @property {string} text
80
+ * @property {string} [author]
81
+ * @property {string} [date]
82
+ */
83
+
84
+ /**
85
+ * 页码设置。落进页脚部件的 PAGE 域 + 节属性的 w:pgNumType
86
+ * @typedef {Object} PageNumberConf
87
+ * @property {boolean} enabled
88
+ * @property {string} [align] left | center | right;空串 = left
89
+ * @property {string} [format] arabic | chinese;空串 = arabic
90
+ * @property {number} [start] 起始页码;<=0 视为 1
91
+ */
92
+
93
+ /**
94
+ * 页面几何(页边距单位毫米)
95
+ * @typedef {Object} PageConf
96
+ * @property {string} [size] A4 | Letter | Legal
97
+ * @property {string} [orientation] portrait | landscape
98
+ * @property {{top:number,right:number,bottom:number,left:number}} [margins]
99
+ * @property {number} [columns] 分栏数(0/1 = 单栏)
100
+ * @property {number} [colGap] 栏间距,毫米
101
+ * @property {boolean} [colSeparator] 栏间分隔线(w:cols 的 w:sep)
102
+ * @property {number} [headerTop] 页眉距纸张上边缘,毫米(0 = Word 默认 12.7)
103
+ * @property {number} [footerBottom] 页脚距纸张下边缘,毫米(0 = Word 默认 12.7)
104
+ */
105
+
106
+ /**
107
+ * 页面水印(落进页眉部件的 VML 图形——Word 的水印就是页眉里的一张倾斜文字图)
108
+ * [设计] 不设「平铺」字段:Word 的水印靠页眉每页重复,一页就一个图形,OOXML 侧没有平铺表达,
109
+ * 留着它只能是一次会话内生效、存盘即丢的假开关。
110
+ * @typedef {Object} WatermarkConf
111
+ * @property {string} [data] 水印文字
112
+ * @property {string} [color] #rrggbb
113
+ * @property {number} [size] 字号(pt)
114
+ * @property {number} [opacity] 透明度与 Word 的 VML 口径一致(0~1)
115
+ */
116
+
117
+ /**
118
+ * 解析 .docx 为 Document(读回方向总入口,走 read.js)
119
+ * @param {Uint8Array|ArrayBuffer} data
120
+ * @returns {Promise<Document>}
121
+ */
122
+ export const parseDocx = (data) => readDocument(data);
123
+
124
+ /**
125
+ * 把 Document 打成 .docx(写出方向总入口,走 write.js)
126
+ * @param {Document} doc
127
+ * @returns {Promise<Uint8Array>}
128
+ */
129
+ export const buildDocx = (doc) => writeDocument(doc);
130
+
131
+ /* ---------- 单位换算 ---------- */
132
+
133
+ /** twips:二十分之一点。1 英寸 = 25.4mm = 1440 twips,这一串比例是换算的全部依据 */
134
+ const TWIPS_PER_MM = 1440 / 25.4;
135
+
136
+ /** 96dpi 下 1px = 1/96 英寸 */
137
+ const TWIPS_PER_PX = 15;
138
+
139
+ /** 保留一位小数(页面几何回读要毫米级的稳定值,多出来的浮点尾巴会让页面设置反复漂移) */
140
+ export const round1 = (v) => Math.round(v * 10) / 10;
141
+
142
+ export const mmToTwips = (mm) => Math.round(mm * TWIPS_PER_MM);
143
+ export const twipsToMM = (tw) => tw / TWIPS_PER_MM;
144
+
145
+ /** 纸张尺寸(twips,纵向)。Word 的 pgSz 就用这套数,回读时按最近者归名
146
+ * [设计] 由 constants.js 的 mm 表经 mmToTwips 派生,不另立一份 twips 表(同纸两套单位,改一处必漏一处) */
147
+ export const PAGE_SIZES_TWIPS = Object.fromEntries(
148
+ Object.entries(PAGE_SIZES).map(([name, { w, h }]) => [name, [mmToTwips(w), mmToTwips(h)]])
149
+ );
150
+ export const pxToTwips = (px) => Math.round(px * TWIPS_PER_PX);
151
+ export const twipsToPx = (tw) => tw / TWIPS_PER_PX;
152
+
153
+ /** 1px = 0.75pt,w:sz 以半点计 */
154
+ export const pxToHalfPoints = (px) => Math.round(px * 1.5);
155
+ export const halfPointsToPx = (hp) => hp / 1.5;
156
+
157
+ /**
158
+ * Word 单倍行高相对字号的倍数:Microsoft YaHei 10.5pt 的单倍行高实测 18pt(24px),即 24/14。
159
+ * 行距 auto 是「相对于单倍行高」的倍数,而 CSS 的 line-height 是「相对于字号」的倍数——
160
+ * 两侧换算必须过这个比值,否则编辑器里的 1.25 行会被 Word 当成 1.25 × 24px 排成两倍高。
161
+ * [约束] Word 的单倍行高随字体自带度量走:本值只对正文默认字体(导出侧写的就是 Microsoft YaHei)成立;
162
+ * 整篇换用其他字体后,Word 的行高会按该字体度量变化,编辑器则始终按字号 × 倍数排版。
163
+ */
164
+ const WORD_LINE_RATIO = 12 / 7;
165
+
166
+ /** CSS 行距倍数 → w:line(lineRule=auto):1.0 倍 = 140 */
167
+ export const cssLineToWord = (cssLine) => Math.round((cssLine * 240) / WORD_LINE_RATIO);
168
+
169
+ /** w:line(lineRule=auto)→ CSS 行距倍数 */
170
+ export const wordLineToCss = (wordLine) => (wordLine * WORD_LINE_RATIO) / 240;
171
+
172
+ /** 保留两位小数:twips 折 px、字号半点折 px 这类值取整会让整篇宽度/高度累积出可见误差 */
173
+ export const round2 = (v) => Math.round(v * 100) / 100;
174
+
175
+ /** 百分比写成 "25" 而不是 "25.00"(OOXML 的百分比属性对冗余小数不友好) */
176
+ export const pctText = (v) => String(round2(v));
177
+
178
+ /* ---------- 纸张几何 ---------- */
179
+
180
+ /** 纸张尺寸(毫米,纵向)——尺寸表唯一源是 constants.js 的 PAGE_SIZES(未知档回落 A4) */
181
+ const paperMM = (size, orientation) => {
182
+ const { w, h } = PAGE_SIZES[size] || PAGE_SIZES.A4;
183
+ return orientation === 'landscape' ? [h, w] : [w, h];
184
+ };
185
+
186
+ /** 正文栏宽(纸宽减左右页边距) */
187
+ export const textWidthMM = (pc) => {
188
+ const p = pc || defaultPageConf();
189
+ let left = 25.4;
190
+ let right = 25.4;
191
+ if (p.margins) {
192
+ left = p.margins.left;
193
+ right = p.margins.right;
194
+ }
195
+ const [w] = paperMM(p.size, p.orientation);
196
+ const width = w - left - right;
197
+ return width > 10 ? width : 210 - 50.8;
198
+ };