2
0

ui_utils.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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.animationStarted = exports.VERTICAL_PADDING = exports.UNKNOWN_SCALE = exports.TextLayerMode = exports.SpreadMode = exports.SidebarView = exports.ScrollMode = exports.SCROLLBAR_PADDING = exports.RenderingStates = exports.RendererType = exports.ProgressBar = exports.PresentationModeState = exports.OutputScale = exports.MIN_SCALE = exports.MAX_SCALE = exports.MAX_AUTO_SCALE = exports.DEFAULT_SCALE_VALUE = exports.DEFAULT_SCALE_DELTA = exports.DEFAULT_SCALE = exports.AutoPrintRegExp = void 0;
  27. exports.apiPageLayoutToViewerModes = apiPageLayoutToViewerModes;
  28. exports.apiPageModeToSidebarView = apiPageModeToSidebarView;
  29. exports.approximateFraction = approximateFraction;
  30. exports.backtrackBeforeAllVisibleElements = backtrackBeforeAllVisibleElements;
  31. exports.docStyle = void 0;
  32. exports.getActiveOrFocusedElement = getActiveOrFocusedElement;
  33. exports.getPageSizeInches = getPageSizeInches;
  34. exports.getVisibleElements = getVisibleElements;
  35. exports.isPortraitOrientation = isPortraitOrientation;
  36. exports.isValidRotation = isValidRotation;
  37. exports.isValidScrollMode = isValidScrollMode;
  38. exports.isValidSpreadMode = isValidSpreadMode;
  39. exports.noContextMenuHandler = noContextMenuHandler;
  40. exports.normalizeWheelEventDelta = normalizeWheelEventDelta;
  41. exports.normalizeWheelEventDirection = normalizeWheelEventDirection;
  42. exports.parseQueryString = parseQueryString;
  43. exports.removeNullCharacters = removeNullCharacters;
  44. exports.roundToDivide = roundToDivide;
  45. exports.scrollIntoView = scrollIntoView;
  46. exports.watchScroll = watchScroll;
  47. var _pdf = require("../pdf");
  48. const DEFAULT_SCALE_VALUE = "auto";
  49. exports.DEFAULT_SCALE_VALUE = DEFAULT_SCALE_VALUE;
  50. const DEFAULT_SCALE = 1.0;
  51. exports.DEFAULT_SCALE = DEFAULT_SCALE;
  52. const DEFAULT_SCALE_DELTA = 1.1;
  53. exports.DEFAULT_SCALE_DELTA = DEFAULT_SCALE_DELTA;
  54. const MIN_SCALE = 0.1;
  55. exports.MIN_SCALE = MIN_SCALE;
  56. const MAX_SCALE = 10.0;
  57. exports.MAX_SCALE = MAX_SCALE;
  58. const UNKNOWN_SCALE = 0;
  59. exports.UNKNOWN_SCALE = UNKNOWN_SCALE;
  60. const MAX_AUTO_SCALE = 1.25;
  61. exports.MAX_AUTO_SCALE = MAX_AUTO_SCALE;
  62. const SCROLLBAR_PADDING = 40;
  63. exports.SCROLLBAR_PADDING = SCROLLBAR_PADDING;
  64. const VERTICAL_PADDING = 5;
  65. exports.VERTICAL_PADDING = VERTICAL_PADDING;
  66. const RenderingStates = {
  67. INITIAL: 0,
  68. RUNNING: 1,
  69. PAUSED: 2,
  70. FINISHED: 3
  71. };
  72. exports.RenderingStates = RenderingStates;
  73. const PresentationModeState = {
  74. UNKNOWN: 0,
  75. NORMAL: 1,
  76. CHANGING: 2,
  77. FULLSCREEN: 3
  78. };
  79. exports.PresentationModeState = PresentationModeState;
  80. const SidebarView = {
  81. UNKNOWN: -1,
  82. NONE: 0,
  83. THUMBS: 1,
  84. OUTLINE: 2,
  85. ATTACHMENTS: 3,
  86. LAYERS: 4
  87. };
  88. exports.SidebarView = SidebarView;
  89. const RendererType = {
  90. CANVAS: "canvas",
  91. SVG: "svg"
  92. };
  93. exports.RendererType = RendererType;
  94. const TextLayerMode = {
  95. DISABLE: 0,
  96. ENABLE: 1,
  97. ENABLE_ENHANCE: 2
  98. };
  99. exports.TextLayerMode = TextLayerMode;
  100. const ScrollMode = {
  101. UNKNOWN: -1,
  102. VERTICAL: 0,
  103. HORIZONTAL: 1,
  104. WRAPPED: 2,
  105. PAGE: 3
  106. };
  107. exports.ScrollMode = ScrollMode;
  108. const SpreadMode = {
  109. UNKNOWN: -1,
  110. NONE: 0,
  111. ODD: 1,
  112. EVEN: 2
  113. };
  114. exports.SpreadMode = SpreadMode;
  115. const AutoPrintRegExp = /\bprint\s*\(/;
  116. exports.AutoPrintRegExp = AutoPrintRegExp;
  117. class OutputScale {
  118. constructor() {
  119. const pixelRatio = window.devicePixelRatio || 1;
  120. this.sx = pixelRatio;
  121. this.sy = pixelRatio;
  122. }
  123. get scaled() {
  124. return this.sx !== 1 || this.sy !== 1;
  125. }
  126. }
  127. exports.OutputScale = OutputScale;
  128. function scrollIntoView(element, spot, scrollMatches = false) {
  129. let parent = element.offsetParent;
  130. if (!parent) {
  131. console.error("offsetParent is not set -- cannot scroll");
  132. return;
  133. }
  134. let offsetY = element.offsetTop + element.clientTop;
  135. let offsetX = element.offsetLeft + element.clientLeft;
  136. while (parent.clientHeight === parent.scrollHeight && parent.clientWidth === parent.scrollWidth || scrollMatches && (parent.classList.contains("markedContent") || getComputedStyle(parent).overflow === "hidden")) {
  137. offsetY += parent.offsetTop;
  138. offsetX += parent.offsetLeft;
  139. parent = parent.offsetParent;
  140. if (!parent) {
  141. return;
  142. }
  143. }
  144. if (spot) {
  145. if (spot.top !== undefined) {
  146. offsetY += spot.top;
  147. }
  148. if (spot.left !== undefined) {
  149. offsetX += spot.left;
  150. parent.scrollLeft = offsetX;
  151. }
  152. }
  153. parent.scrollTop = offsetY;
  154. }
  155. function watchScroll(viewAreaElement, callback) {
  156. const debounceScroll = function (evt) {
  157. if (rAF) {
  158. return;
  159. }
  160. rAF = window.requestAnimationFrame(function viewAreaElementScrolled() {
  161. rAF = null;
  162. const currentX = viewAreaElement.scrollLeft;
  163. const lastX = state.lastX;
  164. if (currentX !== lastX) {
  165. state.right = currentX > lastX;
  166. }
  167. state.lastX = currentX;
  168. const currentY = viewAreaElement.scrollTop;
  169. const lastY = state.lastY;
  170. if (currentY !== lastY) {
  171. state.down = currentY > lastY;
  172. }
  173. state.lastY = currentY;
  174. callback(state);
  175. });
  176. };
  177. const state = {
  178. right: true,
  179. down: true,
  180. lastX: viewAreaElement.scrollLeft,
  181. lastY: viewAreaElement.scrollTop,
  182. _eventHandler: debounceScroll
  183. };
  184. let rAF = null;
  185. viewAreaElement.addEventListener("scroll", debounceScroll, true);
  186. return state;
  187. }
  188. function parseQueryString(query) {
  189. const params = new Map();
  190. for (const [key, value] of new URLSearchParams(query)) {
  191. params.set(key.toLowerCase(), value);
  192. }
  193. return params;
  194. }
  195. const NullCharactersRegExp = /\x00/g;
  196. const InvisibleCharactersRegExp = /[\x01-\x1F]/g;
  197. function removeNullCharacters(str, replaceInvisible = false) {
  198. if (typeof str !== "string") {
  199. console.error(`The argument must be a string.`);
  200. return str;
  201. }
  202. if (replaceInvisible) {
  203. str = str.replace(InvisibleCharactersRegExp, " ");
  204. }
  205. return str.replace(NullCharactersRegExp, "");
  206. }
  207. function approximateFraction(x) {
  208. if (Math.floor(x) === x) {
  209. return [x, 1];
  210. }
  211. const xinv = 1 / x;
  212. const limit = 8;
  213. if (xinv > limit) {
  214. return [1, limit];
  215. } else if (Math.floor(xinv) === xinv) {
  216. return [1, xinv];
  217. }
  218. const x_ = x > 1 ? xinv : x;
  219. let a = 0,
  220. b = 1,
  221. c = 1,
  222. d = 1;
  223. while (true) {
  224. const p = a + c,
  225. q = b + d;
  226. if (q > limit) {
  227. break;
  228. }
  229. if (x_ <= p / q) {
  230. c = p;
  231. d = q;
  232. } else {
  233. a = p;
  234. b = q;
  235. }
  236. }
  237. let result;
  238. if (x_ - a / b < c / d - x_) {
  239. result = x_ === x ? [a, b] : [b, a];
  240. } else {
  241. result = x_ === x ? [c, d] : [d, c];
  242. }
  243. return result;
  244. }
  245. function roundToDivide(x, div) {
  246. const r = x % div;
  247. return r === 0 ? x : Math.round(x - r + div);
  248. }
  249. function getPageSizeInches({
  250. view,
  251. userUnit,
  252. rotate
  253. }) {
  254. const [x1, y1, x2, y2] = view;
  255. const changeOrientation = rotate % 180 !== 0;
  256. const width = (x2 - x1) / 72 * userUnit;
  257. const height = (y2 - y1) / 72 * userUnit;
  258. return {
  259. width: changeOrientation ? height : width,
  260. height: changeOrientation ? width : height
  261. };
  262. }
  263. function backtrackBeforeAllVisibleElements(index, views, top) {
  264. if (index < 2) {
  265. return index;
  266. }
  267. let elt = views[index].div;
  268. let pageTop = elt.offsetTop + elt.clientTop;
  269. if (pageTop >= top) {
  270. elt = views[index - 1].div;
  271. pageTop = elt.offsetTop + elt.clientTop;
  272. }
  273. for (let i = index - 2; i >= 0; --i) {
  274. elt = views[i].div;
  275. if (elt.offsetTop + elt.clientTop + elt.clientHeight <= pageTop) {
  276. break;
  277. }
  278. index = i;
  279. }
  280. return index;
  281. }
  282. function getVisibleElements({
  283. scrollEl,
  284. views,
  285. sortByVisibility = false,
  286. horizontal = false,
  287. rtl = false
  288. }) {
  289. const top = scrollEl.scrollTop,
  290. bottom = top + scrollEl.clientHeight;
  291. const left = scrollEl.scrollLeft,
  292. right = left + scrollEl.clientWidth;
  293. function isElementBottomAfterViewTop(view) {
  294. const element = view.div;
  295. const elementBottom = element.offsetTop + element.clientTop + element.clientHeight;
  296. return elementBottom > top;
  297. }
  298. function isElementNextAfterViewHorizontally(view) {
  299. const element = view.div;
  300. const elementLeft = element.offsetLeft + element.clientLeft;
  301. const elementRight = elementLeft + element.clientWidth;
  302. return rtl ? elementLeft < right : elementRight > left;
  303. }
  304. const visible = [],
  305. ids = new Set(),
  306. numViews = views.length;
  307. let firstVisibleElementInd = (0, _pdf.binarySearchFirstItem)(views, horizontal ? isElementNextAfterViewHorizontally : isElementBottomAfterViewTop);
  308. if (firstVisibleElementInd > 0 && firstVisibleElementInd < numViews && !horizontal) {
  309. firstVisibleElementInd = backtrackBeforeAllVisibleElements(firstVisibleElementInd, views, top);
  310. }
  311. let lastEdge = horizontal ? right : -1;
  312. for (let i = firstVisibleElementInd; i < numViews; i++) {
  313. const view = views[i],
  314. element = view.div;
  315. const currentWidth = element.offsetLeft + element.clientLeft;
  316. const currentHeight = element.offsetTop + element.clientTop;
  317. const viewWidth = element.clientWidth,
  318. viewHeight = element.clientHeight;
  319. const viewRight = currentWidth + viewWidth;
  320. const viewBottom = currentHeight + viewHeight;
  321. if (lastEdge === -1) {
  322. if (viewBottom >= bottom) {
  323. lastEdge = viewBottom;
  324. }
  325. } else if ((horizontal ? currentWidth : currentHeight) > lastEdge) {
  326. break;
  327. }
  328. if (viewBottom <= top || currentHeight >= bottom || viewRight <= left || currentWidth >= right) {
  329. continue;
  330. }
  331. const hiddenHeight = Math.max(0, top - currentHeight) + Math.max(0, viewBottom - bottom);
  332. const hiddenWidth = Math.max(0, left - currentWidth) + Math.max(0, viewRight - right);
  333. const fractionHeight = (viewHeight - hiddenHeight) / viewHeight,
  334. fractionWidth = (viewWidth - hiddenWidth) / viewWidth;
  335. const percent = fractionHeight * fractionWidth * 100 | 0;
  336. visible.push({
  337. id: view.id,
  338. x: currentWidth,
  339. y: currentHeight,
  340. view,
  341. percent,
  342. widthPercent: fractionWidth * 100 | 0
  343. });
  344. ids.add(view.id);
  345. }
  346. const first = visible[0],
  347. last = visible.at(-1);
  348. if (sortByVisibility) {
  349. visible.sort(function (a, b) {
  350. const pc = a.percent - b.percent;
  351. if (Math.abs(pc) > 0.001) {
  352. return -pc;
  353. }
  354. return a.id - b.id;
  355. });
  356. }
  357. return {
  358. first,
  359. last,
  360. views: visible,
  361. ids
  362. };
  363. }
  364. function noContextMenuHandler(evt) {
  365. evt.preventDefault();
  366. }
  367. function normalizeWheelEventDirection(evt) {
  368. let delta = Math.hypot(evt.deltaX, evt.deltaY);
  369. const angle = Math.atan2(evt.deltaY, evt.deltaX);
  370. if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) {
  371. delta = -delta;
  372. }
  373. return delta;
  374. }
  375. function normalizeWheelEventDelta(evt) {
  376. let delta = normalizeWheelEventDirection(evt);
  377. const MOUSE_DOM_DELTA_PIXEL_MODE = 0;
  378. const MOUSE_DOM_DELTA_LINE_MODE = 1;
  379. const MOUSE_PIXELS_PER_LINE = 30;
  380. const MOUSE_LINES_PER_PAGE = 30;
  381. if (evt.deltaMode === MOUSE_DOM_DELTA_PIXEL_MODE) {
  382. delta /= MOUSE_PIXELS_PER_LINE * MOUSE_LINES_PER_PAGE;
  383. } else if (evt.deltaMode === MOUSE_DOM_DELTA_LINE_MODE) {
  384. delta /= MOUSE_LINES_PER_PAGE;
  385. }
  386. return delta;
  387. }
  388. function isValidRotation(angle) {
  389. return Number.isInteger(angle) && angle % 90 === 0;
  390. }
  391. function isValidScrollMode(mode) {
  392. return Number.isInteger(mode) && Object.values(ScrollMode).includes(mode) && mode !== ScrollMode.UNKNOWN;
  393. }
  394. function isValidSpreadMode(mode) {
  395. return Number.isInteger(mode) && Object.values(SpreadMode).includes(mode) && mode !== SpreadMode.UNKNOWN;
  396. }
  397. function isPortraitOrientation(size) {
  398. return size.width <= size.height;
  399. }
  400. const animationStarted = new Promise(function (resolve) {
  401. if (typeof window === "undefined") {
  402. setTimeout(resolve, 20);
  403. return;
  404. }
  405. window.requestAnimationFrame(resolve);
  406. });
  407. exports.animationStarted = animationStarted;
  408. const docStyle = typeof document === "undefined" ? null : document.documentElement.style;
  409. exports.docStyle = docStyle;
  410. function clamp(v, min, max) {
  411. return Math.min(Math.max(v, min), max);
  412. }
  413. class ProgressBar {
  414. #classList = null;
  415. #percent = 0;
  416. #visible = true;
  417. constructor(id) {
  418. if (arguments.length > 1) {
  419. throw new Error("ProgressBar no longer accepts any additional options, " + "please use CSS rules to modify its appearance instead.");
  420. }
  421. const bar = document.getElementById(id);
  422. this.#classList = bar.classList;
  423. }
  424. get percent() {
  425. return this.#percent;
  426. }
  427. set percent(val) {
  428. this.#percent = clamp(val, 0, 100);
  429. if (isNaN(val)) {
  430. this.#classList.add("indeterminate");
  431. return;
  432. }
  433. this.#classList.remove("indeterminate");
  434. docStyle.setProperty("--progressBar-percent", `${this.#percent}%`);
  435. }
  436. setWidth(viewer) {
  437. if (!viewer) {
  438. return;
  439. }
  440. const container = viewer.parentNode;
  441. const scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
  442. if (scrollbarWidth > 0) {
  443. docStyle.setProperty("--progressBar-end-offset", `${scrollbarWidth}px`);
  444. }
  445. }
  446. hide() {
  447. if (!this.#visible) {
  448. return;
  449. }
  450. this.#visible = false;
  451. this.#classList.add("hidden");
  452. }
  453. show() {
  454. if (this.#visible) {
  455. return;
  456. }
  457. this.#visible = true;
  458. this.#classList.remove("hidden");
  459. }
  460. }
  461. exports.ProgressBar = ProgressBar;
  462. function getActiveOrFocusedElement() {
  463. let curRoot = document;
  464. let curActiveOrFocused = curRoot.activeElement || curRoot.querySelector(":focus");
  465. while (curActiveOrFocused?.shadowRoot) {
  466. curRoot = curActiveOrFocused.shadowRoot;
  467. curActiveOrFocused = curRoot.activeElement || curRoot.querySelector(":focus");
  468. }
  469. return curActiveOrFocused;
  470. }
  471. function apiPageLayoutToViewerModes(layout) {
  472. let scrollMode = ScrollMode.VERTICAL,
  473. spreadMode = SpreadMode.NONE;
  474. switch (layout) {
  475. case "SinglePage":
  476. scrollMode = ScrollMode.PAGE;
  477. break;
  478. case "OneColumn":
  479. break;
  480. case "TwoPageLeft":
  481. scrollMode = ScrollMode.PAGE;
  482. case "TwoColumnLeft":
  483. spreadMode = SpreadMode.ODD;
  484. break;
  485. case "TwoPageRight":
  486. scrollMode = ScrollMode.PAGE;
  487. case "TwoColumnRight":
  488. spreadMode = SpreadMode.EVEN;
  489. break;
  490. }
  491. return {
  492. scrollMode,
  493. spreadMode
  494. };
  495. }
  496. function apiPageModeToSidebarView(mode) {
  497. switch (mode) {
  498. case "UseNone":
  499. return SidebarView.NONE;
  500. case "UseThumbs":
  501. return SidebarView.THUMBS;
  502. case "UseOutlines":
  503. return SidebarView.OUTLINE;
  504. case "UseAttachments":
  505. return SidebarView.ATTACHMENTS;
  506. case "UseOC":
  507. return SidebarView.LAYERS;
  508. }
  509. return SidebarView.NONE;
  510. }