@a200888213/gbdocx 0.0.0-stage → 0.4.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.
- package/LICENSE +21 -0
- package/README.md +206 -2
- package/index.html +25 -0
- package/package.json +48 -6
- package/serve.mjs +22 -0
- package/src/app/file-io.js +122 -0
- package/src/app/main.js +9 -0
- package/src/app/word/catalog.js +52 -0
- package/src/app/word/comments.js +112 -0
- package/src/app/word/doc.js +113 -0
- package/src/app/word/edit.js +72 -0
- package/src/app/word/file.js +162 -0
- package/src/app/word/hf.js +76 -0
- package/src/app/word/layout.js +155 -0
- package/src/app/word/menus.js +175 -0
- package/src/app/word/notes.js +110 -0
- package/src/app/word/shared.js +22 -0
- package/src/app/word/view.js +117 -0
- package/src/app/word-app.js +459 -0
- package/src/app/word-sheets.js +101 -0
- package/src/kernel/docx/catalog.js +41 -0
- package/src/kernel/docx/codec/blocks.js +123 -0
- package/src/kernel/docx/codec/carry.js +129 -0
- package/src/kernel/docx/codec/comments.js +25 -0
- package/src/kernel/docx/codec/docx.js +198 -0
- package/src/kernel/docx/codec/docxStyles.js +177 -0
- package/src/kernel/docx/codec/fonts.js +195 -0
- package/src/kernel/docx/codec/helpers.js +161 -0
- package/src/kernel/docx/codec/hfHtml.js +152 -0
- package/src/kernel/docx/codec/html.js +739 -0
- package/src/kernel/docx/codec/losses.js +420 -0
- package/src/kernel/docx/codec/markdown.js +170 -0
- package/src/kernel/docx/codec/parts.js +249 -0
- package/src/kernel/docx/codec/read.js +603 -0
- package/src/kernel/docx/codec/render.js +1156 -0
- package/src/kernel/docx/codec/roundtrip.js +236 -0
- package/src/kernel/docx/codec/write.js +833 -0
- package/src/kernel/docx/comments.js +14 -0
- package/src/kernel/docx/constants.js +63 -0
- package/src/kernel/docx/menus.js +211 -0
- package/src/kernel/docx/pageGeometry.js +123 -0
- package/src/kernel/docx/pagination.js +481 -0
- package/src/kernel/docx/watermark.js +45 -0
- package/src/kernel/docx/wordShortcuts.js +37 -0
- package/src/kernel/editor/constants.js +8 -0
- package/src/kernel/editor/engine/blocks.js +594 -0
- package/src/kernel/editor/engine/blocksEnter.js +109 -0
- package/src/kernel/editor/engine/blocksInsert.js +254 -0
- package/src/kernel/editor/engine/blocksStructure.js +226 -0
- package/src/kernel/editor/engine/commands.js +181 -0
- package/src/kernel/editor/engine/dom.js +646 -0
- package/src/kernel/editor/engine/editor.js +213 -0
- package/src/kernel/editor/engine/history.js +149 -0
- package/src/kernel/editor/engine/inline.js +656 -0
- package/src/kernel/editor/engine/input.js +554 -0
- package/src/kernel/editor/engine/interaction.js +137 -0
- package/src/kernel/editor/engine/pointer.js +39 -0
- package/src/kernel/editor/engine/revision.js +138 -0
- package/src/kernel/editor/engine/schema.js +417 -0
- package/src/kernel/editor/engine/search.js +113 -0
- package/src/kernel/editor/engine/selection.js +119 -0
- package/src/kernel/editor/engine/state.js +118 -0
- package/src/kernel/editor/engine/tableClip.js +154 -0
- package/src/kernel/editor/engine/tableGrid.js +55 -0
- package/src/kernel/editor/engine/tableInteraction.js +388 -0
- package/src/kernel/editor/engine/tableMenu.js +250 -0
- package/src/kernel/editor/engine/tableOps.js +174 -0
- package/src/kernel/editor/engine/tables.js +732 -0
- package/src/kernel/editor/overlayGeometry.js +20 -0
- package/src/kernel/editor/pasteClean.js +293 -0
- package/src/kernel/editor/utils.js +49 -0
- package/src/kernel/keyboard.js +114 -0
- package/src/kernel/notify.js +54 -0
- package/src/kernel/textStyles.js +84 -0
- package/src/kernel/util/clipboard.js +89 -0
- package/src/kernel/util/imageFile.js +91 -0
- package/src/kernel/util/safeValue.js +25 -0
- package/src/kernel/util/shortcutKey.js +33 -0
- package/src/surface/editor-surface.js +600 -0
- package/src/ui/color-panel.js +69 -0
- package/src/ui/context-menu.js +130 -0
- package/src/ui/dialogs.js +249 -0
- package/src/ui/find-bar.js +151 -0
- package/src/ui/icons.js +134 -0
- package/src/ui/menubar.js +28 -0
- package/src/ui/panels.js +115 -0
- package/src/ui/popup.js +79 -0
- package/src/ui/prompt.js +89 -0
- package/src/ui/ribbon.js +485 -0
- package/src/ui/special-chars.js +39 -0
- package/src/ui/statusbar.js +81 -0
- package/src/ui/toolbar.js +32 -0
- package/styles/app.css +944 -0
- package/styles/editor-content.css +440 -0
- package/styles/tokens.css +154 -0
- package/styles/word-panel.css +410 -0
|
@@ -0,0 +1,739 @@
|
|
|
1
|
+
// [文件] web·docx·codec·html —— 编辑器正文 HTML 子集 → 块模型(浏览器 DOM 版:字符串走 DOMParser,也可直接吃活的编辑面元素,全程只读)
|
|
2
|
+
/*
|
|
3
|
+
正文 HTML 只认 contenteditable 会产出的那一小撮标签。两个标签集合是分开的:
|
|
4
|
+
BLOCK_TAGS 决定「一个容器是不是装块级内容」(决定要不要拆成多段),
|
|
5
|
+
INLINE_BREAKERS 决定「行内语境里冒出哪些标签时必须先把当前段落地再单独成块」。
|
|
6
|
+
两者的成员并不相同,合成一个集合会让 <h2> 被降级成普通段落。
|
|
7
|
+
*/
|
|
8
|
+
import { PX_PER_MM, decodeDataUrl, hexColor, htmlAttr, htmlStyleProp } from 'gbooxml';
|
|
9
|
+
import { decodeTabs, TABS_ATTR, TBL_BDR_ATTR, TBL_MAR_ATTR } from './helpers.js';
|
|
10
|
+
import { isSafeHref } from '../../util/safeValue.js';
|
|
11
|
+
import { parseFontFamily } from './fonts.js';
|
|
12
|
+
import { COMMENT_ATTR, DOC_CLASS, HINT_ATTR, NOTE_ATTR, THEME_COLOR_ATTR, THEME_SHADE_ATTR, THEME_TINT_ATTR } from '../../editor/engine/schema.js';
|
|
13
|
+
import { TAG_TO_MARK } from '../../editor/engine/inline.js';
|
|
14
|
+
import { CODE_FILL, QUOTE_INDENT_TWIPS, RULE_COLOR } from './parts.js';
|
|
15
|
+
import { MARK, makeCell, makeForm, makeImageRef, makePara, makeRun, numRef, paraBlock, secBreakBlock, tableBlock, textRun, tocBlock } from './blocks.js';
|
|
16
|
+
import { textWidthMM, pxToTwips } from './docx.js';
|
|
17
|
+
|
|
18
|
+
const BLOCK_TAGS = new Set(['p', 'div', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'table', 'blockquote', 'pre', 'hr', 'li']);
|
|
19
|
+
|
|
20
|
+
const INLINE_BREAKERS = new Set(['ul', 'ol', 'table', 'p', 'div', 'blockquote', 'pre']);
|
|
21
|
+
|
|
22
|
+
const tagOf = (el) => (el.tagName || '').toLowerCase();
|
|
23
|
+
|
|
24
|
+
/** 按「空白分隔的类名」匹配(避免 "doc-title2" 命中 "doc-title") */
|
|
25
|
+
const hasClass = (el, cls) => ` ${htmlAttr(el, 'class')} `.includes(` ${cls} `);
|
|
26
|
+
|
|
27
|
+
/** 一条 border 声明("2px solid #ff0000")→ { size, color };size 单位 1/8 磅(与 w:sz 同口径) */
|
|
28
|
+
const borderOf = (decl) => {
|
|
29
|
+
const m = /^([\d.]+)px\s+solid\s+(\S+)$/i.exec(String(decl || '').trim());
|
|
30
|
+
if (!m) return null;
|
|
31
|
+
const px = Number.parseFloat(m[1]);
|
|
32
|
+
// px → pt(×0.75) → 1/8pt(×8)
|
|
33
|
+
return { size: px > 0 ? Math.max(1, Math.round(px * 6)) : 0, color: hexColor(m[2], '') };
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/** 元素行内样式里的段落边框(HTML 侧只用 border-top / border-bottom 表达上下两条线) */
|
|
37
|
+
const bordersOfStyle = (style) => {
|
|
38
|
+
const out = {};
|
|
39
|
+
const top = borderOf(htmlStyleProp(style || '', 'border-top'));
|
|
40
|
+
if (top) out.top = top;
|
|
41
|
+
const bottom = borderOf(htmlStyleProp(style || '', 'border-bottom'));
|
|
42
|
+
if (bottom) out.bottom = bottom;
|
|
43
|
+
return Object.keys(out).length ? out : null;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** 分隔线段落(<hr>):没有样式时落默认细灰下边框,与编辑器自己的分隔线同一口径 */
|
|
47
|
+
const rulePara = (style) => ({
|
|
48
|
+
...makePara(),
|
|
49
|
+
borders: bordersOfStyle(style) || { bottom: { size: 6, color: RULE_COLOR } }
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const hasBlockChild = (el) => Array.from(el.children).some((c) => BLOCK_TAGS.has(tagOf(c)));
|
|
53
|
+
|
|
54
|
+
/** 行内文本里的换行折成空格(HTML 里换行就是空白,真换行由 <br> 表达) */
|
|
55
|
+
const normalizeText = (s) => s.replace(/\n/g, ' ');
|
|
56
|
+
|
|
57
|
+
/** 取纯文本,<br> 折成换行(代码块按行切分要用它) */
|
|
58
|
+
const textContent = (el) => {
|
|
59
|
+
let out = '';
|
|
60
|
+
const walk = (x) => {
|
|
61
|
+
if (x.nodeType === Node.TEXT_NODE) {
|
|
62
|
+
out += x.data;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (x.nodeType === Node.ELEMENT_NODE && tagOf(x) === 'br') out += '\n';
|
|
66
|
+
for (const c of x.childNodes) walk(c);
|
|
67
|
+
};
|
|
68
|
+
walk(el);
|
|
69
|
+
return out;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
/** 读整型属性(缺省或非法回退 def) */
|
|
73
|
+
const attrInt = (el, name, def) => {
|
|
74
|
+
const v = Number.parseInt(String(htmlAttr(el, name)).trim(), 10);
|
|
75
|
+
return !Number.isNaN(v) && v > 0 ? v : def;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** 读整型属性并夹到 [1, max] */
|
|
79
|
+
const clampAttr = (el, name, def, max) => Math.min(Math.max(attrInt(el, name, def), 1), max);
|
|
80
|
+
|
|
81
|
+
/** 字号声明换算成像素(px 直取,pt 按 96dpi 折) */
|
|
82
|
+
const fontSizePx = (decl, keep) => {
|
|
83
|
+
if (decl.endsWith('px')) {
|
|
84
|
+
const v = Number.parseFloat(decl.slice(0, -2));
|
|
85
|
+
return !Number.isNaN(v) && v > 0 ? v : keep;
|
|
86
|
+
}
|
|
87
|
+
if (decl.endsWith('pt')) {
|
|
88
|
+
const v = Number.parseFloat(decl.slice(0, -2));
|
|
89
|
+
if (!Number.isNaN(v) && v > 0) return v / 0.75;
|
|
90
|
+
}
|
|
91
|
+
return keep;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
/** 内核标记键 → docx form 字段(字段名是 docx 方言:等宽在这叫 mono;HTML 标签别名集合走内核唯一表 TAG_TO_MARK) */
|
|
95
|
+
const FORM_FIELD_OF_MARK = { bold: 'bold', italic: 'italic', underline: 'underline', strike: 'strike', code: 'mono', superscript: 'superscript', subscript: 'subscript' };
|
|
96
|
+
|
|
97
|
+
/** 把一个行内标签与它的 style 叠加到当前格式上(f 视为只读,返回新副本) */
|
|
98
|
+
const inlineForm = (el, f) => {
|
|
99
|
+
const next = { ...f };
|
|
100
|
+
const tag = tagOf(el);
|
|
101
|
+
const mark = TAG_TO_MARK[tag.toUpperCase()];
|
|
102
|
+
if (mark) next[FORM_FIELD_OF_MARK[mark]] = true;
|
|
103
|
+
// <del> 在正文 HTML 里就是删除线(修订语义由 doc-* 类承载),单独并入 strike,不进内核别名表
|
|
104
|
+
else if (tag === 'del') next.strike = true;
|
|
105
|
+
switch (tag) {
|
|
106
|
+
case 'a': {
|
|
107
|
+
const href = htmlAttr(el, 'href');
|
|
108
|
+
// [约束] 非白名单协议不写进 hyperlink 部件(与装载/渲染同一白名单,禁绕行)
|
|
109
|
+
if (href !== '' && isSafeHref(href)) next.link = href;
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
case 'font': {
|
|
113
|
+
const c = htmlAttr(el, 'color');
|
|
114
|
+
if (c !== '') next.color = c;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
default:
|
|
118
|
+
}
|
|
119
|
+
// 主题色引用(读入渲染带出、内核保留的 data 属性;与 style 无关,须先于 style 早退读取)
|
|
120
|
+
const themeName = htmlAttr(el, THEME_COLOR_ATTR);
|
|
121
|
+
if (themeName !== '') {
|
|
122
|
+
next.colorTheme = themeName;
|
|
123
|
+
const tint = htmlAttr(el, THEME_TINT_ATTR);
|
|
124
|
+
if (tint !== '') next.colorThemeTint = tint;
|
|
125
|
+
const shade = htmlAttr(el, THEME_SHADE_ATTR);
|
|
126
|
+
if (shade !== '') next.colorThemeShade = shade;
|
|
127
|
+
}
|
|
128
|
+
// 字体提示(歧义标点交给中文字体)与 style 无关,同样要在早退之前读
|
|
129
|
+
if (htmlAttr(el, HINT_ATTR) === 'eastAsia') next.hint = 'eastAsia';
|
|
130
|
+
const style = htmlAttr(el, 'style');
|
|
131
|
+
if (style === '') return next;
|
|
132
|
+
const color = htmlStyleProp(style, 'color');
|
|
133
|
+
if (color !== '') next.color = color;
|
|
134
|
+
const weight = htmlStyleProp(style, 'font-weight');
|
|
135
|
+
if (weight === 'bold' || weight === '700') next.bold = true;
|
|
136
|
+
if (htmlStyleProp(style, 'font-style') === 'italic') next.italic = true;
|
|
137
|
+
const deco = htmlStyleProp(style, 'text-decoration');
|
|
138
|
+
if (deco.includes('underline')) next.underline = true;
|
|
139
|
+
else if (deco.includes('line-through')) next.strike = true;
|
|
140
|
+
const fam = htmlStyleProp(style, 'font-family');
|
|
141
|
+
if (fam !== '') {
|
|
142
|
+
// 字体链拆成「拉丁名 + 东亚名」两个名字存:落盘时 ascii 与 eastAsia 各写一个,
|
|
143
|
+
// 只留链首会把中文的东亚字体抹成拉丁字体,下次打开整篇中文换字形
|
|
144
|
+
const parts = parseFontFamily(fam);
|
|
145
|
+
if (parts.latin !== '') next.font = parts.latin;
|
|
146
|
+
if (parts.ea !== '') next.fontEa = parts.ea;
|
|
147
|
+
}
|
|
148
|
+
// 字符间距(px)与字符缩放(%):读入端写的就是这两个单位,原样回填
|
|
149
|
+
const spacing = Number.parseFloat(htmlStyleProp(style, 'letter-spacing'));
|
|
150
|
+
if (Number.isFinite(spacing) && spacing !== 0) next.spacing = spacing;
|
|
151
|
+
const stretch = Number.parseFloat(htmlStyleProp(style, 'font-stretch'));
|
|
152
|
+
if (Number.isFinite(stretch) && stretch >= 1 && stretch <= 600 && stretch !== 100) next.scale = stretch;
|
|
153
|
+
const bg = htmlStyleProp(style, 'background-color');
|
|
154
|
+
if (bg !== '') next.bg = bg;
|
|
155
|
+
next.size = fontSizePx(htmlStyleProp(style, 'font-size'), next.size);
|
|
156
|
+
return next;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
/** 缩进声明(px / em)→ twips;em 按 16px 基准折算(与内核缩进级数换算同口径) */
|
|
160
|
+
const indentTwips = (decl) => {
|
|
161
|
+
const v = Number.parseFloat(decl);
|
|
162
|
+
if (Number.isNaN(v) || v <= 0) return 0;
|
|
163
|
+
return pxToTwips(String(decl).trim().endsWith('em') ? v * 16 : v);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 段落属性。indTwips 非 0 时优先(引用块用它),否则取 HTML 的 padding-left(缩进载体,与内核缩进命令同口径)
|
|
168
|
+
* [坑] OOXML 的 pPr 子元素有严格顺序(pStyle → spacing → ind → jc),Word 对乱序直接判文件损坏,
|
|
169
|
+
* 所以这里只攒字段、由序列化端按固定顺序输出
|
|
170
|
+
*/
|
|
171
|
+
const paraProps = (el, style, indTwips) => {
|
|
172
|
+
const p = makePara({ style });
|
|
173
|
+
const st = htmlAttr(el, 'style');
|
|
174
|
+
const lh = htmlStyleProp(st, 'line-height').trim();
|
|
175
|
+
if (lh !== '') {
|
|
176
|
+
const v = Number.parseFloat(lh);
|
|
177
|
+
// 固定行距(px,来自导入文档的 exact/atLeast)落 lineExact;倍数落 line(写出时折算成 Word 的 auto 行距)
|
|
178
|
+
if (lh.endsWith('px')) {
|
|
179
|
+
if (v > 0) p.lineExact = pxToTwips(v);
|
|
180
|
+
} else if (!Number.isNaN(v) && v > 0.5 && v < 10) {
|
|
181
|
+
p.line = Math.round(v * 240);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (indTwips <= 0) indTwips = indentTwips(htmlStyleProp(st, 'padding-left'));
|
|
185
|
+
p.indent = indTwips;
|
|
186
|
+
// 首行缩进(正)/ 悬挂(负):与 w:ind 的 firstLine / hanging 对应
|
|
187
|
+
// 编辑器写的是 em(text-indent: 2em),单位必须和 padding-left 走同一套折算,否则 2em 会当成 2px
|
|
188
|
+
const tiRaw = String(htmlStyleProp(st, 'text-indent')).trim();
|
|
189
|
+
const ti = indentTwips(tiRaw.replace(/^-/, ''));
|
|
190
|
+
if (ti > 0) {
|
|
191
|
+
if (tiRaw.startsWith('-')) p.hanging = ti;
|
|
192
|
+
else p.firstLine = ti;
|
|
193
|
+
}
|
|
194
|
+
// 段前/段后(读入侧把 w:spacing 的 before/after 渲染成 margin 的近似显示,这里按值还原)
|
|
195
|
+
const mt = Number.parseFloat(htmlStyleProp(st, 'margin-top'));
|
|
196
|
+
if (!Number.isNaN(mt) && mt > 0.5) p.before = pxToTwips(mt);
|
|
197
|
+
const mb = Number.parseFloat(htmlStyleProp(st, 'margin-bottom'));
|
|
198
|
+
if (!Number.isNaN(mb) && mb > 0.5) p.after = pxToTwips(mb);
|
|
199
|
+
// 自定义制表位定义(读入编码在 data 属性上,原样还原)
|
|
200
|
+
const tabsRaw = htmlAttr(el, TABS_ATTR);
|
|
201
|
+
if (tabsRaw !== '') {
|
|
202
|
+
const tabs = decodeTabs(tabsRaw);
|
|
203
|
+
if (tabs.length) p.tabs = tabs;
|
|
204
|
+
}
|
|
205
|
+
// 分页控制(读入端放在类上)
|
|
206
|
+
if (hasClass(el, DOC_CLASS.KEEP_NEXT)) p.keepNext = true;
|
|
207
|
+
if (hasClass(el, DOC_CLASS.KEEP_LINES)) p.keepLines = true;
|
|
208
|
+
if (hasClass(el, DOC_CLASS.PBB)) p.pageBreakBefore = true;
|
|
209
|
+
if (hasClass(el, DOC_CLASS.WIDOW_OFF)) p.widowOff = true;
|
|
210
|
+
switch (htmlStyleProp(st, 'text-align')) {
|
|
211
|
+
case 'center':
|
|
212
|
+
p.align = 'center';
|
|
213
|
+
break;
|
|
214
|
+
case 'right':
|
|
215
|
+
p.align = 'right';
|
|
216
|
+
break;
|
|
217
|
+
case 'justify':
|
|
218
|
+
// 末行也拉开 = Word 的分散对齐(distribute);只有一行时 both 不会拉开,两者不能合并
|
|
219
|
+
p.align = htmlStyleProp(st, 'text-align-last') === 'justify' ? 'distribute' : 'justify';
|
|
220
|
+
break;
|
|
221
|
+
default:
|
|
222
|
+
}
|
|
223
|
+
// 段落边框(只有边框没有文字的就是一条分隔线):写出端按 pBdr 原样落盘
|
|
224
|
+
const borders = bordersOfStyle(st);
|
|
225
|
+
if (borders) p.borders = borders;
|
|
226
|
+
return p;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/** 用原始宽高比补出缺失的那一边;没有原始尺寸时用 fallback 比例 */
|
|
230
|
+
const scaleSide = (known, num, den, fallback) => (num > 0 && den > 0 ? (known * num) / den : known * fallback);
|
|
231
|
+
|
|
232
|
+
/** 读图片的 width/height(属性优先,其次 style) */
|
|
233
|
+
const imageAttrPx = (el, name) => {
|
|
234
|
+
const v = htmlAttr(el, name);
|
|
235
|
+
if (v !== '') {
|
|
236
|
+
const px = Number.parseFloat(v.replace(/px$/, ''));
|
|
237
|
+
if (!Number.isNaN(px) && px > 0) return px;
|
|
238
|
+
}
|
|
239
|
+
const s = htmlStyleProp(htmlAttr(el, 'style'), name);
|
|
240
|
+
if (s.endsWith('px')) {
|
|
241
|
+
const px = Number.parseFloat(s.slice(0, -2));
|
|
242
|
+
if (!Number.isNaN(px) && px > 0) return px;
|
|
243
|
+
}
|
|
244
|
+
return 0;
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
/** 两份行内格式是否完全一致(都是标量字段,逐键比即可) */
|
|
248
|
+
const sameForm = (a, b) => {
|
|
249
|
+
const keys = Object.keys(a);
|
|
250
|
+
if (keys.length !== Object.keys(b).length) return false;
|
|
251
|
+
return keys.every((k) => a[k] === b[k]);
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
/** 段落的行内内容收集器;行内语境里冒出块级标签时先落地当前段 */
|
|
255
|
+
class RunSink {
|
|
256
|
+
constructor(w, p) {
|
|
257
|
+
this.w = w;
|
|
258
|
+
this.p = p;
|
|
259
|
+
this.runs = [];
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
add(r) {
|
|
263
|
+
// 相邻同格式文字合并回一个 run:标点挤压/肤色壳会把一段文字切成很多文本节点,
|
|
264
|
+
// 不合并的话保存一次 run 数就翻倍(258 → 600+),文件变大、Word 里按 run 选中也变碎
|
|
265
|
+
const last = this.runs[this.runs.length - 1];
|
|
266
|
+
if (last && r.mark === MARK.TEXT && last.mark === MARK.TEXT && sameForm(last.form, r.form)) {
|
|
267
|
+
last.text += r.text;
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
this.runs.push(r);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
flush() {
|
|
274
|
+
this.w.push({ ...this.p, runs: this.runs });
|
|
275
|
+
this.runs = [];
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
breakOut(el, f) {
|
|
279
|
+
if (this.runs.length > 0) this.flush();
|
|
280
|
+
this.w.block(el, f);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** htmlWalker 遍历正文 HTML 并产出块序列 */
|
|
285
|
+
class HtmlWalker {
|
|
286
|
+
constructor({ cmtIds, textWpx }) {
|
|
287
|
+
this.blocks = [];
|
|
288
|
+
this.cmtIds = cmtIds;
|
|
289
|
+
this.textWpx = textWpx;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** 输出一层的子节点(游离文本按一个段落收) */
|
|
293
|
+
walkChildren(el, f) {
|
|
294
|
+
for (const c of el.childNodes) {
|
|
295
|
+
if (c.nodeType === Node.TEXT_NODE) {
|
|
296
|
+
const t = normalizeText(c.data);
|
|
297
|
+
if (t !== '') this.push({ ...makePara(), runs: [textRun(t, { ...f })] });
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
if (c.nodeType !== Node.ELEMENT_NODE) continue;
|
|
301
|
+
const from = this.blocks.length;
|
|
302
|
+
this.block(c, f);
|
|
303
|
+
// 跨栏标记跟着这个元素产出的所有块走(多栏文档里整块标题行要自成单栏节)
|
|
304
|
+
if (hasClass(c, DOC_CLASS.SPAN_ALL)) {
|
|
305
|
+
for (let i = from; i < this.blocks.length; i += 1) this.blocks[i].spanAll = true;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/* ---------- 块级 ---------- */
|
|
311
|
+
|
|
312
|
+
block(el, f) {
|
|
313
|
+
if (hasClass(el, DOC_CLASS.PAGEBREAK)) {
|
|
314
|
+
this.push({ ...makePara(), runs: [makeRun({ mark: MARK.PAGE })] });
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (hasClass(el, DOC_CLASS.SECTIONBREAK)) {
|
|
318
|
+
this.blocks.push(secBreakBlock());
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (hasClass(el, DOC_CLASS.TOC)) {
|
|
322
|
+
// 目录块:缓存行收成内部块序列,写出端包回 TOC 域幕
|
|
323
|
+
const inner = new HtmlWalker({ cmtIds: this.cmtIds, textWpx: this.textWpx });
|
|
324
|
+
inner.walkChildren(el, f);
|
|
325
|
+
this.blocks.push(tocBlock(inner.blocks));
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
switch (tagOf(el)) {
|
|
329
|
+
case 'p':
|
|
330
|
+
case 'div':
|
|
331
|
+
this.paragraph(el, f, paraProps(el, '', 0));
|
|
332
|
+
break;
|
|
333
|
+
case 'h1':
|
|
334
|
+
case 'h2':
|
|
335
|
+
case 'h3':
|
|
336
|
+
case 'h4':
|
|
337
|
+
case 'h5':
|
|
338
|
+
case 'h6': {
|
|
339
|
+
let style = `Heading${tagOf(el)[1]}`;
|
|
340
|
+
if (hasClass(el, DOC_CLASS.TITLE)) style = 'Title';
|
|
341
|
+
this.paragraph(el, f, paraProps(el, style, 0));
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
case 'blockquote':
|
|
345
|
+
this.quote(el, f);
|
|
346
|
+
break;
|
|
347
|
+
case 'pre':
|
|
348
|
+
this.codeBlock(el, f);
|
|
349
|
+
break;
|
|
350
|
+
case 'ul':
|
|
351
|
+
this.list(el, f, 0, false);
|
|
352
|
+
break;
|
|
353
|
+
case 'ol':
|
|
354
|
+
this.list(el, f, 0, true);
|
|
355
|
+
break;
|
|
356
|
+
case 'table':
|
|
357
|
+
this.tableBlock(el, f);
|
|
358
|
+
break;
|
|
359
|
+
case 'hr':
|
|
360
|
+
this.push(rulePara(htmlAttr(el, 'style')));
|
|
361
|
+
break;
|
|
362
|
+
default:
|
|
363
|
+
this.paragraph(el, f, paraProps(el, '', 0));
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** 一个段落属性的容器:装块级内容就往下拆,否则整段收成行内 run */
|
|
368
|
+
paragraph(el, f, p) {
|
|
369
|
+
if (hasBlockChild(el)) {
|
|
370
|
+
this.walkChildren(el, f);
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
this.content(el, f, p);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** 把 el 的子节点收成一个段落(即使空也落地:编辑器里的空行也是内容) */
|
|
377
|
+
content(el, f, p) {
|
|
378
|
+
const sink = new RunSink(this, p);
|
|
379
|
+
this.inline(el, f, sink, false);
|
|
380
|
+
sink.flush();
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** 引用块:缩进是 Word 侧的视觉载体,pStyle=Quote 供回读端精确识别。
|
|
384
|
+
* [坑] 编辑器产出的 blockquote 里包着 <p>,不能先按「有无块级子元素」整块 walkChildren——
|
|
385
|
+
* 那样内层段落拿不到引用缩进,回读就成了普通段(引用块丢失)。这里逐个子块套引用属性。 */
|
|
386
|
+
quote(el, f) {
|
|
387
|
+
let pushed = false;
|
|
388
|
+
for (const c of el.childNodes) {
|
|
389
|
+
if (c.nodeType === Node.TEXT_NODE) {
|
|
390
|
+
const t = normalizeText(c.data);
|
|
391
|
+
if (t !== '') {
|
|
392
|
+
this.push({ ...paraProps(el, 'Quote', QUOTE_INDENT_TWIPS), runs: [textRun(t, { ...f })] });
|
|
393
|
+
pushed = true;
|
|
394
|
+
}
|
|
395
|
+
continue;
|
|
396
|
+
}
|
|
397
|
+
if (c.nodeType !== Node.ELEMENT_NODE) continue;
|
|
398
|
+
const tag = tagOf(c);
|
|
399
|
+
if (tag === 'ul' || tag === 'ol') this.list(c, f, 0, tag === 'ol');
|
|
400
|
+
else if (tag === 'pre') this.codeBlock(c, f);
|
|
401
|
+
else if (tag === 'table') this.tableBlock(c, f);
|
|
402
|
+
else if (tag === 'blockquote') this.quote(c, f);
|
|
403
|
+
else if (hasBlockChild(c)) this.walkChildren(c, f);
|
|
404
|
+
else this.content(c, f, paraProps(c, 'Quote', QUOTE_INDENT_TWIPS));
|
|
405
|
+
pushed = true;
|
|
406
|
+
}
|
|
407
|
+
if (!pushed) this.push({ ...paraProps(el, 'Quote', QUOTE_INDENT_TWIPS), runs: [] });
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** 代码块:一行一个段落,行内内容取纯文本(灰底 + 等宽由回读端识别) */
|
|
411
|
+
codeBlock(el, f) {
|
|
412
|
+
const mono = { ...f, mono: true };
|
|
413
|
+
for (const line of textContent(el).replace(/\n+$/, '').split('\n')) {
|
|
414
|
+
this.push({ ...makePara({ shade: CODE_FILL }), runs: [textRun(line, mono)] });
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** 列表;level 超过 3 后不再下潜(numbering.xml 只定义到 ilvl 3) */
|
|
419
|
+
list(el, f, level, ordered) {
|
|
420
|
+
if (level > 3) level = 3;
|
|
421
|
+
const isTask = htmlAttr(el, 'data-type') === 'taskList';
|
|
422
|
+
for (const c of el.children) {
|
|
423
|
+
if (tagOf(c) !== 'li') continue;
|
|
424
|
+
// 待办列表用两份带勾选字形的编号(numId 3/4)承载勾选态:正文保持纯文字,
|
|
425
|
+
// 不把 ☑/☐ 写进 run,否则往返会多出字符且再存一次就变成普通项目符号
|
|
426
|
+
const checked = isTask
|
|
427
|
+
&& (htmlAttr(c, 'data-checked').toLowerCase() === 'true' || Boolean(c.querySelector('input[type="checkbox"]')?.checked));
|
|
428
|
+
const p = makePara({ num: numRef(level, ordered, isTask ? (checked ? 'checked' : 'unchecked') : '') });
|
|
429
|
+
const sink = new RunSink(this, p);
|
|
430
|
+
// [坑] <li> 里的嵌套列表必须留给自己递归(下面那段),所以这里 skipList 掉:
|
|
431
|
+
// 两个地方都走一遍会把同一批子项输出两遍,且其中一遍落在错误的层级上
|
|
432
|
+
if (isTask) {
|
|
433
|
+
// 待办正文在 div 里,按行内收集;不能当普通 li 走行内收集,否则 div 会被当成新块另起一段
|
|
434
|
+
const content = Array.from(c.children).find((x) => tagOf(x) === 'div');
|
|
435
|
+
this.inline(content || c, f, sink, true);
|
|
436
|
+
} else {
|
|
437
|
+
this.inline(c, f, sink, true);
|
|
438
|
+
}
|
|
439
|
+
sink.flush();
|
|
440
|
+
for (const gc of c.children) {
|
|
441
|
+
const t = tagOf(gc);
|
|
442
|
+
if (t === 'ul') this.list(gc, f, level + 1, false);
|
|
443
|
+
else if (t === 'ol') this.list(gc, f, level + 1, true);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/* ---------- 行内 ---------- */
|
|
449
|
+
|
|
450
|
+
inline(el, f, sink, skipList) {
|
|
451
|
+
for (const c of el.childNodes) {
|
|
452
|
+
if (c.nodeType === Node.TEXT_NODE) {
|
|
453
|
+
const t = normalizeText(c.data);
|
|
454
|
+
if (t !== '') sink.add(textRun(t, { ...f }));
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (c.nodeType !== Node.ELEMENT_NODE) continue;
|
|
458
|
+
const tag = tagOf(c);
|
|
459
|
+
switch (tag) {
|
|
460
|
+
case 'br':
|
|
461
|
+
sink.add(makeRun({ mark: MARK.BREAK }));
|
|
462
|
+
break;
|
|
463
|
+
case 'img': {
|
|
464
|
+
const ref = this.imageRef(c);
|
|
465
|
+
if (ref) sink.add(makeRun({ mark: MARK.IMAGE, img: ref }));
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
case 'ins':
|
|
469
|
+
case 'del': {
|
|
470
|
+
const inner = { ...f, del: tag === 'del' };
|
|
471
|
+
sink.add(makeRun({ mark: MARK.REV_OPEN, rev: tag, author: htmlAttr(c, 'data-author'), date: htmlAttr(c, 'data-date') }));
|
|
472
|
+
this.inline(c, inner, sink, skipList);
|
|
473
|
+
sink.add(makeRun({ mark: MARK.REV_CLOSE, rev: tag }));
|
|
474
|
+
break;
|
|
475
|
+
}
|
|
476
|
+
case 'sup': {
|
|
477
|
+
// 脚注/尾注锚点(read 侧渲染的 <sup class="doc-footnote|doc-endnote" data-fn>);
|
|
478
|
+
// 序号文字不进模型——写出端由 Word 自动编号,模型里只留定位
|
|
479
|
+
if (hasClass(c, DOC_CLASS.FOOTNOTE)) {
|
|
480
|
+
sink.add(makeRun({ mark: MARK.NOTE, note: htmlAttr(c, NOTE_ATTR) }));
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
if (hasClass(c, DOC_CLASS.ENDNOTE)) {
|
|
484
|
+
sink.add(makeRun({ mark: MARK.NOTE, note: htmlAttr(c, NOTE_ATTR), end: true }));
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
this.inline(c, inlineForm(c, f), sink, skipList);
|
|
488
|
+
break;
|
|
489
|
+
}
|
|
490
|
+
case 'span':
|
|
491
|
+
if (hasClass(c, DOC_CLASS.COMMENT)) {
|
|
492
|
+
sink.add(makeRun({ mark: MARK.CMT_OPEN, cmt: this.commentId(htmlAttr(c, COMMENT_ATTR)) }));
|
|
493
|
+
this.inline(c, f, sink, skipList);
|
|
494
|
+
sink.add(makeRun({ mark: MARK.CMT_CLOSE }));
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
// 歧义标点的字体壳只用于显示(内联 font-family 由 run 的 w:hint 推出),样式不吃它的:
|
|
498
|
+
// 吃进去会在 w:spacing/字体上凭空多出一个 run,往返越存越碎
|
|
499
|
+
if (hasClass(c, DOC_CLASS.EA)) {
|
|
500
|
+
this.inline(c, f, sink, skipList);
|
|
501
|
+
break;
|
|
502
|
+
}
|
|
503
|
+
this.inline(c, inlineForm(c, f), sink, skipList);
|
|
504
|
+
break;
|
|
505
|
+
default:
|
|
506
|
+
if (INLINE_BREAKERS.has(tag)) {
|
|
507
|
+
if (tag === 'ul' || tag === 'ol') {
|
|
508
|
+
if (skipList) continue;
|
|
509
|
+
}
|
|
510
|
+
sink.breakOut(c, f);
|
|
511
|
+
continue;
|
|
512
|
+
}
|
|
513
|
+
this.inline(c, inlineForm(c, f), sink, skipList);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
/** 批注键 → 数字 id(首次出现时分配) */
|
|
519
|
+
commentId(key) {
|
|
520
|
+
if (this.cmtIds.has(key)) return this.cmtIds.get(key);
|
|
521
|
+
const id = this.cmtIds.size;
|
|
522
|
+
this.cmtIds.set(key, id);
|
|
523
|
+
return id;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/* ---------- 图片 ---------- */
|
|
527
|
+
|
|
528
|
+
/** 内嵌图片的尺寸以原始像素为宽高比的唯一真相(编辑器缩放时只写 width,height:auto) */
|
|
529
|
+
imageRef(el) {
|
|
530
|
+
const decoded = decodeDataUrl(htmlAttr(el, 'src'));
|
|
531
|
+
if (!decoded) {
|
|
532
|
+
// [设计] 非内嵌图片直接跳过:写一个指向包外的引用,Word 打开必然是空图
|
|
533
|
+
return null;
|
|
534
|
+
}
|
|
535
|
+
// [坑] 这里用浏览器已加载图片的 naturalWidth/Height(源码取原始像素):脱离文档的图片拿不到,
|
|
536
|
+
// 落到 width/height 属性与 400×300 兜底,与 Go 版「探测不出原始尺寸」的退化口径一致
|
|
537
|
+
const natW = el.naturalWidth || 0;
|
|
538
|
+
const natH = el.naturalHeight || 0;
|
|
539
|
+
let wPx = imageAttrPx(el, 'width');
|
|
540
|
+
let hPx = imageAttrPx(el, 'height');
|
|
541
|
+
if (wPx > 0 && hPx <= 0) hPx = scaleSide(wPx, natW, natH, 3 / 4);
|
|
542
|
+
else if (hPx > 0 && wPx <= 0) wPx = scaleSide(hPx, natH, natW, 4 / 3);
|
|
543
|
+
else if (wPx <= 0 && hPx <= 0) {
|
|
544
|
+
if (natW > 0 && natH > 0) {
|
|
545
|
+
wPx = natW;
|
|
546
|
+
hPx = natH;
|
|
547
|
+
} else {
|
|
548
|
+
wPx = 400;
|
|
549
|
+
hPx = 300;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
// 图片不许超出正文栏宽,否则 Word 里会被裁掉
|
|
553
|
+
if (wPx > this.textWpx) {
|
|
554
|
+
hPx = (hPx * this.textWpx) / wPx;
|
|
555
|
+
wPx = this.textWpx;
|
|
556
|
+
}
|
|
557
|
+
return makeImageRef({ data: decoded.data, ext: decoded.ext, w: wPx, h: hPx });
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
/* ---------- 表格 ---------- */
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* HTML 表格 → 网格模型。纵向合并在这里就展开成占位格,
|
|
564
|
+
* 序列化端只负责平铺(OOXML 的 vMerge 本来就是「首格 restart + 后续行空 tc 续」的形状)
|
|
565
|
+
*/
|
|
566
|
+
tableBlock(el, f) {
|
|
567
|
+
const cols = gridCols(el);
|
|
568
|
+
const pcts = tableColPercents(el, cols);
|
|
569
|
+
// rowHeights 与 rows 平行(行高是行级属性,模型的行仍是格子数组):0 = 未声明
|
|
570
|
+
const t = { widthPct: tableWidthPct(el, this.textWpx), cols: pcts, rows: [], rowHeights: [] };
|
|
571
|
+
// 表样式摊平后的外观编码(读入渲染带上来的 data 属性;白名单校验后原样还原)
|
|
572
|
+
const bdr = htmlAttr(el, TBL_BDR_ATTR);
|
|
573
|
+
if (/^(none|single:\d+:[0-9a-f]{6})$/i.test(bdr)) t.bdr = bdr.toLowerCase();
|
|
574
|
+
const mar = htmlAttr(el, TBL_MAR_ATTR);
|
|
575
|
+
if (/^\d+:\d+:\d+:\d+$/.test(mar)) t.mar = mar;
|
|
576
|
+
const cover = new Array(cols).fill(0); // 各网格列还需要往下续几行
|
|
577
|
+
walkRows(el, (tr) => {
|
|
578
|
+
const row = [];
|
|
579
|
+
let ci = 0;
|
|
580
|
+
const fillCont = () => {
|
|
581
|
+
while (ci < cols && cover[ci] > 0) {
|
|
582
|
+
row.push(makeCell({ cont: true }));
|
|
583
|
+
cover[ci] -= 1;
|
|
584
|
+
ci += 1;
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
for (const td of tr.children) {
|
|
588
|
+
const tag = tagOf(td);
|
|
589
|
+
if (tag !== 'td' && tag !== 'th') continue;
|
|
590
|
+
fillCont();
|
|
591
|
+
if (ci >= cols) break;
|
|
592
|
+
const span = clampAttr(td, 'colspan', 1, cols - ci);
|
|
593
|
+
const rowSpan = attrInt(td, 'rowspan', 1);
|
|
594
|
+
cover[ci] = rowSpan > 1 ? rowSpan - 1 : 0;
|
|
595
|
+
let width = 0;
|
|
596
|
+
for (let k = 0; k < span; k += 1) width += pcts[ci + k];
|
|
597
|
+
row.push(this.tableCell(td, f, width, span, rowSpan > 1));
|
|
598
|
+
ci += span;
|
|
599
|
+
}
|
|
600
|
+
fillCont();
|
|
601
|
+
t.rows.push(row);
|
|
602
|
+
t.rowHeights.push(rowHeightPx(tr));
|
|
603
|
+
});
|
|
604
|
+
this.pushTable(t);
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
tableCell(td, f, width, span, restart) {
|
|
608
|
+
const cf = { ...f };
|
|
609
|
+
const style = htmlAttr(td, 'style');
|
|
610
|
+
if (tagOf(td) === 'th') cf.bold = true;
|
|
611
|
+
const fw = htmlStyleProp(style, 'font-weight');
|
|
612
|
+
if (fw === '700' || fw === 'bold') cf.bold = true;
|
|
613
|
+
const inner = new HtmlWalker({ cmtIds: this.cmtIds, textWpx: this.textWpx });
|
|
614
|
+
if (hasBlockChild(td)) inner.walkChildren(td, cf);
|
|
615
|
+
else inner.content(td, cf, makePara());
|
|
616
|
+
return makeCell({
|
|
617
|
+
span,
|
|
618
|
+
restart,
|
|
619
|
+
shade: hexColor(htmlStyleProp(style, 'background-color'), ''),
|
|
620
|
+
vAlign: cellVAlign(style),
|
|
621
|
+
width,
|
|
622
|
+
blocks: inner.blocks
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
push(p) {
|
|
627
|
+
this.blocks.push(paraBlock(p));
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
pushTable(t) {
|
|
631
|
+
this.blocks.push(tableBlock(t));
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
/** 按 tr 顺序遍历(thead/tbody/tfoot 只是分组,不改变顺序) */
|
|
636
|
+
const walkRows = (el, fn) => {
|
|
637
|
+
for (const c of el.children) {
|
|
638
|
+
switch (tagOf(c)) {
|
|
639
|
+
case 'thead':
|
|
640
|
+
case 'tbody':
|
|
641
|
+
case 'tfoot':
|
|
642
|
+
walkRows(c, fn);
|
|
643
|
+
break;
|
|
644
|
+
case 'tr':
|
|
645
|
+
fn(c);
|
|
646
|
+
break;
|
|
647
|
+
default:
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
};
|
|
651
|
+
|
|
652
|
+
/** td 的垂直对齐:HTML 的 middle/bottom → Word 的 center/bottom(默认 top 与编辑器 CSS 一致,不必回写) */
|
|
653
|
+
const cellVAlign = (style) => {
|
|
654
|
+
const v = htmlStyleProp(style || '', 'vertical-align').trim();
|
|
655
|
+
if (v === 'middle' || v === 'center') return 'center';
|
|
656
|
+
if (v === 'bottom') return 'bottom';
|
|
657
|
+
return '';
|
|
658
|
+
};
|
|
659
|
+
|
|
660
|
+
/** 行高(tr 上的 height,px);0 = 未声明 */
|
|
661
|
+
const rowHeightPx = (tr) => {
|
|
662
|
+
const h = Number.parseFloat(htmlStyleProp(htmlAttr(tr, 'style'), 'height'));
|
|
663
|
+
return Number.isFinite(h) && h > 0 ? Math.round(h * 100) / 100 : 0;
|
|
664
|
+
};
|
|
665
|
+
|
|
666
|
+
/** 网格列数 = 各行 colspan 之和的最大值(tblGrid 的 gridCol 数必须与它一致) */const gridCols = (tbl) => {
|
|
667
|
+
let cols = 0;
|
|
668
|
+
walkRows(tbl, (tr) => {
|
|
669
|
+
let sum = 0;
|
|
670
|
+
for (const td of tr.children) {
|
|
671
|
+
const tag = tagOf(td);
|
|
672
|
+
if (tag === 'td' || tag === 'th') sum += attrInt(td, 'colspan', 1);
|
|
673
|
+
}
|
|
674
|
+
if (sum > cols) cols = sum;
|
|
675
|
+
});
|
|
676
|
+
return cols === 0 ? 1 : cols;
|
|
677
|
+
};
|
|
678
|
+
|
|
679
|
+
/** 取 colgroup 的列宽(百分比或像素都收)并归一到 100;缺省或有一个坏值就等分 */
|
|
680
|
+
const tableColPercents = (tbl, cols) => {
|
|
681
|
+
const out = new Array(cols).fill(0);
|
|
682
|
+
let got = 0;
|
|
683
|
+
for (const cg of tbl.children) {
|
|
684
|
+
if (got >= cols) break;
|
|
685
|
+
if (tagOf(cg) !== 'colgroup') continue;
|
|
686
|
+
for (const col of cg.children) {
|
|
687
|
+
if (got >= cols) break;
|
|
688
|
+
if (tagOf(col) !== 'col') continue;
|
|
689
|
+
const decl = htmlStyleProp(htmlAttr(col, 'style'), 'width').trim();
|
|
690
|
+
const num = decl.replace(/%$/, '').replace(/px$/, '');
|
|
691
|
+
// 有一个取不到宽度的列:整套宽度都不可信
|
|
692
|
+
if (num === decl || num === '') break;
|
|
693
|
+
const v = Number.parseFloat(num);
|
|
694
|
+
if (Number.isNaN(v) || v <= 0) break;
|
|
695
|
+
out[got] = v; // 单位无所谓,下面按总和归一
|
|
696
|
+
got += 1;
|
|
697
|
+
}
|
|
698
|
+
break;
|
|
699
|
+
}
|
|
700
|
+
if (got !== cols) return out.fill(100 / cols);
|
|
701
|
+
const sum = out.reduce((a, b) => a + b, 0);
|
|
702
|
+
if (sum > 0) {
|
|
703
|
+
for (let i = 0; i < cols; i += 1) out[i] = (out[i] * 100) / sum;
|
|
704
|
+
}
|
|
705
|
+
return out;
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
/** 表格自身宽度占正文栏宽的百分比(编辑器改过宽度就取内联值,否则满宽) */
|
|
709
|
+
const tableWidthPct = (tbl, textWpx) => {
|
|
710
|
+
const decl = htmlStyleProp(htmlAttr(tbl, 'style'), 'width').trim();
|
|
711
|
+
if (decl.endsWith('%')) {
|
|
712
|
+
const v = Number.parseFloat(decl.slice(0, -1));
|
|
713
|
+
if (!Number.isNaN(v) && v > 1) return Math.min(v, 100);
|
|
714
|
+
}
|
|
715
|
+
if (decl.endsWith('px') && textWpx > 0) {
|
|
716
|
+
const v = Number.parseFloat(decl.slice(0, -2));
|
|
717
|
+
if (!Number.isNaN(v) && v > 10) return Math.min((v * 100) / textWpx, 100);
|
|
718
|
+
}
|
|
719
|
+
return 100;
|
|
720
|
+
};
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* 正文 HTML → 块序列 + 批注键到数字 id 的映射(数字 id 按正文出现顺序分配)
|
|
724
|
+
* @param {string|Element} input HTML 串(走 DOMParser)或现成的正文根元素(只读遍历)
|
|
725
|
+
* @param {object} [page] 页面设置(图片/表格宽度的换算基准;缺省按 A4 默认页)
|
|
726
|
+
* @returns {{blocks: Array, comments: Map<string, number>}}
|
|
727
|
+
*/
|
|
728
|
+
export function htmlToBlocks(input, page) {
|
|
729
|
+
let root;
|
|
730
|
+
if (typeof input === 'string') {
|
|
731
|
+
root = new DOMParser().parseFromString(`<body>${input}</body>`, 'text/html').body;
|
|
732
|
+
} else {
|
|
733
|
+
root = input;
|
|
734
|
+
}
|
|
735
|
+
if (!root) return { blocks: [], comments: new Map() };
|
|
736
|
+
const w = new HtmlWalker({ cmtIds: new Map(), textWpx: textWidthMM(page) * PX_PER_MM });
|
|
737
|
+
w.walkChildren(root, makeForm());
|
|
738
|
+
return { blocks: w.blocks, comments: w.cmtIds };
|
|
739
|
+
}
|