@a200888213/gbdocx 0.6.1 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +215 -215
- package/index.html +31 -30
- package/package.json +49 -48
- package/serve.mjs +27 -23
- 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 +191 -191
- package/src/app/word/hf.js +76 -76
- package/src/app/word/layout.js +155 -155
- package/src/app/word/menus.js +201 -201
- package/src/app/word/notes.js +110 -110
- package/src/app/word/shared.js +22 -22
- package/src/app/word/view.js +159 -159
- package/src/app/word-app.js +487 -487
- package/src/app/word-sheets.js +101 -101
- package/src/kernel/docx/catalog.js +41 -41
- 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/menus.js +348 -348
- 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/docx/wordShortcuts.js +37 -37
- 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/editor.js +213 -213
- 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/search.js +113 -113
- 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/pasteClean.js +293 -293
- package/src/kernel/editor/utils.js +48 -48
- package/src/kernel/keyboard.js +114 -114
- package/src/kernel/notify.js +54 -54
- package/src/kernel/textStyles.js +84 -84
- package/src/kernel/util/clipboard.js +89 -89
- package/src/kernel/util/imageFile.js +91 -91
- package/src/kernel/util/safeValue.js +25 -25
- package/src/kernel/util/shortcutKey.js +33 -33
- package/src/surface/editor-surface.js +603 -600
- package/src/ui/color-panel.js +69 -69
- package/src/ui/context-menu.js +135 -133
- package/src/ui/dialogs.js +249 -249
- package/src/ui/find-bar.js +151 -151
- package/src/ui/icons.js +245 -137
- package/src/ui/menubar.js +28 -28
- package/src/ui/panels.js +115 -115
- package/src/ui/popup.js +79 -79
- 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/app.css +970 -970
- package/styles/editor-content.css +440 -440
- package/styles/tokens.css +154 -154
- package/styles/word-panel.css +410 -410
package/src/ui/panels.js
CHANGED
|
@@ -1,115 +1,115 @@
|
|
|
1
|
-
// [文件] gbdocx·Panels —— 目录导航面板 + 批注面板
|
|
2
|
-
|
|
3
|
-
/** 目录面板:items=[{key,text,level}];onLocate(key) */
|
|
4
|
-
export class CatalogPanel {
|
|
5
|
-
constructor(onLocate) {
|
|
6
|
-
this.onLocate = onLocate;
|
|
7
|
-
this.el = document.createElement('aside');
|
|
8
|
-
this.el.className = 'gbd-panel gbd-catalog';
|
|
9
|
-
this.el.innerHTML = '<div class="gbd-panel__title">目录导航</div><div class="gbd-panel__body"></div>';
|
|
10
|
-
this.body = this.el.querySelector('.gbd-panel__body');
|
|
11
|
-
this.activeKey = '';
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
setItems(items) {
|
|
15
|
-
this.items = items || [];
|
|
16
|
-
this.body.replaceChildren();
|
|
17
|
-
if (!this.items.length) {
|
|
18
|
-
const empty = document.createElement('div');
|
|
19
|
-
empty.className = 'gbd-panel__empty';
|
|
20
|
-
empty.textContent = '正文里还没有标题';
|
|
21
|
-
this.body.appendChild(empty);
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
for (const item of this.items) {
|
|
25
|
-
const row = document.createElement('button');
|
|
26
|
-
row.type = 'button';
|
|
27
|
-
row.className = 'gbd-catalog__item';
|
|
28
|
-
row.style.paddingLeft = `${8 + (item.level - 1) * 14}px`;
|
|
29
|
-
row.textContent = item.text;
|
|
30
|
-
row.dataset.key = item.key;
|
|
31
|
-
row.addEventListener('click', () => {
|
|
32
|
-
this.activeKey = item.key;
|
|
33
|
-
this.render();
|
|
34
|
-
this.onLocate(item.key);
|
|
35
|
-
});
|
|
36
|
-
this.body.appendChild(row);
|
|
37
|
-
}
|
|
38
|
-
this.render();
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
render() {
|
|
42
|
-
this.body.querySelectorAll('.gbd-catalog__item').forEach((el) => {
|
|
43
|
-
el.classList.toggle('gbd-is-active', el.dataset.key === this.activeKey);
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** 批注面板:comments=[{key,text,author,date,resolved}];onLocate/toggle/remove */
|
|
49
|
-
export class CommentsPanel {
|
|
50
|
-
constructor(io) {
|
|
51
|
-
this.io = io;
|
|
52
|
-
this.el = document.createElement('aside');
|
|
53
|
-
this.el.className = 'gbd-panel gbd-comments';
|
|
54
|
-
this.el.innerHTML = '<div class="gbd-panel__title">批注</div><div class="gbd-panel__body"></div>';
|
|
55
|
-
this.body = this.el.querySelector('.gbd-panel__body');
|
|
56
|
-
this.activeKey = '';
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
setItems(comments) {
|
|
60
|
-
this.comments = comments || [];
|
|
61
|
-
this.body.replaceChildren();
|
|
62
|
-
if (!this.comments.length) {
|
|
63
|
-
const empty = document.createElement('div');
|
|
64
|
-
empty.className = 'gbd-panel__empty';
|
|
65
|
-
empty.textContent = '暂无批注';
|
|
66
|
-
this.body.appendChild(empty);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
for (const c of this.comments) {
|
|
70
|
-
const card = document.createElement('div');
|
|
71
|
-
card.className = 'gbd-cmt-card';
|
|
72
|
-
card.dataset.key = c.key;
|
|
73
|
-
const meta = document.createElement('div');
|
|
74
|
-
meta.className = 'gbd-cmt-card__meta';
|
|
75
|
-
meta.textContent = `${c.author || '匿名'} · ${(c.date || '').slice(0, 16).replace('T', ' ')}`;
|
|
76
|
-
if (c.resolved) {
|
|
77
|
-
const tag = document.createElement('span');
|
|
78
|
-
tag.className = 'gbd-cmt-card__done';
|
|
79
|
-
tag.textContent = '已解决';
|
|
80
|
-
meta.appendChild(tag);
|
|
81
|
-
}
|
|
82
|
-
const text = document.createElement('div');
|
|
83
|
-
text.className = 'gbd-cmt-card__text';
|
|
84
|
-
text.textContent = c.text;
|
|
85
|
-
const actions = document.createElement('div');
|
|
86
|
-
actions.className = 'gbd-cmt-card__actions';
|
|
87
|
-
const toggle = document.createElement('button');
|
|
88
|
-
toggle.type = 'button';
|
|
89
|
-
toggle.className = 'gbd-linkbtn';
|
|
90
|
-
toggle.textContent = c.resolved ? '标记未解决' : '标记解决';
|
|
91
|
-
toggle.addEventListener('click', () => this.io.toggle(c.key));
|
|
92
|
-
const del = document.createElement('button');
|
|
93
|
-
del.type = 'button';
|
|
94
|
-
del.className = 'gbd-linkbtn gbd-linkbtn--danger';
|
|
95
|
-
del.textContent = '删除';
|
|
96
|
-
del.addEventListener('click', () => this.io.remove(c.key));
|
|
97
|
-
actions.append(toggle, del);
|
|
98
|
-
card.append(meta, text, actions);
|
|
99
|
-
card.addEventListener('click', (e) => {
|
|
100
|
-
if (e.target.closest('.gbd-cmt-card__actions')) return;
|
|
101
|
-
this.activeKey = c.key;
|
|
102
|
-
this.render();
|
|
103
|
-
this.io.locate(c.key);
|
|
104
|
-
});
|
|
105
|
-
this.body.appendChild(card);
|
|
106
|
-
}
|
|
107
|
-
this.render();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
render() {
|
|
111
|
-
this.body.querySelectorAll('.gbd-cmt-card').forEach((el) => {
|
|
112
|
-
el.classList.toggle('gbd-is-active', el.dataset.key === this.activeKey);
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
}
|
|
1
|
+
// [文件] gbdocx·Panels —— 目录导航面板 + 批注面板
|
|
2
|
+
|
|
3
|
+
/** 目录面板:items=[{key,text,level}];onLocate(key) */
|
|
4
|
+
export class CatalogPanel {
|
|
5
|
+
constructor(onLocate) {
|
|
6
|
+
this.onLocate = onLocate;
|
|
7
|
+
this.el = document.createElement('aside');
|
|
8
|
+
this.el.className = 'gbd-panel gbd-catalog';
|
|
9
|
+
this.el.innerHTML = '<div class="gbd-panel__title">目录导航</div><div class="gbd-panel__body"></div>';
|
|
10
|
+
this.body = this.el.querySelector('.gbd-panel__body');
|
|
11
|
+
this.activeKey = '';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
setItems(items) {
|
|
15
|
+
this.items = items || [];
|
|
16
|
+
this.body.replaceChildren();
|
|
17
|
+
if (!this.items.length) {
|
|
18
|
+
const empty = document.createElement('div');
|
|
19
|
+
empty.className = 'gbd-panel__empty';
|
|
20
|
+
empty.textContent = '正文里还没有标题';
|
|
21
|
+
this.body.appendChild(empty);
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
for (const item of this.items) {
|
|
25
|
+
const row = document.createElement('button');
|
|
26
|
+
row.type = 'button';
|
|
27
|
+
row.className = 'gbd-catalog__item';
|
|
28
|
+
row.style.paddingLeft = `${8 + (item.level - 1) * 14}px`;
|
|
29
|
+
row.textContent = item.text;
|
|
30
|
+
row.dataset.key = item.key;
|
|
31
|
+
row.addEventListener('click', () => {
|
|
32
|
+
this.activeKey = item.key;
|
|
33
|
+
this.render();
|
|
34
|
+
this.onLocate(item.key);
|
|
35
|
+
});
|
|
36
|
+
this.body.appendChild(row);
|
|
37
|
+
}
|
|
38
|
+
this.render();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
render() {
|
|
42
|
+
this.body.querySelectorAll('.gbd-catalog__item').forEach((el) => {
|
|
43
|
+
el.classList.toggle('gbd-is-active', el.dataset.key === this.activeKey);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** 批注面板:comments=[{key,text,author,date,resolved}];onLocate/toggle/remove */
|
|
49
|
+
export class CommentsPanel {
|
|
50
|
+
constructor(io) {
|
|
51
|
+
this.io = io;
|
|
52
|
+
this.el = document.createElement('aside');
|
|
53
|
+
this.el.className = 'gbd-panel gbd-comments';
|
|
54
|
+
this.el.innerHTML = '<div class="gbd-panel__title">批注</div><div class="gbd-panel__body"></div>';
|
|
55
|
+
this.body = this.el.querySelector('.gbd-panel__body');
|
|
56
|
+
this.activeKey = '';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
setItems(comments) {
|
|
60
|
+
this.comments = comments || [];
|
|
61
|
+
this.body.replaceChildren();
|
|
62
|
+
if (!this.comments.length) {
|
|
63
|
+
const empty = document.createElement('div');
|
|
64
|
+
empty.className = 'gbd-panel__empty';
|
|
65
|
+
empty.textContent = '暂无批注';
|
|
66
|
+
this.body.appendChild(empty);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
for (const c of this.comments) {
|
|
70
|
+
const card = document.createElement('div');
|
|
71
|
+
card.className = 'gbd-cmt-card';
|
|
72
|
+
card.dataset.key = c.key;
|
|
73
|
+
const meta = document.createElement('div');
|
|
74
|
+
meta.className = 'gbd-cmt-card__meta';
|
|
75
|
+
meta.textContent = `${c.author || '匿名'} · ${(c.date || '').slice(0, 16).replace('T', ' ')}`;
|
|
76
|
+
if (c.resolved) {
|
|
77
|
+
const tag = document.createElement('span');
|
|
78
|
+
tag.className = 'gbd-cmt-card__done';
|
|
79
|
+
tag.textContent = '已解决';
|
|
80
|
+
meta.appendChild(tag);
|
|
81
|
+
}
|
|
82
|
+
const text = document.createElement('div');
|
|
83
|
+
text.className = 'gbd-cmt-card__text';
|
|
84
|
+
text.textContent = c.text;
|
|
85
|
+
const actions = document.createElement('div');
|
|
86
|
+
actions.className = 'gbd-cmt-card__actions';
|
|
87
|
+
const toggle = document.createElement('button');
|
|
88
|
+
toggle.type = 'button';
|
|
89
|
+
toggle.className = 'gbd-linkbtn';
|
|
90
|
+
toggle.textContent = c.resolved ? '标记未解决' : '标记解决';
|
|
91
|
+
toggle.addEventListener('click', () => this.io.toggle(c.key));
|
|
92
|
+
const del = document.createElement('button');
|
|
93
|
+
del.type = 'button';
|
|
94
|
+
del.className = 'gbd-linkbtn gbd-linkbtn--danger';
|
|
95
|
+
del.textContent = '删除';
|
|
96
|
+
del.addEventListener('click', () => this.io.remove(c.key));
|
|
97
|
+
actions.append(toggle, del);
|
|
98
|
+
card.append(meta, text, actions);
|
|
99
|
+
card.addEventListener('click', (e) => {
|
|
100
|
+
if (e.target.closest('.gbd-cmt-card__actions')) return;
|
|
101
|
+
this.activeKey = c.key;
|
|
102
|
+
this.render();
|
|
103
|
+
this.io.locate(c.key);
|
|
104
|
+
});
|
|
105
|
+
this.body.appendChild(card);
|
|
106
|
+
}
|
|
107
|
+
this.render();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
render() {
|
|
111
|
+
this.body.querySelectorAll('.gbd-cmt-card').forEach((el) => {
|
|
112
|
+
el.classList.toggle('gbd-is-active', el.dataset.key === this.activeKey);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
package/src/ui/popup.js
CHANGED
|
@@ -1,79 +1,79 @@
|
|
|
1
|
-
// [文件] gbdocx·Popup —— 自绘浮层基座(定位/限高/外点关闭/Esc 关闭),供下拉、右键菜单、气泡、色板共用
|
|
2
|
-
import { computePopupPosition } from '../kernel/editor/utils.js';
|
|
3
|
-
|
|
4
|
-
const OPEN = new Set();
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 打开一个浮层
|
|
8
|
-
* @param {object} opts
|
|
9
|
-
* @param {HTMLElement|DOMRect} opts.anchor 锚点元素或其矩形
|
|
10
|
-
* @param {HTMLElement} opts.content 浮层内容体(挂到 body)
|
|
11
|
-
* @param {string} [opts.placement] 展开方向(bottom-start 等)
|
|
12
|
-
* @param {string} [opts.className] 附加类名
|
|
13
|
-
* @param {() => void} [opts.onClose] 关闭回调
|
|
14
|
-
* @param {boolean} [opts.closeOnScroll]
|
|
15
|
-
* @param {boolean} [opts.nested] 子级浮层(子菜单):打开时不清掉父级浮层
|
|
16
|
-
* @param {(target: EventTarget) => boolean} [opts.isInside] 额外判定「点在浮层内部」(如子菜单)
|
|
17
|
-
* @returns {{ el: HTMLElement, close: () => void }}
|
|
18
|
-
*/
|
|
19
|
-
export function openPopup({ anchor, content, placement = 'bottom-start', className = '', onClose, closeOnScroll = false, nested = false, isInside }) {
|
|
20
|
-
if (!nested) closeAll();
|
|
21
|
-
const wrap = document.createElement('div');
|
|
22
|
-
wrap.className = `gbd-popup ${className}`.trim();
|
|
23
|
-
wrap.appendChild(content);
|
|
24
|
-
// 样式作用域:浮层挂 body,但仍须落在 .word-scope 子树里(本包 CSS 全部按该前缀作用域化)
|
|
25
|
-
const scope = document.createElement('div');
|
|
26
|
-
scope.className = 'word-scope';
|
|
27
|
-
scope.appendChild(wrap);
|
|
28
|
-
document.body.appendChild(scope);
|
|
29
|
-
|
|
30
|
-
const rect = anchor instanceof Element ? anchor.getBoundingClientRect() : anchor;
|
|
31
|
-
const measure = wrap.getBoundingClientRect();
|
|
32
|
-
const pos = computePopupPosition(rect, { width: measure.width, height: measure.height }, { placement });
|
|
33
|
-
Object.assign(wrap.style, { left: `${pos.left}px`, top: `${pos.top}px` });
|
|
34
|
-
if (pos.maxHeight > 0) wrap.style.maxHeight = `${pos.maxHeight}px`;
|
|
35
|
-
|
|
36
|
-
let attachTimer = 0;
|
|
37
|
-
const close = () => {
|
|
38
|
-
if (!wrap.isConnected) return;
|
|
39
|
-
clearTimeout(attachTimer);
|
|
40
|
-
scope.remove();
|
|
41
|
-
OPEN.delete(entry);
|
|
42
|
-
document.removeEventListener('mousedown', onDocDown, true);
|
|
43
|
-
document.removeEventListener('keydown', onDocKey, true);
|
|
44
|
-
document.removeEventListener('gbdocx-menu-close', onDocDown);
|
|
45
|
-
window.removeEventListener('scroll', onScroll, true);
|
|
46
|
-
window.removeEventListener('resize', onScroll);
|
|
47
|
-
onClose?.();
|
|
48
|
-
};
|
|
49
|
-
const onDocDown = (e) => {
|
|
50
|
-
if (wrap.contains(e.target) || isInside?.(e.target)) return;
|
|
51
|
-
close();
|
|
52
|
-
};
|
|
53
|
-
const onDocKey = (e) => {
|
|
54
|
-
if (e.key === 'Escape') {
|
|
55
|
-
e.preventDefault();
|
|
56
|
-
e.stopPropagation();
|
|
57
|
-
close();
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
const onScroll = () => { if (closeOnScroll) close(); };
|
|
61
|
-
const entry = { close };
|
|
62
|
-
OPEN.add(entry);
|
|
63
|
-
// 延后一拍再挂外点监听:打开它的那次点击不要立刻把它自己关掉。
|
|
64
|
-
// [坑] 定时器必须可取消:close 先跑(Esc/外点)时若回调仍挂着,监听会挂在一个已移除的浮层上永不摘除
|
|
65
|
-
attachTimer = setTimeout(() => {
|
|
66
|
-
if (!wrap.isConnected) return;
|
|
67
|
-
document.addEventListener('mousedown', onDocDown, true);
|
|
68
|
-
window.addEventListener('scroll', onScroll, true);
|
|
69
|
-
window.addEventListener('resize', onScroll);
|
|
70
|
-
}, 0);
|
|
71
|
-
document.addEventListener('keydown', onDocKey, true);
|
|
72
|
-
document.addEventListener('gbdocx-menu-close', onDocDown);
|
|
73
|
-
return { el: wrap, close };
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/** 关闭全部已开浮层(右键菜单等再打开时先收旧) */
|
|
77
|
-
export function closeAll() {
|
|
78
|
-
for (const entry of [...OPEN]) entry.close();
|
|
79
|
-
}
|
|
1
|
+
// [文件] gbdocx·Popup —— 自绘浮层基座(定位/限高/外点关闭/Esc 关闭),供下拉、右键菜单、气泡、色板共用
|
|
2
|
+
import { computePopupPosition } from '../kernel/editor/utils.js';
|
|
3
|
+
|
|
4
|
+
const OPEN = new Set();
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 打开一个浮层
|
|
8
|
+
* @param {object} opts
|
|
9
|
+
* @param {HTMLElement|DOMRect} opts.anchor 锚点元素或其矩形
|
|
10
|
+
* @param {HTMLElement} opts.content 浮层内容体(挂到 body)
|
|
11
|
+
* @param {string} [opts.placement] 展开方向(bottom-start 等)
|
|
12
|
+
* @param {string} [opts.className] 附加类名
|
|
13
|
+
* @param {() => void} [opts.onClose] 关闭回调
|
|
14
|
+
* @param {boolean} [opts.closeOnScroll]
|
|
15
|
+
* @param {boolean} [opts.nested] 子级浮层(子菜单):打开时不清掉父级浮层
|
|
16
|
+
* @param {(target: EventTarget) => boolean} [opts.isInside] 额外判定「点在浮层内部」(如子菜单)
|
|
17
|
+
* @returns {{ el: HTMLElement, close: () => void }}
|
|
18
|
+
*/
|
|
19
|
+
export function openPopup({ anchor, content, placement = 'bottom-start', className = '', onClose, closeOnScroll = false, nested = false, isInside }) {
|
|
20
|
+
if (!nested) closeAll();
|
|
21
|
+
const wrap = document.createElement('div');
|
|
22
|
+
wrap.className = `gbd-popup ${className}`.trim();
|
|
23
|
+
wrap.appendChild(content);
|
|
24
|
+
// 样式作用域:浮层挂 body,但仍须落在 .word-scope 子树里(本包 CSS 全部按该前缀作用域化)
|
|
25
|
+
const scope = document.createElement('div');
|
|
26
|
+
scope.className = 'word-scope';
|
|
27
|
+
scope.appendChild(wrap);
|
|
28
|
+
document.body.appendChild(scope);
|
|
29
|
+
|
|
30
|
+
const rect = anchor instanceof Element ? anchor.getBoundingClientRect() : anchor;
|
|
31
|
+
const measure = wrap.getBoundingClientRect();
|
|
32
|
+
const pos = computePopupPosition(rect, { width: measure.width, height: measure.height }, { placement });
|
|
33
|
+
Object.assign(wrap.style, { left: `${pos.left}px`, top: `${pos.top}px` });
|
|
34
|
+
if (pos.maxHeight > 0) wrap.style.maxHeight = `${pos.maxHeight}px`;
|
|
35
|
+
|
|
36
|
+
let attachTimer = 0;
|
|
37
|
+
const close = () => {
|
|
38
|
+
if (!wrap.isConnected) return;
|
|
39
|
+
clearTimeout(attachTimer);
|
|
40
|
+
scope.remove();
|
|
41
|
+
OPEN.delete(entry);
|
|
42
|
+
document.removeEventListener('mousedown', onDocDown, true);
|
|
43
|
+
document.removeEventListener('keydown', onDocKey, true);
|
|
44
|
+
document.removeEventListener('gbdocx-menu-close', onDocDown);
|
|
45
|
+
window.removeEventListener('scroll', onScroll, true);
|
|
46
|
+
window.removeEventListener('resize', onScroll);
|
|
47
|
+
onClose?.();
|
|
48
|
+
};
|
|
49
|
+
const onDocDown = (e) => {
|
|
50
|
+
if (wrap.contains(e.target) || isInside?.(e.target)) return;
|
|
51
|
+
close();
|
|
52
|
+
};
|
|
53
|
+
const onDocKey = (e) => {
|
|
54
|
+
if (e.key === 'Escape') {
|
|
55
|
+
e.preventDefault();
|
|
56
|
+
e.stopPropagation();
|
|
57
|
+
close();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const onScroll = () => { if (closeOnScroll) close(); };
|
|
61
|
+
const entry = { close };
|
|
62
|
+
OPEN.add(entry);
|
|
63
|
+
// 延后一拍再挂外点监听:打开它的那次点击不要立刻把它自己关掉。
|
|
64
|
+
// [坑] 定时器必须可取消:close 先跑(Esc/外点)时若回调仍挂着,监听会挂在一个已移除的浮层上永不摘除
|
|
65
|
+
attachTimer = setTimeout(() => {
|
|
66
|
+
if (!wrap.isConnected) return;
|
|
67
|
+
document.addEventListener('mousedown', onDocDown, true);
|
|
68
|
+
window.addEventListener('scroll', onScroll, true);
|
|
69
|
+
window.addEventListener('resize', onScroll);
|
|
70
|
+
}, 0);
|
|
71
|
+
document.addEventListener('keydown', onDocKey, true);
|
|
72
|
+
document.addEventListener('gbdocx-menu-close', onDocDown);
|
|
73
|
+
return { el: wrap, close };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 关闭全部已开浮层(右键菜单等再打开时先收旧) */
|
|
77
|
+
export function closeAll() {
|
|
78
|
+
for (const entry of [...OPEN]) entry.close();
|
|
79
|
+
}
|
package/src/ui/prompt.js
CHANGED
|
@@ -1,89 +1,89 @@
|
|
|
1
|
-
// [文件] gbdocx·PromptPopover —— 单字段气泡表单(链接地址 / 批注内容 / 脚注内容共用一套;触发点无稳定 DOM 时走 openAt 锚点)
|
|
2
|
-
import { openPopup } from './popup.js';
|
|
3
|
-
|
|
4
|
-
/** 单字段气泡:openAt(anchor, spec) 打开;关闭未提交回调 onClosed(false),提交返回 true 即关闭并不回调 */
|
|
5
|
-
export class PromptPopover {
|
|
6
|
-
constructor() {
|
|
7
|
-
this.handle = null;
|
|
8
|
-
this.onClosed = null;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @param {HTMLElement|DOMRect|undefined} anchor 锚点(缺省回落屏上方居中)
|
|
13
|
-
* @param {object} spec {title, placeholder, maxlength, required, submit(text)->bool}
|
|
14
|
-
*/
|
|
15
|
-
openAt(anchor, spec = {}) {
|
|
16
|
-
this.close();
|
|
17
|
-
const box = document.createElement('div');
|
|
18
|
-
box.className = 'gbd-prompt';
|
|
19
|
-
box.innerHTML = `
|
|
20
|
-
<div class="gbd-prompt__title"></div>
|
|
21
|
-
<input class="gbd-prompt__input" type="text" />
|
|
22
|
-
<div class="gbd-prompt__actions">
|
|
23
|
-
<button type="button" class="gbd-btn" data-act="cancel">取消</button>
|
|
24
|
-
<button type="button" class="gbd-btn gbd-btn--primary" data-act="ok">确定</button>
|
|
25
|
-
</div>`;
|
|
26
|
-
const titleEl = box.querySelector('.gbd-prompt__title');
|
|
27
|
-
const input = box.querySelector('.gbd-prompt__input');
|
|
28
|
-
titleEl.textContent = spec.title || '';
|
|
29
|
-
if (spec.placeholder) input.placeholder = spec.placeholder;
|
|
30
|
-
if (spec.maxlength) input.maxLength = spec.maxlength;
|
|
31
|
-
input.value = spec.value || '';
|
|
32
|
-
|
|
33
|
-
const closeAndReport = (submitted) => {
|
|
34
|
-
notifyClosed(submitted);
|
|
35
|
-
this.close();
|
|
36
|
-
};
|
|
37
|
-
// 每条气泡只上报一次关闭,但不清空宿主注册的 onClosed:
|
|
38
|
-
// 该回调是实例级(WordApp 只在构造时赋值一次),旧实现关闭第一条气泡就把它置空,
|
|
39
|
-
// 之后所有「取消批注/取消脚注」的未提交回滚都不会再执行
|
|
40
|
-
let settled = false;
|
|
41
|
-
const notifyClosed = (submitted) => {
|
|
42
|
-
if (settled) return;
|
|
43
|
-
settled = true;
|
|
44
|
-
this.onClosed?.(submitted);
|
|
45
|
-
};
|
|
46
|
-
const submit = () => {
|
|
47
|
-
const text = input.value;
|
|
48
|
-
if (spec.required !== false && spec.required !== undefined && spec.required && !String(text).trim()) {
|
|
49
|
-
input.focus();
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
if (spec.submit ? spec.submit(text) !== false : true) {
|
|
53
|
-
notifyClosed(true);
|
|
54
|
-
this.close();
|
|
55
|
-
} else {
|
|
56
|
-
input.focus();
|
|
57
|
-
input.select();
|
|
58
|
-
}
|
|
59
|
-
};
|
|
60
|
-
box.querySelector('[data-act="cancel"]').addEventListener('click', () => closeAndReport(false));
|
|
61
|
-
box.querySelector('[data-act="ok"]').addEventListener('click', submit);
|
|
62
|
-
input.addEventListener('keydown', (e) => {
|
|
63
|
-
if (e.key === 'Enter') {
|
|
64
|
-
e.preventDefault();
|
|
65
|
-
submit();
|
|
66
|
-
} else if (e.key === 'Escape') {
|
|
67
|
-
e.preventDefault();
|
|
68
|
-
closeAndReport(false);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const rect = anchor instanceof Element ? anchor.getBoundingClientRect() : anchor;
|
|
73
|
-
const fallback = rect || { left: window.innerWidth / 2 - 120, top: 120, right: window.innerWidth / 2 + 120, bottom: 120 };
|
|
74
|
-
this.handle = openPopup({
|
|
75
|
-
anchor: fallback,
|
|
76
|
-
content: box,
|
|
77
|
-
placement: 'bottom-start',
|
|
78
|
-
className: 'gbd-popup--prompt',
|
|
79
|
-
onClose: () => notifyClosed(false),
|
|
80
|
-
});
|
|
81
|
-
requestAnimationFrame(() => input.focus());
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
close() {
|
|
85
|
-
const h = this.handle;
|
|
86
|
-
this.handle = null;
|
|
87
|
-
h?.close();
|
|
88
|
-
}
|
|
89
|
-
}
|
|
1
|
+
// [文件] gbdocx·PromptPopover —— 单字段气泡表单(链接地址 / 批注内容 / 脚注内容共用一套;触发点无稳定 DOM 时走 openAt 锚点)
|
|
2
|
+
import { openPopup } from './popup.js';
|
|
3
|
+
|
|
4
|
+
/** 单字段气泡:openAt(anchor, spec) 打开;关闭未提交回调 onClosed(false),提交返回 true 即关闭并不回调 */
|
|
5
|
+
export class PromptPopover {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.handle = null;
|
|
8
|
+
this.onClosed = null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {HTMLElement|DOMRect|undefined} anchor 锚点(缺省回落屏上方居中)
|
|
13
|
+
* @param {object} spec {title, placeholder, maxlength, required, submit(text)->bool}
|
|
14
|
+
*/
|
|
15
|
+
openAt(anchor, spec = {}) {
|
|
16
|
+
this.close();
|
|
17
|
+
const box = document.createElement('div');
|
|
18
|
+
box.className = 'gbd-prompt';
|
|
19
|
+
box.innerHTML = `
|
|
20
|
+
<div class="gbd-prompt__title"></div>
|
|
21
|
+
<input class="gbd-prompt__input" type="text" />
|
|
22
|
+
<div class="gbd-prompt__actions">
|
|
23
|
+
<button type="button" class="gbd-btn" data-act="cancel">取消</button>
|
|
24
|
+
<button type="button" class="gbd-btn gbd-btn--primary" data-act="ok">确定</button>
|
|
25
|
+
</div>`;
|
|
26
|
+
const titleEl = box.querySelector('.gbd-prompt__title');
|
|
27
|
+
const input = box.querySelector('.gbd-prompt__input');
|
|
28
|
+
titleEl.textContent = spec.title || '';
|
|
29
|
+
if (spec.placeholder) input.placeholder = spec.placeholder;
|
|
30
|
+
if (spec.maxlength) input.maxLength = spec.maxlength;
|
|
31
|
+
input.value = spec.value || '';
|
|
32
|
+
|
|
33
|
+
const closeAndReport = (submitted) => {
|
|
34
|
+
notifyClosed(submitted);
|
|
35
|
+
this.close();
|
|
36
|
+
};
|
|
37
|
+
// 每条气泡只上报一次关闭,但不清空宿主注册的 onClosed:
|
|
38
|
+
// 该回调是实例级(WordApp 只在构造时赋值一次),旧实现关闭第一条气泡就把它置空,
|
|
39
|
+
// 之后所有「取消批注/取消脚注」的未提交回滚都不会再执行
|
|
40
|
+
let settled = false;
|
|
41
|
+
const notifyClosed = (submitted) => {
|
|
42
|
+
if (settled) return;
|
|
43
|
+
settled = true;
|
|
44
|
+
this.onClosed?.(submitted);
|
|
45
|
+
};
|
|
46
|
+
const submit = () => {
|
|
47
|
+
const text = input.value;
|
|
48
|
+
if (spec.required !== false && spec.required !== undefined && spec.required && !String(text).trim()) {
|
|
49
|
+
input.focus();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
if (spec.submit ? spec.submit(text) !== false : true) {
|
|
53
|
+
notifyClosed(true);
|
|
54
|
+
this.close();
|
|
55
|
+
} else {
|
|
56
|
+
input.focus();
|
|
57
|
+
input.select();
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
box.querySelector('[data-act="cancel"]').addEventListener('click', () => closeAndReport(false));
|
|
61
|
+
box.querySelector('[data-act="ok"]').addEventListener('click', submit);
|
|
62
|
+
input.addEventListener('keydown', (e) => {
|
|
63
|
+
if (e.key === 'Enter') {
|
|
64
|
+
e.preventDefault();
|
|
65
|
+
submit();
|
|
66
|
+
} else if (e.key === 'Escape') {
|
|
67
|
+
e.preventDefault();
|
|
68
|
+
closeAndReport(false);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
const rect = anchor instanceof Element ? anchor.getBoundingClientRect() : anchor;
|
|
73
|
+
const fallback = rect || { left: window.innerWidth / 2 - 120, top: 120, right: window.innerWidth / 2 + 120, bottom: 120 };
|
|
74
|
+
this.handle = openPopup({
|
|
75
|
+
anchor: fallback,
|
|
76
|
+
content: box,
|
|
77
|
+
placement: 'bottom-start',
|
|
78
|
+
className: 'gbd-popup--prompt',
|
|
79
|
+
onClose: () => notifyClosed(false),
|
|
80
|
+
});
|
|
81
|
+
requestAnimationFrame(() => input.focus());
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
close() {
|
|
85
|
+
const h = this.handle;
|
|
86
|
+
this.handle = null;
|
|
87
|
+
h?.close();
|
|
88
|
+
}
|
|
89
|
+
}
|