pdf_single_page_viewer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. get _pageWidthScaleFactor() {
  40. return 1;
  41. }
  42. _resetView() {
  43. super._resetView();
  44. this._previousPageNumber = 1;
  45. this._shadowViewer = document.createDocumentFragment();
  46. this._updateScrollDown = null;
  47. }
  48. _ensurePageViewVisible() {
  49. const pageView = this._pages[this._currentPageNumber - 1];
  50. const previousPageView = this._pages[this._previousPageNumber - 1];
  51. const viewerNodes = this.viewer.childNodes;
  52. switch (viewerNodes.length) {
  53. case 0:
  54. this.viewer.appendChild(pageView.div);
  55. break;
  56. case 1:
  57. if (viewerNodes[0] !== previousPageView.div) {
  58. throw new Error("_ensurePageViewVisible: Unexpected previously visible page.");
  59. }
  60. if (pageView === previousPageView) {
  61. break;
  62. }
  63. this._shadowViewer.appendChild(previousPageView.div);
  64. this.viewer.appendChild(pageView.div);
  65. this.container.scrollTop = 0;
  66. break;
  67. default:
  68. throw new Error("_ensurePageViewVisible: Only one page should be visible at a time.");
  69. }
  70. this._previousPageNumber = this._currentPageNumber;
  71. }
  72. _scrollUpdate() {
  73. if (this._updateScrollDown) {
  74. this._updateScrollDown();
  75. }
  76. super._scrollUpdate();
  77. }
  78. _scrollIntoView({
  79. pageDiv,
  80. pageSpot = null,
  81. pageNumber = null
  82. }) {
  83. if (pageNumber) {
  84. this._setCurrentPageNumber(pageNumber);
  85. }
  86. const scrolledDown = this._currentPageNumber >= this._previousPageNumber;
  87. this._ensurePageViewVisible();
  88. this.update();
  89. super._scrollIntoView({
  90. pageDiv,
  91. pageSpot,
  92. pageNumber
  93. });
  94. this._updateScrollDown = () => {
  95. this.scroll.down = scrolledDown;
  96. this._updateScrollDown = null;
  97. };
  98. }
  99. _getVisiblePages() {
  100. return this._getCurrentVisiblePage();
  101. }
  102. _updateHelper(visiblePages) {}
  103. get _isScrollModeHorizontal() {
  104. return (0, _pdf.shadow)(this, "_isScrollModeHorizontal", false);
  105. }
  106. _updateScrollMode() {}
  107. _updateSpreadMode() {}
  108. _getPageAdvance() {
  109. return 1;
  110. }
  111. }
  112. exports.PDFSinglePageViewer = PDFSinglePageViewer;