gbpptx 0.2.0 → 0.4.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.
Files changed (46) hide show
  1. package/README.md +237 -214
  2. package/package.json +5 -3
  3. package/src/kernel/model.js +1 -2
  4. package/src/kernel/parts.js +11 -15
  5. package/src/kernel/print.js +14 -16
  6. package/web/app/main.js +8 -8
  7. package/web/app/slides/arrange.js +101 -0
  8. package/web/app/slides/clipboard.js +93 -0
  9. package/web/app/slides/file.js +213 -0
  10. package/web/app/slides/find.js +201 -0
  11. package/web/app/slides/history.js +100 -0
  12. package/web/app/slides/keys.js +179 -0
  13. package/web/app/slides/menus.js +105 -0
  14. package/web/app/slides/objects.js +254 -0
  15. package/web/app/slides/render.js +183 -0
  16. package/web/app/slides/shared.js +85 -0
  17. package/web/app/slides/slides.js +103 -0
  18. package/web/app/slides/table.js +102 -0
  19. package/web/app/slides/text.js +418 -0
  20. package/web/app/slides/view.js +156 -0
  21. package/web/app/slides-app.js +323 -2446
  22. package/web/app/ui/canvas.js +769 -755
  23. package/web/app/ui/color-panel.js +58 -76
  24. package/web/app/ui/dialogs.js +120 -159
  25. package/web/app/ui/file-io.js +116 -132
  26. package/web/app/ui/format-pane.js +427 -427
  27. package/web/app/ui/icons.js +129 -129
  28. package/web/app/ui/menu.js +75 -89
  29. package/web/app/ui/notify.js +45 -45
  30. package/web/app/ui/page-view.js +221 -221
  31. package/web/app/ui/popup.js +80 -80
  32. package/web/app/ui/present.js +292 -292
  33. package/web/app/ui/ribbon.js +868 -878
  34. package/web/app/ui/slide-pane.js +197 -189
  35. package/web/app/ui/statusbar.js +93 -93
  36. package/web/app/ui/text-styles.js +84 -84
  37. package/web/app/ui/titlebar.js +184 -67
  38. package/web/app/ui/transition.js +30 -30
  39. package/web/index.html +23 -21
  40. package/web/serve.mjs +22 -67
  41. package/web/styles/office.css +466 -644
  42. package/web/styles/slides.css +97 -219
  43. package/web/styles/tokens.css +154 -155
  44. package/src/kernel/util/escape.js +0 -21
  45. package/src/kernel/util/id.js +0 -13
  46. package/web/app/ui/backstage.js +0 -255
