@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,129 +1,129 @@
1
- // [文件] web·docx·codec·carry —— 整部件透传:编辑器不建模但 Word 按关系类型发现的部件(theme/glossary/customXml/扩展属性)原样带过往返
2
- /*
3
- [设计] 为什么不是「未建模元素也保留」:ooxml 的树只存元素/属性/文本三段、不存原始字节与命名空间
4
- 前缀(那是它不被没见过的元素整篇解析失败的前提)。元素级原文保留需要序列化器,属引擎级改造;
5
- 整部件透传是同一目标下最小可用的一步——这些部件 Word 按关系类型发现,内容与我们写的部件正交。
6
-
7
- [设计] 只带「关系闭包完整」的候选:闭包里任何一个目标解析不了(包内没有、或没有 Override
8
- 内容类型——二进制嵌入物与只按扩展名声明内容类型的部件都过不了),整个候选放弃。
9
- 宁可回到「不带」的现状,也不写悬空引用让 Word 判文件损坏。
10
- */
11
- import { parseNode, relsPath, resolveTarget } from 'gbooxml';
12
- import { PART_DOCUMENT, PART_NUMBERING, PART_STYLES } from './parts.js';
13
-
14
- /** 可透传的关系类型(Word 按类型从 document 关系表 / 包根关系表发现这些部件,与正文内容无引用耦合) */
15
- const CARRY_TYPES = new Set([
16
- 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
17
- 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable',
18
- 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings',
19
- 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument',
20
- 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml',
21
- 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
22
- 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties'
23
- ]);
24
-
25
- /** 我们按原路径重写的部件:闭包指向它们 = 指向写出后的包,有效 */
26
- const OWN_PARTS = new Set([PART_DOCUMENT, PART_STYLES, PART_NUMBERING]);
27
-
28
- /** [Content_Types].xml 的 Override 表(部件路径 → 内容类型;只有 Override 的部件才谈得上原样透传) */
29
- const contentTypes = (pkg) => {
30
- const out = new Map();
31
- const raw = pkg.part('[Content_Types].xml');
32
- if (!raw) return out;
33
- let tree;
34
- try {
35
- tree = parseNode(raw);
36
- } catch {
37
- return out;
38
- }
39
- for (const o of tree.children('Override')) out.set(o.attr('PartName').replace(/^\//, ''), o.attr('ContentType'));
40
- return out;
41
- };
42
-
43
- /** 候选部件的关系闭包:它自己的 rels 文件 + 闭包里要连带透传的部件;解析不了返回 ok=false */
44
- const closureOf = (pkg, cts, path, seen) => {
45
- const parts = [];
46
- const relsName = relsPath(path);
47
- const relsRaw = pkg.part(relsName);
48
- if (!relsRaw) return { ok: true, parts };
49
- parts.push({ path: relsName, ct: '' }); // .rels 内容类型由 Default 覆盖,写出侧走 addRaw
50
- let tree;
51
- try {
52
- tree = parseNode(relsRaw);
53
- } catch {
54
- return { ok: false, parts };
55
- }
56
- for (const r of tree.children('Relationship')) {
57
- if (r.attr('TargetMode').toLowerCase() === 'external') continue; // 包外目标不随包走,也不影响包完整性
58
- const target = resolveTarget(path, r.attr('Target'));
59
- if (OWN_PARTS.has(target)) continue; // 我们原路径重写
60
- if (target === '' || seen.has(target)) continue;
61
- const ct = cts.get(target);
62
- if (!ct || !pkg.part(target)) return { ok: false, parts };
63
- seen.add(target);
64
- parts.push({ path: target, ct });
65
- const sub = closureOf(pkg, cts, target, seen);
66
- if (!sub.ok) return { ok: false, parts: [...parts, ...sub.parts] };
67
- parts.push(...sub.parts);
68
- }
69
- return { ok: true, parts };
70
- };
71
-
72
- /**
73
- * 读入时收集可透传部件(字节直接引用包内已解压数据,不复制)
74
- * [设计] 两遍式:先收齐顶层候选(关系类型命中的),再逐个算闭包——同一个部件既被 document
75
- * 关系直接引用、又可能被别的候选连带时,顶层注册优先(降级成闭包件会丢掉 document 级关系,
76
- * Word 就按类型找不到它了)。遍历顺序因此不影响结果。
77
- * @param {import('gbooxml').OpcReader} pkg
78
- * @returns {Array<{path:string,bytes:Uint8Array,ct:string,from:string|null,type:string,target:string}>}
79
- * from 非 null = 顶层候选(写出时注册关系);null = 闭包连带部件(只加字节,关系由透传的 rels 文件承载)
80
- */
81
- export const collectCarry = (pkg) => {
82
- const cts = contentTypes(pkg);
83
- const taken = new Set();
84
- const candidates = [];
85
- const sources = [
86
- { from: PART_DOCUMENT, rels: pkg.rels(PART_DOCUMENT) },
87
- { from: '', rels: pkg.rels('') }
88
- ];
89
- for (const { from, rels } of sources) {
90
- for (const rel of rels.values()) {
91
- if (rel.external || !CARRY_TYPES.has(rel.type)) continue;
92
- const target = resolveTarget(from, rel.target);
93
- if (target === '' || taken.has(target)) continue;
94
- const ct = cts.get(target);
95
- const bytes = pkg.part(target);
96
- if (!ct || !bytes) continue;
97
- taken.add(target);
98
- candidates.push({ path: target, bytes, ct, from, type: rel.type, target: rel.target });
99
- }
100
- }
101
- const out = [];
102
- for (const c of candidates) {
103
- const closure = closureOf(pkg, cts, c.path, new Set([c.path]));
104
- if (!closure.ok) continue; // 闭包不全:候选整体放弃(见文件头 [设计])
105
- out.push(c);
106
- for (const part of closure.parts) {
107
- if (taken.has(part.path)) continue;
108
- const bytes = pkg.part(part.path);
109
- if (!bytes) continue;
110
- taken.add(part.path);
111
- out.push({ path: part.path, bytes, ct: part.ct, from: null, type: '', target: '' });
112
- }
113
- }
114
- return out;
115
- };
116
-
117
- /**
118
- * 写出时把透传部件装回包(顶层候选顺带注册关系;rId 由 OpcPackage 自动分配,不与正文图片冲突)
119
- * @param {import('gbooxml').OpcPackage} pkg
120
- * @param {Array} carry collectCarry 的产物
121
- */
122
- export const writeCarry = (pkg, carry) => {
123
- for (const c of carry || []) {
124
- if (!c?.bytes) continue;
125
- if (c.ct) pkg.add(c.path, c.ct, c.bytes);
126
- else pkg.addRaw(c.path, c.bytes);
127
- if (c.from !== null) pkg.rel(c.from, c.type, c.target);
128
- }
129
- };
1
+ // [文件] web·docx·codec·carry —— 整部件透传:编辑器不建模但 Word 按关系类型发现的部件(theme/glossary/customXml/扩展属性)原样带过往返
2
+ /*
3
+ [设计] 为什么不是「未建模元素也保留」:ooxml 的树只存元素/属性/文本三段、不存原始字节与命名空间
4
+ 前缀(那是它不被没见过的元素整篇解析失败的前提)。元素级原文保留需要序列化器,属引擎级改造;
5
+ 整部件透传是同一目标下最小可用的一步——这些部件 Word 按关系类型发现,内容与我们写的部件正交。
6
+
7
+ [设计] 只带「关系闭包完整」的候选:闭包里任何一个目标解析不了(包内没有、或没有 Override
8
+ 内容类型——二进制嵌入物与只按扩展名声明内容类型的部件都过不了),整个候选放弃。
9
+ 宁可回到「不带」的现状,也不写悬空引用让 Word 判文件损坏。
10
+ */
11
+ import { parseNode, relsPath, resolveTarget } from 'gbooxml';
12
+ import { PART_DOCUMENT, PART_NUMBERING, PART_STYLES } from './parts.js';
13
+
14
+ /** 可透传的关系类型(Word 按类型从 document 关系表 / 包根关系表发现这些部件,与正文内容无引用耦合) */
15
+ const CARRY_TYPES = new Set([
16
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme',
17
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable',
18
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings',
19
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/glossaryDocument',
20
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/customXml',
21
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties',
22
+ 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties'
23
+ ]);
24
+
25
+ /** 我们按原路径重写的部件:闭包指向它们 = 指向写出后的包,有效 */
26
+ const OWN_PARTS = new Set([PART_DOCUMENT, PART_STYLES, PART_NUMBERING]);
27
+
28
+ /** [Content_Types].xml 的 Override 表(部件路径 → 内容类型;只有 Override 的部件才谈得上原样透传) */
29
+ const contentTypes = (pkg) => {
30
+ const out = new Map();
31
+ const raw = pkg.part('[Content_Types].xml');
32
+ if (!raw) return out;
33
+ let tree;
34
+ try {
35
+ tree = parseNode(raw);
36
+ } catch {
37
+ return out;
38
+ }
39
+ for (const o of tree.children('Override')) out.set(o.attr('PartName').replace(/^\//, ''), o.attr('ContentType'));
40
+ return out;
41
+ };
42
+
43
+ /** 候选部件的关系闭包:它自己的 rels 文件 + 闭包里要连带透传的部件;解析不了返回 ok=false */
44
+ const closureOf = (pkg, cts, path, seen) => {
45
+ const parts = [];
46
+ const relsName = relsPath(path);
47
+ const relsRaw = pkg.part(relsName);
48
+ if (!relsRaw) return { ok: true, parts };
49
+ parts.push({ path: relsName, ct: '' }); // .rels 内容类型由 Default 覆盖,写出侧走 addRaw
50
+ let tree;
51
+ try {
52
+ tree = parseNode(relsRaw);
53
+ } catch {
54
+ return { ok: false, parts };
55
+ }
56
+ for (const r of tree.children('Relationship')) {
57
+ if (r.attr('TargetMode').toLowerCase() === 'external') continue; // 包外目标不随包走,也不影响包完整性
58
+ const target = resolveTarget(path, r.attr('Target'));
59
+ if (OWN_PARTS.has(target)) continue; // 我们原路径重写
60
+ if (target === '' || seen.has(target)) continue;
61
+ const ct = cts.get(target);
62
+ if (!ct || !pkg.part(target)) return { ok: false, parts };
63
+ seen.add(target);
64
+ parts.push({ path: target, ct });
65
+ const sub = closureOf(pkg, cts, target, seen);
66
+ if (!sub.ok) return { ok: false, parts: [...parts, ...sub.parts] };
67
+ parts.push(...sub.parts);
68
+ }
69
+ return { ok: true, parts };
70
+ };
71
+
72
+ /**
73
+ * 读入时收集可透传部件(字节直接引用包内已解压数据,不复制)
74
+ * [设计] 两遍式:先收齐顶层候选(关系类型命中的),再逐个算闭包——同一个部件既被 document
75
+ * 关系直接引用、又可能被别的候选连带时,顶层注册优先(降级成闭包件会丢掉 document 级关系,
76
+ * Word 就按类型找不到它了)。遍历顺序因此不影响结果。
77
+ * @param {import('gbooxml').OpcReader} pkg
78
+ * @returns {Array<{path:string,bytes:Uint8Array,ct:string,from:string|null,type:string,target:string}>}
79
+ * from 非 null = 顶层候选(写出时注册关系);null = 闭包连带部件(只加字节,关系由透传的 rels 文件承载)
80
+ */
81
+ export const collectCarry = (pkg) => {
82
+ const cts = contentTypes(pkg);
83
+ const taken = new Set();
84
+ const candidates = [];
85
+ const sources = [
86
+ { from: PART_DOCUMENT, rels: pkg.rels(PART_DOCUMENT) },
87
+ { from: '', rels: pkg.rels('') }
88
+ ];
89
+ for (const { from, rels } of sources) {
90
+ for (const rel of rels.values()) {
91
+ if (rel.external || !CARRY_TYPES.has(rel.type)) continue;
92
+ const target = resolveTarget(from, rel.target);
93
+ if (target === '' || taken.has(target)) continue;
94
+ const ct = cts.get(target);
95
+ const bytes = pkg.part(target);
96
+ if (!ct || !bytes) continue;
97
+ taken.add(target);
98
+ candidates.push({ path: target, bytes, ct, from, type: rel.type, target: rel.target });
99
+ }
100
+ }
101
+ const out = [];
102
+ for (const c of candidates) {
103
+ const closure = closureOf(pkg, cts, c.path, new Set([c.path]));
104
+ if (!closure.ok) continue; // 闭包不全:候选整体放弃(见文件头 [设计])
105
+ out.push(c);
106
+ for (const part of closure.parts) {
107
+ if (taken.has(part.path)) continue;
108
+ const bytes = pkg.part(part.path);
109
+ if (!bytes) continue;
110
+ taken.add(part.path);
111
+ out.push({ path: part.path, bytes, ct: part.ct, from: null, type: '', target: '' });
112
+ }
113
+ }
114
+ return out;
115
+ };
116
+
117
+ /**
118
+ * 写出时把透传部件装回包(顶层候选顺带注册关系;rId 由 OpcPackage 自动分配,不与正文图片冲突)
119
+ * @param {import('gbooxml').OpcPackage} pkg
120
+ * @param {Array} carry collectCarry 的产物
121
+ */
122
+ export const writeCarry = (pkg, carry) => {
123
+ for (const c of carry || []) {
124
+ if (!c?.bytes) continue;
125
+ if (c.ct) pkg.add(c.path, c.ct, c.bytes);
126
+ else pkg.addRaw(c.path, c.bytes);
127
+ if (c.from !== null) pkg.rel(c.from, c.type, c.target);
128
+ }
129
+ };
@@ -1,25 +1,25 @@
1
- // [文件] web·docx·codec·comments —— 批注回读:comments.xml → 批注清单(写出侧见 parts.js 的 commentsPart)
2
- import { parseNode } from 'gbooxml';
3
- import { PART_COMMENTS } from './parts.js';
4
-
5
- /**
6
- * 解析 word/comments.xml。
7
- * Key 用 docx 里的数字 id 字符串,与正文回读出的 data-cmt 对齐;部件缺失或损坏一律当作没有批注
8
- * @param {import('gbooxml').OpcReader} pkg
9
- */
10
- export const parseComments = (pkg) => {
11
- const raw = pkg.part(PART_COMMENTS);
12
- if (!raw) return [];
13
- let tree;
14
- try {
15
- tree = parseNode(raw);
16
- } catch {
17
- return [];
18
- }
19
- return tree.children('comment').map((c) => ({
20
- key: c.attr('id'),
21
- text: c.texts().join(' '),
22
- author: c.attr('author'),
23
- date: c.attr('date')
24
- }));
25
- };
1
+ // [文件] web·docx·codec·comments —— 批注回读:comments.xml → 批注清单(写出侧见 parts.js 的 commentsPart)
2
+ import { parseNode } from 'gbooxml';
3
+ import { PART_COMMENTS } from './parts.js';
4
+
5
+ /**
6
+ * 解析 word/comments.xml。
7
+ * Key 用 docx 里的数字 id 字符串,与正文回读出的 data-cmt 对齐;部件缺失或损坏一律当作没有批注
8
+ * @param {import('gbooxml').OpcReader} pkg
9
+ */
10
+ export const parseComments = (pkg) => {
11
+ const raw = pkg.part(PART_COMMENTS);
12
+ if (!raw) return [];
13
+ let tree;
14
+ try {
15
+ tree = parseNode(raw);
16
+ } catch {
17
+ return [];
18
+ }
19
+ return tree.children('comment').map((c) => ({
20
+ key: c.attr('id'),
21
+ text: c.texts().join(' '),
22
+ author: c.attr('author'),
23
+ date: c.attr('date')
24
+ }));
25
+ };