@a200888213/gbdocx 0.5.1 → 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 +22 -6
- package/src/app/word-app.js +486 -467
- 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
package/src/ui/menubar.js
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
// [文件] gbdocx·MenuBar —— 顶部菜单栏(结构来自 docx/menus.js 的 buildMenus;条目为函数,每次打开现求值)
|
|
2
|
-
import { openMenu } from './context-menu.js';
|
|
3
|
-
|
|
4
|
-
/** 构建菜单栏 DOM:menus = [{key,label,items:()=>条目[]}];disabledKeys 里的菜单只读档不可点 */
|
|
5
|
-
export function buildMenuBar(menus, disabledKeys = []) {
|
|
6
|
-
const bar = document.createElement('div');
|
|
7
|
-
bar.className = 'gbd-menubar';
|
|
8
|
-
for (const menu of menus) {
|
|
9
|
-
const btn = document.createElement('button');
|
|
10
|
-
btn.type = 'button';
|
|
11
|
-
btn.className = 'gbd-menubar__btn';
|
|
12
|
-
btn.disabled = disabledKeys.includes(menu.key);
|
|
13
|
-
btn.textContent = menu.label;
|
|
14
|
-
btn.addEventListener('click', () => {
|
|
15
|
-
// 按钮态由浮层关闭回调统一收:Esc / 外点 / 点条目都会经过它,
|
|
16
|
-
// 不再挂 gbdocx-menu-close 监听(那条路径只在点条目时触发,其余关闭方式会残留监听)
|
|
17
|
-
btn.classList.add('gbd-is-open');
|
|
18
|
-
openMenu({
|
|
19
|
-
anchor: btn,
|
|
20
|
-
items: menu.items(),
|
|
21
|
-
placement: 'bottom-start',
|
|
22
|
-
onClose: () => btn.classList.remove('gbd-is-open'),
|
|
23
|
-
});
|
|
24
|
-
});
|
|
25
|
-
bar.appendChild(btn);
|
|
26
|
-
}
|
|
27
|
-
return bar;
|
|
28
|
-
}
|
|
1
|
+
// [文件] gbdocx·MenuBar —— 顶部菜单栏(结构来自 docx/menus.js 的 buildMenus;条目为函数,每次打开现求值)
|
|
2
|
+
import { openMenu } from './context-menu.js';
|
|
3
|
+
|
|
4
|
+
/** 构建菜单栏 DOM:menus = [{key,label,items:()=>条目[]}];disabledKeys 里的菜单只读档不可点 */
|
|
5
|
+
export function buildMenuBar(menus, disabledKeys = []) {
|
|
6
|
+
const bar = document.createElement('div');
|
|
7
|
+
bar.className = 'gbd-menubar';
|
|
8
|
+
for (const menu of menus) {
|
|
9
|
+
const btn = document.createElement('button');
|
|
10
|
+
btn.type = 'button';
|
|
11
|
+
btn.className = 'gbd-menubar__btn';
|
|
12
|
+
btn.disabled = disabledKeys.includes(menu.key);
|
|
13
|
+
btn.textContent = menu.label;
|
|
14
|
+
btn.addEventListener('click', () => {
|
|
15
|
+
// 按钮态由浮层关闭回调统一收:Esc / 外点 / 点条目都会经过它,
|
|
16
|
+
// 不再挂 gbdocx-menu-close 监听(那条路径只在点条目时触发,其余关闭方式会残留监听)
|
|
17
|
+
btn.classList.add('gbd-is-open');
|
|
18
|
+
openMenu({
|
|
19
|
+
anchor: btn,
|
|
20
|
+
items: menu.items(),
|
|
21
|
+
placement: 'bottom-start',
|
|
22
|
+
onClose: () => btn.classList.remove('gbd-is-open'),
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
bar.appendChild(btn);
|
|
26
|
+
}
|
|
27
|
+
return bar;
|
|
28
|
+
}
|
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/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
|
+
}
|