@a200888213/gbdocx 0.0.0-stage → 0.3.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 +205 -2
- package/index.html +24 -0
- package/package.json +47 -6
- package/serve.mjs +20 -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 +152 -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 +88 -0
- package/src/app/word-app.js +440 -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 +121 -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 +208 -0
- package/src/kernel/editor/engine/commands.js +180 -0
- package/src/kernel/editor/engine/dom.js +622 -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 +371 -0
- package/src/kernel/editor/engine/tableMenu.js +250 -0
- package/src/kernel/editor/engine/tableOps.js +178 -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 +469 -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 +36 -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/special-chars.js +39 -0
- package/src/ui/statusbar.js +81 -0
- package/src/ui/toolbar.js +199 -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 +450 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// [文件] web·editor-kernel·overlayGeometry —— 编辑面浮件坐标基准:把 getBoundingClientRect 的视觉坐标折算回滚动容器的局部坐标
|
|
2
|
+
// [通用:工具] overlayOrigin —— 浮件坐标基准(视觉 → 局部,缩放感知)【强制通道】
|
|
3
|
+
// [坑] 屏幕缩放(Word 应用挂在 .word-editor 的 CSS zoom 上)会让「视觉差值」≠「局部 left/top」:
|
|
4
|
+
// 局部 px × 缩放 = 视觉 px,直接把视觉差值当 left/top 用,非 100% 缩放时浮件会整体偏移。
|
|
5
|
+
// 故基准点与缩放系数都从一枚探针现量——探针挂在滚动容器 padding box 原点、局部宽恒 PROBE_WIDTH,
|
|
6
|
+
// 它随内容滚动,故原点、滚动量、缩放三件事一次量全(不必碰 scrollLeft / clientLeft 的缩放口径)。
|
|
7
|
+
|
|
8
|
+
/** 探针的局部宽度(px):量到的视觉宽度 ÷ 此值 = 所在子树的缩放系数 */
|
|
9
|
+
export const PROBE_WIDTH = 100;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 浮件坐标基准
|
|
13
|
+
* @param {HTMLElement|null} probe 探针元素(挂在滚动容器 padding box 原点、局部宽 = PROBE_WIDTH)
|
|
14
|
+
* @returns {{x:number, y:number, z:number}} x/y = 局部 (0,0) 的视觉坐标;z = 缩放系数
|
|
15
|
+
*/
|
|
16
|
+
export function overlayOrigin(probe) {
|
|
17
|
+
const rect = probe?.getBoundingClientRect?.();
|
|
18
|
+
if (!rect) return { x: 0, y: 0, z: 1 };
|
|
19
|
+
return { x: rect.left, y: rect.top, z: rect.width > 0 ? rect.width / PROBE_WIDTH : 1 };
|
|
20
|
+
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
// [文件] web·editor-kernel·pasteClean —— 粘贴内容清洗
|
|
2
|
+
// [设计] 分两档处理,不做一刀切:
|
|
3
|
+
// · Office(Word/Outlook)粘贴:stripping 最凶——要去掉 mso-* 与命名空间标签,把 pt 字号归一成
|
|
4
|
+
// 预设档位(否则工具栏字号下拉回显不出来),行高同理;丢弃 font-family(Word 会带一堆本机字体)。
|
|
5
|
+
// · 其它来源(内部复制、网页):保留字号字体原样,保证编辑器内复制粘贴不失真,只清掉结构性垃圾。
|
|
6
|
+
// 判断依据是 Office 特征串:Word 的 HTML 必然含 <o:p> / mso- 样式 / microsoft-com 命名空间。
|
|
7
|
+
|
|
8
|
+
import { LINE_HEIGHTS } from './constants.js';
|
|
9
|
+
import { moveChildren } from './engine/dom.js';
|
|
10
|
+
import { SAFE_IMG } from '../util/safeValue.js';
|
|
11
|
+
import { FONT_SIZES } from '../textStyles.js';
|
|
12
|
+
|
|
13
|
+
/** 结构性垃圾标签:样式块、脚本、外链声明、可执行/可嵌入内容,以及 Word 的命名空间标签(o:p、w:*、v:*) */
|
|
14
|
+
const DROP_TAGS = new Set([
|
|
15
|
+
'style', 'script', 'meta', 'link', 'title', 'base', 'xml',
|
|
16
|
+
'iframe', 'object', 'embed', 'form', 'button', 'select', 'textarea',
|
|
17
|
+
'svg', 'math'
|
|
18
|
+
]);
|
|
19
|
+
|
|
20
|
+
/** 允许保留的内联样式;其余(margin/padding/border/background-image/mso-*…)一律丢弃 */
|
|
21
|
+
const KEEP_PROPS = new Set([
|
|
22
|
+
'text-align',
|
|
23
|
+
'font-weight',
|
|
24
|
+
'font-style',
|
|
25
|
+
'text-decoration',
|
|
26
|
+
'color',
|
|
27
|
+
'background-color',
|
|
28
|
+
'font-size',
|
|
29
|
+
'font-family',
|
|
30
|
+
'line-height',
|
|
31
|
+
'width',
|
|
32
|
+
'min-width',
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
/** Office 粘贴特征 */
|
|
36
|
+
const OFFICE_SIGNATURE = /<o:p|mso-|urn:schemas-microsoft-com|<!--\[if/i;
|
|
37
|
+
|
|
38
|
+
const SAFE_LINE_HEIGHT_MIN = 1;
|
|
39
|
+
const SAFE_LINE_HEIGHT_MAX = 4;
|
|
40
|
+
|
|
41
|
+
/** pt / px → 归档到预设字号;无法安全换算或超出范围的返回 ''(丢弃该声明) */
|
|
42
|
+
function snapFontSize(value) {
|
|
43
|
+
const matched = String(value)
|
|
44
|
+
.trim()
|
|
45
|
+
.match(/^([\d.]+)\s*(pt|px)$/i);
|
|
46
|
+
if (!matched) return '';
|
|
47
|
+
const num = Number(matched[1]);
|
|
48
|
+
if (!Number.isFinite(num)) return '';
|
|
49
|
+
const px = Math.round(matched[2].toLowerCase() === 'pt' ? num * (4 / 3) : num);
|
|
50
|
+
if (px < FONT_SIZES[0] || px > FONT_SIZES[FONT_SIZES.length - 1]) return '';
|
|
51
|
+
return `${FONT_SIZES.reduce((best, size) => (Math.abs(size - px) < Math.abs(best - px) ? size : best), FONT_SIZES[0])}px`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 行高 → 归档到预设档位
|
|
56
|
+
* [坑] 必须设下限:Word 里 30% 这种行高换算成倍数就是 0.3,落到预设档会变成 0.25,
|
|
57
|
+
* 直接导致文字互相压叠(这正是踩过的那个坑)。低于 1 倍一律丢弃。
|
|
58
|
+
*/
|
|
59
|
+
function snapLineHeight(value) {
|
|
60
|
+
const raw = String(value).trim();
|
|
61
|
+
let ratio = null;
|
|
62
|
+
const percent = raw.match(/^([\d.]+)%$/);
|
|
63
|
+
if (percent) ratio = Number(percent[1]) / 100;
|
|
64
|
+
else {
|
|
65
|
+
const plain = raw.match(/^([\d.]+)$/);
|
|
66
|
+
if (plain) ratio = Number(plain[1]);
|
|
67
|
+
}
|
|
68
|
+
if (!ratio || !Number.isFinite(ratio)) return '';
|
|
69
|
+
if (ratio < SAFE_LINE_HEIGHT_MIN || ratio > SAFE_LINE_HEIGHT_MAX) return '';
|
|
70
|
+
return LINE_HEIGHTS.reduce(
|
|
71
|
+
(best, item) => (Math.abs(Number(item) - ratio) < Math.abs(Number(best) - ratio) ? item : best),
|
|
72
|
+
LINE_HEIGHTS[0]
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 逐条过滤内联样式 */
|
|
77
|
+
function cleanStyleValue(rawStyle, aggressive) {
|
|
78
|
+
const kept = [];
|
|
79
|
+
String(rawStyle)
|
|
80
|
+
.split(';')
|
|
81
|
+
.forEach((declaration) => {
|
|
82
|
+
const colon = declaration.indexOf(':');
|
|
83
|
+
if (colon < 0) return;
|
|
84
|
+
const prop = declaration.slice(0, colon).trim().toLowerCase();
|
|
85
|
+
let value = declaration.slice(colon + 1).trim();
|
|
86
|
+
if (!prop || !value || !KEEP_PROPS.has(prop)) return;
|
|
87
|
+
|
|
88
|
+
if (aggressive) {
|
|
89
|
+
if (prop === 'font-family') return; // Word 会带入本机字体名,落到文档里只会造成字体混乱
|
|
90
|
+
if (prop === 'font-size') value = snapFontSize(value);
|
|
91
|
+
if (prop === 'line-height') value = snapLineHeight(value);
|
|
92
|
+
if (!value) return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
kept.push(`${prop}: ${value}`);
|
|
96
|
+
});
|
|
97
|
+
return kept.join('; ');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** 去掉所有注释节点(Word 的条件注释) */
|
|
101
|
+
function stripComments(root) {
|
|
102
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_COMMENT);
|
|
103
|
+
const nodes = [];
|
|
104
|
+
while (walker.nextNode()) nodes.push(walker.currentNode);
|
|
105
|
+
nodes.forEach((node) => node.remove());
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 长度声明 → 归一权重(pt / px → px;百分比单独标记);无法解析或非正数返回 null */
|
|
109
|
+
function lengthOf(value) {
|
|
110
|
+
const matched = String(value || '')
|
|
111
|
+
.trim()
|
|
112
|
+
.match(/^([\d.]+)\s*(pt|px|%)?$/i);
|
|
113
|
+
if (!matched) return null;
|
|
114
|
+
const num = Number(matched[1]);
|
|
115
|
+
if (!Number.isFinite(num) || num <= 0) return null;
|
|
116
|
+
const unit = (matched[2] || 'px').toLowerCase();
|
|
117
|
+
if (unit === '%') return { percent: num };
|
|
118
|
+
return { px: unit === 'pt' ? num * (4 / 3) : num };
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** 读元素的内联 width 声明(cleanStyleValue 已把样式压成 `prop: value` 序列) */
|
|
122
|
+
function declaredWidthOf(el) {
|
|
123
|
+
for (const declaration of String(el.getAttribute('style') || '').split(';')) {
|
|
124
|
+
const colon = declaration.indexOf(':');
|
|
125
|
+
if (colon < 0) continue;
|
|
126
|
+
if (declaration.slice(0, colon).trim().toLowerCase() === 'width') return declaration.slice(colon + 1).trim();
|
|
127
|
+
}
|
|
128
|
+
return '';
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** 元素声明的列宽:优先内联样式,其次 HTML width 属性 */
|
|
132
|
+
const widthOf = (el) => lengthOf(declaredWidthOf(el) || el.getAttribute('width') || '');
|
|
133
|
+
|
|
134
|
+
/** 剥掉结构元素上的定宽声明:表格外框统一交给 CSS 的 width: 100% */
|
|
135
|
+
function stripFixedWidth(el) {
|
|
136
|
+
el.removeAttribute('width');
|
|
137
|
+
const style = el.getAttribute('style');
|
|
138
|
+
if (!style) return;
|
|
139
|
+
const kept = [];
|
|
140
|
+
for (const declaration of style.split(';')) {
|
|
141
|
+
const colon = declaration.indexOf(':');
|
|
142
|
+
if (colon < 0) continue;
|
|
143
|
+
const prop = declaration.slice(0, colon).trim().toLowerCase();
|
|
144
|
+
if (prop === 'width' || prop === 'min-width') continue;
|
|
145
|
+
kept.push(declaration.trim());
|
|
146
|
+
}
|
|
147
|
+
if (kept.length) el.setAttribute('style', kept.join('; '));
|
|
148
|
+
else el.removeAttribute('style');
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** 各列宽度 → 归一到合计 100% 的百分比;信息不足返回 null(退化为固定布局等分) */
|
|
152
|
+
function percentWidths(widths, tableWidth) {
|
|
153
|
+
if (!widths.length || widths.some((item) => !item)) return null;
|
|
154
|
+
if (widths.every((item) => item.percent != null)) {
|
|
155
|
+
const total = widths.reduce((sum, item) => sum + item.percent, 0);
|
|
156
|
+
return total > 0 ? widths.map((item) => (item.percent / total) * 100) : null;
|
|
157
|
+
}
|
|
158
|
+
const resolved = widths.map((item) => item.px ?? (tableWidth?.px ? (item.percent / 100) * tableWidth.px : null));
|
|
159
|
+
if (resolved.some((item) => !item)) return null;
|
|
160
|
+
const total = resolved.reduce((sum, item) => sum + item, 0);
|
|
161
|
+
return total > 0 ? resolved.map((item) => (item / total) * 100) : null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 粘贴表格的宽度收口:Word / 网页表格会把绝对宽度写进 table、colgroup、col、td,
|
|
166
|
+
* 直接落进正文会覆盖 `table { width: 100% }` 并撑出纸张(用户看到的「跑到页面外侧」)。
|
|
167
|
+
* 这里把列宽换算成 colgroup 百分比、剥掉所有绝对定宽,让表格始终贴合版心;
|
|
168
|
+
* 列宽信息缺失时退化成固定布局等分,也不让像素宽度残留。
|
|
169
|
+
*/
|
|
170
|
+
function normalizePastedTable(table) {
|
|
171
|
+
const colgroups = Array.from(table.children).filter((el) => el.tagName === 'COLGROUP');
|
|
172
|
+
const cols = colgroups.flatMap((group) => Array.from(group.children).filter((el) => el.tagName === 'COL'));
|
|
173
|
+
// 找一行没有跨列的格子做列宽来源:标题行整行合并的表格也能从正文行拿到比例
|
|
174
|
+
const plainRow = Array.from(table.rows || []).find((row) => Array.from(row.cells).every((cell) => cell.colSpan === 1));
|
|
175
|
+
const cells = plainRow ? Array.from(plainRow.cells) : [];
|
|
176
|
+
const gridCount = cells.length || cols.length;
|
|
177
|
+
const tableWidth = widthOf(table);
|
|
178
|
+
|
|
179
|
+
let widths = cols.map(widthOf);
|
|
180
|
+
if (cells.length && (cols.length !== cells.length || !widths.some(Boolean))) {
|
|
181
|
+
widths = cells.map(widthOf);
|
|
182
|
+
} else if (cells.length === cols.length) {
|
|
183
|
+
const cellWidths = cells.map(widthOf);
|
|
184
|
+
widths = widths.map((item, i) => item || cellWidths[i]);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// 先剥宽再回写,顺序反了会把刚换算好的百分比一起剥掉
|
|
188
|
+
stripFixedWidth(table);
|
|
189
|
+
cols.forEach(stripFixedWidth);
|
|
190
|
+
for (const row of Array.from(table.rows || [])) {
|
|
191
|
+
stripFixedWidth(row);
|
|
192
|
+
for (const cell of Array.from(row.cells)) stripFixedWidth(cell);
|
|
193
|
+
}
|
|
194
|
+
for (const child of Array.from(table.children)) {
|
|
195
|
+
if (!/^(THEAD|TBODY|TFOOT|COLGROUP)$/.test(child.tagName)) continue;
|
|
196
|
+
stripFixedWidth(child);
|
|
197
|
+
for (const row of Array.from(child.children)) if (row.tagName === 'TR') stripFixedWidth(row);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const percents = gridCount && widths.length === gridCount ? percentWidths(widths, tableWidth) : null;
|
|
201
|
+
if (!percents) return;
|
|
202
|
+
|
|
203
|
+
if (cols.length === percents.length) {
|
|
204
|
+
cols.forEach((col, i) => {
|
|
205
|
+
col.setAttribute('style', `width: ${Math.round(percents[i] * 10) / 10}%`);
|
|
206
|
+
});
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
colgroups.forEach((group) => group.remove());
|
|
210
|
+
const colgroup = document.createElement('colgroup');
|
|
211
|
+
percents.forEach((percent) => {
|
|
212
|
+
const col = document.createElement('col');
|
|
213
|
+
col.setAttribute('style', `width: ${Math.round(percent * 10) / 10}%`);
|
|
214
|
+
colgroup.appendChild(col);
|
|
215
|
+
});
|
|
216
|
+
table.prepend(colgroup);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** 从 style 串里抽取 vertical-align 的上下标值(Word / 网页粘贴的上下标就是它,不是 sup/sub 标签) */
|
|
220
|
+
const verticalAlignOf = (style) => {
|
|
221
|
+
for (const decl of String(style).split(';')) {
|
|
222
|
+
const colon = decl.indexOf(':');
|
|
223
|
+
if (colon < 0) continue;
|
|
224
|
+
if (decl.slice(0, colon).trim().toLowerCase() !== 'vertical-align') continue;
|
|
225
|
+
const v = decl.slice(colon + 1).trim().toLowerCase();
|
|
226
|
+
if (v === 'super') return 'super';
|
|
227
|
+
if (v === 'sub' || v === 'subscript') return 'sub';
|
|
228
|
+
}
|
|
229
|
+
return '';
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* 清洗粘贴进来的 HTML
|
|
234
|
+
* @param {string} html 剪贴板中的 text/html
|
|
235
|
+
* @returns {string} 清洗后的 HTML(交由内核装载)
|
|
236
|
+
*/
|
|
237
|
+
export function cleanPastedHtml(html) {
|
|
238
|
+
if (!html || typeof html !== 'string') return html;
|
|
239
|
+
// 无 DOMParser 的环境(服务端渲染 / 单测)原样返回,交给装载侧兜底
|
|
240
|
+
if (typeof DOMParser === 'undefined') return html;
|
|
241
|
+
|
|
242
|
+
const aggressive = OFFICE_SIGNATURE.test(html);
|
|
243
|
+
const parsed = new DOMParser().parseFromString(html, 'text/html');
|
|
244
|
+
const body = parsed.body;
|
|
245
|
+
|
|
246
|
+
// 静态 NodeList,可安全地边遍历边移除
|
|
247
|
+
body.querySelectorAll('*').forEach((el) => {
|
|
248
|
+
const tag = el.tagName.toLowerCase();
|
|
249
|
+
if (DROP_TAGS.has(tag) || tag.includes(':')) {
|
|
250
|
+
el.remove();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
el.removeAttribute('class');
|
|
255
|
+
el.removeAttribute('id');
|
|
256
|
+
el.removeAttribute('dir');
|
|
257
|
+
el.removeAttribute('lang');
|
|
258
|
+
el.removeAttribute('align');
|
|
259
|
+
// 事件属性是脚本入口:粘贴内容一律不允许携带(onclick/onerror/onload…)
|
|
260
|
+
for (const attr of Array.from(el.attributes)) {
|
|
261
|
+
if (attr.name.toLowerCase().startsWith('on')) el.removeAttribute(attr.name);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (el.hasAttribute('style')) {
|
|
265
|
+
const rawStyle = el.getAttribute('style');
|
|
266
|
+
const va = verticalAlignOf(rawStyle);
|
|
267
|
+
const cleaned = cleanStyleValue(rawStyle, aggressive);
|
|
268
|
+
if (cleaned) el.setAttribute('style', cleaned);
|
|
269
|
+
else el.removeAttribute('style');
|
|
270
|
+
// 上下标换壳:内核与 docx 写出都只认 sup/sub 标签,vertical-align 声明会在一次编辑重建时被拉平
|
|
271
|
+
if (va && tag === 'span') {
|
|
272
|
+
const box = document.createElement(va === 'super' ? 'sup' : 'sub');
|
|
273
|
+
if (el.hasAttribute('style')) {
|
|
274
|
+
el.replaceWith(box);
|
|
275
|
+
box.appendChild(el);
|
|
276
|
+
} else {
|
|
277
|
+
moveChildren(el, box);
|
|
278
|
+
el.replaceWith(box);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// 指向本地文件 / 剪贴板协议的图片粘进来只会是坏图,直接丢掉而不是留个红色占位
|
|
284
|
+
if (tag === 'img') {
|
|
285
|
+
const src = el.getAttribute('src') || '';
|
|
286
|
+
if (!src || !SAFE_IMG.test(src)) el.remove();
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
stripComments(body);
|
|
291
|
+
body.querySelectorAll('table').forEach(normalizePastedTable);
|
|
292
|
+
return body.innerHTML;
|
|
293
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// [文件] web·editor-kernel·utils —— 编辑器浮层定位工具(颜色归一口径见 guabiutils 的 normalizeColor)
|
|
2
|
+
// 无状态、不依赖 Vue 与编辑器实例,便于单独推理与复用。
|
|
3
|
+
|
|
4
|
+
import { clampFloatPosition } from 'guabiutils';
|
|
5
|
+
|
|
6
|
+
const PLACEMENT_GAP = 6;
|
|
7
|
+
const VIEWPORT_PADDING = 8;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 浮层定位:强制通道 clampFloatPosition 的编辑器口径适配(锚点矩形 + 面板实测尺寸 → fixed 坐标 + 展开侧限高)
|
|
11
|
+
* @param {{left:number,top:number,right:number,bottom:number}} anchor 锚点矩形(点锚传零宽高)
|
|
12
|
+
* @param {{width:number,height:number}} panel 浮层自身尺寸(渲染后实测,禁止估算)
|
|
13
|
+
* @param {{placement?:string, gap?:number}} options placement: bottom-start|bottom|top|top-start|right-start|left-start
|
|
14
|
+
* @returns {{left:number, top:number, maxHeight:number}} maxHeight 必须下发给面板,越界走面板内部滚动
|
|
15
|
+
*/
|
|
16
|
+
export function computePopupPosition(anchor, panel, options = {}) {
|
|
17
|
+
const { placement = 'bottom-start', gap = PLACEMENT_GAP } = options;
|
|
18
|
+
const { left, top, maxHeight } = clampFloatPosition({
|
|
19
|
+
anchor,
|
|
20
|
+
size: panel,
|
|
21
|
+
prefer: placement,
|
|
22
|
+
gap,
|
|
23
|
+
margin: VIEWPORT_PADDING,
|
|
24
|
+
arrowSize: 0,
|
|
25
|
+
});
|
|
26
|
+
return { left: Math.round(left), top: Math.round(top), maxHeight: Math.round(maxHeight) };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 从任意对象安全取 DOMRect(元素不存在时返回 null) */
|
|
30
|
+
export function rectOf(el) {
|
|
31
|
+
if (!el || typeof el.getBoundingClientRect !== 'function') return null;
|
|
32
|
+
return el.getBoundingClientRect();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 浮层 inline 定位样式(坐标 + 展开侧限高)
|
|
37
|
+
* [坑] 锚点或面板尺寸量不到时 pos.maxHeight 仍是初始 0,原样下发会把它压成零高——
|
|
38
|
+
* 浮层「点了没反应」就是这么来的,所以只在量到真实限高时才下发
|
|
39
|
+
* @param {{left:number, top:number, maxHeight?:number}} pos
|
|
40
|
+
* @param {Object} [extra] 面板私有变量(如列数)
|
|
41
|
+
*/
|
|
42
|
+
export function panelStyleOf(pos, extra = {}) {
|
|
43
|
+
return {
|
|
44
|
+
left: `${pos.left}px`,
|
|
45
|
+
top: `${pos.top}px`,
|
|
46
|
+
...(pos.maxHeight > 0 ? { maxHeight: `${pos.maxHeight}px` } : {}),
|
|
47
|
+
...extra,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// [文件] gbdocx·keyboard —— 全局键盘事件总线(唯一 document 监听 + 优先级分发)与 useKeyboard 注册
|
|
2
|
+
// 说明:移植自 guabi 的 keyboardBus/useKeyboard,去掉 Vue 作用域与窗口归属门控(单窗口应用不需要),
|
|
3
|
+
// 保留优先级排序、active 门控与「return true / stopPropagation 即消费」的语义。
|
|
4
|
+
|
|
5
|
+
/** 优先级档位:数值大者先执行,同档按注册先后 */
|
|
6
|
+
export const KEY_PRIORITY = {
|
|
7
|
+
CAPTURE: 1000,
|
|
8
|
+
FLOAT: 800,
|
|
9
|
+
WINDOW: 600,
|
|
10
|
+
APPLET: 400,
|
|
11
|
+
GLOBAL: 200,
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** 归一键盘事件键名(小写);非真实按键事件返回空串 */
|
|
15
|
+
export const eventKey = (e) => (typeof e?.key === 'string' ? e.key.toLowerCase() : '');
|
|
16
|
+
|
|
17
|
+
const NON_TEXT_INPUT_TYPES = new Set(['button', 'submit', 'reset', 'checkbox', 'radio', 'range', 'color', 'file']);
|
|
18
|
+
|
|
19
|
+
/** 焦点元素是否处于「文本输入态」(文本域 / 文本输入框 / 可编辑区 / 下拉) */
|
|
20
|
+
export function isTypingTarget(el) {
|
|
21
|
+
if (!el || !document.documentElement.contains(el)) return false;
|
|
22
|
+
if (el.isContentEditable) return true;
|
|
23
|
+
const tag = el.tagName;
|
|
24
|
+
if (tag === 'TEXTAREA' || tag === 'SELECT') return true;
|
|
25
|
+
if (tag === 'INPUT') return !NON_TEXT_INPUT_TYPES.has(String(el.type || 'text').toLowerCase());
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const registry = { keydown: { capture: [], bubble: [] }, keyup: { capture: [], bubble: [] } };
|
|
30
|
+
let sorted = true;
|
|
31
|
+
let bound = false;
|
|
32
|
+
let seq = 0;
|
|
33
|
+
|
|
34
|
+
function sortRegistry() {
|
|
35
|
+
for (const type of ['keydown', 'keyup']) {
|
|
36
|
+
for (const phase of ['capture', 'bubble']) {
|
|
37
|
+
registry[type][phase].sort((a, b) => b.priority - a.priority || a.seq - b.seq);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
sorted = true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function dispatch(type, phase, e) {
|
|
44
|
+
const list = registry[type][phase];
|
|
45
|
+
if (!list.length) return;
|
|
46
|
+
if (!eventKey(e)) return;
|
|
47
|
+
if (!sorted) sortRegistry();
|
|
48
|
+
for (const item of list.slice()) {
|
|
49
|
+
if (item.active && !item.active(e)) continue;
|
|
50
|
+
const consumed = item.handler(e) === true;
|
|
51
|
+
if (consumed || e.cancelBubble === true) return;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const onDownCapture = (e) => dispatch('keydown', 'capture', e);
|
|
56
|
+
const onDownBubble = (e) => dispatch('keydown', 'bubble', e);
|
|
57
|
+
const onUpCapture = (e) => dispatch('keyup', 'capture', e);
|
|
58
|
+
const onUpBubble = (e) => dispatch('keyup', 'bubble', e);
|
|
59
|
+
|
|
60
|
+
function bind() {
|
|
61
|
+
document.addEventListener('keydown', onDownCapture, true);
|
|
62
|
+
document.addEventListener('keydown', onDownBubble);
|
|
63
|
+
document.addEventListener('keyup', onUpCapture, true);
|
|
64
|
+
document.addEventListener('keyup', onUpBubble);
|
|
65
|
+
bound = true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function unbind() {
|
|
69
|
+
document.removeEventListener('keydown', onDownCapture, true);
|
|
70
|
+
document.removeEventListener('keydown', onDownBubble);
|
|
71
|
+
document.removeEventListener('keyup', onUpCapture, true);
|
|
72
|
+
document.removeEventListener('keyup', onUpBubble);
|
|
73
|
+
bound = false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function syncBound() {
|
|
77
|
+
const need =
|
|
78
|
+
registry.keydown.capture.length || registry.keydown.bubble.length ||
|
|
79
|
+
registry.keyup.capture.length || registry.keyup.bubble.length;
|
|
80
|
+
if (need && !bound) bind();
|
|
81
|
+
else if (!need && bound) unbind();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function subscribe(type, handler, { priority = KEY_PRIORITY.GLOBAL, phase = 'capture', active } = {}) {
|
|
85
|
+
if (typeof handler !== 'function') return () => {};
|
|
86
|
+
const item = { seq: ++seq, priority, active, handler };
|
|
87
|
+
const list = registry[type][phase === 'bubble' ? 'bubble' : 'capture'];
|
|
88
|
+
list.push(item);
|
|
89
|
+
sorted = false;
|
|
90
|
+
syncBound();
|
|
91
|
+
return () => {
|
|
92
|
+
const i = list.indexOf(item);
|
|
93
|
+
if (i >= 0) list.splice(i, 1);
|
|
94
|
+
syncBound();
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export const onKeydown = (handler, opts) => subscribe('keydown', handler, opts);
|
|
99
|
+
export const onKeyup = (handler, opts) => subscribe('keyup', handler, opts);
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 注册键盘处理器(同 guabi useKeyboard 的调用形态,去掉作用域门控)
|
|
103
|
+
* @param {{keydown?:Function, keyup?:Function}} handlers
|
|
104
|
+
* @param {{priority?:number, phase?:'capture'|'bubble', active?:Function}} [opts]
|
|
105
|
+
* @returns {() => void} 注销函数
|
|
106
|
+
*/
|
|
107
|
+
export function useKeyboard(handlers, opts = {}) {
|
|
108
|
+
const offs = [];
|
|
109
|
+
if (typeof handlers?.keydown === 'function') offs.push(onKeydown(handlers.keydown, opts));
|
|
110
|
+
if (typeof handlers?.keyup === 'function') offs.push(onKeyup(handlers.keyup, opts));
|
|
111
|
+
return () => {
|
|
112
|
+
while (offs.length) offs.pop()();
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// [文件] gbdocx·notify —— 轻量 toast 通知(success / error / warning / info;forModule 兼容旧调用形态)
|
|
2
|
+
|
|
3
|
+
const HOST_ID = 'gbdocx-notify-host';
|
|
4
|
+
|
|
5
|
+
/** 确保通知容器存在(固定右上角,逐条自上而下堆叠) */
|
|
6
|
+
function host() {
|
|
7
|
+
let el = document.getElementById(HOST_ID);
|
|
8
|
+
if (!el) {
|
|
9
|
+
el = document.createElement('div');
|
|
10
|
+
el.id = HOST_ID;
|
|
11
|
+
el.className = 'gbdocx-notify-host';
|
|
12
|
+
// 样式作用域:通知宿主挂 body,但仍须落在 .word-scope 子树里
|
|
13
|
+
const scope = document.createElement('div');
|
|
14
|
+
scope.className = 'word-scope';
|
|
15
|
+
scope.appendChild(el);
|
|
16
|
+
document.body.appendChild(scope);
|
|
17
|
+
}
|
|
18
|
+
return el;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ICONS = { success: '✓', error: '✕', warning: '!', info: 'i' };
|
|
22
|
+
|
|
23
|
+
function show(type, message, duration = 2600) {
|
|
24
|
+
const root = host();
|
|
25
|
+
const item = document.createElement('div');
|
|
26
|
+
item.className = `gbdocx-notify gbdocx-notify--${type}`;
|
|
27
|
+
const badge = document.createElement('span');
|
|
28
|
+
badge.className = 'gbdocx-notify__badge';
|
|
29
|
+
badge.textContent = ICONS[type] || 'i';
|
|
30
|
+
const text = document.createElement('span');
|
|
31
|
+
text.className = 'gbdocx-notify__text';
|
|
32
|
+
text.textContent = String(message ?? '');
|
|
33
|
+
item.append(badge, text);
|
|
34
|
+
root.appendChild(item);
|
|
35
|
+
requestAnimationFrame(() => item.classList.add('gbd-is-in'));
|
|
36
|
+
const close = () => {
|
|
37
|
+
item.classList.remove('gbd-is-in');
|
|
38
|
+
setTimeout(() => item.remove(), 200);
|
|
39
|
+
};
|
|
40
|
+
item.addEventListener('click', close);
|
|
41
|
+
if (duration > 0) setTimeout(close, duration);
|
|
42
|
+
return close;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const notify = {
|
|
46
|
+
success: (message, duration) => show('success', message, duration),
|
|
47
|
+
error: (message, duration) => show('error', message, duration ?? 3600),
|
|
48
|
+
warning: (message, duration) => show('warning', message, duration ?? 3200),
|
|
49
|
+
info: (message, duration) => show('info', message, duration),
|
|
50
|
+
/** [兼容] 旧调用形态 notify.forModule('x'):模块只影响配色,这里忽略模块名 */
|
|
51
|
+
forModule: () => notify,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export default notify;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// [文件] web·core·文字样式常量 —— 字号/字体/调色板/对齐/字体效果/组合色的单一来源(富文本编辑器与演示文稿工具条共用,禁各域再建第二张表)
|
|
2
|
+
// [通用:常量] textStyles —— FONT_SIZES / FONT_FAMILIES / TEXT_COLORS / BG_COLORS / ALIGN_OPTIONS / FONT_EFFECTS / COMBO_STYLES / RECENT_STYLE_LIMIT
|
|
3
|
+
|
|
4
|
+
/** 字号预设(px,12~72) */
|
|
5
|
+
export const FONT_SIZES = [12, 13, 14, 15, 16, 18, 20, 24, 28, 32, 36, 42, 48, 54, 62, 72];
|
|
6
|
+
|
|
7
|
+
/** 字体(系统字体 + Web 安全字体;内网不回源加载,命中本机字体即生效) */
|
|
8
|
+
export const FONT_FAMILIES = [
|
|
9
|
+
{ label: '微软雅黑', value: 'Microsoft YaHei, "微软雅黑"' },
|
|
10
|
+
{ label: '苹方', value: 'PingFang SC, "苹方-简"' },
|
|
11
|
+
{ label: '宋体', value: 'SimSun, "宋体", serif' },
|
|
12
|
+
{ label: '黑体', value: 'SimHei, "黑体", sans-serif' },
|
|
13
|
+
{ label: '楷体', value: 'KaiTi, "楷体", serif' },
|
|
14
|
+
{ label: '仿宋', value: 'FangSong, "仿宋", serif' },
|
|
15
|
+
{ label: '思源黑体', value: '"Noto Sans SC", "Source Han Sans SC", sans-serif' },
|
|
16
|
+
{ label: '思源宋体', value: '"Noto Serif SC", "Source Han Serif SC", serif' },
|
|
17
|
+
{ label: 'Arial', value: 'Arial, Helvetica, sans-serif' },
|
|
18
|
+
{ label: 'Helvetica', value: '"Helvetica Neue", Helvetica, sans-serif' },
|
|
19
|
+
{ label: 'Inter', value: 'Inter, -apple-system, sans-serif' },
|
|
20
|
+
{ label: 'Roboto', value: 'Roboto, "Segoe UI", sans-serif' },
|
|
21
|
+
{ label: 'Times New Roman', value: '"Times New Roman", Times, serif' },
|
|
22
|
+
{ label: 'Georgia', value: 'Georgia, "Times New Roman", serif' },
|
|
23
|
+
{ label: 'Courier New', value: '"Courier New", Courier, monospace' },
|
|
24
|
+
{ label: 'Consolas', value: 'Consolas, "Courier New", monospace' },
|
|
25
|
+
{ label: 'Monaco', value: 'Monaco, Consolas, monospace' },
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
/** 对齐方式(互斥高亮) */
|
|
29
|
+
export const ALIGN_OPTIONS = [
|
|
30
|
+
{ value: 'left', title: '左对齐', icon: 'alignLeft' },
|
|
31
|
+
{ value: 'center', title: '居中', icon: 'alignCenter' },
|
|
32
|
+
{ value: 'right', title: '右对齐', icon: 'alignRight' },
|
|
33
|
+
{ value: 'justify', title: '两端对齐', icon: 'alignJustify' },
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
/** 文字色调色板(淡→深,两行:灰阶 + 彩色) */
|
|
37
|
+
export const TEXT_COLORS = [
|
|
38
|
+
'#000000', '#434343', '#666666', '#999999', '#b7b7b7', '#cccccc', '#d9d9d9', '#efefef', '#f3f3f3', '#ffffff',
|
|
39
|
+
'#980000', '#ff0000', '#ff9900', '#bf9000', '#38761d', '#00ff00', '#00ffff', '#4a86e8', '#0000ff', '#9900ff',
|
|
40
|
+
'#cc4125', '#e06666', '#f6b26b', '#ffd966', '#93c47d', '#76a5af', '#6d9eeb', '#8e7cc3', '#c27ba0', '#ff00ff',
|
|
41
|
+
];
|
|
42
|
+
|
|
43
|
+
/** 背景色(高亮)调色板:每组由浅到深,共 6 列 */
|
|
44
|
+
export const BG_COLORS = [
|
|
45
|
+
'#ffffff', '#f2f3f5', '#e7e9eb', '#d4d5d9', '#c9c7c6', '#a8adb3',
|
|
46
|
+
'#fde9e9', '#fddede', '#ffd4d4', '#fbc1c1', '#f8b0b0', '#f5a3a3',
|
|
47
|
+
'#fff3d6', '#ffe9b3', '#ffdf8f', '#ffd25e', '#ffc233', '#ffb500',
|
|
48
|
+
'#fffbd0', '#fdf2bc', '#fbea93', '#f8e35e', '#f5dc33', '#f5d500',
|
|
49
|
+
'#e8fad4', '#d3f2b3', '#b9ec8e', '#a5e266', '#8fd354', '#71b823',
|
|
50
|
+
'#d6f5f5', '#b3e9e9', '#8fdcdc', '#66cccc', '#4db8b8', '#339999',
|
|
51
|
+
'#dae8fc', '#d0e4f7', '#b4d6f5', '#9cc2f0', '#7db3ea', '#5698e0',
|
|
52
|
+
];
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 字体效果(互斥布尔样式;key 即内核标记键,value 是落到内联 style 的 CSS 值)
|
|
56
|
+
* 空心 = -webkit-text-stroke 描边 + 透明填充;阴影 = 多层 text-shadow。
|
|
57
|
+
*/
|
|
58
|
+
export const FONT_EFFECTS = [
|
|
59
|
+
{
|
|
60
|
+
key: 'textStroke',
|
|
61
|
+
label: '空心',
|
|
62
|
+
value: '0.2px currentColor',
|
|
63
|
+
preview: { WebkitTextStroke: '0.2px currentColor', WebkitTextFillColor: 'transparent' },
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
key: 'textShadow',
|
|
67
|
+
label: '阴影',
|
|
68
|
+
value: '1px 1px var(--gbd-dt-border-strong), 2px 2px var(--gbd-dt-border-strong)',
|
|
69
|
+
preview: { textShadow: '1px 1px var(--gbd-dt-border-strong), 2px 2px var(--gbd-dt-border-strong)' },
|
|
70
|
+
},
|
|
71
|
+
];
|
|
72
|
+
|
|
73
|
+
/** 组合色(前景 + 背景一体应用);取值同调色板的 hex */
|
|
74
|
+
export const COMBO_STYLES = [
|
|
75
|
+
{ color: '#000000', backgroundColor: '#fde9e9' },
|
|
76
|
+
{ color: '#980000', backgroundColor: '#ffd4d4' },
|
|
77
|
+
{ color: '#bf9000', backgroundColor: '#fff3d6' },
|
|
78
|
+
{ color: '#38761d', backgroundColor: '#e8fad4' },
|
|
79
|
+
{ color: '#4a86e8', backgroundColor: '#dae8fc' },
|
|
80
|
+
{ color: '#9900ff', backgroundColor: '#e7e9eb' },
|
|
81
|
+
];
|
|
82
|
+
|
|
83
|
+
/** 「最近使用的外观」保留条数 */
|
|
84
|
+
export const RECENT_STYLE_LIMIT = 14;
|