@a200888213/gbdocx 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +205 -2
- package/index.html +24 -0
- package/package.json +47 -6
- package/serve.mjs +20 -0
- package/src/app/file-io.js +122 -0
- package/src/app/main.js +9 -0
- package/src/app/word/catalog.js +52 -0
- package/src/app/word/comments.js +112 -0
- package/src/app/word/doc.js +113 -0
- package/src/app/word/edit.js +72 -0
- package/src/app/word/file.js +162 -0
- package/src/app/word/hf.js +76 -0
- package/src/app/word/layout.js +152 -0
- package/src/app/word/menus.js +175 -0
- package/src/app/word/notes.js +110 -0
- package/src/app/word/shared.js +22 -0
- package/src/app/word/view.js +88 -0
- package/src/app/word-app.js +440 -0
- package/src/app/word-sheets.js +101 -0
- package/src/kernel/docx/catalog.js +41 -0
- package/src/kernel/docx/codec/blocks.js +123 -0
- package/src/kernel/docx/codec/carry.js +129 -0
- package/src/kernel/docx/codec/comments.js +25 -0
- package/src/kernel/docx/codec/docx.js +198 -0
- package/src/kernel/docx/codec/docxStyles.js +177 -0
- package/src/kernel/docx/codec/fonts.js +195 -0
- package/src/kernel/docx/codec/helpers.js +161 -0
- package/src/kernel/docx/codec/hfHtml.js +152 -0
- package/src/kernel/docx/codec/html.js +739 -0
- package/src/kernel/docx/codec/losses.js +420 -0
- package/src/kernel/docx/codec/markdown.js +170 -0
- package/src/kernel/docx/codec/parts.js +249 -0
- package/src/kernel/docx/codec/read.js +603 -0
- package/src/kernel/docx/codec/render.js +1156 -0
- package/src/kernel/docx/codec/roundtrip.js +236 -0
- package/src/kernel/docx/codec/write.js +833 -0
- package/src/kernel/docx/comments.js +14 -0
- package/src/kernel/docx/constants.js +63 -0
- package/src/kernel/docx/menus.js +211 -0
- package/src/kernel/docx/pageGeometry.js +121 -0
- package/src/kernel/docx/pagination.js +481 -0
- package/src/kernel/docx/watermark.js +45 -0
- package/src/kernel/docx/wordShortcuts.js +37 -0
- package/src/kernel/editor/constants.js +8 -0
- package/src/kernel/editor/engine/blocks.js +594 -0
- package/src/kernel/editor/engine/blocksEnter.js +109 -0
- package/src/kernel/editor/engine/blocksInsert.js +254 -0
- package/src/kernel/editor/engine/blocksStructure.js +208 -0
- package/src/kernel/editor/engine/commands.js +180 -0
- package/src/kernel/editor/engine/dom.js +622 -0
- package/src/kernel/editor/engine/editor.js +213 -0
- package/src/kernel/editor/engine/history.js +149 -0
- package/src/kernel/editor/engine/inline.js +656 -0
- package/src/kernel/editor/engine/input.js +554 -0
- package/src/kernel/editor/engine/interaction.js +137 -0
- package/src/kernel/editor/engine/pointer.js +39 -0
- package/src/kernel/editor/engine/revision.js +138 -0
- package/src/kernel/editor/engine/schema.js +417 -0
- package/src/kernel/editor/engine/search.js +113 -0
- package/src/kernel/editor/engine/selection.js +119 -0
- package/src/kernel/editor/engine/state.js +118 -0
- package/src/kernel/editor/engine/tableClip.js +154 -0
- package/src/kernel/editor/engine/tableGrid.js +55 -0
- package/src/kernel/editor/engine/tableInteraction.js +371 -0
- package/src/kernel/editor/engine/tableMenu.js +250 -0
- package/src/kernel/editor/engine/tableOps.js +178 -0
- package/src/kernel/editor/engine/tables.js +732 -0
- package/src/kernel/editor/overlayGeometry.js +20 -0
- package/src/kernel/editor/pasteClean.js +293 -0
- package/src/kernel/editor/utils.js +49 -0
- package/src/kernel/keyboard.js +114 -0
- package/src/kernel/notify.js +54 -0
- package/src/kernel/textStyles.js +84 -0
- package/src/kernel/util/clipboard.js +89 -0
- package/src/kernel/util/imageFile.js +91 -0
- package/src/kernel/util/safeValue.js +25 -0
- package/src/kernel/util/shortcutKey.js +33 -0
- package/src/surface/editor-surface.js +469 -0
- package/src/ui/color-panel.js +69 -0
- package/src/ui/context-menu.js +130 -0
- package/src/ui/dialogs.js +249 -0
- package/src/ui/find-bar.js +151 -0
- package/src/ui/icons.js +36 -0
- package/src/ui/menubar.js +28 -0
- package/src/ui/panels.js +115 -0
- package/src/ui/popup.js +79 -0
- package/src/ui/prompt.js +89 -0
- package/src/ui/special-chars.js +39 -0
- package/src/ui/statusbar.js +81 -0
- package/src/ui/toolbar.js +199 -0
- package/styles/app.css +944 -0
- package/styles/editor-content.css +440 -0
- package/styles/tokens.css +154 -0
- package/styles/word-panel.css +450 -0
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
// [文件] gbdocx·Dialogs —— 通用模态 + 页面设置 / 页边距 / 分栏 对话框
|
|
2
|
+
import { HF_MODES, PAGE_NUMBER_ALIGNS, PAGE_NUMBER_FORMATS, PAGE_SIZES, defaultPageConf, defaultPageNumbers, defaultWatermark } from '../kernel/docx/constants.js';
|
|
3
|
+
|
|
4
|
+
/** 通用模态:build(bodyEl) 填充内容;onConfirm() 返回 false 可阻止关闭 */
|
|
5
|
+
export function openModal({ title, build, onConfirm, confirmText = '确定', width = 460 }) {
|
|
6
|
+
const overlay = document.createElement('div');
|
|
7
|
+
overlay.className = 'gbd-modal-overlay';
|
|
8
|
+
const box = document.createElement('div');
|
|
9
|
+
box.className = 'gbd-modal';
|
|
10
|
+
box.style.width = `${width}px`;
|
|
11
|
+
box.innerHTML = `
|
|
12
|
+
<div class="gbd-modal__head"><span class="gbd-modal__title"></span><button type="button" class="gbd-modal__close">✕</button></div>
|
|
13
|
+
<div class="gbd-modal__body"></div>
|
|
14
|
+
<div class="gbd-modal__foot">
|
|
15
|
+
<button type="button" class="gbd-btn" data-act="cancel">取消</button>
|
|
16
|
+
<button type="button" class="gbd-btn gbd-btn--primary" data-act="ok"></button>
|
|
17
|
+
</div>`;
|
|
18
|
+
box.querySelector('.gbd-modal__title').textContent = title;
|
|
19
|
+
box.querySelector('[data-act="ok"]').textContent = confirmText;
|
|
20
|
+
const bodyEl = box.querySelector('.gbd-modal__body');
|
|
21
|
+
const close = () => {
|
|
22
|
+
document.removeEventListener('keydown', onKey, true);
|
|
23
|
+
overlay.parentElement?.remove();
|
|
24
|
+
};
|
|
25
|
+
const onKey = (e) => {
|
|
26
|
+
if (e.key === 'Escape') {
|
|
27
|
+
e.stopPropagation();
|
|
28
|
+
close();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const confirm = () => {
|
|
32
|
+
if (onConfirm && onConfirm() === false) return;
|
|
33
|
+
close();
|
|
34
|
+
};
|
|
35
|
+
box.querySelector('[data-act="cancel"]').addEventListener('click', close);
|
|
36
|
+
box.querySelector('.gbd-modal__close').addEventListener('click', close);
|
|
37
|
+
box.querySelector('[data-act="ok"]').addEventListener('click', confirm);
|
|
38
|
+
overlay.addEventListener('mousedown', (e) => {
|
|
39
|
+
if (e.target === overlay) close();
|
|
40
|
+
});
|
|
41
|
+
document.addEventListener('keydown', onKey, true);
|
|
42
|
+
build?.(bodyEl, close);
|
|
43
|
+
overlay.appendChild(box);
|
|
44
|
+
// 样式作用域:模态挂 body,但仍须落在 .word-scope 子树里
|
|
45
|
+
const scope = document.createElement('div');
|
|
46
|
+
scope.className = 'word-scope';
|
|
47
|
+
scope.appendChild(overlay);
|
|
48
|
+
document.body.appendChild(scope);
|
|
49
|
+
return { close };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function field(labelText, control) {
|
|
53
|
+
const row = document.createElement('label');
|
|
54
|
+
row.className = 'gbd-field';
|
|
55
|
+
const span = document.createElement('span');
|
|
56
|
+
span.className = 'gbd-field__label';
|
|
57
|
+
span.textContent = labelText;
|
|
58
|
+
row.append(span, control);
|
|
59
|
+
return row;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function selectEl(options, value) {
|
|
63
|
+
const sel = document.createElement('select');
|
|
64
|
+
sel.className = 'gbd-input';
|
|
65
|
+
for (const o of options) {
|
|
66
|
+
const opt = document.createElement('option');
|
|
67
|
+
opt.value = o.value;
|
|
68
|
+
opt.textContent = o.label;
|
|
69
|
+
sel.appendChild(opt);
|
|
70
|
+
}
|
|
71
|
+
sel.value = value ?? '';
|
|
72
|
+
return sel;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function numberEl(value, { min, max, step = 1 } = {}) {
|
|
76
|
+
const input = document.createElement('input');
|
|
77
|
+
input.type = 'number';
|
|
78
|
+
input.className = 'gbd-input';
|
|
79
|
+
if (min !== undefined) input.min = min;
|
|
80
|
+
if (max !== undefined) input.max = max;
|
|
81
|
+
input.step = step;
|
|
82
|
+
input.value = value;
|
|
83
|
+
return input;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** 页面设置对话框(纸张/方向/分栏/页码/水印/页眉页脚),确认回调收到 {page, pageNumbers, watermark, header, footer} */
|
|
87
|
+
export function openPageSetupDialog(current, onConfirm) {
|
|
88
|
+
const page = { ...defaultPageConf(), ...(current.page || {}) };
|
|
89
|
+
const pn = { ...defaultPageNumbers(), ...(current.pageNumbers || {}) };
|
|
90
|
+
const wm = { ...defaultWatermark(), ...(current.watermark || {}) };
|
|
91
|
+
const margins = { ...page.margins };
|
|
92
|
+
let out = null;
|
|
93
|
+
openModal({
|
|
94
|
+
title: '页面设置',
|
|
95
|
+
width: 520,
|
|
96
|
+
build: (body) => {
|
|
97
|
+
const grid = document.createElement('div');
|
|
98
|
+
grid.className = 'gbd-field-grid';
|
|
99
|
+
const sizeSel = selectEl(Object.keys(PAGE_SIZES).map((k) => ({ value: k, label: k })), page.size);
|
|
100
|
+
const orientSel = selectEl([{ value: 'portrait', label: '纵向' }, { value: 'landscape', label: '横向' }], page.orientation);
|
|
101
|
+
const colsSel = numberEl(page.columns || 1, { min: 1, max: 3 });
|
|
102
|
+
grid.append(field('纸张', sizeSel), field('方向', orientSel), field('分栏', colsSel));
|
|
103
|
+
body.appendChild(grid);
|
|
104
|
+
|
|
105
|
+
const marginGrid = document.createElement('div');
|
|
106
|
+
marginGrid.className = 'gbd-field-grid';
|
|
107
|
+
const mTop = numberEl(margins.top, { min: 0, step: 0.1 });
|
|
108
|
+
const mBottom = numberEl(margins.bottom, { min: 0, step: 0.1 });
|
|
109
|
+
const mLeft = numberEl(margins.left, { min: 0, step: 0.1 });
|
|
110
|
+
const mRight = numberEl(margins.right, { min: 0, step: 0.1 });
|
|
111
|
+
marginGrid.append(field('上边距 mm', mTop), field('下边距 mm', mBottom), field('左边距 mm', mLeft), field('右边距 mm', mRight));
|
|
112
|
+
body.appendChild(marginGrid);
|
|
113
|
+
|
|
114
|
+
const section = document.createElement('div');
|
|
115
|
+
section.className = 'gbd-field-section';
|
|
116
|
+
section.textContent = '页码';
|
|
117
|
+
body.appendChild(section);
|
|
118
|
+
const pnGrid = document.createElement('div');
|
|
119
|
+
pnGrid.className = 'gbd-field-grid';
|
|
120
|
+
const pnEnabled = document.createElement('input');
|
|
121
|
+
pnEnabled.type = 'checkbox';
|
|
122
|
+
pnEnabled.checked = pn.enabled;
|
|
123
|
+
const pnAlign = selectEl(PAGE_NUMBER_ALIGNS, pn.align);
|
|
124
|
+
const pnFormat = selectEl(PAGE_NUMBER_FORMATS, pn.format);
|
|
125
|
+
const pnStart = numberEl(pn.start, { min: 1 });
|
|
126
|
+
pnGrid.append(field('显示页码', pnEnabled), field('位置', pnAlign), field('格式', pnFormat), field('起始页码', pnStart));
|
|
127
|
+
body.appendChild(pnGrid);
|
|
128
|
+
|
|
129
|
+
const wmSection = document.createElement('div');
|
|
130
|
+
wmSection.className = 'gbd-field-section';
|
|
131
|
+
wmSection.textContent = '水印';
|
|
132
|
+
body.appendChild(wmSection);
|
|
133
|
+
const wmGrid = document.createElement('div');
|
|
134
|
+
wmGrid.className = 'gbd-field-grid';
|
|
135
|
+
const wmText = document.createElement('input');
|
|
136
|
+
wmText.type = 'text';
|
|
137
|
+
wmText.className = 'gbd-input';
|
|
138
|
+
wmText.value = wm.data || '';
|
|
139
|
+
wmText.placeholder = '留空即无水印';
|
|
140
|
+
const wmColor = document.createElement('input');
|
|
141
|
+
wmColor.type = 'color';
|
|
142
|
+
wmColor.className = 'gbd-input';
|
|
143
|
+
wmColor.value = /^#[0-9a-f]{6}$/i.test(wm.color || '') ? wm.color : '#AEB5C0';
|
|
144
|
+
const wmSize = numberEl(wm.size, { min: 20, max: 400 });
|
|
145
|
+
const wmOpacity = numberEl(wm.opacity, { min: 0, max: 1, step: 0.05 });
|
|
146
|
+
wmGrid.append(field('水印文字', wmText), field('颜色', wmColor), field('字号 pt', wmSize), field('透明度', wmOpacity));
|
|
147
|
+
body.appendChild(wmGrid);
|
|
148
|
+
|
|
149
|
+
const hfSection = document.createElement('div');
|
|
150
|
+
hfSection.className = 'gbd-field-section';
|
|
151
|
+
hfSection.textContent = '页眉页脚';
|
|
152
|
+
body.appendChild(hfSection);
|
|
153
|
+
const hfGrid = document.createElement('div');
|
|
154
|
+
hfGrid.className = 'gbd-field-grid';
|
|
155
|
+
const hfMode = selectEl(HF_MODES, current.hfMode || 'all');
|
|
156
|
+
const plainHf = (html) => (html || '').replace(/<[^>]*>/g, '');
|
|
157
|
+
const textInput = (value) => {
|
|
158
|
+
const input = document.createElement('input');
|
|
159
|
+
input.type = 'text';
|
|
160
|
+
input.className = 'gbd-input';
|
|
161
|
+
input.value = value;
|
|
162
|
+
return input;
|
|
163
|
+
};
|
|
164
|
+
const hfHeader = textInput(plainHf(current.header));
|
|
165
|
+
const hfFooter = textInput(plainHf(current.footer));
|
|
166
|
+
hfGrid.append(field('重复模式', hfMode), field('页眉文字', hfHeader), field('页脚文字', hfFooter));
|
|
167
|
+
body.appendChild(hfGrid);
|
|
168
|
+
|
|
169
|
+
const firstGrid = document.createElement('div');
|
|
170
|
+
firstGrid.className = 'gbd-field-grid';
|
|
171
|
+
const hfHeaderFirst = textInput(plainHf(current.headerFirst));
|
|
172
|
+
const hfFooterFirst = textInput(plainHf(current.footerFirst));
|
|
173
|
+
firstGrid.append(field('首页页眉', hfHeaderFirst), field('首页页脚', hfFooterFirst));
|
|
174
|
+
body.appendChild(firstGrid);
|
|
175
|
+
const syncFirst = () => {
|
|
176
|
+
const show = hfMode.value === 'except-first' || hfMode.value === 'odd-even-first';
|
|
177
|
+
firstGrid.style.display = show ? '' : 'none';
|
|
178
|
+
};
|
|
179
|
+
hfMode.addEventListener('change', syncFirst);
|
|
180
|
+
syncFirst();
|
|
181
|
+
|
|
182
|
+
// 文本框只承载纯文本:值没改就保留原有富文本 HTML,改过的才用新文字覆盖
|
|
183
|
+
const picked = (input, original) => (input.value === plainHf(original) ? original : input.value);
|
|
184
|
+
|
|
185
|
+
out = () => ({
|
|
186
|
+
page: {
|
|
187
|
+
...page,
|
|
188
|
+
size: sizeSel.value,
|
|
189
|
+
orientation: orientSel.value,
|
|
190
|
+
columns: Math.max(1, Math.min(3, Number(colsSel.value) || 1)),
|
|
191
|
+
margins: { top: Number(mTop.value), bottom: Number(mBottom.value), left: Number(mLeft.value), right: Number(mRight.value) },
|
|
192
|
+
},
|
|
193
|
+
pageNumbers: { enabled: pnEnabled.checked, align: pnAlign.value, format: pnFormat.value, start: Number(pnStart.value) || 1 },
|
|
194
|
+
watermark: { data: wmText.value, color: wmColor.value, size: Number(wmSize.value) || 120, opacity: Number(wmOpacity.value) || 0.3 },
|
|
195
|
+
hfMode: hfMode.value,
|
|
196
|
+
header: picked(hfHeader, current.header),
|
|
197
|
+
footer: picked(hfFooter, current.footer),
|
|
198
|
+
headerFirst: picked(hfHeaderFirst, current.headerFirst),
|
|
199
|
+
footerFirst: picked(hfFooterFirst, current.footerFirst),
|
|
200
|
+
});
|
|
201
|
+
},
|
|
202
|
+
onConfirm: () => onConfirm(out()),
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** 页边距对话框 */
|
|
207
|
+
export function openMarginDialog(current, onConfirm) {
|
|
208
|
+
const margins = { ...defaultPageConf().margins, ...(current || {}) };
|
|
209
|
+
let out = null;
|
|
210
|
+
openModal({
|
|
211
|
+
title: '页边距',
|
|
212
|
+
width: 380,
|
|
213
|
+
build: (body) => {
|
|
214
|
+
const grid = document.createElement('div');
|
|
215
|
+
grid.className = 'gbd-field-grid';
|
|
216
|
+
const top = numberEl(margins.top, { min: 0, step: 0.1 });
|
|
217
|
+
const bottom = numberEl(margins.bottom, { min: 0, step: 0.1 });
|
|
218
|
+
const left = numberEl(margins.left, { min: 0, step: 0.1 });
|
|
219
|
+
const right = numberEl(margins.right, { min: 0, step: 0.1 });
|
|
220
|
+
grid.append(field('上 mm', top), field('下 mm', bottom), field('左 mm', left), field('右 mm', right));
|
|
221
|
+
body.appendChild(grid);
|
|
222
|
+
out = () => ({ top: Number(top.value), bottom: Number(bottom.value), left: Number(left.value), right: Number(right.value) });
|
|
223
|
+
},
|
|
224
|
+
onConfirm: () => onConfirm(out()),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** 分栏对话框 */
|
|
229
|
+
export function openColumnDialog(current, onConfirm) {
|
|
230
|
+
const page = { ...defaultPageConf(), ...(current || {}) };
|
|
231
|
+
let out = null;
|
|
232
|
+
openModal({
|
|
233
|
+
title: '分栏',
|
|
234
|
+
width: 360,
|
|
235
|
+
build: (body) => {
|
|
236
|
+
const grid = document.createElement('div');
|
|
237
|
+
grid.className = 'gbd-field-grid';
|
|
238
|
+
const cols = numberEl(page.columns || 1, { min: 1, max: 3 });
|
|
239
|
+
const gap = numberEl(page.colGap || 8, { min: 0, step: 0.5 });
|
|
240
|
+
const sepOn = document.createElement('input');
|
|
241
|
+
sepOn.type = 'checkbox';
|
|
242
|
+
sepOn.checked = !!page.colSeparator;
|
|
243
|
+
grid.append(field('栏数', cols), field('栏间距 mm', gap), field('分隔线', sepOn));
|
|
244
|
+
body.appendChild(grid);
|
|
245
|
+
out = () => ({ columns: Math.max(1, Math.min(3, Number(cols.value) || 1)), colGap: Number(gap.value), colSeparator: sepOn.checked });
|
|
246
|
+
},
|
|
247
|
+
onConfirm: () => onConfirm(out()),
|
|
248
|
+
});
|
|
249
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// [文件] gbdocx·FindBar —— 查找与替换浮条(两行:查找行常显、替换行默认收起;走内核 search 的 CSS Highlight 高亮,正文零改写)
|
|
2
|
+
export class FindBar {
|
|
3
|
+
/** @param {() => object|null} getEditor 内核实例读取器 */
|
|
4
|
+
constructor(getEditor) {
|
|
5
|
+
this.getEditor = getEditor;
|
|
6
|
+
this.replaceOpen = false;
|
|
7
|
+
this.readOnly = false;
|
|
8
|
+
this.el = document.createElement('div');
|
|
9
|
+
this.el.className = 'gbd-findbar';
|
|
10
|
+
this.el.innerHTML = `
|
|
11
|
+
<div class="gbd-findbar__row">
|
|
12
|
+
<input class="gbd-findbar__input" type="text" placeholder="查找…" />
|
|
13
|
+
<span class="gbd-findbar__count">0/0</span>
|
|
14
|
+
<button type="button" class="gbd-iconbtn" data-act="prev" title="上一个">↑</button>
|
|
15
|
+
<button type="button" class="gbd-iconbtn" data-act="next" title="下一个">↓</button>
|
|
16
|
+
<label class="gbd-findbar__case"><input type="checkbox" data-act="case" />区分大小写</label>
|
|
17
|
+
<button type="button" class="gbd-iconbtn" data-act="toggleReplace" title="展开替换"></button>
|
|
18
|
+
<button type="button" class="gbd-iconbtn" data-act="close" title="关闭">✕</button>
|
|
19
|
+
</div>
|
|
20
|
+
<div class="gbd-findbar__row" data-k="replace" hidden>
|
|
21
|
+
<input class="gbd-findbar__replace" type="text" placeholder="替换为…" />
|
|
22
|
+
<button type="button" class="gbd-btn" data-act="replace">替换</button>
|
|
23
|
+
<button type="button" class="gbd-btn" data-act="replaceAll">全部替换</button>
|
|
24
|
+
</div>`;
|
|
25
|
+
this.el.hidden = true;
|
|
26
|
+
this.input = this.el.querySelector('.gbd-findbar__input');
|
|
27
|
+
this.replaceInput = this.el.querySelector('.gbd-findbar__replace');
|
|
28
|
+
this.replaceRow = this.el.querySelector('[data-k="replace"]');
|
|
29
|
+
this.countEl = this.el.querySelector('.gbd-findbar__count');
|
|
30
|
+
this.caseEl = this.el.querySelector('[data-act="case"]');
|
|
31
|
+
this.toggleReplaceEl = this.el.querySelector('[data-act="toggleReplace"]');
|
|
32
|
+
this.bind();
|
|
33
|
+
this.syncReplace();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
bind() {
|
|
37
|
+
const on = (act, fn) => this.el.querySelector(`[data-act="${act}"]`).addEventListener('click', (e) => {
|
|
38
|
+
e.preventDefault();
|
|
39
|
+
fn();
|
|
40
|
+
});
|
|
41
|
+
this.input.addEventListener('input', () => this.refresh());
|
|
42
|
+
this.caseEl.addEventListener('change', () => this.refresh());
|
|
43
|
+
this.input.addEventListener('keydown', (e) => {
|
|
44
|
+
if (e.key === 'Enter') {
|
|
45
|
+
e.preventDefault();
|
|
46
|
+
e.shiftKey ? this.prev() : this.next();
|
|
47
|
+
} else if (e.key === 'Escape') {
|
|
48
|
+
e.preventDefault();
|
|
49
|
+
this.close();
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
this.replaceInput.addEventListener('keydown', (e) => {
|
|
53
|
+
if (e.key === 'Enter') {
|
|
54
|
+
e.preventDefault();
|
|
55
|
+
this.replaceCurrent();
|
|
56
|
+
} else if (e.key === 'Escape') {
|
|
57
|
+
e.preventDefault();
|
|
58
|
+
this.close();
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
on('prev', () => this.prev());
|
|
62
|
+
on('next', () => this.next());
|
|
63
|
+
on('replace', () => this.replaceCurrent());
|
|
64
|
+
on('replaceAll', () => this.replaceAll());
|
|
65
|
+
on('close', () => this.close());
|
|
66
|
+
on('toggleReplace', () => this.setReplaceOpen(!this.replaceOpen));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
get search() {
|
|
70
|
+
return this.getEditor()?.search || null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** 替换行展开/收起(展开时聚焦替换输入) */
|
|
74
|
+
setReplaceOpen(on) {
|
|
75
|
+
this.replaceOpen = !!on;
|
|
76
|
+
this.syncReplace();
|
|
77
|
+
if (this.replaceOpen) requestAnimationFrame(() => this.replaceInput.focus());
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
syncReplace() {
|
|
81
|
+
this.replaceRow.hidden = !this.replaceOpen;
|
|
82
|
+
this.toggleReplaceEl.textContent = this.replaceOpen ? '▴' : '▾';
|
|
83
|
+
this.toggleReplaceEl.title = this.replaceOpen ? '收起替换' : '展开替换';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
refresh() {
|
|
87
|
+
const s = this.search;
|
|
88
|
+
if (!s) return;
|
|
89
|
+
s.setKeyword(this.input.value, { caseSensitive: this.caseEl.checked });
|
|
90
|
+
this.renderCount();
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
renderCount() {
|
|
94
|
+
const s = this.search;
|
|
95
|
+
if (!s) return;
|
|
96
|
+
const snap = s.snapshot();
|
|
97
|
+
this.countEl.textContent = `${snap.total ? snap.activeIndex + 1 : 0}/${snap.total}`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
next() {
|
|
101
|
+
this.search?.next();
|
|
102
|
+
this.renderCount();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
prev() {
|
|
106
|
+
this.search?.prev();
|
|
107
|
+
this.renderCount();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
replaceCurrent() {
|
|
111
|
+
if (this.readOnly) return;
|
|
112
|
+
const ed = this.getEditor();
|
|
113
|
+
if (!ed) return;
|
|
114
|
+
ed.run(() => ed.search.replaceCurrent(this.replaceInput.value));
|
|
115
|
+
this.renderCount();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
replaceAll() {
|
|
119
|
+
if (this.readOnly) return;
|
|
120
|
+
const ed = this.getEditor();
|
|
121
|
+
if (!ed) return;
|
|
122
|
+
ed.run(() => ed.search.replaceAll(this.replaceInput.value));
|
|
123
|
+
this.renderCount();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** 只读档只留查找,替换行的入口与执行一并锁掉 */
|
|
127
|
+
setReadOnly(on) {
|
|
128
|
+
this.readOnly = !!on;
|
|
129
|
+
this.toggleReplaceEl.disabled = this.readOnly;
|
|
130
|
+
if (this.readOnly) this.setReplaceOpen(false);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 打开:replace=true(Ctrl+H / 菜单「查找和替换」)时展开替换行,否则只显示查找行 */
|
|
134
|
+
open(replace = false) {
|
|
135
|
+
this.el.hidden = false;
|
|
136
|
+
this.setReplaceOpen(replace && !this.readOnly);
|
|
137
|
+
requestAnimationFrame(() => {
|
|
138
|
+
this.input.focus();
|
|
139
|
+
this.input.select();
|
|
140
|
+
});
|
|
141
|
+
this.refresh();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
close() {
|
|
145
|
+
if (this.el.hidden) return;
|
|
146
|
+
this.el.hidden = true;
|
|
147
|
+
this.replaceOpen = false;
|
|
148
|
+
this.syncReplace();
|
|
149
|
+
this.search?.clear();
|
|
150
|
+
}
|
|
151
|
+
}
|
package/src/ui/icons.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// [文件] gbdocx·UI 图标集 —— 工具栏/菜单用的内联 SVG(stroke 风格,currentColor 上色)
|
|
2
|
+
const S = (body, size = 16) =>
|
|
3
|
+
`<svg viewBox="0 0 24 24" width="${size}" height="${size}" fill="none" stroke="currentColor" stroke-width="1.9" stroke-linecap="round" stroke-linejoin="round">${body}</svg>`;
|
|
4
|
+
|
|
5
|
+
export const ICONS = {
|
|
6
|
+
bold: S('<path d="M7 5h6a3.5 3.5 0 0 1 0 7H7zM7 12h7a3.5 3.5 0 0 1 0 7H7z"/>'),
|
|
7
|
+
italic: S('<path d="M15 5h-5M14 19H9M14 5l-4 14"/>'),
|
|
8
|
+
underline: S('<path d="M7 5v7a5 5 0 0 0 10 0V5M5 20h14"/>'),
|
|
9
|
+
strike: S('<path d="M5 12h14M8 8a3 3 0 0 1 3-3h3a3 3 0 0 1 0 6M16 16a3 3 0 0 1-3 3h-3a3 3 0 0 1-2-1"/>'),
|
|
10
|
+
superscript: S('<path d="M5 18l6-9 6 9M6 5h4l4 5"/>'),
|
|
11
|
+
subscript: S('<path d="M5 15l6-9 6 9M15 19h4l-4 4"/>'),
|
|
12
|
+
colorIcon: S('<path d="M6 18h12M8 14l4-9 4 9M7 14h10"/><path d="M4 21h16"/>'),
|
|
13
|
+
bgIcon: S('<path d="M4 20h16M6 14l4-9 4 9M7 14h10"/>'),
|
|
14
|
+
alignLeft: S('<path d="M4 6h16M4 12h10M4 18h14"/>'),
|
|
15
|
+
alignCenter: S('<path d="M4 6h16M7 12h10M5 18h14"/>'),
|
|
16
|
+
alignRight: S('<path d="M4 6h16M10 12h10M6 18h14"/>'),
|
|
17
|
+
alignJustify: S('<path d="M4 6h16M4 12h16M4 18h16"/>'),
|
|
18
|
+
bullet: S('<path d="M9 6h11M9 12h11M9 18h11M5 6h.01M5 12h.01M5 18h.01"/>'),
|
|
19
|
+
ordered: S('<path d="M10 6h10M10 12h10M10 18h10M4 5h1v3M4 11h2l-2 3h2M4 17h1.5a1 1 0 0 1 0 2H4h1.5a1 1 0 0 1 0 2H4"/>'),
|
|
20
|
+
indentAdd: S('<path d="M4 6h16M10 12h10M4 18h16M6 9l3 3-3 3"/>'),
|
|
21
|
+
indentReduce: S('<path d="M4 6h16M10 12h10M4 18h16M9 9l-3 3 3 3"/>'),
|
|
22
|
+
quote: S('<path d="M7 7H4v6h5V8M17 7h-3v6h5V8"/>'),
|
|
23
|
+
code: S('<path d="M9 8l-4 4 4 4M15 8l4 4-4 4"/>'),
|
|
24
|
+
table: S('<rect x="3" y="4" width="18" height="16" rx="1.5"/><path d="M3 10h18M3 15h18M9 4v16M15 4v16"/>'),
|
|
25
|
+
image: S('<rect x="3" y="4" width="18" height="16" rx="1.5"/><path d="M4 16l4-4 4 4 3-3 5 5"/><circle cx="9" cy="9" r="1.4"/>'),
|
|
26
|
+
linkIcon: S('<path d="M10 13a4 4 0 0 0 5.7 0l2.3-2.3a4 4 0 0 0-5.7-5.7L11 6"/><path d="M14 11a4 4 0 0 0-5.7 0L6 13.3a4 4 0 0 0 5.7 5.7L13 18"/>'),
|
|
27
|
+
divider: S('<path d="M3 12h18"/>'),
|
|
28
|
+
pageBreak: S('<path d="M4 4h16v6H4zM4 14h6v6H4zM14 14h6v6h-6z"/>'),
|
|
29
|
+
undo: S('<path d="M9 7L4 12l5 5M4 12h11a5 5 0 0 1 0 10"/>'),
|
|
30
|
+
redo: S('<path d="M15 7l5 5-5 5M20 12H9a5 5 0 0 0 0 10"/>'),
|
|
31
|
+
clear: S('<path d="M6 6l12 12M18 6L6 18"/>'),
|
|
32
|
+
chevronDown: S('<path d="M6 9l6 6 6-6"/>'),
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** 取图标 SVG 串(未登记返回空串) */
|
|
36
|
+
export const icon = (name) => ICONS[name] || '';
|
|
@@ -0,0 +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
|
+
}
|
package/src/ui/panels.js
ADDED
|
@@ -0,0 +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
|
+
}
|
package/src/ui/popup.js
ADDED
|
@@ -0,0 +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
|
+
}
|