@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
@@ -1,101 +1,101 @@
1
- // [文件] gbdocx·纸面层渲染 —— 分页纸面 + 页眉页脚带 + 页码浮层 + 脚注/尾注带 + 水印(纯渲染,几何来自 pageGeometry)
2
- import { GUTTER, bandStyle, fromSheetTop, pageNumberStyle, sheetStyle, stackStyle } from '../kernel/docx/pageGeometry.js';
3
- import { watermarkStyle } from '../kernel/docx/watermark.js';
4
-
5
- /**
6
- * 渲染纸面层
7
- * @param {HTMLElement} host 背景层容器(.re-backdrop-host)
8
- * @param {object} ctx
9
- * geo(), layout(), pageCount(), page(), pageNumbers(), watermark(), zoom()
10
- * hfMode(), hfTextAt(which,p), showHf(p,which), showPageNumber(p), hfDisplayHtml(which,p), footerHasPageField(p)
11
- * pageNumberText(p), fnPageOf(p), fnHeights(), bandEdit(), sheetStyleOf(p), hfBandStyle(p,which), pageNumStyle(p), fnBandStyle(p), enBandStyle()
12
- * onBandDblClick(slot, el), onFootnoteDblClick(fn), onEndnoteDblClick(en)
13
- */
14
- export function renderSheets(host, ctx) {
15
- const geo = ctx.geo();
16
- const layout = ctx.layout();
17
- const pageCount = ctx.pageCount();
18
- const wmStyle = watermarkStyle(ctx.watermark(), geo);
19
-
20
- host.replaceChildren();
21
- const sheets = document.createElement('div');
22
- sheets.className = 'word-sheets';
23
- Object.assign(sheets.style, stackStyle(pageCount, geo, layout));
24
- Object.assign(sheets.style, { left: '50%', transform: 'translateX(-50%)' });
25
-
26
- for (let p = 1; p <= pageCount; p += 1) {
27
- const sheet = document.createElement('div');
28
- sheet.className = 'word-sheet';
29
- Object.assign(sheet.style, ctx.sheetStyleOf(p));
30
- if (wmStyle) Object.assign(sheet.style, wmStyle);
31
- sheets.appendChild(sheet);
32
-
33
- for (const which of ['header', 'footer']) {
34
- if (!ctx.showHf(p, which)) continue;
35
- const slot = ctx.bandSlot(which, p);
36
- const editing = ctx.bandEdit() === slot;
37
- const band = document.createElement('div');
38
- band.className = `word-sheet-hf word-sheet-hf--${which === 'header' ? 'head' : 'foot'}`;
39
- if (editing) band.classList.add('gbd-is-editing');
40
- Object.assign(band.style, ctx.hfBandStyle(p, which));
41
- band.contentEditable = editing ? 'true' : 'false';
42
- band.innerHTML = ctx.hfDisplayHtml(which, p);
43
- band.addEventListener('dblclick', () => ctx.onBandDblClick(slot, band));
44
- band.addEventListener('blur', () => ctx.onBandCommit());
45
- band.addEventListener('keydown', (e) => {
46
- if (e.key === 'Escape') {
47
- e.preventDefault();
48
- ctx.onBandCommit();
49
- }
50
- });
51
- sheets.appendChild(band);
52
- }
53
-
54
- if (ctx.showPageNumber(p) && !ctx.footerHasPageField(p)) {
55
- const num = document.createElement('div');
56
- num.className = 'word-sheet-num';
57
- Object.assign(num.style, ctx.pageNumStyle(p));
58
- num.textContent = ctx.pageNumberText(p);
59
- sheets.appendChild(num);
60
- }
61
-
62
- const fns = ctx.fnPageOf(p);
63
- if (fns.length) {
64
- const band = document.createElement('div');
65
- band.className = 'word-sheet-fn';
66
- band.dataset.page = String(p);
67
- Object.assign(band.style, ctx.fnBandStyle(p));
68
- for (const fn of fns) {
69
- const item = document.createElement('div');
70
- item.className = 'word-sheet-fn-item';
71
- const sup = document.createElement('sup');
72
- sup.className = 'word-sheet-fn-ord';
73
- sup.textContent = fn.ord || '';
74
- item.append(sup, document.createTextNode(fn.text));
75
- item.addEventListener('dblclick', () => ctx.onFootnoteDblClick(fn));
76
- band.appendChild(item);
77
- }
78
- sheets.appendChild(band);
79
- }
80
- }
81
-
82
- const endnotes = ctx.endnotes();
83
- if (endnotes.length) {
84
- const band = document.createElement('div');
85
- band.className = 'word-sheet-fn word-sheet-en';
86
- Object.assign(band.style, ctx.enBandStyle());
87
- endnotes.forEach((en, i) => {
88
- const item = document.createElement('div');
89
- item.className = 'word-sheet-fn-item';
90
- const sup = document.createElement('sup');
91
- sup.className = 'word-sheet-fn-ord';
92
- sup.textContent = String(i + 1);
93
- item.append(sup, document.createTextNode(en.text));
94
- item.addEventListener('dblclick', () => ctx.onEndnoteDblClick(en));
95
- band.appendChild(item);
96
- });
97
- sheets.appendChild(band);
98
- }
99
-
100
- host.appendChild(sheets);
101
- }
1
+ // [文件] gbdocx·纸面层渲染 —— 分页纸面 + 页眉页脚带 + 页码浮层 + 脚注/尾注带 + 水印(纯渲染,几何来自 pageGeometry)
2
+ import { GUTTER, bandStyle, fromSheetTop, pageNumberStyle, sheetStyle, stackStyle } from '../kernel/docx/pageGeometry.js';
3
+ import { watermarkStyle } from '../kernel/docx/watermark.js';
4
+
5
+ /**
6
+ * 渲染纸面层
7
+ * @param {HTMLElement} host 背景层容器(.re-backdrop-host)
8
+ * @param {object} ctx
9
+ * geo(), layout(), pageCount(), page(), pageNumbers(), watermark(), zoom()
10
+ * hfMode(), hfTextAt(which,p), showHf(p,which), showPageNumber(p), hfDisplayHtml(which,p), footerHasPageField(p)
11
+ * pageNumberText(p), fnPageOf(p), fnHeights(), bandEdit(), sheetStyleOf(p), hfBandStyle(p,which), pageNumStyle(p), fnBandStyle(p), enBandStyle()
12
+ * onBandDblClick(slot, el), onFootnoteDblClick(fn), onEndnoteDblClick(en)
13
+ */
14
+ export function renderSheets(host, ctx) {
15
+ const geo = ctx.geo();
16
+ const layout = ctx.layout();
17
+ const pageCount = ctx.pageCount();
18
+ const wmStyle = watermarkStyle(ctx.watermark(), geo);
19
+
20
+ host.replaceChildren();
21
+ const sheets = document.createElement('div');
22
+ sheets.className = 'word-sheets';
23
+ Object.assign(sheets.style, stackStyle(pageCount, geo, layout));
24
+ Object.assign(sheets.style, { left: '50%', transform: 'translateX(-50%)' });
25
+
26
+ for (let p = 1; p <= pageCount; p += 1) {
27
+ const sheet = document.createElement('div');
28
+ sheet.className = 'word-sheet';
29
+ Object.assign(sheet.style, ctx.sheetStyleOf(p));
30
+ if (wmStyle) Object.assign(sheet.style, wmStyle);
31
+ sheets.appendChild(sheet);
32
+
33
+ for (const which of ['header', 'footer']) {
34
+ if (!ctx.showHf(p, which)) continue;
35
+ const slot = ctx.bandSlot(which, p);
36
+ const editing = ctx.bandEdit() === slot;
37
+ const band = document.createElement('div');
38
+ band.className = `word-sheet-hf word-sheet-hf--${which === 'header' ? 'head' : 'foot'}`;
39
+ if (editing) band.classList.add('gbd-is-editing');
40
+ Object.assign(band.style, ctx.hfBandStyle(p, which));
41
+ band.contentEditable = editing ? 'true' : 'false';
42
+ band.innerHTML = ctx.hfDisplayHtml(which, p);
43
+ band.addEventListener('dblclick', () => ctx.onBandDblClick(slot, band));
44
+ band.addEventListener('blur', () => ctx.onBandCommit());
45
+ band.addEventListener('keydown', (e) => {
46
+ if (e.key === 'Escape') {
47
+ e.preventDefault();
48
+ ctx.onBandCommit();
49
+ }
50
+ });
51
+ sheets.appendChild(band);
52
+ }
53
+
54
+ if (ctx.showPageNumber(p) && !ctx.footerHasPageField(p)) {
55
+ const num = document.createElement('div');
56
+ num.className = 'word-sheet-num';
57
+ Object.assign(num.style, ctx.pageNumStyle(p));
58
+ num.textContent = ctx.pageNumberText(p);
59
+ sheets.appendChild(num);
60
+ }
61
+
62
+ const fns = ctx.fnPageOf(p);
63
+ if (fns.length) {
64
+ const band = document.createElement('div');
65
+ band.className = 'word-sheet-fn';
66
+ band.dataset.page = String(p);
67
+ Object.assign(band.style, ctx.fnBandStyle(p));
68
+ for (const fn of fns) {
69
+ const item = document.createElement('div');
70
+ item.className = 'word-sheet-fn-item';
71
+ const sup = document.createElement('sup');
72
+ sup.className = 'word-sheet-fn-ord';
73
+ sup.textContent = fn.ord || '';
74
+ item.append(sup, document.createTextNode(fn.text));
75
+ item.addEventListener('dblclick', () => ctx.onFootnoteDblClick(fn));
76
+ band.appendChild(item);
77
+ }
78
+ sheets.appendChild(band);
79
+ }
80
+ }
81
+
82
+ const endnotes = ctx.endnotes();
83
+ if (endnotes.length) {
84
+ const band = document.createElement('div');
85
+ band.className = 'word-sheet-fn word-sheet-en';
86
+ Object.assign(band.style, ctx.enBandStyle());
87
+ endnotes.forEach((en, i) => {
88
+ const item = document.createElement('div');
89
+ item.className = 'word-sheet-fn-item';
90
+ const sup = document.createElement('sup');
91
+ sup.className = 'word-sheet-fn-ord';
92
+ sup.textContent = String(i + 1);
93
+ item.append(sup, document.createTextNode(en.text));
94
+ item.addEventListener('dblclick', () => ctx.onEndnoteDblClick(en));
95
+ band.appendChild(item);
96
+ });
97
+ sheets.appendChild(band);
98
+ }
99
+
100
+ host.appendChild(sheets);
101
+ }
@@ -1,41 +1,41 @@
1
- // [文件] web·office·docx·目录 —— 正文标题树抽取与点击定位(列表项带元素引用,缩进由 level 表达,面板只消费这份扁平列表)
2
- import { DOC_CLASS } from '../editor/engine/schema.js';
3
- import { CATALOG_HIT } from '../editor/engine/dom.js';
4
-
5
- /**
6
- * 抽目录:正文根内的标题按文档顺序扁平化
7
- * @param {HTMLElement} root 编辑面根
8
- * @returns {Array<{key:string, text:string, level:number, el:HTMLElement}>}
9
- */
10
- export const collectCatalog = (root) => {
11
- if (!root) return [];
12
- const out = [];
13
- for (const el of Array.from(root.querySelectorAll('h1,h2,h3,h4,h5,h6'))) {
14
- const text = (el.textContent || '').trim();
15
- if (!text) continue;
16
- const level = el.classList.contains(DOC_CLASS.TITLE) ? 1 : Number(el.tagName[1]);
17
- out.push({ key: `t${out.length + 1}`, text, level, el });
18
- }
19
- return out;
20
- };
21
-
22
- /** 目录项定位:滚进视口并闪一下(与批注锚点定位同口径,禁各自写一套滚动) */
23
- const scrollToCatalogItem = (el) => {
24
- if (!el) return;
25
- el.scrollIntoView({ block: 'center', behavior: 'smooth' });
26
- el.classList.add(CATALOG_HIT);
27
- setTimeout(() => el.classList.remove(CATALOG_HIT), 1200);
28
- };
29
-
30
- /** 目录接线:重建目录(原位替换,Vue 才能追踪) */
31
- export const bindCatalog = (list, root) => {
32
- list.value = collectCatalog(root);
33
- };
34
-
35
- /** 定位某一项,返回命中的键(未命中返回 null) */
36
- export const gotoCatalogItem = (list, key) => {
37
- const item = (list.value || []).find((c) => c.key === key);
38
- if (!item) return null;
39
- scrollToCatalogItem(item.el);
40
- return key;
41
- };
1
+ // [文件] web·office·docx·目录 —— 正文标题树抽取与点击定位(列表项带元素引用,缩进由 level 表达,面板只消费这份扁平列表)
2
+ import { DOC_CLASS } from '../editor/engine/schema.js';
3
+ import { CATALOG_HIT } from '../editor/engine/dom.js';
4
+
5
+ /**
6
+ * 抽目录:正文根内的标题按文档顺序扁平化
7
+ * @param {HTMLElement} root 编辑面根
8
+ * @returns {Array<{key:string, text:string, level:number, el:HTMLElement}>}
9
+ */
10
+ export const collectCatalog = (root) => {
11
+ if (!root) return [];
12
+ const out = [];
13
+ for (const el of Array.from(root.querySelectorAll('h1,h2,h3,h4,h5,h6'))) {
14
+ const text = (el.textContent || '').trim();
15
+ if (!text) continue;
16
+ const level = el.classList.contains(DOC_CLASS.TITLE) ? 1 : Number(el.tagName[1]);
17
+ out.push({ key: `t${out.length + 1}`, text, level, el });
18
+ }
19
+ return out;
20
+ };
21
+
22
+ /** 目录项定位:滚进视口并闪一下(与批注锚点定位同口径,禁各自写一套滚动) */
23
+ const scrollToCatalogItem = (el) => {
24
+ if (!el) return;
25
+ el.scrollIntoView({ block: 'center', behavior: 'smooth' });
26
+ el.classList.add(CATALOG_HIT);
27
+ setTimeout(() => el.classList.remove(CATALOG_HIT), 1200);
28
+ };
29
+
30
+ /** 目录接线:重建目录(原位替换,Vue 才能追踪) */
31
+ export const bindCatalog = (list, root) => {
32
+ list.value = collectCatalog(root);
33
+ };
34
+
35
+ /** 定位某一项,返回命中的键(未命中返回 null) */
36
+ export const gotoCatalogItem = (list, key) => {
37
+ const item = (list.value || []).find((c) => c.key === key);
38
+ if (!item) return null;
39
+ scrollToCatalogItem(item.el);
40
+ return key;
41
+ };
@@ -1,123 +1,123 @@
1
- // [文件] web·docx·codec·blocks —— 块模型:HTML 与 OOXML 双方归一到的中间结构(写出与读回只认它)
2
-
3
- /** run 的类型。嵌套关系(修订标记包住一串 run、批注范围包住一串 run)用扁平序列
4
- * 里的开闭标记表达,模型里不再嵌一层树——OOXML 侧本来就是 w:ins/w:commentRange 这种包夹元素 */
5
- export const MARK = {
6
- TEXT: 'text', // 普通文字
7
- BREAK: 'break', // 软换行 <w:br/>
8
- PAGE: 'page', // 分页符 <w:br w:type="page"/>
9
- IMAGE: 'image', // 内嵌图片
10
- NOTE: 'note', // 脚注/尾注引用 <w:footnoteReference>/<w:endnoteReference>
11
- REV_OPEN: 'revOpen', // 修订标记起点
12
- REV_CLOSE: 'revClose', // 修订标记终点
13
- CMT_OPEN: 'cmtOpen', // 批注范围起点
14
- CMT_CLOSE: 'cmtClose' // 批注范围终点
15
- };
16
-
17
- /**
18
- * 行内格式。OOXML 的 rPr 是「显式开、显式关」的集合,故这里用布尔而非三态
19
- * @returns {{bold:boolean, italic:boolean, underline:boolean, strike:boolean, superscript:boolean, subscript:boolean,
20
- * mono:boolean, del:boolean, font:string, color:string, bg:string, size:number, link:string}}
21
- */
22
- export function makeForm(fields = {}) {
23
- return {
24
- bold: false,
25
- italic: false,
26
- underline: false,
27
- strike: false,
28
- superscript: false, // 上标(w:vertAlign superscript;与下标互斥)
29
- subscript: false, // 下标(w:vertAlign subscript)
30
- mono: false, // 等宽(代码)
31
- del: false, // 删除建议内的文字,要写 w:delText 而不是 w:t
32
- font: '', // 字体族名(拉丁字形,w:ascii/hAnsi)
33
- fontEa: '', // 东亚字形字体名(w:eastAsia);空则按 font 的字体链反解
34
- hint: '', // 歧义标点的字形归属('eastAsia' = 交给中文字体,对应 w:rFonts/@w:hint)
35
- spacing: 0, // 字符间距(px,负数为收紧);0 = 不写
36
- scale: 0, // 字符缩放(百分比);0/100 = 不缩放
37
- color: '', // "#rrggbb"
38
- colorTheme: '', // 主题色引用(w:themeColor 名;非空时按引用落盘,不摊平成 hex)
39
- colorThemeTint: '', // 主题色调亮(w:themeTint,两位 hex 原样透传)
40
- colorThemeShade: '', // 主题色调暗(w:themeShade)
41
- bg: '', // "#rrggbb" 文字底色
42
- size: 0, // px;0 = 跟随默认
43
- link: '', // 处于超链接内时的 href
44
- ...fields
45
- };
46
- }
47
-
48
- /** 一段行内内容 */
49
- export function makeRun(fields = {}) {
50
- const { mark = MARK.TEXT, text = '', rev = '', cmt = 0, note = '', end = false, author = '', date = '', img = null, form = null } = fields;
51
- return { mark, text, rev, cmt, note, end, author, date, img, form: form ?? makeForm() };
52
- }
53
-
54
- /** 造一段普通文字 run */
55
- export const textRun = (text, form) => makeRun({ mark: MARK.TEXT, text, form: form ?? makeForm() });
56
-
57
- /** 列表项编号引用。numId 1 是项目符号、2 是有序号、3/4 是已勾选/未勾选待办(对应 numbering.xml) */
58
- export const numRef = (level, ordered, task = '') => ({ level, ordered, task });
59
-
60
- /**
61
- * 段落。列表项也是段落,只是多了编号引用
62
- * @returns {{style:string, align:string, indent:number, firstLine:number, hanging:number, before:number, after:number,
63
- * line:number, lineExact:number, shade:string, borders:Object|null,
64
- * tabs:Array|null, num:null|{level:number,ordered:boolean}, runs:Array}}
65
- */
66
- export function makePara(fields = {}) {
67
- return {
68
- style: '', // pStyle 值(Heading1~6 / Title);"" = 正文
69
- align: '', // "" | center | right | justify
70
- indent: 0, // 左缩进(twips);0 = 不写
71
- firstLine: 0, // 首行缩进(twips);0 = 不写
72
- hanging: 0, // 悬挂缩进(twips);0 = 不写
73
- before: 0, // 段前(twips);0 = 不写
74
- after: 0, // 段后(twips);0 = 不写
75
- line: 0, // 行距(240 分之一行;CSS 倍数的等价编码);0 = 不写
76
- lineExact: 0, // 固定行距(twips;来自导入文档的 exact/atLeast);>0 优先于 line
77
- shade: '', // 段落底色 "RRGGBB"(代码块靠它标记)
78
- borders: null, // 段落边框 { top?: {size,color}, bottom?: {size,color}, left?, right? };size = 1/8 磅
79
- tabs: null, // 自定义制表位 [{pos,align,leader}];null = 不写 w:tabs
80
- keepNext: false, // 与下段同页(w:keepNext)
81
- keepLines: false, // 整段不拆页(w:keepLines)
82
- pageBreakBefore: false, // 段前分页(w:pageBreakBefore)
83
- widowOff: false, // 关闭孤行控制(w:widowControl val=0;缺省即开启,只在显式关时写)
84
- num: null, // 非 null 表示列表项
85
- runs: [],
86
- ...fields
87
- };
88
- }
89
-
90
- /** 一张内嵌图片,尺寸已是最终显示值(像素) */
91
- export function makeImageRef(fields = {}) {
92
- return { data: new Uint8Array(0), ext: '', w: 0, h: 0, ...fields };
93
- }
94
-
95
- /** 一个网格格 */
96
- export function makeCell(fields = {}) {
97
- return {
98
- span: 1, // 横向跨列
99
- restart: false, // 纵向合并起点(w:vMerge restart)
100
- cont: false, // 纵向合并续格(空 tc + 无值 w:vMerge)
101
- shade: '', // "RRGGBB"
102
- vAlign: '', // 垂直对齐 'top'|'center'|'bottom';'' = 未声明(Word 默认 top)
103
- width: 0, // 占表格宽的百分比
104
- blocks: [],
105
- ...fields
106
- };
107
- }
108
-
109
- /** 一个块级元素:段落与表格二选一;secBreak = 节边界标记块、toc = 目录块(TOC 域,缓存行在 tocBlocks) */
110
- const makeBlock = (fields = {}) => ({ para: null, table: null, spanAll: false, secBreak: false, toc: false, tocBlocks: null, ...fields });
111
-
112
- /** 段落块(spanAll 顶层块的跨栏标记:多栏文档里 col-span-all 的块自成单栏节,
113
- * 与分栏正文之间要插连续分节符——HTML 侧的 class 与 OOXML 侧的单栏节是同一件事) */
114
- export const paraBlock = (para, spanAll = false) => makeBlock({ para, spanAll });
115
-
116
- /** 表格块 */
117
- export const tableBlock = (table) => makeBlock({ table });
118
-
119
- /** 节边界块(内嵌 sectPr 所在位置;写出端据此在正文流中落该节的 sectPr) */
120
- export const secBreakBlock = () => makeBlock({ secBreak: true });
121
-
122
- /** 目录块(TOC 域的缓存行序列;写出端包回域幕,读端把域跨度收成这一个块) */
123
- export const tocBlock = (blocks) => makeBlock({ toc: true, tocBlocks: blocks });
1
+ // [文件] web·docx·codec·blocks —— 块模型:HTML 与 OOXML 双方归一到的中间结构(写出与读回只认它)
2
+
3
+ /** run 的类型。嵌套关系(修订标记包住一串 run、批注范围包住一串 run)用扁平序列
4
+ * 里的开闭标记表达,模型里不再嵌一层树——OOXML 侧本来就是 w:ins/w:commentRange 这种包夹元素 */
5
+ export const MARK = {
6
+ TEXT: 'text', // 普通文字
7
+ BREAK: 'break', // 软换行 <w:br/>
8
+ PAGE: 'page', // 分页符 <w:br w:type="page"/>
9
+ IMAGE: 'image', // 内嵌图片
10
+ NOTE: 'note', // 脚注/尾注引用 <w:footnoteReference>/<w:endnoteReference>
11
+ REV_OPEN: 'revOpen', // 修订标记起点
12
+ REV_CLOSE: 'revClose', // 修订标记终点
13
+ CMT_OPEN: 'cmtOpen', // 批注范围起点
14
+ CMT_CLOSE: 'cmtClose' // 批注范围终点
15
+ };
16
+
17
+ /**
18
+ * 行内格式。OOXML 的 rPr 是「显式开、显式关」的集合,故这里用布尔而非三态
19
+ * @returns {{bold:boolean, italic:boolean, underline:boolean, strike:boolean, superscript:boolean, subscript:boolean,
20
+ * mono:boolean, del:boolean, font:string, color:string, bg:string, size:number, link:string}}
21
+ */
22
+ export function makeForm(fields = {}) {
23
+ return {
24
+ bold: false,
25
+ italic: false,
26
+ underline: false,
27
+ strike: false,
28
+ superscript: false, // 上标(w:vertAlign superscript;与下标互斥)
29
+ subscript: false, // 下标(w:vertAlign subscript)
30
+ mono: false, // 等宽(代码)
31
+ del: false, // 删除建议内的文字,要写 w:delText 而不是 w:t
32
+ font: '', // 字体族名(拉丁字形,w:ascii/hAnsi)
33
+ fontEa: '', // 东亚字形字体名(w:eastAsia);空则按 font 的字体链反解
34
+ hint: '', // 歧义标点的字形归属('eastAsia' = 交给中文字体,对应 w:rFonts/@w:hint)
35
+ spacing: 0, // 字符间距(px,负数为收紧);0 = 不写
36
+ scale: 0, // 字符缩放(百分比);0/100 = 不缩放
37
+ color: '', // "#rrggbb"
38
+ colorTheme: '', // 主题色引用(w:themeColor 名;非空时按引用落盘,不摊平成 hex)
39
+ colorThemeTint: '', // 主题色调亮(w:themeTint,两位 hex 原样透传)
40
+ colorThemeShade: '', // 主题色调暗(w:themeShade)
41
+ bg: '', // "#rrggbb" 文字底色
42
+ size: 0, // px;0 = 跟随默认
43
+ link: '', // 处于超链接内时的 href
44
+ ...fields
45
+ };
46
+ }
47
+
48
+ /** 一段行内内容 */
49
+ export function makeRun(fields = {}) {
50
+ const { mark = MARK.TEXT, text = '', rev = '', cmt = 0, note = '', end = false, author = '', date = '', img = null, form = null } = fields;
51
+ return { mark, text, rev, cmt, note, end, author, date, img, form: form ?? makeForm() };
52
+ }
53
+
54
+ /** 造一段普通文字 run */
55
+ export const textRun = (text, form) => makeRun({ mark: MARK.TEXT, text, form: form ?? makeForm() });
56
+
57
+ /** 列表项编号引用。numId 1 是项目符号、2 是有序号、3/4 是已勾选/未勾选待办(对应 numbering.xml) */
58
+ export const numRef = (level, ordered, task = '') => ({ level, ordered, task });
59
+
60
+ /**
61
+ * 段落。列表项也是段落,只是多了编号引用
62
+ * @returns {{style:string, align:string, indent:number, firstLine:number, hanging:number, before:number, after:number,
63
+ * line:number, lineExact:number, shade:string, borders:Object|null,
64
+ * tabs:Array|null, num:null|{level:number,ordered:boolean}, runs:Array}}
65
+ */
66
+ export function makePara(fields = {}) {
67
+ return {
68
+ style: '', // pStyle 值(Heading1~6 / Title);"" = 正文
69
+ align: '', // "" | center | right | justify
70
+ indent: 0, // 左缩进(twips);0 = 不写
71
+ firstLine: 0, // 首行缩进(twips);0 = 不写
72
+ hanging: 0, // 悬挂缩进(twips);0 = 不写
73
+ before: 0, // 段前(twips);0 = 不写
74
+ after: 0, // 段后(twips);0 = 不写
75
+ line: 0, // 行距(240 分之一行;CSS 倍数的等价编码);0 = 不写
76
+ lineExact: 0, // 固定行距(twips;来自导入文档的 exact/atLeast);>0 优先于 line
77
+ shade: '', // 段落底色 "RRGGBB"(代码块靠它标记)
78
+ borders: null, // 段落边框 { top?: {size,color}, bottom?: {size,color}, left?, right? };size = 1/8 磅
79
+ tabs: null, // 自定义制表位 [{pos,align,leader}];null = 不写 w:tabs
80
+ keepNext: false, // 与下段同页(w:keepNext)
81
+ keepLines: false, // 整段不拆页(w:keepLines)
82
+ pageBreakBefore: false, // 段前分页(w:pageBreakBefore)
83
+ widowOff: false, // 关闭孤行控制(w:widowControl val=0;缺省即开启,只在显式关时写)
84
+ num: null, // 非 null 表示列表项
85
+ runs: [],
86
+ ...fields
87
+ };
88
+ }
89
+
90
+ /** 一张内嵌图片,尺寸已是最终显示值(像素) */
91
+ export function makeImageRef(fields = {}) {
92
+ return { data: new Uint8Array(0), ext: '', w: 0, h: 0, ...fields };
93
+ }
94
+
95
+ /** 一个网格格 */
96
+ export function makeCell(fields = {}) {
97
+ return {
98
+ span: 1, // 横向跨列
99
+ restart: false, // 纵向合并起点(w:vMerge restart)
100
+ cont: false, // 纵向合并续格(空 tc + 无值 w:vMerge)
101
+ shade: '', // "RRGGBB"
102
+ vAlign: '', // 垂直对齐 'top'|'center'|'bottom';'' = 未声明(Word 默认 top)
103
+ width: 0, // 占表格宽的百分比
104
+ blocks: [],
105
+ ...fields
106
+ };
107
+ }
108
+
109
+ /** 一个块级元素:段落与表格二选一;secBreak = 节边界标记块、toc = 目录块(TOC 域,缓存行在 tocBlocks) */
110
+ const makeBlock = (fields = {}) => ({ para: null, table: null, spanAll: false, secBreak: false, toc: false, tocBlocks: null, ...fields });
111
+
112
+ /** 段落块(spanAll 顶层块的跨栏标记:多栏文档里 col-span-all 的块自成单栏节,
113
+ * 与分栏正文之间要插连续分节符——HTML 侧的 class 与 OOXML 侧的单栏节是同一件事) */
114
+ export const paraBlock = (para, spanAll = false) => makeBlock({ para, spanAll });
115
+
116
+ /** 表格块 */
117
+ export const tableBlock = (table) => makeBlock({ table });
118
+
119
+ /** 节边界块(内嵌 sectPr 所在位置;写出端据此在正文流中落该节的 sectPr) */
120
+ export const secBreakBlock = () => makeBlock({ secBreak: true });
121
+
122
+ /** 目录块(TOC 域的缓存行序列;写出端包回域幕,读端把域跨度收成这一个块) */
123
+ export const tocBlock = (blocks) => makeBlock({ toc: true, tocBlocks: blocks });