@file-viewer/pptx 2.2.3 → 2.2.4

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/dist/viewer.d.ts CHANGED
@@ -20,6 +20,7 @@ export declare class PptxViewer {
20
20
  private slideWindowListeners;
21
21
  private slideWindowFrame;
22
22
  private charts;
23
+ private readonly mediaRecords;
23
24
  private disposed;
24
25
  private completed;
25
26
  private readonly handleSlideWindowChange;
@@ -59,6 +60,9 @@ export declare class PptxViewer {
59
60
  cloneForExport(): Promise<HTMLElement>;
60
61
  private complete;
61
62
  private fail;
63
+ private storeMedia;
64
+ private hydrateMedia;
65
+ private releaseMedia;
62
66
  private attachResizeObserver;
63
67
  private scheduleResize;
64
68
  private resize;
package/dist/viewer.js CHANGED
@@ -100,6 +100,7 @@ export class PptxViewer {
100
100
  this.slideWindowListeners = [];
101
101
  this.slideWindowFrame = 0;
102
102
  this.charts = null;
103
+ this.mediaRecords = new Map();
103
104
  this.disposed = false;
104
105
  this.completed = false;
105
106
  this.handleSlideWindowChange = () => this.scheduleSlideWindowUpdate();
@@ -139,6 +140,7 @@ export class PptxViewer {
139
140
  var _a, _b, _c, _d;
140
141
  this.disposed = true;
141
142
  this.previewThumbnailDataUrl = null;
143
+ this.releaseMedia();
142
144
  (_a = this.worker) === null || _a === void 0 ? void 0 : _a.terminate();
143
145
  this.worker = null;
144
146
  (_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
@@ -155,6 +157,7 @@ export class PptxViewer {
155
157
  startWorker() {
156
158
  var _a;
157
159
  (_a = this.worker) === null || _a === void 0 ? void 0 : _a.terminate();
160
+ this.releaseMedia();
158
161
  this.completed = false;
159
162
  this.charts = null;
160
163
  this.previewThumbnailDataUrl = null;
@@ -195,11 +198,15 @@ export class PptxViewer {
195
198
  }
196
199
  else {
197
200
  const element = appendHtml(this.content, String(message.data || ''));
201
+ this.hydrateMedia(element);
198
202
  this.scheduleResize();
199
203
  (_b = (_a = this.options).onSlideRendered) === null || _b === void 0 ? void 0 : _b.call(_a, Number(message.slide_num || 0), element);
200
204
  }
201
205
  break;
202
206
  }
207
+ case 'media':
208
+ this.storeMedia(message.data);
209
+ break;
203
210
  case 'slide-error': {
204
211
  const slideNumber = Number(message.slide_num || 0);
205
212
  const error = message.data;
@@ -333,6 +340,7 @@ export class PptxViewer {
333
340
  const designerCanvases = this.getDesignerCanvases(record);
334
341
  record.slot.replaceChildren();
335
342
  record.element = appendHtml(record.slot, record.html);
343
+ this.hydrateMedia(record.element);
336
344
  record.slot.append(...designerCanvases);
337
345
  record.rendered = true;
338
346
  record.postProcessed = false;
@@ -603,9 +611,76 @@ export class PptxViewer {
603
611
  this.completed = true;
604
612
  (_a = this.worker) === null || _a === void 0 ? void 0 : _a.terminate();
605
613
  this.worker = null;
614
+ this.releaseMedia();
606
615
  this.content.dataset.renderState = 'error';
607
616
  (_c = (_b = this.options).onError) === null || _c === void 0 ? void 0 : _c.call(_b, error);
608
617
  }
618
+ storeMedia(data) {
619
+ if (!data || typeof data !== 'object') {
620
+ return;
621
+ }
622
+ const media = data;
623
+ if (typeof media.id !== 'string' ||
624
+ !media.id ||
625
+ typeof media.mimeType !== 'string' ||
626
+ !(media.buffer instanceof ArrayBuffer)) {
627
+ return;
628
+ }
629
+ this.releaseMedia(media.id);
630
+ this.mediaRecords.set(media.id, {
631
+ buffer: media.buffer,
632
+ mimeType: media.mimeType,
633
+ objectUrl: null,
634
+ revokeObjectUrl: null,
635
+ });
636
+ this.hydrateMedia(this.content);
637
+ }
638
+ hydrateMedia(root) {
639
+ var _a;
640
+ if (!root) {
641
+ return;
642
+ }
643
+ const elements = Array.from(root.querySelectorAll('[data-pptx-media-id]'));
644
+ if (root.matches('[data-pptx-media-id]')) {
645
+ elements.unshift(root);
646
+ }
647
+ for (const element of elements) {
648
+ const mediaId = element.dataset.pptxMediaId;
649
+ const record = mediaId ? this.mediaRecords.get(mediaId) : undefined;
650
+ if (!record) {
651
+ continue;
652
+ }
653
+ if (!record.objectUrl) {
654
+ const view = this.target.ownerDocument.defaultView;
655
+ const BlobCtor = (view === null || view === void 0 ? void 0 : view.Blob) || globalThis.Blob;
656
+ const UrlApi = (view === null || view === void 0 ? void 0 : view.URL) || globalThis.URL;
657
+ if (typeof BlobCtor !== 'function' ||
658
+ typeof (UrlApi === null || UrlApi === void 0 ? void 0 : UrlApi.createObjectURL) !== 'function') {
659
+ continue;
660
+ }
661
+ const objectUrl = UrlApi.createObjectURL(new BlobCtor([record.buffer], { type: record.mimeType }));
662
+ record.objectUrl = objectUrl;
663
+ record.revokeObjectUrl = () => UrlApi.revokeObjectURL(objectUrl);
664
+ }
665
+ if (element.src !== record.objectUrl) {
666
+ element.src = record.objectUrl;
667
+ (_a = element.load) === null || _a === void 0 ? void 0 : _a.call(element);
668
+ }
669
+ }
670
+ }
671
+ releaseMedia(mediaId) {
672
+ var _a, _b;
673
+ if (mediaId) {
674
+ const record = this.mediaRecords.get(mediaId);
675
+ (_a = record === null || record === void 0 ? void 0 : record.revokeObjectUrl) === null || _a === void 0 ? void 0 : _a.call(record);
676
+ this.mediaRecords.delete(mediaId);
677
+ return;
678
+ }
679
+ for (const record of this.mediaRecords.values()) {
680
+ (_b = record.revokeObjectUrl) === null || _b === void 0 ? void 0 : _b.call(record);
681
+ }
682
+ this.mediaRecords.clear();
683
+ }
609
684
  attachResizeObserver() {
610
685
  if (typeof ResizeObserver === 'undefined') {
611
686
  return;