@netless/app-slide 0.2.22 → 0.2.24

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@netless/app-slide",
3
- "version": "0.2.22",
3
+ "version": "0.2.24",
4
4
  "main": "dist/main.cjs.js",
5
5
  "module": "dist/main.es.js",
6
6
  "types": "dist/index.d.ts",
@@ -10,12 +10,21 @@
10
10
  "README-zh.md"
11
11
  ],
12
12
  "devDependencies": {
13
- "@netless/slide": "^0.8.4",
13
+ "jspdf": "2.5.1",
14
+ "@netless/slide": "^0.8.5",
14
15
  "@types/color-string": "^1.5.2",
15
16
  "color-string": "^1.9.1",
16
17
  "side-effect-manager": "^1.2.1",
17
18
  "vanilla-lazyload": "^17.8.3"
18
19
  },
20
+ "peerDependencies": {
21
+ "jspdf": "2.5.1"
22
+ },
23
+ "peerDependenciesMeta": {
24
+ "jspdf": {
25
+ "optional": true
26
+ }
27
+ },
19
28
  "scripts": {
20
29
  "types": "cross-env NODE_ENV=production tsc --declaration --emitDeclarationOnly --outDir dist",
21
30
  "build": "vite build && npm run types",
@@ -17,6 +17,13 @@ export interface SlideDocsViewerConfig {
17
17
  view: View;
18
18
  mountSlideController: (options: MountSlideOptions) => SlideController;
19
19
  mountWhiteboard: (dom: HTMLDivElement) => void;
20
+ baseScenePath: string;
21
+ appId: string;
22
+ }
23
+
24
+ export interface SavePdfConfig {
25
+ appId: string;
26
+ type: "@netless/_request_save_pdf_";
20
27
  }
21
28
 
22
29
  export class SlideDocsViewer {
@@ -27,14 +34,24 @@ export class SlideDocsViewer {
27
34
  protected readonly whiteboardView: SlideDocsViewerConfig["view"];
28
35
  protected readonly mountSlideController: SlideDocsViewerConfig["mountSlideController"];
29
36
  protected readonly mountWhiteboard: SlideDocsViewerConfig["mountWhiteboard"];
37
+ private readonly baseScenePath: string;
38
+ private readonly appId: string;
30
39
  private isViewMounted = false;
31
40
 
32
- public constructor({ box, view, mountSlideController, mountWhiteboard }: SlideDocsViewerConfig) {
41
+ public constructor({
42
+ box,
43
+ view,
44
+ mountSlideController,
45
+ mountWhiteboard,
46
+ baseScenePath,
47
+ appId,
48
+ }: SlideDocsViewerConfig) {
33
49
  this.box = box;
34
50
  this.whiteboardView = view;
35
51
  this.mountSlideController = mountSlideController;
36
52
  this.mountWhiteboard = mountWhiteboard;
37
-
53
+ this.baseScenePath = baseScenePath;
54
+ this.appId = appId;
38
55
  this.viewer = new DocsViewer({
39
56
  readonly: box.readonly,
40
57
  onNewPageIndex: this.onNewPageIndex,
@@ -48,6 +65,17 @@ export class SlideDocsViewer {
48
65
  box.events.on("readonly", handler);
49
66
  return () => box.events.off("readonly", handler);
50
67
  });
68
+ this.sideEffect.add(() => {
69
+ const handleDownloadPdf = (evt: MessageEvent<SavePdfConfig>) => {
70
+ if (evt.data.type === "@netless/_request_save_pdf_" && evt.data.appId === this.appId) {
71
+ this.toPdf().catch(() => this.reportProgress(100, null));
72
+ }
73
+ };
74
+ window.addEventListener("message", handleDownloadPdf);
75
+ return () => {
76
+ window.removeEventListener("message", handleDownloadPdf);
77
+ };
78
+ });
51
79
 
52
80
  this.render();
53
81
  }
@@ -230,4 +258,116 @@ export class SlideDocsViewer {
230
258
  }
231
259
 
232
260
  protected namespace = "netless-app-slide";
261
+
262
+ private getWhiteSnapshot(
263
+ pageIndex: number,
264
+ canvas: HTMLCanvasElement,
265
+ ctx: CanvasRenderingContext2D
266
+ ) {
267
+ const { width, height } = this.whiteboardView.size;
268
+ canvas.width = width;
269
+ canvas.height = height;
270
+ this.whiteboardView.screenshotToCanvas(
271
+ ctx,
272
+ `${this.baseScenePath}/${pageIndex}`,
273
+ width,
274
+ height,
275
+ {
276
+ centerX: 0,
277
+ centerY: 0,
278
+ scale: this.whiteboardView.camera.scale,
279
+ }
280
+ );
281
+ }
282
+
283
+ private reportProgress(progress: number, result: { pdf: ArrayBuffer; title: string } | null) {
284
+ window.postMessage({
285
+ type: "@netless/_result_save_pdf_",
286
+ appId: this.appId,
287
+ progress,
288
+ result,
289
+ });
290
+ }
291
+
292
+ private toPdf = async () => {
293
+ if (!this.slideController) {
294
+ this.reportProgress(100, null);
295
+ return;
296
+ }
297
+ const { slide } = this.slideController;
298
+ const MAX = 1920;
299
+ const resizeCanvas = document.createElement("canvas");
300
+ const resizeCtx = resizeCanvas.getContext("2d");
301
+ const { slideCount, width, height } = slide;
302
+ let pdfWidth = Math.floor(width);
303
+ let pdfHeight = Math.floor(height);
304
+ if (pdfWidth > MAX) {
305
+ pdfWidth = MAX;
306
+ pdfHeight = Math.floor((height * pdfWidth) / width);
307
+ }
308
+ if (pdfHeight > MAX) {
309
+ pdfHeight = MAX;
310
+ pdfWidth = Math.floor((width * pdfHeight) / height);
311
+ }
312
+ resizeCanvas.width = pdfWidth;
313
+ resizeCanvas.height = pdfHeight;
314
+
315
+ const whiteSnapshotCanvas = document.createElement("canvas");
316
+ whiteSnapshotCanvas.width = width;
317
+ whiteSnapshotCanvas.height = height;
318
+ const whiteCtx = whiteSnapshotCanvas.getContext("2d");
319
+ if (!whiteCtx || !this.getWhiteSnapshot || !resizeCtx) {
320
+ this.reportProgress(100, null);
321
+ return null;
322
+ }
323
+
324
+ const orientation = pdfWidth > pdfHeight ? "l" : "p";
325
+ const { jsPDF } = await import("jspdf");
326
+ const pdf = new jsPDF({
327
+ format: [pdfWidth, pdfHeight],
328
+ orientation,
329
+ compress: true,
330
+ });
331
+
332
+ for (let i = 1; i <= slideCount; i++) {
333
+ let slideSnapshot = null;
334
+ try {
335
+ slideSnapshot = await this.slideController.slide.snapshotWithTimingEnd(i);
336
+ } catch {
337
+ // ignore
338
+ }
339
+
340
+ if (slideSnapshot) {
341
+ const img = document.createElement("img");
342
+ img.src = slideSnapshot;
343
+ await new Promise(resolve => (img.onload = resolve));
344
+ resizeCtx.drawImage(img, 0, 0, pdfWidth, pdfHeight);
345
+ }
346
+ whiteCtx.clearRect(0, 0, width, height);
347
+ this.getWhiteSnapshot(i, whiteSnapshotCanvas, whiteCtx);
348
+ try {
349
+ const whiteSnapshot = whiteSnapshotCanvas.toDataURL("image/png");
350
+ const whiteImg = document.createElement("img");
351
+ whiteImg.src = whiteSnapshot;
352
+ await new Promise(resolve => (whiteImg.onload = resolve));
353
+ resizeCtx.drawImage(whiteImg, 0, 0, pdfWidth, pdfHeight);
354
+ } catch (e) {
355
+ // ignore
356
+ }
357
+
358
+ const outputDataUrl = resizeCanvas.toDataURL("image/jpeg", 0.6);
359
+ if (i > 1) {
360
+ pdf.addPage();
361
+ }
362
+ pdf.addImage(outputDataUrl, "JPEG", 0, 0, pdfWidth, pdfHeight, "", "FAST");
363
+ resizeCtx.clearRect(0, 0, pdfWidth, pdfHeight);
364
+ const progress = Math.ceil((i / slideCount) * 100);
365
+ if (progress < 100) {
366
+ this.reportProgress(Math.ceil((i / slideCount) * 100), null);
367
+ }
368
+ }
369
+ const dataUrl = pdf.output("arraybuffer");
370
+ const title = this.box.title;
371
+ this.reportProgress(100, { pdf: dataUrl, title });
372
+ };
233
373
  }
package/src/index.ts CHANGED
@@ -167,6 +167,8 @@ const SlideApp: NetlessApp<Attributes, MagixEvents, AppOptions, AppResult> = {
167
167
  view,
168
168
  mountSlideController,
169
169
  mountWhiteboard: context.mountView.bind(context),
170
+ baseScenePath,
171
+ appId: context.appId,
170
172
  });
171
173
 
172
174
  if (import.meta.env.DEV) {