pdf_presentation_mode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import {
  16. normalizeWheelEventDelta,
  17. PresentationModeState,
  18. ScrollMode,
  19. SpreadMode,
  20. } from "./ui_utils.js";
  21. import { AnnotationEditorType } from "pdfjs-lib";
  22. const DELAY_BEFORE_HIDING_CONTROLS = 3000; // in ms
  23. const ACTIVE_SELECTOR = "pdfPresentationMode";
  24. const CONTROLS_SELECTOR = "pdfPresentationModeControls";
  25. const MOUSE_SCROLL_COOLDOWN_TIME = 50; // in ms
  26. const PAGE_SWITCH_THRESHOLD = 0.1;
  27. // Number of CSS pixels for a movement to count as a swipe.
  28. const SWIPE_MIN_DISTANCE_THRESHOLD = 50;
  29. // Swipe angle deviation from the x or y axis before it is not
  30. // considered a swipe in that direction any more.
  31. const SWIPE_ANGLE_THRESHOLD = Math.PI / 6;
  32. /**
  33. * @typedef {Object} PDFPresentationModeOptions
  34. * @property {HTMLDivElement} container - The container for the viewer element.
  35. * @property {PDFViewer} pdfViewer - The document viewer.
  36. * @property {EventBus} eventBus - The application event bus.
  37. */
  38. class PDFPresentationMode {
  39. #state = PresentationModeState.UNKNOWN;
  40. #args = null;
  41. /**
  42. * @param {PDFPresentationModeOptions} options
  43. */
  44. constructor({ container, pdfViewer, eventBus }) {
  45. this.container = container;
  46. this.pdfViewer = pdfViewer;
  47. this.eventBus = eventBus;
  48. this.contextMenuOpen = false;
  49. this.mouseScrollTimeStamp = 0;
  50. this.mouseScrollDelta = 0;
  51. this.touchSwipeState = null;
  52. }
  53. /**
  54. * Request the browser to enter fullscreen mode.
  55. * @returns {Promise<boolean>} Indicating if the request was successful.
  56. */
  57. async request() {
  58. const { container, pdfViewer } = this;
  59. if (this.active || !pdfViewer.pagesCount || !container.requestFullscreen) {
  60. return false;
  61. }
  62. this.#addFullscreenChangeListeners();
  63. this.#notifyStateChange(PresentationModeState.CHANGING);
  64. const promise = container.requestFullscreen();
  65. this.#args = {
  66. pageNumber: pdfViewer.currentPageNumber,
  67. scaleValue: pdfViewer.currentScaleValue,
  68. scrollMode: pdfViewer.scrollMode,
  69. spreadMode: null,
  70. annotationEditorMode: null,
  71. };
  72. if (
  73. pdfViewer.spreadMode !== SpreadMode.NONE &&
  74. !(pdfViewer.pageViewsReady && pdfViewer.hasEqualPageSizes)
  75. ) {
  76. console.warn(
  77. "Ignoring Spread modes when entering PresentationMode, " +
  78. "since the document may contain varying page sizes."
  79. );
  80. this.#args.spreadMode = pdfViewer.spreadMode;
  81. }
  82. if (pdfViewer.annotationEditorMode !== AnnotationEditorType.DISABLE) {
  83. this.#args.annotationEditorMode = pdfViewer.annotationEditorMode;
  84. }
  85. try {
  86. await promise;
  87. pdfViewer.focus(); // Fixes bug 1787456.
  88. return true;
  89. } catch (reason) {
  90. this.#removeFullscreenChangeListeners();
  91. this.#notifyStateChange(PresentationModeState.NORMAL);
  92. }
  93. return false;
  94. }
  95. get active() {
  96. return (
  97. this.#state === PresentationModeState.CHANGING ||
  98. this.#state === PresentationModeState.FULLSCREEN
  99. );
  100. }
  101. #mouseWheel(evt) {
  102. if (!this.active) {
  103. return;
  104. }
  105. evt.preventDefault();
  106. const delta = normalizeWheelEventDelta(evt);
  107. const currentTime = Date.now();
  108. const storedTime = this.mouseScrollTimeStamp;
  109. // If we've already switched page, avoid accidentally switching again.
  110. if (
  111. currentTime > storedTime &&
  112. currentTime - storedTime < MOUSE_SCROLL_COOLDOWN_TIME
  113. ) {
  114. return;
  115. }
  116. // If the scroll direction changed, reset the accumulated scroll delta.
  117. if (
  118. (this.mouseScrollDelta > 0 && delta < 0) ||
  119. (this.mouseScrollDelta < 0 && delta > 0)
  120. ) {
  121. this.#resetMouseScrollState();
  122. }
  123. this.mouseScrollDelta += delta;
  124. if (Math.abs(this.mouseScrollDelta) >= PAGE_SWITCH_THRESHOLD) {
  125. const totalDelta = this.mouseScrollDelta;
  126. this.#resetMouseScrollState();
  127. const success =
  128. totalDelta > 0
  129. ? this.pdfViewer.previousPage()
  130. : this.pdfViewer.nextPage();
  131. if (success) {
  132. this.mouseScrollTimeStamp = currentTime;
  133. }
  134. }
  135. }
  136. #notifyStateChange(state) {
  137. this.#state = state;
  138. this.eventBus.dispatch("presentationmodechanged", { source: this, state });
  139. }
  140. #enter() {
  141. this.#notifyStateChange(PresentationModeState.FULLSCREEN);
  142. this.container.classList.add(ACTIVE_SELECTOR);
  143. // Ensure that the correct page is scrolled into view when entering
  144. // Presentation Mode, by waiting until fullscreen mode in enabled.
  145. setTimeout(() => {
  146. this.pdfViewer.scrollMode = ScrollMode.PAGE;
  147. if (this.#args.spreadMode !== null) {
  148. this.pdfViewer.spreadMode = SpreadMode.NONE;
  149. }
  150. this.pdfViewer.currentPageNumber = this.#args.pageNumber;
  151. this.pdfViewer.currentScaleValue = "page-fit";
  152. if (this.#args.annotationEditorMode !== null) {
  153. this.pdfViewer.annotationEditorMode = AnnotationEditorType.NONE;
  154. }
  155. }, 0);
  156. this.#addWindowListeners();
  157. this.#showControls();
  158. this.contextMenuOpen = false;
  159. // Text selection is disabled in Presentation Mode, thus it's not possible
  160. // for the user to deselect text that is selected (e.g. with "Select all")
  161. // when entering Presentation Mode, hence we remove any active selection.
  162. window.getSelection().removeAllRanges();
  163. }
  164. #exit() {
  165. const pageNumber = this.pdfViewer.currentPageNumber;
  166. this.container.classList.remove(ACTIVE_SELECTOR);
  167. // Ensure that the correct page is scrolled into view when exiting
  168. // Presentation Mode, by waiting until fullscreen mode is disabled.
  169. setTimeout(() => {
  170. this.#removeFullscreenChangeListeners();
  171. this.#notifyStateChange(PresentationModeState.NORMAL);
  172. this.pdfViewer.scrollMode = this.#args.scrollMode;
  173. if (this.#args.spreadMode !== null) {
  174. this.pdfViewer.spreadMode = this.#args.spreadMode;
  175. }
  176. this.pdfViewer.currentScaleValue = this.#args.scaleValue;
  177. this.pdfViewer.currentPageNumber = pageNumber;
  178. if (this.#args.annotationEditorMode !== null) {
  179. this.pdfViewer.annotationEditorMode = this.#args.annotationEditorMode;
  180. }
  181. this.#args = null;
  182. }, 0);
  183. this.#removeWindowListeners();
  184. this.#hideControls();
  185. this.#resetMouseScrollState();
  186. this.contextMenuOpen = false;
  187. }
  188. #mouseDown(evt) {
  189. if (this.contextMenuOpen) {
  190. this.contextMenuOpen = false;
  191. evt.preventDefault();
  192. return;
  193. }
  194. if (evt.button !== 0) {
  195. return;
  196. }
  197. // Enable clicking of links in presentation mode. Note: only links
  198. // pointing to destinations in the current PDF document work.
  199. if (
  200. evt.target.href &&
  201. evt.target.parentNode?.hasAttribute("data-internal-link")
  202. ) {
  203. return;
  204. }
  205. // Unless an internal link was clicked, advance one page.
  206. evt.preventDefault();
  207. if (evt.shiftKey) {
  208. this.pdfViewer.previousPage();
  209. } else {
  210. this.pdfViewer.nextPage();
  211. }
  212. }
  213. #contextMenu() {
  214. this.contextMenuOpen = true;
  215. }
  216. #showControls() {
  217. if (this.controlsTimeout) {
  218. clearTimeout(this.controlsTimeout);
  219. } else {
  220. this.container.classList.add(CONTROLS_SELECTOR);
  221. }
  222. this.controlsTimeout = setTimeout(() => {
  223. this.container.classList.remove(CONTROLS_SELECTOR);
  224. delete this.controlsTimeout;
  225. }, DELAY_BEFORE_HIDING_CONTROLS);
  226. }
  227. #hideControls() {
  228. if (!this.controlsTimeout) {
  229. return;
  230. }
  231. clearTimeout(this.controlsTimeout);
  232. this.container.classList.remove(CONTROLS_SELECTOR);
  233. delete this.controlsTimeout;
  234. }
  235. /**
  236. * Resets the properties used for tracking mouse scrolling events.
  237. */
  238. #resetMouseScrollState() {
  239. this.mouseScrollTimeStamp = 0;
  240. this.mouseScrollDelta = 0;
  241. }
  242. #touchSwipe(evt) {
  243. if (!this.active) {
  244. return;
  245. }
  246. if (evt.touches.length > 1) {
  247. // Multiple touch points detected; cancel the swipe.
  248. this.touchSwipeState = null;
  249. return;
  250. }
  251. switch (evt.type) {
  252. case "touchstart":
  253. this.touchSwipeState = {
  254. startX: evt.touches[0].pageX,
  255. startY: evt.touches[0].pageY,
  256. endX: evt.touches[0].pageX,
  257. endY: evt.touches[0].pageY,
  258. };
  259. break;
  260. case "touchmove":
  261. if (this.touchSwipeState === null) {
  262. return;
  263. }
  264. this.touchSwipeState.endX = evt.touches[0].pageX;
  265. this.touchSwipeState.endY = evt.touches[0].pageY;
  266. // Avoid the swipe from triggering browser gestures (Chrome in
  267. // particular has some sort of swipe gesture in fullscreen mode).
  268. evt.preventDefault();
  269. break;
  270. case "touchend":
  271. if (this.touchSwipeState === null) {
  272. return;
  273. }
  274. let delta = 0;
  275. const dx = this.touchSwipeState.endX - this.touchSwipeState.startX;
  276. const dy = this.touchSwipeState.endY - this.touchSwipeState.startY;
  277. const absAngle = Math.abs(Math.atan2(dy, dx));
  278. if (
  279. Math.abs(dx) > SWIPE_MIN_DISTANCE_THRESHOLD &&
  280. (absAngle <= SWIPE_ANGLE_THRESHOLD ||
  281. absAngle >= Math.PI - SWIPE_ANGLE_THRESHOLD)
  282. ) {
  283. // Horizontal swipe.
  284. delta = dx;
  285. } else if (
  286. Math.abs(dy) > SWIPE_MIN_DISTANCE_THRESHOLD &&
  287. Math.abs(absAngle - Math.PI / 2) <= SWIPE_ANGLE_THRESHOLD
  288. ) {
  289. // Vertical swipe.
  290. delta = dy;
  291. }
  292. if (delta > 0) {
  293. this.pdfViewer.previousPage();
  294. } else if (delta < 0) {
  295. this.pdfViewer.nextPage();
  296. }
  297. break;
  298. }
  299. }
  300. #addWindowListeners() {
  301. this.showControlsBind = this.#showControls.bind(this);
  302. this.mouseDownBind = this.#mouseDown.bind(this);
  303. this.mouseWheelBind = this.#mouseWheel.bind(this);
  304. this.resetMouseScrollStateBind = this.#resetMouseScrollState.bind(this);
  305. this.contextMenuBind = this.#contextMenu.bind(this);
  306. this.touchSwipeBind = this.#touchSwipe.bind(this);
  307. window.addEventListener("mousemove", this.showControlsBind);
  308. window.addEventListener("mousedown", this.mouseDownBind);
  309. window.addEventListener("wheel", this.mouseWheelBind, { passive: false });
  310. window.addEventListener("keydown", this.resetMouseScrollStateBind);
  311. window.addEventListener("contextmenu", this.contextMenuBind);
  312. window.addEventListener("touchstart", this.touchSwipeBind);
  313. window.addEventListener("touchmove", this.touchSwipeBind);
  314. window.addEventListener("touchend", this.touchSwipeBind);
  315. }
  316. #removeWindowListeners() {
  317. window.removeEventListener("mousemove", this.showControlsBind);
  318. window.removeEventListener("mousedown", this.mouseDownBind);
  319. window.removeEventListener("wheel", this.mouseWheelBind, {
  320. passive: false,
  321. });
  322. window.removeEventListener("keydown", this.resetMouseScrollStateBind);
  323. window.removeEventListener("contextmenu", this.contextMenuBind);
  324. window.removeEventListener("touchstart", this.touchSwipeBind);
  325. window.removeEventListener("touchmove", this.touchSwipeBind);
  326. window.removeEventListener("touchend", this.touchSwipeBind);
  327. delete this.showControlsBind;
  328. delete this.mouseDownBind;
  329. delete this.mouseWheelBind;
  330. delete this.resetMouseScrollStateBind;
  331. delete this.contextMenuBind;
  332. delete this.touchSwipeBind;
  333. }
  334. #fullscreenChange() {
  335. if (/* isFullscreen = */ document.fullscreenElement) {
  336. this.#enter();
  337. } else {
  338. this.#exit();
  339. }
  340. }
  341. #addFullscreenChangeListeners() {
  342. this.fullscreenChangeBind = this.#fullscreenChange.bind(this);
  343. window.addEventListener("fullscreenchange", this.fullscreenChangeBind);
  344. }
  345. #removeFullscreenChangeListeners() {
  346. window.removeEventListener("fullscreenchange", this.fullscreenChangeBind);
  347. delete this.fullscreenChangeBind;
  348. }
  349. }
  350. export { PDFPresentationMode };