gbpptx 0.1.0 → 0.2.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.
@@ -1,100 +1,292 @@
1
- // [文件] gbpptx·放映 —— 全屏放映层(按 F5 进入;左右/空格翻页、Esc 退出)
2
- import { SLIDE_H, SLIDE_W } from '../../../src/kernel/model.js';
1
+ // [文件] gbpptx·放映 —— 全屏放映 + 演讲者视图(切换动画/计时器/备注/下一页预览)
2
+
3
+ import { transitionOf } from '../../../src/kernel/model.js';
3
4
  import { renderPage } from './page-view.js';
5
+ import { icon } from './icons.js';
6
+ import { animateSlideFrame } from './transition.js';
7
+ import { notify } from './notify.js';
4
8
 
5
9
  export class Present {
6
- /**
7
- * @param {object} io { getSlides, getTheme }
8
- */
9
- constructor(io) {
10
- this.io = io;
10
+ constructor(app) {
11
+ this.app = app;
11
12
  this.el = null;
12
- this.index = 0;
13
- this.onKey = (e) => {
14
- if (!this.el) return;
15
- if (e.key === 'Escape') this.close();
16
- else if (e.key === 'ArrowRight' || e.key === ' ' || e.key === 'PageDown') { e.preventDefault(); this.next(); }
17
- else if (e.key === 'ArrowLeft' || e.key === 'PageUp') { e.preventDefault(); this.prev(); }
13
+ this.fs = false;
14
+ this.pos = 0;
15
+ this.black = false;
16
+ this.white = false;
17
+ this.presenter = false;
18
+ this.timer = null;
19
+ this.startedAt = 0;
20
+ this.controlsTimer = 0;
21
+ this.onKey = (e) => this.handleKey(e);
22
+ this.onResize = () => { if (this.el) this.render(true); };
23
+ this.onFsChange = () => {
24
+ if (this.el && this.fs && !document.fullscreenElement) this.close();
18
25
  };
19
26
  }
20
27
 
21
28
  get slides() {
22
- return this.io.getSlides() || [];
29
+ return this.app.snapshot.slides.filter((s) => !s.hidden);
30
+ }
31
+
32
+ get current() {
33
+ return this.slides[this.pos] || null;
23
34
  }
24
35
 
25
36
  open(startIndex = 0) {
26
37
  const slides = this.slides;
27
- if (!slides.length) return;
38
+ if (!slides.length) {
39
+ notify.warning('没有可放映的幻灯片(可能全部被隐藏)');
40
+ return;
41
+ }
28
42
  this.close();
29
- this.index = Math.max(0, Math.min(slides.length - 1, startIndex));
43
+ this.presenter = !!this.app.presentOptions.presenter;
44
+ const visibleIndex = Math.max(0, slides.findIndex((s) => this.app.snapshot.slides.indexOf(s) >= startIndex));
45
+ this.pos = visibleIndex >= 0 ? visibleIndex : 0;
46
+ this.startedAt = Date.now();
30
47
  const overlay = document.createElement('div');
31
- overlay.className = 'sl-present';
48
+ overlay.className = 'pp-show';
49
+ overlay.tabIndex = 0;
32
50
  this.el = overlay;
33
51
  document.body.appendChild(overlay);
34
- this.render();
52
+ this.bind();
53
+ this.render(true);
35
54
  document.addEventListener('keydown', this.onKey, true);
55
+ document.addEventListener('fullscreenchange', this.onFsChange);
56
+ window.addEventListener('resize', this.onResize);
57
+ this.timer = setInterval(() => this.tick(), 1000);
58
+ const req = overlay.requestFullscreen?.({ navigationUI: 'hide' });
59
+ if (req?.then) req.then(() => { this.fs = true; }).catch(() => { this.fs = false; });
60
+ else this.fs = false;
61
+ overlay.focus();
62
+ this.hideControlsSoon();
36
63
  }
37
64
 
38
- render() {
39
- const slides = this.slides;
40
- const overlay = this.el;
41
- if (!overlay) return;
42
- overlay.replaceChildren();
43
- const page = renderPage(slides[this.index], this.io.getTheme());
44
- const scale = Math.min(window.innerWidth / SLIDE_W, window.innerHeight / SLIDE_H);
45
- page.style.transform = `scale(${scale})`;
46
- page.style.transformOrigin = 'center center';
47
- const holder = document.createElement('div');
48
- holder.style.cssText = `width:${SLIDE_W * scale}px;height:${SLIDE_H * scale}px;display:flex;align-items:center;justify-content:center;overflow:hidden`;
49
- const inner = document.createElement('div');
50
- inner.style.cssText = `width:${SLIDE_W}px;height:${SLIDE_H}px;transform:scale(${scale});transform-origin:center center`;
51
- inner.appendChild(page);
52
- page.style.transform = '';
53
- holder.appendChild(inner);
54
- overlay.appendChild(holder);
65
+ bind() {
66
+ const el = this.el;
67
+ el.addEventListener('click', (e) => {
68
+ if (e.target.closest('.pp-show__bar, .pp-show__side')) return;
69
+ this.next();
70
+ });
71
+ el.addEventListener('contextmenu', (e) => {
72
+ e.preventDefault();
73
+ this.prev();
74
+ });
75
+ el.addEventListener('mousemove', () => this.showControls());
76
+ }
77
+
78
+ render(initial = false) {
79
+ if (!this.el) return;
80
+ const slide = this.current;
81
+ if (!slide) return this.close();
82
+ const size = this.app.size;
83
+ const theme = this.app.snapshot.theme;
84
+ this.el.classList.toggle('pp-show--presenter', this.presenter);
85
+ this.el.classList.toggle('is-black', this.black);
86
+ this.el.classList.toggle('is-white', this.white);
87
+ this.el.replaceChildren();
88
+ this.el.style.setProperty('--show-bg', this.black ? '#000' : this.white ? '#fff' : '#111');
89
+
90
+ const stage = document.createElement('div');
91
+ stage.className = 'pp-show__stage';
92
+ const page = renderPage(slide, theme, { size });
93
+ const fit = this.fitScale(size);
94
+ page.style.transform = `scale(${fit})`;
95
+ page.style.transformOrigin = '0 0';
96
+ const frame = document.createElement('div');
97
+ frame.className = 'pp-show__frame';
98
+ frame.style.width = `${size.w * fit}px`;
99
+ frame.style.height = `${size.h * fit}px`;
100
+ frame.appendChild(page);
101
+ stage.appendChild(frame);
102
+ this.el.appendChild(stage);
55
103
 
104
+ if (this.presenter) this.renderPresenter();
105
+
106
+ const bar = this.renderBar();
107
+ this.el.appendChild(bar);
108
+
109
+ if (!initial) animateSlideFrame(page.parentElement, transitionOf(slide));
110
+ this.showControls();
111
+ this.updateSide();
112
+ }
113
+
114
+ fitScale(size) {
115
+ const w = this.el.clientWidth || window.innerWidth;
116
+ const h = this.el.clientHeight || window.innerHeight;
117
+ if (this.presenter) return Math.min((w * 0.66) / size.w, (h - 40) / size.h, 1.35);
118
+ return Math.min((w - 40) / size.w, (h - 40) / size.h);
119
+ }
120
+
121
+ renderBar() {
56
122
  const bar = document.createElement('div');
57
- bar.className = 'sl-present__bar';
58
- const prev = document.createElement('button');
59
- prev.type = 'button';
60
- prev.className = 'sl-present__btn';
61
- prev.textContent = '上一页';
62
- prev.addEventListener('click', () => this.prev());
123
+ bar.className = 'pp-show__bar';
124
+ const prev = this.iconBtn('chevronLeft', '上一页', () => this.prev());
125
+ const next = this.iconBtn('chevronRight', '下一页', () => this.next());
126
+ const mode = this.iconBtn(this.presenter ? 'presenter' : 'monitor', this.presenter ? '演讲者视图' : '放映视图', () => this.togglePresenter());
127
+ const black = this.iconBtn('eye', '黑屏 (B)', () => this.toggleBlack());
128
+ const quit = this.iconBtn('close', '结束放映 (Esc)', () => this.close());
63
129
  const info = document.createElement('span');
64
- info.textContent = `${this.index + 1} / ${slides.length}`;
65
- const next = document.createElement('button');
66
- next.type = 'button';
67
- next.className = 'sl-present__btn';
68
- next.textContent = '下一页';
69
- next.addEventListener('click', () => this.next());
70
- const quit = document.createElement('button');
71
- quit.type = 'button';
72
- quit.className = 'sl-present__btn';
73
- quit.textContent = '退出 (Esc)';
74
- quit.addEventListener('click', () => this.close());
75
- bar.append(prev, info, next, quit);
76
- overlay.appendChild(bar);
130
+ info.className = 'pp-show__count';
131
+ info.textContent = `${this.pos + 1} / ${this.slides.length}`;
132
+ bar.append(prev, info, next, mode, black, quit);
133
+ return bar;
134
+ }
135
+
136
+ iconBtn(name, title, onClick) {
137
+ const b = document.createElement('button');
138
+ b.type = 'button';
139
+ b.className = 'pp-show__btn';
140
+ b.title = title;
141
+ b.innerHTML = icon(name, 18);
142
+ b.addEventListener('click', (e) => { e.stopPropagation(); onClick(); });
143
+ return b;
144
+ }
145
+
146
+ renderPresenter() {
147
+ const side = document.createElement('div');
148
+ side.className = 'pp-show__side';
149
+ const timer = document.createElement('div');
150
+ timer.className = 'pp-show__timer';
151
+ timer.dataset.k = 'timer';
152
+ timer.textContent = '00:00';
153
+ const next = document.createElement('div');
154
+ next.className = 'pp-show__next';
155
+ const nextLabel = document.createElement('div');
156
+ nextLabel.className = 'pp-show__side-label';
157
+ nextLabel.textContent = '下一页';
158
+ next.appendChild(nextLabel);
159
+ const nextSlide = this.slides[this.pos + 1];
160
+ if (nextSlide) {
161
+ const size = this.app.size;
162
+ const preview = renderPage(nextSlide, this.app.snapshot.theme, { size });
163
+ const scale = Math.min(260 / size.w, 160 / size.h);
164
+ preview.style.transform = `scale(${scale})`;
165
+ preview.style.transformOrigin = 'top left';
166
+ const box = document.createElement('div');
167
+ box.className = 'pp-show__nextbox';
168
+ box.style.width = `${size.w * scale}px`;
169
+ box.style.height = `${size.h * scale}px`;
170
+ box.appendChild(preview);
171
+ next.appendChild(box);
172
+ } else {
173
+ const end = document.createElement('div');
174
+ end.className = 'pp-show__end';
175
+ end.textContent = '演示结束';
176
+ next.appendChild(end);
177
+ }
178
+ const notesLabel = document.createElement('div');
179
+ notesLabel.className = 'pp-show__side-label';
180
+ notesLabel.textContent = '备注';
181
+ const notes = document.createElement('div');
182
+ notes.className = 'pp-show__notes';
183
+ notes.textContent = this.current?.notes || '(本页没有备注)';
184
+ side.append(timer, next, notesLabel, notes);
185
+ this.el.appendChild(side);
186
+ }
187
+
188
+ updateSide() {
189
+ const timer = this.el?.querySelector('[data-k="timer"]');
190
+ if (timer) timer.textContent = this.elapsed();
191
+ }
192
+
193
+ tick() {
194
+ if (!this.el) return;
195
+ if (this.presenter) this.updateSide();
196
+ }
197
+
198
+ elapsed() {
199
+ const secs = Math.max(0, Math.floor((Date.now() - this.startedAt) / 1000));
200
+ return `${String(Math.floor(secs / 60)).padStart(2, '0')}:${String(secs % 60).padStart(2, '0')}`;
77
201
  }
78
202
 
79
203
  next() {
80
- if (this.index < this.slides.length - 1) {
81
- this.index += 1;
82
- this.render();
204
+ if (this.black || this.white) { this.black = false; this.white = false; this.render(); return; }
205
+ if (this.pos < this.slides.length - 1) {
206
+ this.pos += 1;
207
+ } else if (this.app.presentOptions.loop) {
208
+ this.pos = 0;
209
+ } else {
210
+ return;
83
211
  }
212
+ this.render();
84
213
  }
85
214
 
86
215
  prev() {
87
- if (this.index > 0) {
88
- this.index -= 1;
89
- this.render();
216
+ if (this.black || this.white) { this.black = false; this.white = false; this.render(); return; }
217
+ if (this.pos > 0) this.pos -= 1;
218
+ else if (this.app.presentOptions.loop) this.pos = this.slides.length - 1;
219
+ else return;
220
+ this.render();
221
+ }
222
+
223
+ toggleBlack() {
224
+ this.black = !this.black;
225
+ this.white = false;
226
+ this.render();
227
+ }
228
+
229
+ toggleWhite() {
230
+ this.white = !this.white;
231
+ this.black = false;
232
+ this.render();
233
+ }
234
+
235
+ togglePresenter() {
236
+ this.presenter = !this.presenter;
237
+ this.app.presentOptions.presenter = this.presenter;
238
+ this.app.ribbon?.refresh();
239
+ this.render();
240
+ }
241
+
242
+ showControls() {
243
+ if (!this.el) return;
244
+ this.el.classList.remove('is-idle');
245
+ clearTimeout(this.controlsTimer);
246
+ this.hideControlsSoon();
247
+ }
248
+
249
+ hideControlsSoon() {
250
+ clearTimeout(this.controlsTimer);
251
+ this.controlsTimer = setTimeout(() => {
252
+ if (this.el && !this.el.matches(':hover')) this.el.classList.add('is-idle');
253
+ }, 2800);
254
+ }
255
+
256
+ handleKey(e) {
257
+ if (!this.el) return;
258
+ const k = e.key;
259
+ if (k === 'Escape') { e.preventDefault(); this.close(); return; }
260
+ if (k === 'ArrowRight' || k === ' ' || k === 'PageDown' || k === 'Enter' || k === 'ArrowDown') {
261
+ e.preventDefault();
262
+ this.next();
263
+ return;
264
+ }
265
+ if (k === 'ArrowLeft' || k === 'PageUp' || k === 'ArrowUp' || k === 'Backspace') {
266
+ e.preventDefault();
267
+ this.prev();
268
+ return;
90
269
  }
270
+ if (k === 'Home') { e.preventDefault(); this.pos = 0; this.render(); return; }
271
+ if (k === 'End') { e.preventDefault(); this.pos = Math.max(0, this.slides.length - 1); this.render(); return; }
272
+ if (k === 'b' || k === 'B' || k === '.') { e.preventDefault(); this.toggleBlack(); return; }
273
+ if (k === 'w' || k === 'W' || k === ',') { e.preventDefault(); this.toggleWhite(); return; }
274
+ if (k === 's' || k === 'S') { e.preventDefault(); this.togglePresenter(); }
91
275
  }
92
276
 
93
277
  close() {
94
- if (this.el) {
95
- document.removeEventListener('keydown', this.onKey, true);
96
- this.el.remove();
97
- this.el = null;
98
- }
278
+ if (!this.el) return;
279
+ clearInterval(this.timer);
280
+ clearTimeout(this.controlsTimer);
281
+ document.removeEventListener('keydown', this.onKey, true);
282
+ document.removeEventListener('fullscreenchange', this.onFsChange);
283
+ window.removeEventListener('resize', this.onResize);
284
+ this.el.remove();
285
+ this.el = null;
286
+ if (document.fullscreenElement) document.exitFullscreen?.().catch(() => {});
287
+ this.fs = false;
288
+ this.black = false;
289
+ this.white = false;
290
+ this.app.ribbon?.refresh();
99
291
  }
100
292
  }