gbpptx 0.6.1 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +253 -253
- package/package.json +48 -47
- package/src/index.js +29 -29
- package/src/kernel/model.js +651 -651
- package/src/kernel/parts.js +173 -173
- package/src/kernel/pptx.js +72 -72
- package/src/kernel/print.js +76 -76
- package/src/kernel/read.js +1018 -1018
- package/src/kernel/roundtrip.js +174 -174
- package/src/kernel/text.js +278 -278
- package/src/kernel/write.js +385 -385
- package/web/app/main.js +8 -8
- package/web/app/slides/arrange.js +101 -101
- package/web/app/slides/clipboard.js +93 -93
- package/web/app/slides/file.js +240 -240
- package/web/app/slides/find.js +201 -201
- package/web/app/slides/history.js +100 -100
- package/web/app/slides/keys.js +187 -187
- package/web/app/slides/menus.js +105 -105
- package/web/app/slides/objects.js +254 -254
- package/web/app/slides/render.js +183 -183
- package/web/app/slides/shared.js +85 -85
- package/web/app/slides/slides.js +103 -103
- package/web/app/slides/table.js +102 -102
- package/web/app/slides/text.js +418 -418
- package/web/app/slides/view.js +182 -182
- package/web/app/slides-app.js +354 -354
- package/web/app/ui/canvas.js +769 -769
- package/web/app/ui/color-panel.js +58 -58
- package/web/app/ui/dialogs.js +120 -120
- package/web/app/ui/file-io.js +116 -116
- package/web/app/ui/format-pane.js +427 -427
- package/web/app/ui/icons.js +241 -133
- package/web/app/ui/menu.js +77 -75
- package/web/app/ui/notify.js +45 -45
- package/web/app/ui/page-view.js +221 -221
- package/web/app/ui/popup.js +80 -80
- package/web/app/ui/present.js +292 -292
- package/web/app/ui/ribbon.js +855 -855
- package/web/app/ui/slide-pane.js +197 -197
- package/web/app/ui/statusbar.js +93 -93
- package/web/app/ui/text-styles.js +84 -84
- package/web/app/ui/titlebar.js +253 -253
- package/web/app/ui/transition.js +30 -30
- package/web/index.html +30 -29
- package/web/serve.mjs +26 -23
- package/web/styles/office.css +463 -463
- package/web/styles/slides.css +97 -97
- package/web/styles/tokens.css +154 -154
package/web/app/slides-app.js
CHANGED
|
@@ -1,354 +1,354 @@
|
|
|
1
|
-
// [文件] gbpptx·SlidesApp —— Office 式演示编辑器装配(Ribbon/窗格/画布/放映/文件读写/查找替换)
|
|
2
|
-
//
|
|
3
|
-
// [设计] 本文件只留「装配 + 渲染 + 生命周期 + 自检」;其余职责按段落拆到 ./slides/*:
|
|
4
|
-
// render(选择与渲染)/ history(脏状态与撤销)/ text(文本编辑与格式)/ objects(属性与插入)/
|
|
5
|
-
// clipboard / arrange / table / slides(页操作)/ view(视图与放映)/ file / find / menus / keys。
|
|
6
|
-
// 各模块导出方法组,由文件末尾 Object.assign 装配到 SlidesApp.prototype —— 同一个 this、同一套调用,
|
|
7
|
-
// 外部拿到的 SlidesApp 形状与拆分前完全一致。
|
|
8
|
-
// [坑] 装配必须在实例化之前完成:Object.assign 在模块求值时执行,而实例只能在 import 之后 new。
|
|
9
|
-
|
|
10
|
-
import { clamp, createReadOnlyGuard } from 'guabiutils';
|
|
11
|
-
import { createDraft, sizeOf } from '../../src/kernel/model.js';
|
|
12
|
-
import { buildPptxVerified } from '../../src/kernel/roundtrip.js';
|
|
13
|
-
import { parsePptx } from '../../src/kernel/read.js';
|
|
14
|
-
import { notify } from './ui/notify.js';
|
|
15
|
-
import { TitleBar } from './ui/titlebar.js';
|
|
16
|
-
import { Ribbon } from './ui/ribbon.js';
|
|
17
|
-
import { SlidePane } from './ui/slide-pane.js';
|
|
18
|
-
import { Canvas } from './ui/canvas.js';
|
|
19
|
-
import { FormatPane } from './ui/format-pane.js';
|
|
20
|
-
import { StatusBar } from './ui/statusbar.js';
|
|
21
|
-
import { Present } from './ui/present.js';
|
|
22
|
-
import { icon } from './ui/icons.js';
|
|
23
|
-
import { el, loadPrefs, savePrefs, ZOOM_MIN, ZOOM_MAX } from './slides/shared.js';
|
|
24
|
-
import { renderMethods } from './slides/render.js';
|
|
25
|
-
import { historyMethods } from './slides/history.js';
|
|
26
|
-
import { textMethods } from './slides/text.js';
|
|
27
|
-
import { objectsMethods } from './slides/objects.js';
|
|
28
|
-
import { clipboardMethods } from './slides/clipboard.js';
|
|
29
|
-
import { arrangeMethods } from './slides/arrange.js';
|
|
30
|
-
import { tableMethods } from './slides/table.js';
|
|
31
|
-
import { slidesMethods } from './slides/slides.js';
|
|
32
|
-
import { viewMethods } from './slides/view.js';
|
|
33
|
-
import { fileMethods } from './slides/file.js';
|
|
34
|
-
import { findMethods } from './slides/find.js';
|
|
35
|
-
import { menusMethods } from './slides/menus.js';
|
|
36
|
-
import { keysMethods } from './slides/keys.js';
|
|
37
|
-
|
|
38
|
-
export class SlidesApp {
|
|
39
|
-
/**
|
|
40
|
-
* @param {object} [options] readOnly=只读预览(禁写且锁编辑控件);hideMenuBar=隐藏顶部菜单栏那一行;
|
|
41
|
-
* host=嵌入宿主的文件能力(getTitle 文档名 / rename 重命名 / download 下载),缺省走包内实现
|
|
42
|
-
*/
|
|
43
|
-
constructor(mount, options = {}) {
|
|
44
|
-
this.mount = mount;
|
|
45
|
-
this.readOnly = Boolean(options.readOnly);
|
|
46
|
-
this.menuBarVisible = !options.hideMenuBar;
|
|
47
|
-
this.host = options.host || null;
|
|
48
|
-
const prefs = loadPrefs();
|
|
49
|
-
this.prefs = prefs;
|
|
50
|
-
this.snapshot = createDraft();
|
|
51
|
-
this.index = 0;
|
|
52
|
-
this.selectedIds = [];
|
|
53
|
-
this.zoom = clamp(Number(prefs.zoom) || 65, ZOOM_MIN, ZOOM_MAX);
|
|
54
|
-
this.dirty = false;
|
|
55
|
-
this.saving = false;
|
|
56
|
-
this.revision = 0;
|
|
57
|
-
this.history = [];
|
|
58
|
-
this.future = [];
|
|
59
|
-
this.clipboard = null;
|
|
60
|
-
this.editing = null;
|
|
61
|
-
this.tableCursor = null;
|
|
62
|
-
this.notesSession = false;
|
|
63
|
-
this.autosave = options.autoSave !== undefined ? !!options.autoSave : !!prefs.autosave;
|
|
64
|
-
this.notesVisible = prefs.notesVisible !== false;
|
|
65
|
-
this.gridVisible = !!prefs.grid;
|
|
66
|
-
this.guidesVisible = prefs.guides === true;
|
|
67
|
-
this.ribbonCollapsed = !!prefs.ribbonCollapsed;
|
|
68
|
-
this.formatPaneOpen = !!prefs.formatPaneOpen;
|
|
69
|
-
this.view = 'normal';
|
|
70
|
-
this.preview = false;
|
|
71
|
-
this.previewRestore = null;
|
|
72
|
-
this.pageFullscreen = false;
|
|
73
|
-
this.presentOptions = { presenter: !!prefs.presenter, loop: !!prefs.loop };
|
|
74
|
-
this.lastBytes = null;
|
|
75
|
-
|
|
76
|
-
const seq = Number(prefs.docSeq) || 1;
|
|
77
|
-
prefs.docSeq = seq + 1;
|
|
78
|
-
savePrefs(prefs);
|
|
79
|
-
this.doc = { name: `演示文稿${seq}`, handle: null, savedAt: 0 };
|
|
80
|
-
|
|
81
|
-
this.build();
|
|
82
|
-
this.bindGlobal();
|
|
83
|
-
this.presenter = new Present(this);
|
|
84
|
-
this.applyMode();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/* ===================== 骨架 ===================== */
|
|
88
|
-
|
|
89
|
-
get size() {
|
|
90
|
-
return sizeOf(this.snapshot);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/** 文档标题:宿主给了就用宿主的(服务端文档名),否则用本地文档名 */
|
|
94
|
-
getTitle() {
|
|
95
|
-
return this.host?.getTitle?.() || this.doc.name || '新建演示';
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/** 文档头「重命名」是否可用:宿主提供才渲染,独立模式走包内实现 */
|
|
99
|
-
canRename() {
|
|
100
|
-
return this.host ? typeof this.host.rename === 'function' : true;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/** 文档头「下载」是否可用:宿主提供才渲染,独立模式走包内实现 */
|
|
104
|
-
canDownload() {
|
|
105
|
-
return this.host ? typeof this.host.download === 'function' : true;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** 宿主改了文档名之后刷新文档头(标题/状态) */
|
|
109
|
-
refreshHead() {
|
|
110
|
-
this.titlebar?.refresh();
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
build() {
|
|
114
|
-
const root = el('div', 'pp-app');
|
|
115
|
-
root.innerHTML = `
|
|
116
|
-
<div class="pp-head-host"></div>
|
|
117
|
-
<div class="pp-topbar-host"></div>
|
|
118
|
-
<div class="pp-ribbon-host"></div>
|
|
119
|
-
<div class="pp-main">
|
|
120
|
-
<div class="pp-main__left"></div>
|
|
121
|
-
<div class="pp-center">
|
|
122
|
-
<div class="pp-canvas-host"></div>
|
|
123
|
-
<div class="pp-notes" hidden>
|
|
124
|
-
<div class="pp-notes__head">
|
|
125
|
-
<span class="pp-notes__title">备注</span>
|
|
126
|
-
<button type="button" class="pp-iconbtn" data-act="notes-close" title="关闭备注">${icon('close', 14)}</button>
|
|
127
|
-
</div>
|
|
128
|
-
<textarea class="pp-notes__text" placeholder="单击此处添加演讲者备注"></textarea>
|
|
129
|
-
</div>
|
|
130
|
-
</div>
|
|
131
|
-
<div class="pp-main__right"></div>
|
|
132
|
-
</div>
|
|
133
|
-
<div class="pp-statusbar-host"></div>`;
|
|
134
|
-
this.mount.appendChild(root);
|
|
135
|
-
this.root = root;
|
|
136
|
-
this.topbarHost = root.querySelector('.pp-topbar-host');
|
|
137
|
-
this.headHost = root.querySelector('.pp-head-host');
|
|
138
|
-
this.ribbonHost = root.querySelector('.pp-ribbon-host');
|
|
139
|
-
this.formatPaneHost = root.querySelector('.pp-main__right');
|
|
140
|
-
this.left = root.querySelector('.pp-main__left');
|
|
141
|
-
this.center = root.querySelector('.pp-center');
|
|
142
|
-
this.right = root.querySelector('.pp-main__right');
|
|
143
|
-
this.canvasHost = root.querySelector('.pp-canvas-host');
|
|
144
|
-
this.notesEl = root.querySelector('.pp-notes');
|
|
145
|
-
this.notesText = root.querySelector('.pp-notes__text');
|
|
146
|
-
|
|
147
|
-
this.titlebar = new TitleBar(this);
|
|
148
|
-
this.ribbon = new Ribbon(this);
|
|
149
|
-
this.slidePane = new SlidePane(this);
|
|
150
|
-
this.canvas = new Canvas(this);
|
|
151
|
-
this.formatPane = new FormatPane(this);
|
|
152
|
-
this.statusbar = new StatusBar(this);
|
|
153
|
-
|
|
154
|
-
root.querySelector('.pp-topbar-host').appendChild(this.titlebar.el);
|
|
155
|
-
root.querySelector('.pp-head-host').appendChild(this.titlebar.headEl);
|
|
156
|
-
root.querySelector('.pp-ribbon-host').appendChild(this.ribbon.el);
|
|
157
|
-
this.left.appendChild(this.slidePane.el);
|
|
158
|
-
this.canvasHost.appendChild(this.canvas.el);
|
|
159
|
-
this.right.appendChild(this.formatPane.el);
|
|
160
|
-
root.querySelector('.pp-statusbar-host').appendChild(this.statusbar.el);
|
|
161
|
-
|
|
162
|
-
this.sorterEl = el('div', 'pp-sorter');
|
|
163
|
-
this.sorterEl.hidden = true;
|
|
164
|
-
this.center.appendChild(this.sorterEl);
|
|
165
|
-
this.readingEl = el('div', 'pp-reading');
|
|
166
|
-
this.readingEl.hidden = true;
|
|
167
|
-
this.center.appendChild(this.readingEl);
|
|
168
|
-
this.buildSorterEvents();
|
|
169
|
-
|
|
170
|
-
this.bindNotes(root.querySelector('[data-act="notes-close"]'));
|
|
171
|
-
// 只 inert 功能区与各选项卡本身:文档头的预览按钮在 inert 之外,预览档里仍可点「退出预览」
|
|
172
|
-
this.guard = createReadOnlyGuard(root, {
|
|
173
|
-
editZones: () => [
|
|
174
|
-
this.ribbon.bodyEl,
|
|
175
|
-
...this.ribbon.tabsEl.querySelectorAll('.pp-rb-tab'),
|
|
176
|
-
this.ribbon.contextHost,
|
|
177
|
-
this.formatPaneHost,
|
|
178
|
-
this.notesEl,
|
|
179
|
-
],
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/** 构造参数落 DOM:装配完成后才调用,此时标题栏、Ribbon、格式窗格都已就位 */
|
|
184
|
-
applyMode() {
|
|
185
|
-
this.setMenuBarVisible(this.menuBarVisible);
|
|
186
|
-
if (this.readOnly) this.setReadOnly(true);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/** 显示/隐藏顶部菜单栏那一行;文档头(标题/自动保存/保存/预览)不受影响 */
|
|
190
|
-
setMenuBarVisible(on) {
|
|
191
|
-
this.menuBarVisible = !!on;
|
|
192
|
-
this.topbarHost.style.display = this.menuBarVisible ? '' : 'none';
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/** 只读开关:DOM 守卫 + 编辑面板 + 菜单栏/备注可用性同一处切换 */
|
|
196
|
-
setReadOnly(on) {
|
|
197
|
-
this.readOnly = !!on;
|
|
198
|
-
this.guard.setReadOnly(this.readOnly);
|
|
199
|
-
this.notesText.disabled = this.readOnly;
|
|
200
|
-
this.renderAll();
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
bindNotes(closeBtn) {
|
|
204
|
-
this.notesText.addEventListener('focus', () => { this.notesSession = false; });
|
|
205
|
-
this.notesText.addEventListener('input', () => {
|
|
206
|
-
const slide = this.currentSlide();
|
|
207
|
-
if (!slide) return;
|
|
208
|
-
if (!this.notesSession) {
|
|
209
|
-
this.pushHistory();
|
|
210
|
-
this.notesSession = true;
|
|
211
|
-
}
|
|
212
|
-
slide.notes = this.notesText.value;
|
|
213
|
-
this.markDirty();
|
|
214
|
-
this.statusbar.setStatus('正在编辑备注');
|
|
215
|
-
});
|
|
216
|
-
this.notesText.addEventListener('blur', () => {
|
|
217
|
-
if (!this.notesSession) return;
|
|
218
|
-
this.notesSession = false;
|
|
219
|
-
this.statusbar.setStatus('');
|
|
220
|
-
this.ribbon.refresh();
|
|
221
|
-
});
|
|
222
|
-
closeBtn.addEventListener('click', () => this.toggleNotes());
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
buildSorterEvents() {
|
|
226
|
-
this.sorterEl.addEventListener('click', (e) => {
|
|
227
|
-
const card = e.target.closest('[data-index]');
|
|
228
|
-
if (card) this.selectSlide(Number(card.dataset.index));
|
|
229
|
-
});
|
|
230
|
-
this.sorterEl.addEventListener('dblclick', (e) => {
|
|
231
|
-
const card = e.target.closest('[data-index]');
|
|
232
|
-
if (card) {
|
|
233
|
-
this.selectSlide(Number(card.dataset.index));
|
|
234
|
-
this.setView('normal');
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
this.sorterEl.addEventListener('contextmenu', (e) => {
|
|
238
|
-
const card = e.target.closest('[data-index]');
|
|
239
|
-
if (!card) return;
|
|
240
|
-
e.preventDefault();
|
|
241
|
-
const i = Number(card.dataset.index);
|
|
242
|
-
this.selectSlide(i);
|
|
243
|
-
this.openSlideMenu(e.clientX, e.clientY, i);
|
|
244
|
-
});
|
|
245
|
-
this.sorterEl.addEventListener('dragstart', (e) => {
|
|
246
|
-
const card = e.target.closest('[data-index]');
|
|
247
|
-
if (!card) return;
|
|
248
|
-
this.dragIndex = Number(card.dataset.index);
|
|
249
|
-
card.classList.add('gbp-is-dragging');
|
|
250
|
-
e.dataTransfer.effectAllowed = 'move';
|
|
251
|
-
e.dataTransfer.setData('text/plain', String(this.dragIndex));
|
|
252
|
-
});
|
|
253
|
-
this.sorterEl.addEventListener('dragend', () => {
|
|
254
|
-
this.dragIndex = null;
|
|
255
|
-
this.sorterEl.querySelectorAll('.gbp-is-dragging, .gbp-is-drop').forEach((n) => n.classList.remove('gbp-is-dragging', 'gbp-is-drop'));
|
|
256
|
-
});
|
|
257
|
-
this.sorterEl.addEventListener('dragover', (e) => {
|
|
258
|
-
const card = e.target.closest('[data-index]');
|
|
259
|
-
if (!card || this.dragIndex === null) return;
|
|
260
|
-
e.preventDefault();
|
|
261
|
-
this.sorterEl.querySelectorAll('.gbp-is-drop').forEach((n) => n.classList.remove('gbp-is-drop'));
|
|
262
|
-
card.classList.add('gbp-is-drop');
|
|
263
|
-
});
|
|
264
|
-
this.sorterEl.addEventListener('drop', (e) => {
|
|
265
|
-
const card = e.target.closest('[data-index]');
|
|
266
|
-
if (!card || this.dragIndex === null) return;
|
|
267
|
-
e.preventDefault();
|
|
268
|
-
const to = Number(card.dataset.index);
|
|
269
|
-
if (to !== this.dragIndex) this.moveSlideTo(this.dragIndex, to);
|
|
270
|
-
this.dragIndex = null;
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/* ===================== 启动/销毁 ===================== */
|
|
275
|
-
|
|
276
|
-
start() {
|
|
277
|
-
this.renderAll();
|
|
278
|
-
notify.info(this.readOnly ? '已就绪:只读预览' : '已就绪:可直接编辑,或用「文件 → 打开」载入 .pptx');
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
destroy() {
|
|
282
|
-
// 全屏中的根节点挂在 body 上:先归还原位,宿主随后清空挂载点时才能一并移除
|
|
283
|
-
if (this.pageFullscreen) this.setPageFullscreen(false);
|
|
284
|
-
this.canvas?.unbindWheel?.();
|
|
285
|
-
this.presenter?.close();
|
|
286
|
-
clearTimeout(this.autosaveTimer);
|
|
287
|
-
document.removeEventListener('keydown', this.onKeyBound);
|
|
288
|
-
document.removeEventListener('paste', this.onPasteBound);
|
|
289
|
-
document.removeEventListener('selectionchange', this.onSelectionChangeBound);
|
|
290
|
-
document.removeEventListener('keydown', this.onFullscreenEsc, true);
|
|
291
|
-
window.removeEventListener('beforeunload', this.onBeforeUnloadBound);
|
|
292
|
-
window.removeEventListener('dragover', this.onDragOverBound);
|
|
293
|
-
window.removeEventListener('drop', this.onDropBound);
|
|
294
|
-
window.removeEventListener('resize', this.onResizeBound);
|
|
295
|
-
this.guard.destroy();
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/* ===================== 渲染 ===================== */
|
|
299
|
-
|
|
300
|
-
async selfTest() {
|
|
301
|
-
const snap = {
|
|
302
|
-
version: 2,
|
|
303
|
-
title: '自检',
|
|
304
|
-
theme: 'midnight',
|
|
305
|
-
size: { w: 960, h: 540 },
|
|
306
|
-
slides: [
|
|
307
|
-
{
|
|
308
|
-
id: 's1',
|
|
309
|
-
bg: '',
|
|
310
|
-
notes: '备注第一行\n第二行',
|
|
311
|
-
hidden: false,
|
|
312
|
-
transition: { type: 'fade', dir: 'l', speed: 'med' },
|
|
313
|
-
objects: [
|
|
314
|
-
{ id: 'o1', type: 'text', x: 40, y: 30, w: 400, h: 120, rot: 0, z: 1, props: { html: '<div>标题 <b>加粗</b></div><ul><li>项目一</li><li>项目二</li></ul>', font: '', fontSize: 32, color: '', align: 'left', bold: false, italic: false, underline: false, strike: false, anchor: 'top', lineSpacing: 1, fill: '', stroke: '', strokeW: 0 } },
|
|
315
|
-
{ id: 'o2', type: 'shape', x: 500, y: 60, w: 220, h: 140, rot: 15, z: 2, props: { kind: 'star', fill: '#4c9be8', stroke: '#333333', strokeW: 2, dash: 'dash', shadow: true, text: '形状文字', textColor: '#ffffff', font: '', fontSize: 18, bold: false, italic: false, underline: false, align: 'center', anchor: 'middle' } },
|
|
316
|
-
{ id: 'o3', type: 'line', x: 260, y: 300, w: -200, h: 80, rot: 0, z: 3, props: { stroke: '#202124', strokeW: 2, dash: 'dot', arrowStart: true, arrowEnd: true } },
|
|
317
|
-
{ id: 'o4', type: 'table', x: 120, y: 380, w: 440, h: 120, rot: 0, z: 4, props: { rows: [['A', 'B'], ['C', 'D']], headerRow: true, bandedRows: true, tableStyle: 'banded', font: '', fontSize: 16, color: '', colW: [], rowH: [] } },
|
|
318
|
-
],
|
|
319
|
-
},
|
|
320
|
-
{ id: 's2', bg: '#123456', notes: '', hidden: true, transition: { type: 'push', dir: 'u', speed: 'slow' }, objects: [] },
|
|
321
|
-
],
|
|
322
|
-
};
|
|
323
|
-
const bytes = await buildPptxVerified(snap);
|
|
324
|
-
const back = await parsePptx(bytes);
|
|
325
|
-
return {
|
|
326
|
-
bytes: bytes.length,
|
|
327
|
-
size: back.size,
|
|
328
|
-
slides: back.slides.length,
|
|
329
|
-
theme: back.theme,
|
|
330
|
-
hidden: back.slides.map((s) => s.hidden),
|
|
331
|
-
transition: back.slides.map((s) => s.transition.type),
|
|
332
|
-
objects: back.slides[0].objects.map((o) => o.type),
|
|
333
|
-
list: back.slides[0].objects[0]?.props?.html?.includes('<ul>'),
|
|
334
|
-
tableStyle: back.slides[0].objects[3]?.props?.tableStyle,
|
|
335
|
-
};
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
Object.assign(
|
|
340
|
-
SlidesApp.prototype,
|
|
341
|
-
renderMethods,
|
|
342
|
-
historyMethods,
|
|
343
|
-
textMethods,
|
|
344
|
-
objectsMethods,
|
|
345
|
-
clipboardMethods,
|
|
346
|
-
arrangeMethods,
|
|
347
|
-
tableMethods,
|
|
348
|
-
slidesMethods,
|
|
349
|
-
viewMethods,
|
|
350
|
-
fileMethods,
|
|
351
|
-
findMethods,
|
|
352
|
-
menusMethods,
|
|
353
|
-
keysMethods
|
|
354
|
-
);
|
|
1
|
+
// [文件] gbpptx·SlidesApp —— Office 式演示编辑器装配(Ribbon/窗格/画布/放映/文件读写/查找替换)
|
|
2
|
+
//
|
|
3
|
+
// [设计] 本文件只留「装配 + 渲染 + 生命周期 + 自检」;其余职责按段落拆到 ./slides/*:
|
|
4
|
+
// render(选择与渲染)/ history(脏状态与撤销)/ text(文本编辑与格式)/ objects(属性与插入)/
|
|
5
|
+
// clipboard / arrange / table / slides(页操作)/ view(视图与放映)/ file / find / menus / keys。
|
|
6
|
+
// 各模块导出方法组,由文件末尾 Object.assign 装配到 SlidesApp.prototype —— 同一个 this、同一套调用,
|
|
7
|
+
// 外部拿到的 SlidesApp 形状与拆分前完全一致。
|
|
8
|
+
// [坑] 装配必须在实例化之前完成:Object.assign 在模块求值时执行,而实例只能在 import 之后 new。
|
|
9
|
+
|
|
10
|
+
import { clamp, createReadOnlyGuard } from 'guabiutils';
|
|
11
|
+
import { createDraft, sizeOf } from '../../src/kernel/model.js';
|
|
12
|
+
import { buildPptxVerified } from '../../src/kernel/roundtrip.js';
|
|
13
|
+
import { parsePptx } from '../../src/kernel/read.js';
|
|
14
|
+
import { notify } from './ui/notify.js';
|
|
15
|
+
import { TitleBar } from './ui/titlebar.js';
|
|
16
|
+
import { Ribbon } from './ui/ribbon.js';
|
|
17
|
+
import { SlidePane } from './ui/slide-pane.js';
|
|
18
|
+
import { Canvas } from './ui/canvas.js';
|
|
19
|
+
import { FormatPane } from './ui/format-pane.js';
|
|
20
|
+
import { StatusBar } from './ui/statusbar.js';
|
|
21
|
+
import { Present } from './ui/present.js';
|
|
22
|
+
import { icon } from './ui/icons.js';
|
|
23
|
+
import { el, loadPrefs, savePrefs, ZOOM_MIN, ZOOM_MAX } from './slides/shared.js';
|
|
24
|
+
import { renderMethods } from './slides/render.js';
|
|
25
|
+
import { historyMethods } from './slides/history.js';
|
|
26
|
+
import { textMethods } from './slides/text.js';
|
|
27
|
+
import { objectsMethods } from './slides/objects.js';
|
|
28
|
+
import { clipboardMethods } from './slides/clipboard.js';
|
|
29
|
+
import { arrangeMethods } from './slides/arrange.js';
|
|
30
|
+
import { tableMethods } from './slides/table.js';
|
|
31
|
+
import { slidesMethods } from './slides/slides.js';
|
|
32
|
+
import { viewMethods } from './slides/view.js';
|
|
33
|
+
import { fileMethods } from './slides/file.js';
|
|
34
|
+
import { findMethods } from './slides/find.js';
|
|
35
|
+
import { menusMethods } from './slides/menus.js';
|
|
36
|
+
import { keysMethods } from './slides/keys.js';
|
|
37
|
+
|
|
38
|
+
export class SlidesApp {
|
|
39
|
+
/**
|
|
40
|
+
* @param {object} [options] readOnly=只读预览(禁写且锁编辑控件);hideMenuBar=隐藏顶部菜单栏那一行;
|
|
41
|
+
* host=嵌入宿主的文件能力(getTitle 文档名 / rename 重命名 / download 下载),缺省走包内实现
|
|
42
|
+
*/
|
|
43
|
+
constructor(mount, options = {}) {
|
|
44
|
+
this.mount = mount;
|
|
45
|
+
this.readOnly = Boolean(options.readOnly);
|
|
46
|
+
this.menuBarVisible = !options.hideMenuBar;
|
|
47
|
+
this.host = options.host || null;
|
|
48
|
+
const prefs = loadPrefs();
|
|
49
|
+
this.prefs = prefs;
|
|
50
|
+
this.snapshot = createDraft();
|
|
51
|
+
this.index = 0;
|
|
52
|
+
this.selectedIds = [];
|
|
53
|
+
this.zoom = clamp(Number(prefs.zoom) || 65, ZOOM_MIN, ZOOM_MAX);
|
|
54
|
+
this.dirty = false;
|
|
55
|
+
this.saving = false;
|
|
56
|
+
this.revision = 0;
|
|
57
|
+
this.history = [];
|
|
58
|
+
this.future = [];
|
|
59
|
+
this.clipboard = null;
|
|
60
|
+
this.editing = null;
|
|
61
|
+
this.tableCursor = null;
|
|
62
|
+
this.notesSession = false;
|
|
63
|
+
this.autosave = options.autoSave !== undefined ? !!options.autoSave : !!prefs.autosave;
|
|
64
|
+
this.notesVisible = prefs.notesVisible !== false;
|
|
65
|
+
this.gridVisible = !!prefs.grid;
|
|
66
|
+
this.guidesVisible = prefs.guides === true;
|
|
67
|
+
this.ribbonCollapsed = !!prefs.ribbonCollapsed;
|
|
68
|
+
this.formatPaneOpen = !!prefs.formatPaneOpen;
|
|
69
|
+
this.view = 'normal';
|
|
70
|
+
this.preview = false;
|
|
71
|
+
this.previewRestore = null;
|
|
72
|
+
this.pageFullscreen = false;
|
|
73
|
+
this.presentOptions = { presenter: !!prefs.presenter, loop: !!prefs.loop };
|
|
74
|
+
this.lastBytes = null;
|
|
75
|
+
|
|
76
|
+
const seq = Number(prefs.docSeq) || 1;
|
|
77
|
+
prefs.docSeq = seq + 1;
|
|
78
|
+
savePrefs(prefs);
|
|
79
|
+
this.doc = { name: `演示文稿${seq}`, handle: null, savedAt: 0 };
|
|
80
|
+
|
|
81
|
+
this.build();
|
|
82
|
+
this.bindGlobal();
|
|
83
|
+
this.presenter = new Present(this);
|
|
84
|
+
this.applyMode();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/* ===================== 骨架 ===================== */
|
|
88
|
+
|
|
89
|
+
get size() {
|
|
90
|
+
return sizeOf(this.snapshot);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** 文档标题:宿主给了就用宿主的(服务端文档名),否则用本地文档名 */
|
|
94
|
+
getTitle() {
|
|
95
|
+
return this.host?.getTitle?.() || this.doc.name || '新建演示';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 文档头「重命名」是否可用:宿主提供才渲染,独立模式走包内实现 */
|
|
99
|
+
canRename() {
|
|
100
|
+
return this.host ? typeof this.host.rename === 'function' : true;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** 文档头「下载」是否可用:宿主提供才渲染,独立模式走包内实现 */
|
|
104
|
+
canDownload() {
|
|
105
|
+
return this.host ? typeof this.host.download === 'function' : true;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** 宿主改了文档名之后刷新文档头(标题/状态) */
|
|
109
|
+
refreshHead() {
|
|
110
|
+
this.titlebar?.refresh();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
build() {
|
|
114
|
+
const root = el('div', 'pp-app');
|
|
115
|
+
root.innerHTML = `
|
|
116
|
+
<div class="pp-head-host"></div>
|
|
117
|
+
<div class="pp-topbar-host"></div>
|
|
118
|
+
<div class="pp-ribbon-host"></div>
|
|
119
|
+
<div class="pp-main">
|
|
120
|
+
<div class="pp-main__left"></div>
|
|
121
|
+
<div class="pp-center">
|
|
122
|
+
<div class="pp-canvas-host"></div>
|
|
123
|
+
<div class="pp-notes" hidden>
|
|
124
|
+
<div class="pp-notes__head">
|
|
125
|
+
<span class="pp-notes__title">备注</span>
|
|
126
|
+
<button type="button" class="pp-iconbtn" data-act="notes-close" title="关闭备注">${icon('close', 14)}</button>
|
|
127
|
+
</div>
|
|
128
|
+
<textarea class="pp-notes__text" placeholder="单击此处添加演讲者备注"></textarea>
|
|
129
|
+
</div>
|
|
130
|
+
</div>
|
|
131
|
+
<div class="pp-main__right"></div>
|
|
132
|
+
</div>
|
|
133
|
+
<div class="pp-statusbar-host"></div>`;
|
|
134
|
+
this.mount.appendChild(root);
|
|
135
|
+
this.root = root;
|
|
136
|
+
this.topbarHost = root.querySelector('.pp-topbar-host');
|
|
137
|
+
this.headHost = root.querySelector('.pp-head-host');
|
|
138
|
+
this.ribbonHost = root.querySelector('.pp-ribbon-host');
|
|
139
|
+
this.formatPaneHost = root.querySelector('.pp-main__right');
|
|
140
|
+
this.left = root.querySelector('.pp-main__left');
|
|
141
|
+
this.center = root.querySelector('.pp-center');
|
|
142
|
+
this.right = root.querySelector('.pp-main__right');
|
|
143
|
+
this.canvasHost = root.querySelector('.pp-canvas-host');
|
|
144
|
+
this.notesEl = root.querySelector('.pp-notes');
|
|
145
|
+
this.notesText = root.querySelector('.pp-notes__text');
|
|
146
|
+
|
|
147
|
+
this.titlebar = new TitleBar(this);
|
|
148
|
+
this.ribbon = new Ribbon(this);
|
|
149
|
+
this.slidePane = new SlidePane(this);
|
|
150
|
+
this.canvas = new Canvas(this);
|
|
151
|
+
this.formatPane = new FormatPane(this);
|
|
152
|
+
this.statusbar = new StatusBar(this);
|
|
153
|
+
|
|
154
|
+
root.querySelector('.pp-topbar-host').appendChild(this.titlebar.el);
|
|
155
|
+
root.querySelector('.pp-head-host').appendChild(this.titlebar.headEl);
|
|
156
|
+
root.querySelector('.pp-ribbon-host').appendChild(this.ribbon.el);
|
|
157
|
+
this.left.appendChild(this.slidePane.el);
|
|
158
|
+
this.canvasHost.appendChild(this.canvas.el);
|
|
159
|
+
this.right.appendChild(this.formatPane.el);
|
|
160
|
+
root.querySelector('.pp-statusbar-host').appendChild(this.statusbar.el);
|
|
161
|
+
|
|
162
|
+
this.sorterEl = el('div', 'pp-sorter');
|
|
163
|
+
this.sorterEl.hidden = true;
|
|
164
|
+
this.center.appendChild(this.sorterEl);
|
|
165
|
+
this.readingEl = el('div', 'pp-reading');
|
|
166
|
+
this.readingEl.hidden = true;
|
|
167
|
+
this.center.appendChild(this.readingEl);
|
|
168
|
+
this.buildSorterEvents();
|
|
169
|
+
|
|
170
|
+
this.bindNotes(root.querySelector('[data-act="notes-close"]'));
|
|
171
|
+
// 只 inert 功能区与各选项卡本身:文档头的预览按钮在 inert 之外,预览档里仍可点「退出预览」
|
|
172
|
+
this.guard = createReadOnlyGuard(root, {
|
|
173
|
+
editZones: () => [
|
|
174
|
+
this.ribbon.bodyEl,
|
|
175
|
+
...this.ribbon.tabsEl.querySelectorAll('.pp-rb-tab'),
|
|
176
|
+
this.ribbon.contextHost,
|
|
177
|
+
this.formatPaneHost,
|
|
178
|
+
this.notesEl,
|
|
179
|
+
],
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** 构造参数落 DOM:装配完成后才调用,此时标题栏、Ribbon、格式窗格都已就位 */
|
|
184
|
+
applyMode() {
|
|
185
|
+
this.setMenuBarVisible(this.menuBarVisible);
|
|
186
|
+
if (this.readOnly) this.setReadOnly(true);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** 显示/隐藏顶部菜单栏那一行;文档头(标题/自动保存/保存/预览)不受影响 */
|
|
190
|
+
setMenuBarVisible(on) {
|
|
191
|
+
this.menuBarVisible = !!on;
|
|
192
|
+
this.topbarHost.style.display = this.menuBarVisible ? '' : 'none';
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** 只读开关:DOM 守卫 + 编辑面板 + 菜单栏/备注可用性同一处切换 */
|
|
196
|
+
setReadOnly(on) {
|
|
197
|
+
this.readOnly = !!on;
|
|
198
|
+
this.guard.setReadOnly(this.readOnly);
|
|
199
|
+
this.notesText.disabled = this.readOnly;
|
|
200
|
+
this.renderAll();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
bindNotes(closeBtn) {
|
|
204
|
+
this.notesText.addEventListener('focus', () => { this.notesSession = false; });
|
|
205
|
+
this.notesText.addEventListener('input', () => {
|
|
206
|
+
const slide = this.currentSlide();
|
|
207
|
+
if (!slide) return;
|
|
208
|
+
if (!this.notesSession) {
|
|
209
|
+
this.pushHistory();
|
|
210
|
+
this.notesSession = true;
|
|
211
|
+
}
|
|
212
|
+
slide.notes = this.notesText.value;
|
|
213
|
+
this.markDirty();
|
|
214
|
+
this.statusbar.setStatus('正在编辑备注');
|
|
215
|
+
});
|
|
216
|
+
this.notesText.addEventListener('blur', () => {
|
|
217
|
+
if (!this.notesSession) return;
|
|
218
|
+
this.notesSession = false;
|
|
219
|
+
this.statusbar.setStatus('');
|
|
220
|
+
this.ribbon.refresh();
|
|
221
|
+
});
|
|
222
|
+
closeBtn.addEventListener('click', () => this.toggleNotes());
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
buildSorterEvents() {
|
|
226
|
+
this.sorterEl.addEventListener('click', (e) => {
|
|
227
|
+
const card = e.target.closest('[data-index]');
|
|
228
|
+
if (card) this.selectSlide(Number(card.dataset.index));
|
|
229
|
+
});
|
|
230
|
+
this.sorterEl.addEventListener('dblclick', (e) => {
|
|
231
|
+
const card = e.target.closest('[data-index]');
|
|
232
|
+
if (card) {
|
|
233
|
+
this.selectSlide(Number(card.dataset.index));
|
|
234
|
+
this.setView('normal');
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
this.sorterEl.addEventListener('contextmenu', (e) => {
|
|
238
|
+
const card = e.target.closest('[data-index]');
|
|
239
|
+
if (!card) return;
|
|
240
|
+
e.preventDefault();
|
|
241
|
+
const i = Number(card.dataset.index);
|
|
242
|
+
this.selectSlide(i);
|
|
243
|
+
this.openSlideMenu(e.clientX, e.clientY, i);
|
|
244
|
+
});
|
|
245
|
+
this.sorterEl.addEventListener('dragstart', (e) => {
|
|
246
|
+
const card = e.target.closest('[data-index]');
|
|
247
|
+
if (!card) return;
|
|
248
|
+
this.dragIndex = Number(card.dataset.index);
|
|
249
|
+
card.classList.add('gbp-is-dragging');
|
|
250
|
+
e.dataTransfer.effectAllowed = 'move';
|
|
251
|
+
e.dataTransfer.setData('text/plain', String(this.dragIndex));
|
|
252
|
+
});
|
|
253
|
+
this.sorterEl.addEventListener('dragend', () => {
|
|
254
|
+
this.dragIndex = null;
|
|
255
|
+
this.sorterEl.querySelectorAll('.gbp-is-dragging, .gbp-is-drop').forEach((n) => n.classList.remove('gbp-is-dragging', 'gbp-is-drop'));
|
|
256
|
+
});
|
|
257
|
+
this.sorterEl.addEventListener('dragover', (e) => {
|
|
258
|
+
const card = e.target.closest('[data-index]');
|
|
259
|
+
if (!card || this.dragIndex === null) return;
|
|
260
|
+
e.preventDefault();
|
|
261
|
+
this.sorterEl.querySelectorAll('.gbp-is-drop').forEach((n) => n.classList.remove('gbp-is-drop'));
|
|
262
|
+
card.classList.add('gbp-is-drop');
|
|
263
|
+
});
|
|
264
|
+
this.sorterEl.addEventListener('drop', (e) => {
|
|
265
|
+
const card = e.target.closest('[data-index]');
|
|
266
|
+
if (!card || this.dragIndex === null) return;
|
|
267
|
+
e.preventDefault();
|
|
268
|
+
const to = Number(card.dataset.index);
|
|
269
|
+
if (to !== this.dragIndex) this.moveSlideTo(this.dragIndex, to);
|
|
270
|
+
this.dragIndex = null;
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/* ===================== 启动/销毁 ===================== */
|
|
275
|
+
|
|
276
|
+
start() {
|
|
277
|
+
this.renderAll();
|
|
278
|
+
notify.info(this.readOnly ? '已就绪:只读预览' : '已就绪:可直接编辑,或用「文件 → 打开」载入 .pptx');
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
destroy() {
|
|
282
|
+
// 全屏中的根节点挂在 body 上:先归还原位,宿主随后清空挂载点时才能一并移除
|
|
283
|
+
if (this.pageFullscreen) this.setPageFullscreen(false);
|
|
284
|
+
this.canvas?.unbindWheel?.();
|
|
285
|
+
this.presenter?.close();
|
|
286
|
+
clearTimeout(this.autosaveTimer);
|
|
287
|
+
document.removeEventListener('keydown', this.onKeyBound);
|
|
288
|
+
document.removeEventListener('paste', this.onPasteBound);
|
|
289
|
+
document.removeEventListener('selectionchange', this.onSelectionChangeBound);
|
|
290
|
+
document.removeEventListener('keydown', this.onFullscreenEsc, true);
|
|
291
|
+
window.removeEventListener('beforeunload', this.onBeforeUnloadBound);
|
|
292
|
+
window.removeEventListener('dragover', this.onDragOverBound);
|
|
293
|
+
window.removeEventListener('drop', this.onDropBound);
|
|
294
|
+
window.removeEventListener('resize', this.onResizeBound);
|
|
295
|
+
this.guard.destroy();
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/* ===================== 渲染 ===================== */
|
|
299
|
+
|
|
300
|
+
async selfTest() {
|
|
301
|
+
const snap = {
|
|
302
|
+
version: 2,
|
|
303
|
+
title: '自检',
|
|
304
|
+
theme: 'midnight',
|
|
305
|
+
size: { w: 960, h: 540 },
|
|
306
|
+
slides: [
|
|
307
|
+
{
|
|
308
|
+
id: 's1',
|
|
309
|
+
bg: '',
|
|
310
|
+
notes: '备注第一行\n第二行',
|
|
311
|
+
hidden: false,
|
|
312
|
+
transition: { type: 'fade', dir: 'l', speed: 'med' },
|
|
313
|
+
objects: [
|
|
314
|
+
{ id: 'o1', type: 'text', x: 40, y: 30, w: 400, h: 120, rot: 0, z: 1, props: { html: '<div>标题 <b>加粗</b></div><ul><li>项目一</li><li>项目二</li></ul>', font: '', fontSize: 32, color: '', align: 'left', bold: false, italic: false, underline: false, strike: false, anchor: 'top', lineSpacing: 1, fill: '', stroke: '', strokeW: 0 } },
|
|
315
|
+
{ id: 'o2', type: 'shape', x: 500, y: 60, w: 220, h: 140, rot: 15, z: 2, props: { kind: 'star', fill: '#4c9be8', stroke: '#333333', strokeW: 2, dash: 'dash', shadow: true, text: '形状文字', textColor: '#ffffff', font: '', fontSize: 18, bold: false, italic: false, underline: false, align: 'center', anchor: 'middle' } },
|
|
316
|
+
{ id: 'o3', type: 'line', x: 260, y: 300, w: -200, h: 80, rot: 0, z: 3, props: { stroke: '#202124', strokeW: 2, dash: 'dot', arrowStart: true, arrowEnd: true } },
|
|
317
|
+
{ id: 'o4', type: 'table', x: 120, y: 380, w: 440, h: 120, rot: 0, z: 4, props: { rows: [['A', 'B'], ['C', 'D']], headerRow: true, bandedRows: true, tableStyle: 'banded', font: '', fontSize: 16, color: '', colW: [], rowH: [] } },
|
|
318
|
+
],
|
|
319
|
+
},
|
|
320
|
+
{ id: 's2', bg: '#123456', notes: '', hidden: true, transition: { type: 'push', dir: 'u', speed: 'slow' }, objects: [] },
|
|
321
|
+
],
|
|
322
|
+
};
|
|
323
|
+
const bytes = await buildPptxVerified(snap);
|
|
324
|
+
const back = await parsePptx(bytes);
|
|
325
|
+
return {
|
|
326
|
+
bytes: bytes.length,
|
|
327
|
+
size: back.size,
|
|
328
|
+
slides: back.slides.length,
|
|
329
|
+
theme: back.theme,
|
|
330
|
+
hidden: back.slides.map((s) => s.hidden),
|
|
331
|
+
transition: back.slides.map((s) => s.transition.type),
|
|
332
|
+
objects: back.slides[0].objects.map((o) => o.type),
|
|
333
|
+
list: back.slides[0].objects[0]?.props?.html?.includes('<ul>'),
|
|
334
|
+
tableStyle: back.slides[0].objects[3]?.props?.tableStyle,
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
Object.assign(
|
|
340
|
+
SlidesApp.prototype,
|
|
341
|
+
renderMethods,
|
|
342
|
+
historyMethods,
|
|
343
|
+
textMethods,
|
|
344
|
+
objectsMethods,
|
|
345
|
+
clipboardMethods,
|
|
346
|
+
arrangeMethods,
|
|
347
|
+
tableMethods,
|
|
348
|
+
slidesMethods,
|
|
349
|
+
viewMethods,
|
|
350
|
+
fileMethods,
|
|
351
|
+
findMethods,
|
|
352
|
+
menusMethods,
|
|
353
|
+
keysMethods
|
|
354
|
+
);
|