@a200888213/gbdocx 0.3.0 → 0.5.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 +4 -3
- package/index.html +5 -0
- package/package.json +48 -47
- package/serve.mjs +3 -1
- package/src/app/word/edit.js +2 -2
- package/src/app/word/layout.js +5 -2
- package/src/app/word/menus.js +26 -0
- package/src/app/word/view.js +31 -1
- package/src/app/word-app.js +36 -21
- package/src/kernel/docx/menus.js +144 -7
- package/src/kernel/docx/pageGeometry.js +2 -0
- package/src/kernel/editor/engine/blocksStructure.js +19 -1
- package/src/kernel/editor/engine/commands.js +1 -0
- package/src/kernel/editor/engine/dom.js +42 -18
- package/src/kernel/editor/engine/tableInteraction.js +17 -0
- package/src/kernel/editor/engine/tableOps.js +1 -5
- package/src/surface/editor-surface.js +131 -0
- package/src/ui/context-menu.js +4 -1
- package/src/ui/icons.js +99 -2
- package/src/ui/ribbon.js +485 -0
- package/src/ui/toolbar.js +2 -169
- package/styles/app.css +139 -123
- package/styles/tokens.css +5 -5
- package/styles/word-panel.css +9 -49
package/src/ui/ribbon.js
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
// [文件] gbdocx·Ribbon —— WPS 风格选项卡栏(开始/插入/页面布局/引用/审阅/视图/章节/开发工具/特色功能/查找)
|
|
2
|
+
//
|
|
3
|
+
// [设计] 只装配功能区(选项卡 + 分组按钮),菜单栏/文件头/状态栏的结构与行为不动;
|
|
4
|
+
// 视觉密度、分组节奏与按钮尺寸统一由 app.css 的 ribbon 段控制。
|
|
5
|
+
// 「开始」页的分类与按钮顺序照 WPS 还原(剪贴板/字体/段落/样式);其余页同理解构。
|
|
6
|
+
// 本模块是纯装配:按钮只声明「调哪个命令/哪个 ctx 动作」,不含编辑逻辑。
|
|
7
|
+
// 编辑器命令走 io.run(内部挡只读);应用动作走 menuCtx(只读档已短路写操作)。
|
|
8
|
+
// 没接入的能力不摆按钮:占位按钮点了只会提示「暂未接入」,等于假入口。
|
|
9
|
+
import { icon } from './icons.js';
|
|
10
|
+
import { openMenu } from './context-menu.js';
|
|
11
|
+
import { makeSelect } from './toolbar.js';
|
|
12
|
+
import { HF_MODES, PAGE_NUMBER_ALIGNS } from '../kernel/docx/constants.js';
|
|
13
|
+
import { FONT_FAMILIES, FONT_SIZES, TEXT_COLORS, BG_COLORS } from '../kernel/textStyles.js';
|
|
14
|
+
|
|
15
|
+
/** 选项卡顺序即 WPS 截图顺序 */
|
|
16
|
+
const TABS = [
|
|
17
|
+
{ key: 'home', label: '开始' },
|
|
18
|
+
{ key: 'insert', label: '插入' },
|
|
19
|
+
{ key: 'layout', label: '页面布局' },
|
|
20
|
+
{ key: 'reference', label: '引用' },
|
|
21
|
+
{ key: 'review', label: '审阅' },
|
|
22
|
+
{ key: 'view', label: '视图' },
|
|
23
|
+
{ key: 'chapter', label: '章节' },
|
|
24
|
+
{ key: 'developer', label: '开发工具' },
|
|
25
|
+
{ key: 'feature', label: '特色功能' },
|
|
26
|
+
{ key: 'find', label: '查找', icon: 'find' },
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
/* ============ 描述符构造(纯数据,供渲染器消费) ============ */
|
|
30
|
+
|
|
31
|
+
/** 大按钮:图标在上、文字在下,WPS 功能区主按钮 */
|
|
32
|
+
const big = (iconName, title, onClick, opts = {}) => ({ kind: 'big', iconName, title, onClick, ...opts });
|
|
33
|
+
/** 小按钮:单行图标+文字(如 B/I/U、对齐、项目符号) */
|
|
34
|
+
const small = (iconName, title, onClick, opts = {}) => ({ kind: 'small', iconName, title, onClick, ...opts });
|
|
35
|
+
/** 下拉按钮:点击弹 menu;items 每次打开现求值 */
|
|
36
|
+
const menu = (iconName, title, items, opts = {}) => ({ kind: 'menu', iconName, title, items, ...opts });
|
|
37
|
+
/** 分组:带底部组名(剪贴板/字体/段落/样式…) */
|
|
38
|
+
const group = (title, ...items) => ({ kind: 'group', title, items });
|
|
39
|
+
/** 横向一排按钮(字体组内 B I U 一行) */
|
|
40
|
+
const row = (...items) => ({ kind: 'row', items });
|
|
41
|
+
/** 竖向堆叠若干行(一个分组里多行) */
|
|
42
|
+
const stack = (...rows) => ({ kind: 'stack', items: rows });
|
|
43
|
+
/** 样式卡片(样式名按自身字号预览,如 WPS 样式库);blockType 用于点亮当前段落样式 */
|
|
44
|
+
const styleCard = (preview, name, onClick, previewClass, blockType) => ({
|
|
45
|
+
kind: 'style', preview, name, onClick, previewClass,
|
|
46
|
+
active: blockType ? (s) => s.blockType === blockType : null,
|
|
47
|
+
});
|
|
48
|
+
/** 下拉选择框 */
|
|
49
|
+
const select = (spec) => ({ kind: 'select', spec });
|
|
50
|
+
|
|
51
|
+
/* ============ 渲染 ============ */
|
|
52
|
+
|
|
53
|
+
/** 收集按钮激活态求值器,refresh 时按现取状态统一点亮 */
|
|
54
|
+
class PanelContext {
|
|
55
|
+
constructor(io) {
|
|
56
|
+
this.io = io;
|
|
57
|
+
this.states = [];
|
|
58
|
+
this.swatches = [];
|
|
59
|
+
this.selects = [];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 登记按钮的激活/禁用求值器,refresh 时统一结算 */
|
|
63
|
+
trackState(button, active, disabled) {
|
|
64
|
+
if (!active && !disabled) return;
|
|
65
|
+
this.states.push({ button, active, disabled });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
button(desc) {
|
|
69
|
+
const b = document.createElement('button');
|
|
70
|
+
b.type = 'button';
|
|
71
|
+
// iconOnly 的菜单按钮(字体颜色/突出显示/行距)按小按钮渲染,不能沿用大按钮的 54px 高度
|
|
72
|
+
b.className = desc.kind === 'small' || desc.iconOnly ? 'gbd-rbtn--small' : 'gbd-rbtn';
|
|
73
|
+
b.title = desc.title;
|
|
74
|
+
const iconHtml = desc.iconName ? `<span class="gbd-rbtn__ico">${icon(desc.iconName)}</span>` : '';
|
|
75
|
+
const isMenu = desc.kind === 'menu';
|
|
76
|
+
const caret = isMenu ? '<span class="gbd-rbtn__caret">▾</span>' : '';
|
|
77
|
+
// iconOnly:只显示图标(B/I/U 等),文字留在 title 做提示
|
|
78
|
+
const labelHtml = desc.iconOnly ? '' : `<span class="gbd-rbtn__label">${desc.title}</span>`;
|
|
79
|
+
const swatchHtml = desc.swatch ? '<span class="gbd-rbtn__swatch"></span>' : '';
|
|
80
|
+
b.innerHTML = iconHtml + labelHtml + caret + swatchHtml;
|
|
81
|
+
if (desc.iconOnly) b.classList.add('gbd-rbtn--icon');
|
|
82
|
+
if (desc.swatch) b.classList.add('gbd-rbtn--swatch');
|
|
83
|
+
if (desc.swatch) this.swatches.push([b.querySelector('.gbd-rbtn__swatch'), desc.swatch]);
|
|
84
|
+
const io = this.io;
|
|
85
|
+
b.addEventListener('mousedown', (e) => e.preventDefault());
|
|
86
|
+
b.addEventListener('click', (e) => {
|
|
87
|
+
if (isMenu) {
|
|
88
|
+
openMenu({ anchor: e.currentTarget, items: desc.items(), placement: 'bottom-start' });
|
|
89
|
+
} else {
|
|
90
|
+
desc.onClick?.(e);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
this.trackState(b, desc.active, desc.disabled);
|
|
94
|
+
return b;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
node(item) {
|
|
98
|
+
switch (item.kind) {
|
|
99
|
+
case 'big':
|
|
100
|
+
case 'small':
|
|
101
|
+
case 'menu':
|
|
102
|
+
return this.button(item);
|
|
103
|
+
case 'style': {
|
|
104
|
+
const b = document.createElement('button');
|
|
105
|
+
b.type = 'button';
|
|
106
|
+
b.className = 'gbd-rstyle';
|
|
107
|
+
b.title = item.name;
|
|
108
|
+
b.innerHTML = `<span class="gbd-rstyle__preview ${item.previewClass || ''}">${item.preview}</span>`;
|
|
109
|
+
b.addEventListener('mousedown', (e) => e.preventDefault());
|
|
110
|
+
b.addEventListener('click', item.onClick);
|
|
111
|
+
this.trackState(b, item.active, null);
|
|
112
|
+
return b;
|
|
113
|
+
}
|
|
114
|
+
case 'row': {
|
|
115
|
+
const el = document.createElement('div');
|
|
116
|
+
el.className = 'gbd-rgroup__row';
|
|
117
|
+
for (const it of item.items) el.appendChild(this.node(it));
|
|
118
|
+
return el;
|
|
119
|
+
}
|
|
120
|
+
case 'stack': {
|
|
121
|
+
const el = document.createElement('div');
|
|
122
|
+
el.className = 'gbd-rgroup__col';
|
|
123
|
+
for (const it of item.items) el.appendChild(this.node(it));
|
|
124
|
+
return el;
|
|
125
|
+
}
|
|
126
|
+
case 'select': {
|
|
127
|
+
const s = makeSelect({ ...item.spec, get: item.spec.get });
|
|
128
|
+
this.selects.push(s.refresh);
|
|
129
|
+
return s.el;
|
|
130
|
+
}
|
|
131
|
+
case 'group': {
|
|
132
|
+
const el = document.createElement('div');
|
|
133
|
+
el.className = 'gbd-rgroup';
|
|
134
|
+
const body = document.createElement('div');
|
|
135
|
+
body.className = 'gbd-rgroup__body';
|
|
136
|
+
for (const it of item.items) body.appendChild(this.node(it));
|
|
137
|
+
const name = document.createElement('div');
|
|
138
|
+
name.className = 'gbd-rgroup__name';
|
|
139
|
+
name.textContent = item.title;
|
|
140
|
+
el.append(body, name);
|
|
141
|
+
return el;
|
|
142
|
+
}
|
|
143
|
+
default:
|
|
144
|
+
return document.createComment('unknown ribbon item');
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
render(panel, items) {
|
|
149
|
+
for (const item of items) panel.appendChild(this.node(item));
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
refresh() {
|
|
153
|
+
const state = this.io.getState();
|
|
154
|
+
for (const { button, active, disabled } of this.states) {
|
|
155
|
+
if (active) button.classList.toggle('gbd-is-active', Boolean(active(state)));
|
|
156
|
+
if (disabled) button.disabled = Boolean(disabled(state));
|
|
157
|
+
}
|
|
158
|
+
for (const [el, evaluate] of this.swatches) el.style.background = evaluate(state) || 'transparent';
|
|
159
|
+
for (const r of this.selects) r();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/* ============ 各选项卡面板(构建函数返回描述符数组) ============ */
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @param {object} io 见 buildRibbon 的 JSDoc;各构建函数通过闭包读取
|
|
167
|
+
* @returns {Record<string, (io) => Array>} key -> 描述符数组构建函数
|
|
168
|
+
*/
|
|
169
|
+
function panelDefs(io) {
|
|
170
|
+
const ctx = () => io.getCtx();
|
|
171
|
+
const state = () => io.getState();
|
|
172
|
+
const run = (name, ...args) => io.run(name, ...args);
|
|
173
|
+
|
|
174
|
+
/** 字体名下拉 */
|
|
175
|
+
const fontSelect = () => select({
|
|
176
|
+
title: '字体', width: 120,
|
|
177
|
+
options: [{ value: '', label: '默认字体' }, ...FONT_FAMILIES.map((f) => ({ value: f.value, label: f.label, style: { fontFamily: f.value } }))],
|
|
178
|
+
get: () => state().fontFamily || '',
|
|
179
|
+
onChange: (v) => (v ? run('setFontFamily', v) : run('unsetFontFamily')),
|
|
180
|
+
});
|
|
181
|
+
/** 字号下拉 */
|
|
182
|
+
const sizeSelect = () => select({
|
|
183
|
+
title: '字号', width: 48,
|
|
184
|
+
options: [{ value: '', label: '默认' }, ...FONT_SIZES.map((s) => ({ value: s, label: String(s) }))],
|
|
185
|
+
get: () => Number.parseFloat(state().fontSize) || '',
|
|
186
|
+
onChange: (v) => (v ? run('setFontSize', `${v}px`) : run('unsetFontSize')),
|
|
187
|
+
});
|
|
188
|
+
/** 字体色下拉(取色板现成值,点击即应用) */
|
|
189
|
+
const colorMenu = () => TEXT_COLORS.slice(0, 20).map((c) => ({
|
|
190
|
+
label: c,
|
|
191
|
+
onClick: () => run('setColor', c),
|
|
192
|
+
}));
|
|
193
|
+
const bgMenu = () => BG_COLORS.slice(0, 18).map((c) => ({
|
|
194
|
+
label: c,
|
|
195
|
+
onClick: () => run('setBackgroundColor', c),
|
|
196
|
+
}));
|
|
197
|
+
const pageNumberMenu = () => PAGE_NUMBER_ALIGNS.map((a) => ({
|
|
198
|
+
label: a.label,
|
|
199
|
+
checked: state().pageNumbers.enabled && state().pageNumbers.align === a.value,
|
|
200
|
+
onClick: () => ctx().insert.setPageNumber(a.value),
|
|
201
|
+
}));
|
|
202
|
+
const hfMenu = () => HF_MODES.map((m) => ({
|
|
203
|
+
label: m.label,
|
|
204
|
+
checked: state().hfMode === m.value,
|
|
205
|
+
onClick: () => ctx().format.setHfMode(m.value),
|
|
206
|
+
}));
|
|
207
|
+
|
|
208
|
+
return {
|
|
209
|
+
// ---------- 开始 ----------
|
|
210
|
+
home: () => [
|
|
211
|
+
group('剪贴板',
|
|
212
|
+
big('paste', '粘贴', () => ctx().edit.paste()),
|
|
213
|
+
row(
|
|
214
|
+
small('cut', '剪切', () => ctx().edit.cut()),
|
|
215
|
+
small('copy', '复制', () => ctx().edit.copy())
|
|
216
|
+
)
|
|
217
|
+
),
|
|
218
|
+
group('字体',
|
|
219
|
+
stack(
|
|
220
|
+
row(fontSelect(), sizeSelect(),
|
|
221
|
+
small('growFont', '增大', () => run('setFontSize', `${Math.min(72, (Number.parseFloat(state().fontSize) || 14) + 2)}px`), { iconOnly: true }),
|
|
222
|
+
small('shrinkFont', '减小', () => run('setFontSize', `${Math.max(8, (Number.parseFloat(state().fontSize) || 14) - 2)}px`), { iconOnly: true }),
|
|
223
|
+
small('eraser', '清除格式', () => run('clearFormat'), { iconOnly: true })
|
|
224
|
+
),
|
|
225
|
+
row(
|
|
226
|
+
small('bold', '加粗', () => run('toggleBold'), { iconOnly: true, active: (s) => s.bold }),
|
|
227
|
+
small('italic', '斜体', () => run('toggleItalic'), { iconOnly: true, active: (s) => s.italic }),
|
|
228
|
+
small('underline', '下划线', () => run('toggleUnderline'), { iconOnly: true, active: (s) => s.underline }),
|
|
229
|
+
small('strike', '删除线', () => run('toggleStrike'), { iconOnly: true, active: (s) => s.strike }),
|
|
230
|
+
small('superscript', '上标', () => run('toggleSuperscript'), { iconOnly: true, active: (s) => s.superscript }),
|
|
231
|
+
small('subscript', '下标', () => run('toggleSubscript'), { iconOnly: true, active: (s) => s.subscript }),
|
|
232
|
+
menu('highlight', '突出显示', bgMenu, { iconOnly: true, swatch: (s) => s.backgroundColor || BG_COLORS[15] }),
|
|
233
|
+
menu('colorIcon', '字体颜色', colorMenu, { iconOnly: true, swatch: (s) => s.color || TEXT_COLORS[0] })
|
|
234
|
+
)
|
|
235
|
+
)
|
|
236
|
+
),
|
|
237
|
+
group('段落',
|
|
238
|
+
stack(
|
|
239
|
+
row(
|
|
240
|
+
small('bullet', '项目符号', () => run('toggleBulletList'), { iconOnly: true, active: (s) => s.bulletList }),
|
|
241
|
+
small('ordered', '编号', () => run('toggleOrderedList'), { iconOnly: true, active: (s) => s.orderedList }),
|
|
242
|
+
small('outdent', '减少缩进', () => run('decreaseBlockIndent'), { iconOnly: true }),
|
|
243
|
+
small('indentAdd', '增加缩进', () => run('increaseBlockIndent'), { iconOnly: true })
|
|
244
|
+
),
|
|
245
|
+
row(
|
|
246
|
+
small('alignLeft', '左对齐', () => run('setBlockAlign', 'left'), { iconOnly: true, active: (s) => s.textAlign === 'left' }),
|
|
247
|
+
small('alignCenter', '居中', () => run('setBlockAlign', 'center'), { iconOnly: true, active: (s) => s.textAlign === 'center' }),
|
|
248
|
+
small('alignRight', '右对齐', () => run('setBlockAlign', 'right'), { iconOnly: true, active: (s) => s.textAlign === 'right' }),
|
|
249
|
+
small('alignJustify', '两端对齐', () => run('setBlockAlign', 'justify'), { iconOnly: true, active: (s) => s.textAlign === 'justify' }),
|
|
250
|
+
menu('lineSpacing', '行距', () => ['1', '1.15', '1.5', '2', '2.5', '3'].map((v) => ({
|
|
251
|
+
label: v, checked: state().lineHeight === v, onClick: () => run('setBlockLineHeight', v),
|
|
252
|
+
})), { iconOnly: true })
|
|
253
|
+
)
|
|
254
|
+
)
|
|
255
|
+
),
|
|
256
|
+
group('样式',
|
|
257
|
+
row(
|
|
258
|
+
styleCard('正文', '正文', () => run('convertBlock', 'paragraph'), '', 'paragraph'),
|
|
259
|
+
styleCard('标题1', '标题 1', () => run('convertBlock', 'heading1'), 'gbd-rstyle__preview--h1', 'heading1'),
|
|
260
|
+
styleCard('标题2', '标题 2', () => run('convertBlock', 'heading2'), 'gbd-rstyle__preview--h2', 'heading2'),
|
|
261
|
+
styleCard('标题3', '标题 3', () => run('convertBlock', 'heading3'), 'gbd-rstyle__preview--h3', 'heading3')
|
|
262
|
+
)
|
|
263
|
+
),
|
|
264
|
+
group('编辑',
|
|
265
|
+
stack(
|
|
266
|
+
row(
|
|
267
|
+
small('undo', '撤销', () => run('undo'), { disabled: (s) => !s.canUndo }),
|
|
268
|
+
small('redo', '重做', () => run('redo'), { disabled: (s) => !s.canRedo })
|
|
269
|
+
),
|
|
270
|
+
row(small('find', '查找', () => io.find(false)))
|
|
271
|
+
),
|
|
272
|
+
big('comment', '批注', () => ctx().insert.comment())
|
|
273
|
+
),
|
|
274
|
+
],
|
|
275
|
+
|
|
276
|
+
// ---------- 插入 ----------
|
|
277
|
+
insert: () => [
|
|
278
|
+
group('分页',
|
|
279
|
+
big('pageBreak', '分页符', () => run('insertPageBreak'))
|
|
280
|
+
),
|
|
281
|
+
group('表格', big('table', '表格', (e) => io.actions.insertTable(e))),
|
|
282
|
+
group('插图',
|
|
283
|
+
big('image', '图片', () => io.actions.insertImage())
|
|
284
|
+
),
|
|
285
|
+
group('符号',
|
|
286
|
+
big('specialChars', '符号', () => ctx().insert.specialChars())
|
|
287
|
+
),
|
|
288
|
+
group('页眉页脚',
|
|
289
|
+
big('headerFooter', '页眉页脚', () => ctx().file.pageSetup()),
|
|
290
|
+
big('pageNumber', '页码', (e) => openMenu({ anchor: e.currentTarget, items: pageNumberMenu(), placement: 'bottom-start' })),
|
|
291
|
+
big('watermark', '水印', () => ctx().insert.watermark())
|
|
292
|
+
),
|
|
293
|
+
group('链接',
|
|
294
|
+
big('linkIcon', '超级链接', () => io.actions.insertLink())
|
|
295
|
+
),
|
|
296
|
+
group('脚注与批注',
|
|
297
|
+
big('footnote', '脚注', () => ctx().insert.footnote()),
|
|
298
|
+
big('comment', '批注', () => ctx().insert.comment())
|
|
299
|
+
),
|
|
300
|
+
],
|
|
301
|
+
|
|
302
|
+
// ---------- 页面布局 ----------
|
|
303
|
+
layout: () => [
|
|
304
|
+
group('页面设置',
|
|
305
|
+
big('margin', '页边距', () => io.openMargin()),
|
|
306
|
+
big('orientation', '纸张方向', () => ctx().file.pageSetup()),
|
|
307
|
+
big('paperSize', '纸张大小', () => ctx().file.pageSetup()),
|
|
308
|
+
big('columns', '分栏', (e) => openMenu({ anchor: e.currentTarget, items: [1, 2, 3].map((n) => ({
|
|
309
|
+
label: n === 1 ? '单栏' : `${n} 栏`, checked: state().columns === n, onClick: () => ctx().format.setLayout(n),
|
|
310
|
+
})), placement: 'bottom-start' }))
|
|
311
|
+
),
|
|
312
|
+
],
|
|
313
|
+
|
|
314
|
+
// ---------- 引用 ----------
|
|
315
|
+
reference: () => [
|
|
316
|
+
group('目录',
|
|
317
|
+
big('toc', '目录', (e) => openMenu({ anchor: e.currentTarget, items: [
|
|
318
|
+
{ label: '插入目录', onClick: () => ctx().insert.toc() },
|
|
319
|
+
{ label: '刷新目录', onClick: () => ctx().insert.refreshToc() },
|
|
320
|
+
], placement: 'bottom-start' })),
|
|
321
|
+
big('updateToc', '更新目录', () => ctx().insert.refreshToc())
|
|
322
|
+
),
|
|
323
|
+
group('脚注',
|
|
324
|
+
big('footnote', '插入脚注', () => ctx().insert.footnote())
|
|
325
|
+
),
|
|
326
|
+
],
|
|
327
|
+
|
|
328
|
+
// ---------- 审阅 ----------
|
|
329
|
+
review: () => [
|
|
330
|
+
group('修订',
|
|
331
|
+
big('suggest', '修订', () => ctx().edit.setSuggesting(!state().suggesting), { active: (s) => s.suggesting }),
|
|
332
|
+
big('accept', '接受', () => ctx().edit.acceptAll()),
|
|
333
|
+
big('reject', '拒绝', () => ctx().edit.rejectAll())
|
|
334
|
+
),
|
|
335
|
+
group('批注',
|
|
336
|
+
big('comment', '插入批注', () => ctx().insert.comment()),
|
|
337
|
+
big('panel', '批注面板', () => ctx().view.setView('commentsPanel', !state().commentsPanel), { active: (s) => s.commentsPanel })
|
|
338
|
+
),
|
|
339
|
+
group('查找',
|
|
340
|
+
big('find', '查找', () => io.find(false)),
|
|
341
|
+
big('find', '替换', () => io.find(true))
|
|
342
|
+
),
|
|
343
|
+
],
|
|
344
|
+
|
|
345
|
+
// ---------- 视图 ----------
|
|
346
|
+
view: () => [
|
|
347
|
+
group('缩放',
|
|
348
|
+
big('zoomIn', '放大', () => ctx().view.zoomIn()),
|
|
349
|
+
big('zoomOut', '缩小', () => ctx().view.zoomOut()),
|
|
350
|
+
big('zoomReset', '100%', () => ctx().view.zoomReset())
|
|
351
|
+
),
|
|
352
|
+
group('显示',
|
|
353
|
+
big('bookmark', '目录导航', () => ctx().view.setView('catalog', !state().catalog), { active: (s) => s.catalog }),
|
|
354
|
+
big('panel', '批注面板', () => ctx().view.setView('commentsPanel', !state().commentsPanel), { active: (s) => s.commentsPanel })
|
|
355
|
+
),
|
|
356
|
+
],
|
|
357
|
+
|
|
358
|
+
// ---------- 章节 ----------
|
|
359
|
+
chapter: () => [
|
|
360
|
+
group('章节',
|
|
361
|
+
big('toc', '插入目录', () => ctx().insert.toc()),
|
|
362
|
+
big('updateToc', '刷新目录', () => ctx().insert.refreshToc())
|
|
363
|
+
),
|
|
364
|
+
group('分页',
|
|
365
|
+
big('pageBreak', '分页符', () => run('insertPageBreak')),
|
|
366
|
+
big('pageNumber', '页码', (e) => openMenu({ anchor: e.currentTarget, items: pageNumberMenu(), placement: 'bottom-start' }))
|
|
367
|
+
),
|
|
368
|
+
group('页眉页脚',
|
|
369
|
+
big('headerFooter', '页眉页脚', (e) => openMenu({ anchor: e.currentTarget, items: hfMenu(), placement: 'bottom-start' })),
|
|
370
|
+
big('watermark', '水印', () => ctx().insert.watermark())
|
|
371
|
+
),
|
|
372
|
+
],
|
|
373
|
+
|
|
374
|
+
// ---------- 开发工具 ----------
|
|
375
|
+
developer: () => [
|
|
376
|
+
group('内容块',
|
|
377
|
+
big('code', '代码块', () => run('toggleCodeBlock'), { active: (s) => s.codeBlock }),
|
|
378
|
+
big('quote', '引用块', () => run('toggleBlockquote'), { active: (s) => s.blockquote }),
|
|
379
|
+
big('ordered', '待办列表', () => run('toggleTaskList'), { active: (s) => s.taskList })
|
|
380
|
+
),
|
|
381
|
+
group('编辑',
|
|
382
|
+
big('clear', '清除格式', () => run('clearFormat')),
|
|
383
|
+
big('suggest', '修订', () => ctx().edit.setSuggesting(!state().suggesting), { active: (s) => s.suggesting }),
|
|
384
|
+
big('specialChars', '特殊符号', () => ctx().insert.specialChars())
|
|
385
|
+
),
|
|
386
|
+
],
|
|
387
|
+
|
|
388
|
+
// ---------- 特色功能 ----------
|
|
389
|
+
feature: () => [
|
|
390
|
+
group('文档增强',
|
|
391
|
+
big('watermark', '水印', () => ctx().insert.watermark()),
|
|
392
|
+
big('toc', '目录', () => ctx().insert.toc()),
|
|
393
|
+
big('pageBreak', '分页符', () => run('insertPageBreak'))
|
|
394
|
+
),
|
|
395
|
+
group('输出',
|
|
396
|
+
big('print', '打印 / PDF', () => ctx().file.print()),
|
|
397
|
+
big('export', '导出 Word', () => ctx().file.exportCopy()),
|
|
398
|
+
big('export', '导出 Markdown', () => ctx().file.exportMarkdown())
|
|
399
|
+
),
|
|
400
|
+
],
|
|
401
|
+
|
|
402
|
+
// ---------- 查找 ----------
|
|
403
|
+
find: () => [
|
|
404
|
+
group('查找',
|
|
405
|
+
big('find', '查找', () => io.find(false)),
|
|
406
|
+
big('find', '查找替换', () => io.find(true)),
|
|
407
|
+
big('selectAll', '全选', () => run('selectAll'))
|
|
408
|
+
),
|
|
409
|
+
],
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* 构建选项卡栏。
|
|
415
|
+
* @param {object} io
|
|
416
|
+
* @param {() => object} io.getState 现取合并状态
|
|
417
|
+
* @param {() => object} io.getCtx 现取 menuCtx(只读短路已内含)
|
|
418
|
+
* @param {() => boolean} io.isDisabled 是否只读
|
|
419
|
+
* @param {(name:string, ...args:any[]) => any} io.run 执行编辑器命令
|
|
420
|
+
* @param {object} io.actions {insertTable, insertImage, insertLink}
|
|
421
|
+
* @param {(replace:boolean) => void} io.find 打开查找栏
|
|
422
|
+
* @param {() => void} io.openMargin 页边距对话框
|
|
423
|
+
*/
|
|
424
|
+
export function buildRibbon(io) {
|
|
425
|
+
const root = document.createElement('div');
|
|
426
|
+
root.className = 'gbd-ribbon';
|
|
427
|
+
const tabsEl = document.createElement('div');
|
|
428
|
+
tabsEl.className = 'gbd-ribbon__tabs';
|
|
429
|
+
tabsEl.setAttribute('role', 'tablist');
|
|
430
|
+
const panelsEl = document.createElement('div');
|
|
431
|
+
panelsEl.className = 'gbd-ribbon__panels';
|
|
432
|
+
root.append(tabsEl, panelsEl);
|
|
433
|
+
|
|
434
|
+
const defs = panelDefs(io);
|
|
435
|
+
const entries = [];
|
|
436
|
+
let activeKey = TABS[0].key;
|
|
437
|
+
|
|
438
|
+
for (const { key, label, icon: tabIcon } of TABS) {
|
|
439
|
+
const tab = document.createElement('button');
|
|
440
|
+
tab.type = 'button';
|
|
441
|
+
tab.className = 'gbd-ribbon__tab';
|
|
442
|
+
tab.dataset.tab = key;
|
|
443
|
+
tab.setAttribute('role', 'tab');
|
|
444
|
+
tab.innerHTML = (tabIcon ? `<span class="gbd-ribbon__tabicon">${icon(tabIcon)}</span>` : '') + `<span>${label}</span>`;
|
|
445
|
+
|
|
446
|
+
const panel = document.createElement('div');
|
|
447
|
+
panel.className = 'gbd-ribbon__panel';
|
|
448
|
+
panel.dataset.panel = key;
|
|
449
|
+
panel.setAttribute('role', 'tabpanel');
|
|
450
|
+
const pctx = new PanelContext(io);
|
|
451
|
+
pctx.render(panel, defs[key]());
|
|
452
|
+
pctx.refresh(); // 首帧即按现态点亮(激活样式/色条/撤销可用性),不等第一次 state 事件
|
|
453
|
+
entries.push({ key, tab, panel, refresh: () => pctx.refresh() });
|
|
454
|
+
tab.addEventListener('click', () => select(key));
|
|
455
|
+
tabsEl.appendChild(tab);
|
|
456
|
+
panelsEl.appendChild(panel);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
function select(key) {
|
|
460
|
+
if (activeKey === key) return;
|
|
461
|
+
activeKey = key;
|
|
462
|
+
for (const entry of entries) {
|
|
463
|
+
const on = entry.key === key;
|
|
464
|
+
entry.panel.hidden = !on;
|
|
465
|
+
entry.tab.classList.toggle('gbd-is-active', on);
|
|
466
|
+
entry.tab.setAttribute('aria-selected', String(on));
|
|
467
|
+
if (on) entry.refresh();
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
for (const entry of entries) {
|
|
472
|
+
const on = entry.key === activeKey;
|
|
473
|
+
entry.panel.hidden = !on;
|
|
474
|
+
entry.tab.classList.toggle('gbd-is-active', on);
|
|
475
|
+
entry.tab.setAttribute('aria-selected', String(on));
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
root.refresh = () => { for (const entry of entries) entry.refresh(); };
|
|
479
|
+
root.getActiveTab = () => activeKey;
|
|
480
|
+
root.selectTab = select;
|
|
481
|
+
return root;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/** 供测试断言:选项卡定义(顺序 + key/label) */
|
|
485
|
+
export const RIBBON_TABS = TABS.map(({ key, label }) => ({ key, label }));
|
package/src/ui/toolbar.js
CHANGED
|
@@ -1,175 +1,8 @@
|
|
|
1
|
-
// [文件] gbdocx·
|
|
2
|
-
import { icon } from './icons.js';
|
|
1
|
+
// [文件] gbdocx·Select —— 功能区下拉(触发按钮 + 浮层条目;value 由 get() 现取)
|
|
3
2
|
import { openMenu } from './context-menu.js';
|
|
4
|
-
import { openColorPanel } from './color-panel.js';
|
|
5
|
-
import { ALIGN_OPTIONS, BG_COLORS, FONT_FAMILIES, FONT_SIZES, TEXT_COLORS } from '../kernel/textStyles.js';
|
|
6
|
-
import { PARA_STYLES } from '../kernel/docx/constants.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @param {object} io
|
|
10
|
-
* @param {() => object|null} io.getEditor
|
|
11
|
-
* @param {() => object} io.getState
|
|
12
|
-
* @param {() => boolean} io.isDisabled
|
|
13
|
-
* @param {object} io.actions {insertTable, insertImage, insertLink}
|
|
14
|
-
*/
|
|
15
|
-
export function buildToolbar(io) {
|
|
16
|
-
const bar = document.createElement('div');
|
|
17
|
-
bar.className = 'gbd-toolbar';
|
|
18
|
-
const refs = { buttons: {}, selects: {} };
|
|
19
|
-
|
|
20
|
-
const run = (name, ...args) => {
|
|
21
|
-
if (io.isDisabled()) return;
|
|
22
|
-
const ed = io.getEditor();
|
|
23
|
-
if (!ed || ed.destroyed) return io.getEditor();
|
|
24
|
-
ed.focus();
|
|
25
|
-
return ed.exec(name, ...args);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
/* ---- 段落样式 / 字号 / 字体(下拉) ---- */
|
|
29
|
-
const group1 = document.createElement('div');
|
|
30
|
-
group1.className = 'gbd-toolbar__group';
|
|
31
|
-
refs.selects.block = makeSelect({
|
|
32
|
-
title: '段落样式',
|
|
33
|
-
width: 96,
|
|
34
|
-
options: PARA_STYLES.map((s) => ({ value: s.value, label: s.label })),
|
|
35
|
-
get: () => io.getState().blockType,
|
|
36
|
-
onChange: (v) => run('convertBlock', v),
|
|
37
|
-
});
|
|
38
|
-
refs.selects.size = makeSelect({
|
|
39
|
-
title: '字号',
|
|
40
|
-
width: 56,
|
|
41
|
-
options: [{ value: '', label: '默认' }, ...FONT_SIZES.map((s) => ({ value: s, label: String(s) }))],
|
|
42
|
-
get: () => Number.parseFloat(io.getState().fontSize) || '',
|
|
43
|
-
onChange: (v) => (v ? run('setFontSize', `${v}px`) : run('unsetFontSize')),
|
|
44
|
-
});
|
|
45
|
-
refs.selects.family = makeSelect({
|
|
46
|
-
title: '字体',
|
|
47
|
-
width: 110,
|
|
48
|
-
options: [{ value: '', label: '默认' }, ...FONT_FAMILIES.map((f) => ({ value: f.value, label: f.label, style: { fontFamily: f.value } }))],
|
|
49
|
-
get: () => io.getState().fontFamily || '',
|
|
50
|
-
onChange: (v) => (v ? run('setFontFamily', v) : run('unsetFontFamily')),
|
|
51
|
-
});
|
|
52
|
-
group1.append(refs.selects.block.el, refs.selects.size.el, refs.selects.family.el);
|
|
53
|
-
bar.appendChild(group1);
|
|
54
|
-
bar.appendChild(sep());
|
|
55
|
-
|
|
56
|
-
/* ---- 行内格式 ---- */
|
|
57
|
-
const group2 = document.createElement('div');
|
|
58
|
-
group2.className = 'gbd-toolbar__group';
|
|
59
|
-
const inline = [
|
|
60
|
-
['bold', '加粗 (Ctrl+B)', 'toggleBold'],
|
|
61
|
-
['italic', '斜体 (Ctrl+I)', 'toggleItalic'],
|
|
62
|
-
['underline', '下划线 (Ctrl+U)', 'toggleUnderline'],
|
|
63
|
-
['strike', '删除线', 'toggleStrike'],
|
|
64
|
-
['superscript', '上标 (Ctrl+=)', 'toggleSuperscript'],
|
|
65
|
-
['subscript', '下标 (Ctrl+Shift+=)', 'toggleSubscript'],
|
|
66
|
-
];
|
|
67
|
-
for (const [key, title, cmd] of inline) {
|
|
68
|
-
const b = toolButton(key, title, () => run(cmd));
|
|
69
|
-
b._stateKey = key;
|
|
70
|
-
group2.appendChild(b);
|
|
71
|
-
refs.buttons[key] = b;
|
|
72
|
-
}
|
|
73
|
-
const colorBtn = toolButton('colorIcon', '字体颜色', (e) => {
|
|
74
|
-
openColorPanel({ anchor: e.currentTarget, colors: TEXT_COLORS, value: io.getState().color, title: '字体颜色', columns: 10, onPick: (c) => run(c ? 'setColor' : 'unsetColor', c) });
|
|
75
|
-
});
|
|
76
|
-
const bgBtn = toolButton('bgIcon', '底色/高亮', (e) => {
|
|
77
|
-
openColorPanel({ anchor: e.currentTarget, colors: BG_COLORS, value: io.getState().backgroundColor, title: '背景色(高亮)', columns: 6, onPick: (c) => run(c ? 'setBackgroundColor' : 'unsetBackgroundColor', c) });
|
|
78
|
-
});
|
|
79
|
-
refs.buttons.color = colorBtn;
|
|
80
|
-
refs.buttons.bg = bgBtn;
|
|
81
|
-
group2.append(colorBtn, bgBtn);
|
|
82
|
-
bar.appendChild(group2);
|
|
83
|
-
bar.appendChild(sep());
|
|
84
|
-
|
|
85
|
-
/* ---- 对齐 / 列表 / 缩进 / 引用 / 代码 ---- */
|
|
86
|
-
const group3 = document.createElement('div');
|
|
87
|
-
group3.className = 'gbd-toolbar__group';
|
|
88
|
-
for (const a of ALIGN_OPTIONS) {
|
|
89
|
-
const b = toolButton(a.icon, a.title, () => run('setBlockAlign', a.value));
|
|
90
|
-
b._align = a.value;
|
|
91
|
-
group3.appendChild(b);
|
|
92
|
-
refs.buttons[`align-${a.value}`] = b;
|
|
93
|
-
}
|
|
94
|
-
const bl = toolButton('bullet', '无序列表', () => run('toggleBulletList'));
|
|
95
|
-
bl._stateKey = 'bulletList';
|
|
96
|
-
const ol = toolButton('ordered', '有序列表', () => run('toggleOrderedList'));
|
|
97
|
-
ol._stateKey = 'orderedList';
|
|
98
|
-
const indAdd = toolButton('indentAdd', '增加缩进', () => run('increaseBlockIndent'));
|
|
99
|
-
const indReduce = toolButton('indentReduce', '减少缩进', () => run('decreaseBlockIndent'));
|
|
100
|
-
const quote = toolButton('quote', '引用块', () => run('toggleBlockquote'));
|
|
101
|
-
quote._stateKey = 'blockquote';
|
|
102
|
-
const code = toolButton('code', '代码块', () => run('toggleCodeBlock'));
|
|
103
|
-
code._stateKey = 'codeBlock';
|
|
104
|
-
// 刷新只遍历 refs.buttons:不登记就永远拿不到激活态(点了列表/引用/代码块按钮后不亮)
|
|
105
|
-
refs.buttons.bullet = bl;
|
|
106
|
-
refs.buttons.ordered = ol;
|
|
107
|
-
refs.buttons.quote = quote;
|
|
108
|
-
refs.buttons.code = code;
|
|
109
|
-
group3.append(bl, ol, indAdd, indReduce, quote, code);
|
|
110
|
-
bar.appendChild(group3);
|
|
111
|
-
bar.appendChild(sep());
|
|
112
|
-
|
|
113
|
-
/* ---- 插入 ---- */
|
|
114
|
-
const group4 = document.createElement('div');
|
|
115
|
-
group4.className = 'gbd-toolbar__group';
|
|
116
|
-
group4.append(
|
|
117
|
-
toolButton('table', '插入表格', (e) => io.actions.insertTable(e)),
|
|
118
|
-
toolButton('image', '插入图片', () => io.actions.insertImage()),
|
|
119
|
-
toolButton('linkIcon', '插入链接', () => io.actions.insertLink()),
|
|
120
|
-
toolButton('divider', '分隔线', () => run('setHorizontalRule')),
|
|
121
|
-
toolButton('pageBreak', '分页符', () => run('insertPageBreak')),
|
|
122
|
-
);
|
|
123
|
-
bar.appendChild(group4);
|
|
124
|
-
bar.appendChild(sep());
|
|
125
|
-
|
|
126
|
-
/* ---- 撤销/重做/清除 ---- */
|
|
127
|
-
const group5 = document.createElement('div');
|
|
128
|
-
group5.className = 'gbd-toolbar__group';
|
|
129
|
-
refs.buttons.undo = toolButton('undo', '撤销 (Ctrl+Z)', () => run('undo'));
|
|
130
|
-
refs.buttons.redo = toolButton('redo', '重做 (Ctrl+Y)', () => run('redo'));
|
|
131
|
-
refs.buttons.clear = toolButton('clear', '清除格式', () => run('clearFormat'));
|
|
132
|
-
group5.append(refs.buttons.undo, refs.buttons.redo, refs.buttons.clear);
|
|
133
|
-
bar.appendChild(group5);
|
|
134
|
-
|
|
135
|
-
/** 按状态快照刷新激活态/禁用态 */
|
|
136
|
-
bar.refresh = () => {
|
|
137
|
-
const s = io.getState();
|
|
138
|
-
const disabled = io.isDisabled();
|
|
139
|
-
for (const [key, b] of Object.entries(refs.buttons)) {
|
|
140
|
-
if (b._stateKey) b.classList.toggle('gbd-is-active', Boolean(s[b._stateKey]));
|
|
141
|
-
if (b._align) b.classList.toggle('gbd-is-active', s.textAlign === b._align);
|
|
142
|
-
if (key === 'color') b.classList.toggle('gbd-is-active', Boolean(s.color));
|
|
143
|
-
if (key === 'bg') b.classList.toggle('gbd-is-active', Boolean(s.backgroundColor));
|
|
144
|
-
if (key === 'undo') b.disabled = disabled || !s.canUndo;
|
|
145
|
-
if (key === 'redo') b.disabled = disabled || !s.canRedo;
|
|
146
|
-
}
|
|
147
|
-
refs.selects.block.refresh();
|
|
148
|
-
refs.selects.size.refresh();
|
|
149
|
-
refs.selects.family.refresh();
|
|
150
|
-
};
|
|
151
|
-
return bar;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
function sep() {
|
|
155
|
-
const s = document.createElement('span');
|
|
156
|
-
s.className = 'gbd-toolbar__sep';
|
|
157
|
-
return s;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function toolButton(name, title, onClick) {
|
|
161
|
-
const b = document.createElement('button');
|
|
162
|
-
b.type = 'button';
|
|
163
|
-
b.className = 'gbd-toolbtn';
|
|
164
|
-
b.title = title;
|
|
165
|
-
b.innerHTML = icon(name);
|
|
166
|
-
b.addEventListener('mousedown', (e) => e.preventDefault());
|
|
167
|
-
b.addEventListener('click', onClick);
|
|
168
|
-
return b;
|
|
169
|
-
}
|
|
170
3
|
|
|
171
4
|
/** 下拉选择(触发按钮 + 浮层条目;value 由 get() 现取) */
|
|
172
|
-
function makeSelect({ title, options, width, get, onChange }) {
|
|
5
|
+
export function makeSelect({ title, options, width, get, onChange }) {
|
|
173
6
|
const btn = document.createElement('button');
|
|
174
7
|
btn.type = 'button';
|
|
175
8
|
btn.className = 'gbd-select';
|