@a200888213/gbdocx 0.6.1 → 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/package.json +1 -1
- 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/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-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/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
- package/styles/word-panel.css +1 -1
|
@@ -1,137 +1,137 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·interaction —— 编辑面交互事件处理器(焦点/选区变化/点击原子块/待办勾选)(混入 EditorCore.prototype)
|
|
2
|
-
// [约束] 带 `on*` 前缀的成员是直接挂到编辑面上的事件处理器:由 editor.js 构造时统一 bind(this),
|
|
3
|
-
// 以便 addEventListener / removeEventListener 拿到同一引用(等价原箭头函数字段的绑定语义)。
|
|
4
|
-
import * as D from './dom.js';
|
|
5
|
-
import * as I from './inline.js';
|
|
6
|
-
|
|
7
|
-
/** 需在构造时 bind(this) 的事件处理器名(挂 root/document 监听,必须引用稳定) */
|
|
8
|
-
export const INTERACTION_HANDLERS = ['onFocus', 'onBlur', 'onSelectionChange', 'onPointerState', 'onClick', 'onCheckboxChange'];
|
|
9
|
-
|
|
10
|
-
/** 交互事件方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
11
|
-
export const interactionMethods = {
|
|
12
|
-
onFocus() {
|
|
13
|
-
this.emit('focus');
|
|
14
|
-
},
|
|
15
|
-
|
|
16
|
-
onBlur() {
|
|
17
|
-
const sel = this.captureSelection();
|
|
18
|
-
if (sel) this.savedRange = sel;
|
|
19
|
-
this.clearBlockSelection();
|
|
20
|
-
this.emit('blur');
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
onSelectionChange() {
|
|
24
|
-
if (!this.isFocused()) return;
|
|
25
|
-
const sel = this.captureSelection();
|
|
26
|
-
if (sel) this.savedRange = sel;
|
|
27
|
-
const range = D.rangeOf(this.root);
|
|
28
|
-
this.pending = range && range.collapsed ? { ...this.inlineSnapshot() } : null;
|
|
29
|
-
this.syncBlockSelection();
|
|
30
|
-
this.syncState();
|
|
31
|
-
this.emit('selection');
|
|
32
|
-
this.emit('selectionUpdate');
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
/** 清掉选区跨块高亮(运行期类) */
|
|
36
|
-
clearBlockSelection() {
|
|
37
|
-
for (const el of Array.from(this.root.querySelectorAll(`.${D.BLOCK_SELECTED}`))) el.classList.remove(D.BLOCK_SELECTED);
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 选区跨块高亮:非折叠选区覆盖到的顶层块整块加淡蓝底(与块标悬浮高亮 gbd-is-block-hover 各管各的)。
|
|
42
|
-
* [设计] 用顶层块(root.children)作高亮单位:选区落在引用/高亮块内部的段落时,高亮的是整个容器,与用户「选中的块」直觉一致。
|
|
43
|
-
* [坑] 桌面式框选(blockMarqueeActive)期间高亮归框选管,本方法直接让位,否则会被每次 selectionchange 抹掉。
|
|
44
|
-
*/
|
|
45
|
-
syncBlockSelection() {
|
|
46
|
-
if (this.blockMarqueeActive) return;
|
|
47
|
-
this.clearBlockSelection();
|
|
48
|
-
const range = D.rangeOf(this.root);
|
|
49
|
-
if (!range || range.collapsed) return;
|
|
50
|
-
for (const el of Array.from(this.root.children)) {
|
|
51
|
-
if (D.isRuntimeBlock(el)) continue;
|
|
52
|
-
if (range.intersectsNode(el)) el.classList.add(D.BLOCK_SELECTED);
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* 桌面式框选:按客户端矩形给被覆盖到的顶层块打选区高亮(不产生原生 Range,纯视觉块选)。
|
|
58
|
-
* [设计] 用客户端坐标做命中(getBoundingClientRect 同基准),宿主不必换算缩放;
|
|
59
|
-
* 每次调用全量重算并 toggle(移出框的块自动撤高亮),返回命中数供宿主决定是否显示「已选 N 块」。
|
|
60
|
-
*/
|
|
61
|
-
marqueeSelect(clientRect) {
|
|
62
|
-
let hits = 0;
|
|
63
|
-
for (const el of Array.from(this.root.children)) {
|
|
64
|
-
if (D.isRuntimeBlock(el)) continue;
|
|
65
|
-
const r = el.getBoundingClientRect();
|
|
66
|
-
const hit = r.width > 0 && r.height > 0 && !(r.left > clientRect.right || r.right < clientRect.left || r.top > clientRect.bottom || r.bottom < clientRect.top);
|
|
67
|
-
el.classList.toggle(D.BLOCK_SELECTED, hit);
|
|
68
|
-
if (hit) hits += 1;
|
|
69
|
-
}
|
|
70
|
-
return hits;
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
onPointerState() {
|
|
74
|
-
this.syncState();
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
/** 折叠光标 → 待定标记态(工具栏切了标记再打字才生效,与主流编辑器一致)。
|
|
78
|
-
* [坑] 判据必须是「与光标处当前格式不同」:在加粗文字里关掉加粗时 pending 全是 false,
|
|
79
|
-
* 只看真值会当成没有待定标记,浏览器直接把新字插进加粗壳里 */
|
|
80
|
-
hasPendingMarks() {
|
|
81
|
-
const marks = this.pending;
|
|
82
|
-
if (!marks) return false;
|
|
83
|
-
const current = this.inlineSnapshot();
|
|
84
|
-
const same = (a, b) => String(a ?? '').trim().toLowerCase() === String(b ?? '').trim().toLowerCase();
|
|
85
|
-
return I.TAG_MARKS.some((key) => Boolean(marks[key]) !== Boolean(current[key]))
|
|
86
|
-
|| I.TEXT_STYLE_MARKS.some((key) => !same(marks[key], current[key]))
|
|
87
|
-
|| !same(marks.link, current.link);
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
clearPendingMarks() {
|
|
91
|
-
this.pending = null;
|
|
92
|
-
},
|
|
93
|
-
|
|
94
|
-
onClick(event) {
|
|
95
|
-
const target = event.target;
|
|
96
|
-
if (target?.tagName === 'A' && !this.isFocused()) {
|
|
97
|
-
event.preventDefault();
|
|
98
|
-
return;
|
|
99
|
-
}
|
|
100
|
-
const atom = closestAtom(this.root, target);
|
|
101
|
-
// [坑] 只读档(contenteditable=false)点击照样命中原子块,但选中装饰与缩放手柄都是改文档的手段,
|
|
102
|
-
// 只读下必须不给;只留「点图放大」这一条读路径(宿主在 EditorSurface 上挂)
|
|
103
|
-
if (atom && this.isEditable()) this.selectAtom(atom);
|
|
104
|
-
else this.clearAtom();
|
|
105
|
-
},
|
|
106
|
-
|
|
107
|
-
/** 待办条目复选框:change 同步 data-checked(序列化口径) */
|
|
108
|
-
onCheckboxChange(event) {
|
|
109
|
-
const input = event.target;
|
|
110
|
-
if (input?.tagName !== 'INPUT' || input.type !== 'checkbox') return;
|
|
111
|
-
const li = input.closest('li[data-checked]');
|
|
112
|
-
if (!li) return;
|
|
113
|
-
this.run(() => {
|
|
114
|
-
li.setAttribute('data-checked', input.checked ? 'true' : 'false');
|
|
115
|
-
});
|
|
116
|
-
},
|
|
117
|
-
|
|
118
|
-
/** 选中原子块(图片 / 分割线):运行期类名,序列化时剥掉 */
|
|
119
|
-
selectAtom(el) {
|
|
120
|
-
this.clearAtom();
|
|
121
|
-
this.selectedAtom = el;
|
|
122
|
-
el.classList.add(D.ATOM_SELECTED);
|
|
123
|
-
this.emit('atom', el);
|
|
124
|
-
},
|
|
125
|
-
|
|
126
|
-
clearAtom() {
|
|
127
|
-
if (this.selectedAtom) this.selectedAtom.classList.remove(D.ATOM_SELECTED);
|
|
128
|
-
this.selectedAtom = null;
|
|
129
|
-
this.emit('atom', null);
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
function closestAtom(root, node) {
|
|
134
|
-
if (!node) return null;
|
|
135
|
-
const el = node.nodeType === Node.TEXT_NODE ? node.parentElement : node;
|
|
136
|
-
return D.closestWithin(el, (item) => item.tagName === 'IMG' || item.tagName === 'HR', root);
|
|
137
|
-
}
|
|
1
|
+
// [文件] web·editor-kernel·engine·interaction —— 编辑面交互事件处理器(焦点/选区变化/点击原子块/待办勾选)(混入 EditorCore.prototype)
|
|
2
|
+
// [约束] 带 `on*` 前缀的成员是直接挂到编辑面上的事件处理器:由 editor.js 构造时统一 bind(this),
|
|
3
|
+
// 以便 addEventListener / removeEventListener 拿到同一引用(等价原箭头函数字段的绑定语义)。
|
|
4
|
+
import * as D from './dom.js';
|
|
5
|
+
import * as I from './inline.js';
|
|
6
|
+
|
|
7
|
+
/** 需在构造时 bind(this) 的事件处理器名(挂 root/document 监听,必须引用稳定) */
|
|
8
|
+
export const INTERACTION_HANDLERS = ['onFocus', 'onBlur', 'onSelectionChange', 'onPointerState', 'onClick', 'onCheckboxChange'];
|
|
9
|
+
|
|
10
|
+
/** 交互事件方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
11
|
+
export const interactionMethods = {
|
|
12
|
+
onFocus() {
|
|
13
|
+
this.emit('focus');
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
onBlur() {
|
|
17
|
+
const sel = this.captureSelection();
|
|
18
|
+
if (sel) this.savedRange = sel;
|
|
19
|
+
this.clearBlockSelection();
|
|
20
|
+
this.emit('blur');
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
onSelectionChange() {
|
|
24
|
+
if (!this.isFocused()) return;
|
|
25
|
+
const sel = this.captureSelection();
|
|
26
|
+
if (sel) this.savedRange = sel;
|
|
27
|
+
const range = D.rangeOf(this.root);
|
|
28
|
+
this.pending = range && range.collapsed ? { ...this.inlineSnapshot() } : null;
|
|
29
|
+
this.syncBlockSelection();
|
|
30
|
+
this.syncState();
|
|
31
|
+
this.emit('selection');
|
|
32
|
+
this.emit('selectionUpdate');
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
/** 清掉选区跨块高亮(运行期类) */
|
|
36
|
+
clearBlockSelection() {
|
|
37
|
+
for (const el of Array.from(this.root.querySelectorAll(`.${D.BLOCK_SELECTED}`))) el.classList.remove(D.BLOCK_SELECTED);
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 选区跨块高亮:非折叠选区覆盖到的顶层块整块加淡蓝底(与块标悬浮高亮 gbd-is-block-hover 各管各的)。
|
|
42
|
+
* [设计] 用顶层块(root.children)作高亮单位:选区落在引用/高亮块内部的段落时,高亮的是整个容器,与用户「选中的块」直觉一致。
|
|
43
|
+
* [坑] 桌面式框选(blockMarqueeActive)期间高亮归框选管,本方法直接让位,否则会被每次 selectionchange 抹掉。
|
|
44
|
+
*/
|
|
45
|
+
syncBlockSelection() {
|
|
46
|
+
if (this.blockMarqueeActive) return;
|
|
47
|
+
this.clearBlockSelection();
|
|
48
|
+
const range = D.rangeOf(this.root);
|
|
49
|
+
if (!range || range.collapsed) return;
|
|
50
|
+
for (const el of Array.from(this.root.children)) {
|
|
51
|
+
if (D.isRuntimeBlock(el)) continue;
|
|
52
|
+
if (range.intersectsNode(el)) el.classList.add(D.BLOCK_SELECTED);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 桌面式框选:按客户端矩形给被覆盖到的顶层块打选区高亮(不产生原生 Range,纯视觉块选)。
|
|
58
|
+
* [设计] 用客户端坐标做命中(getBoundingClientRect 同基准),宿主不必换算缩放;
|
|
59
|
+
* 每次调用全量重算并 toggle(移出框的块自动撤高亮),返回命中数供宿主决定是否显示「已选 N 块」。
|
|
60
|
+
*/
|
|
61
|
+
marqueeSelect(clientRect) {
|
|
62
|
+
let hits = 0;
|
|
63
|
+
for (const el of Array.from(this.root.children)) {
|
|
64
|
+
if (D.isRuntimeBlock(el)) continue;
|
|
65
|
+
const r = el.getBoundingClientRect();
|
|
66
|
+
const hit = r.width > 0 && r.height > 0 && !(r.left > clientRect.right || r.right < clientRect.left || r.top > clientRect.bottom || r.bottom < clientRect.top);
|
|
67
|
+
el.classList.toggle(D.BLOCK_SELECTED, hit);
|
|
68
|
+
if (hit) hits += 1;
|
|
69
|
+
}
|
|
70
|
+
return hits;
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
onPointerState() {
|
|
74
|
+
this.syncState();
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
/** 折叠光标 → 待定标记态(工具栏切了标记再打字才生效,与主流编辑器一致)。
|
|
78
|
+
* [坑] 判据必须是「与光标处当前格式不同」:在加粗文字里关掉加粗时 pending 全是 false,
|
|
79
|
+
* 只看真值会当成没有待定标记,浏览器直接把新字插进加粗壳里 */
|
|
80
|
+
hasPendingMarks() {
|
|
81
|
+
const marks = this.pending;
|
|
82
|
+
if (!marks) return false;
|
|
83
|
+
const current = this.inlineSnapshot();
|
|
84
|
+
const same = (a, b) => String(a ?? '').trim().toLowerCase() === String(b ?? '').trim().toLowerCase();
|
|
85
|
+
return I.TAG_MARKS.some((key) => Boolean(marks[key]) !== Boolean(current[key]))
|
|
86
|
+
|| I.TEXT_STYLE_MARKS.some((key) => !same(marks[key], current[key]))
|
|
87
|
+
|| !same(marks.link, current.link);
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
clearPendingMarks() {
|
|
91
|
+
this.pending = null;
|
|
92
|
+
},
|
|
93
|
+
|
|
94
|
+
onClick(event) {
|
|
95
|
+
const target = event.target;
|
|
96
|
+
if (target?.tagName === 'A' && !this.isFocused()) {
|
|
97
|
+
event.preventDefault();
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
const atom = closestAtom(this.root, target);
|
|
101
|
+
// [坑] 只读档(contenteditable=false)点击照样命中原子块,但选中装饰与缩放手柄都是改文档的手段,
|
|
102
|
+
// 只读下必须不给;只留「点图放大」这一条读路径(宿主在 EditorSurface 上挂)
|
|
103
|
+
if (atom && this.isEditable()) this.selectAtom(atom);
|
|
104
|
+
else this.clearAtom();
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
/** 待办条目复选框:change 同步 data-checked(序列化口径) */
|
|
108
|
+
onCheckboxChange(event) {
|
|
109
|
+
const input = event.target;
|
|
110
|
+
if (input?.tagName !== 'INPUT' || input.type !== 'checkbox') return;
|
|
111
|
+
const li = input.closest('li[data-checked]');
|
|
112
|
+
if (!li) return;
|
|
113
|
+
this.run(() => {
|
|
114
|
+
li.setAttribute('data-checked', input.checked ? 'true' : 'false');
|
|
115
|
+
});
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
/** 选中原子块(图片 / 分割线):运行期类名,序列化时剥掉 */
|
|
119
|
+
selectAtom(el) {
|
|
120
|
+
this.clearAtom();
|
|
121
|
+
this.selectedAtom = el;
|
|
122
|
+
el.classList.add(D.ATOM_SELECTED);
|
|
123
|
+
this.emit('atom', el);
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
clearAtom() {
|
|
127
|
+
if (this.selectedAtom) this.selectedAtom.classList.remove(D.ATOM_SELECTED);
|
|
128
|
+
this.selectedAtom = null;
|
|
129
|
+
this.emit('atom', null);
|
|
130
|
+
},
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
function closestAtom(root, node) {
|
|
134
|
+
if (!node) return null;
|
|
135
|
+
const el = node.nodeType === Node.TEXT_NODE ? node.parentElement : node;
|
|
136
|
+
return D.closestWithin(el, (item) => item.tagName === 'IMG' || item.tagName === 'HR', root);
|
|
137
|
+
}
|
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·pointer —— 坐标命中与光标几何:块命中 / 留白落点 / 光标可视矩形(混入 EditorCore.prototype)
|
|
2
|
-
import * as D from './dom.js';
|
|
3
|
-
import * as B from './blocks.js';
|
|
4
|
-
|
|
5
|
-
/** 坐标与光标几何方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
6
|
-
export const pointerMethods = {
|
|
7
|
-
blockAt(x, y) {
|
|
8
|
-
return D.blockAtCoords(this.root, x, y);
|
|
9
|
-
},
|
|
10
|
-
|
|
11
|
-
/** 点到留白:把光标落到该坐标最近的可编辑位置 */
|
|
12
|
-
focusAt(x, y) {
|
|
13
|
-
const hit = D.pointAtCoords(this.root, x, y);
|
|
14
|
-
this.root.focus({ preventScroll: true });
|
|
15
|
-
if (hit) {
|
|
16
|
-
const block = D.blockOf(hit.node, this.root);
|
|
17
|
-
if (block) {
|
|
18
|
-
D.selectRange(this.root, { block, offset: D.charOffset(block, hit.node, hit.offset) }, { block, offset: D.charOffset(block, hit.node, hit.offset) });
|
|
19
|
-
return true;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
const near = D.nearestStructure(this.root, y);
|
|
23
|
-
const target = near ? D.firstTextBlockIn(near, this.root) || near : B.appendParagraph(this.root);
|
|
24
|
-
D.setCaret(target, 0);
|
|
25
|
-
return true;
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
/** 当前选区(或光标)的可视矩形:斜杠菜单/气泡菜单定位基准 */
|
|
29
|
-
caretRect() {
|
|
30
|
-
const range = D.rangeOf(this.root);
|
|
31
|
-
if (range) {
|
|
32
|
-
const rects = Array.from(range.getClientRects());
|
|
33
|
-
const rect = rects.find((item) => item.height > 0) || range.getBoundingClientRect();
|
|
34
|
-
if (rect && (rect.height || rect.width)) return rect;
|
|
35
|
-
}
|
|
36
|
-
const block = this.selectionRanges()[0]?.block;
|
|
37
|
-
return D.blockRect(block);
|
|
38
|
-
},
|
|
39
|
-
};
|
|
1
|
+
// [文件] web·editor-kernel·engine·pointer —— 坐标命中与光标几何:块命中 / 留白落点 / 光标可视矩形(混入 EditorCore.prototype)
|
|
2
|
+
import * as D from './dom.js';
|
|
3
|
+
import * as B from './blocks.js';
|
|
4
|
+
|
|
5
|
+
/** 坐标与光标几何方法(装配到 EditorCore.prototype;this 为内核实例) */
|
|
6
|
+
export const pointerMethods = {
|
|
7
|
+
blockAt(x, y) {
|
|
8
|
+
return D.blockAtCoords(this.root, x, y);
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
/** 点到留白:把光标落到该坐标最近的可编辑位置 */
|
|
12
|
+
focusAt(x, y) {
|
|
13
|
+
const hit = D.pointAtCoords(this.root, x, y);
|
|
14
|
+
this.root.focus({ preventScroll: true });
|
|
15
|
+
if (hit) {
|
|
16
|
+
const block = D.blockOf(hit.node, this.root);
|
|
17
|
+
if (block) {
|
|
18
|
+
D.selectRange(this.root, { block, offset: D.charOffset(block, hit.node, hit.offset) }, { block, offset: D.charOffset(block, hit.node, hit.offset) });
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const near = D.nearestStructure(this.root, y);
|
|
23
|
+
const target = near ? D.firstTextBlockIn(near, this.root) || near : B.appendParagraph(this.root);
|
|
24
|
+
D.setCaret(target, 0);
|
|
25
|
+
return true;
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
/** 当前选区(或光标)的可视矩形:斜杠菜单/气泡菜单定位基准 */
|
|
29
|
+
caretRect() {
|
|
30
|
+
const range = D.rangeOf(this.root);
|
|
31
|
+
if (range) {
|
|
32
|
+
const rects = Array.from(range.getClientRects());
|
|
33
|
+
const rect = rects.find((item) => item.height > 0) || range.getBoundingClientRect();
|
|
34
|
+
if (rect && (rect.height || rect.width)) return rect;
|
|
35
|
+
}
|
|
36
|
+
const block = this.selectionRanges()[0]?.block;
|
|
37
|
+
return D.blockRect(block);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
@@ -1,138 +1,138 @@
|
|
|
1
|
-
// [文件] web·editor-kernel·engine·revision —— 建议修改(修订)输入层:打字变 ins、删除变 del(beforeinput 拦截),全部接受/拒绝
|
|
2
|
-
/**
|
|
3
|
-
* [设计] 与旧实现同一交互模型:suggesting 期间拦 beforeinput——
|
|
4
|
-
* insertText → 建议插入(光标紧邻同类建议时扩展它,连续打字合并成一个标记)
|
|
5
|
-
* deleteContentBackward/Forward → 建议删除(选区正好盖住自己的 del 时撤销建议、真删)
|
|
6
|
-
* 解决语义:接受插入/拒绝删除 = 文字保留(剥壳);拒绝插入/接受删除 = 文字移除。
|
|
7
|
-
* 段落拆分与合并不做修订跟踪(与旧实现同口径)。
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
import { unwrapElement } from './dom.js';
|
|
11
|
-
import { DOC_CLASS } from './schema.js';
|
|
12
|
-
|
|
13
|
-
/** 全部修订标记的选择器(计数与「全部接受/拒绝」共用) */
|
|
14
|
-
const REV_SELECTOR = `.${DOC_CLASS.INS}, .${DOC_CLASS.DEL}`;
|
|
15
|
-
|
|
16
|
-
/** 当前修订数量(菜单接受/拒绝项的可用性) */
|
|
17
|
-
export const countRevisions = (root) => root?.querySelectorAll(REV_SELECTOR).length || 0;
|
|
18
|
-
|
|
19
|
-
/** 光标移到节点之后(插入/删除建议后的落点语义) */
|
|
20
|
-
function placeAfter(node) {
|
|
21
|
-
const range = document.createRange();
|
|
22
|
-
range.setStartAfter(node);
|
|
23
|
-
range.collapse(true);
|
|
24
|
-
const sel = window.getSelection();
|
|
25
|
-
sel.removeAllRanges();
|
|
26
|
-
sel.addRange(range);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** 光标左侧紧邻的同类建议标记(合并连续输入用;在文字中间不打断现有标记) */
|
|
30
|
-
function adjacentRevision(range, cls) {
|
|
31
|
-
let node = range.startContainer;
|
|
32
|
-
if (node.nodeType === Node.TEXT_NODE) {
|
|
33
|
-
if (range.startOffset !== 0) return null;
|
|
34
|
-
node = node.previousSibling;
|
|
35
|
-
} else {
|
|
36
|
-
node = range.startOffset > 0 ? node.childNodes[range.startOffset - 1] : null;
|
|
37
|
-
}
|
|
38
|
-
return node && node.nodeType === Node.ELEMENT_NODE && node.classList.contains(cls) ? node : null;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** 选区建议删除:正好盖住自己的 del → 撤销建议(真删);否则抽出内容包进 del */
|
|
42
|
-
function suggestDeleteRange(root, range) {
|
|
43
|
-
const dels = [...(root?.querySelectorAll(`.${DOC_CLASS.DEL}`) || [])];
|
|
44
|
-
const covered = dels.find((el) => range.intersectsNode(el) && range.toString() === el.textContent);
|
|
45
|
-
if (covered) {
|
|
46
|
-
covered.remove();
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
const frag = range.extractContents();
|
|
50
|
-
const del = document.createElement('del');
|
|
51
|
-
del.className = DOC_CLASS.DEL;
|
|
52
|
-
del.appendChild(frag);
|
|
53
|
-
range.insertNode(del);
|
|
54
|
-
placeAfter(del);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** 建议插入(光标紧邻同类 ins 时扩展它) */
|
|
58
|
-
function suggestInsert(root, text) {
|
|
59
|
-
const sel = window.getSelection();
|
|
60
|
-
if (!sel?.rangeCount || !root) return false;
|
|
61
|
-
let range = sel.getRangeAt(0);
|
|
62
|
-
if (!root.contains(range.startContainer)) return false;
|
|
63
|
-
if (!range.collapsed) {
|
|
64
|
-
suggestDeleteRange(root, range);
|
|
65
|
-
range = sel.getRangeAt(0); // 删除建议后光标已重定位
|
|
66
|
-
}
|
|
67
|
-
const anchor = adjacentRevision(range, DOC_CLASS.INS);
|
|
68
|
-
if (anchor) {
|
|
69
|
-
anchor.appendChild(document.createTextNode(text));
|
|
70
|
-
placeAfter(anchor.lastChild);
|
|
71
|
-
return true;
|
|
72
|
-
}
|
|
73
|
-
const ins = document.createElement('ins');
|
|
74
|
-
ins.className = DOC_CLASS.INS;
|
|
75
|
-
ins.textContent = text;
|
|
76
|
-
range.insertNode(ins);
|
|
77
|
-
placeAfter(ins);
|
|
78
|
-
return true;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** 建议删除(collapsed 时圈住相邻一个字符;跨节点边界降级为不处理,交给浏览器默认行为) */
|
|
82
|
-
function suggestDelete(root, forward) {
|
|
83
|
-
const sel = window.getSelection();
|
|
84
|
-
if (!sel?.rangeCount || !root) return false;
|
|
85
|
-
const range = sel.getRangeAt(0);
|
|
86
|
-
if (!root.contains(range.startContainer)) return false;
|
|
87
|
-
if (!range.collapsed) {
|
|
88
|
-
suggestDeleteRange(root, range);
|
|
89
|
-
return true;
|
|
90
|
-
}
|
|
91
|
-
const node = range.startContainer;
|
|
92
|
-
if (node.nodeType !== Node.TEXT_NODE) return false;
|
|
93
|
-
const off = range.startOffset;
|
|
94
|
-
const target = forward ? off + 1 : off - 1;
|
|
95
|
-
if (target < 0 || target > node.nodeValue.length) return false;
|
|
96
|
-
const r = range.cloneRange();
|
|
97
|
-
r.setStart(node, Math.min(off, target));
|
|
98
|
-
r.setEnd(node, Math.max(off, target));
|
|
99
|
-
suggestDeleteRange(root, r);
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** beforeinput 分发(建议修改模式开启时由 input.js 调用);返回 true = 已处理,调用方须 preventDefault */
|
|
104
|
-
export function handleRevisionInput(root, event) {
|
|
105
|
-
if (event.inputType === 'insertText' && event.data) return suggestInsert(root, event.data);
|
|
106
|
-
if (event.inputType === 'deleteContentBackward') return suggestDelete(root, false);
|
|
107
|
-
if (event.inputType === 'deleteContentForward') return suggestDelete(root, true);
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** 接受 / 拒绝全部修订:接受=插入保留、删除移除;拒绝反之(外层先解决,嵌套标记随之断开) */
|
|
112
|
-
export function resolveRevisions(root, accept) {
|
|
113
|
-
let count = 0;
|
|
114
|
-
for (const el of Array.from(root?.querySelectorAll(REV_SELECTOR) || [])) {
|
|
115
|
-
if (!el.isConnected) continue;
|
|
116
|
-
const keepContent = el.classList.contains(DOC_CLASS.INS) === Boolean(accept);
|
|
117
|
-
if (keepContent) {
|
|
118
|
-
unwrapElement(el);
|
|
119
|
-
}
|
|
120
|
-
el.remove();
|
|
121
|
-
count += 1;
|
|
122
|
-
}
|
|
123
|
-
root?.normalize();
|
|
124
|
-
return count;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/** 接受 / 拒绝单条修订(docx 面板右键菜单;语义与全部版的单条一致) */
|
|
128
|
-
export function resolveRevisionAt(el, accept) {
|
|
129
|
-
if (!el?.isConnected) return false;
|
|
130
|
-
const keepContent = el.classList.contains(DOC_CLASS.INS) === Boolean(accept);
|
|
131
|
-
// [坑] 必须先记父节点:el.remove() 之后 parentElement 恒为 null,归一化就永远不执行
|
|
132
|
-
const parent = el.parentElement;
|
|
133
|
-
if (keepContent) unwrapElement(el);
|
|
134
|
-
el.remove();
|
|
135
|
-
// 拆壳后两侧可能各留半截文本节点,归并回去免得后续选区/序列化出现碎片
|
|
136
|
-
parent?.normalize();
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
1
|
+
// [文件] web·editor-kernel·engine·revision —— 建议修改(修订)输入层:打字变 ins、删除变 del(beforeinput 拦截),全部接受/拒绝
|
|
2
|
+
/**
|
|
3
|
+
* [设计] 与旧实现同一交互模型:suggesting 期间拦 beforeinput——
|
|
4
|
+
* insertText → 建议插入(光标紧邻同类建议时扩展它,连续打字合并成一个标记)
|
|
5
|
+
* deleteContentBackward/Forward → 建议删除(选区正好盖住自己的 del 时撤销建议、真删)
|
|
6
|
+
* 解决语义:接受插入/拒绝删除 = 文字保留(剥壳);拒绝插入/接受删除 = 文字移除。
|
|
7
|
+
* 段落拆分与合并不做修订跟踪(与旧实现同口径)。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { unwrapElement } from './dom.js';
|
|
11
|
+
import { DOC_CLASS } from './schema.js';
|
|
12
|
+
|
|
13
|
+
/** 全部修订标记的选择器(计数与「全部接受/拒绝」共用) */
|
|
14
|
+
const REV_SELECTOR = `.${DOC_CLASS.INS}, .${DOC_CLASS.DEL}`;
|
|
15
|
+
|
|
16
|
+
/** 当前修订数量(菜单接受/拒绝项的可用性) */
|
|
17
|
+
export const countRevisions = (root) => root?.querySelectorAll(REV_SELECTOR).length || 0;
|
|
18
|
+
|
|
19
|
+
/** 光标移到节点之后(插入/删除建议后的落点语义) */
|
|
20
|
+
function placeAfter(node) {
|
|
21
|
+
const range = document.createRange();
|
|
22
|
+
range.setStartAfter(node);
|
|
23
|
+
range.collapse(true);
|
|
24
|
+
const sel = window.getSelection();
|
|
25
|
+
sel.removeAllRanges();
|
|
26
|
+
sel.addRange(range);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 光标左侧紧邻的同类建议标记(合并连续输入用;在文字中间不打断现有标记) */
|
|
30
|
+
function adjacentRevision(range, cls) {
|
|
31
|
+
let node = range.startContainer;
|
|
32
|
+
if (node.nodeType === Node.TEXT_NODE) {
|
|
33
|
+
if (range.startOffset !== 0) return null;
|
|
34
|
+
node = node.previousSibling;
|
|
35
|
+
} else {
|
|
36
|
+
node = range.startOffset > 0 ? node.childNodes[range.startOffset - 1] : null;
|
|
37
|
+
}
|
|
38
|
+
return node && node.nodeType === Node.ELEMENT_NODE && node.classList.contains(cls) ? node : null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 选区建议删除:正好盖住自己的 del → 撤销建议(真删);否则抽出内容包进 del */
|
|
42
|
+
function suggestDeleteRange(root, range) {
|
|
43
|
+
const dels = [...(root?.querySelectorAll(`.${DOC_CLASS.DEL}`) || [])];
|
|
44
|
+
const covered = dels.find((el) => range.intersectsNode(el) && range.toString() === el.textContent);
|
|
45
|
+
if (covered) {
|
|
46
|
+
covered.remove();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const frag = range.extractContents();
|
|
50
|
+
const del = document.createElement('del');
|
|
51
|
+
del.className = DOC_CLASS.DEL;
|
|
52
|
+
del.appendChild(frag);
|
|
53
|
+
range.insertNode(del);
|
|
54
|
+
placeAfter(del);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 建议插入(光标紧邻同类 ins 时扩展它) */
|
|
58
|
+
function suggestInsert(root, text) {
|
|
59
|
+
const sel = window.getSelection();
|
|
60
|
+
if (!sel?.rangeCount || !root) return false;
|
|
61
|
+
let range = sel.getRangeAt(0);
|
|
62
|
+
if (!root.contains(range.startContainer)) return false;
|
|
63
|
+
if (!range.collapsed) {
|
|
64
|
+
suggestDeleteRange(root, range);
|
|
65
|
+
range = sel.getRangeAt(0); // 删除建议后光标已重定位
|
|
66
|
+
}
|
|
67
|
+
const anchor = adjacentRevision(range, DOC_CLASS.INS);
|
|
68
|
+
if (anchor) {
|
|
69
|
+
anchor.appendChild(document.createTextNode(text));
|
|
70
|
+
placeAfter(anchor.lastChild);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
const ins = document.createElement('ins');
|
|
74
|
+
ins.className = DOC_CLASS.INS;
|
|
75
|
+
ins.textContent = text;
|
|
76
|
+
range.insertNode(ins);
|
|
77
|
+
placeAfter(ins);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** 建议删除(collapsed 时圈住相邻一个字符;跨节点边界降级为不处理,交给浏览器默认行为) */
|
|
82
|
+
function suggestDelete(root, forward) {
|
|
83
|
+
const sel = window.getSelection();
|
|
84
|
+
if (!sel?.rangeCount || !root) return false;
|
|
85
|
+
const range = sel.getRangeAt(0);
|
|
86
|
+
if (!root.contains(range.startContainer)) return false;
|
|
87
|
+
if (!range.collapsed) {
|
|
88
|
+
suggestDeleteRange(root, range);
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
const node = range.startContainer;
|
|
92
|
+
if (node.nodeType !== Node.TEXT_NODE) return false;
|
|
93
|
+
const off = range.startOffset;
|
|
94
|
+
const target = forward ? off + 1 : off - 1;
|
|
95
|
+
if (target < 0 || target > node.nodeValue.length) return false;
|
|
96
|
+
const r = range.cloneRange();
|
|
97
|
+
r.setStart(node, Math.min(off, target));
|
|
98
|
+
r.setEnd(node, Math.max(off, target));
|
|
99
|
+
suggestDeleteRange(root, r);
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** beforeinput 分发(建议修改模式开启时由 input.js 调用);返回 true = 已处理,调用方须 preventDefault */
|
|
104
|
+
export function handleRevisionInput(root, event) {
|
|
105
|
+
if (event.inputType === 'insertText' && event.data) return suggestInsert(root, event.data);
|
|
106
|
+
if (event.inputType === 'deleteContentBackward') return suggestDelete(root, false);
|
|
107
|
+
if (event.inputType === 'deleteContentForward') return suggestDelete(root, true);
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 接受 / 拒绝全部修订:接受=插入保留、删除移除;拒绝反之(外层先解决,嵌套标记随之断开) */
|
|
112
|
+
export function resolveRevisions(root, accept) {
|
|
113
|
+
let count = 0;
|
|
114
|
+
for (const el of Array.from(root?.querySelectorAll(REV_SELECTOR) || [])) {
|
|
115
|
+
if (!el.isConnected) continue;
|
|
116
|
+
const keepContent = el.classList.contains(DOC_CLASS.INS) === Boolean(accept);
|
|
117
|
+
if (keepContent) {
|
|
118
|
+
unwrapElement(el);
|
|
119
|
+
}
|
|
120
|
+
el.remove();
|
|
121
|
+
count += 1;
|
|
122
|
+
}
|
|
123
|
+
root?.normalize();
|
|
124
|
+
return count;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 接受 / 拒绝单条修订(docx 面板右键菜单;语义与全部版的单条一致) */
|
|
128
|
+
export function resolveRevisionAt(el, accept) {
|
|
129
|
+
if (!el?.isConnected) return false;
|
|
130
|
+
const keepContent = el.classList.contains(DOC_CLASS.INS) === Boolean(accept);
|
|
131
|
+
// [坑] 必须先记父节点:el.remove() 之后 parentElement 恒为 null,归一化就永远不执行
|
|
132
|
+
const parent = el.parentElement;
|
|
133
|
+
if (keepContent) unwrapElement(el);
|
|
134
|
+
el.remove();
|
|
135
|
+
// 拆壳后两侧可能各留半截文本节点,归并回去免得后续选区/序列化出现碎片
|
|
136
|
+
parent?.normalize();
|
|
137
|
+
return true;
|
|
138
|
+
}
|