@a200888213/gbdocx 0.5.2 → 0.6.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/README.md +215 -207
- package/index.html +3 -2
- package/package.json +48 -48
- package/serve.mjs +3 -2
- package/src/app/file-io.js +122 -122
- package/src/app/main.js +9 -9
- package/src/app/word/catalog.js +52 -52
- package/src/app/word/comments.js +112 -112
- package/src/app/word/doc.js +113 -113
- package/src/app/word/edit.js +72 -72
- package/src/app/word/file.js +30 -1
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word/view.js +6 -2
- package/src/app/word-app.js +486 -469
- package/src/app/word-sheets.js +101 -101
- package/src/kernel/docx/codec/blocks.js +123 -123
- package/src/kernel/docx/codec/carry.js +129 -129
- package/src/kernel/docx/codec/comments.js +25 -25
- package/src/kernel/docx/codec/docx.js +198 -198
- package/src/kernel/docx/codec/docxStyles.js +177 -177
- package/src/kernel/docx/codec/fonts.js +195 -195
- package/src/kernel/docx/codec/helpers.js +161 -161
- package/src/kernel/docx/codec/hfHtml.js +152 -152
- package/src/kernel/docx/codec/html.js +739 -739
- package/src/kernel/docx/codec/losses.js +420 -420
- package/src/kernel/docx/codec/markdown.js +170 -170
- package/src/kernel/docx/codec/parts.js +249 -249
- package/src/kernel/docx/codec/read.js +603 -603
- package/src/kernel/docx/codec/render.js +1156 -1156
- package/src/kernel/docx/codec/roundtrip.js +236 -236
- package/src/kernel/docx/codec/write.js +833 -833
- package/src/kernel/docx/comments.js +14 -14
- package/src/kernel/docx/pageGeometry.js +123 -123
- package/src/kernel/docx/pagination.js +481 -481
- package/src/kernel/docx/watermark.js +45 -45
- package/src/kernel/editor/constants.js +8 -8
- package/src/kernel/editor/engine/blocks.js +594 -594
- package/src/kernel/editor/engine/blocksEnter.js +109 -109
- package/src/kernel/editor/engine/blocksInsert.js +254 -254
- package/src/kernel/editor/engine/blocksStructure.js +226 -226
- package/src/kernel/editor/engine/commands.js +181 -181
- package/src/kernel/editor/engine/dom.js +646 -646
- package/src/kernel/editor/engine/history.js +149 -149
- package/src/kernel/editor/engine/inline.js +656 -656
- package/src/kernel/editor/engine/input.js +554 -554
- package/src/kernel/editor/engine/interaction.js +137 -137
- package/src/kernel/editor/engine/pointer.js +39 -39
- package/src/kernel/editor/engine/revision.js +138 -138
- package/src/kernel/editor/engine/schema.js +417 -417
- package/src/kernel/editor/engine/selection.js +119 -119
- package/src/kernel/editor/engine/state.js +118 -118
- package/src/kernel/editor/engine/tableClip.js +154 -154
- package/src/kernel/editor/engine/tableGrid.js +55 -55
- package/src/kernel/editor/engine/tableInteraction.js +388 -388
- package/src/kernel/editor/engine/tableMenu.js +250 -250
- package/src/kernel/editor/engine/tableOps.js +174 -174
- package/src/kernel/editor/engine/tables.js +732 -732
- package/src/kernel/editor/overlayGeometry.js +20 -20
- package/src/kernel/editor/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +600 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +133 -133
- package/src/ui/find-bar.js +151 -151
- package/src/ui/icons.js +137 -135
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- package/src/ui/prompt.js +89 -89
- package/src/ui/ribbon.js +485 -485
- package/src/ui/special-chars.js +39 -39
- package/src/ui/statusbar.js +81 -81
- package/src/ui/toolbar.js +32 -32
- package/styles/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
|
@@ -1,119 +1,119 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·selection —— 内核内容与选区:序列化 / 程序化装载 / 选区捕获与复原(混入 EditorCore.prototype)
|
|
2
|
-
// [设计] 选区快照分两份:节点引用(startNode 等)只服务「本次命令的选区接续」,路径(start/end.path)供历史快照跨 DOM 重建,
|
|
3
|
-
// 历史不带 node,避免攥住脱离文档的节点。
|
|
4
|
-
import * as D from './dom.js';
|
|
5
|
-
import { normalizeBody, serializeBody } from './schema.js';
|
|
6
|
-
|
|
7
|
-
/** 内容与选区方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
8
|
-
export const selectionMethods = {
|
|
9
|
-
getHTML() {
|
|
10
|
-
return serializeBody(this.root);
|
|
11
|
-
},
|
|
12
|
-
|
|
13
|
-
/** 程序化装载(切档 / 外部写入):重置历史与选区 */
|
|
14
|
-
setHTML(html) {
|
|
15
|
-
const root = this.root;
|
|
16
|
-
this.applying = true;
|
|
17
|
-
root.innerHTML = html || '';
|
|
18
|
-
normalizeBody(root, { editable: true });
|
|
19
|
-
this.pending = null;
|
|
20
|
-
this.cellRect = null;
|
|
21
|
-
// 换文档后右键命中格/拖拽起点的旧元素都脱离 DOM,留着会让下一次表格动作命中失效
|
|
22
|
-
this.contextCell = null;
|
|
23
|
-
this.cellDragFrom = null;
|
|
24
|
-
this.clearAtom();
|
|
25
|
-
this.undoStack = [];
|
|
26
|
-
this.redoStack = [];
|
|
27
|
-
this.baseline = this.snapshot();
|
|
28
|
-
setTimeout(() => {
|
|
29
|
-
this.applying = false;
|
|
30
|
-
}, 0);
|
|
31
|
-
this.search.refresh();
|
|
32
|
-
this.syncState();
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* 按快照路径复原选区
|
|
37
|
-
* [坑] 块命令会整块换标签或包进列表,原光标所在的文本节点随之脱离文档;不补这一步,
|
|
38
|
-
* 用户点「标题 / 列表」之后光标就丢了,接着打字会落到编辑器外
|
|
39
|
-
*/
|
|
40
|
-
applyCaptured(sel) {
|
|
41
|
-
if (!sel?.start?.path) return;
|
|
42
|
-
const start = this.pointBlock(sel.startNode) || this.pathBlock(sel.start.path);
|
|
43
|
-
if (!start) return;
|
|
44
|
-
const end = this.pointBlock(sel.endNode) || this.pathBlock(sel.end?.path || []) || start;
|
|
45
|
-
D.selectRange(
|
|
46
|
-
this.root,
|
|
47
|
-
{ block: start, offset: this.offsetIn(start, sel.startNode, sel.startOffset, sel.start.offset) },
|
|
48
|
-
{ block: end, offset: this.offsetIn(end, sel.endNode, sel.endOffset, sel.end?.offset ?? sel.start.offset) }
|
|
49
|
-
);
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
/** 元素还在原位就用它,否则退回按路径找 */
|
|
53
|
-
pointBlock(node) {
|
|
54
|
-
if (!node || !this.root.contains(node)) return null;
|
|
55
|
-
return D.blockOf(node, this.root) || this.textBlockAt(node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement);
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
pathBlock(path) {
|
|
59
|
-
return this.textBlockAt(D.resolvePath(this.root, path));
|
|
60
|
-
},
|
|
61
|
-
|
|
62
|
-
offsetIn(block, node, nodeOffset, fallback) {
|
|
63
|
-
if (node && this.root.contains(node) && (block === node || block.contains(node))) return D.charOffset(block, node, nodeOffset);
|
|
64
|
-
return fallback ?? 0;
|
|
65
|
-
},
|
|
66
|
-
|
|
67
|
-
/** 路径落点可能已是容器(包进列表 / 引用后),要下沉到真正的文本块 */
|
|
68
|
-
textBlockAt(el) {
|
|
69
|
-
if (!el || !this.root.contains(el)) return null;
|
|
70
|
-
return D.isTextBlock(el, this.root) ? el : D.firstTextBlockIn(el, this.root);
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
isFocused() {
|
|
74
|
-
return document.activeElement === this.root || this.root.contains(document.activeElement);
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
focus() {
|
|
78
|
-
this.root.focus({ preventScroll: true });
|
|
79
|
-
if (!D.rangeOf(this.root) && this.savedRange) this.applyCaptured(this.savedRange);
|
|
80
|
-
},
|
|
81
|
-
|
|
82
|
-
selectionRanges() {
|
|
83
|
-
return D.blocksInRange(this.root);
|
|
84
|
-
},
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 程序化改动后把选区接回原位
|
|
88
|
-
* [坑] 两种塌法都要接:① 整块换标签后浏览器把选区挪到父节点(丢文本块);
|
|
89
|
-
* ② 行内重建后选区被压成折叠光标(再点一次同一个标记会因为「选区没了」而取消不掉)
|
|
90
|
-
*/
|
|
91
|
-
keepSelection(beforeSel) {
|
|
92
|
-
if (!beforeSel?.start) return;
|
|
93
|
-
const live = D.rangeOf(this.root);
|
|
94
|
-
const liveBlock = live && D.blockOf(live.startContainer, this.root);
|
|
95
|
-
if (!liveBlock) {
|
|
96
|
-
this.applyCaptured(beforeSel);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
const wasRange = beforeSel.end && (beforeSel.end.path.join() !== beforeSel.start.path.join() || beforeSel.end.offset !== beforeSel.start.offset);
|
|
100
|
-
if (wasRange && live.collapsed) this.applyCaptured(beforeSel);
|
|
101
|
-
},
|
|
102
|
-
|
|
103
|
-
/** 当前选区(含折叠光标);工具栏抢走焦点后据此复原 */
|
|
104
|
-
captureSelection() {
|
|
105
|
-
const range = D.rangeOf(this.root);
|
|
106
|
-
if (!range) return null;
|
|
107
|
-
const start = blockPoint(this.root, range.startContainer, range.startOffset);
|
|
108
|
-
const end = blockPoint(this.root, range.endContainer, range.endOffset);
|
|
109
|
-
if (!start) return null;
|
|
110
|
-
// 节点引用只服务本次命令的选区接续(历史快照另存,不带 node,避免攥住脱离文档的节点)
|
|
111
|
-
return { start, end, startNode: range.startContainer, startOffset: range.startOffset, endNode: range.endContainer, endOffset: range.endOffset };
|
|
112
|
-
},
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
function blockPoint(root, node, offset) {
|
|
116
|
-
const block = D.blockOf(node, root);
|
|
117
|
-
if (!block) return null;
|
|
118
|
-
return { path: D.pathOf(block, root), offset: D.charOffset(block, node, offset) };
|
|
119
|
-
}
|
|
1
|
+
// [文件] web·editor-kernel·engine·selection —— 内核内容与选区:序列化 / 程序化装载 / 选区捕获与复原(混入 EditorCore.prototype)
|
|
2
|
+
// [设计] 选区快照分两份:节点引用(startNode 等)只服务「本次命令的选区接续」,路径(start/end.path)供历史快照跨 DOM 重建,
|
|
3
|
+
// 历史不带 node,避免攥住脱离文档的节点。
|
|
4
|
+
import * as D from './dom.js';
|
|
5
|
+
import { normalizeBody, serializeBody } from './schema.js';
|
|
6
|
+
|
|
7
|
+
/** 内容与选区方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
8
|
+
export const selectionMethods = {
|
|
9
|
+
getHTML() {
|
|
10
|
+
return serializeBody(this.root);
|
|
11
|
+
},
|
|
12
|
+
|
|
13
|
+
/** 程序化装载(切档 / 外部写入):重置历史与选区 */
|
|
14
|
+
setHTML(html) {
|
|
15
|
+
const root = this.root;
|
|
16
|
+
this.applying = true;
|
|
17
|
+
root.innerHTML = html || '';
|
|
18
|
+
normalizeBody(root, { editable: true });
|
|
19
|
+
this.pending = null;
|
|
20
|
+
this.cellRect = null;
|
|
21
|
+
// 换文档后右键命中格/拖拽起点的旧元素都脱离 DOM,留着会让下一次表格动作命中失效
|
|
22
|
+
this.contextCell = null;
|
|
23
|
+
this.cellDragFrom = null;
|
|
24
|
+
this.clearAtom();
|
|
25
|
+
this.undoStack = [];
|
|
26
|
+
this.redoStack = [];
|
|
27
|
+
this.baseline = this.snapshot();
|
|
28
|
+
setTimeout(() => {
|
|
29
|
+
this.applying = false;
|
|
30
|
+
}, 0);
|
|
31
|
+
this.search.refresh();
|
|
32
|
+
this.syncState();
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 按快照路径复原选区
|
|
37
|
+
* [坑] 块命令会整块换标签或包进列表,原光标所在的文本节点随之脱离文档;不补这一步,
|
|
38
|
+
* 用户点「标题 / 列表」之后光标就丢了,接着打字会落到编辑器外
|
|
39
|
+
*/
|
|
40
|
+
applyCaptured(sel) {
|
|
41
|
+
if (!sel?.start?.path) return;
|
|
42
|
+
const start = this.pointBlock(sel.startNode) || this.pathBlock(sel.start.path);
|
|
43
|
+
if (!start) return;
|
|
44
|
+
const end = this.pointBlock(sel.endNode) || this.pathBlock(sel.end?.path || []) || start;
|
|
45
|
+
D.selectRange(
|
|
46
|
+
this.root,
|
|
47
|
+
{ block: start, offset: this.offsetIn(start, sel.startNode, sel.startOffset, sel.start.offset) },
|
|
48
|
+
{ block: end, offset: this.offsetIn(end, sel.endNode, sel.endOffset, sel.end?.offset ?? sel.start.offset) }
|
|
49
|
+
);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
/** 元素还在原位就用它,否则退回按路径找 */
|
|
53
|
+
pointBlock(node) {
|
|
54
|
+
if (!node || !this.root.contains(node)) return null;
|
|
55
|
+
return D.blockOf(node, this.root) || this.textBlockAt(node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement);
|
|
56
|
+
},
|
|
57
|
+
|
|
58
|
+
pathBlock(path) {
|
|
59
|
+
return this.textBlockAt(D.resolvePath(this.root, path));
|
|
60
|
+
},
|
|
61
|
+
|
|
62
|
+
offsetIn(block, node, nodeOffset, fallback) {
|
|
63
|
+
if (node && this.root.contains(node) && (block === node || block.contains(node))) return D.charOffset(block, node, nodeOffset);
|
|
64
|
+
return fallback ?? 0;
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/** 路径落点可能已是容器(包进列表 / 引用后),要下沉到真正的文本块 */
|
|
68
|
+
textBlockAt(el) {
|
|
69
|
+
if (!el || !this.root.contains(el)) return null;
|
|
70
|
+
return D.isTextBlock(el, this.root) ? el : D.firstTextBlockIn(el, this.root);
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
isFocused() {
|
|
74
|
+
return document.activeElement === this.root || this.root.contains(document.activeElement);
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
focus() {
|
|
78
|
+
this.root.focus({ preventScroll: true });
|
|
79
|
+
if (!D.rangeOf(this.root) && this.savedRange) this.applyCaptured(this.savedRange);
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
selectionRanges() {
|
|
83
|
+
return D.blocksInRange(this.root);
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 程序化改动后把选区接回原位
|
|
88
|
+
* [坑] 两种塌法都要接:① 整块换标签后浏览器把选区挪到父节点(丢文本块);
|
|
89
|
+
* ② 行内重建后选区被压成折叠光标(再点一次同一个标记会因为「选区没了」而取消不掉)
|
|
90
|
+
*/
|
|
91
|
+
keepSelection(beforeSel) {
|
|
92
|
+
if (!beforeSel?.start) return;
|
|
93
|
+
const live = D.rangeOf(this.root);
|
|
94
|
+
const liveBlock = live && D.blockOf(live.startContainer, this.root);
|
|
95
|
+
if (!liveBlock) {
|
|
96
|
+
this.applyCaptured(beforeSel);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
const wasRange = beforeSel.end && (beforeSel.end.path.join() !== beforeSel.start.path.join() || beforeSel.end.offset !== beforeSel.start.offset);
|
|
100
|
+
if (wasRange && live.collapsed) this.applyCaptured(beforeSel);
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
/** 当前选区(含折叠光标);工具栏抢走焦点后据此复原 */
|
|
104
|
+
captureSelection() {
|
|
105
|
+
const range = D.rangeOf(this.root);
|
|
106
|
+
if (!range) return null;
|
|
107
|
+
const start = blockPoint(this.root, range.startContainer, range.startOffset);
|
|
108
|
+
const end = blockPoint(this.root, range.endContainer, range.endOffset);
|
|
109
|
+
if (!start) return null;
|
|
110
|
+
// 节点引用只服务本次命令的选区接续(历史快照另存,不带 node,避免攥住脱离文档的节点)
|
|
111
|
+
return { start, end, startNode: range.startContainer, startOffset: range.startOffset, endNode: range.endContainer, endOffset: range.endOffset };
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
function blockPoint(root, node, offset) {
|
|
116
|
+
const block = D.blockOf(node, root);
|
|
117
|
+
if (!block) return null;
|
|
118
|
+
return { path: D.pathOf(block, root), offset: D.charOffset(block, node, offset) };
|
|
119
|
+
}
|
|
@@ -1,118 +1,118 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·state —— 内核状态快照:工具栏 / 气泡菜单 / 块菜单共用的单一口径(混入 EditorCore.prototype)
|
|
2
|
-
// [设计] 状态由内核一次算出(state()),各 UI 只消费快照,不再各自推测激活态。
|
|
3
|
-
import * as D from './dom.js';
|
|
4
|
-
import * as I from './inline.js';
|
|
5
|
-
import * as B from './blocks.js';
|
|
6
|
-
import { INDENT_MAX_LEVEL } from '../constants.js';
|
|
7
|
-
|
|
8
|
-
/** 状态快照方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
9
|
-
export const stateMethods = {
|
|
10
|
-
inlineSnapshot() {
|
|
11
|
-
const ranges = this.selectionRanges();
|
|
12
|
-
return I.inlineState(this.root, ranges).marks;
|
|
13
|
-
},
|
|
14
|
-
|
|
15
|
-
/** 工具栏 / 气泡菜单 / 块菜单共用的状态快照(单一口径) */
|
|
16
|
-
state() {
|
|
17
|
-
const root = this.root;
|
|
18
|
-
const ranges = this.selectionRanges();
|
|
19
|
-
// 折叠光标时待定标记就是「此处接下来打的字长什么样」,工具栏亮灭必须同源,否则按了加粗按钮不亮
|
|
20
|
-
const marks = (ranges.length && this.pending) || I.inlineState(root, ranges).marks;
|
|
21
|
-
const hosts = B.styleHosts(root, ranges);
|
|
22
|
-
const host = hosts[0] || null;
|
|
23
|
-
const style = host ? B.blockStyleOf(host) : { lineHeight: '', textAlign: null, indentLevel: 0, firstLineIndent: false };
|
|
24
|
-
const block = ranges[0]?.block || null;
|
|
25
|
-
const structure = block ? D.structureTarget(block, root) : null;
|
|
26
|
-
const kind = B.kindOf(structure);
|
|
27
|
-
const listKind = block ? B.listKindOf(block, root) : '';
|
|
28
|
-
const inTable = !!D.tableOf(block || root, root);
|
|
29
|
-
const headingLevel = Number(/^heading([1-6])$/.exec(kind)?.[1]) || 0;
|
|
30
|
-
const text = root.textContent.replace(/[\n]/g, '');
|
|
31
|
-
return {
|
|
32
|
-
ready: true,
|
|
33
|
-
canUndo: this.undoStack.length > 0,
|
|
34
|
-
canRedo: this.redoStack.length > 0,
|
|
35
|
-
bold: marks.bold,
|
|
36
|
-
italic: marks.italic,
|
|
37
|
-
underline: marks.underline,
|
|
38
|
-
strike: marks.strike,
|
|
39
|
-
code: marks.code,
|
|
40
|
-
superscript: marks.superscript,
|
|
41
|
-
subscript: marks.subscript,
|
|
42
|
-
link: Boolean(marks.link),
|
|
43
|
-
linkHref: marks.link || '',
|
|
44
|
-
color: marks.color,
|
|
45
|
-
backgroundColor: marks.backgroundColor,
|
|
46
|
-
fontSize: marks.fontSize,
|
|
47
|
-
fontFamily: marks.fontFamily,
|
|
48
|
-
textStroke: marks.textStroke,
|
|
49
|
-
textShadow: marks.textShadow,
|
|
50
|
-
// 结构标记与建议修改模式(Word 应用消费;普通富文本忽略)
|
|
51
|
-
rev: marks.rev || '',
|
|
52
|
-
cmt: marks.cmt || '',
|
|
53
|
-
suggesting: this.suggesting,
|
|
54
|
-
editable: this.isEditable(),
|
|
55
|
-
blockType: kind === 'title' ? 'title' : headingLevel ? `heading${headingLevel}` : kind === 'codeBlock' || kind === 'blockquote' || kind === 'callout' || kind === 'superBlock' ? kind : 'paragraph',
|
|
56
|
-
headingLevel,
|
|
57
|
-
lineHeight: style.lineHeight,
|
|
58
|
-
textAlign: style.textAlign || 'left',
|
|
59
|
-
textAlignRaw: style.textAlign,
|
|
60
|
-
indentLevel: style.indentLevel,
|
|
61
|
-
firstLineIndent: style.firstLineIndent,
|
|
62
|
-
canIndent: style.indentLevel < INDENT_MAX_LEVEL,
|
|
63
|
-
canOutdent: style.indentLevel > 0,
|
|
64
|
-
blockquote: kind === 'blockquote',
|
|
65
|
-
codeBlock: kind === 'codeBlock',
|
|
66
|
-
bulletList: listKind === 'bulletList',
|
|
67
|
-
orderedList: listKind === 'orderedList',
|
|
68
|
-
taskList: listKind === 'taskList',
|
|
69
|
-
inTable,
|
|
70
|
-
hasSelection: ranges.some((item) => item.end > item.start),
|
|
71
|
-
isEmpty: !text.trim() && !root.querySelector('img'),
|
|
72
|
-
// [设计] line/column 是给状态栏补的纯派生读数:由选区几何与字符偏移即时算出,
|
|
73
|
-
// 不参与任何编辑逻辑(命令/历史/序列化都不读它),故新增字段对既有消费方零影响。
|
|
74
|
-
...this.caretPosition(style),
|
|
75
|
-
};
|
|
76
|
-
},
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 光标位置读数:line = 视觉行号(从 1 起),column = 当前段落内字符偏移(从 1 起,即光标前字符数)
|
|
80
|
-
* [设计] 纯读:只测量不写 DOM;取不到(未挂载 / root 为 null / 无选区 / 无 caretRect)时两者回退 0,绝不抛错
|
|
81
|
-
* @param {{lineHeight:string}} style state() 已算好的块样式(复用,避免重复计算)
|
|
82
|
-
* @returns {{line:number, column:number}}
|
|
83
|
-
*/
|
|
84
|
-
caretPosition(style) {
|
|
85
|
-
const fallback = { line: 0, column: 0 };
|
|
86
|
-
const root = this.root;
|
|
87
|
-
if (!root) return fallback;
|
|
88
|
-
try {
|
|
89
|
-
const rect = this.caretRect();
|
|
90
|
-
const rootRect = root.getBoundingClientRect();
|
|
91
|
-
let line = 0;
|
|
92
|
-
if (rect && rootRect) {
|
|
93
|
-
let lineHeightPx = parseFloat(style?.lineHeight);
|
|
94
|
-
if (!Number.isFinite(lineHeightPx) || lineHeightPx <= 0) lineHeightPx = parseFloat(getComputedStyle(root).lineHeight);
|
|
95
|
-
if (!Number.isFinite(lineHeightPx) || lineHeightPx <= 0) lineHeightPx = 16;
|
|
96
|
-
// caretRect().top 与 rootRect.top 同受外层 CSS zoom 影响,相减/相除比值不变,故无需乘除 zoom 系数
|
|
97
|
-
const relTop = rect.top - rootRect.top;
|
|
98
|
-
line = Math.floor(relTop / lineHeightPx) + 1;
|
|
99
|
-
if (line < 1) line = 1;
|
|
100
|
-
}
|
|
101
|
-
const first = this.selectionRanges()[0];
|
|
102
|
-
const column = first ? Math.max(0, Math.round(first.start)) + 1 : 0;
|
|
103
|
-
return { line, column };
|
|
104
|
-
} catch {
|
|
105
|
-
return fallback;
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
syncState() {
|
|
110
|
-
this.emit('state', this.state());
|
|
111
|
-
},
|
|
112
|
-
|
|
113
|
-
/** 块自定义属性(el 缺省=光标所在最外层结构块,与 setBlockAttrs 命令同口径;元素已不在编辑面内返回 null) */
|
|
114
|
-
blockAttrsOf(el = null) {
|
|
115
|
-
const target = (el && B.liveTarget(this.root, el)) || B.currentStructureBlock(this.root);
|
|
116
|
-
return target ? B.readBlockAttrs(target) : null;
|
|
117
|
-
},
|
|
118
|
-
};
|
|
1
|
+
// [文件] web·editor-kernel·engine·state —— 内核状态快照:工具栏 / 气泡菜单 / 块菜单共用的单一口径(混入 EditorCore.prototype)
|
|
2
|
+
// [设计] 状态由内核一次算出(state()),各 UI 只消费快照,不再各自推测激活态。
|
|
3
|
+
import * as D from './dom.js';
|
|
4
|
+
import * as I from './inline.js';
|
|
5
|
+
import * as B from './blocks.js';
|
|
6
|
+
import { INDENT_MAX_LEVEL } from '../constants.js';
|
|
7
|
+
|
|
8
|
+
/** 状态快照方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
9
|
+
export const stateMethods = {
|
|
10
|
+
inlineSnapshot() {
|
|
11
|
+
const ranges = this.selectionRanges();
|
|
12
|
+
return I.inlineState(this.root, ranges).marks;
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
/** 工具栏 / 气泡菜单 / 块菜单共用的状态快照(单一口径) */
|
|
16
|
+
state() {
|
|
17
|
+
const root = this.root;
|
|
18
|
+
const ranges = this.selectionRanges();
|
|
19
|
+
// 折叠光标时待定标记就是「此处接下来打的字长什么样」,工具栏亮灭必须同源,否则按了加粗按钮不亮
|
|
20
|
+
const marks = (ranges.length && this.pending) || I.inlineState(root, ranges).marks;
|
|
21
|
+
const hosts = B.styleHosts(root, ranges);
|
|
22
|
+
const host = hosts[0] || null;
|
|
23
|
+
const style = host ? B.blockStyleOf(host) : { lineHeight: '', textAlign: null, indentLevel: 0, firstLineIndent: false };
|
|
24
|
+
const block = ranges[0]?.block || null;
|
|
25
|
+
const structure = block ? D.structureTarget(block, root) : null;
|
|
26
|
+
const kind = B.kindOf(structure);
|
|
27
|
+
const listKind = block ? B.listKindOf(block, root) : '';
|
|
28
|
+
const inTable = !!D.tableOf(block || root, root);
|
|
29
|
+
const headingLevel = Number(/^heading([1-6])$/.exec(kind)?.[1]) || 0;
|
|
30
|
+
const text = root.textContent.replace(/[\n]/g, '');
|
|
31
|
+
return {
|
|
32
|
+
ready: true,
|
|
33
|
+
canUndo: this.undoStack.length > 0,
|
|
34
|
+
canRedo: this.redoStack.length > 0,
|
|
35
|
+
bold: marks.bold,
|
|
36
|
+
italic: marks.italic,
|
|
37
|
+
underline: marks.underline,
|
|
38
|
+
strike: marks.strike,
|
|
39
|
+
code: marks.code,
|
|
40
|
+
superscript: marks.superscript,
|
|
41
|
+
subscript: marks.subscript,
|
|
42
|
+
link: Boolean(marks.link),
|
|
43
|
+
linkHref: marks.link || '',
|
|
44
|
+
color: marks.color,
|
|
45
|
+
backgroundColor: marks.backgroundColor,
|
|
46
|
+
fontSize: marks.fontSize,
|
|
47
|
+
fontFamily: marks.fontFamily,
|
|
48
|
+
textStroke: marks.textStroke,
|
|
49
|
+
textShadow: marks.textShadow,
|
|
50
|
+
// 结构标记与建议修改模式(Word 应用消费;普通富文本忽略)
|
|
51
|
+
rev: marks.rev || '',
|
|
52
|
+
cmt: marks.cmt || '',
|
|
53
|
+
suggesting: this.suggesting,
|
|
54
|
+
editable: this.isEditable(),
|
|
55
|
+
blockType: kind === 'title' ? 'title' : headingLevel ? `heading${headingLevel}` : kind === 'codeBlock' || kind === 'blockquote' || kind === 'callout' || kind === 'superBlock' ? kind : 'paragraph',
|
|
56
|
+
headingLevel,
|
|
57
|
+
lineHeight: style.lineHeight,
|
|
58
|
+
textAlign: style.textAlign || 'left',
|
|
59
|
+
textAlignRaw: style.textAlign,
|
|
60
|
+
indentLevel: style.indentLevel,
|
|
61
|
+
firstLineIndent: style.firstLineIndent,
|
|
62
|
+
canIndent: style.indentLevel < INDENT_MAX_LEVEL,
|
|
63
|
+
canOutdent: style.indentLevel > 0,
|
|
64
|
+
blockquote: kind === 'blockquote',
|
|
65
|
+
codeBlock: kind === 'codeBlock',
|
|
66
|
+
bulletList: listKind === 'bulletList',
|
|
67
|
+
orderedList: listKind === 'orderedList',
|
|
68
|
+
taskList: listKind === 'taskList',
|
|
69
|
+
inTable,
|
|
70
|
+
hasSelection: ranges.some((item) => item.end > item.start),
|
|
71
|
+
isEmpty: !text.trim() && !root.querySelector('img'),
|
|
72
|
+
// [设计] line/column 是给状态栏补的纯派生读数:由选区几何与字符偏移即时算出,
|
|
73
|
+
// 不参与任何编辑逻辑(命令/历史/序列化都不读它),故新增字段对既有消费方零影响。
|
|
74
|
+
...this.caretPosition(style),
|
|
75
|
+
};
|
|
76
|
+
},
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* 光标位置读数:line = 视觉行号(从 1 起),column = 当前段落内字符偏移(从 1 起,即光标前字符数)
|
|
80
|
+
* [设计] 纯读:只测量不写 DOM;取不到(未挂载 / root 为 null / 无选区 / 无 caretRect)时两者回退 0,绝不抛错
|
|
81
|
+
* @param {{lineHeight:string}} style state() 已算好的块样式(复用,避免重复计算)
|
|
82
|
+
* @returns {{line:number, column:number}}
|
|
83
|
+
*/
|
|
84
|
+
caretPosition(style) {
|
|
85
|
+
const fallback = { line: 0, column: 0 };
|
|
86
|
+
const root = this.root;
|
|
87
|
+
if (!root) return fallback;
|
|
88
|
+
try {
|
|
89
|
+
const rect = this.caretRect();
|
|
90
|
+
const rootRect = root.getBoundingClientRect();
|
|
91
|
+
let line = 0;
|
|
92
|
+
if (rect && rootRect) {
|
|
93
|
+
let lineHeightPx = parseFloat(style?.lineHeight);
|
|
94
|
+
if (!Number.isFinite(lineHeightPx) || lineHeightPx <= 0) lineHeightPx = parseFloat(getComputedStyle(root).lineHeight);
|
|
95
|
+
if (!Number.isFinite(lineHeightPx) || lineHeightPx <= 0) lineHeightPx = 16;
|
|
96
|
+
// caretRect().top 与 rootRect.top 同受外层 CSS zoom 影响,相减/相除比值不变,故无需乘除 zoom 系数
|
|
97
|
+
const relTop = rect.top - rootRect.top;
|
|
98
|
+
line = Math.floor(relTop / lineHeightPx) + 1;
|
|
99
|
+
if (line < 1) line = 1;
|
|
100
|
+
}
|
|
101
|
+
const first = this.selectionRanges()[0];
|
|
102
|
+
const column = first ? Math.max(0, Math.round(first.start)) + 1 : 0;
|
|
103
|
+
return { line, column };
|
|
104
|
+
} catch {
|
|
105
|
+
return fallback;
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
syncState() {
|
|
110
|
+
this.emit('state', this.state());
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
/** 块自定义属性(el 缺省=光标所在最外层结构块,与 setBlockAttrs 命令同口径;元素已不在编辑面内返回 null) */
|
|
114
|
+
blockAttrsOf(el = null) {
|
|
115
|
+
const target = (el && B.liveTarget(this.root, el)) || B.currentStructureBlock(this.root);
|
|
116
|
+
return target ? B.readBlockAttrs(target) : null;
|
|
117
|
+
},
|
|
118
|
+
};
|