@a200888213/gbdocx 0.5.1 → 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 +22 -6
  17. package/src/app/word-app.js +486 -467
  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,420 +1,420 @@
1
- // [文件] web·docx·codec·losses —— 读入时盘点「保存必丢」的不支持特性(保存前确认弹窗的数据源;标签表与文案在此唯一)
2
- /*
3
- 口径与 docx.js 文件头的「不支持」清单一一对应:这些内容读得回来(或至少读得见),
4
- 但写出链路没有它们的位置,保存即从文件里消失。盘点放在读入侧而不是保存侧——
5
- 保存时模型里已经没有它们了,只有读入那一刻还数得清。
6
-
7
- [设计] 只盘「用户看得见的内容」(形状/域/嵌入对象/批注状态/页眉页脚对象/公式/书签/隐藏文字等)。
8
- 已实现往返通路的不在此列:脚注尾注、修订作者、多节版式、上下标、缩进段距、主题色、制表位、
9
- 内容控件内容(展开读,仅控件行为留在这里)、目录域、页眉页脚富文本、表样式(摊平为直接格式)。
10
- */
11
- import { parseNode } from 'gbooxml';
12
- import { HF_FIELD_KINDS } from '../../editor/engine/schema.js';
13
- import { PART_COMMENTS, PART_DOCUMENT, PART_ENDNOTES, PART_FOOTNOTES, PART_NUMBERING, PART_SETTINGS, PART_STYLES } from './parts.js';
14
- import { isRuleShape } from './render.js';
15
-
16
- /** 各类丢失的人话标签(确认框文案用;key 与盘点出的 kind 一致) */
17
- const LOSS_LABEL = {
18
- ole: '嵌入对象',
19
- field: '域代码(引用等)',
20
- shape: '文本框与形状(含图表/SmartArt)',
21
- sdt: '内容控件的控件行为',
22
- math: '公式(OMML)',
23
- bookmark: '书签',
24
- hidden: '隐藏文字',
25
- rtl: '从右到左排版',
26
- imageAdjust: '图片裁剪/调整',
27
- imageLayout: '浮动图片的环绕/定位',
28
- symbol: '符号字符',
29
- formField: '表单域',
30
- move: '移动修订',
31
- lineNumber: '行号',
32
- pageBorder: '页面边框',
33
- background: '页面背景',
34
- protection: '文档保护',
35
- hyphenation: '自动断字',
36
- macro: 'VBA 宏',
37
- commentState: '批注的回复与解决状态',
38
- headerFooterObject: '页眉页脚中的表格/图片',
39
- styles: '自定义样式',
40
- numbering: '自定义编号格式',
41
- docGrid: '文档网格(行距网格)',
42
- textDirection: '竖排/文字方向',
43
- tableFormat: '表格单元格格式(单元格边框/重复表头)',
44
- noteFormat: '脚注/尾注的格式与分段',
45
- commentFormat: '批注的格式与分段',
46
- trackChanges: '修订跟踪开关(保存后关闭)',
47
- brokenPart: '无法解析的部件(其内容已丢失)'
48
- };
49
-
50
- /** 丢失清单的可读文案(「域代码(引用等) ×2、嵌入对象」) */
51
- export const lossText = (losses) =>
52
- (losses || [])
53
- .filter((l) => LOSS_LABEL[l.kind])
54
- .map((l) => (l.count > 1 ? `${LOSS_LABEL[l.kind]} ×${l.count}` : LOSS_LABEL[l.kind]))
55
- .join('、');
56
-
57
- /** 深度遍历(计数用,不管顺序) */
58
- const walkAll = (node, visit) => {
59
- const stack = [node];
60
- while (stack.length > 0) {
61
- const n = stack.pop();
62
- visit(n);
63
- for (const k of n.kids) stack.push(k);
64
- }
65
- };
66
-
67
- /** 子树里有没有 blip(图片的关系锚点;带它的 drawing/pict 是内嵌图片,不算丢失) */
68
- const hasBlip = (n) => {
69
- let found = false;
70
- walkAll(n, (x) => {
71
- if (x.name === 'blip') found = true;
72
- });
73
- return found;
74
- };
75
-
76
- /** 嵌入对象:document 的关系里挂着 oleObject/package(OLE 复合对象、附加程序) */
77
- const oleCount = (pkg) => {
78
- let n = 0;
79
- for (const rel of pkg.rels(PART_DOCUMENT).values()) {
80
- if (/\/oleObject$|\/package$/.test(rel.type)) n += 1;
81
- }
82
- return n;
83
- };
84
-
85
- /** VBA 宏(.docm 的 vbaProject 部件不进出链路;文件能打开,宏保存即丢) */
86
- const macroCount = (pkg) => (pkg.has('word/vbaProject.bin') ? 1 : 0);
87
-
88
- /** 批注线程丢失:commentsExtended(回复/解决状态)整块不往返,多段批注固定压成一段 */
89
- const commentStateCount = (pkg) => {
90
- let n = pkg.has('word/commentsExtended.xml') ? 1 : 0;
91
- const raw = pkg.part(PART_COMMENTS);
92
- if (!raw) return n;
93
- let tree;
94
- try {
95
- tree = parseNode(raw);
96
- } catch {
97
- return n;
98
- }
99
- for (const c of tree.children('comment')) {
100
- if (c.children('p').length > 1) n += 1;
101
- }
102
- return n;
103
- };
104
-
105
- /** 设置部件里的丢失:文档保护 / 自动断字 / 修订跟踪(settings 是重写部件,原设置不保留) */
106
- const settingsLosses = (pkg) => {
107
- const raw = pkg.part(PART_SETTINGS);
108
- if (!raw) return {};
109
- let tree;
110
- try {
111
- tree = parseNode(raw);
112
- } catch {
113
- return {};
114
- }
115
- const track = tree.child('trackChanges');
116
- return {
117
- protection: tree.child('documentProtection') ? 1 : 0,
118
- hyphenation: tree.child('autoHyphenation') ? 1 : 0,
119
- trackChanges: track && track.attr('val') !== '0' && track.attr('val') !== 'false' ? 1 : 0
120
- };
121
- };
122
-
123
- /** styles.xml 整件重写:Word 内置样式在 Word 侧仍有同名定义,真正丢的是用户自定义样式(w:customStyle="1") */
124
- const customStyleCount = (pkg) => {
125
- const raw = pkg.part(PART_STYLES);
126
- if (!raw) return 0;
127
- let tree;
128
- try {
129
- tree = parseNode(raw);
130
- } catch {
131
- return 0;
132
- }
133
- let n = 0;
134
- walkAll(tree, (x) => {
135
- const custom = x.attr('customStyle');
136
- if (x.name === 'style' && (custom === '1' || custom === 'true')) n += 1;
137
- });
138
- return n;
139
- };
140
-
141
- /** numbering.xml 由固定模板重建:非内置格式(中文编号等)与层级覆盖(起始值/重排)都会丢 */
142
- const SUPPORTED_NUM_FMT = new Set(['bullet', 'decimal', 'lowerLetter', 'lowerRoman']);
143
- const numberingLossCount = (pkg) => {
144
- const raw = pkg.part(PART_NUMBERING);
145
- if (!raw) return 0;
146
- let tree;
147
- try {
148
- tree = parseNode(raw);
149
- } catch {
150
- return 0;
151
- }
152
- let n = 0;
153
- walkAll(tree, (x) => {
154
- if (x.name === 'numFmt' && !SUPPORTED_NUM_FMT.has(x.attr('val'))) n += 1;
155
- else if (x.name === 'lvlOverride' || x.name === 'startOverride') n += 1;
156
- });
157
- return n;
158
- };
159
-
160
- /** 无法解析的部件:读入侧静默降级为空(页眉页脚/批注/脚注等),保存即把原内容丢掉 */
161
- const brokenPartCount = (pkg) => {
162
- let n = 0;
163
- const names = [...pkg.paths('word/header'), ...pkg.paths('word/footer'), PART_COMMENTS, PART_FOOTNOTES, PART_ENDNOTES];
164
- for (const name of names) {
165
- const raw = pkg.part(name);
166
- if (!raw) continue;
167
- try {
168
- parseNode(raw);
169
- } catch {
170
- n += 1;
171
- }
172
- }
173
- return n;
174
- };
175
-
176
- /** 富文本格式标签(批注/脚注压成纯文本时丢失;vertAlign 除外——脚注引用号本来就有) */
177
- const RICH_RUN_TAGS = new Set(['b', 'i', 'u', 'strike', 'dstrike', 'color', 'sz', 'szCs', 'rFonts', 'highlight', 'shd', 'em', 'caps', 'smallCaps', 'spacing']);
178
- const hasRichRun = (node) => {
179
- let rich = false;
180
- walkAll(node, (x) => {
181
- if (x.name === 'rPr' && x.kids.some((k) => RICH_RUN_TAGS.has(k.name))) rich = true;
182
- });
183
- return rich;
184
- };
185
-
186
- /** 批注格式:单段批注的格式同样会压成纯文本(多段另由 commentState 盘点) */
187
- const commentFormatCount = (pkg) => {
188
- const raw = pkg.part(PART_COMMENTS);
189
- if (!raw) return 0;
190
- let tree;
191
- try {
192
- tree = parseNode(raw);
193
- } catch {
194
- return 0;
195
- }
196
- return tree.children('comment').filter(hasRichRun).length;
197
- };
198
-
199
- /** 脚注/尾注:多段或带格式的正文压成一段纯文本 */
200
- const noteFormatCount = (pkg) => {
201
- let n = 0;
202
- for (const part of [PART_FOOTNOTES, PART_ENDNOTES]) {
203
- const raw = pkg.part(part);
204
- if (!raw) continue;
205
- let tree;
206
- try {
207
- tree = parseNode(raw);
208
- } catch {
209
- continue;
210
- }
211
- const tag = part === PART_FOOTNOTES ? 'footnote' : 'endnote';
212
- for (const note of tree.children(tag)) {
213
- const type = note.attr('type');
214
- if (type === 'separator' || type === 'continuationSeparator') continue;
215
- if (note.children('p').length > 1 || hasRichRun(note)) n += 1;
216
- }
217
- }
218
- return n;
219
- };
220
-
221
- /** 图片调整类标记(裁剪与亮度/对比/双色调/灰阶/黑白/透明度等) */
222
- const IMAGE_ADJUST_TAGS = new Set(['srcRect', 'lum', 'duotone', 'grayscl', 'biLevel', 'alphaModFix', 'blur', 'prstShdw', 'glow']);
223
-
224
- /** 正文一次遍历盘点:域 / 形状 / 内容控件 / 公式 / 书签 / 隐藏文字 / RTL / 图片调整 / 符号 / 表单域 / 移动修订 / 行号 / 页面边框 / 垂直对齐 / 页面背景 */
225
- const bodyLosses = (body) => {
226
- const c = {
227
- field: 0,
228
- shape: 0,
229
- sdt: 0,
230
- math: 0,
231
- bookmark: 0,
232
- hidden: 0,
233
- rtl: 0,
234
- imageAdjust: 0,
235
- imageLayout: 0,
236
- symbol: 0,
237
- formField: 0,
238
- move: 0,
239
- lineNumber: 0,
240
- pageBorder: 0,
241
- background: 0,
242
- docGrid: 0,
243
- textDirection: 0,
244
- tableFormat: 0
245
- };
246
- // 页面背景挂在 document 根上(不在 body 子树里)
247
- if (body.parent?.name === 'document' && body.parent.child('background')) c.background = 1;
248
- const countOne = (k) => {
249
- switch (k.name) {
250
- case 'instrText': {
251
- // 目录域随目录块往返(PAGEREF 是它的内部机制);其余域写出链路不认,静默丢弃
252
- const code = k.text.trim().toUpperCase().split(/\s+/)[0] || '';
253
- if (code !== 'TOC' && code !== 'PAGEREF') c.field += 1;
254
- break;
255
- }
256
- case 'fldSimple': {
257
- // 简单域:缓存文字保住(render 侧读子 run),域本身不往返
258
- const code = (k.attr('instr') || '').trim().toUpperCase().split(/\s+/)[0] || '';
259
- if (code !== 'TOC' && code !== 'PAGEREF') c.field += 1;
260
- break;
261
- }
262
- case 'sdt':
263
- c.sdt += 1;
264
- break;
265
- case 'oMathPara':
266
- c.math += 1;
267
- break;
268
- case 'oMath':
269
- if (k.parent?.name !== 'oMathPara') c.math += 1;
270
- break;
271
- case 'bookmarkStart':
272
- // 自动书签(_GoBack 等)不算用户内容
273
- if (!(k.attr('name') || '').startsWith('_')) c.bookmark += 1;
274
- break;
275
- case 'sym':
276
- c.symbol += 1;
277
- break;
278
- case 'ffData':
279
- c.formField += 1;
280
- break;
281
- case 'moveFrom':
282
- case 'moveTo':
283
- c.move += 1;
284
- break;
285
- case 'lnNumType':
286
- c.lineNumber += 1;
287
- break;
288
- case 'pgBorders':
289
- c.pageBorder += 1;
290
- break;
291
- case 'docGrid':
292
- c.docGrid += 1;
293
- break;
294
- case 'textDirection': {
295
- const dir = k.attr('val');
296
- if (dir !== '' && dir !== 'lrTb') c.textDirection += 1;
297
- break;
298
- }
299
- case 'tcBorders':
300
- case 'tcMar':
301
- case 'tblHeader':
302
- case 'tblInd':
303
- case 'tblCellSpacing':
304
- c.tableFormat += 1;
305
- break;
306
- case 'bidi':
307
- case 'rtl':
308
- c.rtl += 1;
309
- break;
310
- case 'vanish': {
311
- const v = k.attr('val');
312
- if (v !== '0' && v !== 'false') c.hidden += 1;
313
- break;
314
- }
315
- default:
316
- if (IMAGE_ADJUST_TAGS.has(k.name)) c.imageAdjust += 1;
317
- }
318
- };
319
- // 图形容器只数最外层:v:shape 可能包在 w:pict 里,两个都数会把一个对象算两份
320
- const walk = (el, inShape) => {
321
- for (const k of el.kids) {
322
- if (!inShape) {
323
- if (k.name === 'drawing' || k.name === 'pict') {
324
- // 带 blip 的走内嵌图片;其余(图表骨架、SmartArt、形状、文本框)没有写出位置
325
- if (!hasBlip(k)) {
326
- // 水平分隔线不算丢:它会被折成段落下边框,线宽/线色/位置都带得回去
327
- if (!isRuleShape(k)) c.shape += 1;
328
- walk(k, true);
329
- continue;
330
- }
331
- // 浮动图片(wp:anchor):图片本身保留,但环绕方式/相对定位写不回去,保存前要告知
332
- let anchored = false;
333
- walkAll(k, (x) => {
334
- if (x.name === 'anchor') anchored = true;
335
- });
336
- if (anchored) c.imageLayout += 1;
337
- } else if (k.name === 'shape') {
338
- // VML 形状(页眉里的水印形状不会走到这里——那是页眉部件,不是正文)
339
- c.shape += 1;
340
- walk(k, true);
341
- continue;
342
- } else {
343
- countOne(k);
344
- }
345
- }
346
- walk(k, false);
347
- }
348
- };
349
- walk(body, false);
350
- return c;
351
- };
352
-
353
- /** 页眉页脚里除 PAGE/NUMPAGES/SECTIONPAGES 之外的域(写出只保留缓存文字,域语义丢失) */
354
- const headerFooterFieldCount = (pkg) => {
355
- let n = 0;
356
- for (const name of [...pkg.paths('word/header'), ...pkg.paths('word/footer')]) {
357
- const raw = pkg.part(name);
358
- if (!raw) continue;
359
- let tree;
360
- try {
361
- tree = parseNode(raw);
362
- } catch {
363
- continue;
364
- }
365
- walkAll(tree, (x) => {
366
- const code = x.name === 'instrText'
367
- ? x.text.trim().toUpperCase().split(/\s+/)[0] || ''
368
- : x.name === 'fldSimple' ? (x.attr('instr') || '').trim().toUpperCase().split(/\s+/)[0] || '' : '';
369
- if (code && !HF_FIELD_KINDS.has(code)) n += 1;
370
- });
371
- }
372
- return n;
373
- };
374
-
375
- /** 页眉页脚部件里的表格/图片(往返只保留文本与基础格式;水印那张旋转文字图是保留下来的,按 shape type 排除) */
376
- const headerFooterObjectCount = (pkg) => {
377
- let n = 0;
378
- for (const name of [...pkg.paths('word/header'), ...pkg.paths('word/footer')]) {
379
- const raw = pkg.part(name);
380
- if (!raw) continue;
381
- let tree;
382
- try {
383
- tree = parseNode(raw);
384
- } catch {
385
- continue;
386
- }
387
- walkAll(tree, (x) => {
388
- if (x.name === 'tbl' || x.name === 'blip') n += 1;
389
- else if (x.name === 'shape' && x.attr('type') !== '#_x0000_t136') n += 1;
390
- });
391
- }
392
- return n;
393
- };
394
-
395
- /**
396
- * 盘点一份 .docx 读入后「保存必丢」的内容
397
- * @param {import('gbooxml').OpcReader} pkg 已打开的包
398
- * @param {import('gbooxml').OoxmlNode} body document.xml 的 body 节点
399
- * @returns {Array<{kind: string, count: number}>} 非空清单(空数组 = 没有已知丢失)
400
- */
401
- export const collectLosses = (pkg, body) => {
402
- const bodyCounts = bodyLosses(body);
403
- bodyCounts.field += headerFooterFieldCount(pkg);
404
- const counts = {
405
- ole: oleCount(pkg),
406
- macro: macroCount(pkg),
407
- commentState: commentStateCount(pkg),
408
- commentFormat: commentFormatCount(pkg),
409
- headerFooterObject: headerFooterObjectCount(pkg),
410
- styles: customStyleCount(pkg),
411
- numbering: numberingLossCount(pkg),
412
- noteFormat: noteFormatCount(pkg),
413
- brokenPart: brokenPartCount(pkg),
414
- ...settingsLosses(pkg),
415
- ...bodyCounts
416
- };
417
- return Object.entries(counts)
418
- .filter(([, count]) => count > 0)
419
- .map(([kind, count]) => ({ kind, count }));
420
- };
1
+ // [文件] web·docx·codec·losses —— 读入时盘点「保存必丢」的不支持特性(保存前确认弹窗的数据源;标签表与文案在此唯一)
2
+ /*
3
+ 口径与 docx.js 文件头的「不支持」清单一一对应:这些内容读得回来(或至少读得见),
4
+ 但写出链路没有它们的位置,保存即从文件里消失。盘点放在读入侧而不是保存侧——
5
+ 保存时模型里已经没有它们了,只有读入那一刻还数得清。
6
+
7
+ [设计] 只盘「用户看得见的内容」(形状/域/嵌入对象/批注状态/页眉页脚对象/公式/书签/隐藏文字等)。
8
+ 已实现往返通路的不在此列:脚注尾注、修订作者、多节版式、上下标、缩进段距、主题色、制表位、
9
+ 内容控件内容(展开读,仅控件行为留在这里)、目录域、页眉页脚富文本、表样式(摊平为直接格式)。
10
+ */
11
+ import { parseNode } from 'gbooxml';
12
+ import { HF_FIELD_KINDS } from '../../editor/engine/schema.js';
13
+ import { PART_COMMENTS, PART_DOCUMENT, PART_ENDNOTES, PART_FOOTNOTES, PART_NUMBERING, PART_SETTINGS, PART_STYLES } from './parts.js';
14
+ import { isRuleShape } from './render.js';
15
+
16
+ /** 各类丢失的人话标签(确认框文案用;key 与盘点出的 kind 一致) */
17
+ const LOSS_LABEL = {
18
+ ole: '嵌入对象',
19
+ field: '域代码(引用等)',
20
+ shape: '文本框与形状(含图表/SmartArt)',
21
+ sdt: '内容控件的控件行为',
22
+ math: '公式(OMML)',
23
+ bookmark: '书签',
24
+ hidden: '隐藏文字',
25
+ rtl: '从右到左排版',
26
+ imageAdjust: '图片裁剪/调整',
27
+ imageLayout: '浮动图片的环绕/定位',
28
+ symbol: '符号字符',
29
+ formField: '表单域',
30
+ move: '移动修订',
31
+ lineNumber: '行号',
32
+ pageBorder: '页面边框',
33
+ background: '页面背景',
34
+ protection: '文档保护',
35
+ hyphenation: '自动断字',
36
+ macro: 'VBA 宏',
37
+ commentState: '批注的回复与解决状态',
38
+ headerFooterObject: '页眉页脚中的表格/图片',
39
+ styles: '自定义样式',
40
+ numbering: '自定义编号格式',
41
+ docGrid: '文档网格(行距网格)',
42
+ textDirection: '竖排/文字方向',
43
+ tableFormat: '表格单元格格式(单元格边框/重复表头)',
44
+ noteFormat: '脚注/尾注的格式与分段',
45
+ commentFormat: '批注的格式与分段',
46
+ trackChanges: '修订跟踪开关(保存后关闭)',
47
+ brokenPart: '无法解析的部件(其内容已丢失)'
48
+ };
49
+
50
+ /** 丢失清单的可读文案(「域代码(引用等) ×2、嵌入对象」) */
51
+ export const lossText = (losses) =>
52
+ (losses || [])
53
+ .filter((l) => LOSS_LABEL[l.kind])
54
+ .map((l) => (l.count > 1 ? `${LOSS_LABEL[l.kind]} ×${l.count}` : LOSS_LABEL[l.kind]))
55
+ .join('、');
56
+
57
+ /** 深度遍历(计数用,不管顺序) */
58
+ const walkAll = (node, visit) => {
59
+ const stack = [node];
60
+ while (stack.length > 0) {
61
+ const n = stack.pop();
62
+ visit(n);
63
+ for (const k of n.kids) stack.push(k);
64
+ }
65
+ };
66
+
67
+ /** 子树里有没有 blip(图片的关系锚点;带它的 drawing/pict 是内嵌图片,不算丢失) */
68
+ const hasBlip = (n) => {
69
+ let found = false;
70
+ walkAll(n, (x) => {
71
+ if (x.name === 'blip') found = true;
72
+ });
73
+ return found;
74
+ };
75
+
76
+ /** 嵌入对象:document 的关系里挂着 oleObject/package(OLE 复合对象、附加程序) */
77
+ const oleCount = (pkg) => {
78
+ let n = 0;
79
+ for (const rel of pkg.rels(PART_DOCUMENT).values()) {
80
+ if (/\/oleObject$|\/package$/.test(rel.type)) n += 1;
81
+ }
82
+ return n;
83
+ };
84
+
85
+ /** VBA 宏(.docm 的 vbaProject 部件不进出链路;文件能打开,宏保存即丢) */
86
+ const macroCount = (pkg) => (pkg.has('word/vbaProject.bin') ? 1 : 0);
87
+
88
+ /** 批注线程丢失:commentsExtended(回复/解决状态)整块不往返,多段批注固定压成一段 */
89
+ const commentStateCount = (pkg) => {
90
+ let n = pkg.has('word/commentsExtended.xml') ? 1 : 0;
91
+ const raw = pkg.part(PART_COMMENTS);
92
+ if (!raw) return n;
93
+ let tree;
94
+ try {
95
+ tree = parseNode(raw);
96
+ } catch {
97
+ return n;
98
+ }
99
+ for (const c of tree.children('comment')) {
100
+ if (c.children('p').length > 1) n += 1;
101
+ }
102
+ return n;
103
+ };
104
+
105
+ /** 设置部件里的丢失:文档保护 / 自动断字 / 修订跟踪(settings 是重写部件,原设置不保留) */
106
+ const settingsLosses = (pkg) => {
107
+ const raw = pkg.part(PART_SETTINGS);
108
+ if (!raw) return {};
109
+ let tree;
110
+ try {
111
+ tree = parseNode(raw);
112
+ } catch {
113
+ return {};
114
+ }
115
+ const track = tree.child('trackChanges');
116
+ return {
117
+ protection: tree.child('documentProtection') ? 1 : 0,
118
+ hyphenation: tree.child('autoHyphenation') ? 1 : 0,
119
+ trackChanges: track && track.attr('val') !== '0' && track.attr('val') !== 'false' ? 1 : 0
120
+ };
121
+ };
122
+
123
+ /** styles.xml 整件重写:Word 内置样式在 Word 侧仍有同名定义,真正丢的是用户自定义样式(w:customStyle="1") */
124
+ const customStyleCount = (pkg) => {
125
+ const raw = pkg.part(PART_STYLES);
126
+ if (!raw) return 0;
127
+ let tree;
128
+ try {
129
+ tree = parseNode(raw);
130
+ } catch {
131
+ return 0;
132
+ }
133
+ let n = 0;
134
+ walkAll(tree, (x) => {
135
+ const custom = x.attr('customStyle');
136
+ if (x.name === 'style' && (custom === '1' || custom === 'true')) n += 1;
137
+ });
138
+ return n;
139
+ };
140
+
141
+ /** numbering.xml 由固定模板重建:非内置格式(中文编号等)与层级覆盖(起始值/重排)都会丢 */
142
+ const SUPPORTED_NUM_FMT = new Set(['bullet', 'decimal', 'lowerLetter', 'lowerRoman']);
143
+ const numberingLossCount = (pkg) => {
144
+ const raw = pkg.part(PART_NUMBERING);
145
+ if (!raw) return 0;
146
+ let tree;
147
+ try {
148
+ tree = parseNode(raw);
149
+ } catch {
150
+ return 0;
151
+ }
152
+ let n = 0;
153
+ walkAll(tree, (x) => {
154
+ if (x.name === 'numFmt' && !SUPPORTED_NUM_FMT.has(x.attr('val'))) n += 1;
155
+ else if (x.name === 'lvlOverride' || x.name === 'startOverride') n += 1;
156
+ });
157
+ return n;
158
+ };
159
+
160
+ /** 无法解析的部件:读入侧静默降级为空(页眉页脚/批注/脚注等),保存即把原内容丢掉 */
161
+ const brokenPartCount = (pkg) => {
162
+ let n = 0;
163
+ const names = [...pkg.paths('word/header'), ...pkg.paths('word/footer'), PART_COMMENTS, PART_FOOTNOTES, PART_ENDNOTES];
164
+ for (const name of names) {
165
+ const raw = pkg.part(name);
166
+ if (!raw) continue;
167
+ try {
168
+ parseNode(raw);
169
+ } catch {
170
+ n += 1;
171
+ }
172
+ }
173
+ return n;
174
+ };
175
+
176
+ /** 富文本格式标签(批注/脚注压成纯文本时丢失;vertAlign 除外——脚注引用号本来就有) */
177
+ const RICH_RUN_TAGS = new Set(['b', 'i', 'u', 'strike', 'dstrike', 'color', 'sz', 'szCs', 'rFonts', 'highlight', 'shd', 'em', 'caps', 'smallCaps', 'spacing']);
178
+ const hasRichRun = (node) => {
179
+ let rich = false;
180
+ walkAll(node, (x) => {
181
+ if (x.name === 'rPr' && x.kids.some((k) => RICH_RUN_TAGS.has(k.name))) rich = true;
182
+ });
183
+ return rich;
184
+ };
185
+
186
+ /** 批注格式:单段批注的格式同样会压成纯文本(多段另由 commentState 盘点) */
187
+ const commentFormatCount = (pkg) => {
188
+ const raw = pkg.part(PART_COMMENTS);
189
+ if (!raw) return 0;
190
+ let tree;
191
+ try {
192
+ tree = parseNode(raw);
193
+ } catch {
194
+ return 0;
195
+ }
196
+ return tree.children('comment').filter(hasRichRun).length;
197
+ };
198
+
199
+ /** 脚注/尾注:多段或带格式的正文压成一段纯文本 */
200
+ const noteFormatCount = (pkg) => {
201
+ let n = 0;
202
+ for (const part of [PART_FOOTNOTES, PART_ENDNOTES]) {
203
+ const raw = pkg.part(part);
204
+ if (!raw) continue;
205
+ let tree;
206
+ try {
207
+ tree = parseNode(raw);
208
+ } catch {
209
+ continue;
210
+ }
211
+ const tag = part === PART_FOOTNOTES ? 'footnote' : 'endnote';
212
+ for (const note of tree.children(tag)) {
213
+ const type = note.attr('type');
214
+ if (type === 'separator' || type === 'continuationSeparator') continue;
215
+ if (note.children('p').length > 1 || hasRichRun(note)) n += 1;
216
+ }
217
+ }
218
+ return n;
219
+ };
220
+
221
+ /** 图片调整类标记(裁剪与亮度/对比/双色调/灰阶/黑白/透明度等) */
222
+ const IMAGE_ADJUST_TAGS = new Set(['srcRect', 'lum', 'duotone', 'grayscl', 'biLevel', 'alphaModFix', 'blur', 'prstShdw', 'glow']);
223
+
224
+ /** 正文一次遍历盘点:域 / 形状 / 内容控件 / 公式 / 书签 / 隐藏文字 / RTL / 图片调整 / 符号 / 表单域 / 移动修订 / 行号 / 页面边框 / 垂直对齐 / 页面背景 */
225
+ const bodyLosses = (body) => {
226
+ const c = {
227
+ field: 0,
228
+ shape: 0,
229
+ sdt: 0,
230
+ math: 0,
231
+ bookmark: 0,
232
+ hidden: 0,
233
+ rtl: 0,
234
+ imageAdjust: 0,
235
+ imageLayout: 0,
236
+ symbol: 0,
237
+ formField: 0,
238
+ move: 0,
239
+ lineNumber: 0,
240
+ pageBorder: 0,
241
+ background: 0,
242
+ docGrid: 0,
243
+ textDirection: 0,
244
+ tableFormat: 0
245
+ };
246
+ // 页面背景挂在 document 根上(不在 body 子树里)
247
+ if (body.parent?.name === 'document' && body.parent.child('background')) c.background = 1;
248
+ const countOne = (k) => {
249
+ switch (k.name) {
250
+ case 'instrText': {
251
+ // 目录域随目录块往返(PAGEREF 是它的内部机制);其余域写出链路不认,静默丢弃
252
+ const code = k.text.trim().toUpperCase().split(/\s+/)[0] || '';
253
+ if (code !== 'TOC' && code !== 'PAGEREF') c.field += 1;
254
+ break;
255
+ }
256
+ case 'fldSimple': {
257
+ // 简单域:缓存文字保住(render 侧读子 run),域本身不往返
258
+ const code = (k.attr('instr') || '').trim().toUpperCase().split(/\s+/)[0] || '';
259
+ if (code !== 'TOC' && code !== 'PAGEREF') c.field += 1;
260
+ break;
261
+ }
262
+ case 'sdt':
263
+ c.sdt += 1;
264
+ break;
265
+ case 'oMathPara':
266
+ c.math += 1;
267
+ break;
268
+ case 'oMath':
269
+ if (k.parent?.name !== 'oMathPara') c.math += 1;
270
+ break;
271
+ case 'bookmarkStart':
272
+ // 自动书签(_GoBack 等)不算用户内容
273
+ if (!(k.attr('name') || '').startsWith('_')) c.bookmark += 1;
274
+ break;
275
+ case 'sym':
276
+ c.symbol += 1;
277
+ break;
278
+ case 'ffData':
279
+ c.formField += 1;
280
+ break;
281
+ case 'moveFrom':
282
+ case 'moveTo':
283
+ c.move += 1;
284
+ break;
285
+ case 'lnNumType':
286
+ c.lineNumber += 1;
287
+ break;
288
+ case 'pgBorders':
289
+ c.pageBorder += 1;
290
+ break;
291
+ case 'docGrid':
292
+ c.docGrid += 1;
293
+ break;
294
+ case 'textDirection': {
295
+ const dir = k.attr('val');
296
+ if (dir !== '' && dir !== 'lrTb') c.textDirection += 1;
297
+ break;
298
+ }
299
+ case 'tcBorders':
300
+ case 'tcMar':
301
+ case 'tblHeader':
302
+ case 'tblInd':
303
+ case 'tblCellSpacing':
304
+ c.tableFormat += 1;
305
+ break;
306
+ case 'bidi':
307
+ case 'rtl':
308
+ c.rtl += 1;
309
+ break;
310
+ case 'vanish': {
311
+ const v = k.attr('val');
312
+ if (v !== '0' && v !== 'false') c.hidden += 1;
313
+ break;
314
+ }
315
+ default:
316
+ if (IMAGE_ADJUST_TAGS.has(k.name)) c.imageAdjust += 1;
317
+ }
318
+ };
319
+ // 图形容器只数最外层:v:shape 可能包在 w:pict 里,两个都数会把一个对象算两份
320
+ const walk = (el, inShape) => {
321
+ for (const k of el.kids) {
322
+ if (!inShape) {
323
+ if (k.name === 'drawing' || k.name === 'pict') {
324
+ // 带 blip 的走内嵌图片;其余(图表骨架、SmartArt、形状、文本框)没有写出位置
325
+ if (!hasBlip(k)) {
326
+ // 水平分隔线不算丢:它会被折成段落下边框,线宽/线色/位置都带得回去
327
+ if (!isRuleShape(k)) c.shape += 1;
328
+ walk(k, true);
329
+ continue;
330
+ }
331
+ // 浮动图片(wp:anchor):图片本身保留,但环绕方式/相对定位写不回去,保存前要告知
332
+ let anchored = false;
333
+ walkAll(k, (x) => {
334
+ if (x.name === 'anchor') anchored = true;
335
+ });
336
+ if (anchored) c.imageLayout += 1;
337
+ } else if (k.name === 'shape') {
338
+ // VML 形状(页眉里的水印形状不会走到这里——那是页眉部件,不是正文)
339
+ c.shape += 1;
340
+ walk(k, true);
341
+ continue;
342
+ } else {
343
+ countOne(k);
344
+ }
345
+ }
346
+ walk(k, false);
347
+ }
348
+ };
349
+ walk(body, false);
350
+ return c;
351
+ };
352
+
353
+ /** 页眉页脚里除 PAGE/NUMPAGES/SECTIONPAGES 之外的域(写出只保留缓存文字,域语义丢失) */
354
+ const headerFooterFieldCount = (pkg) => {
355
+ let n = 0;
356
+ for (const name of [...pkg.paths('word/header'), ...pkg.paths('word/footer')]) {
357
+ const raw = pkg.part(name);
358
+ if (!raw) continue;
359
+ let tree;
360
+ try {
361
+ tree = parseNode(raw);
362
+ } catch {
363
+ continue;
364
+ }
365
+ walkAll(tree, (x) => {
366
+ const code = x.name === 'instrText'
367
+ ? x.text.trim().toUpperCase().split(/\s+/)[0] || ''
368
+ : x.name === 'fldSimple' ? (x.attr('instr') || '').trim().toUpperCase().split(/\s+/)[0] || '' : '';
369
+ if (code && !HF_FIELD_KINDS.has(code)) n += 1;
370
+ });
371
+ }
372
+ return n;
373
+ };
374
+
375
+ /** 页眉页脚部件里的表格/图片(往返只保留文本与基础格式;水印那张旋转文字图是保留下来的,按 shape type 排除) */
376
+ const headerFooterObjectCount = (pkg) => {
377
+ let n = 0;
378
+ for (const name of [...pkg.paths('word/header'), ...pkg.paths('word/footer')]) {
379
+ const raw = pkg.part(name);
380
+ if (!raw) continue;
381
+ let tree;
382
+ try {
383
+ tree = parseNode(raw);
384
+ } catch {
385
+ continue;
386
+ }
387
+ walkAll(tree, (x) => {
388
+ if (x.name === 'tbl' || x.name === 'blip') n += 1;
389
+ else if (x.name === 'shape' && x.attr('type') !== '#_x0000_t136') n += 1;
390
+ });
391
+ }
392
+ return n;
393
+ };
394
+
395
+ /**
396
+ * 盘点一份 .docx 读入后「保存必丢」的内容
397
+ * @param {import('gbooxml').OpcReader} pkg 已打开的包
398
+ * @param {import('gbooxml').OoxmlNode} body document.xml 的 body 节点
399
+ * @returns {Array<{kind: string, count: number}>} 非空清单(空数组 = 没有已知丢失)
400
+ */
401
+ export const collectLosses = (pkg, body) => {
402
+ const bodyCounts = bodyLosses(body);
403
+ bodyCounts.field += headerFooterFieldCount(pkg);
404
+ const counts = {
405
+ ole: oleCount(pkg),
406
+ macro: macroCount(pkg),
407
+ commentState: commentStateCount(pkg),
408
+ commentFormat: commentFormatCount(pkg),
409
+ headerFooterObject: headerFooterObjectCount(pkg),
410
+ styles: customStyleCount(pkg),
411
+ numbering: numberingLossCount(pkg),
412
+ noteFormat: noteFormatCount(pkg),
413
+ brokenPart: brokenPartCount(pkg),
414
+ ...settingsLosses(pkg),
415
+ ...bodyCounts
416
+ };
417
+ return Object.entries(counts)
418
+ .filter(([, count]) => count > 0)
419
+ .map(([kind, count]) => ({ kind, count }));
420
+ };