@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,594 @@
|
|
|
1
|
+
// [文件] web·editor-kernel·engine·blocks —— 块级命令:块类型转换 / 列表与待办 / 块级样式 / 结构增删移 / 相邻插入 / 回车拆块
|
|
2
|
+
// [约束] 块级属性(行高 / 对齐 / 缩进 / 段落间距)只写在目标块的内联 style 上(与存量正文同口径),代码块与原子块不承载块级样式;
|
|
3
|
+
// 「目标块」一律取选区命中的最外层块,与用户看到的整块一致;显式传入的目标先验 isConnected。
|
|
4
|
+
|
|
5
|
+
import { INDENT_MAX_LEVEL, INDENT_STEP_EM } from '../constants.js';
|
|
6
|
+
import { DOC_CLASS, DROP_ATTRS } from './schema.js';
|
|
7
|
+
import {
|
|
8
|
+
blockLength,
|
|
9
|
+
blockOf,
|
|
10
|
+
blocksInRange,
|
|
11
|
+
CALLOUT_ATTR,
|
|
12
|
+
COL_ATTR,
|
|
13
|
+
charOffset,
|
|
14
|
+
closestWithin,
|
|
15
|
+
domPoint,
|
|
16
|
+
firstTextBlockIn,
|
|
17
|
+
isBlockEmpty,
|
|
18
|
+
hasBlockBody,
|
|
19
|
+
isCell,
|
|
20
|
+
isCodeContent,
|
|
21
|
+
isColumnBlock,
|
|
22
|
+
isContainerBlock,
|
|
23
|
+
isRuntimeBlock,
|
|
24
|
+
isStyleableBlock,
|
|
25
|
+
isTaskItem,
|
|
26
|
+
isTextBlock,
|
|
27
|
+
rangeOf,
|
|
28
|
+
moveChildren,
|
|
29
|
+
renameBlock,
|
|
30
|
+
setCaret,
|
|
31
|
+
setStyleValue,
|
|
32
|
+
styleHost,
|
|
33
|
+
styleTarget,
|
|
34
|
+
structureTarget,
|
|
35
|
+
styleValue,
|
|
36
|
+
SUPER_ATTR,
|
|
37
|
+
wrapElements,
|
|
38
|
+
} from './dom.js';
|
|
39
|
+
/** 块级样式:内核属性名 → CSS 属性 */
|
|
40
|
+
const LINE_HEIGHT = 'line-height';
|
|
41
|
+
const TEXT_ALIGN = 'text-align';
|
|
42
|
+
const INDENT = 'padding-left';
|
|
43
|
+
const FIRST_LINE_INDENT = 'text-indent';
|
|
44
|
+
const MARGIN_TOP = 'margin-top';
|
|
45
|
+
const MARGIN_BOTTOM = 'margin-bottom';
|
|
46
|
+
/** 受块转换搬迁与「清除格式」管理的块级样式属性(段落间距/首行缩进在导入语料里普遍存在,转换不搬会静默丢) */
|
|
47
|
+
const BLOCK_STYLE_PROPS = [LINE_HEIGHT, TEXT_ALIGN, INDENT, FIRST_LINE_INDENT, MARGIN_TOP, MARGIN_BOTTOM];
|
|
48
|
+
|
|
49
|
+
/** 块自定义属性载体:单个 data-* 属性存 JSON(key→value)。
|
|
50
|
+
* [设计] 不把每个自定义属性摊成元素上的独立属性:会与内核/docx 方言属性混名、且 normalizeBody/serialize 的
|
|
51
|
+
* 属性白名单一旦收紧就连带丢失;收成一条 JSON 由读写两端自己解释,随 Block.Attrs 原样往返(存量正文无此属性=零影响)。
|
|
52
|
+
* [坑] 值恒为字符串、键去空白;空键丢弃,空 map 移除整条属性(正文不留 data-battrs="{}" 噪声)。 */
|
|
53
|
+
const BLOCK_ATTRS_ATTR = 'data-battrs';
|
|
54
|
+
|
|
55
|
+
const clampLevel = (value) => Math.min(Math.max(Math.round(Number(value) || 0), 0), INDENT_MAX_LEVEL);
|
|
56
|
+
|
|
57
|
+
/* ===================== 目标解析 ===================== */
|
|
58
|
+
|
|
59
|
+
/** 选区(或显式元素)命中的样式挂载点,去重保序;代码块等不承载块级样式的块直接跳过 */
|
|
60
|
+
export function styleHosts(root, ranges = null, els = null) {
|
|
61
|
+
const out = [];
|
|
62
|
+
const blocks = (els || (ranges || blocksInRange(root)).map((item) => item.block)).filter((el) => el?.isConnected);
|
|
63
|
+
for (const block of blocks) {
|
|
64
|
+
const target = styleTarget(block, root);
|
|
65
|
+
if (!target) continue;
|
|
66
|
+
const host = styleHost(target);
|
|
67
|
+
if (host && !out.includes(host)) out.push(host);
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** 目标必须还挂在编辑面里:菜单/拖拽握着的元素可能已被上一轮改动换掉 */
|
|
73
|
+
export function liveTarget(root, el) {
|
|
74
|
+
if (!el) return null;
|
|
75
|
+
return el.isConnected && root.contains(el) ? el : null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** 选区(或显式元素)命中的结构块:复制 / 删除 / 移动 / 转换的目标 */
|
|
79
|
+
export function structureTargets(root, ranges = null, els = null) {
|
|
80
|
+
const out = [];
|
|
81
|
+
const blocks = (els || (ranges || blocksInRange(root)).map((item) => item.block)).filter((el) => el?.isConnected);
|
|
82
|
+
for (const block of blocks) {
|
|
83
|
+
const target = structureTarget(block, root) || block;
|
|
84
|
+
if (!out.includes(target)) out.push(target);
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/* ===================== 块级样式 ===================== */
|
|
90
|
+
|
|
91
|
+
/** 缩进级数:存量正文可能存 px(外部粘贴),按基准字号换算回级数 */
|
|
92
|
+
export function indentLevelOf(host) {
|
|
93
|
+
const raw = styleValue(host, INDENT);
|
|
94
|
+
if (!raw) return 0;
|
|
95
|
+
const num = Number.parseFloat(raw);
|
|
96
|
+
if (Number.isNaN(num) || num <= 0) return 0;
|
|
97
|
+
return raw.endsWith('em') ? clampLevel(num / INDENT_STEP_EM) : clampLevel(num / (INDENT_STEP_EM * 16));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export function blockStyleOf(host) {
|
|
101
|
+
return {
|
|
102
|
+
lineHeight: styleValue(host, LINE_HEIGHT),
|
|
103
|
+
textAlign: styleValue(host, TEXT_ALIGN) || null,
|
|
104
|
+
indentLevel: indentLevelOf(host),
|
|
105
|
+
firstLineIndent: Boolean(styleValue(host, FIRST_LINE_INDENT)),
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function applyStyle(hosts, make) {
|
|
110
|
+
let changed = false;
|
|
111
|
+
for (const host of hosts) {
|
|
112
|
+
const patch = make(host);
|
|
113
|
+
if (!patch) continue;
|
|
114
|
+
for (const [prop, value] of Object.entries(patch)) {
|
|
115
|
+
if (styleValue(host, prop) === String(value || '')) continue;
|
|
116
|
+
setStyleValue(host, prop, value);
|
|
117
|
+
changed = true;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return changed;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const indentCss = (level) => (level > 0 ? `${level * INDENT_STEP_EM}em` : '');
|
|
124
|
+
|
|
125
|
+
export const setLineHeight = (root, value, ranges) => applyStyle(styleHosts(root, ranges), () => ({ [LINE_HEIGHT]: value || '' }));
|
|
126
|
+
export const setBlockAlign = (root, value, ranges) => applyStyle(styleHosts(root, ranges), () => ({ [TEXT_ALIGN]: value || '' }));
|
|
127
|
+
|
|
128
|
+
/** 增减一级:已达上限的块跳过,全部到顶返回 false(工具栏据此置灰) */
|
|
129
|
+
export function changeBlockIndent(root, dir, ranges) {
|
|
130
|
+
return applyStyle(styleHosts(root, ranges), (host) => {
|
|
131
|
+
const level = indentLevelOf(host);
|
|
132
|
+
const next = dir > 0 ? (level >= INDENT_MAX_LEVEL ? null : level + 1) : level > 0 ? level - 1 : null;
|
|
133
|
+
return next === null ? null : { [INDENT]: indentCss(next) };
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** 首行缩进开关(2 字符 = 2em,随字号走;导入文档的既有 text-indent 只按「有/无」参与开关) */
|
|
138
|
+
export function toggleFirstLineIndent(root, ranges) {
|
|
139
|
+
return applyStyle(styleHosts(root, ranges), (host) => ({
|
|
140
|
+
[FIRST_LINE_INDENT]: styleValue(host, FIRST_LINE_INDENT) ? '' : '2em',
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** 清除块级样式(行高 / 对齐 / 缩进) */
|
|
145
|
+
export function resetBlockStyle(root, ranges) {
|
|
146
|
+
return applyStyle(styleHosts(root, ranges), (host) => {
|
|
147
|
+
const dirty = BLOCK_STYLE_PROPS.some((prop) => styleValue(host, prop));
|
|
148
|
+
return dirty ? { [LINE_HEIGHT]: '', [TEXT_ALIGN]: '', [INDENT]: '' } : null;
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/* ===================== 块类型识别 ===================== */
|
|
153
|
+
|
|
154
|
+
export function kindOf(el) {
|
|
155
|
+
if (!el) return '';
|
|
156
|
+
const tag = el.tagName;
|
|
157
|
+
if (tag === 'H1' && el.classList.contains(DOC_CLASS.TITLE)) return 'title';
|
|
158
|
+
if (/^H[1-6]$/.test(tag)) return `heading${tag[1]}`;
|
|
159
|
+
if (tag === 'P') return 'paragraph';
|
|
160
|
+
if (tag === 'BLOCKQUOTE') return 'blockquote';
|
|
161
|
+
if (tag === 'PRE') return 'codeBlock';
|
|
162
|
+
if (tag === 'HR') return 'divider';
|
|
163
|
+
if (tag === 'IMG') return 'image';
|
|
164
|
+
if (tag === 'TABLE') return 'table';
|
|
165
|
+
if (tag === 'UL') return el.getAttribute('data-type') === 'taskList' ? 'taskList' : 'bulletList';
|
|
166
|
+
if (tag === 'OL') return 'orderedList';
|
|
167
|
+
if (tag === 'LI') return isTaskItem(el) ? 'taskItem' : 'listItem';
|
|
168
|
+
if (el.hasAttribute(CALLOUT_ATTR)) return 'callout';
|
|
169
|
+
if (el.hasAttribute(SUPER_ATTR)) return 'superBlock';
|
|
170
|
+
if (el.hasAttribute(COL_ATTR)) return 'column';
|
|
171
|
+
return '';
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** 光标所在块命中的列表类型(无列表返回 '') */
|
|
175
|
+
export const listKindOf = (block, root) => kindOf(listOf(block, root));
|
|
176
|
+
|
|
177
|
+
/* ===================== 结构基元 ===================== */
|
|
178
|
+
|
|
179
|
+
/** 把元素从其容器里提出来(容器随之空判定由调用方处理) */
|
|
180
|
+
function liftOutOf(el, container) {
|
|
181
|
+
if (!el || !container?.contains(el)) return;
|
|
182
|
+
container.before(el);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 单独提出一块,并保持正文顺序
|
|
187
|
+
* [坑] liftOutOf 恒置容器之前,只在「整容器逐块提出」的循环里才成立;
|
|
188
|
+
* 退格只提出其中一块时,提后半块会把它跳到引用前面(正文顺序被改)
|
|
189
|
+
*/
|
|
190
|
+
export function liftOneOutOf(container, el) {
|
|
191
|
+
if (!container?.contains(el) || el.parentElement !== container) {
|
|
192
|
+
liftOutOf(el, container);
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const kids = Array.from(container.children);
|
|
196
|
+
const tail = kids.slice(kids.indexOf(el) + 1);
|
|
197
|
+
if (!tail.length) container.after(el);
|
|
198
|
+
else if (kids.indexOf(el) === 0) container.before(el);
|
|
199
|
+
else {
|
|
200
|
+
// 中间块:后半截另起一个同型容器接在它后面,被提出的块夹在两段之间
|
|
201
|
+
const clone = container.cloneNode(false);
|
|
202
|
+
tail.forEach((node) => clone.appendChild(node));
|
|
203
|
+
container.after(el, clone);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** 空容器自底向上清理(拆完列表/引用后必做,否则正文里长出一堆空壳) */
|
|
208
|
+
export function pruneEmpty(parent) {
|
|
209
|
+
let el = parent;
|
|
210
|
+
while (el && el.tagName && el.nodeType === Node.ELEMENT_NODE) {
|
|
211
|
+
const isContainer = el.tagName === 'UL' || el.tagName === 'OL' || el.tagName === 'BLOCKQUOTE' || el.tagName === 'PRE' || el.tagName === 'LI' || isContainerBlock(el) || isColumnBlock(el);
|
|
212
|
+
if (!isContainer) break;
|
|
213
|
+
if (hasBlockBody(el)) break;
|
|
214
|
+
const next = el.parentElement;
|
|
215
|
+
el.remove();
|
|
216
|
+
el = next;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export const listItemOf = (el, root) => closestWithin(el, (node) => node.tagName === 'LI', root);
|
|
221
|
+
const listOf = (el, root) => closestWithin(el, (node) => node.tagName === 'UL' || node.tagName === 'OL', root);
|
|
222
|
+
export const quoteOf = (el, root) => closestWithin(el, (node) => node.tagName === 'BLOCKQUOTE', root);
|
|
223
|
+
|
|
224
|
+
/** 待办条目的正文宿主(li > label + div) */
|
|
225
|
+
export function taskContentHost(li) {
|
|
226
|
+
let div = Array.from(li.children).find((child) => child.tagName === 'DIV');
|
|
227
|
+
if (!div) {
|
|
228
|
+
div = document.createElement('div');
|
|
229
|
+
const label = li.querySelector('label');
|
|
230
|
+
for (const child of Array.from(li.childNodes)) if (child !== label) div.appendChild(child);
|
|
231
|
+
li.appendChild(div);
|
|
232
|
+
}
|
|
233
|
+
return div;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export function makeCheckbox() {
|
|
237
|
+
const label = document.createElement('label');
|
|
238
|
+
label.setAttribute('contenteditable', 'false');
|
|
239
|
+
const input = document.createElement('input');
|
|
240
|
+
input.type = 'checkbox';
|
|
241
|
+
label.appendChild(input);
|
|
242
|
+
return { label, input };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** 复制块级内联样式(转换 / 拆块都保形);data-* 方言属性一并搬(制表位定义等编解码标记挂在块上) */
|
|
246
|
+
export function copyBlockStyle(from, to) {
|
|
247
|
+
for (const prop of BLOCK_STYLE_PROPS) {
|
|
248
|
+
const value = styleValue(from, prop);
|
|
249
|
+
if (value) setStyleValue(to, prop, value);
|
|
250
|
+
}
|
|
251
|
+
for (const attr of Array.from(from.attributes)) {
|
|
252
|
+
if (attr.name.startsWith('data-') && !DROP_ATTRS.includes(attr.name)) to.setAttribute(attr.name, attr.value);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** 文本块 → li(待办形态带复选框壳与 div 正文宿主) */
|
|
257
|
+
function makeListItem(block, listKind) {
|
|
258
|
+
const li = document.createElement('li');
|
|
259
|
+
copyBlockStyle(block, li);
|
|
260
|
+
const content = document.createDocumentFragment();
|
|
261
|
+
moveChildren(block, content);
|
|
262
|
+
if (listKind === 'taskList') {
|
|
263
|
+
li.setAttribute('data-checked', 'false');
|
|
264
|
+
const { label } = makeCheckbox();
|
|
265
|
+
const div = document.createElement('div');
|
|
266
|
+
div.appendChild(content);
|
|
267
|
+
li.append(label, div);
|
|
268
|
+
} else {
|
|
269
|
+
li.appendChild(content);
|
|
270
|
+
}
|
|
271
|
+
return li;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** 相邻同类块打包成列表 */
|
|
275
|
+
function buildList(blocks, listKind) {
|
|
276
|
+
const list = document.createElement(listKind === 'orderedList' ? 'ol' : 'ul');
|
|
277
|
+
if (listKind === 'taskList') list.setAttribute('data-type', 'taskList');
|
|
278
|
+
wrapElements(blocks, list);
|
|
279
|
+
for (const block of blocks) {
|
|
280
|
+
list.appendChild(makeListItem(block, listKind));
|
|
281
|
+
block.remove();
|
|
282
|
+
}
|
|
283
|
+
return list;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** li → 独立段落:段落提到列表之前,列表空了随即删掉(留在列表内会被连表一起删) */
|
|
287
|
+
export function releaseListItem(li) {
|
|
288
|
+
const list = li.parentElement;
|
|
289
|
+
const block = document.createElement('p');
|
|
290
|
+
copyBlockStyle(li, block);
|
|
291
|
+
const source = isTaskItem(li) ? taskContentHost(li) : li;
|
|
292
|
+
moveChildren(source, block);
|
|
293
|
+
if (list && (list.tagName === 'UL' || list.tagName === 'OL')) {
|
|
294
|
+
list.before(block);
|
|
295
|
+
li.remove();
|
|
296
|
+
pruneEmpty(list);
|
|
297
|
+
} else {
|
|
298
|
+
li.replaceWith(block);
|
|
299
|
+
}
|
|
300
|
+
return block;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** 整表拆回段落(列表由 pruneEmpty 收走) */
|
|
304
|
+
function releaseList(list) {
|
|
305
|
+
return Array.from(list.children).filter((el) => el.tagName === 'LI').map((li) => releaseListItem(li));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** 代码块 → 若干段落(按换行拆) */
|
|
309
|
+
function preToParagraphs(pre) {
|
|
310
|
+
const text = (pre.querySelector('code') || pre).textContent || '';
|
|
311
|
+
const blocks = text.split('\n').map((line) => {
|
|
312
|
+
const p = document.createElement('p');
|
|
313
|
+
p.textContent = line;
|
|
314
|
+
return p;
|
|
315
|
+
});
|
|
316
|
+
const parent = pre.parentElement || pre.getRootNode();
|
|
317
|
+
blocks.forEach((block) => parent.insertBefore(block, pre));
|
|
318
|
+
pre.remove();
|
|
319
|
+
return blocks;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/* ===================== 块类型转换 ===================== */
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* 目标块转成指定类型
|
|
326
|
+
* @param {'paragraph'|'heading1'..'heading6'|'blockquote'|'codeBlock'|'bulletList'|'orderedList'|'taskList'|'callout'|'superBlock'} kind
|
|
327
|
+
* @param {Element[]|null} els 显式目标(块菜单「转换为」传打开时确定的块)
|
|
328
|
+
*/
|
|
329
|
+
export function setBlockKind(root, kind, els = null) {
|
|
330
|
+
const targets = structureTargets(root, null, els);
|
|
331
|
+
if (!targets.length) return false;
|
|
332
|
+
const heading = /^heading([1-6])$/.exec(kind);
|
|
333
|
+
|
|
334
|
+
// 文本块族:正文 / 文档标题(H1 + doc-title 类)/ 各级标题
|
|
335
|
+
if (kind === 'title' || kind === 'paragraph' || heading) {
|
|
336
|
+
const tag = kind === 'title' ? 'H1' : heading ? `H${heading[1]}` : 'P';
|
|
337
|
+
const wantTitle = kind === 'title';
|
|
338
|
+
let changed = false;
|
|
339
|
+
for (const target of [...targets]) changed = transformToText(target, tag, wantTitle) || changed;
|
|
340
|
+
return changed;
|
|
341
|
+
}
|
|
342
|
+
if (kind === 'blockquote') return toggleBlockquote(root, targets);
|
|
343
|
+
if (kind === 'codeBlock') return toggleCodeBlock(root, targets);
|
|
344
|
+
if (kind === 'bulletList' || kind === 'orderedList' || kind === 'taskList') return toggleList(root, targets, kind);
|
|
345
|
+
if (kind === 'callout') return toggleCallout(targets);
|
|
346
|
+
if (kind === 'superBlock') return toggleSuperBlock(targets);
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** 目标块变成指定文本块标签(先退出列表 / 引用 / 代码块) */
|
|
351
|
+
/** doc-title 类的增删(wantTitle=null 表示不动;返回是否改变) */
|
|
352
|
+
function applyTitleClass(el, wantTitle) {
|
|
353
|
+
if (!el || wantTitle === null) return false;
|
|
354
|
+
const has = el.classList.contains(DOC_CLASS.TITLE);
|
|
355
|
+
if (wantTitle === has) return false;
|
|
356
|
+
if (wantTitle) el.classList.add(DOC_CLASS.TITLE);
|
|
357
|
+
else el.classList.remove(DOC_CLASS.TITLE);
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/** 目标块变成指定文本块标签(先退出列表 / 引用 / 代码块);wantTitle 处理 doc-title 类 */
|
|
362
|
+
function transformToText(target, tag, wantTitle = null) {
|
|
363
|
+
if (!target.isConnected) return false;
|
|
364
|
+
if (target.tagName === 'HR' || target.tagName === 'IMG' || target.tagName === 'TABLE') return false;
|
|
365
|
+
let result = null;
|
|
366
|
+
if (target.tagName === 'PRE') {
|
|
367
|
+
const blocks = preToParagraphs(target);
|
|
368
|
+
result = tag !== 'P' ? renameBlock(blocks[0], tag) : blocks[0];
|
|
369
|
+
} else if (target.tagName === 'BLOCKQUOTE') {
|
|
370
|
+
const inner = Array.from(target.children);
|
|
371
|
+
inner.forEach((block) => liftOutOf(block, target));
|
|
372
|
+
target.remove();
|
|
373
|
+
result = tag !== 'P' && inner[0] ? renameBlock(inner[0], tag) : null;
|
|
374
|
+
} else if (target.tagName === 'LI') {
|
|
375
|
+
const block = releaseListItem(target);
|
|
376
|
+
result = tag !== 'P' ? renameBlock(block, tag) : block;
|
|
377
|
+
} else if (target.tagName === 'UL' || target.tagName === 'OL') {
|
|
378
|
+
const blocks = releaseList(target);
|
|
379
|
+
target.remove();
|
|
380
|
+
result = tag !== 'P' && blocks[0] ? renameBlock(blocks[0], tag) : null;
|
|
381
|
+
} else if (isCell(target)) {
|
|
382
|
+
return false;
|
|
383
|
+
} else if (isStyleableBlock(target)) {
|
|
384
|
+
result = renameBlock(target, tag);
|
|
385
|
+
if (result === target) {
|
|
386
|
+
// 标签没变也可能是类要变(标题1 ⇄ 文档标题)
|
|
387
|
+
return applyTitleClass(target, wantTitle);
|
|
388
|
+
}
|
|
389
|
+
} else {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
if (result) applyTitleClass(result, wantTitle);
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** 引用容器进 / 出 */
|
|
397
|
+
function toggleBlockquote(root, targets) {
|
|
398
|
+
if (targets.every((el) => el.tagName === 'BLOCKQUOTE')) {
|
|
399
|
+
for (const quote of targets) {
|
|
400
|
+
Array.from(quote.children).forEach((block) => liftOutOf(block, quote));
|
|
401
|
+
quote.remove();
|
|
402
|
+
}
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
// 列表 / 表格 / 段落都可以整块挂进引用,只有 li 与代码块要先换成合法形态
|
|
406
|
+
const group = [];
|
|
407
|
+
for (const target of targets) {
|
|
408
|
+
if (target.tagName === 'LI') group.push(releaseListItem(target));
|
|
409
|
+
else if (target.tagName === 'PRE') group.push(...preToParagraphs(target));
|
|
410
|
+
else if (target.tagName !== 'BLOCKQUOTE') group.push(target);
|
|
411
|
+
}
|
|
412
|
+
if (!group.length) return false;
|
|
413
|
+
const quote = document.createElement('blockquote');
|
|
414
|
+
wrapElements(group, quote);
|
|
415
|
+
return true;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** 代码块进 / 出:多块合并为一个代码块(换行连接) */
|
|
419
|
+
function toggleCodeBlock(root, targets) {
|
|
420
|
+
const pres = targets.filter((el) => el.tagName === 'PRE');
|
|
421
|
+
if (pres.length) {
|
|
422
|
+
pres.forEach(preToParagraphs);
|
|
423
|
+
return true;
|
|
424
|
+
}
|
|
425
|
+
const texts = targets.map((el) => (el.querySelector('code') || el).textContent || '');
|
|
426
|
+
const pre = document.createElement('pre');
|
|
427
|
+
const code = document.createElement('code');
|
|
428
|
+
code.textContent = texts.join('\n');
|
|
429
|
+
pre.appendChild(code);
|
|
430
|
+
targets[0].before(pre);
|
|
431
|
+
targets.forEach((el) => el.remove());
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** 列表进 / 出(同类型再点一次退出) */
|
|
436
|
+
function toggleList(root, targets, listKind) {
|
|
437
|
+
const lists = new Set();
|
|
438
|
+
const loose = [];
|
|
439
|
+
for (const target of targets) {
|
|
440
|
+
if (target.tagName === 'UL' || target.tagName === 'OL') lists.add(target);
|
|
441
|
+
else if (target.tagName === 'LI') lists.add(target.parentElement);
|
|
442
|
+
else loose.push(target);
|
|
443
|
+
}
|
|
444
|
+
if (lists.size && !loose.length && [...lists].every((list) => kindOf(list) === listKind)) {
|
|
445
|
+
[...lists].forEach((list) => releaseList(list));
|
|
446
|
+
return true;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
const blocks = [];
|
|
450
|
+
for (const target of [...targets]) {
|
|
451
|
+
if (target.tagName === 'UL' || target.tagName === 'OL') blocks.push(...releaseList(target));
|
|
452
|
+
else if (target.tagName === 'LI') blocks.push(releaseListItem(target));
|
|
453
|
+
else if (target.tagName === 'BLOCKQUOTE') {
|
|
454
|
+
const quote = target;
|
|
455
|
+
const inner = Array.from(quote.children);
|
|
456
|
+
inner.forEach((block) => liftOutOf(block, quote));
|
|
457
|
+
quote.remove();
|
|
458
|
+
blocks.push(...inner);
|
|
459
|
+
} else if (target.tagName === 'PRE') blocks.push(...preToParagraphs(target));
|
|
460
|
+
else if (target.tagName === 'P' || /^H[1-6]$/.test(target.tagName)) blocks.push(target);
|
|
461
|
+
}
|
|
462
|
+
const alive = blocks.filter((block) => block.isConnected);
|
|
463
|
+
if (!alive.length) return false;
|
|
464
|
+
|
|
465
|
+
// 相邻成组:不相邻的块各建一表,避免把不相干的块并成一个列表
|
|
466
|
+
let run = [];
|
|
467
|
+
const flush = () => {
|
|
468
|
+
if (run.length) buildList(run, listKind);
|
|
469
|
+
run = [];
|
|
470
|
+
};
|
|
471
|
+
for (const block of alive) {
|
|
472
|
+
if (run.length && block.previousElementSibling !== run[run.length - 1]) flush();
|
|
473
|
+
run.push(block);
|
|
474
|
+
}
|
|
475
|
+
flush();
|
|
476
|
+
return true;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
|
|
480
|
+
|
|
481
|
+
/* ===================== 容器块:高亮块 / 超级块 ===================== */
|
|
482
|
+
|
|
483
|
+
/** 向上找最外层「退出型」容器(高亮块 / 超级块;列透明——退出列即退出整个超级块)。
|
|
484
|
+
* [设计] blockquote 不纳入:它保持「回车在容器内续段」的既有语义,退出只走行首退格(与存量一致)。 */
|
|
485
|
+
export function noteContainerOf(block, root) {
|
|
486
|
+
let result = null;
|
|
487
|
+
let el = block?.parentElement;
|
|
488
|
+
while (el && el !== root && root.contains(el)) {
|
|
489
|
+
if (isContainerBlock(el)) result = el; // 高亮块/超级块(列 isColumnBlock 不命中,继续向上到超级块)
|
|
490
|
+
el = el.parentElement;
|
|
491
|
+
}
|
|
492
|
+
return result;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/** 退出型容器的「末尾文本块」:高亮块=最后一个子块;超级块=最后一列的最后一个子块(回车退出判定用) */
|
|
496
|
+
export function lastContainerBlock(container) {
|
|
497
|
+
if (!container) return null;
|
|
498
|
+
if (container.hasAttribute(SUPER_ATTR)) {
|
|
499
|
+
const col = container.lastElementChild;
|
|
500
|
+
return col && col.hasAttribute(COL_ATTR) ? col.lastElementChild : null;
|
|
501
|
+
}
|
|
502
|
+
return container.lastElementChild;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/** 高亮块进 / 出:目标全是高亮块则整体展开(子块提回原位);否则把其余块包进一个新高亮块(已是高亮块的原位不动,不揉合) */
|
|
506
|
+
function toggleCallout(targets) {
|
|
507
|
+
if (targets.every((el) => el.hasAttribute(CALLOUT_ATTR))) {
|
|
508
|
+
for (const box of targets) {
|
|
509
|
+
Array.from(box.children).forEach((block) => liftOutOf(block, box));
|
|
510
|
+
box.remove();
|
|
511
|
+
}
|
|
512
|
+
return true;
|
|
513
|
+
}
|
|
514
|
+
const group = targets.filter((el) => !el.hasAttribute(CALLOUT_ATTR));
|
|
515
|
+
if (!group.length) return false;
|
|
516
|
+
const box = document.createElement('div');
|
|
517
|
+
box.setAttribute(CALLOUT_ATTR, 'info');
|
|
518
|
+
wrapElements(group, box);
|
|
519
|
+
return true;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
/** 超级块进 / 出:目标全是超级块则按列顺序展开所有子块;否则把其余块各自成一列、并排进一个新超级块 */
|
|
523
|
+
function toggleSuperBlock(targets) {
|
|
524
|
+
if (targets.every((el) => el.hasAttribute(SUPER_ATTR))) {
|
|
525
|
+
for (const sup of targets) {
|
|
526
|
+
for (const col of Array.from(sup.children)) {
|
|
527
|
+
if (!col.hasAttribute(COL_ATTR)) continue;
|
|
528
|
+
for (const block of Array.from(col.children)) sup.before(block);
|
|
529
|
+
}
|
|
530
|
+
sup.remove();
|
|
531
|
+
}
|
|
532
|
+
return true;
|
|
533
|
+
}
|
|
534
|
+
const group = targets.filter(
|
|
535
|
+
(el) => !el.hasAttribute(SUPER_ATTR) && !el.hasAttribute(CALLOUT_ATTR) && !el.hasAttribute(COL_ATTR),
|
|
536
|
+
);
|
|
537
|
+
if (!group.length) return false;
|
|
538
|
+
const sup = document.createElement('div');
|
|
539
|
+
sup.setAttribute(SUPER_ATTR, '');
|
|
540
|
+
group[0].before(sup);
|
|
541
|
+
for (const el of group) {
|
|
542
|
+
const col = document.createElement('div');
|
|
543
|
+
col.setAttribute(COL_ATTR, '');
|
|
544
|
+
col.appendChild(el);
|
|
545
|
+
sup.appendChild(col);
|
|
546
|
+
}
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/* ===================== 块自定义属性(data-battrs JSON) ===================== */
|
|
551
|
+
|
|
552
|
+
/** 读块自定义属性(解析失败/非对象一律返回 {},绝不让坏 JSON 冒出去) */
|
|
553
|
+
export function readBlockAttrs(el) {
|
|
554
|
+
const raw = el?.getAttribute?.(BLOCK_ATTRS_ATTR);
|
|
555
|
+
if (!raw) return {};
|
|
556
|
+
try {
|
|
557
|
+
const obj = JSON.parse(raw);
|
|
558
|
+
return obj && typeof obj === 'object' && !Array.isArray(obj) ? obj : {};
|
|
559
|
+
} catch {
|
|
560
|
+
return {};
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** 写块自定义属性(键排序后序列化,保证同内容的字节稳定、避免无谓改写);
|
|
565
|
+
* 无变化返回 false(exec 据此不推历史)。 */
|
|
566
|
+
export function writeBlockAttrs(el, map) {
|
|
567
|
+
if (!el?.setAttribute) return false;
|
|
568
|
+
const clean = {};
|
|
569
|
+
for (const [k, v] of Object.entries(map || {})) {
|
|
570
|
+
const key = String(k).trim();
|
|
571
|
+
if (key) clean[key] = String(v ?? '');
|
|
572
|
+
}
|
|
573
|
+
const keys = Object.keys(clean).sort();
|
|
574
|
+
const next = keys.length ? JSON.stringify(clean, keys) : '';
|
|
575
|
+
const prev = el.getAttribute(BLOCK_ATTRS_ATTR) || '';
|
|
576
|
+
if (next === prev) return false;
|
|
577
|
+
if (next) el.setAttribute(BLOCK_ATTRS_ATTR, next);
|
|
578
|
+
else el.removeAttribute(BLOCK_ATTRS_ATTR);
|
|
579
|
+
return true;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
/** 光标所在的最外层结构块(块属性面板的「当前块」);选区不在本编辑面内返回 null(调用方据此保留上一处,不回退末块) */
|
|
583
|
+
export function currentStructureBlock(root) {
|
|
584
|
+
const range = rangeOf(root);
|
|
585
|
+
if (!range) return null;
|
|
586
|
+
const block = blockOf(range.startContainer, root);
|
|
587
|
+
if (!block) return null;
|
|
588
|
+
return structureTarget(block, root) || block;
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// 拆分后聚合出口:engine 内 `import * as B from './blocks.js'` 与按名导入的消费方无需感知内部文件划分
|
|
592
|
+
export * from './blocksInsert.js';
|
|
593
|
+
export * from './blocksStructure.js';
|
|
594
|
+
export * from './blocksEnter.js';
|