gbpptx 0.6.0 → 0.6.2
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 -21
- package/README.md +253 -253
- package/package.json +48 -47
- package/src/index.js +29 -29
- package/src/kernel/model.js +651 -651
- package/src/kernel/parts.js +173 -173
- package/src/kernel/pptx.js +72 -72
- package/src/kernel/print.js +76 -76
- package/src/kernel/read.js +1018 -1018
- package/src/kernel/roundtrip.js +174 -174
- package/src/kernel/text.js +278 -278
- package/src/kernel/write.js +385 -385
- package/web/app/main.js +8 -8
- package/web/app/slides/arrange.js +101 -101
- package/web/app/slides/clipboard.js +93 -93
- package/web/app/slides/file.js +240 -240
- package/web/app/slides/find.js +201 -201
- package/web/app/slides/history.js +100 -100
- package/web/app/slides/keys.js +187 -187
- package/web/app/slides/menus.js +105 -105
- package/web/app/slides/objects.js +254 -254
- package/web/app/slides/render.js +183 -183
- package/web/app/slides/shared.js +85 -85
- package/web/app/slides/slides.js +103 -103
- package/web/app/slides/table.js +102 -102
- package/web/app/slides/text.js +418 -418
- package/web/app/slides/view.js +182 -182
- package/web/app/slides-app.js +354 -354
- package/web/app/ui/canvas.js +769 -769
- package/web/app/ui/color-panel.js +58 -58
- package/web/app/ui/dialogs.js +120 -120
- package/web/app/ui/file-io.js +116 -116
- package/web/app/ui/format-pane.js +427 -427
- package/web/app/ui/icons.js +241 -133
- package/web/app/ui/menu.js +77 -75
- package/web/app/ui/notify.js +45 -45
- package/web/app/ui/page-view.js +221 -221
- package/web/app/ui/popup.js +80 -80
- package/web/app/ui/present.js +292 -292
- package/web/app/ui/ribbon.js +855 -855
- package/web/app/ui/slide-pane.js +197 -197
- package/web/app/ui/statusbar.js +93 -93
- package/web/app/ui/text-styles.js +84 -84
- package/web/app/ui/titlebar.js +253 -250
- package/web/app/ui/transition.js +30 -30
- package/web/index.html +30 -29
- package/web/serve.mjs +26 -23
- package/web/styles/office.css +463 -463
- package/web/styles/slides.css +97 -97
- package/web/styles/tokens.css +154 -154
package/src/kernel/text.js
CHANGED
|
@@ -1,278 +1,278 @@
|
|
|
1
|
-
// [文件] gbpptx·正文转换 —— 文本框 HTML 子集 ⇄ 段落/run(块级摊平成段落,列表保留符号语义)
|
|
2
|
-
// [设计] HTML 只认 contenteditable 会产出的那一小撮标签(div/p/br/b/i/u/s/span/font/ul/ol/li):
|
|
3
|
-
// 块级摊平成段落,行内标记落到 run,列表落到段落的 list/level 上,写出端再变回真正的
|
|
4
|
-
// <a:buChar>/<a:buAutoNum>,而不是把「• 」写进文字——这样 PowerPoint 往返仍是可编辑列表。
|
|
5
|
-
|
|
6
|
-
import { htmlAttr, htmlStyleProp, normalizeColor, xmlEscape } from 'gbooxml';
|
|
7
|
-
|
|
8
|
-
/** 块级标签:独立成段 */
|
|
9
|
-
const BLOCK_TAGS = new Set(['DIV', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'BLOCKQUOTE', 'PRE']);
|
|
10
|
-
|
|
11
|
-
/** 段落/run 收集器:行内格式一变就切一个 run,块级边界就收一个段落 */
|
|
12
|
-
class ParaCollector {
|
|
13
|
-
constructor() {
|
|
14
|
-
this.paras = [];
|
|
15
|
-
this.cur = newPara();
|
|
16
|
-
this.run = null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
addText(f, s) {
|
|
20
|
-
if (s === '') return;
|
|
21
|
-
if (this.run && !sameFormat(this.run, f)) this.closeRun();
|
|
22
|
-
if (!this.run) this.run = { ...f, text: '' };
|
|
23
|
-
this.run.text += s;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
closeRun() {
|
|
27
|
-
if (this.run && this.run.text !== '') this.cur.runs.push(this.run);
|
|
28
|
-
this.run = null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
closePara(force) {
|
|
32
|
-
this.closeRun();
|
|
33
|
-
if (this.cur.runs.length > 0 || force) this.paras.push(this.cur);
|
|
34
|
-
this.cur = newPara(this.cur);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
startBlock(align, list = '', level = 0) {
|
|
38
|
-
this.closePara(false);
|
|
39
|
-
this.cur = { align, list, level, runs: [] };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** 遍历一个容器;listCtx 用于列表项内部(换行/段落继承列表身份) */
|
|
43
|
-
walk(node, f, listCtx = null) {
|
|
44
|
-
for (const child of Array.from(node.childNodes)) {
|
|
45
|
-
if (child.nodeType === Node.TEXT_NODE) {
|
|
46
|
-
// 元素内的换行是排版空白,真换行由 <br> 表达
|
|
47
|
-
this.addText(f, child.data.replace(/\n/g, ''));
|
|
48
|
-
continue;
|
|
49
|
-
}
|
|
50
|
-
if (child.nodeType !== Node.ELEMENT_NODE) continue;
|
|
51
|
-
const tag = child.tagName;
|
|
52
|
-
if (tag === 'BR') {
|
|
53
|
-
this.closePara(true);
|
|
54
|
-
if (listCtx) this.cur = newPara(this.cur, listCtx.list, listCtx.level);
|
|
55
|
-
} else if (BLOCK_TAGS.has(tag)) {
|
|
56
|
-
this.startBlock(blockAlign(child), listCtx?.list || '', listCtx?.level || 0);
|
|
57
|
-
this.walk(child, f, listCtx);
|
|
58
|
-
this.closePara(false);
|
|
59
|
-
if (listCtx) this.startBlock(blockAlign(child), listCtx.list, listCtx.level);
|
|
60
|
-
} else if (tag === 'UL' || tag === 'OL') {
|
|
61
|
-
this.closePara(false);
|
|
62
|
-
this.walkList(child, f, tag === 'OL', (listCtx?.level || -1) + 1);
|
|
63
|
-
if (listCtx) this.startBlock('', listCtx.list, listCtx.level);
|
|
64
|
-
} else if (tag === 'IMG') {
|
|
65
|
-
// 图片是独立对象,正文里的 <img> 不进 pptx
|
|
66
|
-
} else {
|
|
67
|
-
this.walk(child, inlineFormat(child, f), listCtx);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/** 列表项 → 带 list/level 的段落;嵌套列表递归到下一级 */
|
|
73
|
-
walkList(list, f, ordered, level) {
|
|
74
|
-
for (const li of Array.from(list.children)) {
|
|
75
|
-
if (li.tagName !== 'LI') continue;
|
|
76
|
-
const lvl = listLevel(li, level);
|
|
77
|
-
this.startBlock(blockAlign(li), ordered ? 'number' : 'bullet', lvl);
|
|
78
|
-
this.walk(li, f, { list: ordered ? 'number' : 'bullet', level: lvl });
|
|
79
|
-
this.closePara(false);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const newPara = (prev, list = '', level = 0) => ({ align: prev?.align || '', list, level, runs: [] });
|
|
85
|
-
|
|
86
|
-
const sameFormat = (a, b) =>
|
|
87
|
-
a.bold === b.bold && a.italic === b.italic && a.underline === b.underline &&
|
|
88
|
-
a.strike === b.strike && a.color === b.color && a.font === b.font && a.size === b.size;
|
|
89
|
-
|
|
90
|
-
/** 列表层级:嵌套层级优先,其次读 parasToHTML 写出的 margin-left */
|
|
91
|
-
function listLevel(li, fallback) {
|
|
92
|
-
const ml = htmlStyleProp(htmlAttr(li, 'style'), 'margin-left');
|
|
93
|
-
if (ml.endsWith('em')) {
|
|
94
|
-
const v = Number.parseFloat(ml);
|
|
95
|
-
if (Number.isFinite(v)) return Math.max(0, Math.min(8, Math.round(v / 1.2)));
|
|
96
|
-
}
|
|
97
|
-
if (ml.endsWith('px')) {
|
|
98
|
-
const v = Number.parseFloat(ml);
|
|
99
|
-
if (Number.isFinite(v)) return Math.max(0, Math.min(8, Math.round(v / 24)));
|
|
100
|
-
}
|
|
101
|
-
return fallback;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/** 块级元素的对齐(contenteditable 写 style,也可能直接给 align 属性) */
|
|
105
|
-
function blockAlign(el) {
|
|
106
|
-
const attr = htmlAttr(el, 'align');
|
|
107
|
-
if (attr) return attr;
|
|
108
|
-
switch (htmlStyleProp(htmlAttr(el, 'style'), 'text-align')) {
|
|
109
|
-
case 'center': return 'center';
|
|
110
|
-
case 'right': return 'right';
|
|
111
|
-
case 'justify': return 'justify';
|
|
112
|
-
default: return '';
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** 把一个行内标签与它的 style 叠加到当前格式上 */
|
|
117
|
-
function inlineFormat(el, f) {
|
|
118
|
-
const out = { ...f };
|
|
119
|
-
switch (el.tagName) {
|
|
120
|
-
case 'B': case 'STRONG': out.bold = true; break;
|
|
121
|
-
case 'I': case 'EM': out.italic = true; break;
|
|
122
|
-
case 'U': out.underline = true; break;
|
|
123
|
-
case 'S': case 'STRIKE': case 'DEL': out.strike = true; break;
|
|
124
|
-
default: break;
|
|
125
|
-
}
|
|
126
|
-
const style = htmlAttr(el, 'style');
|
|
127
|
-
if (style) {
|
|
128
|
-
const c = normalizeColor(htmlStyleProp(style, 'color'));
|
|
129
|
-
if (c !== '') out.color = c;
|
|
130
|
-
const deco = htmlStyleProp(style, 'text-decoration');
|
|
131
|
-
if (deco.includes('underline')) out.underline = true;
|
|
132
|
-
if (deco.includes('line-through')) out.strike = true;
|
|
133
|
-
const weight = htmlStyleProp(style, 'font-weight');
|
|
134
|
-
if (weight === 'bold' || Number(weight) >= 600) out.bold = true;
|
|
135
|
-
if (/italic|oblique/.test(htmlStyleProp(style, 'font-style'))) out.italic = true;
|
|
136
|
-
const fam = htmlStyleProp(style, 'font-family');
|
|
137
|
-
if (fam !== '') out.font = fam.split(',')[0].trim().replace(/^['"]|['"]$/g, '');
|
|
138
|
-
const size = htmlStyleProp(style, 'font-size');
|
|
139
|
-
if (size.endsWith('px')) {
|
|
140
|
-
const v = Number.parseFloat(size.slice(0, -2));
|
|
141
|
-
if (Number.isFinite(v) && v > 0) out.size = v;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
const colorAttr = normalizeColor(htmlAttr(el, 'color'));
|
|
145
|
-
if (colorAttr !== '') out.color = colorAttr;
|
|
146
|
-
const face = htmlAttr(el, 'face');
|
|
147
|
-
if (face) out.font = face;
|
|
148
|
-
return out;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** 文本框 HTML → 段落序列(空 HTML 或解析失败一律当没有内容) */
|
|
152
|
-
export function htmlToParas(src) {
|
|
153
|
-
if (!String(src || '').trim()) return [];
|
|
154
|
-
const doc = new DOMParser().parseFromString(`<body>${src}</body>`, 'text/html');
|
|
155
|
-
const box = doc.body;
|
|
156
|
-
const c = new ParaCollector();
|
|
157
|
-
c.walk(box, { bold: false, italic: false, underline: false, strike: false, color: '', font: '', size: 0 });
|
|
158
|
-
c.closePara(false);
|
|
159
|
-
// 末尾空段是 contenteditable / PowerPoint 默认空段落的产物,不带信息
|
|
160
|
-
while (c.paras.length > 0 && c.paras[c.paras.length - 1].runs.length === 0) c.paras.pop();
|
|
161
|
-
return c.paras;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/** 段落序列 → 文本框 HTML(列表写成 ul/ol,普通段落写成 div) */
|
|
165
|
-
export function parasToHTML(paras) {
|
|
166
|
-
let out = '';
|
|
167
|
-
let openList = '';
|
|
168
|
-
const closeList = () => {
|
|
169
|
-
if (openList) out += `</${openList}>`;
|
|
170
|
-
openList = '';
|
|
171
|
-
};
|
|
172
|
-
for (const p of paras) {
|
|
173
|
-
const list = p.list === 'bullet' ? 'ul' : p.list === 'number' ? 'ol' : '';
|
|
174
|
-
if (list !== openList) {
|
|
175
|
-
closeList();
|
|
176
|
-
if (list) {
|
|
177
|
-
out += `<${list}>`;
|
|
178
|
-
openList = list;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
const align = p.align && p.align !== 'left' ? `text-align:${p.align}` : '';
|
|
182
|
-
const indent = p.level > 0 ? `margin-left:${(p.level * 1.2).toFixed(1)}em` : '';
|
|
183
|
-
const style = [align, indent].filter(Boolean).join(';');
|
|
184
|
-
const styleAttr = style ? ` style="${style}"` : '';
|
|
185
|
-
const body = p.runs.length ? p.runs.map((r) => runToHTML(r)).join('') : '<br>';
|
|
186
|
-
if (list) out += `<li${styleAttr}>${body}</li>`;
|
|
187
|
-
else out += `<div${styleAttr}>${body}</div>`;
|
|
188
|
-
}
|
|
189
|
-
closeList();
|
|
190
|
-
return out;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/** 一个 run → HTML(格式用标签 + 内联样式表达) */
|
|
194
|
-
export function runToHTML(r) {
|
|
195
|
-
let text = xmlEscape(r.text);
|
|
196
|
-
if (text === '') return '';
|
|
197
|
-
if (r.underline) text = `<u>${text}</u>`;
|
|
198
|
-
if (r.strike) text = `<s>${text}</s>`;
|
|
199
|
-
if (r.italic) text = `<i>${text}</i>`;
|
|
200
|
-
if (r.bold) text = `<b>${text}</b>`;
|
|
201
|
-
const decls = [];
|
|
202
|
-
if (r.color) decls.push(`color:${r.color}`);
|
|
203
|
-
if (r.size > 0) decls.push(`font-size:${r.size}px`);
|
|
204
|
-
if (r.font) decls.push(`font-family:${r.font}`);
|
|
205
|
-
if (decls.length) text = `<span style="${decls.join(';')}">${text}</span>`;
|
|
206
|
-
return text;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/** 段落 → 纯文本行(表格单元格与图形内文字用;列表带标记、按级别缩进) */
|
|
210
|
-
export function parasPlainLines(paras) {
|
|
211
|
-
let number = 0;
|
|
212
|
-
return paras.map((p) => {
|
|
213
|
-
if (p.list === 'number') number += 1;
|
|
214
|
-
else if (!p.list) number = 0;
|
|
215
|
-
const prefix = p.list === 'bullet' ? '• ' : p.list === 'number' ? `${number}. ` : '';
|
|
216
|
-
return `${' '.repeat(p.level || 0)}${prefix}${p.runs.map((r) => r.text).join('')}`;
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
/** 段落 → 多行纯文本 */
|
|
221
|
-
export const parasPlainText = (paras) => parasPlainLines(paras).join('\n');
|
|
222
|
-
|
|
223
|
-
/** 纯文本 → 段落(每行一段,图形内文字与表格单元格共用) */
|
|
224
|
-
export const parasOfPlain = (text) =>
|
|
225
|
-
String(text ?? '').split('\n').map((line) => ({
|
|
226
|
-
align: '', list: '', level: 0,
|
|
227
|
-
runs: [{ text: line, bold: false, italic: false, underline: false, strike: false, color: '', font: '', size: 0 }],
|
|
228
|
-
}));
|
|
229
|
-
|
|
230
|
-
/** 纯文本行数(字数统计与占位符判断共用) */
|
|
231
|
-
export const countPlainChars = (paras) => paras.reduce((n, p) => n + p.runs.reduce((m, r) => m + r.text.length, 0), 0);
|
|
232
|
-
|
|
233
|
-
const SAFE_TAGS = new Set(['DIV', 'P', 'BR', 'B', 'STRONG', 'I', 'EM', 'U', 'S', 'STRIKE', 'DEL', 'SPAN', 'FONT', 'UL', 'OL', 'LI']);
|
|
234
|
-
const SAFE_STYLES = new Set([
|
|
235
|
-
'color', 'font-size', 'font-family', 'font-weight', 'font-style',
|
|
236
|
-
'text-decoration', 'text-align', 'line-height', 'margin-left',
|
|
237
|
-
]);
|
|
238
|
-
const DROP_TAGS = new Set(['SCRIPT', 'STYLE', 'IFRAME', 'OBJECT', 'EMBED', 'LINK', 'META', 'IMG', 'SVG', 'MATH']);
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* 清洗就地编辑器产出的 HTML:只留段落/列表/行内格式白名单,去掉事件属性与可执行样式。
|
|
242
|
-
* 渲染与提交共用同一份,避免粘贴进来的 HTML 在画布里执行脚本。
|
|
243
|
-
*/
|
|
244
|
-
export function sanitizeTextHtml(html) {
|
|
245
|
-
if (!html) return '';
|
|
246
|
-
const doc = new DOMParser().parseFromString(`<body>${html}</body>`, 'text/html');
|
|
247
|
-
const clean = (parent) => {
|
|
248
|
-
for (const child of [...parent.childNodes]) {
|
|
249
|
-
if (child.nodeType === Node.TEXT_NODE) continue;
|
|
250
|
-
if (child.nodeType !== Node.ELEMENT_NODE) { child.remove(); continue; }
|
|
251
|
-
if (!SAFE_TAGS.has(child.tagName)) {
|
|
252
|
-
if (DROP_TAGS.has(child.tagName)) {
|
|
253
|
-
child.remove();
|
|
254
|
-
} else {
|
|
255
|
-
while (child.firstChild) parent.insertBefore(child.firstChild, child);
|
|
256
|
-
child.remove();
|
|
257
|
-
}
|
|
258
|
-
continue;
|
|
259
|
-
}
|
|
260
|
-
if (child.hasAttribute('style')) {
|
|
261
|
-
const kept = [];
|
|
262
|
-
for (let i = 0; i < child.style.length; i += 1) {
|
|
263
|
-
const prop = child.style[i];
|
|
264
|
-
if (SAFE_STYLES.has(prop)) kept.push(`${prop}:${child.style.getPropertyValue(prop)}`);
|
|
265
|
-
}
|
|
266
|
-
if (kept.length) child.setAttribute('style', kept.join(';'));
|
|
267
|
-
else child.removeAttribute('style');
|
|
268
|
-
}
|
|
269
|
-
for (const attr of [...child.attributes]) {
|
|
270
|
-
const name = attr.name.toLowerCase();
|
|
271
|
-
if (!['style', 'color', 'face', 'size', 'align'].includes(name)) child.removeAttribute(attr.name);
|
|
272
|
-
}
|
|
273
|
-
clean(child);
|
|
274
|
-
}
|
|
275
|
-
};
|
|
276
|
-
clean(doc.body);
|
|
277
|
-
return doc.body.innerHTML;
|
|
278
|
-
}
|
|
1
|
+
// [文件] gbpptx·正文转换 —— 文本框 HTML 子集 ⇄ 段落/run(块级摊平成段落,列表保留符号语义)
|
|
2
|
+
// [设计] HTML 只认 contenteditable 会产出的那一小撮标签(div/p/br/b/i/u/s/span/font/ul/ol/li):
|
|
3
|
+
// 块级摊平成段落,行内标记落到 run,列表落到段落的 list/level 上,写出端再变回真正的
|
|
4
|
+
// <a:buChar>/<a:buAutoNum>,而不是把「• 」写进文字——这样 PowerPoint 往返仍是可编辑列表。
|
|
5
|
+
|
|
6
|
+
import { htmlAttr, htmlStyleProp, normalizeColor, xmlEscape } from 'gbooxml';
|
|
7
|
+
|
|
8
|
+
/** 块级标签:独立成段 */
|
|
9
|
+
const BLOCK_TAGS = new Set(['DIV', 'P', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'BLOCKQUOTE', 'PRE']);
|
|
10
|
+
|
|
11
|
+
/** 段落/run 收集器:行内格式一变就切一个 run,块级边界就收一个段落 */
|
|
12
|
+
class ParaCollector {
|
|
13
|
+
constructor() {
|
|
14
|
+
this.paras = [];
|
|
15
|
+
this.cur = newPara();
|
|
16
|
+
this.run = null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
addText(f, s) {
|
|
20
|
+
if (s === '') return;
|
|
21
|
+
if (this.run && !sameFormat(this.run, f)) this.closeRun();
|
|
22
|
+
if (!this.run) this.run = { ...f, text: '' };
|
|
23
|
+
this.run.text += s;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
closeRun() {
|
|
27
|
+
if (this.run && this.run.text !== '') this.cur.runs.push(this.run);
|
|
28
|
+
this.run = null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
closePara(force) {
|
|
32
|
+
this.closeRun();
|
|
33
|
+
if (this.cur.runs.length > 0 || force) this.paras.push(this.cur);
|
|
34
|
+
this.cur = newPara(this.cur);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
startBlock(align, list = '', level = 0) {
|
|
38
|
+
this.closePara(false);
|
|
39
|
+
this.cur = { align, list, level, runs: [] };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** 遍历一个容器;listCtx 用于列表项内部(换行/段落继承列表身份) */
|
|
43
|
+
walk(node, f, listCtx = null) {
|
|
44
|
+
for (const child of Array.from(node.childNodes)) {
|
|
45
|
+
if (child.nodeType === Node.TEXT_NODE) {
|
|
46
|
+
// 元素内的换行是排版空白,真换行由 <br> 表达
|
|
47
|
+
this.addText(f, child.data.replace(/\n/g, ''));
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (child.nodeType !== Node.ELEMENT_NODE) continue;
|
|
51
|
+
const tag = child.tagName;
|
|
52
|
+
if (tag === 'BR') {
|
|
53
|
+
this.closePara(true);
|
|
54
|
+
if (listCtx) this.cur = newPara(this.cur, listCtx.list, listCtx.level);
|
|
55
|
+
} else if (BLOCK_TAGS.has(tag)) {
|
|
56
|
+
this.startBlock(blockAlign(child), listCtx?.list || '', listCtx?.level || 0);
|
|
57
|
+
this.walk(child, f, listCtx);
|
|
58
|
+
this.closePara(false);
|
|
59
|
+
if (listCtx) this.startBlock(blockAlign(child), listCtx.list, listCtx.level);
|
|
60
|
+
} else if (tag === 'UL' || tag === 'OL') {
|
|
61
|
+
this.closePara(false);
|
|
62
|
+
this.walkList(child, f, tag === 'OL', (listCtx?.level || -1) + 1);
|
|
63
|
+
if (listCtx) this.startBlock('', listCtx.list, listCtx.level);
|
|
64
|
+
} else if (tag === 'IMG') {
|
|
65
|
+
// 图片是独立对象,正文里的 <img> 不进 pptx
|
|
66
|
+
} else {
|
|
67
|
+
this.walk(child, inlineFormat(child, f), listCtx);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 列表项 → 带 list/level 的段落;嵌套列表递归到下一级 */
|
|
73
|
+
walkList(list, f, ordered, level) {
|
|
74
|
+
for (const li of Array.from(list.children)) {
|
|
75
|
+
if (li.tagName !== 'LI') continue;
|
|
76
|
+
const lvl = listLevel(li, level);
|
|
77
|
+
this.startBlock(blockAlign(li), ordered ? 'number' : 'bullet', lvl);
|
|
78
|
+
this.walk(li, f, { list: ordered ? 'number' : 'bullet', level: lvl });
|
|
79
|
+
this.closePara(false);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const newPara = (prev, list = '', level = 0) => ({ align: prev?.align || '', list, level, runs: [] });
|
|
85
|
+
|
|
86
|
+
const sameFormat = (a, b) =>
|
|
87
|
+
a.bold === b.bold && a.italic === b.italic && a.underline === b.underline &&
|
|
88
|
+
a.strike === b.strike && a.color === b.color && a.font === b.font && a.size === b.size;
|
|
89
|
+
|
|
90
|
+
/** 列表层级:嵌套层级优先,其次读 parasToHTML 写出的 margin-left */
|
|
91
|
+
function listLevel(li, fallback) {
|
|
92
|
+
const ml = htmlStyleProp(htmlAttr(li, 'style'), 'margin-left');
|
|
93
|
+
if (ml.endsWith('em')) {
|
|
94
|
+
const v = Number.parseFloat(ml);
|
|
95
|
+
if (Number.isFinite(v)) return Math.max(0, Math.min(8, Math.round(v / 1.2)));
|
|
96
|
+
}
|
|
97
|
+
if (ml.endsWith('px')) {
|
|
98
|
+
const v = Number.parseFloat(ml);
|
|
99
|
+
if (Number.isFinite(v)) return Math.max(0, Math.min(8, Math.round(v / 24)));
|
|
100
|
+
}
|
|
101
|
+
return fallback;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 块级元素的对齐(contenteditable 写 style,也可能直接给 align 属性) */
|
|
105
|
+
function blockAlign(el) {
|
|
106
|
+
const attr = htmlAttr(el, 'align');
|
|
107
|
+
if (attr) return attr;
|
|
108
|
+
switch (htmlStyleProp(htmlAttr(el, 'style'), 'text-align')) {
|
|
109
|
+
case 'center': return 'center';
|
|
110
|
+
case 'right': return 'right';
|
|
111
|
+
case 'justify': return 'justify';
|
|
112
|
+
default: return '';
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** 把一个行内标签与它的 style 叠加到当前格式上 */
|
|
117
|
+
function inlineFormat(el, f) {
|
|
118
|
+
const out = { ...f };
|
|
119
|
+
switch (el.tagName) {
|
|
120
|
+
case 'B': case 'STRONG': out.bold = true; break;
|
|
121
|
+
case 'I': case 'EM': out.italic = true; break;
|
|
122
|
+
case 'U': out.underline = true; break;
|
|
123
|
+
case 'S': case 'STRIKE': case 'DEL': out.strike = true; break;
|
|
124
|
+
default: break;
|
|
125
|
+
}
|
|
126
|
+
const style = htmlAttr(el, 'style');
|
|
127
|
+
if (style) {
|
|
128
|
+
const c = normalizeColor(htmlStyleProp(style, 'color'));
|
|
129
|
+
if (c !== '') out.color = c;
|
|
130
|
+
const deco = htmlStyleProp(style, 'text-decoration');
|
|
131
|
+
if (deco.includes('underline')) out.underline = true;
|
|
132
|
+
if (deco.includes('line-through')) out.strike = true;
|
|
133
|
+
const weight = htmlStyleProp(style, 'font-weight');
|
|
134
|
+
if (weight === 'bold' || Number(weight) >= 600) out.bold = true;
|
|
135
|
+
if (/italic|oblique/.test(htmlStyleProp(style, 'font-style'))) out.italic = true;
|
|
136
|
+
const fam = htmlStyleProp(style, 'font-family');
|
|
137
|
+
if (fam !== '') out.font = fam.split(',')[0].trim().replace(/^['"]|['"]$/g, '');
|
|
138
|
+
const size = htmlStyleProp(style, 'font-size');
|
|
139
|
+
if (size.endsWith('px')) {
|
|
140
|
+
const v = Number.parseFloat(size.slice(0, -2));
|
|
141
|
+
if (Number.isFinite(v) && v > 0) out.size = v;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
const colorAttr = normalizeColor(htmlAttr(el, 'color'));
|
|
145
|
+
if (colorAttr !== '') out.color = colorAttr;
|
|
146
|
+
const face = htmlAttr(el, 'face');
|
|
147
|
+
if (face) out.font = face;
|
|
148
|
+
return out;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 文本框 HTML → 段落序列(空 HTML 或解析失败一律当没有内容) */
|
|
152
|
+
export function htmlToParas(src) {
|
|
153
|
+
if (!String(src || '').trim()) return [];
|
|
154
|
+
const doc = new DOMParser().parseFromString(`<body>${src}</body>`, 'text/html');
|
|
155
|
+
const box = doc.body;
|
|
156
|
+
const c = new ParaCollector();
|
|
157
|
+
c.walk(box, { bold: false, italic: false, underline: false, strike: false, color: '', font: '', size: 0 });
|
|
158
|
+
c.closePara(false);
|
|
159
|
+
// 末尾空段是 contenteditable / PowerPoint 默认空段落的产物,不带信息
|
|
160
|
+
while (c.paras.length > 0 && c.paras[c.paras.length - 1].runs.length === 0) c.paras.pop();
|
|
161
|
+
return c.paras;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** 段落序列 → 文本框 HTML(列表写成 ul/ol,普通段落写成 div) */
|
|
165
|
+
export function parasToHTML(paras) {
|
|
166
|
+
let out = '';
|
|
167
|
+
let openList = '';
|
|
168
|
+
const closeList = () => {
|
|
169
|
+
if (openList) out += `</${openList}>`;
|
|
170
|
+
openList = '';
|
|
171
|
+
};
|
|
172
|
+
for (const p of paras) {
|
|
173
|
+
const list = p.list === 'bullet' ? 'ul' : p.list === 'number' ? 'ol' : '';
|
|
174
|
+
if (list !== openList) {
|
|
175
|
+
closeList();
|
|
176
|
+
if (list) {
|
|
177
|
+
out += `<${list}>`;
|
|
178
|
+
openList = list;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const align = p.align && p.align !== 'left' ? `text-align:${p.align}` : '';
|
|
182
|
+
const indent = p.level > 0 ? `margin-left:${(p.level * 1.2).toFixed(1)}em` : '';
|
|
183
|
+
const style = [align, indent].filter(Boolean).join(';');
|
|
184
|
+
const styleAttr = style ? ` style="${style}"` : '';
|
|
185
|
+
const body = p.runs.length ? p.runs.map((r) => runToHTML(r)).join('') : '<br>';
|
|
186
|
+
if (list) out += `<li${styleAttr}>${body}</li>`;
|
|
187
|
+
else out += `<div${styleAttr}>${body}</div>`;
|
|
188
|
+
}
|
|
189
|
+
closeList();
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** 一个 run → HTML(格式用标签 + 内联样式表达) */
|
|
194
|
+
export function runToHTML(r) {
|
|
195
|
+
let text = xmlEscape(r.text);
|
|
196
|
+
if (text === '') return '';
|
|
197
|
+
if (r.underline) text = `<u>${text}</u>`;
|
|
198
|
+
if (r.strike) text = `<s>${text}</s>`;
|
|
199
|
+
if (r.italic) text = `<i>${text}</i>`;
|
|
200
|
+
if (r.bold) text = `<b>${text}</b>`;
|
|
201
|
+
const decls = [];
|
|
202
|
+
if (r.color) decls.push(`color:${r.color}`);
|
|
203
|
+
if (r.size > 0) decls.push(`font-size:${r.size}px`);
|
|
204
|
+
if (r.font) decls.push(`font-family:${r.font}`);
|
|
205
|
+
if (decls.length) text = `<span style="${decls.join(';')}">${text}</span>`;
|
|
206
|
+
return text;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** 段落 → 纯文本行(表格单元格与图形内文字用;列表带标记、按级别缩进) */
|
|
210
|
+
export function parasPlainLines(paras) {
|
|
211
|
+
let number = 0;
|
|
212
|
+
return paras.map((p) => {
|
|
213
|
+
if (p.list === 'number') number += 1;
|
|
214
|
+
else if (!p.list) number = 0;
|
|
215
|
+
const prefix = p.list === 'bullet' ? '• ' : p.list === 'number' ? `${number}. ` : '';
|
|
216
|
+
return `${' '.repeat(p.level || 0)}${prefix}${p.runs.map((r) => r.text).join('')}`;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** 段落 → 多行纯文本 */
|
|
221
|
+
export const parasPlainText = (paras) => parasPlainLines(paras).join('\n');
|
|
222
|
+
|
|
223
|
+
/** 纯文本 → 段落(每行一段,图形内文字与表格单元格共用) */
|
|
224
|
+
export const parasOfPlain = (text) =>
|
|
225
|
+
String(text ?? '').split('\n').map((line) => ({
|
|
226
|
+
align: '', list: '', level: 0,
|
|
227
|
+
runs: [{ text: line, bold: false, italic: false, underline: false, strike: false, color: '', font: '', size: 0 }],
|
|
228
|
+
}));
|
|
229
|
+
|
|
230
|
+
/** 纯文本行数(字数统计与占位符判断共用) */
|
|
231
|
+
export const countPlainChars = (paras) => paras.reduce((n, p) => n + p.runs.reduce((m, r) => m + r.text.length, 0), 0);
|
|
232
|
+
|
|
233
|
+
const SAFE_TAGS = new Set(['DIV', 'P', 'BR', 'B', 'STRONG', 'I', 'EM', 'U', 'S', 'STRIKE', 'DEL', 'SPAN', 'FONT', 'UL', 'OL', 'LI']);
|
|
234
|
+
const SAFE_STYLES = new Set([
|
|
235
|
+
'color', 'font-size', 'font-family', 'font-weight', 'font-style',
|
|
236
|
+
'text-decoration', 'text-align', 'line-height', 'margin-left',
|
|
237
|
+
]);
|
|
238
|
+
const DROP_TAGS = new Set(['SCRIPT', 'STYLE', 'IFRAME', 'OBJECT', 'EMBED', 'LINK', 'META', 'IMG', 'SVG', 'MATH']);
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 清洗就地编辑器产出的 HTML:只留段落/列表/行内格式白名单,去掉事件属性与可执行样式。
|
|
242
|
+
* 渲染与提交共用同一份,避免粘贴进来的 HTML 在画布里执行脚本。
|
|
243
|
+
*/
|
|
244
|
+
export function sanitizeTextHtml(html) {
|
|
245
|
+
if (!html) return '';
|
|
246
|
+
const doc = new DOMParser().parseFromString(`<body>${html}</body>`, 'text/html');
|
|
247
|
+
const clean = (parent) => {
|
|
248
|
+
for (const child of [...parent.childNodes]) {
|
|
249
|
+
if (child.nodeType === Node.TEXT_NODE) continue;
|
|
250
|
+
if (child.nodeType !== Node.ELEMENT_NODE) { child.remove(); continue; }
|
|
251
|
+
if (!SAFE_TAGS.has(child.tagName)) {
|
|
252
|
+
if (DROP_TAGS.has(child.tagName)) {
|
|
253
|
+
child.remove();
|
|
254
|
+
} else {
|
|
255
|
+
while (child.firstChild) parent.insertBefore(child.firstChild, child);
|
|
256
|
+
child.remove();
|
|
257
|
+
}
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (child.hasAttribute('style')) {
|
|
261
|
+
const kept = [];
|
|
262
|
+
for (let i = 0; i < child.style.length; i += 1) {
|
|
263
|
+
const prop = child.style[i];
|
|
264
|
+
if (SAFE_STYLES.has(prop)) kept.push(`${prop}:${child.style.getPropertyValue(prop)}`);
|
|
265
|
+
}
|
|
266
|
+
if (kept.length) child.setAttribute('style', kept.join(';'));
|
|
267
|
+
else child.removeAttribute('style');
|
|
268
|
+
}
|
|
269
|
+
for (const attr of [...child.attributes]) {
|
|
270
|
+
const name = attr.name.toLowerCase();
|
|
271
|
+
if (!['style', 'color', 'face', 'size', 'align'].includes(name)) child.removeAttribute(attr.name);
|
|
272
|
+
}
|
|
273
|
+
clean(child);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
clean(doc.body);
|
|
277
|
+
return doc.body.innerHTML;
|
|
278
|
+
}
|