@vertexvis/doc-viewer 1.0.2-testing.0 → 1.0.2-testing.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.
@@ -625,22 +625,10 @@ function apiSupportsLayers(api) {
625
625
  class DocumentLayersController {
626
626
  constructor(api) {
627
627
  this.apiSupportsLayers = false;
628
- if (api != null) {
629
- this.setApi(api);
630
- }
631
- }
632
- setApi(api) {
633
628
  if (apiSupportsLayers(api)) {
634
629
  this.apiSupportsLayers = true;
635
630
  this.api = api;
636
631
  }
637
- else {
638
- this.clear();
639
- }
640
- }
641
- clear() {
642
- this.apiSupportsLayers = false;
643
- this.api = undefined;
644
632
  }
645
633
  /**
646
634
  * Returns a boolean indicating whether the document supports layers.
@@ -675,6 +663,9 @@ class DocumentLayersController {
675
663
  function getElementBoundingClientRect(element) {
676
664
  return element.getBoundingClientRect();
677
665
  }
666
+ function createElement(tagName) {
667
+ return document.createElement(tagName);
668
+ }
678
669
  function getAllVertexElementChildren(element) {
679
670
  return getAllChildren(element)
680
671
  .filter(node => node.nodeName.startsWith('VERTEX-'))
@@ -34557,7 +34548,7 @@ class PdfJsRenderer extends DocumentRenderer {
34557
34548
  this.state = { pages: {}, rendering: false };
34558
34549
  // Render pages using an offscreen canvas to avoid flickering that can occur for
34559
34550
  // PDFs with annotations.
34560
- this.offscreenCanvas = document.createElement('canvas');
34551
+ this.offscreenCanvas = createElement('canvas');
34561
34552
  this.handleStateChanged = this.handleStateChanged.bind(this);
34562
34553
  this.stateChangedDisposable = this.api.onStateChanged(this.handleStateChanged);
34563
34554
  }
@@ -34599,10 +34590,18 @@ class PdfJsRenderer extends DocumentRenderer {
34599
34590
  });
34600
34591
  this.offscreenCanvas.width = this.canvas.width;
34601
34592
  this.offscreenCanvas.height = this.canvas.height;
34593
+ const offscreenCanvasCtx = this.offscreenCanvas.getContext('2d');
34594
+ // Fill the offscreen canvas with a white background prior to rendering the page to give the content
34595
+ // of the PDF a white background, but maintain the canvas it gets drawn to when copying to the main canvas.
34596
+ if (offscreenCanvasCtx != null) {
34597
+ offscreenCanvasCtx.fillStyle = '#ffffff';
34598
+ offscreenCanvasCtx.fillRect(panOffset.x + centerOffsetX, panOffset.y, scaled.width, scaled.height);
34599
+ }
34602
34600
  await page.render({
34603
34601
  canvas: this.offscreenCanvas,
34604
34602
  viewport: scaled,
34605
34603
  intent: 'display',
34604
+ background: 'transparent',
34606
34605
  optionalContentConfigPromise: optionalContentConfig != null ? Promise.resolve(optionalContentConfig) : undefined,
34607
34606
  }).promise;
34608
34607
  // Emit an event before the page is drawn, then draw the page to the canvas on the next
@@ -34632,7 +34631,7 @@ class PdfJsProvider {
34632
34631
  }
34633
34632
  }
34634
34633
 
34635
- const documentViewerCss = () => `:host{display:block;position:relative;width:300px;height:300px;min-width:1px;min-height:1px;user-select:none;-webkit-user-select:none}.viewer-container{width:100%;height:100%;overflow:hidden}.canvas-container{display:flex;justify-content:center;align-items:center;width:100%;height:100%;position:relative}`;
34634
+ const documentViewerCss = () => `:host{display:block;position:relative;width:300px;height:300px;min-width:1px;min-height:1px;user-select:none;-webkit-user-select:none;--viewer-background:#cccccc}.viewer-container{width:100%;height:100%;overflow:hidden}.canvas-container{display:flex;justify-content:center;align-items:center;width:100%;height:100%;position:relative}#document-viewer-canvas{background-color:var(--viewer-background)}`;
34636
34635
 
34637
34636
  const VertexDocumentViewer = class {
34638
34637
  constructor(hostRef) {
@@ -34664,7 +34663,6 @@ const VertexDocumentViewer = class {
34664
34663
  this.handleDocumentApiStateChanged = this.handleDocumentApiStateChanged.bind(this);
34665
34664
  this.handlePageLoaded = this.handlePageLoaded.bind(this);
34666
34665
  this.handlePageDrawn = this.handlePageDrawn.bind(this);
34667
- this.layers = new DocumentLayersController();
34668
34666
  this.resizeObserver = new ResizeObserver(this.handleElementResize);
34669
34667
  this.registerSlotChangeListeners();
34670
34668
  }
@@ -34677,7 +34675,7 @@ const VertexDocumentViewer = class {
34677
34675
  var _a;
34678
34676
  (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.observe(this.hostEl);
34679
34677
  this.updateComponentDimensions();
34680
- this.loadInitialSrc();
34678
+ this.handleSrcChange();
34681
34679
  this.injectViewerApi();
34682
34680
  }
34683
34681
  disconnectedCallback() {
@@ -34744,13 +34742,12 @@ const VertexDocumentViewer = class {
34744
34742
  await documentApi.loadPage(pageNumber);
34745
34743
  }
34746
34744
  async handleSrcChange() {
34747
- var _a;
34748
34745
  if (this.src != null && this.canvasEl != null) {
34749
34746
  this.clearCurrentDocument();
34750
34747
  const { api, renderer } = this.provider.create(this.canvasEl, this.config);
34751
34748
  this.documentApi = api;
34752
34749
  this.documentRenderer = renderer;
34753
- (_a = this.layers) === null || _a === void 0 ? void 0 : _a.setApi(api);
34750
+ this.layers = new DocumentLayersController(api);
34754
34751
  this.updateInteractionHandler();
34755
34752
  this.updateDocumentApiListeners();
34756
34753
  await this.documentApi.updateViewport(this.dimensions);
@@ -34767,9 +34764,9 @@ const VertexDocumentViewer = class {
34767
34764
  this.updateInteractionHandler();
34768
34765
  }
34769
34766
  render() {
34770
- return (index.h(index.Host, { key: 'a990831d5f2328f2cf21be0b82d4018b05decd51' }, index.h("div", { key: '349abae36254da570e640417fab459680c576465', ref: ref => (this.viewerContainerElement = ref), class: "viewer-container", onContextMenu: event => event.preventDefault() }, index.h("div", { key: '6fca3f1d5a2c31bdcc0c8dbbd468e306db981740', ref: ref => (this.canvasContainerElement = ref), class: classNames('canvas-container', {
34767
+ return (index.h(index.Host, { key: 'f1ddf0b4e321bc244a5d757e48f6d71c23caca3c' }, index.h("div", { key: '174c64c537f4b0f1c45fc6543e5b7f54515e4063', ref: ref => (this.viewerContainerElement = ref), class: "viewer-container", onContextMenu: event => event.preventDefault() }, index.h("div", { key: '4c47dadba03b32bde017252a9a8ea7f6e6d323f3', ref: ref => (this.canvasContainerElement = ref), class: classNames('canvas-container', {
34771
34768
  'enable-pointer-events ': window.PointerEvent != null,
34772
- }) }, index.h("canvas", { key: 'fe22d0a0cbe37951495420fb8efe6ca1062c38a7', role: "presentation", ref: el => (this.canvasEl = el) })), index.h("slot", { key: '08c7488d0a52bdb369a5a1b70070f3f15da75e3c' }))));
34769
+ }) }, index.h("canvas", { key: 'ae645889dd5cb93084f820f9f3d14a56de0213f7', id: "document-viewer-canvas", role: "presentation", ref: el => (this.canvasEl = el) })), index.h("slot", { key: '7762c493306bf5d21cd16218f1abd3c6d0300ace' }))));
34773
34770
  }
34774
34771
  getDocumentApi() {
34775
34772
  if (this.documentApi == null) {
@@ -34777,25 +34774,15 @@ const VertexDocumentViewer = class {
34777
34774
  }
34778
34775
  return this.documentApi;
34779
34776
  }
34780
- async loadInitialSrc() {
34781
- await Promise.resolve();
34782
- await this.handleSrcChange();
34783
- }
34784
34777
  clearCurrentDocument() {
34785
- var _a, _b, _c, _d, _e, _f, _g;
34778
+ var _a, _b, _c, _d, _e, _f;
34786
34779
  (_a = this.documentRenderer) === null || _a === void 0 ? void 0 : _a.dispose();
34787
34780
  (_b = this.documentApi) === null || _b === void 0 ? void 0 : _b.dispose();
34788
34781
  (_c = this.panInteractionHandler) === null || _c === void 0 ? void 0 : _c.dispose();
34789
34782
  (_d = this.documentApiStateChangedDisposable) === null || _d === void 0 ? void 0 : _d.dispose();
34790
34783
  (_e = this.pageLoadedDisposable) === null || _e === void 0 ? void 0 : _e.dispose();
34791
34784
  (_f = this.pageDrawnDisposable) === null || _f === void 0 ? void 0 : _f.dispose();
34792
- this.documentRenderer = undefined;
34793
- this.documentApi = undefined;
34794
- this.panInteractionHandler = undefined;
34795
- this.documentApiStateChangedDisposable = undefined;
34796
- this.pageLoadedDisposable = undefined;
34797
- this.pageDrawnDisposable = undefined;
34798
- (_g = this.layers) === null || _g === void 0 ? void 0 : _g.clear();
34785
+ this.layers = undefined;
34799
34786
  }
34800
34787
  handleDocumentApiStateChanged(state) {
34801
34788
  this.documentState = state;
@@ -8,7 +8,8 @@ export class MockOptionalContentConfig extends Map {
8
8
  }
9
9
  }
10
10
  export const mockGetViewport = jest.fn(() => ({ width: 100, height: 100 }));
11
- export const mockPageRender = jest.fn(() => ({ promise: Promise.resolve() }));
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ export const mockPageRender = jest.fn((args) => ({ promise: Promise.resolve() }));
12
13
  export const mockGetPage = jest.fn(() => ({
13
14
  getTextContent: jest.fn(() => ({
14
15
  items: [],
@@ -8,6 +8,13 @@
8
8
 
9
9
  user-select: none;
10
10
  -webkit-user-select: none;
11
+
12
+ /**
13
+ * @prop --viewer-background:
14
+ * A CSS color value used as the background painted behind the loaded PDF,
15
+ * visible in the negative space around the page. Defaults to #cccccc.
16
+ */
17
+ --viewer-background: #cccccc;
11
18
  }
12
19
 
13
20
  .viewer-container {
@@ -24,3 +31,7 @@
24
31
  height: 100%;
25
32
  position: relative;
26
33
  }
34
+
35
+ #document-viewer-canvas {
36
+ background-color: var(--viewer-background);
37
+ }
@@ -34,7 +34,6 @@ export class VertexDocumentViewer {
34
34
  this.handleDocumentApiStateChanged = this.handleDocumentApiStateChanged.bind(this);
35
35
  this.handlePageLoaded = this.handlePageLoaded.bind(this);
36
36
  this.handlePageDrawn = this.handlePageDrawn.bind(this);
37
- this.layers = new DocumentLayersController();
38
37
  this.resizeObserver = new ResizeObserver(this.handleElementResize);
39
38
  this.registerSlotChangeListeners();
40
39
  }