@@ -1,878 +1,868 @@
1
- // [文件] gbpptx·Ribbon —— Office 式功能区(开始/插入/设计/切换/放映/审阅/视图 + 形状/图片/表格上下文标签)
2
-
3
- import {
4
- FONT_FAMILIES, FONT_SIZES, TEXT_COLORS,
5
- } from './text-styles.js';
6
- import {
7
- SHAPE_KINDS, SLIDE_LAYOUTS, TABLE_STYLES, THEME_KEYS, TRANSITIONS, contrastText, shapePath, themeOf,
8
- } from '../../../src/kernel/model.js';
9
- import { icon } from './icons.js';
10
- import { openMenu } from './menu.js';
11
- import { openPopup } from './popup.js';
12
- import { openColorPanel } from './color-panel.js';
13
-
14
- const keepFocus = (el) => {
15
- el.addEventListener('mousedown', (e) => {
16
- if (e.button === 0) e.preventDefault();
17
- });
18
- };
19
-
20
- const el = (tag, cls, html) => {
21
- const node = document.createElement(tag);
22
- if (cls) node.className = cls;
23
- if (html != null) node.innerHTML = html;
24
- return node;
25
- };
26
-
27
- const button = (cls, html, title) => {
28
- const b = el('button', cls, html);
29
- b.type = 'button';
30
- if (title) b.title = title;
31
- keepFocus(b);
32
- return b;
33
- };
34
-
35
- const big = (name, label, onClick, title) => {
36
- const b = button('pp-rb-big', `${icon(name, 26)}<span class="pp-rb-big__label">${label}</span>`, title || label);
37
- b.addEventListener('click', onClick);
38
- return b;
39
- };
40
-
41
- const splitBig = (name, label, onClick, onMenu, title) => {
42
- const wrap = el('div', 'pp-rb-split');
43
- const main = big(name, label, onClick, title);
44
- const caret = button('pp-rb-split__caret', icon('chevronDown', 12), '更多');
45
- caret.addEventListener('click', onMenu);
46
- wrap.append(main, caret);
47
- return wrap;
48
- };
49
-
50
- const small = (name, label, onClick, title, cls = '') => {
51
- const b = button(`pp-rb-small${cls ? ` ${cls}` : ''}`, `${icon(name, 16)}${label ? `<span>${label}</span>` : ''}`, title || label || name);
52
- b.addEventListener('click', onClick);
53
- return b;
54
- };
55
-
56
- const menuButton = (name, label, items, title, cls = '') => {
57
- const b = small(name, label, () => openMenu({ anchor: b, items: items() }), title || label, cls);
58
- return b;
59
- };
60
-
61
- const column = (...children) => {
62
- const c = el('div', 'pp-rb-col');
63
- c.append(...children);
64
- return c;
65
- };
66
-
67
- const group = (label, ...children) => {
68
- const g = el('div', 'pp-rb-group');
69
- const body = el('div', 'pp-rb-group__body');
70
- body.append(...children);
71
- const cap = el('div', 'pp-rb-group__label');
72
- cap.textContent = label;
73
- g.append(body, cap);
74
- return g;
75
- };
76
-
77
- const combo = (width, onClick, title) => {
78
- const b = button('pp-rb-combo', '<span class="pp-rb-combo__label"></span><span class="pp-rb-combo__caret">▾</span>', title);
79
- b.style.width = `${width}px`;
80
- b.addEventListener('click', onClick);
81
- return b;
82
- };
83
-
84
- const colorButton = (name, label, title, getColor, onPick) => {
85
- const b = button('pp-rb-small pp-rb-colorbtn', `${icon(name, 16)}${label ? `<span>${label}</span>` : ''}<i class="pp-rb-colorbar"></i>`, title);
86
- b.addEventListener('click', (e) => {
87
- openColorPanel({
88
- anchor: e.currentTarget,
89
- colors: TEXT_COLORS,
90
- value: getColor() || '',
91
- columns: 10,
92
- title,
93
- onPick,
94
- });
95
- });
96
- b.colorBar = b.querySelector('.pp-rb-colorbar');
97
- return b;
98
- };
99
-
100
- const blend = (a, b, t) => {
101
- const parse = (hex) => {
102
- const n = Number.parseInt(String(hex).replace('#', ''), 16);
103
- return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 };
104
- };
105
- const x = parse(a);
106
- const y = parse(b);
107
- const k = Math.max(0, Math.min(1, t));
108
- return `#${[x.r + (y.r - x.r) * k, x.g + (y.g - x.g) * k, x.b + (y.b - x.b) * k]
109
- .map((v) => Math.round(v).toString(16).padStart(2, '0')).join('')}`;
110
- };
111
-
112
- const readableText = (background, fallback) => {
113
- const n = Number.parseInt(String(background).replace('#', ''), 16);
114
- const lum = Number.isFinite(n) ? (0.2126 * ((n >> 16) & 255) + 0.7152 * ((n >> 8) & 255) + 0.0722 * (n & 255)) / 255 : 1;
115
- return lum > 0.55 ? '#201f1e' : fallback;
116
- };
117
-
118
- /** 形状快速样式(按当前主题推导) */
119
- export function shapeQuickStyles(themeKey) {
120
- const t = themeOf(themeKey);
121
- return [
122
- { key: 'accent', label: '彩色填充', fill: t.accent, stroke: '', text: contrastText(t.accent) },
123
- { key: 'light', label: '浅色填充', fill: blend(t.accent, t.bg, 0.8), stroke: t.accent, text: readableText(blend(t.accent, t.bg, 0.8), t.text) },
124
- { key: 'outline', label: '轮廓', fill: '', stroke: t.accent, text: t.text },
125
- { key: 'deep', label: '深色填充', fill: blend(t.accent, '#000000', 0.55), stroke: '', text: '#ffffff' },
126
- { key: 'paper', label: '浅色轮廓', fill: t.bg, stroke: blend(t.text, t.bg, 0.5), text: t.text },
127
- { key: 'dark', label: '深色强调', fill: t.text, stroke: '', text: contrastText(t.text) },
128
- ];
129
- }
130
-
131
- function openShapeGallery(anchor, onPick) {
132
- const box = el('div', 'pp-shape-gallery');
133
- const handle = { close: () => {} };
134
- for (const s of SHAPE_KINDS) {
135
- const item = button('pp-shape-gallery__item', `<svg viewBox="0 0 40 30" width="40" height="30" aria-hidden="true"><path d="${shapePath(s.kind, 34, 24)}" transform="translate(3,3)" fill="currentColor"/></svg><span>${s.label}</span>`, s.label);
136
- item.addEventListener('click', () => { handle.close(); onPick(s.kind); });
137
- box.appendChild(item);
138
- }
139
- Object.assign(handle, openPopup({ anchor, content: box, placement: 'bottom-start', className: 'pp-popup--gallery' }));
140
- return handle;
141
- }
142
-
143
- /** Office 式表格尺寸选择器 */
144
- function openTableGrid(anchor, onPick) {
145
- const box = el('div', 'pp-table-grid');
146
- const title = el('div', 'pp-table-grid__title', '插入表格');
147
- const hint = el('div', 'pp-table-grid__hint', '1×1 表格');
148
- const grid = el('div', 'pp-table-grid__grid');
149
- const cells = [];
150
- for (let r = 1; r <= 8; r += 1) {
151
- for (let c = 1; c <= 10; c += 1) {
152
- const cell = button('pp-table-grid__cell', '', `${r}×${c}`);
153
- cell.dataset.r = String(r);
154
- cell.dataset.c = String(c);
155
- cell.addEventListener('mouseenter', () => {
156
- hint.textContent = `${r}×${c} 表格`;
157
- for (const x of cells) {
158
- x.classList.toggle('is-on', Number(x.dataset.r) <= r && Number(x.dataset.c) <= c);
159
- }
160
- });
161
- cell.addEventListener('click', () => { handle.close(); onPick(r, c); });
162
- cells.push(cell);
163
- grid.appendChild(cell);
164
- }
165
- }
166
- grid.addEventListener('mouseleave', () => {
167
- for (const x of cells) x.classList.remove('is-on');
168
- hint.textContent = '1×1 表格';
169
- });
170
- box.append(title, grid, hint);
171
- const handle = openPopup({ anchor, content: box, placement: 'bottom-start', className: 'pp-popup--table-grid' });
172
- return handle;
173
- }
174
-
175
- export class Ribbon {
176
- constructor(app) {
177
- this.app = app;
178
- this.el = el('div', 'pp-ribbon');
179
- this.updates = [];
180
- this.contextKey = '';
181
- this.contextTabs = [];
182
- this.panels = new Map();
183
- this.activeTab = 'home';
184
-
185
- this.tabsEl = el('div', 'pp-ribbon__tabs');
186
- this.bodyEl = el('div', 'pp-ribbon__body');
187
- this.el.append(this.tabsEl, this.bodyEl);
188
- this.buildTabs();
189
- this.buildPanels();
190
- this.activate(app.prefs.ribbonTab && this.isKnownTab(app.prefs.ribbonTab) ? app.prefs.ribbonTab : 'home', { expand: false });
191
- this.setCollapsed(!!app.ribbonCollapsed);
192
- this.refresh();
193
- }
194
-
195
- /* ===================== 骨架 ===================== */
196
-
197
- isKnownTab(key) {
198
- return ['home', 'insert', 'design', 'transitions', 'slideshow', 'review', 'view'].includes(key);
199
- }
200
-
201
- buildTabs() {
202
- this.fileTab = button('pp-rb-tab pp-rb-tab--file', '文件', '文件');
203
- this.fileTab.addEventListener('click', () => {
204
- this.app.openBackstage('home');
205
- this.setFileActive(true);
206
- });
207
- this.tabsEl.appendChild(this.fileTab);
208
- const defs = [
209
- ['home', '开始'], ['insert', '插入'], ['design', '设计'], ['transitions', '切换'],
210
- ['slideshow', '幻灯片放映'], ['review', '审阅'], ['view', '视图'],
211
- ];
212
- for (const [key, label] of defs) {
213
- const tab = button('pp-rb-tab', label, label);
214
- tab.dataset.tab = key;
215
- tab.addEventListener('click', () => {
216
- this.app.closeBackstage();
217
- this.activate(key);
218
- });
219
- this.tabsEl.appendChild(tab);
220
- }
221
- this.contextHost = el('div', 'pp-rb-tabs__context');
222
- this.tabsEl.appendChild(this.contextHost);
223
- const spacer = el('div', 'pp-rb-tabs__spacer');
224
- this.tabsEl.appendChild(spacer);
225
- this.collapseBtn = button('pp-rb-collapse', icon('chevronDown', 14), '收起功能区');
226
- this.collapseBtn.addEventListener('click', () => {
227
- this.app.setRibbonCollapsed(!this.app.ribbonCollapsed);
228
- });
229
- this.tabsEl.appendChild(this.collapseBtn);
230
- }
231
-
232
- buildPanels() {
233
- const builders = {
234
- home: (p) => this.buildHome(p),
235
- insert: (p) => this.buildInsert(p),
236
- design: (p) => this.buildDesign(p),
237
- transitions: (p) => this.buildTransitions(p),
238
- slideshow: (p) => this.buildSlideshow(p),
239
- review: (p) => this.buildReview(p),
240
- view: (p) => this.buildView(p),
241
- };
242
- for (const [key, build] of Object.entries(builders)) {
243
- const panel = el('div', 'pp-rb-panel');
244
- panel.dataset.tab = key;
245
- panel.hidden = true;
246
- build(panel);
247
- this.panels.set(key, panel);
248
- this.bodyEl.appendChild(panel);
249
- }
250
- }
251
-
252
- panel(key) {
253
- if (!this.panels.has(key)) {
254
- const panel = el('div', 'pp-rb-panel');
255
- panel.dataset.tab = key;
256
- panel.hidden = true;
257
- if (key === 'ctx-shape') this.buildContextShape(panel);
258
- if (key === 'ctx-image') this.buildContextImage(panel);
259
- if (key === 'ctx-table') this.buildContextTable(panel);
260
- this.panels.set(key, panel);
261
- this.bodyEl.appendChild(panel);
262
- }
263
- return this.panels.get(key);
264
- }
265
-
266
- activate(key, { expand = true } = {}) {
267
- if (!this.isKnownTab(key) && !['ctx-shape', 'ctx-image', 'ctx-table'].includes(key)) key = 'home';
268
- this.activeTab = key;
269
- this.app.prefs.ribbonTab = key;
270
- for (const tab of this.tabsEl.querySelectorAll('.pp-rb-tab')) {
271
- tab.classList.toggle('is-active', tab.dataset.tab === key);
272
- }
273
- if (this.fileTab) this.fileTab.classList.remove('is-active');
274
- for (const [k, panel] of this.panels) panel.hidden = k !== key;
275
- this.panel(key);
276
- for (const [k, panel] of this.panels) panel.hidden = k !== key;
277
- if (expand && this.app.ribbonCollapsed) this.app.setRibbonCollapsed(false);
278
- this.refresh();
279
- }
280
-
281
- setFileActive(on) {
282
- if (on) {
283
- for (const tab of this.tabsEl.querySelectorAll('.pp-rb-tab')) {
284
- if (tab !== this.fileTab) tab.classList.remove('is-active');
285
- }
286
- }
287
- if (this.fileTab) this.fileTab.classList.toggle('is-active', on);
288
- this.bodyEl.classList.toggle('is-dimmed', on);
289
- }
290
-
291
- setCollapsed(on) {
292
- this.el.classList.toggle('is-collapsed', !!on);
293
- this.collapseBtn.innerHTML = icon(on ? 'chevronUp' : 'chevronDown', 14);
294
- this.collapseBtn.title = on ? '展开功能区' : '收起功能区';
295
- }
296
-
297
- reg(target, update) {
298
- this.updates.push({ target, update });
299
- update();
300
- return target;
301
- }
302
-
303
- refresh() {
304
- this.ensureContext();
305
- for (const entry of this.updates) {
306
- if (!entry.target.isConnected) continue;
307
- entry.update();
308
- }
309
- }
310
-
311
- /* ===================== 上下文标签 ===================== */
312
-
313
- selectionKind() {
314
- const app = this.app;
315
- if (app.selectedIds.length !== 1) return '';
316
- const o = app.firstSelected();
317
- if (!o) return '';
318
- if (o.type === 'shape' || o.type === 'line') return 'shape';
319
- if (o.type === 'image') return 'image';
320
- if (o.type === 'table') return 'table';
321
- return '';
322
- }
323
-
324
- ensureContext() {
325
- const kind = this.selectionKind();
326
- if (kind === this.contextKey) return;
327
- this.contextHost.replaceChildren();
328
- for (const t of this.contextTabs) {
329
- this.panels.get(t.key)?.remove();
330
- this.panels.delete(t.key);
331
- this.updates = this.updates.filter((u) => u.target.isConnected);
332
- }
333
- this.contextTabs = [];
334
- this.contextKey = kind;
335
- if (!kind) {
336
- if (this.activeTab.startsWith('ctx-')) this.activate('home', { expand: false });
337
- return;
338
- }
339
- const defs = {
340
- shape: { key: 'ctx-shape', label: '形状格式' },
341
- image: { key: 'ctx-image', label: '图片格式' },
342
- table: { key: 'ctx-table', label: '表格' },
343
- };
344
- const def = defs[kind];
345
- const tab = button('pp-rb-tab pp-rb-tab--context', def.label, def.label);
346
- tab.dataset.tab = def.key;
347
- tab.addEventListener('click', () => {
348
- this.app.closeBackstage();
349
- this.activate(def.key);
350
- });
351
- this.contextHost.appendChild(tab);
352
- this.contextTabs.push(def);
353
- this.panel(def.key);
354
- if (this.activeTab.startsWith('ctx-')) this.activate(def.key, { expand: false });
355
- }
356
-
357
- /* ===================== 开始 ===================== */
358
-
359
- buildHome(panel) {
360
- const app = this.app;
361
- const hasSel = () => app.selectedIds.length > 0;
362
- const canText = () => app.canFormatText();
363
- const canRich = () => app.canRichText();
364
- const canAlign = () => app.canAlignText();
365
- const canList = () => app.canList();
366
- const state = () => app.textState();
367
-
368
- /* 剪贴板 */
369
- const pasteWrap = el('div', 'pp-rb-split');
370
- const pasteMain = big('paste', '粘贴', () => app.paste(), '粘贴 (Ctrl+V)');
371
- const pasteCaret = button('pp-rb-split__caret', icon('chevronDown', 12), '粘贴选项');
372
- pasteCaret.addEventListener('click', () => openMenu({
373
- anchor: pasteCaret,
374
- items: [
375
- { label: '粘贴', icon: 'paste', onClick: () => app.paste() },
376
- { label: '粘贴为纯文本', icon: 'text', onClick: () => app.pastePlain() },
377
- ],
378
- }));
379
- pasteWrap.append(pasteMain, pasteCaret);
380
- const cutBtn = small('cut', '剪切', () => app.cutSelected(), '剪切 (Ctrl+X)');
381
- const copyBtn = small('copy', '复制', () => app.copySelected(), '复制 (Ctrl+C)');
382
- const clip = group('剪贴板', pasteWrap, column(cutBtn, copyBtn));
383
- panel.appendChild(clip);
384
-
385
- /* 幻灯片 */
386
- const newSlide = splitBig('newSlide', '新建幻灯片', () => app.addSlide(), (e) => this.layoutMenu(e.currentTarget), '新建幻灯片 (Ctrl+M)');
387
- const hideBtn = small('hide', '隐藏', () => app.toggleSlideHidden(), '隐藏幻灯片');
388
- const resetBtn = small('reset', '重设', () => app.resetSelected(), '重设位置与大小');
389
- panel.appendChild(group('幻灯片', newSlide, column(hideBtn, resetBtn)));
390
-
391
- /* 字体 */
392
- const fontBtn = combo(132, () => openMenu({
393
- anchor: fontBtn,
394
- items: FONT_FAMILIES.map((f) => ({ label: f.label, checked: state().font === f.value, onClick: () => app.applyFontFamily(f.value) })),
395
- }), '字体');
396
- const sizeBtn = combo(64, () => openMenu({
397
- anchor: sizeBtn,
398
- items: FONT_SIZES.map((s) => ({ label: String(s), checked: Math.round(state().fontSize) === s, onClick: () => app.applyFontSize(s) })),
399
- }), '字号');
400
- const boldBtn = small('bold', '', () => app.toggleTextStyle('bold'), '加粗 (Ctrl+B)');
401
- const italicBtn = small('italic', '', () => app.toggleTextStyle('italic'), '斜体 (Ctrl+I)');
402
- const underlineBtn = small('underline', '', () => app.toggleTextStyle('underline'), '下划线 (Ctrl+U)');
403
- const strikeBtn = small('strike', '', () => app.toggleTextStyle('strike'), '删除线');
404
- const incBtn = small('increaseFont', '', () => app.stepFontSize(1), '增大字号');
405
- const decBtn = small('decreaseFont', '', () => app.stepFontSize(-1), '减小字号');
406
- const clearBtn = small('clearFormat', '清除格式', () => app.clearFormatting(), '清除所有格式');
407
- const fontColor = colorButton('fontColor', '', '字体颜色', () => state().color || themeOf(app.snapshot.theme).text, (c) => app.applyTextColor(c));
408
- panel.appendChild(group('字体',
409
- fontBtn, sizeBtn, column(incBtn, decBtn),
410
- column(boldBtn, italicBtn),
411
- column(underlineBtn, strikeBtn),
412
- column(clearBtn, fontColor),
413
- ));
414
-
415
- /* 段落 */
416
- const bulletBtn = small('bullets', '', () => app.applyList('bullet'), '项目符号');
417
- const numberBtn = small('numbering', '', () => app.applyList('number'), '编号');
418
- const outdentBtn = small('indentDecrease', '', () => app.applyIndent(-1), '减少缩进');
419
- const indentBtn = small('indentIncrease', '', () => app.applyIndent(1), '增加缩进');
420
- const lineBtn = combo(74, () => openMenu({
421
- anchor: lineBtn,
422
- items: [1, 1.15, 1.5, 2, 2.5, 3].map((v) => ({
423
- label: v === 1 ? '1.0' : String(v),
424
- checked: Math.abs(state().lineSpacing - v) < 0.01,
425
- onClick: () => app.applyLineSpacing(v),
426
- })),
427
- }), '行距');
428
- const alignLeft = small('alignLeft', '', () => app.applyAlign('left'), '左对齐 (Ctrl+L)');
429
- const alignCenter = small('alignCenter', '', () => app.applyAlign('center'), '居中 (Ctrl+E)');
430
- const alignRight = small('alignRight', '', () => app.applyAlign('right'), '右对齐 (Ctrl+R)');
431
- const alignJustify = small('alignJustify', '', () => app.applyAlign('justify'), '两端对齐 (Ctrl+J)');
432
- panel.appendChild(group('段落',
433
- column(bulletBtn, numberBtn),
434
- column(outdentBtn, indentBtn),
435
- lineBtn,
436
- column(alignLeft, alignCenter),
437
- column(alignRight, alignJustify),
438
- ));
439
-
440
- /* 形状与排列 */
441
- const shapeBtn = big('shapes', '形状', (e) => openShapeGallery(e.currentTarget, (kind) => app.insert('shape', kind)), '插入形状');
442
- const fillBtn = colorButton('fill', '填充', '形状填充', () => (app.firstSelected()?.props?.fill || ''), (c) => app.setSelectedProp('fill', c));
443
- const outlineBtn = colorButton('outline', '轮廓', '形状轮廓', () => (app.firstSelected()?.props?.stroke || ''), (c) => app.setSelectedProp('stroke', c));
444
- const effectsBtn = menuButton('effects', '效果', () => [
445
- { label: '无阴影', checked: !app.firstSelected()?.props?.shadow, icon: 'effects', onClick: () => app.setSelectedProp('shadow', false) },
446
- { label: '阴影', checked: !!app.firstSelected()?.props?.shadow, icon: 'shadow', onClick: () => app.setSelectedProp('shadow', true) },
447
- ], '形状效果');
448
- const arrangeItems = () => [
449
- { label: '置于顶层', icon: 'bringForward', onClick: () => app.arrange('front') },
450
- { label: '上移一层', onClick: () => app.arrange('forward') },
451
- { label: '下移一层', onClick: () => app.arrange('backward') },
452
- { label: '置于底层', icon: 'sendBackward', onClick: () => app.arrange('back') },
453
- { divided: true },
454
- {
455
- label: '对齐',
456
- icon: 'alignLeft',
457
- children: [
458
- { label: '左对齐', onClick: () => app.alignToSlide('left') },
459
- { label: '水平居中', onClick: () => app.alignToSlide('centerH') },
460
- { label: '右对齐', onClick: () => app.alignToSlide('right') },
461
- { divided: true },
462
- { label: '顶端对齐', onClick: () => app.alignToSlide('top') },
463
- { label: '垂直居中', onClick: () => app.alignToSlide('middle') },
464
- { label: '底端对齐', onClick: () => app.alignToSlide('bottom') },
465
- ],
466
- },
467
- {
468
- label: '分布',
469
- children: [
470
- { label: '横向分布', onClick: () => app.distribute('h') },
471
- { label: '纵向分布', onClick: () => app.distribute('v') },
472
- ],
473
- },
474
- ];
475
- const arrangeBtn = menuButton('arrange', '排列', arrangeItems, '排列');
476
- const selectBtn = menuButton('selectAll', '选择', () => [
477
- { label: '全选', shortcut: 'Ctrl+A', onClick: () => app.selectAll() },
478
- { label: '取消选择', shortcut: 'Esc', onClick: () => app.clearSelection() },
479
- ], '选择');
480
- panel.appendChild(group('形状', shapeBtn, column(fillBtn, outlineBtn), column(effectsBtn, arrangeBtn), selectBtn));
481
-
482
- /* 编辑 */
483
- const findBtn = big('find', '查找', () => app.openFind(false), '查找 (Ctrl+F)');
484
- const replaceBtn = small('replace', '替换', () => app.openFind(true), '替换 (Ctrl+H)');
485
- const selectAllBtn = small('selectAll', '全选', () => app.selectAll(), '全选 (Ctrl+A)');
486
- panel.appendChild(group('编辑', findBtn, column(replaceBtn, selectAllBtn)));
487
-
488
- /* 状态刷新 */
489
- this.reg(boldBtn, () => { boldBtn.classList.toggle('is-active', !!state().bold); boldBtn.disabled = !canRich(); });
490
- this.reg(italicBtn, () => { italicBtn.classList.toggle('is-active', !!state().italic); italicBtn.disabled = !canRich(); });
491
- this.reg(underlineBtn, () => { underlineBtn.classList.toggle('is-active', !!state().underline); underlineBtn.disabled = !canRich(); });
492
- this.reg(strikeBtn, () => { strikeBtn.classList.toggle('is-active', !!state().strike); strikeBtn.disabled = !canRich(); });
493
- this.reg(alignLeft, () => { alignLeft.classList.toggle('is-active', state().align === 'left'); alignLeft.disabled = !canAlign(); });
494
- this.reg(alignCenter, () => { alignCenter.classList.toggle('is-active', state().align === 'center'); alignCenter.disabled = !canAlign(); });
495
- this.reg(alignRight, () => { alignRight.classList.toggle('is-active', state().align === 'right'); alignRight.disabled = !canAlign(); });
496
- this.reg(alignJustify, () => { alignJustify.classList.toggle('is-active', state().align === 'justify'); alignJustify.disabled = !canAlign(); });
497
- this.reg(bulletBtn, () => { bulletBtn.classList.toggle('is-active', state().list === 'bullet'); bulletBtn.disabled = !canList(); });
498
- this.reg(numberBtn, () => { numberBtn.classList.toggle('is-active', state().list === 'number'); numberBtn.disabled = !canList(); });
499
- const inList = () => app.editing?.kind === 'text' && Boolean(state().list);
500
- this.reg(outdentBtn, () => { outdentBtn.disabled = !inList(); });
501
- this.reg(indentBtn, () => { indentBtn.disabled = !inList(); });
502
- this.reg(fontBtn, () => {
503
- fontBtn.disabled = !canText();
504
- const f = FONT_FAMILIES.find((x) => x.value === state().font);
505
- fontBtn.querySelector('.pp-rb-combo__label').textContent = f ? f.label : (state().font ? String(state().font).split(',')[0].replace(/["']/g, '') : '默认');
506
- });
507
- this.reg(sizeBtn, () => {
508
- sizeBtn.disabled = !canText();
509
- sizeBtn.querySelector('.pp-rb-combo__label').textContent = state().fontSize ? String(Math.round(state().fontSize)) : '字号';
510
- });
511
- this.reg(incBtn, () => { incBtn.disabled = !canText(); });
512
- this.reg(decBtn, () => { decBtn.disabled = !canText(); });
513
- this.reg(clearBtn, () => {
514
- clearBtn.disabled = !app.editing && !app.selectedObjects().some((o) => ['text', 'shape', 'table'].includes(o.type));
515
- });
516
- this.reg(cutBtn, () => { cutBtn.disabled = !hasSel() && !app.editing; });
517
- this.reg(copyBtn, () => { copyBtn.disabled = !hasSel() && !app.editing; });
518
- this.reg(fontColor, () => {
519
- fontColor.disabled = !canText();
520
- fontColor.colorBar.style.background = state().color || themeOf(app.snapshot.theme).text;
521
- });
522
- this.reg(lineBtn, () => {
523
- lineBtn.disabled = !canRich();
524
- const v = state().lineSpacing || 1;
525
- lineBtn.querySelector('.pp-rb-combo__label').textContent = Math.abs(v - 1) < 0.01 ? '1.0' : String(v);
526
- });
527
- this.reg(fillBtn, () => {
528
- const o = app.firstSelected();
529
- fillBtn.disabled = !o || o.type === 'line';
530
- fillBtn.colorBar.style.background = o?.props?.fill || 'transparent';
531
- fillBtn.classList.toggle('is-off', !o?.props?.fill);
532
- });
533
- this.reg(outlineBtn, () => {
534
- const o = app.firstSelected();
535
- outlineBtn.disabled = !o;
536
- outlineBtn.colorBar.style.background = o?.props?.stroke || 'transparent';
537
- outlineBtn.classList.toggle('is-off', !o?.props?.stroke || !o?.props?.strokeW);
538
- });
539
- this.reg(effectsBtn, () => {
540
- const o = app.firstSelected();
541
- effectsBtn.disabled = !o || o.type === 'line';
542
- effectsBtn.classList.toggle('is-active', !!o?.props?.shadow);
543
- });
544
- this.reg(arrangeBtn, () => { arrangeBtn.disabled = !hasSel(); });
545
- this.reg(selectBtn, () => { selectBtn.disabled = !app.snapshot.slides.length; });
546
- this.reg(pasteMain, () => { pasteMain.disabled = !app.canPaste(); });
547
- this.reg(hideBtn, () => { hideBtn.disabled = false; hideBtn.classList.toggle('is-active', !!app.currentSlide()?.hidden); });
548
- this.reg(resetBtn, () => { resetBtn.disabled = !hasSel(); });
549
- }
550
-
551
- layoutMenu(anchor) {
552
- openMenu({
553
- anchor,
554
- items: SLIDE_LAYOUTS.map((l) => ({ label: l.label, onClick: () => this.app.addSlide(l.key) })),
555
- });
556
- }
557
-
558
- /* ===================== 插入 ===================== */
559
-
560
- buildInsert(panel) {
561
- const app = this.app;
562
- const newSlide = splitBig('newSlide', '新建幻灯片', () => app.addSlide(), (e) => this.layoutMenu(e.currentTarget));
563
- panel.appendChild(group('幻灯片', newSlide));
564
-
565
- const tableBtn = big('table', '表格', (e) => openTableGrid(e.currentTarget, (r, c) => app.insertTable(r, c)), '插入表格');
566
- panel.appendChild(group('表格', tableBtn));
567
-
568
- const pic = big('image', '图片', () => app.insert('image'), '插入图片');
569
- const shape = big('shapes', '形状', (e) => openShapeGallery(e.currentTarget, (kind) => app.insert('shape', kind)), '插入形状');
570
- const text = big('textBox', '文本框', () => app.insert('text'), '插入文本框');
571
- const line = small('line', '线条', () => app.insert('line'), '插入线条');
572
- const arrow = small('arrow', '箭头', () => app.insert('arrow'), '插入箭头');
573
- panel.appendChild(group('插图', pic, shape, text, column(line, arrow)));
574
-
575
- const notes = small('notes', '备注', () => app.toggleNotes(), '显示/隐藏备注');
576
- panel.appendChild(group('显示', notes));
577
- }
578
-
579
- /* ===================== 设计 ===================== */
580
-
581
- buildDesign(panel) {
582
- const app = this.app;
583
- const gallery = el('div', 'pp-theme-gallery');
584
- const cards = new Map();
585
- for (const key of THEME_KEYS) {
586
- const t = themeOf(key);
587
- const card = button('pp-theme-card', '', t.label);
588
- card.style.setProperty('--t-bg', t.bg);
589
- card.style.setProperty('--t-text', t.text);
590
- card.style.setProperty('--t-accent', t.accent);
591
- card.innerHTML = `<span class="pp-theme-card__preview"><i class="pp-theme-card__title"></i><i class="pp-theme-card__line"></i><i class="pp-theme-card__block"></i></span><span class="pp-theme-card__label">${t.label}</span>`;
592
- card.addEventListener('click', () => app.setTheme(key));
593
- cards.set(key, card);
594
- gallery.appendChild(card);
595
- }
596
- panel.appendChild(group('主题', gallery));
597
-
598
- const bgBtn = big('background', '设置背景格式', () => app.openFormatPane(), '设置背景格式');
599
- const bgColor = colorButton('fill', '背景色', '页面背景色', () => app.currentSlide()?.bg || themeOf(app.snapshot.theme).bg, (c) => app.setSlideBg(c));
600
- const bgReset = small('reset', '重置背景', () => app.setSlideBg(''), '跟随主题背景');
601
- panel.appendChild(group('背景', bgBtn, column(bgColor, bgReset)));
602
-
603
- const wide = small('size', '宽屏', () => app.setSlideSize('wide'), '16:9');
604
- const standard = small('size', '标准', () => app.setSlideSize('standard'), '4:3');
605
- panel.appendChild(group('幻灯片大小', column(wide, standard)));
606
-
607
- this.reg(gallery, () => {
608
- for (const [key, card] of cards) card.classList.toggle('is-active', app.snapshot.theme === key);
609
- });
610
- this.reg(bgColor, () => {
611
- const bg = app.currentSlide()?.bg;
612
- bgColor.colorBar.style.background = bg || themeOf(app.snapshot.theme).bg;
613
- bgColor.classList.toggle('is-off', !bg);
614
- });
615
- }
616
-
617
- /* ===================== 切换 ===================== */
618
-
619
- buildTransitions(panel) {
620
- const app = this.app;
621
- const gallery = el('div', 'pp-transition-gallery');
622
- const cards = new Map();
623
- const iconOf = { none: 'close', fade: 'effects', push: 'arrow', wipe: 'outline', cover: 'bringForward', split: 'viewSorter', zoom: 'zoomIn' };
624
- for (const tr of TRANSITIONS) {
625
- const card = button('pp-transition-card', `<span class="pp-transition-card__ico">${icon(iconOf[tr.key] || 'transition', 20)}</span><span>${tr.label}</span>`, tr.label);
626
- card.addEventListener('click', () => {
627
- app.setTransition({ type: tr.key, dir: tr.dir || 'l' });
628
- app.previewTransition();
629
- });
630
- cards.set(tr.key, card);
631
- gallery.appendChild(card);
632
- }
633
- panel.appendChild(group('切换到此幻灯片', gallery));
634
-
635
- const duration = combo(82, () => openMenu({
636
- anchor: duration,
637
- items: [
638
- { label: '0.5 秒', checked: app.currentSlide().transition?.speed === 'fast', onClick: () => app.setTransition({ speed: 'fast' }) },
639
- { label: '1 秒', checked: !app.currentSlide().transition?.speed || app.currentSlide().transition?.speed === 'med', onClick: () => app.setTransition({ speed: 'med' }) },
640
- { label: '2 秒', checked: app.currentSlide().transition?.speed === 'slow', onClick: () => app.setTransition({ speed: 'slow' }) },
641
- ],
642
- }), '持续时间');
643
- const applyAll = small('selectAll', '应用到全部', () => app.applyTransitionAll(), '应用到全部幻灯片');
644
- const preview = big('play', '预览', () => app.previewTransition(), '预览切换');
645
- panel.appendChild(group('计时', duration, column(applyAll)));
646
- panel.appendChild(group('预览', preview));
647
-
648
- this.reg(gallery, () => {
649
- const type = app.currentSlide()?.transition?.type || 'none';
650
- for (const [key, card] of cards) card.classList.toggle('is-active', key === type);
651
- });
652
- this.reg(duration, () => {
653
- const speed = app.currentSlide()?.transition?.speed || 'med';
654
- duration.querySelector('.pp-rb-combo__label').textContent = speed === 'fast' ? '0.5 秒' : speed === 'slow' ? '2 秒' : '1 秒';
655
- });
656
- }
657
-
658
- /* ===================== 幻灯片放映 ===================== */
659
-
660
- buildSlideshow(panel) {
661
- const app = this.app;
662
- const fromStart = big('play', '从头开始', () => app.presentFromStart(), '从头开始放映 (F5)');
663
- const fromCurrent = big('present', '从当前页', () => app.presentFromCurrent(), '从当前幻灯片开始 (Shift+F5)');
664
- const presenter = small('presenter', '演讲者视图', () => app.togglePresenter(), '使用演讲者视图');
665
- const loop = small('loop', '循环放映', () => app.toggleLoop(), '按 Esc 前循环放映');
666
- const hide = small('hide', '隐藏幻灯片', () => app.toggleSlideHidden(), '隐藏当前幻灯片');
667
- panel.appendChild(group('开始放映幻灯片', fromStart, fromCurrent, column(presenter, hide)));
668
- panel.appendChild(group('设置', column(loop)));
669
-
670
- this.reg(presenter, () => presenter.classList.toggle('is-active', !!app.presentOptions.presenter));
671
- this.reg(loop, () => loop.classList.toggle('is-active', !!app.presentOptions.loop));
672
- this.reg(hide, () => hide.classList.toggle('is-active', !!app.currentSlide()?.hidden));
673
- }
674
-
675
- /* ===================== 审阅 ===================== */
676
-
677
- buildReview(panel) {
678
- const app = this.app;
679
- const notes = big('notes', '显示备注', () => app.toggleNotes(), '显示/隐藏备注窗格');
680
- const count = big('wordCount', '字数统计', () => app.showWordCount(), '统计幻灯片与文字数量');
681
- panel.appendChild(group('备注', notes));
682
- panel.appendChild(group('校对', count));
683
- this.reg(notes, () => notes.classList.toggle('is-active', !!app.notesVisible));
684
- }
685
-
686
- /* ===================== 视图 ===================== */
687
-
688
- buildView(panel) {
689
- const app = this.app;
690
- const normal = big('viewNormal', '普通', () => app.setView('normal'), '普通视图');
691
- const sorter = big('viewSorter', '幻灯片浏览', () => app.setView('sorter'), '幻灯片浏览视图');
692
- const reading = big('viewReading', '阅读视图', () => app.setView('reading'), '阅读视图');
693
- const grid = small('grid', '网格线', () => app.toggleGrid(), '显示网格线');
694
- const guides = small('guides', '参考线', () => app.toggleGuides(), '显示参考线');
695
- const notes = small('notes', '备注', () => app.toggleNotes(), '显示备注');
696
- const pane = small('formatPane', '格式窗格', () => app.toggleFormatPane(), '设置形状格式窗格');
697
- const fit = big('fitWindow', '适应窗口', () => app.fitToWindow(), '缩放到适合窗口');
698
- const zoom = combo(76, () => openMenu({
699
- anchor: zoom,
700
- items: [50, 75, 100, 125, 150, 200, 400].map((z) => ({ label: `${z}%`, checked: app.zoom === z, onClick: () => app.setZoom(z) })),
701
- }), '缩放');
702
- panel.appendChild(group('演示文稿视图', normal, sorter, reading));
703
- panel.appendChild(group('显示', column(grid, guides), column(notes, pane)));
704
- panel.appendChild(group('缩放', fit, zoom));
705
-
706
- this.reg(normal, () => normal.classList.toggle('is-active', app.view === 'normal'));
707
- this.reg(sorter, () => sorter.classList.toggle('is-active', app.view === 'sorter'));
708
- this.reg(reading, () => reading.classList.toggle('is-active', app.view === 'reading'));
709
- this.reg(grid, () => grid.classList.toggle('is-active', !!app.gridVisible));
710
- this.reg(guides, () => guides.classList.toggle('is-active', !!app.guidesVisible));
711
- this.reg(notes, () => notes.classList.toggle('is-active', !!app.notesVisible));
712
- this.reg(pane, () => pane.classList.toggle('is-active', !!app.formatPaneOpen));
713
- this.reg(zoom, () => { zoom.querySelector('.pp-rb-combo__label').textContent = `${app.zoom}%`; });
714
- }
715
-
716
- /* ===================== 上下文:形状 ===================== */
717
-
718
- buildContextShape(panel) {
719
- const app = this.app;
720
- const shapeBtn = big('shapes', '更改形状', (e) => openShapeGallery(e.currentTarget, (kind) => app.setSelectedProp('kind', kind)), '更改形状');
721
- const gallery = el('div', 'pp-style-gallery');
722
- const cards = new Map();
723
- const render = () => {
724
- gallery.replaceChildren();
725
- for (const s of shapeQuickStyles(app.snapshot.theme)) {
726
- const card = button('pp-style-card', `<span style="background:${s.fill || 'transparent'};border-color:${s.stroke || 'transparent'};color:${s.text}">Aa</span><small>${s.label}</small>`, s.label);
727
- card.addEventListener('click', () => app.applyShapeQuickStyle(s));
728
- cards.set(s.key, card);
729
- gallery.appendChild(card);
730
- }
731
- };
732
- render();
733
- panel.appendChild(group('插入形状', shapeBtn));
734
- panel.appendChild(group('形状样式', gallery));
735
-
736
- const fillBtn = colorButton('fill', '填充', '形状填充', () => app.firstSelected()?.props?.fill || '', (c) => app.setSelectedProp('fill', c));
737
- const outlineColor = colorButton('outline', '轮廓', '形状轮廓', () => app.firstSelected()?.props?.stroke || '', (c) => app.setSelectedProp('stroke', c));
738
- const outlineWidth = menuButton('outline', '粗细', () => [1, 2, 3, 4, 6].map((w) => ({
739
- label: `${w} px`, checked: Math.round(app.firstSelected()?.props?.strokeW) === w, onClick: () => app.setSelectedProp('strokeW', w),
740
- })), '轮廓粗细');
741
- const dash = menuButton('line', '虚线', () => [
742
- { label: '实线', checked: (app.firstSelected()?.props?.dash || 'solid') === 'solid', onClick: () => app.setSelectedProp('dash', 'solid') },
743
- { label: '虚线', checked: app.firstSelected()?.props?.dash === 'dash', onClick: () => app.setSelectedProp('dash', 'dash') },
744
- { label: '点线', checked: app.firstSelected()?.props?.dash === 'dot', onClick: () => app.setSelectedProp('dash', 'dot') },
745
- { label: '点划线', checked: app.firstSelected()?.props?.dash === 'dashDot', onClick: () => app.setSelectedProp('dash', 'dashDot') },
746
- ], '轮廓样式');
747
- const shadow = small('shadow', '阴影', () => app.setSelectedProp('shadow', !app.firstSelected()?.props?.shadow), '阴影');
748
- const noOutline = small('close', '无轮廓', () => app.setSelectedProp('strokeW', 0), '无轮廓');
749
- panel.appendChild(group('形状填充与轮廓', column(fillBtn, noOutline), column(outlineColor, outlineWidth), column(dash, shadow)));
750
-
751
- const rotateLeft = small('rotate', '左转 90°', () => app.rotateSelected(-90), '向左旋转 90°');
752
- const rotateRight = small('rotate', '右转 90°', () => app.rotateSelected(90), '向右旋转 90°');
753
- const arrange = menuButton('arrange', '排列', () => [
754
- { label: '置于顶层', onClick: () => app.arrange('front') },
755
- { label: '上移一层', onClick: () => app.arrange('forward') },
756
- { label: '下移一层', onClick: () => app.arrange('backward') },
757
- { label: '置于底层', onClick: () => app.arrange('back') },
758
- ], '排列');
759
- panel.appendChild(group('排列', column(rotateLeft, rotateRight), arrange));
760
-
761
- this.buildSizeGroup(panel);
762
-
763
- this.reg(gallery, () => {
764
- // 快速样式不强制高亮(组合样式难以反推),仅保持可点
765
- });
766
- this.reg(fillBtn, () => { fillBtn.colorBar.style.background = app.firstSelected()?.props?.fill || 'transparent'; });
767
- this.reg(outlineColor, () => { outlineColor.colorBar.style.background = app.firstSelected()?.props?.stroke || 'transparent'; });
768
- this.reg(shadow, () => { shadow.classList.toggle('is-active', !!app.firstSelected()?.props?.shadow); });
769
- this.reg(noOutline, () => { noOutline.disabled = !app.firstSelected(); });
770
- }
771
-
772
- /* ===================== 上下文:图片 ===================== */
773
-
774
- buildContextImage(panel) {
775
- const app = this.app;
776
- const replace = big('replaceImage', '替换图片', () => app.replaceSelectedImage(), '替换图片');
777
- const reset = big('resetImage', '重置图片', () => app.resetSelectedImage(), '重置裁剪与效果');
778
- const crop = small('crop', '裁剪', () => app.cropSelected(), '裁剪图片');
779
- panel.appendChild(group('调整', replace, reset, column(crop)));
780
-
781
- const shadow = small('shadow', '阴影', () => app.setSelectedProp('shadow', !app.firstSelected()?.props?.shadow), '阴影');
782
- const alt = small('textBox', '替代文字', () => app.editAltText(), '编辑替代文字(无障碍)');
783
- panel.appendChild(group('图片样式', column(shadow, alt)));
784
-
785
- const arrange = menuButton('arrange', '排列', () => [
786
- { label: '置于顶层', onClick: () => app.arrange('front') },
787
- { label: '上移一层', onClick: () => app.arrange('forward') },
788
- { label: '下移一层', onClick: () => app.arrange('backward') },
789
- { label: '置于底层', onClick: () => app.arrange('back') },
790
- ], '排列');
791
- panel.appendChild(group('排列', arrange));
792
- this.buildSizeGroup(panel);
793
- this.reg(shadow, () => { shadow.classList.toggle('is-active', !!app.firstSelected()?.props?.shadow); });
794
- }
795
-
796
- /* ===================== 上下文:表格 ===================== */
797
-
798
- buildContextTable(panel) {
799
- const app = this.app;
800
- const gallery = el('div', 'pp-table-style-gallery');
801
- const cards = new Map();
802
- for (const s of TABLE_STYLES) {
803
- const card = button('pp-table-style-card', `<span class="pp-table-style-card__preview pp-table-style-card__preview--${s.key}"><i></i><i></i><i></i><i></i></span><span>${s.label}</span>`, s.label);
804
- card.addEventListener('click', () => app.setTableStyle(s.key));
805
- cards.set(s.key, card);
806
- gallery.appendChild(card);
807
- }
808
- panel.appendChild(group('表格样式', gallery));
809
-
810
- const header = small('tableStyle', '标题行', () => app.toggleHeaderRow(), '标题行');
811
- const banded = small('tableStyle', '镶边行', () => app.toggleBandedRows(), '镶边行');
812
- panel.appendChild(group('样式选项', column(header, banded)));
813
-
814
- const rowAbove = small('rowAbove', '上方', () => app.insertTableRow('above'), '在上方插入行');
815
- const rowBelow = small('rowBelow', '下方', () => app.insertTableRow('below'), '在下方插入行');
816
- const colLeft = small('colLeft', '左侧', () => app.insertTableColumn('left'), '在左侧插入列');
817
- const colRight = small('colRight', '右侧', () => app.insertTableColumn('right'), '在右侧插入列');
818
- const delRow = small('delete', '删除行', () => app.deleteTableRow(), '删除行');
819
- const delCol = small('delete', '删除列', () => app.deleteTableColumn(), '删除列');
820
- panel.appendChild(group('行和列', column(rowAbove, rowBelow), column(colLeft, colRight), column(delRow, delCol)));
821
-
822
- const arrange = menuButton('arrange', '排列', () => [
823
- { label: '置于顶层', onClick: () => app.arrange('front') },
824
- { label: '置于底层', onClick: () => app.arrange('back') },
825
- ], '排列');
826
- panel.appendChild(group('排列', arrange));
827
- this.buildSizeGroup(panel);
828
-
829
- this.reg(gallery, () => {
830
- const o = app.firstSelected();
831
- for (const [key, card] of cards) card.classList.toggle('is-active', o?.props?.tableStyle === key);
832
- });
833
- this.reg(header, () => { header.classList.toggle('is-active', !!app.firstSelected()?.props?.headerRow); });
834
- this.reg(banded, () => { banded.classList.toggle('is-active', !!app.firstSelected()?.props?.bandedRows); });
835
- }
836
-
837
- /** 大小与位置组(单选对象) */
838
- buildSizeGroup(panel) {
839
- const app = this.app;
840
- const wInput = el('input', 'pp-rb-num');
841
- wInput.type = 'number';
842
- wInput.step = '1';
843
- wInput.min = '1';
844
- wInput.title = '宽度';
845
- const hInput = el('input', 'pp-rb-num');
846
- hInput.type = 'number';
847
- hInput.step = '1';
848
- hInput.min = '1';
849
- hInput.title = '高度';
850
- wInput.addEventListener('mousedown', (e) => e.stopPropagation());
851
- hInput.addEventListener('mousedown', (e) => e.stopPropagation());
852
- const apply = (key, input) => {
853
- const o = app.firstSelected();
854
- const v = Number(input.value);
855
- if (!o || !Number.isFinite(v) || v <= 0) return;
856
- app.updateObject(o.id, { [key]: key === 'w' || key === 'h' ? Math.round(v * (o[key] < 0 ? -1 : 1)) : Math.round(v) });
857
- };
858
- wInput.addEventListener('change', () => apply('w', wInput));
859
- hInput.addEventListener('change', () => apply('h', hInput));
860
- const wWrap = el('label', 'pp-rb-field');
861
- wWrap.append(el('span', '', '宽'), wInput);
862
- const hWrap = el('label', 'pp-rb-field');
863
- hWrap.append(el('span', '', '高'), hInput);
864
- panel.appendChild(group('大小', column(wWrap, hWrap)));
865
- this.reg(wInput, () => {
866
- if (document.activeElement === wInput) return;
867
- const o = app.firstSelected();
868
- wInput.value = o ? String(Math.round(Math.abs(o.w))) : '';
869
- wInput.disabled = !o;
870
- });
871
- this.reg(hInput, () => {
872
- if (document.activeElement === hInput) return;
873
- const o = app.firstSelected();
874
- hInput.value = o ? String(Math.round(Math.abs(o.h))) : '';
875
- hInput.disabled = !o;
876
- });
877
- }
878
- }
1
+ // [文件] gbpptx·Ribbon —— Office 式功能区(开始/插入/设计/切换/放映/审阅/视图 + 形状/图片/表格上下文标签)
2
+
3
+ import {
4
+ FONT_FAMILIES, FONT_SIZES, TEXT_COLORS,
5
+ } from './text-styles.js';
6
+ import {
7
+ SHAPE_KINDS, SLIDE_LAYOUTS, TABLE_STYLES, THEME_KEYS, TRANSITIONS, contrastText, shapePath, themeOf,
8
+ } from '../../../src/kernel/model.js';
9
+ import { icon } from './icons.js';
10
+ import { openMenu } from './menu.js';
11
+ import { openPopup } from './popup.js';
12
+ import { openColorPanel } from './color-panel.js';
13
+
14
+ const keepFocus = (el) => {
15
+ el.addEventListener('mousedown', (e) => {
16
+ if (e.button === 0) e.preventDefault();
17
+ });
18
+ };
19
+
20
+ const el = (tag, cls, html) => {
21
+ const node = document.createElement(tag);
22
+ if (cls) node.className = cls;
23
+ if (html != null) node.innerHTML = html;
24
+ return node;
25
+ };
26
+
27
+ const button = (cls, html, title) => {
28
+ const b = el('button', cls, html);
29
+ b.type = 'button';
30
+ if (title) b.title = title;
31
+ keepFocus(b);
32
+ return b;
33
+ };
34
+
35
+ const big = (name, label, onClick, title) => {
36
+ const b = button('pp-rb-big', `${icon(name, 26)}<span class="pp-rb-big__label">${label}</span>`, title || label);
37
+ b.addEventListener('click', onClick);
38
+ return b;
39
+ };
40
+
41
+ const splitBig = (name, label, onClick, onMenu, title) => {
42
+ const wrap = el('div', 'pp-rb-split');
43
+ const main = big(name, label, onClick, title);
44
+ const caret = button('pp-rb-split__caret', icon('chevronDown', 12), '更多');
45
+ caret.addEventListener('click', onMenu);
46
+ wrap.append(main, caret);
47
+ return wrap;
48
+ };
49
+
50
+ const small = (name, label, onClick, title, cls = '') => {
51
+ const b = button(`pp-rb-small${cls ? ` ${cls}` : ''}`, `${icon(name, 16)}${label ? `<span>${label}</span>` : ''}`, title || label || name);
52
+ b.addEventListener('click', onClick);
53
+ return b;
54
+ };
55
+
56
+ const menuButton = (name, label, items, title, cls = '') => {
57
+ const b = small(name, label, () => openMenu({ anchor: b, items: items() }), title || label, cls);
58
+ return b;
59
+ };
60
+
61
+ const column = (...children) => {
62
+ const c = el('div', 'pp-rb-col');
63
+ c.append(...children);
64
+ return c;
65
+ };
66
+
67
+ const group = (label, ...children) => {
68
+ const g = el('div', 'pp-rb-group');
69
+ const body = el('div', 'pp-rb-group__body');
70
+ body.append(...children);
71
+ const cap = el('div', 'pp-rb-group__label');
72
+ cap.textContent = label;
73
+ g.append(body, cap);
74
+ return g;
75
+ };
76
+
77
+ const combo = (width, onClick, title) => {
78
+ const b = button('pp-rb-combo', '<span class="pp-rb-combo__label"></span><span class="pp-rb-combo__caret">▾</span>', title);
79
+ b.style.width = `${width}px`;
80
+ b.addEventListener('click', onClick);
81
+ return b;
82
+ };
83
+
84
+ const colorButton = (name, label, title, getColor, onPick) => {
85
+ const b = button('pp-rb-small pp-rb-colorbtn', `${icon(name, 16)}${label ? `<span>${label}</span>` : ''}<i class="pp-rb-colorbar"></i>`, title);
86
+ b.addEventListener('click', (e) => {
87
+ openColorPanel({
88
+ anchor: e.currentTarget,
89
+ colors: TEXT_COLORS,
90
+ value: getColor() || '',
91
+ columns: 10,
92
+ title,
93
+ onPick,
94
+ });
95
+ });
96
+ b.colorBar = b.querySelector('.pp-rb-colorbar');
97
+ return b;
98
+ };
99
+
100
+ const blend = (a, b, t) => {
101
+ const parse = (hex) => {
102
+ const n = Number.parseInt(String(hex).replace('#', ''), 16);
103
+ return { r: (n >> 16) & 255, g: (n >> 8) & 255, b: n & 255 };
104
+ };
105
+ const x = parse(a);
106
+ const y = parse(b);
107
+ const k = Math.max(0, Math.min(1, t));
108
+ return `#${[x.r + (y.r - x.r) * k, x.g + (y.g - x.g) * k, x.b + (y.b - x.b) * k]
109
+ .map((v) => Math.round(v).toString(16).padStart(2, '0')).join('')}`;
110
+ };
111
+
112
+ const readableText = (background, fallback) => {
113
+ const n = Number.parseInt(String(background).replace('#', ''), 16);
114
+ const lum = Number.isFinite(n) ? (0.2126 * ((n >> 16) & 255) + 0.7152 * ((n >> 8) & 255) + 0.0722 * (n & 255)) / 255 : 1;
115
+ return lum > 0.55 ? '#201f1e' : fallback;
116
+ };
117
+
118
+ /** 形状快速样式(按当前主题推导) */
119
+ export function shapeQuickStyles(themeKey) {
120
+ const t = themeOf(themeKey);
121
+ return [
122
+ { key: 'accent', label: '彩色填充', fill: t.accent, stroke: '', text: contrastText(t.accent) },
123
+ { key: 'light', label: '浅色填充', fill: blend(t.accent, t.bg, 0.8), stroke: t.accent, text: readableText(blend(t.accent, t.bg, 0.8), t.text) },
124
+ { key: 'outline', label: '轮廓', fill: '', stroke: t.accent, text: t.text },
125
+ { key: 'deep', label: '深色填充', fill: blend(t.accent, '#000000', 0.55), stroke: '', text: '#ffffff' },
126
+ { key: 'paper', label: '浅色轮廓', fill: t.bg, stroke: blend(t.text, t.bg, 0.5), text: t.text },
127
+ { key: 'dark', label: '深色强调', fill: t.text, stroke: '', text: contrastText(t.text) },
128
+ ];
129
+ }
130
+
131
+ function openShapeGallery(anchor, onPick) {
132
+ const box = el('div', 'pp-shape-gallery');
133
+ const handle = { close: () => {} };
134
+ for (const s of SHAPE_KINDS) {
135
+ const item = button('pp-shape-gallery__item', `<svg viewBox="0 0 40 30" width="40" height="30" aria-hidden="true"><path d="${shapePath(s.kind, 34, 24)}" transform="translate(3,3)" fill="currentColor"/></svg><span>${s.label}</span>`, s.label);
136
+ item.addEventListener('click', () => { handle.close(); onPick(s.kind); });
137
+ box.appendChild(item);
138
+ }
139
+ Object.assign(handle, openPopup({ anchor, content: box, placement: 'bottom-start', className: 'pp-popup--gallery' }));
140
+ return handle;
141
+ }
142
+
143
+ /** Office 式表格尺寸选择器 */
144
+ function openTableGrid(anchor, onPick) {
145
+ const box = el('div', 'pp-table-grid');
146
+ const title = el('div', 'pp-table-grid__title', '插入表格');
147
+ const hint = el('div', 'pp-table-grid__hint', '1×1 表格');
148
+ const grid = el('div', 'pp-table-grid__grid');
149
+ const cells = [];
150
+ for (let r = 1; r <= 8; r += 1) {
151
+ for (let c = 1; c <= 10; c += 1) {
152
+ const cell = button('pp-table-grid__cell', '', `${r}×${c}`);
153
+ cell.dataset.r = String(r);
154
+ cell.dataset.c = String(c);
155
+ cell.addEventListener('mouseenter', () => {
156
+ hint.textContent = `${r}×${c} 表格`;
157
+ for (const x of cells) {
158
+ x.classList.toggle('gbp-is-on', Number(x.dataset.r) <= r && Number(x.dataset.c) <= c);
159
+ }
160
+ });
161
+ cell.addEventListener('click', () => { handle.close(); onPick(r, c); });
162
+ cells.push(cell);
163
+ grid.appendChild(cell);
164
+ }
165
+ }
166
+ grid.addEventListener('mouseleave', () => {
167
+ for (const x of cells) x.classList.remove('gbp-is-on');
168
+ hint.textContent = '1×1 表格';
169
+ });
170
+ box.append(title, grid, hint);
171
+ const handle = openPopup({ anchor, content: box, placement: 'bottom-start', className: 'pp-popup--table-grid' });
172
+ return handle;
173
+ }
174
+
175
+ export class Ribbon {
176
+ constructor(app) {
177
+ this.app = app;
178
+ this.el = el('div', 'pp-ribbon');
179
+ this.updates = [];
180
+ this.contextKey = '';
181
+ this.contextTabs = [];
182
+ this.panels = new Map();
183
+ this.activeTab = 'home';
184
+
185
+ this.tabsEl = el('div', 'pp-ribbon__tabs');
186
+ this.bodyEl = el('div', 'pp-ribbon__body');
187
+ this.el.append(this.tabsEl, this.bodyEl);
188
+ this.buildTabs();
189
+ this.buildPanels();
190
+ this.activate(app.prefs.ribbonTab && this.isKnownTab(app.prefs.ribbonTab) ? app.prefs.ribbonTab : 'home', { expand: false });
191
+ this.setCollapsed(!!app.ribbonCollapsed);
192
+ this.refresh();
193
+ }
194
+
195
+ /* ===================== 骨架 ===================== */
196
+
197
+ isKnownTab(key) {
198
+ return ['home', 'insert', 'design', 'transitions', 'slideshow', 'review', 'view'].includes(key);
199
+ }
200
+
201
+ buildTabs() {
202
+ const defs = [
203
+ ['home', '开始'], ['insert', '插入'], ['design', '设计'], ['transitions', '切换'],
204
+ ['slideshow', '幻灯片放映'], ['review', '审阅'], ['view', '视图'],
205
+ ];
206
+ for (const [key, label] of defs) {
207
+ const tab = button('pp-rb-tab', label, label);
208
+ tab.dataset.tab = key;
209
+ tab.addEventListener('click', () => this.activate(key));
210
+ this.tabsEl.appendChild(tab);
211
+ }
212
+ this.contextHost = el('div', 'pp-rb-tabs__context');
213
+ this.tabsEl.appendChild(this.contextHost);
214
+ const spacer = el('div', 'pp-rb-tabs__spacer');
215
+ this.tabsEl.appendChild(spacer);
216
+ this.previewBtn = button('pp-rb-preview', `${icon('eye', 15)}<span>预览</span>`, '预览模式');
217
+ this.previewBtn.setAttribute('aria-pressed', 'false');
218
+ this.previewBtn.addEventListener('click', () => this.app.setPreview(!this.app.preview));
219
+ this.tabsEl.appendChild(this.previewBtn);
220
+ this.collapseBtn = button('pp-rb-collapse', icon('chevronDown', 14), '收起功能区');
221
+ this.collapseBtn.addEventListener('click', () => {
222
+ this.app.setRibbonCollapsed(!this.app.ribbonCollapsed);
223
+ });
224
+ this.tabsEl.appendChild(this.collapseBtn);
225
+ }
226
+
227
+ /** 预览档按钮态:同一位置从「预览」切成「退出预览」 */
228
+ setPreview(on) {
229
+ const active = !!on;
230
+ this.previewBtn.classList.toggle('gbp-is-active', active);
231
+ this.previewBtn.title = active ? '退出预览' : '预览模式';
232
+ this.previewBtn.setAttribute('aria-pressed', String(active));
233
+ this.previewBtn.querySelector('span').textContent = active ? '退出预览' : '预览';
234
+ }
235
+
236
+ buildPanels() {
237
+ const builders = {
238
+ home: (p) => this.buildHome(p),
239
+ insert: (p) => this.buildInsert(p),
240
+ design: (p) => this.buildDesign(p),
241
+ transitions: (p) => this.buildTransitions(p),
242
+ slideshow: (p) => this.buildSlideshow(p),
243
+ review: (p) => this.buildReview(p),
244
+ view: (p) => this.buildView(p),
245
+ };
246
+ for (const [key, build] of Object.entries(builders)) {
247
+ const panel = el('div', 'pp-rb-panel');
248
+ panel.dataset.tab = key;
249
+ panel.hidden = true;
250
+ build(panel);
251
+ this.panels.set(key, panel);
252
+ this.bodyEl.appendChild(panel);
253
+ }
254
+ }
255
+
256
+ panel(key) {
257
+ if (!this.panels.has(key)) {
258
+ const panel = el('div', 'pp-rb-panel');
259
+ panel.dataset.tab = key;
260
+ panel.hidden = true;
261
+ if (key === 'ctx-shape') this.buildContextShape(panel);
262
+ if (key === 'ctx-image') this.buildContextImage(panel);
263
+ if (key === 'ctx-table') this.buildContextTable(panel);
264
+ this.panels.set(key, panel);
265
+ this.bodyEl.appendChild(panel);
266
+ }
267
+ return this.panels.get(key);
268
+ }
269
+
270
+ activate(key, { expand = true } = {}) {
271
+ if (!this.isKnownTab(key) && !['ctx-shape', 'ctx-image', 'ctx-table'].includes(key)) key = 'home';
272
+ this.activeTab = key;
273
+ this.app.prefs.ribbonTab = key;
274
+ for (const tab of this.tabsEl.querySelectorAll('.pp-rb-tab')) {
275
+ tab.classList.toggle('gbp-is-active', tab.dataset.tab === key);
276
+ }
277
+ for (const [k, panel] of this.panels) panel.hidden = k !== key;
278
+ this.panel(key);
279
+ for (const [k, panel] of this.panels) panel.hidden = k !== key;
280
+ if (expand && this.app.ribbonCollapsed) this.app.setRibbonCollapsed(false);
281
+ this.refresh();
282
+ }
283
+
284
+ setCollapsed(on) {
285
+ this.el.classList.toggle('gbp-is-collapsed', !!on);
286
+ this.collapseBtn.innerHTML = icon(on ? 'chevronUp' : 'chevronDown', 14);
287
+ this.collapseBtn.title = on ? '展开功能区' : '收起功能区';
288
+ }
289
+
290
+ reg(target, update) {
291
+ this.updates.push({ target, update });
292
+ update();
293
+ return target;
294
+ }
295
+
296
+ refresh() {
297
+ this.ensureContext();
298
+ for (const entry of this.updates) {
299
+ if (!entry.target.isConnected) continue;
300
+ entry.update();
301
+ }
302
+ }
303
+
304
+ /* ===================== 上下文标签 ===================== */
305
+
306
+ selectionKind() {
307
+ const app = this.app;
308
+ if (app.selectedIds.length !== 1) return '';
309
+ const o = app.firstSelected();
310
+ if (!o) return '';
311
+ if (o.type === 'shape' || o.type === 'line') return 'shape';
312
+ if (o.type === 'image') return 'image';
313
+ if (o.type === 'table') return 'table';
314
+ return '';
315
+ }
316
+
317
+ ensureContext() {
318
+ const kind = this.selectionKind();
319
+ if (kind === this.contextKey) return;
320
+ this.contextHost.replaceChildren();
321
+ for (const t of this.contextTabs) {
322
+ this.panels.get(t.key)?.remove();
323
+ this.panels.delete(t.key);
324
+ this.updates = this.updates.filter((u) => u.target.isConnected);
325
+ }
326
+ this.contextTabs = [];
327
+ this.contextKey = kind;
328
+ if (!kind) {
329
+ if (this.activeTab.startsWith('ctx-')) this.activate('home', { expand: false });
330
+ return;
331
+ }
332
+ const defs = {
333
+ shape: { key: 'ctx-shape', label: '形状格式' },
334
+ image: { key: 'ctx-image', label: '图片格式' },
335
+ table: { key: 'ctx-table', label: '表格' },
336
+ };
337
+ const def = defs[kind];
338
+ const tab = button('pp-rb-tab pp-rb-tab--context', def.label, def.label);
339
+ tab.dataset.tab = def.key;
340
+ tab.addEventListener('click', () => this.activate(def.key));
341
+ this.contextHost.appendChild(tab);
342
+ this.contextTabs.push(def);
343
+ this.panel(def.key);
344
+ if (this.activeTab.startsWith('ctx-')) this.activate(def.key, { expand: false });
345
+ }
346
+
347
+ /* ===================== 开始 ===================== */
348
+
349
+ buildHome(panel) {
350
+ const app = this.app;
351
+ const hasSel = () => app.selectedIds.length > 0;
352
+ const canText = () => app.canFormatText();
353
+ const canRich = () => app.canRichText();
354
+ const canAlign = () => app.canAlignText();
355
+ const canList = () => app.canList();
356
+ const state = () => app.textState();
357
+
358
+ /* 剪贴板 */
359
+ const pasteWrap = el('div', 'pp-rb-split');
360
+ const pasteMain = big('paste', '粘贴', () => app.paste(), '粘贴 (Ctrl+V)');
361
+ const pasteCaret = button('pp-rb-split__caret', icon('chevronDown', 12), '粘贴选项');
362
+ pasteCaret.addEventListener('click', () => openMenu({
363
+ anchor: pasteCaret,
364
+ items: [
365
+ { label: '粘贴', icon: 'paste', onClick: () => app.paste() },
366
+ { label: '粘贴为纯文本', icon: 'text', onClick: () => app.pastePlain() },
367
+ ],
368
+ }));
369
+ pasteWrap.append(pasteMain, pasteCaret);
370
+ const cutBtn = small('cut', '剪切', () => app.cutSelected(), '剪切 (Ctrl+X)');
371
+ const copyBtn = small('copy', '复制', () => app.copySelected(), '复制 (Ctrl+C)');
372
+ const clip = group('剪贴板', pasteWrap, column(cutBtn, copyBtn));
373
+ panel.appendChild(clip);
374
+
375
+ /* 幻灯片 */
376
+ const newSlide = splitBig('newSlide', '新建幻灯片', () => app.addSlide(), (e) => this.layoutMenu(e.currentTarget), '新建幻灯片 (Ctrl+M)');
377
+ const hideBtn = small('hide', '隐藏', () => app.toggleSlideHidden(), '隐藏幻灯片');
378
+ const resetBtn = small('reset', '重设', () => app.resetSelected(), '重设位置与大小');
379
+ panel.appendChild(group('幻灯片', newSlide, column(hideBtn, resetBtn)));
380
+
381
+ /* 字体 */
382
+ const fontBtn = combo(132, () => openMenu({
383
+ anchor: fontBtn,
384
+ items: FONT_FAMILIES.map((f) => ({ label: f.label, checked: state().font === f.value, onClick: () => app.applyFontFamily(f.value) })),
385
+ }), '字体');
386
+ const sizeBtn = combo(64, () => openMenu({
387
+ anchor: sizeBtn,
388
+ items: FONT_SIZES.map((s) => ({ label: String(s), checked: Math.round(state().fontSize) === s, onClick: () => app.applyFontSize(s) })),
389
+ }), '字号');
390
+ const boldBtn = small('bold', '', () => app.toggleTextStyle('bold'), '加粗 (Ctrl+B)');
391
+ const italicBtn = small('italic', '', () => app.toggleTextStyle('italic'), '斜体 (Ctrl+I)');
392
+ const underlineBtn = small('underline', '', () => app.toggleTextStyle('underline'), '下划线 (Ctrl+U)');
393
+ const strikeBtn = small('strike', '', () => app.toggleTextStyle('strike'), '删除线');
394
+ const incBtn = small('increaseFont', '', () => app.stepFontSize(1), '增大字号');
395
+ const decBtn = small('decreaseFont', '', () => app.stepFontSize(-1), '减小字号');
396
+ const clearBtn = small('clearFormat', '清除格式', () => app.clearFormatting(), '清除所有格式');
397
+ const fontColor = colorButton('fontColor', '', '字体颜色', () => state().color || themeOf(app.snapshot.theme).text, (c) => app.applyTextColor(c));
398
+ panel.appendChild(group('字体',
399
+ fontBtn, sizeBtn, column(incBtn, decBtn),
400
+ column(boldBtn, italicBtn),
401
+ column(underlineBtn, strikeBtn),
402
+ column(clearBtn, fontColor),
403
+ ));
404
+
405
+ /* 段落 */
406
+ const bulletBtn = small('bullets', '', () => app.applyList('bullet'), '项目符号');
407
+ const numberBtn = small('numbering', '', () => app.applyList('number'), '编号');
408
+ const outdentBtn = small('indentDecrease', '', () => app.applyIndent(-1), '减少缩进');
409
+ const indentBtn = small('indentIncrease', '', () => app.applyIndent(1), '增加缩进');
410
+ const lineBtn = combo(74, () => openMenu({
411
+ anchor: lineBtn,
412
+ items: [1, 1.15, 1.5, 2, 2.5, 3].map((v) => ({
413
+ label: v === 1 ? '1.0' : String(v),
414
+ checked: Math.abs(state().lineSpacing - v) < 0.01,
415
+ onClick: () => app.applyLineSpacing(v),
416
+ })),
417
+ }), '行距');
418
+ const alignLeft = small('alignLeft', '', () => app.applyAlign('left'), '左对齐 (Ctrl+L)');
419
+ const alignCenter = small('alignCenter', '', () => app.applyAlign('center'), '居中 (Ctrl+E)');
420
+ const alignRight = small('alignRight', '', () => app.applyAlign('right'), '右对齐 (Ctrl+R)');
421
+ const alignJustify = small('alignJustify', '', () => app.applyAlign('justify'), '两端对齐 (Ctrl+J)');
422
+ panel.appendChild(group('段落',
423
+ column(bulletBtn, numberBtn),
424
+ column(outdentBtn, indentBtn),
425
+ lineBtn,
426
+ column(alignLeft, alignCenter),
427
+ column(alignRight, alignJustify),
428
+ ));
429
+
430
+ /* 形状与排列 */
431
+ const shapeBtn = big('shapes', '形状', (e) => openShapeGallery(e.currentTarget, (kind) => app.insert('shape', kind)), '插入形状');
432
+ const fillBtn = colorButton('fill', '填充', '形状填充', () => (app.firstSelected()?.props?.fill || ''), (c) => app.setSelectedProp('fill', c));
433
+ const outlineBtn = colorButton('outline', '轮廓', '形状轮廓', () => (app.firstSelected()?.props?.stroke || ''), (c) => app.setSelectedProp('stroke', c));
434
+ const effectsBtn = menuButton('effects', '效果', () => [
435
+ { label: '无阴影', checked: !app.firstSelected()?.props?.shadow, icon: 'effects', onClick: () => app.setSelectedProp('shadow', false) },
436
+ { label: '阴影', checked: !!app.firstSelected()?.props?.shadow, icon: 'shadow', onClick: () => app.setSelectedProp('shadow', true) },
437
+ ], '形状效果');
438
+ const arrangeItems = () => [
439
+ { label: '置于顶层', icon: 'bringForward', onClick: () => app.arrange('front') },
440
+ { label: '上移一层', onClick: () => app.arrange('forward') },
441
+ { label: '下移一层', onClick: () => app.arrange('backward') },
442
+ { label: '置于底层', icon: 'sendBackward', onClick: () => app.arrange('back') },
443
+ { divided: true },
444
+ {
445
+ label: '对齐',
446
+ icon: 'alignLeft',
447
+ children: [
448
+ { label: '左对齐', onClick: () => app.alignToSlide('left') },
449
+ { label: '水平居中', onClick: () => app.alignToSlide('centerH') },
450
+ { label: '右对齐', onClick: () => app.alignToSlide('right') },
451
+ { divided: true },
452
+ { label: '顶端对齐', onClick: () => app.alignToSlide('top') },
453
+ { label: '垂直居中', onClick: () => app.alignToSlide('middle') },
454
+ { label: '底端对齐', onClick: () => app.alignToSlide('bottom') },
455
+ ],
456
+ },
457
+ {
458
+ label: '分布',
459
+ children: [
460
+ { label: '横向分布', onClick: () => app.distribute('h') },
461
+ { label: '纵向分布', onClick: () => app.distribute('v') },
462
+ ],
463
+ },
464
+ ];
465
+ const arrangeBtn = menuButton('arrange', '排列', arrangeItems, '排列');
466
+ const selectBtn = menuButton('selectAll', '选择', () => [
467
+ { label: '全选', shortcut: 'Ctrl+A', onClick: () => app.selectAll() },
468
+ { label: '取消选择', shortcut: 'Esc', onClick: () => app.clearSelection() },
469
+ ], '选择');
470
+ panel.appendChild(group('形状', shapeBtn, column(fillBtn, outlineBtn), column(effectsBtn, arrangeBtn), selectBtn));
471
+
472
+ /* 编辑 */
473
+ const findBtn = big('find', '查找', () => app.openFind(false), '查找 (Ctrl+F)');
474
+ const replaceBtn = small('replace', '替换', () => app.openFind(true), '替换 (Ctrl+H)');
475
+ const selectAllBtn = small('selectAll', '全选', () => app.selectAll(), '全选 (Ctrl+A)');
476
+ panel.appendChild(group('编辑', findBtn, column(replaceBtn, selectAllBtn)));
477
+
478
+ /* 状态刷新 */
479
+ this.reg(boldBtn, () => { boldBtn.classList.toggle('gbp-is-active', !!state().bold); boldBtn.disabled = !canRich(); });
480
+ this.reg(italicBtn, () => { italicBtn.classList.toggle('gbp-is-active', !!state().italic); italicBtn.disabled = !canRich(); });
481
+ this.reg(underlineBtn, () => { underlineBtn.classList.toggle('gbp-is-active', !!state().underline); underlineBtn.disabled = !canRich(); });
482
+ this.reg(strikeBtn, () => { strikeBtn.classList.toggle('gbp-is-active', !!state().strike); strikeBtn.disabled = !canRich(); });
483
+ this.reg(alignLeft, () => { alignLeft.classList.toggle('gbp-is-active', state().align === 'left'); alignLeft.disabled = !canAlign(); });
484
+ this.reg(alignCenter, () => { alignCenter.classList.toggle('gbp-is-active', state().align === 'center'); alignCenter.disabled = !canAlign(); });
485
+ this.reg(alignRight, () => { alignRight.classList.toggle('gbp-is-active', state().align === 'right'); alignRight.disabled = !canAlign(); });
486
+ this.reg(alignJustify, () => { alignJustify.classList.toggle('gbp-is-active', state().align === 'justify'); alignJustify.disabled = !canAlign(); });
487
+ this.reg(bulletBtn, () => { bulletBtn.classList.toggle('gbp-is-active', state().list === 'bullet'); bulletBtn.disabled = !canList(); });
488
+ this.reg(numberBtn, () => { numberBtn.classList.toggle('gbp-is-active', state().list === 'number'); numberBtn.disabled = !canList(); });
489
+ const inList = () => app.editing?.kind === 'text' && Boolean(state().list);
490
+ this.reg(outdentBtn, () => { outdentBtn.disabled = !inList(); });
491
+ this.reg(indentBtn, () => { indentBtn.disabled = !inList(); });
492
+ this.reg(fontBtn, () => {
493
+ fontBtn.disabled = !canText();
494
+ const f = FONT_FAMILIES.find((x) => x.value === state().font);
495
+ fontBtn.querySelector('.pp-rb-combo__label').textContent = f ? f.label : (state().font ? String(state().font).split(',')[0].replace(/["']/g, '') : '默认');
496
+ });
497
+ this.reg(sizeBtn, () => {
498
+ sizeBtn.disabled = !canText();
499
+ sizeBtn.querySelector('.pp-rb-combo__label').textContent = state().fontSize ? String(Math.round(state().fontSize)) : '字号';
500
+ });
501
+ this.reg(incBtn, () => { incBtn.disabled = !canText(); });
502
+ this.reg(decBtn, () => { decBtn.disabled = !canText(); });
503
+ this.reg(clearBtn, () => {
504
+ clearBtn.disabled = !app.editing && !app.selectedObjects().some((o) => ['text', 'shape', 'table'].includes(o.type));
505
+ });
506
+ this.reg(cutBtn, () => { cutBtn.disabled = !hasSel() && !app.editing; });
507
+ this.reg(copyBtn, () => { copyBtn.disabled = !hasSel() && !app.editing; });
508
+ this.reg(fontColor, () => {
509
+ fontColor.disabled = !canText();
510
+ fontColor.colorBar.style.background = state().color || themeOf(app.snapshot.theme).text;
511
+ });
512
+ this.reg(lineBtn, () => {
513
+ lineBtn.disabled = !canRich();
514
+ const v = state().lineSpacing || 1;
515
+ lineBtn.querySelector('.pp-rb-combo__label').textContent = Math.abs(v - 1) < 0.01 ? '1.0' : String(v);
516
+ });
517
+ this.reg(fillBtn, () => {
518
+ const o = app.firstSelected();
519
+ fillBtn.disabled = !o || o.type === 'line';
520
+ fillBtn.colorBar.style.background = o?.props?.fill || 'transparent';
521
+ fillBtn.classList.toggle('gbp-is-off', !o?.props?.fill);
522
+ });
523
+ this.reg(outlineBtn, () => {
524
+ const o = app.firstSelected();
525
+ outlineBtn.disabled = !o;
526
+ outlineBtn.colorBar.style.background = o?.props?.stroke || 'transparent';
527
+ outlineBtn.classList.toggle('gbp-is-off', !o?.props?.stroke || !o?.props?.strokeW);
528
+ });
529
+ this.reg(effectsBtn, () => {
530
+ const o = app.firstSelected();
531
+ effectsBtn.disabled = !o || o.type === 'line';
532
+ effectsBtn.classList.toggle('gbp-is-active', !!o?.props?.shadow);
533
+ });
534
+ this.reg(arrangeBtn, () => { arrangeBtn.disabled = !hasSel(); });
535
+ this.reg(selectBtn, () => { selectBtn.disabled = !app.snapshot.slides.length; });
536
+ this.reg(pasteMain, () => { pasteMain.disabled = !app.canPaste(); });
537
+ this.reg(hideBtn, () => { hideBtn.disabled = false; hideBtn.classList.toggle('gbp-is-active', !!app.currentSlide()?.hidden); });
538
+ this.reg(resetBtn, () => { resetBtn.disabled = !hasSel(); });
539
+ }
540
+
541
+ layoutMenu(anchor) {
542
+ openMenu({
543
+ anchor,
544
+ items: SLIDE_LAYOUTS.map((l) => ({ label: l.label, onClick: () => this.app.addSlide(l.key) })),
545
+ });
546
+ }
547
+
548
+ /* ===================== 插入 ===================== */
549
+
550
+ buildInsert(panel) {
551
+ const app = this.app;
552
+ const newSlide = splitBig('newSlide', '新建幻灯片', () => app.addSlide(), (e) => this.layoutMenu(e.currentTarget));
553
+ panel.appendChild(group('幻灯片', newSlide));
554
+
555
+ const tableBtn = big('table', '表格', (e) => openTableGrid(e.currentTarget, (r, c) => app.insertTable(r, c)), '插入表格');
556
+ panel.appendChild(group('表格', tableBtn));
557
+
558
+ const pic = big('image', '图片', () => app.insert('image'), '插入图片');
559
+ const shape = big('shapes', '形状', (e) => openShapeGallery(e.currentTarget, (kind) => app.insert('shape', kind)), '插入形状');
560
+ const text = big('textBox', '文本框', () => app.insert('text'), '插入文本框');
561
+ const line = small('line', '线条', () => app.insert('line'), '插入线条');
562
+ const arrow = small('arrow', '箭头', () => app.insert('arrow'), '插入箭头');
563
+ panel.appendChild(group('插图', pic, shape, text, column(line, arrow)));
564
+
565
+ const notes = small('notes', '备注', () => app.toggleNotes(), '显示/隐藏备注');
566
+ panel.appendChild(group('显示', notes));
567
+ }
568
+
569
+ /* ===================== 设计 ===================== */
570
+
571
+ buildDesign(panel) {
572
+ const app = this.app;
573
+ const gallery = el('div', 'pp-theme-gallery');
574
+ const cards = new Map();
575
+ for (const key of THEME_KEYS) {
576
+ const t = themeOf(key);
577
+ const card = button('pp-theme-card', '', t.label);
578
+ card.style.setProperty('--gbp-t-bg', t.bg);
579
+ card.style.setProperty('--gbp-t-text', t.text);
580
+ card.style.setProperty('--gbp-t-accent', t.accent);
581
+ card.innerHTML = `<span class="pp-theme-card__preview"><i class="pp-theme-card__title"></i><i class="pp-theme-card__line"></i><i class="pp-theme-card__block"></i></span><span class="pp-theme-card__label">${t.label}</span>`;
582
+ card.addEventListener('click', () => app.setTheme(key));
583
+ cards.set(key, card);
584
+ gallery.appendChild(card);
585
+ }
586
+ panel.appendChild(group('主题', gallery));
587
+
588
+ const bgBtn = big('background', '设置背景格式', () => app.openFormatPane(), '设置背景格式');
589
+ const bgColor = colorButton('fill', '背景色', '页面背景色', () => app.currentSlide()?.bg || themeOf(app.snapshot.theme).bg, (c) => app.setSlideBg(c));
590
+ const bgReset = small('reset', '重置背景', () => app.setSlideBg(''), '跟随主题背景');
591
+ panel.appendChild(group('背景', bgBtn, column(bgColor, bgReset)));
592
+
593
+ const wide = small('size', '宽屏', () => app.setSlideSize('wide'), '16:9');
594
+ const standard = small('size', '标准', () => app.setSlideSize('standard'), '4:3');
595
+ panel.appendChild(group('幻灯片大小', column(wide, standard)));
596
+
597
+ this.reg(gallery, () => {
598
+ for (const [key, card] of cards) card.classList.toggle('gbp-is-active', app.snapshot.theme === key);
599
+ });
600
+ this.reg(bgColor, () => {
601
+ const bg = app.currentSlide()?.bg;
602
+ bgColor.colorBar.style.background = bg || themeOf(app.snapshot.theme).bg;
603
+ bgColor.classList.toggle('gbp-is-off', !bg);
604
+ });
605
+ }
606
+
607
+ /* ===================== 切换 ===================== */
608
+
609
+ buildTransitions(panel) {
610
+ const app = this.app;
611
+ const gallery = el('div', 'pp-transition-gallery');
612
+ const cards = new Map();
613
+ const iconOf = { none: 'close', fade: 'effects', push: 'arrow', wipe: 'outline', cover: 'bringForward', split: 'viewSorter', zoom: 'zoomIn' };
614
+ for (const tr of TRANSITIONS) {
615
+ const card = button('pp-transition-card', `<span class="pp-transition-card__ico">${icon(iconOf[tr.key] || 'transition', 20)}</span><span>${tr.label}</span>`, tr.label);
616
+ card.addEventListener('click', () => {
617
+ app.setTransition({ type: tr.key, dir: tr.dir || 'l' });
618
+ app.previewTransition();
619
+ });
620
+ cards.set(tr.key, card);
621
+ gallery.appendChild(card);
622
+ }
623
+ panel.appendChild(group('切换到此幻灯片', gallery));
624
+
625
+ const duration = combo(82, () => openMenu({
626
+ anchor: duration,
627
+ items: [
628
+ { label: '0.5 秒', checked: app.currentSlide().transition?.speed === 'fast', onClick: () => app.setTransition({ speed: 'fast' }) },
629
+ { label: '1 秒', checked: !app.currentSlide().transition?.speed || app.currentSlide().transition?.speed === 'med', onClick: () => app.setTransition({ speed: 'med' }) },
630
+ { label: '2 秒', checked: app.currentSlide().transition?.speed === 'slow', onClick: () => app.setTransition({ speed: 'slow' }) },
631
+ ],
632
+ }), '持续时间');
633
+ const applyAll = small('selectAll', '应用到全部', () => app.applyTransitionAll(), '应用到全部幻灯片');
634
+ const preview = big('play', '预览', () => app.previewTransition(), '预览切换');
635
+ panel.appendChild(group('计时', duration, column(applyAll)));
636
+ panel.appendChild(group('预览', preview));
637
+
638
+ this.reg(gallery, () => {
639
+ const type = app.currentSlide()?.transition?.type || 'none';
640
+ for (const [key, card] of cards) card.classList.toggle('gbp-is-active', key === type);
641
+ });
642
+ this.reg(duration, () => {
643
+ const speed = app.currentSlide()?.transition?.speed || 'med';
644
+ duration.querySelector('.pp-rb-combo__label').textContent = speed === 'fast' ? '0.5 秒' : speed === 'slow' ? '2 秒' : '1 秒';
645
+ });
646
+ }
647
+
648
+ /* ===================== 幻灯片放映 ===================== */
649
+
650
+ buildSlideshow(panel) {
651
+ const app = this.app;
652
+ const fromStart = big('play', '从头开始', () => app.presentFromStart(), '从头开始放映 (F5)');
653
+ const fromCurrent = big('present', '从当前页', () => app.presentFromCurrent(), '从当前幻灯片开始 (Shift+F5)');
654
+ const presenter = small('presenter', '演讲者视图', () => app.togglePresenter(), '使用演讲者视图');
655
+ const loop = small('loop', '循环放映', () => app.toggleLoop(), '按 Esc 前循环放映');
656
+ const hide = small('hide', '隐藏幻灯片', () => app.toggleSlideHidden(), '隐藏当前幻灯片');
657
+ panel.appendChild(group('开始放映幻灯片', fromStart, fromCurrent, column(presenter, hide)));
658
+ panel.appendChild(group('设置', column(loop)));
659
+
660
+ this.reg(presenter, () => presenter.classList.toggle('gbp-is-active', !!app.presentOptions.presenter));
661
+ this.reg(loop, () => loop.classList.toggle('gbp-is-active', !!app.presentOptions.loop));
662
+ this.reg(hide, () => hide.classList.toggle('gbp-is-active', !!app.currentSlide()?.hidden));
663
+ }
664
+
665
+ /* ===================== 审阅 ===================== */
666
+
667
+ buildReview(panel) {
668
+ const app = this.app;
669
+ const notes = big('notes', '显示备注', () => app.toggleNotes(), '显示/隐藏备注窗格');
670
+ const count = big('wordCount', '字数统计', () => app.showWordCount(), '统计幻灯片与文字数量');
671
+ panel.appendChild(group('备注', notes));
672
+ panel.appendChild(group('校对', count));
673
+ this.reg(notes, () => notes.classList.toggle('gbp-is-active', !!app.notesVisible));
674
+ }
675
+
676
+ /* ===================== 视图 ===================== */
677
+
678
+ buildView(panel) {
679
+ const app = this.app;
680
+ const normal = big('viewNormal', '普通', () => app.setView('normal'), '普通视图');
681
+ const sorter = big('viewSorter', '幻灯片浏览', () => app.setView('sorter'), '幻灯片浏览视图');
682
+ const reading = big('viewReading', '阅读视图', () => app.setView('reading'), '阅读视图');
683
+ const grid = small('grid', '网格线', () => app.toggleGrid(), '显示网格线');
684
+ const guides = small('guides', '参考线', () => app.toggleGuides(), '显示参考线');
685
+ const notes = small('notes', '备注', () => app.toggleNotes(), '显示备注');
686
+ const pane = small('formatPane', '格式窗格', () => app.toggleFormatPane(), '设置形状格式窗格');
687
+ const fit = big('fitWindow', '适应窗口', () => app.fitToWindow(), '缩放到适合窗口');
688
+ const zoom = combo(76, () => openMenu({
689
+ anchor: zoom,
690
+ items: [50, 75, 100, 125, 150, 200, 400].map((z) => ({ label: `${z}%`, checked: app.zoom === z, onClick: () => app.setZoom(z) })),
691
+ }), '缩放');
692
+ panel.appendChild(group('演示文稿视图', normal, sorter, reading));
693
+ panel.appendChild(group('显示', column(grid, guides), column(notes, pane)));
694
+ panel.appendChild(group('缩放', fit, zoom));
695
+
696
+ this.reg(normal, () => normal.classList.toggle('gbp-is-active', app.view === 'normal'));
697
+ this.reg(sorter, () => sorter.classList.toggle('gbp-is-active', app.view === 'sorter'));
698
+ this.reg(reading, () => reading.classList.toggle('gbp-is-active', app.view === 'reading'));
699
+ this.reg(grid, () => grid.classList.toggle('gbp-is-active', !!app.gridVisible));
700
+ this.reg(guides, () => guides.classList.toggle('gbp-is-active', !!app.guidesVisible));
701
+ this.reg(notes, () => notes.classList.toggle('gbp-is-active', !!app.notesVisible));
702
+ this.reg(pane, () => pane.classList.toggle('gbp-is-active', !!app.formatPaneOpen));
703
+ this.reg(zoom, () => { zoom.querySelector('.pp-rb-combo__label').textContent = `${app.zoom}%`; });
704
+ }
705
+
706
+ /* ===================== 上下文:形状 ===================== */
707
+
708
+ buildContextShape(panel) {
709
+ const app = this.app;
710
+ const shapeBtn = big('shapes', '更改形状', (e) => openShapeGallery(e.currentTarget, (kind) => app.setSelectedProp('kind', kind)), '更改形状');
711
+ const gallery = el('div', 'pp-style-gallery');
712
+ const cards = new Map();
713
+ const render = () => {
714
+ gallery.replaceChildren();
715
+ for (const s of shapeQuickStyles(app.snapshot.theme)) {
716
+ const card = button('pp-style-card', `<span style="background:${s.fill || 'transparent'};border-color:${s.stroke || 'transparent'};color:${s.text}">Aa</span><small>${s.label}</small>`, s.label);
717
+ card.addEventListener('click', () => app.applyShapeQuickStyle(s));
718
+ cards.set(s.key, card);
719
+ gallery.appendChild(card);
720
+ }
721
+ };
722
+ render();
723
+ panel.appendChild(group('插入形状', shapeBtn));
724
+ panel.appendChild(group('形状样式', gallery));
725
+
726
+ const fillBtn = colorButton('fill', '填充', '形状填充', () => app.firstSelected()?.props?.fill || '', (c) => app.setSelectedProp('fill', c));
727
+ const outlineColor = colorButton('outline', '轮廓', '形状轮廓', () => app.firstSelected()?.props?.stroke || '', (c) => app.setSelectedProp('stroke', c));
728
+ const outlineWidth = menuButton('outline', '粗细', () => [1, 2, 3, 4, 6].map((w) => ({
729
+ label: `${w} px`, checked: Math.round(app.firstSelected()?.props?.strokeW) === w, onClick: () => app.setSelectedProp('strokeW', w),
730
+ })), '轮廓粗细');
731
+ const dash = menuButton('line', '虚线', () => [
732
+ { label: '实线', checked: (app.firstSelected()?.props?.dash || 'solid') === 'solid', onClick: () => app.setSelectedProp('dash', 'solid') },
733
+ { label: '虚线', checked: app.firstSelected()?.props?.dash === 'dash', onClick: () => app.setSelectedProp('dash', 'dash') },
734
+ { label: '点线', checked: app.firstSelected()?.props?.dash === 'dot', onClick: () => app.setSelectedProp('dash', 'dot') },
735
+ { label: '点划线', checked: app.firstSelected()?.props?.dash === 'dashDot', onClick: () => app.setSelectedProp('dash', 'dashDot') },
736
+ ], '轮廓样式');
737
+ const shadow = small('shadow', '阴影', () => app.setSelectedProp('shadow', !app.firstSelected()?.props?.shadow), '阴影');
738
+ const noOutline = small('close', '无轮廓', () => app.setSelectedProp('strokeW', 0), '无轮廓');
739
+ panel.appendChild(group('形状填充与轮廓', column(fillBtn, noOutline), column(outlineColor, outlineWidth), column(dash, shadow)));
740
+
741
+ const rotateLeft = small('rotate', '左转 90°', () => app.rotateSelected(-90), '向左旋转 90°');
742
+ const rotateRight = small('rotate', '右转 90°', () => app.rotateSelected(90), '向右旋转 90°');
743
+ const arrange = menuButton('arrange', '排列', () => [
744
+ { label: '置于顶层', onClick: () => app.arrange('front') },
745
+ { label: '上移一层', onClick: () => app.arrange('forward') },
746
+ { label: '下移一层', onClick: () => app.arrange('backward') },
747
+ { label: '置于底层', onClick: () => app.arrange('back') },
748
+ ], '排列');
749
+ panel.appendChild(group('排列', column(rotateLeft, rotateRight), arrange));
750
+
751
+ this.buildSizeGroup(panel);
752
+
753
+ this.reg(gallery, () => {
754
+ // 快速样式不强制高亮(组合样式难以反推),仅保持可点
755
+ });
756
+ this.reg(fillBtn, () => { fillBtn.colorBar.style.background = app.firstSelected()?.props?.fill || 'transparent'; });
757
+ this.reg(outlineColor, () => { outlineColor.colorBar.style.background = app.firstSelected()?.props?.stroke || 'transparent'; });
758
+ this.reg(shadow, () => { shadow.classList.toggle('gbp-is-active', !!app.firstSelected()?.props?.shadow); });
759
+ this.reg(noOutline, () => { noOutline.disabled = !app.firstSelected(); });
760
+ }
761
+
762
+ /* ===================== 上下文:图片 ===================== */
763
+
764
+ buildContextImage(panel) {
765
+ const app = this.app;
766
+ const replace = big('replaceImage', '替换图片', () => app.replaceSelectedImage(), '替换图片');
767
+ const reset = big('resetImage', '重置图片', () => app.resetSelectedImage(), '重置裁剪与效果');
768
+ const crop = small('crop', '裁剪', () => app.cropSelected(), '裁剪图片');
769
+ panel.appendChild(group('调整', replace, reset, column(crop)));
770
+
771
+ const shadow = small('shadow', '阴影', () => app.setSelectedProp('shadow', !app.firstSelected()?.props?.shadow), '阴影');
772
+ const alt = small('textBox', '替代文字', () => app.editAltText(), '编辑替代文字(无障碍)');
773
+ panel.appendChild(group('图片样式', column(shadow, alt)));
774
+
775
+ const arrange = menuButton('arrange', '排列', () => [
776
+ { label: '置于顶层', onClick: () => app.arrange('front') },
777
+ { label: '上移一层', onClick: () => app.arrange('forward') },
778
+ { label: '下移一层', onClick: () => app.arrange('backward') },
779
+ { label: '置于底层', onClick: () => app.arrange('back') },
780
+ ], '排列');
781
+ panel.appendChild(group('排列', arrange));
782
+ this.buildSizeGroup(panel);
783
+ this.reg(shadow, () => { shadow.classList.toggle('gbp-is-active', !!app.firstSelected()?.props?.shadow); });
784
+ }
785
+
786
+ /* ===================== 上下文:表格 ===================== */
787
+
788
+ buildContextTable(panel) {
789
+ const app = this.app;
790
+ const gallery = el('div', 'pp-table-style-gallery');
791
+ const cards = new Map();
792
+ for (const s of TABLE_STYLES) {
793
+ const card = button('pp-table-style-card', `<span class="pp-table-style-card__preview pp-table-style-card__preview--${s.key}"><i></i><i></i><i></i><i></i></span><span>${s.label}</span>`, s.label);
794
+ card.addEventListener('click', () => app.setTableStyle(s.key));
795
+ cards.set(s.key, card);
796
+ gallery.appendChild(card);
797
+ }
798
+ panel.appendChild(group('表格样式', gallery));
799
+
800
+ const header = small('tableStyle', '标题行', () => app.toggleHeaderRow(), '标题行');
801
+ const banded = small('tableStyle', '镶边行', () => app.toggleBandedRows(), '镶边行');
802
+ panel.appendChild(group('样式选项', column(header, banded)));
803
+
804
+ const rowAbove = small('rowAbove', '上方', () => app.insertTableRow('above'), '在上方插入行');
805
+ const rowBelow = small('rowBelow', '下方', () => app.insertTableRow('below'), '在下方插入行');
806
+ const colLeft = small('colLeft', '左侧', () => app.insertTableColumn('left'), '在左侧插入列');
807
+ const colRight = small('colRight', '右侧', () => app.insertTableColumn('right'), '在右侧插入列');
808
+ const delRow = small('delete', '删除行', () => app.deleteTableRow(), '删除行');
809
+ const delCol = small('delete', '删除列', () => app.deleteTableColumn(), '删除列');
810
+ panel.appendChild(group('行和列', column(rowAbove, rowBelow), column(colLeft, colRight), column(delRow, delCol)));
811
+
812
+ const arrange = menuButton('arrange', '排列', () => [
813
+ { label: '置于顶层', onClick: () => app.arrange('front') },
814
+ { label: '置于底层', onClick: () => app.arrange('back') },
815
+ ], '排列');
816
+ panel.appendChild(group('排列', arrange));
817
+ this.buildSizeGroup(panel);
818
+
819
+ this.reg(gallery, () => {
820
+ const o = app.firstSelected();
821
+ for (const [key, card] of cards) card.classList.toggle('gbp-is-active', o?.props?.tableStyle === key);
822
+ });
823
+ this.reg(header, () => { header.classList.toggle('gbp-is-active', !!app.firstSelected()?.props?.headerRow); });
824
+ this.reg(banded, () => { banded.classList.toggle('gbp-is-active', !!app.firstSelected()?.props?.bandedRows); });
825
+ }
826
+
827
+ /** 大小与位置组(单选对象) */
828
+ buildSizeGroup(panel) {
829
+ const app = this.app;
830
+ const wInput = el('input', 'pp-rb-num');
831
+ wInput.type = 'number';
832
+ wInput.step = '1';
833
+ wInput.min = '1';
834
+ wInput.title = '宽度';
835
+ const hInput = el('input', 'pp-rb-num');
836
+ hInput.type = 'number';
837
+ hInput.step = '1';
838
+ hInput.min = '1';
839
+ hInput.title = '高度';
840
+ wInput.addEventListener('mousedown', (e) => e.stopPropagation());
841
+ hInput.addEventListener('mousedown', (e) => e.stopPropagation());
842
+ const apply = (key, input) => {
843
+ const o = app.firstSelected();
844
+ const v = Number(input.value);
845
+ if (!o || !Number.isFinite(v) || v <= 0) return;
846
+ app.updateObject(o.id, { [key]: key === 'w' || key === 'h' ? Math.round(v * (o[key] < 0 ? -1 : 1)) : Math.round(v) });
847
+ };
848
+ wInput.addEventListener('change', () => apply('w', wInput));
849
+ hInput.addEventListener('change', () => apply('h', hInput));
850
+ const wWrap = el('label', 'pp-rb-field');
851
+ wWrap.append(el('span', '', '宽'), wInput);
852
+ const hWrap = el('label', 'pp-rb-field');
853
+ hWrap.append(el('span', '', '高'), hInput);
854
+ panel.appendChild(group('大小', column(wWrap, hWrap)));
855
+ this.reg(wInput, () => {
856
+ if (document.activeElement === wInput) return;
857
+ const o = app.firstSelected();
858
+ wInput.value = o ? String(Math.round(Math.abs(o.w))) : '';
859
+ wInput.disabled = !o;
860
+ });
861
+ this.reg(hInput, () => {
862
+ if (document.activeElement === hInput) return;
863
+ const o = app.firstSelected();
864
+ hInput.value = o ? String(Math.round(Math.abs(o.h))) : '';
865
+ hInput.disabled = !o;
866
+ });
867
+ }
868
+ }