pdf_presentation_mode.js 9.9 KB

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