2
0

pdf_presentation_mode.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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. contextMenuItems = null
  42. }) {
  43. this.container = container;
  44. this.pdfViewer = pdfViewer;
  45. this.eventBus = eventBus;
  46. this.active = false;
  47. this.args = null;
  48. this.contextMenuOpen = false;
  49. this.mouseScrollTimeStamp = 0;
  50. this.mouseScrollDelta = 0;
  51. this.touchSwipeState = null;
  52. if (contextMenuItems) {
  53. contextMenuItems.contextFirstPage.addEventListener("click", () => {
  54. this.contextMenuOpen = false;
  55. this.eventBus.dispatch("firstpage", {
  56. source: this
  57. });
  58. });
  59. contextMenuItems.contextLastPage.addEventListener("click", () => {
  60. this.contextMenuOpen = false;
  61. this.eventBus.dispatch("lastpage", {
  62. source: this
  63. });
  64. });
  65. contextMenuItems.contextPageRotateCw.addEventListener("click", () => {
  66. this.contextMenuOpen = false;
  67. this.eventBus.dispatch("rotatecw", {
  68. source: this
  69. });
  70. });
  71. contextMenuItems.contextPageRotateCcw.addEventListener("click", () => {
  72. this.contextMenuOpen = false;
  73. this.eventBus.dispatch("rotateccw", {
  74. source: this
  75. });
  76. });
  77. }
  78. }
  79. request() {
  80. if (this.switchInProgress || this.active || !this.pdfViewer.pagesCount) {
  81. return false;
  82. }
  83. this._addFullscreenChangeListeners();
  84. this._setSwitchInProgress();
  85. this._notifyStateChange();
  86. if (this.container.requestFullscreen) {
  87. this.container.requestFullscreen();
  88. } else if (this.container.mozRequestFullScreen) {
  89. this.container.mozRequestFullScreen();
  90. } else if (this.container.webkitRequestFullscreen) {
  91. this.container.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
  92. } else {
  93. return false;
  94. }
  95. this.args = {
  96. page: this.pdfViewer.currentPageNumber,
  97. previousScale: this.pdfViewer.currentScaleValue
  98. };
  99. return true;
  100. }
  101. _mouseWheel(evt) {
  102. if (!this.active) {
  103. return;
  104. }
  105. evt.preventDefault();
  106. const delta = (0, _ui_utils.normalizeWheelEventDelta)(evt);
  107. const currentTime = new Date().getTime();
  108. const storedTime = this.mouseScrollTimeStamp;
  109. if (currentTime > storedTime && currentTime - storedTime < MOUSE_SCROLL_COOLDOWN_TIME) {
  110. return;
  111. }
  112. if (this.mouseScrollDelta > 0 && delta < 0 || this.mouseScrollDelta < 0 && delta > 0) {
  113. this._resetMouseScrollState();
  114. }
  115. this.mouseScrollDelta += delta;
  116. if (Math.abs(this.mouseScrollDelta) >= PAGE_SWITCH_THRESHOLD) {
  117. const totalDelta = this.mouseScrollDelta;
  118. this._resetMouseScrollState();
  119. const success = totalDelta > 0 ? this.pdfViewer.previousPage() : this.pdfViewer.nextPage();
  120. if (success) {
  121. this.mouseScrollTimeStamp = currentTime;
  122. }
  123. }
  124. }
  125. get isFullscreen() {
  126. return !!(document.fullscreenElement || document.mozFullScreen || document.webkitIsFullScreen);
  127. }
  128. _notifyStateChange() {
  129. let state = _ui_utils.PresentationModeState.NORMAL;
  130. if (this.switchInProgress) {
  131. state = _ui_utils.PresentationModeState.CHANGING;
  132. } else if (this.active) {
  133. state = _ui_utils.PresentationModeState.FULLSCREEN;
  134. }
  135. this.eventBus.dispatch("presentationmodechanged", {
  136. source: this,
  137. state,
  138. get active() {
  139. throw new Error("Deprecated parameter: `active`, please use `state` instead.");
  140. },
  141. get switchInProgress() {
  142. throw new Error("Deprecated parameter: `switchInProgress`, please use `state` instead.");
  143. }
  144. });
  145. }
  146. _setSwitchInProgress() {
  147. if (this.switchInProgress) {
  148. clearTimeout(this.switchInProgress);
  149. }
  150. this.switchInProgress = setTimeout(() => {
  151. this._removeFullscreenChangeListeners();
  152. delete this.switchInProgress;
  153. this._notifyStateChange();
  154. }, DELAY_BEFORE_RESETTING_SWITCH_IN_PROGRESS);
  155. }
  156. _resetSwitchInProgress() {
  157. if (this.switchInProgress) {
  158. clearTimeout(this.switchInProgress);
  159. delete this.switchInProgress;
  160. }
  161. }
  162. _enter() {
  163. this.active = true;
  164. this._resetSwitchInProgress();
  165. this._notifyStateChange();
  166. this.container.classList.add(ACTIVE_SELECTOR);
  167. setTimeout(() => {
  168. this.pdfViewer.currentPageNumber = this.args.page;
  169. this.pdfViewer.currentScaleValue = "page-fit";
  170. }, 0);
  171. this._addWindowListeners();
  172. this._showControls();
  173. this.contextMenuOpen = false;
  174. this.container.setAttribute("contextmenu", "viewerContextMenu");
  175. window.getSelection().removeAllRanges();
  176. }
  177. _exit() {
  178. const page = this.pdfViewer.currentPageNumber;
  179. this.container.classList.remove(ACTIVE_SELECTOR);
  180. setTimeout(() => {
  181. this.active = false;
  182. this._removeFullscreenChangeListeners();
  183. this._notifyStateChange();
  184. this.pdfViewer.currentScaleValue = this.args.previousScale;
  185. this.pdfViewer.currentPageNumber = page;
  186. this.args = null;
  187. }, 0);
  188. this._removeWindowListeners();
  189. this._hideControls();
  190. this._resetMouseScrollState();
  191. this.container.removeAttribute("contextmenu");
  192. this.contextMenuOpen = false;
  193. }
  194. _mouseDown(evt) {
  195. if (this.contextMenuOpen) {
  196. this.contextMenuOpen = false;
  197. evt.preventDefault();
  198. return;
  199. }
  200. if (evt.button === 0) {
  201. const isInternalLink = evt.target.href && evt.target.classList.contains("internalLink");
  202. if (!isInternalLink) {
  203. evt.preventDefault();
  204. if (evt.shiftKey) {
  205. this.pdfViewer.previousPage();
  206. } else {
  207. this.pdfViewer.nextPage();
  208. }
  209. }
  210. }
  211. }
  212. _contextMenu() {
  213. this.contextMenuOpen = true;
  214. }
  215. _showControls() {
  216. if (this.controlsTimeout) {
  217. clearTimeout(this.controlsTimeout);
  218. } else {
  219. this.container.classList.add(CONTROLS_SELECTOR);
  220. }
  221. this.controlsTimeout = setTimeout(() => {
  222. this.container.classList.remove(CONTROLS_SELECTOR);
  223. delete this.controlsTimeout;
  224. }, DELAY_BEFORE_HIDING_CONTROLS);
  225. }
  226. _hideControls() {
  227. if (!this.controlsTimeout) {
  228. return;
  229. }
  230. clearTimeout(this.controlsTimeout);
  231. this.container.classList.remove(CONTROLS_SELECTOR);
  232. delete this.controlsTimeout;
  233. }
  234. _resetMouseScrollState() {
  235. this.mouseScrollTimeStamp = 0;
  236. this.mouseScrollDelta = 0;
  237. }
  238. _touchSwipe(evt) {
  239. if (!this.active) {
  240. return;
  241. }
  242. if (evt.touches.length > 1) {
  243. this.touchSwipeState = null;
  244. return;
  245. }
  246. switch (evt.type) {
  247. case "touchstart":
  248. this.touchSwipeState = {
  249. startX: evt.touches[0].pageX,
  250. startY: evt.touches[0].pageY,
  251. endX: evt.touches[0].pageX,
  252. endY: evt.touches[0].pageY
  253. };
  254. break;
  255. case "touchmove":
  256. if (this.touchSwipeState === null) {
  257. return;
  258. }
  259. this.touchSwipeState.endX = evt.touches[0].pageX;
  260. this.touchSwipeState.endY = evt.touches[0].pageY;
  261. evt.preventDefault();
  262. break;
  263. case "touchend":
  264. if (this.touchSwipeState === null) {
  265. return;
  266. }
  267. let delta = 0;
  268. const dx = this.touchSwipeState.endX - this.touchSwipeState.startX;
  269. const dy = this.touchSwipeState.endY - this.touchSwipeState.startY;
  270. const absAngle = Math.abs(Math.atan2(dy, dx));
  271. if (Math.abs(dx) > SWIPE_MIN_DISTANCE_THRESHOLD && (absAngle <= SWIPE_ANGLE_THRESHOLD || absAngle >= Math.PI - SWIPE_ANGLE_THRESHOLD)) {
  272. delta = dx;
  273. } else if (Math.abs(dy) > SWIPE_MIN_DISTANCE_THRESHOLD && Math.abs(absAngle - Math.PI / 2) <= SWIPE_ANGLE_THRESHOLD) {
  274. delta = dy;
  275. }
  276. if (delta > 0) {
  277. this.pdfViewer.previousPage();
  278. } else if (delta < 0) {
  279. this.pdfViewer.nextPage();
  280. }
  281. break;
  282. }
  283. }
  284. _addWindowListeners() {
  285. this.showControlsBind = this._showControls.bind(this);
  286. this.mouseDownBind = this._mouseDown.bind(this);
  287. this.mouseWheelBind = this._mouseWheel.bind(this);
  288. this.resetMouseScrollStateBind = this._resetMouseScrollState.bind(this);
  289. this.contextMenuBind = this._contextMenu.bind(this);
  290. this.touchSwipeBind = this._touchSwipe.bind(this);
  291. window.addEventListener("mousemove", this.showControlsBind);
  292. window.addEventListener("mousedown", this.mouseDownBind);
  293. window.addEventListener("wheel", this.mouseWheelBind, {
  294. passive: false
  295. });
  296. window.addEventListener("keydown", this.resetMouseScrollStateBind);
  297. window.addEventListener("contextmenu", this.contextMenuBind);
  298. window.addEventListener("touchstart", this.touchSwipeBind);
  299. window.addEventListener("touchmove", this.touchSwipeBind);
  300. window.addEventListener("touchend", this.touchSwipeBind);
  301. }
  302. _removeWindowListeners() {
  303. window.removeEventListener("mousemove", this.showControlsBind);
  304. window.removeEventListener("mousedown", this.mouseDownBind);
  305. window.removeEventListener("wheel", this.mouseWheelBind, {
  306. passive: false
  307. });
  308. window.removeEventListener("keydown", this.resetMouseScrollStateBind);
  309. window.removeEventListener("contextmenu", this.contextMenuBind);
  310. window.removeEventListener("touchstart", this.touchSwipeBind);
  311. window.removeEventListener("touchmove", this.touchSwipeBind);
  312. window.removeEventListener("touchend", this.touchSwipeBind);
  313. delete this.showControlsBind;
  314. delete this.mouseDownBind;
  315. delete this.mouseWheelBind;
  316. delete this.resetMouseScrollStateBind;
  317. delete this.contextMenuBind;
  318. delete this.touchSwipeBind;
  319. }
  320. _fullscreenChange() {
  321. if (this.isFullscreen) {
  322. this._enter();
  323. } else {
  324. this._exit();
  325. }
  326. }
  327. _addFullscreenChangeListeners() {
  328. this.fullscreenChangeBind = this._fullscreenChange.bind(this);
  329. window.addEventListener("fullscreenchange", this.fullscreenChangeBind);
  330. window.addEventListener("mozfullscreenchange", this.fullscreenChangeBind);
  331. window.addEventListener("webkitfullscreenchange", this.fullscreenChangeBind);
  332. }
  333. _removeFullscreenChangeListeners() {
  334. window.removeEventListener("fullscreenchange", this.fullscreenChangeBind);
  335. window.removeEventListener("mozfullscreenchange", this.fullscreenChangeBind);
  336. window.removeEventListener("webkitfullscreenchange", this.fullscreenChangeBind);
  337. delete this.fullscreenChangeBind;
  338. }
  339. }
  340. exports.PDFPresentationMode = PDFPresentationMode;