pdf_presentation_mode.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.PDFPresentationMode = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. const DELAY_BEFORE_HIDING_CONTROLS = 3000;
  29. const ACTIVE_SELECTOR = "pdfPresentationMode";
  30. const CONTROLS_SELECTOR = "pdfPresentationModeControls";
  31. const MOUSE_SCROLL_COOLDOWN_TIME = 50;
  32. const PAGE_SWITCH_THRESHOLD = 0.1;
  33. const SWIPE_MIN_DISTANCE_THRESHOLD = 50;
  34. const SWIPE_ANGLE_THRESHOLD = Math.PI / 6;
  35. class PDFPresentationMode {
  36. #state = _ui_utils.PresentationModeState.UNKNOWN;
  37. #args = null;
  38. constructor({
  39. container,
  40. pdfViewer,
  41. eventBus
  42. }) {
  43. this.container = container;
  44. this.pdfViewer = pdfViewer;
  45. this.eventBus = eventBus;
  46. this.contextMenuOpen = false;
  47. this.mouseScrollTimeStamp = 0;
  48. this.mouseScrollDelta = 0;
  49. this.touchSwipeState = null;
  50. }
  51. async request() {
  52. const {
  53. container,
  54. pdfViewer
  55. } = this;
  56. if (this.active || !pdfViewer.pagesCount || !container.requestFullscreen) {
  57. return false;
  58. }
  59. this.#addFullscreenChangeListeners();
  60. this.#notifyStateChange(_ui_utils.PresentationModeState.CHANGING);
  61. const promise = container.requestFullscreen();
  62. this.#args = {
  63. pageNumber: pdfViewer.currentPageNumber,
  64. scaleValue: pdfViewer.currentScaleValue,
  65. scrollMode: pdfViewer.scrollMode,
  66. spreadMode: null
  67. };
  68. if (pdfViewer.spreadMode !== _ui_utils.SpreadMode.NONE && !(pdfViewer.pageViewsReady && pdfViewer.hasEqualPageSizes)) {
  69. console.warn("Ignoring Spread modes when entering PresentationMode, " + "since the document may contain varying page sizes.");
  70. this.#args.spreadMode = pdfViewer.spreadMode;
  71. }
  72. try {
  73. await promise;
  74. return true;
  75. } catch (reason) {
  76. this.#removeFullscreenChangeListeners();
  77. this.#notifyStateChange(_ui_utils.PresentationModeState.NORMAL);
  78. }
  79. return false;
  80. }
  81. get active() {
  82. return this.#state === _ui_utils.PresentationModeState.CHANGING || this.#state === _ui_utils.PresentationModeState.FULLSCREEN;
  83. }
  84. #mouseWheel(evt) {
  85. if (!this.active) {
  86. return;
  87. }
  88. evt.preventDefault();
  89. const delta = (0, _ui_utils.normalizeWheelEventDelta)(evt);
  90. const currentTime = Date.now();
  91. const storedTime = this.mouseScrollTimeStamp;
  92. if (currentTime > storedTime && currentTime - storedTime < MOUSE_SCROLL_COOLDOWN_TIME) {
  93. return;
  94. }
  95. if (this.mouseScrollDelta > 0 && delta < 0 || this.mouseScrollDelta < 0 && delta > 0) {
  96. this.#resetMouseScrollState();
  97. }
  98. this.mouseScrollDelta += delta;
  99. if (Math.abs(this.mouseScrollDelta) >= PAGE_SWITCH_THRESHOLD) {
  100. const totalDelta = this.mouseScrollDelta;
  101. this.#resetMouseScrollState();
  102. const success = totalDelta > 0 ? this.pdfViewer.previousPage() : this.pdfViewer.nextPage();
  103. if (success) {
  104. this.mouseScrollTimeStamp = currentTime;
  105. }
  106. }
  107. }
  108. #notifyStateChange(state) {
  109. this.#state = state;
  110. this.eventBus.dispatch("presentationmodechanged", {
  111. source: this,
  112. state
  113. });
  114. }
  115. #enter() {
  116. this.#notifyStateChange(_ui_utils.PresentationModeState.FULLSCREEN);
  117. this.container.classList.add(ACTIVE_SELECTOR);
  118. setTimeout(() => {
  119. this.pdfViewer.scrollMode = _ui_utils.ScrollMode.PAGE;
  120. if (this.#args.spreadMode !== null) {
  121. this.pdfViewer.spreadMode = _ui_utils.SpreadMode.NONE;
  122. }
  123. this.pdfViewer.currentPageNumber = this.#args.pageNumber;
  124. this.pdfViewer.currentScaleValue = "page-fit";
  125. }, 0);
  126. this.#addWindowListeners();
  127. this.#showControls();
  128. this.contextMenuOpen = false;
  129. window.getSelection().removeAllRanges();
  130. }
  131. #exit() {
  132. const pageNumber = this.pdfViewer.currentPageNumber;
  133. this.container.classList.remove(ACTIVE_SELECTOR);
  134. setTimeout(() => {
  135. this.#removeFullscreenChangeListeners();
  136. this.#notifyStateChange(_ui_utils.PresentationModeState.NORMAL);
  137. this.pdfViewer.scrollMode = this.#args.scrollMode;
  138. if (this.#args.spreadMode !== null) {
  139. this.pdfViewer.spreadMode = this.#args.spreadMode;
  140. }
  141. this.pdfViewer.currentScaleValue = this.#args.scaleValue;
  142. this.pdfViewer.currentPageNumber = pageNumber;
  143. this.#args = null;
  144. }, 0);
  145. this.#removeWindowListeners();
  146. this.#hideControls();
  147. this.#resetMouseScrollState();
  148. this.contextMenuOpen = false;
  149. }
  150. #mouseDown(evt) {
  151. if (this.contextMenuOpen) {
  152. this.contextMenuOpen = false;
  153. evt.preventDefault();
  154. return;
  155. }
  156. if (evt.button === 0) {
  157. const isInternalLink = evt.target.href && evt.target.classList.contains("internalLink");
  158. if (!isInternalLink) {
  159. evt.preventDefault();
  160. if (evt.shiftKey) {
  161. this.pdfViewer.previousPage();
  162. } else {
  163. this.pdfViewer.nextPage();
  164. }
  165. }
  166. }
  167. }
  168. #contextMenu() {
  169. this.contextMenuOpen = true;
  170. }
  171. #showControls() {
  172. if (this.controlsTimeout) {
  173. clearTimeout(this.controlsTimeout);
  174. } else {
  175. this.container.classList.add(CONTROLS_SELECTOR);
  176. }
  177. this.controlsTimeout = setTimeout(() => {
  178. this.container.classList.remove(CONTROLS_SELECTOR);
  179. delete this.controlsTimeout;
  180. }, DELAY_BEFORE_HIDING_CONTROLS);
  181. }
  182. #hideControls() {
  183. if (!this.controlsTimeout) {
  184. return;
  185. }
  186. clearTimeout(this.controlsTimeout);
  187. this.container.classList.remove(CONTROLS_SELECTOR);
  188. delete this.controlsTimeout;
  189. }
  190. #resetMouseScrollState() {
  191. this.mouseScrollTimeStamp = 0;
  192. this.mouseScrollDelta = 0;
  193. }
  194. #touchSwipe(evt) {
  195. if (!this.active) {
  196. return;
  197. }
  198. if (evt.touches.length > 1) {
  199. this.touchSwipeState = null;
  200. return;
  201. }
  202. switch (evt.type) {
  203. case "touchstart":
  204. this.touchSwipeState = {
  205. startX: evt.touches[0].pageX,
  206. startY: evt.touches[0].pageY,
  207. endX: evt.touches[0].pageX,
  208. endY: evt.touches[0].pageY
  209. };
  210. break;
  211. case "touchmove":
  212. if (this.touchSwipeState === null) {
  213. return;
  214. }
  215. this.touchSwipeState.endX = evt.touches[0].pageX;
  216. this.touchSwipeState.endY = evt.touches[0].pageY;
  217. evt.preventDefault();
  218. break;
  219. case "touchend":
  220. if (this.touchSwipeState === null) {
  221. return;
  222. }
  223. let delta = 0;
  224. const dx = this.touchSwipeState.endX - this.touchSwipeState.startX;
  225. const dy = this.touchSwipeState.endY - this.touchSwipeState.startY;
  226. const absAngle = Math.abs(Math.atan2(dy, dx));
  227. if (Math.abs(dx) > SWIPE_MIN_DISTANCE_THRESHOLD && (absAngle <= SWIPE_ANGLE_THRESHOLD || absAngle >= Math.PI - SWIPE_ANGLE_THRESHOLD)) {
  228. delta = dx;
  229. } else if (Math.abs(dy) > SWIPE_MIN_DISTANCE_THRESHOLD && Math.abs(absAngle - Math.PI / 2) <= SWIPE_ANGLE_THRESHOLD) {
  230. delta = dy;
  231. }
  232. if (delta > 0) {
  233. this.pdfViewer.previousPage();
  234. } else if (delta < 0) {
  235. this.pdfViewer.nextPage();
  236. }
  237. break;
  238. }
  239. }
  240. #addWindowListeners() {
  241. this.showControlsBind = this.#showControls.bind(this);
  242. this.mouseDownBind = this.#mouseDown.bind(this);
  243. this.mouseWheelBind = this.#mouseWheel.bind(this);
  244. this.resetMouseScrollStateBind = this.#resetMouseScrollState.bind(this);
  245. this.contextMenuBind = this.#contextMenu.bind(this);
  246. this.touchSwipeBind = this.#touchSwipe.bind(this);
  247. window.addEventListener("mousemove", this.showControlsBind);
  248. window.addEventListener("mousedown", this.mouseDownBind);
  249. window.addEventListener("wheel", this.mouseWheelBind, {
  250. passive: false
  251. });
  252. window.addEventListener("keydown", this.resetMouseScrollStateBind);
  253. window.addEventListener("contextmenu", this.contextMenuBind);
  254. window.addEventListener("touchstart", this.touchSwipeBind);
  255. window.addEventListener("touchmove", this.touchSwipeBind);
  256. window.addEventListener("touchend", this.touchSwipeBind);
  257. }
  258. #removeWindowListeners() {
  259. window.removeEventListener("mousemove", this.showControlsBind);
  260. window.removeEventListener("mousedown", this.mouseDownBind);
  261. window.removeEventListener("wheel", this.mouseWheelBind, {
  262. passive: false
  263. });
  264. window.removeEventListener("keydown", this.resetMouseScrollStateBind);
  265. window.removeEventListener("contextmenu", this.contextMenuBind);
  266. window.removeEventListener("touchstart", this.touchSwipeBind);
  267. window.removeEventListener("touchmove", this.touchSwipeBind);
  268. window.removeEventListener("touchend", this.touchSwipeBind);
  269. delete this.showControlsBind;
  270. delete this.mouseDownBind;
  271. delete this.mouseWheelBind;
  272. delete this.resetMouseScrollStateBind;
  273. delete this.contextMenuBind;
  274. delete this.touchSwipeBind;
  275. }
  276. #fullscreenChange() {
  277. if (document.fullscreenElement) {
  278. this.#enter();
  279. } else {
  280. this.#exit();
  281. }
  282. }
  283. #addFullscreenChangeListeners() {
  284. this.fullscreenChangeBind = this.#fullscreenChange.bind(this);
  285. window.addEventListener("fullscreenchange", this.fullscreenChangeBind);
  286. }
  287. #removeFullscreenChangeListeners() {
  288. window.removeEventListener("fullscreenchange", this.fullscreenChangeBind);
  289. delete this.fullscreenChangeBind;
  290. }
  291. }
  292. exports.PDFPresentationMode = PDFPresentationMode;