@a200888213/gbdocx 0.6.1 → 0.6.3

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 (95) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +215 -215
  3. package/index.html +31 -30
  4. package/package.json +49 -48
  5. package/serve.mjs +27 -23
  6. package/src/app/file-io.js +122 -122
  7. package/src/app/main.js +9 -9
  8. package/src/app/word/catalog.js +52 -52
  9. package/src/app/word/comments.js +112 -112
  10. package/src/app/word/doc.js +113 -113
  11. package/src/app/word/edit.js +72 -72
  12. package/src/app/word/file.js +191 -191
  13. package/src/app/word/hf.js +76 -76
  14. package/src/app/word/layout.js +155 -155
  15. package/src/app/word/menus.js +201 -201
  16. package/src/app/word/notes.js +110 -110
  17. package/src/app/word/shared.js +22 -22
  18. package/src/app/word/view.js +159 -159
  19. package/src/app/word-app.js +487 -487
  20. package/src/app/word-sheets.js +101 -101
  21. package/src/kernel/docx/catalog.js +41 -41
  22. package/src/kernel/docx/codec/blocks.js +123 -123
  23. package/src/kernel/docx/codec/carry.js +129 -129
  24. package/src/kernel/docx/codec/comments.js +25 -25
  25. package/src/kernel/docx/codec/docx.js +198 -198
  26. package/src/kernel/docx/codec/docxStyles.js +177 -177
  27. package/src/kernel/docx/codec/fonts.js +195 -195
  28. package/src/kernel/docx/codec/helpers.js +161 -161
  29. package/src/kernel/docx/codec/hfHtml.js +152 -152
  30. package/src/kernel/docx/codec/html.js +739 -739
  31. package/src/kernel/docx/codec/losses.js +420 -420
  32. package/src/kernel/docx/codec/markdown.js +170 -170
  33. package/src/kernel/docx/codec/parts.js +249 -249
  34. package/src/kernel/docx/codec/read.js +603 -603
  35. package/src/kernel/docx/codec/render.js +1156 -1156
  36. package/src/kernel/docx/codec/roundtrip.js +236 -236
  37. package/src/kernel/docx/codec/write.js +833 -833
  38. package/src/kernel/docx/comments.js +14 -14
  39. package/src/kernel/docx/menus.js +348 -348
  40. package/src/kernel/docx/pageGeometry.js +123 -123
  41. package/src/kernel/docx/pagination.js +481 -481
  42. package/src/kernel/docx/watermark.js +45 -45
  43. package/src/kernel/docx/wordShortcuts.js +37 -37
  44. package/src/kernel/editor/constants.js +8 -8
  45. package/src/kernel/editor/engine/blocks.js +594 -594
  46. package/src/kernel/editor/engine/blocksEnter.js +109 -109
  47. package/src/kernel/editor/engine/blocksInsert.js +254 -254
  48. package/src/kernel/editor/engine/blocksStructure.js +226 -226
  49. package/src/kernel/editor/engine/commands.js +181 -181
  50. package/src/kernel/editor/engine/dom.js +646 -646
  51. package/src/kernel/editor/engine/editor.js +213 -213
  52. package/src/kernel/editor/engine/history.js +149 -149
  53. package/src/kernel/editor/engine/inline.js +656 -656
  54. package/src/kernel/editor/engine/input.js +554 -554
  55. package/src/kernel/editor/engine/interaction.js +137 -137
  56. package/src/kernel/editor/engine/pointer.js +39 -39
  57. package/src/kernel/editor/engine/revision.js +138 -138
  58. package/src/kernel/editor/engine/schema.js +417 -417
  59. package/src/kernel/editor/engine/search.js +113 -113
  60. package/src/kernel/editor/engine/selection.js +119 -119
  61. package/src/kernel/editor/engine/state.js +118 -118
  62. package/src/kernel/editor/engine/tableClip.js +154 -154
  63. package/src/kernel/editor/engine/tableGrid.js +55 -55
  64. package/src/kernel/editor/engine/tableInteraction.js +388 -388
  65. package/src/kernel/editor/engine/tableMenu.js +250 -250
  66. package/src/kernel/editor/engine/tableOps.js +174 -174
  67. package/src/kernel/editor/engine/tables.js +732 -732
  68. package/src/kernel/editor/overlayGeometry.js +20 -20
  69. package/src/kernel/editor/pasteClean.js +293 -293
  70. package/src/kernel/editor/utils.js +48 -48
  71. package/src/kernel/keyboard.js +114 -114
  72. package/src/kernel/notify.js +54 -54
  73. package/src/kernel/textStyles.js +84 -84
  74. package/src/kernel/util/clipboard.js +89 -89
  75. package/src/kernel/util/imageFile.js +91 -91
  76. package/src/kernel/util/safeValue.js +25 -25
  77. package/src/kernel/util/shortcutKey.js +33 -33
  78. package/src/surface/editor-surface.js +603 -600
  79. package/src/ui/color-panel.js +69 -69
  80. package/src/ui/context-menu.js +135 -133
  81. package/src/ui/dialogs.js +249 -249
  82. package/src/ui/find-bar.js +151 -151
  83. package/src/ui/icons.js +245 -137
  84. package/src/ui/menubar.js +28 -28
  85. package/src/ui/panels.js +115 -115
  86. package/src/ui/popup.js +79 -79
  87. package/src/ui/prompt.js +89 -89
  88. package/src/ui/ribbon.js +485 -485
  89. package/src/ui/special-chars.js +39 -39
  90. package/src/ui/statusbar.js +81 -81
  91. package/src/ui/toolbar.js +32 -32
  92. package/styles/app.css +970 -970
  93. package/styles/editor-content.css +440 -440
  94. package/styles/tokens.css +154 -154
  95. package/styles/word-panel.css +410 -410
