2
0

ui_utils.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.apiPageLayoutToSpreadMode = apiPageLayoutToSpreadMode;
  27. exports.apiPageModeToSidebarView = apiPageModeToSidebarView;
  28. exports.approximateFraction = approximateFraction;
  29. exports.backtrackBeforeAllVisibleElements = backtrackBeforeAllVisibleElements;
  30. exports.binarySearchFirstItem = binarySearchFirstItem;
  31. exports.getActiveOrFocusedElement = getActiveOrFocusedElement;
  32. exports.getOutputScale = getOutputScale;
  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.moveToEndOfArray = moveToEndOfArray;
  40. exports.noContextMenuHandler = noContextMenuHandler;
  41. exports.normalizeWheelEventDelta = normalizeWheelEventDelta;
  42. exports.normalizeWheelEventDirection = normalizeWheelEventDirection;
  43. exports.parseQueryString = parseQueryString;
  44. exports.roundToDivide = roundToDivide;
  45. exports.scrollIntoView = scrollIntoView;
  46. exports.waitOnEventOrTimeout = waitOnEventOrTimeout;
  47. exports.watchScroll = watchScroll;
  48. exports.WaitOnType = exports.VERTICAL_PADDING = exports.UNKNOWN_SCALE = exports.TextLayerMode = exports.SpreadMode = exports.SidebarView = exports.ScrollMode = exports.SCROLLBAR_PADDING = exports.RendererType = exports.ProgressBar = exports.PresentationModeState = exports.MIN_SCALE = exports.MAX_SCALE = exports.MAX_AUTO_SCALE = exports.EventBus = exports.DEFAULT_SCALE_VALUE = exports.DEFAULT_SCALE = exports.CSS_UNITS = exports.AutoPrintRegExp = exports.animationStarted = void 0;
  49. const CSS_UNITS = 96.0 / 72.0;
  50. exports.CSS_UNITS = CSS_UNITS;
  51. const DEFAULT_SCALE_VALUE = "auto";
  52. exports.DEFAULT_SCALE_VALUE = DEFAULT_SCALE_VALUE;
  53. const DEFAULT_SCALE = 1.0;
  54. exports.DEFAULT_SCALE = DEFAULT_SCALE;
  55. const MIN_SCALE = 0.1;
  56. exports.MIN_SCALE = MIN_SCALE;
  57. const MAX_SCALE = 10.0;
  58. exports.MAX_SCALE = MAX_SCALE;
  59. const UNKNOWN_SCALE = 0;
  60. exports.UNKNOWN_SCALE = UNKNOWN_SCALE;
  61. const MAX_AUTO_SCALE = 1.25;
  62. exports.MAX_AUTO_SCALE = MAX_AUTO_SCALE;
  63. const SCROLLBAR_PADDING = 40;
  64. exports.SCROLLBAR_PADDING = SCROLLBAR_PADDING;
  65. const VERTICAL_PADDING = 5;
  66. exports.VERTICAL_PADDING = VERTICAL_PADDING;
  67. const LOADINGBAR_END_OFFSET_VAR = "--loadingBar-end-offset";
  68. const PresentationModeState = {
  69. UNKNOWN: 0,
  70. NORMAL: 1,
  71. CHANGING: 2,
  72. FULLSCREEN: 3
  73. };
  74. exports.PresentationModeState = PresentationModeState;
  75. const SidebarView = {
  76. UNKNOWN: -1,
  77. NONE: 0,
  78. THUMBS: 1,
  79. OUTLINE: 2,
  80. ATTACHMENTS: 3,
  81. LAYERS: 4
  82. };
  83. exports.SidebarView = SidebarView;
  84. const RendererType = {
  85. CANVAS: "canvas",
  86. SVG: "svg"
  87. };
  88. exports.RendererType = RendererType;
  89. const TextLayerMode = {
  90. DISABLE: 0,
  91. ENABLE: 1,
  92. ENABLE_ENHANCE: 2
  93. };
  94. exports.TextLayerMode = TextLayerMode;
  95. const ScrollMode = {
  96. UNKNOWN: -1,
  97. VERTICAL: 0,
  98. HORIZONTAL: 1,
  99. WRAPPED: 2
  100. };
  101. exports.ScrollMode = ScrollMode;
  102. const SpreadMode = {
  103. UNKNOWN: -1,
  104. NONE: 0,
  105. ODD: 1,
  106. EVEN: 2
  107. };
  108. exports.SpreadMode = SpreadMode;
  109. const AutoPrintRegExp = /\bprint\s*\(/;
  110. exports.AutoPrintRegExp = AutoPrintRegExp;
  111. function getOutputScale(ctx) {
  112. const devicePixelRatio = window.devicePixelRatio || 1;
  113. const backingStoreRatio = ctx.webkitBackingStorePixelRatio || ctx.mozBackingStorePixelRatio || ctx.backingStorePixelRatio || 1;
  114. const pixelRatio = devicePixelRatio / backingStoreRatio;
  115. return {
  116. sx: pixelRatio,
  117. sy: pixelRatio,
  118. scaled: pixelRatio !== 1
  119. };
  120. }
  121. function scrollIntoView(element, spot, skipOverflowHiddenElements = false) {
  122. let parent = element.offsetParent;
  123. if (!parent) {
  124. console.error("offsetParent is not set -- cannot scroll");
  125. return;
  126. }
  127. let offsetY = element.offsetTop + element.clientTop;
  128. let offsetX = element.offsetLeft + element.clientLeft;
  129. while (parent.clientHeight === parent.scrollHeight && parent.clientWidth === parent.scrollWidth || skipOverflowHiddenElements && getComputedStyle(parent).overflow === "hidden") {
  130. if (parent.dataset._scaleY) {
  131. offsetY /= parent.dataset._scaleY;
  132. offsetX /= parent.dataset._scaleX;
  133. }
  134. offsetY += parent.offsetTop;
  135. offsetX += parent.offsetLeft;
  136. parent = parent.offsetParent;
  137. if (!parent) {
  138. return;
  139. }
  140. }
  141. if (spot) {
  142. if (spot.top !== undefined) {
  143. offsetY += spot.top;
  144. }
  145. if (spot.left !== undefined) {
  146. offsetX += spot.left;
  147. parent.scrollLeft = offsetX;
  148. }
  149. }
  150. parent.scrollTop = offsetY;
  151. }
  152. function watchScroll(viewAreaElement, callback) {
  153. const debounceScroll = function (evt) {
  154. if (rAF) {
  155. return;
  156. }
  157. rAF = window.requestAnimationFrame(function viewAreaElementScrolled() {
  158. rAF = null;
  159. const currentX = viewAreaElement.scrollLeft;
  160. const lastX = state.lastX;
  161. if (currentX !== lastX) {
  162. state.right = currentX > lastX;
  163. }
  164. state.lastX = currentX;
  165. const currentY = viewAreaElement.scrollTop;
  166. const lastY = state.lastY;
  167. if (currentY !== lastY) {
  168. state.down = currentY > lastY;
  169. }
  170. state.lastY = currentY;
  171. callback(state);
  172. });
  173. };
  174. const state = {
  175. right: true,
  176. down: true,
  177. lastX: viewAreaElement.scrollLeft,
  178. lastY: viewAreaElement.scrollTop,
  179. _eventHandler: debounceScroll
  180. };
  181. let rAF = null;
  182. viewAreaElement.addEventListener("scroll", debounceScroll, true);
  183. return state;
  184. }
  185. function parseQueryString(query) {
  186. const parts = query.split("&");
  187. const params = Object.create(null);
  188. for (let i = 0, ii = parts.length; i < ii; ++i) {
  189. const param = parts[i].split("=");
  190. const key = param[0].toLowerCase();
  191. const value = param.length > 1 ? param[1] : null;
  192. params[decodeURIComponent(key)] = decodeURIComponent(value);
  193. }
  194. return params;
  195. }
  196. function binarySearchFirstItem(items, condition) {
  197. let minIndex = 0;
  198. let maxIndex = items.length - 1;
  199. if (maxIndex < 0 || !condition(items[maxIndex])) {
  200. return items.length;
  201. }
  202. if (condition(items[minIndex])) {
  203. return minIndex;
  204. }
  205. while (minIndex < maxIndex) {
  206. const currentIndex = minIndex + maxIndex >> 1;
  207. const currentItem = items[currentIndex];
  208. if (condition(currentItem)) {
  209. maxIndex = currentIndex;
  210. } else {
  211. minIndex = currentIndex + 1;
  212. }
  213. }
  214. return minIndex;
  215. }
  216. function approximateFraction(x) {
  217. if (Math.floor(x) === x) {
  218. return [x, 1];
  219. }
  220. const xinv = 1 / x;
  221. const limit = 8;
  222. if (xinv > limit) {
  223. return [1, limit];
  224. } else if (Math.floor(xinv) === xinv) {
  225. return [1, xinv];
  226. }
  227. const x_ = x > 1 ? xinv : x;
  228. let a = 0,
  229. b = 1,
  230. c = 1,
  231. d = 1;
  232. while (true) {
  233. const p = a + c,
  234. q = b + d;
  235. if (q > limit) {
  236. break;
  237. }
  238. if (x_ <= p / q) {
  239. c = p;
  240. d = q;
  241. } else {
  242. a = p;
  243. b = q;
  244. }
  245. }
  246. let result;
  247. if (x_ - a / b < c / d - x_) {
  248. result = x_ === x ? [a, b] : [b, a];
  249. } else {
  250. result = x_ === x ? [c, d] : [d, c];
  251. }
  252. return result;
  253. }
  254. function roundToDivide(x, div) {
  255. const r = x % div;
  256. return r === 0 ? x : Math.round(x - r + div);
  257. }
  258. function getPageSizeInches({
  259. view,
  260. userUnit,
  261. rotate
  262. }) {
  263. const [x1, y1, x2, y2] = view;
  264. const changeOrientation = rotate % 180 !== 0;
  265. const width = (x2 - x1) / 72 * userUnit;
  266. const height = (y2 - y1) / 72 * userUnit;
  267. return {
  268. width: changeOrientation ? height : width,
  269. height: changeOrientation ? width : height
  270. };
  271. }
  272. function backtrackBeforeAllVisibleElements(index, views, top) {
  273. if (index < 2) {
  274. return index;
  275. }
  276. let elt = views[index].div;
  277. let pageTop = elt.offsetTop + elt.clientTop;
  278. if (pageTop >= top) {
  279. elt = views[index - 1].div;
  280. pageTop = elt.offsetTop + elt.clientTop;
  281. }
  282. for (let i = index - 2; i >= 0; --i) {
  283. elt = views[i].div;
  284. if (elt.offsetTop + elt.clientTop + elt.clientHeight <= pageTop) {
  285. break;
  286. }
  287. index = i;
  288. }
  289. return index;
  290. }
  291. function getVisibleElements({
  292. scrollEl,
  293. views,
  294. sortByVisibility = false,
  295. horizontal = false,
  296. rtl = false
  297. }) {
  298. const top = scrollEl.scrollTop,
  299. bottom = top + scrollEl.clientHeight;
  300. const left = scrollEl.scrollLeft,
  301. right = left + scrollEl.clientWidth;
  302. function isElementBottomAfterViewTop(view) {
  303. const element = view.div;
  304. const elementBottom = element.offsetTop + element.clientTop + element.clientHeight;
  305. return elementBottom > top;
  306. }
  307. function isElementNextAfterViewHorizontally(view) {
  308. const element = view.div;
  309. const elementLeft = element.offsetLeft + element.clientLeft;
  310. const elementRight = elementLeft + element.clientWidth;
  311. return rtl ? elementLeft < right : elementRight > left;
  312. }
  313. const visible = [],
  314. numViews = views.length;
  315. let firstVisibleElementInd = binarySearchFirstItem(views, horizontal ? isElementNextAfterViewHorizontally : isElementBottomAfterViewTop);
  316. if (firstVisibleElementInd > 0 && firstVisibleElementInd < numViews && !horizontal) {
  317. firstVisibleElementInd = backtrackBeforeAllVisibleElements(firstVisibleElementInd, views, top);
  318. }
  319. let lastEdge = horizontal ? right : -1;
  320. for (let i = firstVisibleElementInd; i < numViews; i++) {
  321. const view = views[i],
  322. element = view.div;
  323. const currentWidth = element.offsetLeft + element.clientLeft;
  324. const currentHeight = element.offsetTop + element.clientTop;
  325. const viewWidth = element.clientWidth,
  326. viewHeight = element.clientHeight;
  327. const viewRight = currentWidth + viewWidth;
  328. const viewBottom = currentHeight + viewHeight;
  329. if (lastEdge === -1) {
  330. if (viewBottom >= bottom) {
  331. lastEdge = viewBottom;
  332. }
  333. } else if ((horizontal ? currentWidth : currentHeight) > lastEdge) {
  334. break;
  335. }
  336. if (viewBottom <= top || currentHeight >= bottom || viewRight <= left || currentWidth >= right) {
  337. continue;
  338. }
  339. const hiddenHeight = Math.max(0, top - currentHeight) + Math.max(0, viewBottom - bottom);
  340. const hiddenWidth = Math.max(0, left - currentWidth) + Math.max(0, viewRight - right);
  341. const fractionHeight = (viewHeight - hiddenHeight) / viewHeight,
  342. fractionWidth = (viewWidth - hiddenWidth) / viewWidth;
  343. const percent = fractionHeight * fractionWidth * 100 | 0;
  344. visible.push({
  345. id: view.id,
  346. x: currentWidth,
  347. y: currentHeight,
  348. view,
  349. percent,
  350. widthPercent: fractionWidth * 100 | 0
  351. });
  352. }
  353. const first = visible[0],
  354. last = visible[visible.length - 1];
  355. if (sortByVisibility) {
  356. visible.sort(function (a, b) {
  357. const pc = a.percent - b.percent;
  358. if (Math.abs(pc) > 0.001) {
  359. return -pc;
  360. }
  361. return a.id - b.id;
  362. });
  363. }
  364. return {
  365. first,
  366. last,
  367. views: visible
  368. };
  369. }
  370. function noContextMenuHandler(evt) {
  371. evt.preventDefault();
  372. }
  373. function normalizeWheelEventDirection(evt) {
  374. let delta = Math.hypot(evt.deltaX, evt.deltaY);
  375. const angle = Math.atan2(evt.deltaY, evt.deltaX);
  376. if (-0.25 * Math.PI < angle && angle < 0.75 * Math.PI) {
  377. delta = -delta;
  378. }
  379. return delta;
  380. }
  381. function normalizeWheelEventDelta(evt) {
  382. let delta = normalizeWheelEventDirection(evt);
  383. const MOUSE_DOM_DELTA_PIXEL_MODE = 0;
  384. const MOUSE_DOM_DELTA_LINE_MODE = 1;
  385. const MOUSE_PIXELS_PER_LINE = 30;
  386. const MOUSE_LINES_PER_PAGE = 30;
  387. if (evt.deltaMode === MOUSE_DOM_DELTA_PIXEL_MODE) {
  388. delta /= MOUSE_PIXELS_PER_LINE * MOUSE_LINES_PER_PAGE;
  389. } else if (evt.deltaMode === MOUSE_DOM_DELTA_LINE_MODE) {
  390. delta /= MOUSE_LINES_PER_PAGE;
  391. }
  392. return delta;
  393. }
  394. function isValidRotation(angle) {
  395. return Number.isInteger(angle) && angle % 90 === 0;
  396. }
  397. function isValidScrollMode(mode) {
  398. return Number.isInteger(mode) && Object.values(ScrollMode).includes(mode) && mode !== ScrollMode.UNKNOWN;
  399. }
  400. function isValidSpreadMode(mode) {
  401. return Number.isInteger(mode) && Object.values(SpreadMode).includes(mode) && mode !== SpreadMode.UNKNOWN;
  402. }
  403. function isPortraitOrientation(size) {
  404. return size.width <= size.height;
  405. }
  406. const WaitOnType = {
  407. EVENT: "event",
  408. TIMEOUT: "timeout"
  409. };
  410. exports.WaitOnType = WaitOnType;
  411. function waitOnEventOrTimeout({
  412. target,
  413. name,
  414. delay = 0
  415. }) {
  416. return new Promise(function (resolve, reject) {
  417. if (typeof target !== "object" || !(name && typeof name === "string") || !(Number.isInteger(delay) && delay >= 0)) {
  418. throw new Error("waitOnEventOrTimeout - invalid parameters.");
  419. }
  420. function handler(type) {
  421. if (target instanceof EventBus) {
  422. target._off(name, eventHandler);
  423. } else {
  424. target.removeEventListener(name, eventHandler);
  425. }
  426. if (timeout) {
  427. clearTimeout(timeout);
  428. }
  429. resolve(type);
  430. }
  431. const eventHandler = handler.bind(null, WaitOnType.EVENT);
  432. if (target instanceof EventBus) {
  433. target._on(name, eventHandler);
  434. } else {
  435. target.addEventListener(name, eventHandler);
  436. }
  437. const timeoutHandler = handler.bind(null, WaitOnType.TIMEOUT);
  438. const timeout = setTimeout(timeoutHandler, delay);
  439. });
  440. }
  441. const animationStarted = new Promise(function (resolve) {
  442. if (typeof window === "undefined") {
  443. setTimeout(resolve, 20);
  444. return;
  445. }
  446. window.requestAnimationFrame(resolve);
  447. });
  448. exports.animationStarted = animationStarted;
  449. function dispatchDOMEvent(eventName, args = null) {
  450. throw new Error("Not implemented: dispatchDOMEvent");
  451. }
  452. class EventBus {
  453. constructor(options) {
  454. this._listeners = Object.create(null);
  455. }
  456. on(eventName, listener, options = null) {
  457. this._on(eventName, listener, {
  458. external: true,
  459. once: options?.once
  460. });
  461. }
  462. off(eventName, listener, options = null) {
  463. this._off(eventName, listener, {
  464. external: true,
  465. once: options?.once
  466. });
  467. }
  468. dispatch(eventName) {
  469. const eventListeners = this._listeners[eventName];
  470. if (!eventListeners || eventListeners.length === 0) {
  471. return;
  472. }
  473. const args = Array.prototype.slice.call(arguments, 1);
  474. let externalListeners;
  475. eventListeners.slice(0).forEach(({
  476. listener,
  477. external,
  478. once
  479. }) => {
  480. if (once) {
  481. this._off(eventName, listener);
  482. }
  483. if (external) {
  484. (externalListeners || (externalListeners = [])).push(listener);
  485. return;
  486. }
  487. listener.apply(null, args);
  488. });
  489. if (externalListeners) {
  490. externalListeners.forEach(listener => {
  491. listener.apply(null, args);
  492. });
  493. externalListeners = null;
  494. }
  495. }
  496. _on(eventName, listener, options = null) {
  497. var _this$_listeners;
  498. const eventListeners = (_this$_listeners = this._listeners)[eventName] || (_this$_listeners[eventName] = []);
  499. eventListeners.push({
  500. listener,
  501. external: options?.external === true,
  502. once: options?.once === true
  503. });
  504. }
  505. _off(eventName, listener, options = null) {
  506. const eventListeners = this._listeners[eventName];
  507. if (!eventListeners) {
  508. return;
  509. }
  510. for (let i = 0, ii = eventListeners.length; i < ii; i++) {
  511. if (eventListeners[i].listener === listener) {
  512. eventListeners.splice(i, 1);
  513. return;
  514. }
  515. }
  516. }
  517. }
  518. exports.EventBus = EventBus;
  519. function clamp(v, min, max) {
  520. return Math.min(Math.max(v, min), max);
  521. }
  522. class ProgressBar {
  523. constructor(id, {
  524. height,
  525. width,
  526. units
  527. } = {}) {
  528. this.visible = true;
  529. this.div = document.querySelector(id + " .progress");
  530. this.bar = this.div.parentNode;
  531. this.height = height || 100;
  532. this.width = width || 100;
  533. this.units = units || "%";
  534. this.div.style.height = this.height + this.units;
  535. this.percent = 0;
  536. }
  537. _updateBar() {
  538. if (this._indeterminate) {
  539. this.div.classList.add("indeterminate");
  540. this.div.style.width = this.width + this.units;
  541. return;
  542. }
  543. this.div.classList.remove("indeterminate");
  544. const progressSize = this.width * this._percent / 100;
  545. this.div.style.width = progressSize + this.units;
  546. }
  547. get percent() {
  548. return this._percent;
  549. }
  550. set percent(val) {
  551. this._indeterminate = isNaN(val);
  552. this._percent = clamp(val, 0, 100);
  553. this._updateBar();
  554. }
  555. setWidth(viewer) {
  556. if (!viewer) {
  557. return;
  558. }
  559. const container = viewer.parentNode;
  560. const scrollbarWidth = container.offsetWidth - viewer.offsetWidth;
  561. if (scrollbarWidth > 0) {
  562. const doc = document.documentElement;
  563. doc.style.setProperty(LOADINGBAR_END_OFFSET_VAR, `${scrollbarWidth}px`);
  564. }
  565. }
  566. hide() {
  567. if (!this.visible) {
  568. return;
  569. }
  570. this.visible = false;
  571. this.bar.classList.add("hidden");
  572. }
  573. show() {
  574. if (this.visible) {
  575. return;
  576. }
  577. this.visible = true;
  578. this.bar.classList.remove("hidden");
  579. }
  580. }
  581. exports.ProgressBar = ProgressBar;
  582. function moveToEndOfArray(arr, condition) {
  583. const moved = [],
  584. len = arr.length;
  585. let write = 0;
  586. for (let read = 0; read < len; ++read) {
  587. if (condition(arr[read])) {
  588. moved.push(arr[read]);
  589. } else {
  590. arr[write] = arr[read];
  591. ++write;
  592. }
  593. }
  594. for (let read = 0; write < len; ++read, ++write) {
  595. arr[write] = moved[read];
  596. }
  597. }
  598. function getActiveOrFocusedElement() {
  599. let curRoot = document;
  600. let curActiveOrFocused = curRoot.activeElement || curRoot.querySelector(":focus");
  601. while (curActiveOrFocused?.shadowRoot) {
  602. curRoot = curActiveOrFocused.shadowRoot;
  603. curActiveOrFocused = curRoot.activeElement || curRoot.querySelector(":focus");
  604. }
  605. return curActiveOrFocused;
  606. }
  607. function apiPageLayoutToSpreadMode(layout) {
  608. switch (layout) {
  609. case "SinglePage":
  610. case "OneColumn":
  611. return SpreadMode.NONE;
  612. case "TwoColumnLeft":
  613. case "TwoPageLeft":
  614. return SpreadMode.ODD;
  615. case "TwoColumnRight":
  616. case "TwoPageRight":
  617. return SpreadMode.EVEN;
  618. }
  619. return SpreadMode.NONE;
  620. }
  621. function apiPageModeToSidebarView(mode) {
  622. switch (mode) {
  623. case "UseNone":
  624. return SidebarView.NONE;
  625. case "UseThumbs":
  626. return SidebarView.THUMBS;
  627. case "UseOutlines":
  628. return SidebarView.OUTLINE;
  629. case "UseAttachments":
  630. return SidebarView.ATTACHMENTS;
  631. case "UseOC":
  632. return SidebarView.LAYERS;
  633. }
  634. return SidebarView.NONE;
  635. }