@@ -47,7 +46,7 @@ export class VertexDocumentViewer {
47
46
  var _a;
48
47
  (_a = this.resizeObserver) === null || _a === void 0 ? void 0 : _a.observe(this.hostEl);
49
48
  this.updateComponentDimensions();
50
- this.loadInitialSrc();
49
+ this.handleSrcChange();
51
50
  this.injectViewerApi();
52
51
  }
53
52
  disconnectedCallback() {
@@ -114,13 +113,12 @@ export class VertexDocumentViewer {
114
113
  await documentApi.loadPage(pageNumber);
115
114
  }
116
115
  async handleSrcChange() {
117
- var _a;
118
116
  if (this.src != null && this.canvasEl != null) {
119
117
  this.clearCurrentDocument();
120
118
  const { api, renderer } = this.provider.create(this.canvasEl, this.config);
121
119
  this.documentApi = api;
122
120
  this.documentRenderer = renderer;
123
- (_a = this.layers) === null || _a === void 0 ? void 0 : _a.setApi(api);
121
+ this.layers = new DocumentLayersController(api);
124
122
  this.updateInteractionHandler();
125
123
  this.updateDocumentApiListeners();
126
124
  await this.documentApi.updateViewport(this.dimensions);
@@ -137,9 +135,9 @@ export class VertexDocumentViewer {
137
135
  this.updateInteractionHandler();
138
136
  }
139
137
  render() {
140
- return (h(Host, { key: 'a990831d5f2328f2cf21be0b82d4018b05decd51' }, h("div", { key: '349abae36254da570e640417fab459680c576465', ref: ref => (this.viewerContainerElement = ref), class: "viewer-container", onContextMenu: event => event.preventDefault() }, h("div", { key: '6fca3f1d5a2c31bdcc0c8dbbd468e306db981740', ref: ref => (this.canvasContainerElement = ref), class: classNames('canvas-container', {
138
+ return (h(Host, { key: 'f1ddf0b4e321bc244a5d757e48f6d71c23caca3c' }, h("div", { key: '174c64c537f4b0f1c45fc6543e5b7f54515e4063', ref: ref => (this.viewerContainerElement = ref), class: "viewer-container", onContextMenu: event => event.preventDefault() }, h("div", { key: '4c47dadba03b32bde017252a9a8ea7f6e6d323f3', ref: ref => (this.canvasContainerElement = ref), class: classNames('canvas-container', {
141
139
  'enable-pointer-events ': window.PointerEvent != null,
142
- }) }, h("canvas", { key: 'fe22d0a0cbe37951495420fb8efe6ca1062c38a7', role: "presentation", ref: el => (this.canvasEl = el) })), h("slot", { key: '08c7488d0a52bdb369a5a1b70070f3f15da75e3c' }))));
140
+ }) }, h("canvas", { key: 'ae645889dd5cb93084f820f9f3d14a56de0213f7', id: "document-viewer-canvas", role: "presentation", ref: el => (this.canvasEl = el) })), h("slot", { key: '7762c493306bf5d21cd16218f1abd3c6d0300ace' }))));
143
141
  }
144
142
  getDocumentApi() {
145
143
  if (this.documentApi == null) {
@@ -147,25 +145,15 @@ export class VertexDocumentViewer {
147
145
  }
148
146
  return this.documentApi;
149
147
  }
150
- async loadInitialSrc() {
151
- await Promise.resolve();
152
- await this.handleSrcChange();
153
- }
154
148
  clearCurrentDocument() {
155
- var _a, _b, _c, _d, _e, _f, _g;
149
+ var _a, _b, _c, _d, _e, _f;
156
150
  (_a = this.documentRenderer) === null || _a === void 0 ? void 0 : _a.dispose();
157
151
  (_b = this.documentApi) === null || _b === void 0 ? void 0 : _b.dispose();
158
152
  (_c = this.panInteractionHandler) === null || _c === void 0 ? void 0 : _c.dispose();
159
153
  (_d = this.documentApiStateChangedDisposable) === null || _d === void 0 ? void 0 : _d.dispose();
160
154
  (_e = this.pageLoadedDisposable) === null || _e === void 0 ? void 0 : _e.dispose();
161
155
  (_f = this.pageDrawnDisposable) === null || _f === void 0 ? void 0 : _f.dispose();
162
- this.documentRenderer = undefined;
163
- this.documentApi = undefined;
164
- this.panInteractionHandler = undefined;
165
- this.documentApiStateChangedDisposable = undefined;
166
- this.pageLoadedDisposable = undefined;
167
- this.pageDrawnDisposable = undefined;
168
- (_g = this.layers) === null || _g === void 0 ? void 0 : _g.clear();
156
+ this.layers = undefined;
169
157
  }
170
158
  handleDocumentApiStateChanged(state) {
171
159
  this.documentState = state;
@@ -5,22 +5,10 @@ import { apiSupportsLayers } from "./types";
5
5
  export class DocumentLayersController {
6
6
  constructor(api) {
7
7
  this.apiSupportsLayers = false;
8
- if (api != null) {
9
- this.setApi(api);
10
- }
11
- }
12
- setApi(api) {
13
8
  if (apiSupportsLayers(api)) {
14
9
  this.apiSupportsLayers = true;
15
10
  this.api = api;
16
11
  }
17
- else {
18
- this.clear();
19
- }
20
- }
21
- clear() {
22
- this.apiSupportsLayers = false;
23
- this.api = undefined;
24
12
  }
25
13
  /**
26
14
  * Returns a boolean indicating whether the document supports layers.
@@ -4,6 +4,9 @@
4
4
  export function getElementBoundingClientRect(element) {
5
5
  return element.getBoundingClientRect();
6
6
  }
7
+ export function createElement(tagName) {
8
+ return document.createElement(tagName);
9
+ }
7
10
  export function getAllVertexElementChildren(element) {
8
11
  return getAllChildren(element)
9
12
  .filter(node => node.nodeName.startsWith('VERTEX-'))
@@ -3,13 +3,14 @@
3
3
  */
4
4
  import { Dimensions } from "@vertexvis/geometry";
5
5
  import { DocumentRenderer } from "../document/renderer";
6
+ import { createElement } from "../dom";
6
7
  export class PdfJsRenderer extends DocumentRenderer {
7
8
  constructor(api, canvas) {
8
9
  super(api, canvas);
9
10
  this.state = { pages: {}, rendering: false };
10
11
  // Render pages using an offscreen canvas to avoid flickering that can occur for
11
12
  // PDFs with annotations.
12
- this.offscreenCanvas = document.createElement('canvas');
13
+ this.offscreenCanvas = createElement('canvas');
13
14
  this.handleStateChanged = this.handleStateChanged.bind(this);
14
15
  this.stateChangedDisposable = this.api.onStateChanged(this.handleStateChanged);
15
16
  }
@@ -51,10 +52,18 @@ export class PdfJsRenderer extends DocumentRenderer {
51
52
  });
52
53
  this.offscreenCanvas.width = this.canvas.width;
53
54
  this.offscreenCanvas.height = this.canvas.height;
55
+ const offscreenCanvasCtx = this.offscreenCanvas.getContext('2d');
56
+ // Fill the offscreen canvas with a white background prior to rendering the page to give the content
57
+ // of the PDF a white background, but maintain the canvas it gets drawn to when copying to the main canvas.
58
+ if (offscreenCanvasCtx != null) {
59
+ offscreenCanvasCtx.fillStyle = '#ffffff';
60
+ offscreenCanvasCtx.fillRect(panOffset.x + centerOffsetX, panOffset.y, scaled.width, scaled.height);
61
+ }
54
62
  await page.render({
55
63
  canvas: this.offscreenCanvas,
56
64
  viewport: scaled,
57
65
  intent: 'display',
66
+ background: 'transparent',
58
67
  optionalContentConfigPromise: optionalContentConfig != null ? Promise.resolve(optionalContentConfig) : undefined,
59
68
  }).promise;
60
69
  // Emit an event before the page is drawn, then draw the page to the canvas on the next