package/package.json CHANGED
@@ -1,48 +1,49 @@
1
- {
2
- "name": "@a200888213/gbdocx",
3
- "version": "0.6.1",
4
- "description": "挂逼文档:独立 .docx 编辑器(内核移植自 guabi office/docx 编解码 + 自研 contenteditable 编辑器内核,UI 用原生 HTML 重写),只依赖 gbooxml。",
5
- "type": "module",
6
- "license": "MIT",
7
- "main": "./src/app/word-app.js",
8
- "exports": {
9
- ".": "./src/app/word-app.js",
10
- "./app": "./src/app/word-app.js",
11
- "./styles/*": "./styles/*",
12
- "./*": "./src/*.js",
13
- "./package.json": "./package.json"
14
- },
15
- "files": [
16
- "src",
17
- "styles",
18
- "index.html",
19
- "serve.mjs",
20
- "LICENSE"
21
- ],
22
- "keywords": [
23
- "docx",
24
- "document",
25
- "word",
26
- "ooxml",
27
- "editor",
28
- "contenteditable"
29
- ],
30
- "engines": {
31
- "node": ">=18"
32
- },
33
- "scripts": {
34
- "start": "node serve.mjs",
35
- "test": "node --test test/roundtrip.test.js test/ui.test.js test/mode.test.js"
36
- },
37
- "dependencies": {
38
- "gbooxml": "^0.1.1",
39
- "gbofficeui": "^0.3.0",
40
- "guabiutils": "^0.3.0"
41
- },
42
- "devDependencies": {
43
- "jsdom": "^30.1.3"
44
- },
45
- "publishConfig": {
46
- "access": "public"
47
- }
48
- }
1
+ {
2
+ "name": "@a200888213/gbdocx",
3
+ "version": "0.6.3",
4
+ "description": "挂逼文档:独立 .docx 编辑器(内核移植自 guabi office/docx 编解码 + 自研 contenteditable 编辑器内核,UI 用原生 HTML 重写),只依赖 gbooxml。",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "main": "./src/app/word-app.js",
8
+ "exports": {
9
+ ".": "./src/app/word-app.js",
10
+ "./app": "./src/app/word-app.js",
11
+ "./styles/*": "./styles/*",
12
+ "./*": "./src/*.js",
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "src",
17
+ "styles",
18
+ "index.html",
19
+ "serve.mjs",
20
+ "LICENSE"
21
+ ],
22
+ "keywords": [
23
+ "docx",
24
+ "document",
25
+ "word",
26
+ "ooxml",
27
+ "editor",
28
+ "contenteditable"
29
+ ],
30
+ "engines": {
31
+ "node": ">=18"
32
+ },
33
+ "scripts": {
34
+ "start": "node serve.mjs",
35
+ "test": "node --test test/roundtrip.test.js test/ui.test.js test/mode.test.js"
36
+ },
37
+ "dependencies": {
38
+ "gbooxml": "^0.1.1",
39
+ "gbofficeui": "^0.3.0",
40
+ "guabiutils": "^0.3.0",
41
+ "gbicons": "^0.1.0"
42
+ },
43
+ "devDependencies": {
44
+ "jsdom": "^30.1.3"
45
+ },
46
+ "publishConfig": {
47
+ "access": "public"
48
+ }
49
+ }
package/serve.mjs CHANGED
@@ -1,23 +1,27 @@
1
- // [文件] gbdocx·serve —— 静态服务器(把 gbdocx 与同仓的 gbooxml / guabiutils 一起按 URL 空间供给,免打包免安装)
2
- // 用法:node serve.mjs [端口]
3
- import { fileURLToPath } from 'node:url';
4
- import { startStaticServer } from 'guabiutils/dev-server';
5
-
6
- const ooxmlRoot = fileURLToPath(new URL('../gbooxml/', import.meta.url));
7
- const utilsSrc = fileURLToPath(new URL('../../guabiutils/src/', import.meta.url));
8
- const officeUi = fileURLToPath(new URL('../gbofficeui/', import.meta.url));
9
-
10
- const { origin } = await startStaticServer({
11
- root: fileURLToPath(new URL('.', import.meta.url)),
12
- index: 'index.html',
13
- port: Number(process.argv[2] || process.env.GBDOCX_PORT || 5273),
14
- mounts: [
15
- { prefix: '/vendor/gbooxml/', dir: ooxmlRoot },
16
- { prefix: '/vendor/guabiutils/', dir: utilsSrc },
17
- // gbofficeui:共享文档头模块(src/head.js)与顶栏皮肤(styles/)同属一个包根
18
- { prefix: '/vendor/gbofficeui/', dir: officeUi },
19
- ],
20
- });
21
-
22
- console.log(`gbdocx 运行中:${origin}`);
23
- console.log(`gbooxml 映射自:${ooxmlRoot}`);
1
+ // [文件] gbdocx·serve —— 静态服务器(把 gbdocx 与同仓的 gbooxml / guabiutils 一起按 URL 空间供给,免打包免安装)
2
+ // 用法:node serve.mjs [端口]
3
+ import { fileURLToPath } from 'node:url';
4
+ import { startStaticServer } from 'guabiutils/dev-server';
5
+
6
+ const ooxmlRoot = fileURLToPath(new URL('../gbooxml/', import.meta.url));
7
+ const utilsSrc = fileURLToPath(new URL('../../guabiutils/src/', import.meta.url));
8
+ const officeUi = fileURLToPath(new URL('../gbofficeui/', import.meta.url));
9
+ const gbiconsPkg = fileURLToPath(new URL('../../gbicons/', import.meta.url));
10
+ // lucide:图标库 ESM 产物(workspace 提升到仓根 node_modules)
11
+ const lucideEsm = fileURLToPath(new URL('../../../node_modules/lucide/dist/esm/', import.meta.url));
12
+
13
+ const { origin } = await startStaticServer({
14
+ root: fileURLToPath(new URL('.', import.meta.url)),
15
+ index: 'index.html',
16
+ port: Number(process.argv[2] || process.env.GBDOCX_PORT || 5273),
17
+ mounts: [
18
+ { prefix: '/vendor/gbooxml/', dir: ooxmlRoot },
19
+ { prefix: '/vendor/guabiutils/', dir: utilsSrc },
20
+ // gbofficeui:共享文档头模块(src/head.js)与顶栏皮肤(styles/)同属一个包根
21
+ { prefix: '/vendor/gbofficeui/', dir: officeUi },
22
+ { prefix: '/vendor/gbicons/', dir: gbiconsPkg },
23
+ ],
24
+ });
25
+
26
+ console.log(`gbdocx 运行中:${origin}`);
27
+ console.log(`gbooxml 映射自:${ooxmlRoot}`);
@@ -1,122 +1,122 @@
1
- // [文件] gbdocx·文件读写 —— 打开 / 保存 / 另存为(优先 File System Access API,回退 input[type=file] + 下载)
2
-
3
- import { downloadBlob, ensureExt as ensureExtWith } from 'guabiutils';
4
-
5
- export const DOCX_MIME = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
6
-
7
- /** 跨源子框架:浏览器的 File System Access API 在跨源 iframe 里直接抛 SecurityError。
8
- * [坑] 只判断函数是否存在会掉进这个坑:工作台把演示页放在另一个端口的 iframe 里,
9
- * showOpenFilePicker 存在但一调就失败,必须提前识别并走 input/下载回退 */
10
- export const isCrossOriginFrame = (win = window) => {
11
- if (!win || win.self === win.top) return false;
12
- try {
13
- // 同源可读父窗口 location;跨源访问抛 SecurityError
14
- void win.top.location.href;
15
- return false;
16
- } catch {
17
- return true;
18
- }
19
- };
20
-
21
- /** 是否可用文件系统访问 API(Chromium 且非跨源子框架) */
22
- export const hasFsAccess = (win = window) => typeof win.showOpenFilePicker === 'function' && !isCrossOriginFrame(win);
23
-
24
- /**
25
- * 打开 .docx:返回 { name, bytes, handle|null };用户取消返回 null
26
- */
27
- export async function openDocx() {
28
- if (hasFsAccess()) {
29
- let picked = null;
30
- try {
31
- picked = await window.showOpenFilePicker({
32
- types: [{ description: 'Word 文档', accept: { [DOCX_MIME]: ['.docx'] } }],
33
- multiple: false,
34
- });
35
- } catch (err) {
36
- // 取消是正常返回;其余(含策略/环境的 SecurityError)退回 input 选择,别把打开流程卡死
37
- if (err?.name === 'AbortError') return null;
38
- }
39
- const handle = picked?.[0] || null;
40
- if (handle) {
41
- const file = await handle.getFile();
42
- return { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()), handle };
43
- }
44
- }
45
- const file = await pickViaInput();
46
- if (!file) return null;
47
- return { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()), handle: null };
48
- }
49
-
50
- /** 回退路径:隐藏 input 选文件(取消选择时用 cancel/focus 兜底结束,避免 Promise 永挂) */
51
- function pickViaInput() {
52
- return new Promise((resolve) => {
53
- const input = document.createElement('input');
54
- input.type = 'file';
55
- input.accept = '.docx';
56
- let settled = false;
57
- const done = (file) => {
58
- if (settled) return;
59
- settled = true;
60
- window.removeEventListener('focus', onFocus);
61
- resolve(file);
62
- };
63
- // 部分浏览器取消选择不派发任何事件:对话框关闭后窗口重新聚焦,稍等再看有没有文件
64
- const onFocus = () => {
65
- setTimeout(() => { if (!input.files?.length) done(null); }, 300);
66
- };
67
- input.addEventListener('change', () => done(input.files?.[0] || null), { once: true });
68
- input.addEventListener('cancel', () => done(null), { once: true });
69
- window.addEventListener('focus', onFocus, { once: true });
70
- input.click();
71
- });
72
- }
73
-
74
- /** 写入已知句柄:write 失败时中止流,避免半截文件与未关闭的 writable 泄漏 */
75
- async function writeHandle(handle, bytes) {
76
- const writable = await handle.createWritable();
77
- try {
78
- await writable.write(bytes);
79
- } catch (err) {
80
- await writable.abort?.().catch(() => {});
81
- throw err;
82
- }
83
- await writable.close();
84
- }
85
-
86
- /**
87
- * 保存到已知句柄(无句柄则触发下载);返回 { saved:boolean, handle }
88
- */
89
- export async function saveDocx(bytes, name, handle) {
90
- if (handle?.createWritable) {
91
- await writeHandle(handle, bytes);
92
- return { saved: true, handle };
93
- }
94
- downloadBlob(new Blob([bytes], { type: DOCX_MIME }), ensureExt(name, '.docx'));
95
- return { saved: true, handle: null };
96
- }
97
-
98
- /** 另存为:优先系统保存对话框,回退下载;返回 { saved, handle, name } */
99
- export async function saveDocxAs(bytes, name) {
100
- const suggested = ensureExt(name, '.docx');
101
- if (typeof window.showSaveFilePicker === 'function' && !isCrossOriginFrame()) {
102
- let handle = null;
103
- try {
104
- handle = await window.showSaveFilePicker({
105
- suggestedName: suggested,
106
- types: [{ description: 'Word 文档', accept: { [DOCX_MIME]: ['.docx'] } }],
107
- });
108
- } catch (err) {
109
- if (err?.name === 'AbortError') return { saved: false, handle: null, name: suggested };
110
- // 跨源框架/策略拒绝:回退下载
111
- }
112
- if (handle) {
113
- await writeHandle(handle, bytes);
114
- return { saved: true, handle, name: handle.name || suggested };
115
- }
116
- }
117
- downloadBlob(new Blob([bytes], { type: DOCX_MIME }), suggested);
118
- return { saved: true, handle: null, name: suggested };
119
- }
120
-
121
- /** 文件名兜底扩展名(空名用「新建文档」) */
122
- export const ensureExt = (name, ext) => ensureExtWith(name, ext, '新建文档');
1
+ // [文件] gbdocx·文件读写 —— 打开 / 保存 / 另存为(优先 File System Access API,回退 input[type=file] + 下载)
2
+
3
+ import { downloadBlob, ensureExt as ensureExtWith } from 'guabiutils';
4
+
5
+ export const DOCX_MIME = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
6
+
7
+ /** 跨源子框架:浏览器的 File System Access API 在跨源 iframe 里直接抛 SecurityError。
8
+ * [坑] 只判断函数是否存在会掉进这个坑:工作台把演示页放在另一个端口的 iframe 里,
9
+ * showOpenFilePicker 存在但一调就失败,必须提前识别并走 input/下载回退 */
10
+ export const isCrossOriginFrame = (win = window) => {
11
+ if (!win || win.self === win.top) return false;
12
+ try {
13
+ // 同源可读父窗口 location;跨源访问抛 SecurityError
14
+ void win.top.location.href;
15
+ return false;
16
+ } catch {
17
+ return true;
18
+ }
19
+ };
20
+
21
+ /** 是否可用文件系统访问 API(Chromium 且非跨源子框架) */
22
+ export const hasFsAccess = (win = window) => typeof win.showOpenFilePicker === 'function' && !isCrossOriginFrame(win);
23
+
24
+ /**
25
+ * 打开 .docx:返回 { name, bytes, handle|null };用户取消返回 null
26
+ */
27
+ export async function openDocx() {
28
+ if (hasFsAccess()) {
29
+ let picked = null;
30
+ try {
31
+ picked = await window.showOpenFilePicker({
32
+ types: [{ description: 'Word 文档', accept: { [DOCX_MIME]: ['.docx'] } }],
33
+ multiple: false,
34
+ });
35
+ } catch (err) {
36
+ // 取消是正常返回;其余(含策略/环境的 SecurityError)退回 input 选择,别把打开流程卡死
37
+ if (err?.name === 'AbortError') return null;
38
+ }
39
+ const handle = picked?.[0] || null;
40
+ if (handle) {
41
+ const file = await handle.getFile();
42
+ return { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()), handle };
43
+ }
44
+ }
45
+ const file = await pickViaInput();
46
+ if (!file) return null;
47
+ return { name: file.name, bytes: new Uint8Array(await file.arrayBuffer()), handle: null };
48
+ }
49
+
50
+ /** 回退路径:隐藏 input 选文件(取消选择时用 cancel/focus 兜底结束,避免 Promise 永挂) */
51
+ function pickViaInput() {
52
+ return new Promise((resolve) => {
53
+ const input = document.createElement('input');
54
+ input.type = 'file';
55
+ input.accept = '.docx';
56
+ let settled = false;
57
+ const done = (file) => {
58
+ if (settled) return;
59
+ settled = true;
60
+ window.removeEventListener('focus', onFocus);
61
+ resolve(file);
62
+ };
63
+ // 部分浏览器取消选择不派发任何事件:对话框关闭后窗口重新聚焦,稍等再看有没有文件
64
+ const onFocus = () => {
65
+ setTimeout(() => { if (!input.files?.length) done(null); }, 300);
66
+ };
67
+ input.addEventListener('change', () => done(input.files?.[0] || null), { once: true });
68
+ input.addEventListener('cancel', () => done(null), { once: true });
69
+ window.addEventListener('focus', onFocus, { once: true });
70
+ input.click();
71
+ });
72
+ }
73
+
74
+ /** 写入已知句柄:write 失败时中止流,避免半截文件与未关闭的 writable 泄漏 */
75
+ async function writeHandle(handle, bytes) {
76
+ const writable = await handle.createWritable();
77
+ try {
78
+ await writable.write(bytes);
79
+ } catch (err) {
80
+ await writable.abort?.().catch(() => {});
81
+ throw err;
82
+ }
83
+ await writable.close();
84
+ }
85
+
86
+ /**
87
+ * 保存到已知句柄(无句柄则触发下载);返回 { saved:boolean, handle }
88
+ */
89
+ export async function saveDocx(bytes, name, handle) {
90
+ if (handle?.createWritable) {
91
+ await writeHandle(handle, bytes);
92
+ return { saved: true, handle };
93
+ }
94
+ downloadBlob(new Blob([bytes], { type: DOCX_MIME }), ensureExt(name, '.docx'));
95
+ return { saved: true, handle: null };
96
+ }
97
+
98
+ /** 另存为:优先系统保存对话框,回退下载;返回 { saved, handle, name } */
99
+ export async function saveDocxAs(bytes, name) {
100
+ const suggested = ensureExt(name, '.docx');
101
+ if (typeof window.showSaveFilePicker === 'function' && !isCrossOriginFrame()) {
102
+ let handle = null;
103
+ try {
104
+ handle = await window.showSaveFilePicker({
105
+ suggestedName: suggested,
106
+ types: [{ description: 'Word 文档', accept: { [DOCX_MIME]: ['.docx'] } }],
107
+ });
108
+ } catch (err) {
109
+ if (err?.name === 'AbortError') return { saved: false, handle: null, name: suggested };
110
+ // 跨源框架/策略拒绝:回退下载
111
+ }
112
+ if (handle) {
113
+ await writeHandle(handle, bytes);
114
+ return { saved: true, handle, name: handle.name || suggested };
115
+ }
116
+ }
117
+ downloadBlob(new Blob([bytes], { type: DOCX_MIME }), suggested);
118
+ return { saved: true, handle: null, name: suggested };
119
+ }
120
+
121
+ /** 文件名兜底扩展名(空名用「新建文档」) */
122
+ export const ensureExt = (name, ext) => ensureExtWith(name, ext, '新建文档');
package/src/app/main.js CHANGED
@@ -1,9 +1,9 @@
1
- // [文件] gbdocx·main —— 应用引导(装配 WordApp 到 #app)
2
- import { WordApp } from './word-app.js';
3
-
4
- const mount = document.getElementById('app');
5
- const app = new WordApp(mount);
6
- app.start();
7
-
8
- // 调试入口(控制台取内核实例做快速自检)
9
- window.gbdocx = app;
1
+ // [文件] gbdocx·main —— 应用引导(装配 WordApp 到 #app)
2
+ import { WordApp } from './word-app.js';
3
+
4
+ const mount = document.getElementById('app');
5
+ const app = new WordApp(mount);
6
+ app.start();
7
+
8
+ // 调试入口(控制台取内核实例做快速自检)
9
+ window.gbdocx = app;
@@ -1,52 +1,52 @@
1
- // [文件] gbdocx·word·catalog —— 目录 —— 采集、定位、插入与刷新
2
-
3
- // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
- // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
-
6
- import { escapeHtml } from 'guabiutils';
7
- import { pageOfElement } from '../../kernel/docx/pagination.js';
8
- import { collectCatalog, gotoCatalogItem } from '../../kernel/docx/catalog.js';
9
- import { DOC_CLASS } from '../../kernel/editor/engine/schema.js';
10
- import { notify } from '../../kernel/notify.js';
11
- function refreshCatalog() {
12
- const items = collectCatalog(this.editor?.root || null);
13
- this.catalog = items;
14
- this.catalogPanel.setItems(items.map((it) => ({ key: it.key, text: it.text, level: it.level })));
15
- }
16
-
17
- function locateCatalog(key) {
18
- const item = this.catalog.find((c) => c.key === key);
19
- if (item) gotoCatalogItem({ value: this.catalog }, key);
20
- }
21
-
22
- function tocRowParas(items, root) {
23
- const innerW = Math.round(this.layout().innerW);
24
- return items
25
- .filter((it) => it.level <= 3)
26
- .map((it) => `<p style="white-space:pre-wrap;tab-size:${innerW}px;margin:0;">${escapeHtml(it.text)}\t${pageOfElement(root, it.el)}</p>`)
27
- .join('');
28
- }
29
-
30
- function insertToc() {
31
- const ed = this.editor;
32
- if (!ed || this.editMode === 'readonly') return;
33
- const items = collectCatalog(ed.root);
34
- const rows = items.length ? this.tocRowParas(items, ed.root) : '';
35
- if (!rows) return notify.warning('正文里还没有标题,先写几个标题再插目录');
36
- ed.focus();
37
- if (!this.run('insertHtml', `<div class="${DOC_CLASS.TOC}" contenteditable="false">${rows}</div>`)) notify.warning('请先把光标放进正文');
38
- this.onDocUpdate();
39
- }
40
-
41
- function refreshToc() {
42
- const root = this.editor?.root;
43
- const el = root?.querySelector(`.${DOC_CLASS.TOC}`);
44
- if (!root || !el || this.editMode === 'readonly') return notify.warning('文档里还没有目录');
45
- this.paginate();
46
- const rows = this.tocRowParas(collectCatalog(root), root);
47
- if (!rows) return;
48
- this.editor.run(() => { el.innerHTML = rows; });
49
- this.onDocUpdate();
50
- }
51
-
52
- export const catalogMethods = { refreshCatalog, locateCatalog, tocRowParas, insertToc, refreshToc };
1
+ // [文件] gbdocx·word·catalog —— 目录 —— 采集、定位、插入与刷新
2
+
3
+ // [设计] 方法以独立函数导出,由 word-app.js 末尾 Object.assign 装配到 WordApp.prototype:
4
+ // 仍是同一个 this(方法调用时绑定),调用方形状与拆分前完全一致。
5
+
6
+ import { escapeHtml } from 'guabiutils';
7
+ import { pageOfElement } from '../../kernel/docx/pagination.js';
8
+ import { collectCatalog, gotoCatalogItem } from '../../kernel/docx/catalog.js';
9
+ import { DOC_CLASS } from '../../kernel/editor/engine/schema.js';
10
+ import { notify } from '../../kernel/notify.js';
11
+ function refreshCatalog() {
12
+ const items = collectCatalog(this.editor?.root || null);
13
+ this.catalog = items;
14
+ this.catalogPanel.setItems(items.map((it) => ({ key: it.key, text: it.text, level: it.level })));
15
+ }
16
+
17
+ function locateCatalog(key) {
18
+ const item = this.catalog.find((c) => c.key === key);
19
+ if (item) gotoCatalogItem({ value: this.catalog }, key);
20
+ }
21
+
22
+ function tocRowParas(items, root) {
23
+ const innerW = Math.round(this.layout().innerW);
24
+ return items
25
+ .filter((it) => it.level <= 3)
26
+ .map((it) => `<p style="white-space:pre-wrap;tab-size:${innerW}px;margin:0;">${escapeHtml(it.text)}\t${pageOfElement(root, it.el)}</p>`)
27
+ .join('');
28
+ }
29
+
30
+ function insertToc() {
31
+ const ed = this.editor;
32
+ if (!ed || this.editMode === 'readonly') return;
33
+ const items = collectCatalog(ed.root);
34
+ const rows = items.length ? this.tocRowParas(items, ed.root) : '';
35
+ if (!rows) return notify.warning('正文里还没有标题,先写几个标题再插目录');
36
+ ed.focus();
37
+ if (!this.run('insertHtml', `<div class="${DOC_CLASS.TOC}" contenteditable="false">${rows}</div>`)) notify.warning('请先把光标放进正文');
38
+ this.onDocUpdate();
39
+ }
40
+
41
+ function refreshToc() {
42
+ const root = this.editor?.root;
43
+ const el = root?.querySelector(`.${DOC_CLASS.TOC}`);
44
+ if (!root || !el || this.editMode === 'readonly') return notify.warning('文档里还没有目录');
45
+ this.paginate();
46
+ const rows = this.tocRowParas(collectCatalog(root), root);
47
+ if (!rows) return;
48
+ this.editor.run(() => { el.innerHTML = rows; });
49
+ this.onDocUpdate();
50
+ }
51
+
52
+ export const catalogMethods = { refreshCatalog, locateCatalog, tocRowParas, insertToc, refreshToc };