@a200888213/gbdocx 0.6.2 → 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/word/file.js +191 -191
- package/src/app/word/menus.js +201 -201
- package/src/app/word/view.js +159 -159
- package/src/app/word-app.js +487 -487
- package/src/kernel/docx/catalog.js +41 -41
- package/src/kernel/docx/menus.js +348 -348
- package/src/kernel/docx/wordShortcuts.js +37 -37
- package/src/kernel/editor/engine/editor.js +213 -213
- package/src/kernel/editor/engine/search.js +113 -113
- package/src/kernel/editor/pasteClean.js +293 -293
- package/src/kernel/notify.js +54 -54
- package/src/kernel/util/clipboard.js +89 -89
- package/src/kernel/util/safeValue.js +25 -25
- package/src/surface/editor-surface.js +5 -2
- package/src/ui/context-menu.js +4 -2
- package/src/ui/dialogs.js +249 -249
- package/src/ui/icons.js +245 -137
- package/src/ui/popup.js +79 -79
- package/styles/app.css +970 -970
- package/styles/word-panel.css +410 -410
package/src/ui/icons.js
CHANGED
|
@@ -1,137 +1,245 @@
|
|
|
1
|
-
// [文件] gbdocx·UI 图标集 ——
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
1
|
+
// [文件] gbdocx·UI 图标集 —— 工具栏/菜单图标注册表:字形数据取自开源 lucide 图标库(ISC 许可),
|
|
2
|
+
// 本文件只维护「业务键名 → lucide 图标」映射,不再手写任何 path。
|
|
3
|
+
import {
|
|
4
|
+
AArrowDown,
|
|
5
|
+
AArrowUp,
|
|
6
|
+
ALargeSmall,
|
|
7
|
+
AlignCenter,
|
|
8
|
+
AlignCenterVertical,
|
|
9
|
+
AlignHorizontalDistributeCenter,
|
|
10
|
+
AlignJustify,
|
|
11
|
+
AlignLeft,
|
|
12
|
+
AlignRight,
|
|
13
|
+
AlignVerticalSpaceAround,
|
|
14
|
+
ArrowDownToLine,
|
|
15
|
+
ArrowDownUp,
|
|
16
|
+
Asterisk,
|
|
17
|
+
Baseline,
|
|
18
|
+
Bold,
|
|
19
|
+
BookMarked,
|
|
20
|
+
BookOpen,
|
|
21
|
+
Bookmark,
|
|
22
|
+
Box,
|
|
23
|
+
BringToFront,
|
|
24
|
+
Calendar,
|
|
25
|
+
Captions,
|
|
26
|
+
CaseUpper,
|
|
27
|
+
ChartColumn,
|
|
28
|
+
Check,
|
|
29
|
+
ChevronDown,
|
|
30
|
+
ChevronUp,
|
|
31
|
+
Circle,
|
|
32
|
+
ClipboardPaste,
|
|
33
|
+
CodeXml,
|
|
34
|
+
Columns2,
|
|
35
|
+
Contrast,
|
|
36
|
+
Copy,
|
|
37
|
+
Download,
|
|
38
|
+
Droplets,
|
|
39
|
+
Eraser,
|
|
40
|
+
Eye,
|
|
41
|
+
File,
|
|
42
|
+
FileDigit,
|
|
43
|
+
FileSymlink,
|
|
44
|
+
FileText,
|
|
45
|
+
Frame,
|
|
46
|
+
GalleryVerticalEnd,
|
|
47
|
+
GitBranch,
|
|
48
|
+
Grid2X2,
|
|
49
|
+
Group,
|
|
50
|
+
Highlighter,
|
|
51
|
+
Image,
|
|
52
|
+
IndentDecrease,
|
|
53
|
+
IndentIncrease,
|
|
54
|
+
Italic,
|
|
55
|
+
Languages,
|
|
56
|
+
Link,
|
|
57
|
+
List,
|
|
58
|
+
ListEnd,
|
|
59
|
+
ListOrdered,
|
|
60
|
+
ListTree,
|
|
61
|
+
Mail,
|
|
62
|
+
Maximize,
|
|
63
|
+
MessageSquare,
|
|
64
|
+
Minimize,
|
|
65
|
+
Minus,
|
|
66
|
+
Omega,
|
|
67
|
+
PaintBucket,
|
|
68
|
+
PaintRoller,
|
|
69
|
+
Paintbrush,
|
|
70
|
+
Palette,
|
|
71
|
+
PanelRight,
|
|
72
|
+
Paperclip,
|
|
73
|
+
Pencil,
|
|
74
|
+
Printer,
|
|
75
|
+
Quote,
|
|
76
|
+
RectangleVertical,
|
|
77
|
+
Redo2,
|
|
78
|
+
RefreshCw,
|
|
79
|
+
RotateCcw,
|
|
80
|
+
RotateCw,
|
|
81
|
+
Ruler,
|
|
82
|
+
Scan,
|
|
83
|
+
Scissors,
|
|
84
|
+
Search,
|
|
85
|
+
SendToBack,
|
|
86
|
+
SeparatorHorizontal,
|
|
87
|
+
SeparatorVertical,
|
|
88
|
+
Shapes,
|
|
89
|
+
Sigma,
|
|
90
|
+
Sparkles,
|
|
91
|
+
SquareDashed,
|
|
92
|
+
SquareSplitVertical,
|
|
93
|
+
Star,
|
|
94
|
+
Strikethrough,
|
|
95
|
+
Subscript,
|
|
96
|
+
Superscript,
|
|
97
|
+
SwatchBook,
|
|
98
|
+
Table,
|
|
99
|
+
TableProperties,
|
|
100
|
+
Tag,
|
|
101
|
+
Tally4,
|
|
102
|
+
Text,
|
|
103
|
+
TextSelect,
|
|
104
|
+
Type,
|
|
105
|
+
Underline,
|
|
106
|
+
Undo2,
|
|
107
|
+
Wallpaper,
|
|
108
|
+
Workflow,
|
|
109
|
+
WrapText,
|
|
110
|
+
X,
|
|
111
|
+
ZoomIn,
|
|
112
|
+
ZoomOut,
|
|
113
|
+
} from 'gbicons/nodes';
|
|
114
|
+
import { iconNodeSvg } from 'gbicons/nodes';
|
|
115
|
+
|
|
116
|
+
/** 业务键名 → lucide IconNode */
|
|
117
|
+
const NODES = {
|
|
118
|
+
bold: Bold,
|
|
119
|
+
italic: Italic,
|
|
120
|
+
underline: Underline,
|
|
121
|
+
strike: Strikethrough,
|
|
122
|
+
superscript: Superscript,
|
|
123
|
+
subscript: Subscript,
|
|
124
|
+
colorIcon: Baseline,
|
|
125
|
+
bgIcon: PaintBucket,
|
|
126
|
+
alignLeft: AlignLeft,
|
|
127
|
+
alignCenter: AlignCenter,
|
|
128
|
+
alignRight: AlignRight,
|
|
129
|
+
alignJustify: AlignJustify,
|
|
130
|
+
bullet: List,
|
|
131
|
+
ordered: ListOrdered,
|
|
132
|
+
indentAdd: IndentIncrease,
|
|
133
|
+
indentReduce: IndentDecrease,
|
|
134
|
+
quote: Quote,
|
|
135
|
+
code: CodeXml,
|
|
136
|
+
table: Table,
|
|
137
|
+
image: Image,
|
|
138
|
+
linkIcon: Link,
|
|
139
|
+
divider: Minus,
|
|
140
|
+
pageBreak: SquareSplitVertical,
|
|
141
|
+
undo: Undo2,
|
|
142
|
+
redo: Redo2,
|
|
143
|
+
clear: X,
|
|
144
|
+
chevronDown: ChevronDown,
|
|
145
|
+
find: Search,
|
|
146
|
+
pageSetup: FileText,
|
|
147
|
+
columns: Columns2,
|
|
148
|
+
headerFooter: GalleryVerticalEnd,
|
|
149
|
+
footnote: Asterisk,
|
|
150
|
+
comment: MessageSquare,
|
|
151
|
+
toc: ListTree,
|
|
152
|
+
watermark: Droplets,
|
|
153
|
+
pageNumber: FileDigit,
|
|
154
|
+
suggest: Pencil,
|
|
155
|
+
accept: Check,
|
|
156
|
+
reject: X,
|
|
157
|
+
print: Printer,
|
|
158
|
+
export: Download,
|
|
159
|
+
zoomIn: ZoomIn,
|
|
160
|
+
zoomOut: ZoomOut,
|
|
161
|
+
specialChars: Omega,
|
|
162
|
+
bookmark: Bookmark,
|
|
163
|
+
panel: PanelRight,
|
|
164
|
+
eye: Eye,
|
|
165
|
+
fullscreen: Maximize,
|
|
166
|
+
fullscreenExit: Minimize,
|
|
167
|
+
margin: SquareDashed,
|
|
168
|
+
zoomReset: RotateCcw,
|
|
169
|
+
selectAll: TextSelect,
|
|
170
|
+
paste: ClipboardPaste,
|
|
171
|
+
cut: Scissors,
|
|
172
|
+
copy: Copy,
|
|
173
|
+
formatPainter: PaintRoller,
|
|
174
|
+
growFont: AArrowUp,
|
|
175
|
+
shrinkFont: AArrowDown,
|
|
176
|
+
eraser: Eraser,
|
|
177
|
+
highlight: Highlighter,
|
|
178
|
+
pinyin: Languages,
|
|
179
|
+
circleChar: Circle,
|
|
180
|
+
multiLevel: ListTree,
|
|
181
|
+
outdent: IndentDecrease,
|
|
182
|
+
distribute: AlignHorizontalDistributeCenter,
|
|
183
|
+
lineSpacing: AlignVerticalSpaceAround,
|
|
184
|
+
shading: Paintbrush,
|
|
185
|
+
borders: Grid2X2,
|
|
186
|
+
newStyle: Palette,
|
|
187
|
+
coverPage: BookOpen,
|
|
188
|
+
blankPage: File,
|
|
189
|
+
chart: ChartColumn,
|
|
190
|
+
screenshot: Scan,
|
|
191
|
+
shape: Shapes,
|
|
192
|
+
iconGallery: Star,
|
|
193
|
+
mindMap: GitBranch,
|
|
194
|
+
flowChart: Workflow,
|
|
195
|
+
textBox: Type,
|
|
196
|
+
wordArt: CaseUpper,
|
|
197
|
+
equation: Sigma,
|
|
198
|
+
insertText: Text,
|
|
199
|
+
object: Box,
|
|
200
|
+
date: Calendar,
|
|
201
|
+
attachment: Paperclip,
|
|
202
|
+
fileLink: FileSymlink,
|
|
203
|
+
crossReference: BookMarked,
|
|
204
|
+
theme: Contrast,
|
|
205
|
+
colors: SwatchBook,
|
|
206
|
+
effects: Sparkles,
|
|
207
|
+
fontCollection: ALargeSmall,
|
|
208
|
+
orientation: RectangleVertical,
|
|
209
|
+
paperSize: Ruler,
|
|
210
|
+
textDirection: ArrowDownUp,
|
|
211
|
+
separator: SeparatorVertical,
|
|
212
|
+
lineNumbers: Tally4,
|
|
213
|
+
background: Wallpaper,
|
|
214
|
+
pageBorder: Frame,
|
|
215
|
+
paperSettings: TableProperties,
|
|
216
|
+
wrap: WrapText,
|
|
217
|
+
alignObjects: AlignCenterVertical,
|
|
218
|
+
groupObjects: Group,
|
|
219
|
+
rotate: RotateCw,
|
|
220
|
+
bringForward: BringToFront,
|
|
221
|
+
sendBackward: SendToBack,
|
|
222
|
+
updateToc: RefreshCw,
|
|
223
|
+
tocLevel: ListEnd,
|
|
224
|
+
prevFootnote: ChevronUp,
|
|
225
|
+
nextFootnote: ChevronDown,
|
|
226
|
+
insertEndnote: ArrowDownToLine,
|
|
227
|
+
prevEndnote: ChevronUp,
|
|
228
|
+
nextEndnote: ChevronDown,
|
|
229
|
+
footnoteSep: SeparatorHorizontal,
|
|
230
|
+
caption: Captions,
|
|
231
|
+
markCitation: Quote,
|
|
232
|
+
insertIndex: Tag,
|
|
233
|
+
updateIndex: RefreshCw,
|
|
234
|
+
mail: Mail,
|
|
235
|
+
download: Download,
|
|
236
|
+
rename: Pencil,
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/** 预渲染成 16px / 1.9 描边的内联串(24 网格 stroke 风,与功能区排版对齐) */
|
|
240
|
+
const ICONS = Object.fromEntries(
|
|
241
|
+
Object.entries(NODES).map(([name, node]) => [name, iconNodeSvg(node, { sw: 1.9 })]),
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
/** 取图标 SVG 串(未登记返回空串) */
|
|
245
|
+
export const icon = (name) => ICONS[name] || '';
|
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
|
+
}
|