pdf_presentation_mode.js 10 KB

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