pdf_single_page_viewer.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PDFSinglePageViewer = void 0;
  27. var _base_viewer = require("./base_viewer.js");
  28. var _pdf = require("../pdf");
  29. class PDFSinglePageViewer extends _base_viewer.BaseViewer {
  30. constructor(options) {
  31. super(options);
  32. this.eventBus._on("pagesinit", evt => {
  33. this._ensurePageViewVisible();
  34. });
  35. }
  36. get _viewerElement() {
  37. return (0, _pdf.shadow)(this, "_viewerElement", this._shadowViewer);
  38. }
  39. _resetView() {
  40. super._resetView();
  41. this._previousPageNumber = 1;
  42. this._shadowViewer = document.createDocumentFragment();
  43. this._updateScrollDown = null;
  44. }
  45. _ensurePageViewVisible() {
  46. const pageView = this._pages[this._currentPageNumber - 1];
  47. const previousPageView = this._pages[this._previousPageNumber - 1];
  48. const viewerNodes = this.viewer.childNodes;
  49. switch (viewerNodes.length) {
  50. case 0:
  51. this.viewer.appendChild(pageView.div);
  52. break;
  53. case 1:
  54. if (viewerNodes[0] !== previousPageView.div) {
  55. throw new Error("_ensurePageViewVisible: Unexpected previously visible page.");
  56. }
  57. if (pageView === previousPageView) {
  58. break;
  59. }
  60. this._shadowViewer.appendChild(previousPageView.div);
  61. this.viewer.appendChild(pageView.div);
  62. this.container.scrollTop = 0;
  63. break;
  64. default:
  65. throw new Error("_ensurePageViewVisible: Only one page should be visible at a time.");
  66. }
  67. this._previousPageNumber = this._currentPageNumber;
  68. }
  69. _scrollUpdate() {
  70. if (this._updateScrollDown) {
  71. this._updateScrollDown();
  72. }
  73. super._scrollUpdate();
  74. }
  75. _scrollIntoView({
  76. pageDiv,
  77. pageSpot = null,
  78. pageNumber = null
  79. }) {
  80. if (pageNumber) {
  81. this._setCurrentPageNumber(pageNumber);
  82. }
  83. const scrolledDown = this._currentPageNumber >= this._previousPageNumber;
  84. this._ensurePageViewVisible();
  85. this.update();
  86. super._scrollIntoView({
  87. pageDiv,
  88. pageSpot,
  89. pageNumber
  90. });
  91. this._updateScrollDown = () => {
  92. this.scroll.down = scrolledDown;
  93. this._updateScrollDown = null;
  94. };
  95. }
  96. _getVisiblePages() {
  97. return this._getCurrentVisiblePage();
  98. }
  99. _updateHelper(visiblePages) {}
  100. get _isScrollModeHorizontal() {
  101. return (0, _pdf.shadow)(this, "_isScrollModeHorizontal", false);
  102. }
  103. _updateScrollMode() {}
  104. _updateSpreadMode() {}
  105. }
  106. exports.PDFSinglePageViewer = PDFSinglePageViewer;