@internetarchive/bookreader 5.0.0-6-14 → 5.0.0-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (271) hide show
  1. package/.eslintrc.js +17 -15
  2. package/.github/workflows/node.js.yml +72 -10
  3. package/.github/workflows/npm-publish.yml +6 -20
  4. package/.testcaferc.js +10 -0
  5. package/BookReader/BookReader.css +241 -140
  6. package/BookReader/BookReader.js +1 -1
  7. package/BookReader/BookReader.js.LICENSE.txt +24 -20
  8. package/BookReader/BookReader.js.map +1 -1
  9. package/BookReader/ia-bookreader-bundle.js +1519 -0
  10. package/BookReader/ia-bookreader-bundle.js.LICENSE.txt +17 -0
  11. package/BookReader/ia-bookreader-bundle.js.map +1 -0
  12. package/BookReader/icons/close-circle-dark.svg +1 -0
  13. package/BookReader/icons/magnify-minus.svg +1 -1
  14. package/BookReader/icons/magnify-plus.svg +1 -1
  15. package/BookReader/icons/pause.svg +1 -1
  16. package/BookReader/icons/playback-speed.svg +1 -1
  17. package/BookReader/icons/read-aloud.svg +1 -1
  18. package/BookReader/icons/voice.svg +1 -0
  19. package/BookReader/images/BRicons.svg +2 -2
  20. package/BookReader/images/books_graphic.svg +1 -1
  21. package/BookReader/images/icon_book.svg +1 -1
  22. package/BookReader/images/icon_gear.svg +1 -1
  23. package/BookReader/images/icon_info.svg +1 -1
  24. package/BookReader/images/icon_playback-rate.svg +1 -1
  25. package/BookReader/images/icon_search_button.svg +1 -1
  26. package/BookReader/images/icon_share.svg +1 -1
  27. package/BookReader/images/icon_speaker.svg +1 -1
  28. package/BookReader/images/icon_speaker_open.svg +1 -1
  29. package/BookReader/images/marker_chap-off.svg +1 -1
  30. package/BookReader/images/marker_chap-on.svg +1 -1
  31. package/BookReader/images/marker_srch-on.svg +1 -1
  32. package/BookReader/jquery-3.js +2 -0
  33. package/BookReader/jquery-3.js.LICENSE.txt +24 -0
  34. package/BookReader/plugins/plugin.archive_analytics.js +1 -1
  35. package/BookReader/plugins/plugin.archive_analytics.js.map +1 -1
  36. package/BookReader/plugins/plugin.autoplay.js +1 -1
  37. package/BookReader/plugins/plugin.autoplay.js.map +1 -1
  38. package/BookReader/plugins/plugin.chapters.js +1 -1
  39. package/BookReader/plugins/plugin.chapters.js.map +1 -1
  40. package/BookReader/plugins/plugin.iframe.js +1 -1
  41. package/BookReader/plugins/plugin.iframe.js.map +1 -1
  42. package/BookReader/plugins/plugin.mobile_nav.js +1 -1
  43. package/BookReader/plugins/plugin.mobile_nav.js.map +1 -1
  44. package/BookReader/plugins/plugin.resume.js +1 -1
  45. package/BookReader/plugins/plugin.resume.js.map +1 -1
  46. package/BookReader/plugins/plugin.search.js +1 -1
  47. package/BookReader/plugins/plugin.search.js.map +1 -1
  48. package/BookReader/plugins/plugin.text_selection.js +1 -1
  49. package/BookReader/plugins/plugin.text_selection.js.map +1 -1
  50. package/BookReader/plugins/plugin.tts.js +1 -1
  51. package/BookReader/plugins/plugin.tts.js.map +1 -1
  52. package/BookReader/plugins/plugin.url.js +1 -1
  53. package/BookReader/plugins/plugin.url.js.map +1 -1
  54. package/BookReader/plugins/plugin.vendor-fullscreen.js +1 -1
  55. package/BookReader/plugins/plugin.vendor-fullscreen.js.map +1 -1
  56. package/BookReader/webcomponents-bundle.js +3 -0
  57. package/BookReader/webcomponents-bundle.js.LICENSE.txt +9 -0
  58. package/BookReader/webcomponents-bundle.js.map +1 -0
  59. package/BookReaderDemo/BookReaderDemo.css +14 -1
  60. package/BookReaderDemo/BookReaderJSAutoplay.js +4 -1
  61. package/BookReaderDemo/BookReaderJSSimple.js +1 -0
  62. package/BookReaderDemo/IADemoBr.js +147 -0
  63. package/BookReaderDemo/demo-advanced.html +2 -2
  64. package/BookReaderDemo/demo-autoplay.html +2 -1
  65. package/BookReaderDemo/demo-embed-iframe-src.html +2 -1
  66. package/BookReaderDemo/demo-fullscreen-mobile.html +2 -1
  67. package/BookReaderDemo/demo-fullscreen.html +2 -1
  68. package/BookReaderDemo/demo-iiif.html +2 -1
  69. package/BookReaderDemo/demo-internetarchive.html +84 -17
  70. package/BookReaderDemo/demo-multiple.html +2 -1
  71. package/BookReaderDemo/demo-preview-pages.html +2 -1
  72. package/BookReaderDemo/demo-simple.html +2 -1
  73. package/BookReaderDemo/demo-vendor-fullscreen.html +2 -1
  74. package/BookReaderDemo/ia-multiple-volumes-manifest.js +170 -0
  75. package/BookReaderDemo/immersion-1up.html +2 -1
  76. package/BookReaderDemo/immersion-mode.html +2 -1
  77. package/BookReaderDemo/toggle_controls.html +2 -1
  78. package/BookReaderDemo/view_mode.html +2 -1
  79. package/BookReaderDemo/viewmode-cycle.html +2 -3
  80. package/CHANGELOG.md +244 -0
  81. package/README.md +14 -1
  82. package/babel.config.js +19 -0
  83. package/codecov.yml +6 -0
  84. package/index.html +3 -0
  85. package/jsconfig.json +19 -0
  86. package/netlify.toml +5 -0
  87. package/package.json +70 -59
  88. package/renovate.json +52 -0
  89. package/scripts/preversion.js +4 -1
  90. package/src/BookNavigator/assets/bookmark-colors.js +1 -1
  91. package/src/BookNavigator/assets/button-base.js +9 -2
  92. package/src/BookNavigator/assets/ia-logo.js +17 -0
  93. package/src/BookNavigator/assets/icon_checkmark.js +1 -1
  94. package/src/BookNavigator/assets/icon_close.js +1 -1
  95. package/src/BookNavigator/assets/icon_sort_asc.js +5 -0
  96. package/src/BookNavigator/assets/icon_sort_desc.js +5 -0
  97. package/src/BookNavigator/assets/icon_sort_neutral.js +5 -0
  98. package/src/BookNavigator/assets/icon_volumes.js +11 -0
  99. package/src/BookNavigator/book-navigator.js +585 -0
  100. package/src/BookNavigator/bookmarks/bookmark-button.js +3 -2
  101. package/src/BookNavigator/bookmarks/bookmark-edit.js +3 -4
  102. package/src/BookNavigator/bookmarks/bookmarks-list.js +2 -3
  103. package/src/BookNavigator/bookmarks/bookmarks-loginCTA.js +3 -8
  104. package/src/BookNavigator/bookmarks/bookmarks-provider.js +27 -17
  105. package/src/BookNavigator/bookmarks/ia-bookmarks.js +116 -67
  106. package/src/BookNavigator/delete-modal-actions.js +1 -1
  107. package/src/BookNavigator/downloads/downloads-provider.js +36 -21
  108. package/src/BookNavigator/downloads/downloads.js +41 -25
  109. package/src/BookNavigator/search/search-provider.js +80 -28
  110. package/src/BookNavigator/search/search-results.js +34 -25
  111. package/src/BookNavigator/sharing.js +27 -0
  112. package/src/BookNavigator/visual-adjustments/visual-adjustments-provider.js +11 -10
  113. package/src/BookNavigator/visual-adjustments/visual-adjustments.js +3 -3
  114. package/src/BookNavigator/volumes/volumes-provider.js +111 -0
  115. package/src/BookNavigator/volumes/volumes.js +188 -0
  116. package/src/BookReader/BookModel.js +59 -30
  117. package/src/BookReader/DebugConsole.js +3 -3
  118. package/src/BookReader/DragScrollable.js +233 -0
  119. package/src/BookReader/Mode1Up.js +56 -351
  120. package/src/BookReader/Mode1UpLit.js +391 -0
  121. package/src/BookReader/Mode2Up.js +73 -1318
  122. package/src/BookReader/Mode2UpLit.js +781 -0
  123. package/src/BookReader/ModeCoordinateSpace.js +29 -0
  124. package/src/BookReader/ModeSmoothZoom.js +211 -0
  125. package/src/BookReader/ModeThumb.js +17 -11
  126. package/src/BookReader/Navbar/Navbar.js +10 -36
  127. package/src/BookReader/PageContainer.js +69 -6
  128. package/src/BookReader/ReduceSet.js +1 -1
  129. package/src/BookReader/Toolbar/Toolbar.js +10 -37
  130. package/src/BookReader/events.js +2 -0
  131. package/src/BookReader/options.js +24 -2
  132. package/src/BookReader/utils/HTMLDimensionsCacher.js +44 -0
  133. package/src/BookReader/utils/ScrollClassAdder.js +31 -0
  134. package/src/BookReader/utils.js +108 -13
  135. package/src/BookReader.js +481 -828
  136. package/src/assets/icons/close-circle-dark.svg +1 -0
  137. package/src/assets/icons/magnify-minus.svg +3 -7
  138. package/src/assets/icons/magnify-plus.svg +3 -7
  139. package/src/assets/icons/voice.svg +1 -0
  140. package/src/css/_BRBookmarks.scss +1 -1
  141. package/src/css/_BRComponent.scss +1 -1
  142. package/src/css/_BRmain.scss +33 -0
  143. package/src/css/_BRnav.scss +4 -26
  144. package/src/css/_BRpages.scss +147 -40
  145. package/src/css/_BRsearch.scss +25 -11
  146. package/src/css/_TextSelection.scss +16 -17
  147. package/src/css/_colorbox.scss +2 -2
  148. package/src/css/_controls.scss +17 -5
  149. package/src/css/_icons.scss +7 -1
  150. package/src/ia-bookreader/ia-bookreader.js +224 -0
  151. package/src/plugins/plugin.archive_analytics.js +3 -3
  152. package/src/plugins/plugin.autoplay.js +4 -9
  153. package/src/plugins/plugin.chapters.js +28 -35
  154. package/src/plugins/plugin.mobile_nav.js +11 -10
  155. package/src/plugins/plugin.resume.js +3 -3
  156. package/src/plugins/plugin.text_selection.js +32 -41
  157. package/src/plugins/plugin.vendor-fullscreen.js +4 -4
  158. package/src/plugins/search/plugin.search.js +179 -103
  159. package/src/plugins/search/view.js +59 -44
  160. package/src/plugins/tts/AbstractTTSEngine.js +46 -37
  161. package/src/plugins/tts/FestivalTTSEngine.js +13 -14
  162. package/src/plugins/tts/PageChunk.js +15 -21
  163. package/src/plugins/tts/PageChunkIterator.js +8 -12
  164. package/src/plugins/tts/WebTTSEngine.js +87 -71
  165. package/src/plugins/tts/plugin.tts.js +94 -125
  166. package/src/plugins/tts/utils.js +0 -25
  167. package/src/plugins/url/UrlPlugin.js +193 -0
  168. package/src/plugins/{plugin.url.js → url/plugin.url.js} +45 -16
  169. package/src/util/docCookies.js +21 -2
  170. package/tests/e2e/README.md +37 -0
  171. package/tests/e2e/autoplay.test.js +2 -2
  172. package/tests/e2e/base.test.js +7 -7
  173. package/tests/e2e/helpers/base.js +28 -23
  174. package/tests/e2e/helpers/debug.js +1 -1
  175. package/tests/e2e/helpers/desktopSearch.js +14 -13
  176. package/tests/e2e/helpers/mobileSearch.js +3 -3
  177. package/tests/e2e/helpers/params.js +17 -0
  178. package/tests/e2e/helpers/rightToLeft.js +4 -10
  179. package/tests/e2e/models/Navigation.js +13 -4
  180. package/tests/e2e/rightToLeft.test.js +4 -5
  181. package/tests/e2e/viewmode.test.js +40 -33
  182. package/tests/jest/BookNavigator/book-navigator.test.js +658 -0
  183. package/tests/jest/BookNavigator/bookmarks/bookmark-button.test.js +43 -0
  184. package/tests/{karma → jest}/BookNavigator/bookmarks/bookmark-edit.test.js +25 -26
  185. package/tests/{karma → jest}/BookNavigator/bookmarks/bookmarks-list.test.js +41 -42
  186. package/tests/jest/BookNavigator/bookmarks/ia-bookmarks.test.js +45 -0
  187. package/tests/jest/BookNavigator/downloads/downloads-provider.test.js +67 -0
  188. package/tests/jest/BookNavigator/downloads/downloads.test.js +53 -0
  189. package/tests/jest/BookNavigator/search/search-provider.test.js +167 -0
  190. package/tests/{karma/BookNavigator → jest/BookNavigator/search}/search-results.test.js +104 -60
  191. package/tests/jest/BookNavigator/sharing/sharing-provider.test.js +49 -0
  192. package/tests/jest/BookNavigator/visual-adjustments.test.js +200 -0
  193. package/tests/jest/BookNavigator/volumes/volumes-provider.test.js +184 -0
  194. package/tests/jest/BookNavigator/volumes/volumes.test.js +97 -0
  195. package/tests/{BookReader → jest/BookReader}/BookModel.test.js +59 -14
  196. package/tests/jest/BookReader/BookReaderPublicFunctions.test.js +193 -0
  197. package/tests/{BookReader → jest/BookReader}/DebugConsole.test.js +1 -1
  198. package/tests/{BookReader → jest/BookReader}/ImageCache.test.js +4 -4
  199. package/tests/jest/BookReader/Mode1UpLit.test.js +73 -0
  200. package/tests/jest/BookReader/Mode2Up.test.js +98 -0
  201. package/tests/jest/BookReader/Mode2UpLit.test.js +190 -0
  202. package/tests/jest/BookReader/ModeCoordinateSpace.test.js +16 -0
  203. package/tests/jest/BookReader/ModeSmoothZoom.test.js +175 -0
  204. package/tests/jest/BookReader/ModeThumb.test.js +71 -0
  205. package/tests/{BookReader → jest/BookReader}/Navbar/Navbar.test.js +10 -10
  206. package/tests/{BookReader → jest/BookReader}/PageContainer.test.js +88 -6
  207. package/tests/{BookReader → jest/BookReader}/ReduceSet.test.js +1 -1
  208. package/tests/{BookReader → jest/BookReader}/Toolbar/Toolbar.test.js +2 -2
  209. package/tests/jest/BookReader/utils/HTMLDimensionsCacher.test.js +59 -0
  210. package/tests/jest/BookReader/utils/ScrollClassAdder.test.js +49 -0
  211. package/tests/{BookReader → jest/BookReader}/utils/classes.test.js +1 -1
  212. package/tests/jest/BookReader/utils.test.js +217 -0
  213. package/tests/jest/BookReader.keyboard.test.js +190 -0
  214. package/tests/{BookReader.options.test.js → jest/BookReader.options.test.js} +9 -1
  215. package/tests/{BookReader.test.js → jest/BookReader.test.js} +26 -37
  216. package/tests/{plugins → jest/plugins}/plugin.archive_analytics.test.js +2 -2
  217. package/tests/{plugins → jest/plugins}/plugin.autoplay.test.js +4 -4
  218. package/tests/{plugins → jest/plugins}/plugin.chapters.test.js +10 -11
  219. package/tests/{plugins → jest/plugins}/plugin.iframe.test.js +2 -2
  220. package/tests/{plugins → jest/plugins}/plugin.mobile_nav.test.js +5 -5
  221. package/tests/{plugins → jest/plugins}/plugin.resume.test.js +3 -3
  222. package/tests/{plugins → jest/plugins}/plugin.text_selection.test.js +39 -47
  223. package/tests/{plugins → jest/plugins}/plugin.vendor-fullscreen.test.js +2 -2
  224. package/tests/{plugins → jest/plugins}/search/plugin.search.test.js +57 -47
  225. package/tests/{plugins → jest/plugins}/search/plugin.search.view.test.js +35 -6
  226. package/tests/{plugins → jest/plugins}/tts/AbstractTTSEngine.test.js +9 -9
  227. package/tests/{plugins → jest/plugins}/tts/FestivalTTSEngine.test.js +4 -4
  228. package/tests/{plugins → jest/plugins}/tts/PageChunk.test.js +1 -1
  229. package/tests/{plugins → jest/plugins}/tts/PageChunkIterator.test.js +3 -3
  230. package/tests/{plugins → jest/plugins}/tts/WebTTSEngine.test.js +47 -1
  231. package/tests/{plugins → jest/plugins}/tts/utils.test.js +1 -60
  232. package/tests/jest/plugins/url/UrlPlugin.test.js +190 -0
  233. package/tests/{plugins → jest/plugins/url}/plugin.url.test.js +53 -14
  234. package/tests/jest/setup.js +3 -0
  235. package/tests/{util → jest/util}/browserSniffing.test.js +1 -1
  236. package/tests/jest/util/docCookies.test.js +24 -0
  237. package/tests/{util → jest/util}/strings.test.js +1 -1
  238. package/tests/{utils.js → jest/utils.js} +38 -0
  239. package/webpack.config.js +11 -5
  240. package/.babelrc +0 -12
  241. package/.dependabot/config.yml +0 -6
  242. package/.testcaferc.json +0 -5
  243. package/BookReader/bookreader-component-bundle.js +0 -1450
  244. package/BookReader/bookreader-component-bundle.js.LICENSE.txt +0 -38
  245. package/BookReader/bookreader-component-bundle.js.map +0 -1
  246. package/BookReader/jquery-1.10.1.js +0 -2
  247. package/BookReader/jquery-1.10.1.js.LICENSE.txt +0 -24
  248. package/BookReader/plugins/plugin.menu_toggle.js +0 -2
  249. package/BookReader/plugins/plugin.menu_toggle.js.map +0 -1
  250. package/BookReaderDemo/bookreader-template-bundle.js +0 -7178
  251. package/BookReaderDemo/demo-plugin-menu-toggle.html +0 -34
  252. package/karma.conf.js +0 -23
  253. package/src/BookNavigator/BookModel.js +0 -14
  254. package/src/BookNavigator/BookNavigator.js +0 -438
  255. package/src/BookNavigator/assets/book-loader.js +0 -27
  256. package/src/BookNavigator/br-fullscreen-mgr.js +0 -83
  257. package/src/BookNavigator/search/a-search-result.js +0 -55
  258. package/src/BookReaderComponent/BookReaderComponent.js +0 -112
  259. package/src/ItemNavigator/ItemNavigator.js +0 -372
  260. package/src/ItemNavigator/providers/sharing.js +0 -29
  261. package/src/dragscrollable-br.js +0 -261
  262. package/src/plugins/menu_toggle/plugin.menu_toggle.js +0 -324
  263. package/tests/BookReader/BookReaderPublicFunctions.test.js +0 -171
  264. package/tests/BookReader/Mode1Up.test.js +0 -164
  265. package/tests/BookReader/Mode2Up.test.js +0 -247
  266. package/tests/BookReader/utils.test.js +0 -109
  267. package/tests/e2e/ia-production/ia-prod-base.js +0 -17
  268. package/tests/karma/BookNavigator/book-navigator.test.js +0 -132
  269. package/tests/karma/BookNavigator/visual-adjustments.test.js +0 -201
  270. package/tests/plugins/menu_toggle/plugin.menu_toggle.test.js +0 -68
  271. package/tests/util/docCookies.test.js +0 -15
@@ -25,9 +25,13 @@ export const DEFAULT_OPTIONS = {
25
25
  thumbMaxLoading: 4,
26
26
  /** spacing between thumbnails */
27
27
  thumbPadding: 10,
28
+ /** min zoom in columns */
29
+ thumbMinZoomColumns: 2,
30
+ /** max zoom out columns */
31
+ thumbMaxZoomColumns: 8,
28
32
 
29
33
  /** @type {number | 'fast' | 'slow'} speed for flip animation */
30
- flipSpeed: 'fast',
34
+ flipSpeed: 400,
31
35
 
32
36
  showToolbar: true,
33
37
  showNavbar: true,
@@ -275,6 +279,12 @@ export const DEFAULT_OPTIONS = {
275
279
  */
276
280
  startFullscreen: false,
277
281
 
282
+ /**
283
+ * @type {Boolean}
284
+ * will show logo at fullscreen mode
285
+ */
286
+ enableFSLogoShortcut: false,
287
+
278
288
  /**
279
289
  * @type {Boolean}
280
290
  * On init, by default, we want to handle resizing bookreader
@@ -290,7 +300,13 @@ export const DEFAULT_OPTIONS = {
290
300
  useSrcSet: false,
291
301
  };
292
302
 
293
- /** @typedef {'width' | 'height' | 'auto' | 'none'} AutoFitValues */
303
+ /**
304
+ * @typedef {'width' | 'height' | 'auto' | 'none'} AutoFitValues
305
+ * - width: fill the width of the container
306
+ * - height: fill the height of the container
307
+ * - auto: fill the width or height of the container, whichever is smaller
308
+ * - none: do not autofit
309
+ **/
294
310
 
295
311
  /**
296
312
  * @typedef {object} ReductionFactor
@@ -318,3 +334,9 @@ export const DEFAULT_OPTIONS = {
318
334
 
319
335
  /** @typedef {typeof DEFAULT_OPTIONS} BookReaderOptions */
320
336
 
337
+ /**
338
+ * Thrown when an error occurs while parsing options.
339
+ * Potentially recoverable and non-halting.
340
+ */
341
+ export class OptionsParseError extends Error {
342
+ }
@@ -0,0 +1,44 @@
1
+ // @ts-check
2
+ import { debounce } from '../utils';
3
+
4
+ /**
5
+ * Computing these things repeatedly is expensive (the browser needs to
6
+ * do a lot of computations/redrawing to make sure these are correct),
7
+ * so we store them here, and only recompute them when necessary:
8
+ * - window resize could have cause the container to change size
9
+ * - zoom could have cause scrollbars to appear/disappear, changing
10
+ * the client size.
11
+ */
12
+ export class HTMLDimensionsCacher {
13
+ clientWidth = 100;
14
+ clientHeight = 100;
15
+
16
+ boundingClientRect = { top: 0, left: 0 };
17
+
18
+ /**
19
+ * @param {HTMLElement} element
20
+ */
21
+ constructor(element) {
22
+ /** @type {HTMLElement} */
23
+ this.element = element;
24
+ }
25
+
26
+ updateClientSizes = () => {
27
+ const bc = this.element.getBoundingClientRect();
28
+ this.clientWidth = this.element.clientWidth;
29
+ this.clientHeight = this.element.clientHeight;
30
+ this.boundingClientRect.top = bc.top;
31
+ this.boundingClientRect.left = bc.left;
32
+ }
33
+ debouncedUpdateClientSizes = debounce(this.updateClientSizes, 150, false);
34
+
35
+ /** @param {EventTarget} win */
36
+ attachResizeListener(win = window) {
37
+ win.addEventListener('resize', this.debouncedUpdateClientSizes);
38
+ }
39
+
40
+ /** @param {EventTarget} win */
41
+ detachResizeListener(win = window) {
42
+ win.removeEventListener('resize', this.debouncedUpdateClientSizes);
43
+ }
44
+ }
@@ -0,0 +1,31 @@
1
+ /** Adds a class while the given element is experiencing scrolling */
2
+ export class ScrollClassAdder {
3
+ /**
4
+ * @param {HTMLElement} element
5
+ * @param {string} className
6
+ */
7
+ constructor(element, className) {
8
+ /** @type {HTMLElement} */
9
+ this.element = element;
10
+ /** @type {string} */
11
+ this.className = className;
12
+ this.timeout = null;
13
+ }
14
+
15
+ attach() {
16
+ this.element.addEventListener('scroll', this.onScroll);
17
+ }
18
+
19
+ detach() {
20
+ this.element.removeEventListener('scroll', this.onScroll);
21
+ }
22
+
23
+ onScroll = () => {
24
+ this.element.classList.add(this.className);
25
+ clearTimeout(this.timeout);
26
+ // TODO: Also remove class on mousemove, touch, click, etc.
27
+ this.timeout = setTimeout(() => {
28
+ this.element.classList.remove(this.className);
29
+ }, 600);
30
+ }
31
+ }
@@ -40,6 +40,24 @@ export function notInArray(value, array) {
40
40
  return !array.includes(value);
41
41
  }
42
42
 
43
+ /**
44
+ * Determines the active element, going into shadow doms.
45
+ * @return {Element}
46
+ */
47
+ export function getActiveElement(doc = document, recurseShadowDom = true) {
48
+ const activeElement = doc.activeElement;
49
+ if (recurseShadowDom && activeElement?.shadowRoot) {
50
+ return getActiveElement(activeElement.shadowRoot, true);
51
+ }
52
+ return activeElement;
53
+ }
54
+
55
+ /** Check if an input field/textarea is active. Also checks shadow DOMs. */
56
+ export function isInputActive(doc = document) {
57
+ const activeEl = getActiveElement(doc);
58
+ return activeEl?.tagName == "INPUT" || activeEl?.tagName == "TEXTAREA";
59
+ }
60
+
43
61
  /**
44
62
  * @param {HTMLIFrameElement} iframe
45
63
  * @return {Document}
@@ -80,16 +98,17 @@ export function encodeURIComponentPlus(value) {
80
98
  }
81
99
 
82
100
  /**
101
+ * @template {Function} T
83
102
  * Returns a function, that, as long as it continues to be invoked, will not
84
103
  * be triggered. The function will be called after it stops being called for
85
104
  * N milliseconds. If `immediate` is passed, trigger the function on the
86
105
  * leading edge, instead of the trailing.
87
106
  * @see https://davidwalsh.name/javascript-debounce-function
88
107
  *
89
- * @param {Function} func
108
+ * @param {T} func
90
109
  * @param {number} wait
91
110
  * @param {boolean} immediate
92
- * @return {Function}
111
+ * @return {T}
93
112
  */
94
113
  export function debounce(func, wait, immediate) {
95
114
  let timeout;
@@ -108,12 +127,13 @@ export function debounce(func, wait, immediate) {
108
127
  }
109
128
 
110
129
  /**
130
+ * @template T
111
131
  * Throttle function
112
132
  * @see https://remysharp.com/2010/07/21/throttling-function-calls
113
- * @param {Function} fn
133
+ * @param {T} fn
114
134
  * @param {number} threshold
115
135
  * @param {boolean} delay
116
- * @return {Function}
136
+ * @return {T}
117
137
  */
118
138
  export function throttle(fn, threshold, delay) {
119
139
  threshold || (threshold = 250);
@@ -160,14 +180,6 @@ export function PolyfilledCustomEvent(eventName, {bubbles = false, cancelable =
160
180
  return event;
161
181
  }
162
182
 
163
- /**
164
- * Promise based sleep - resolves at default 500ms
165
- * @param {Number} wait time in milliseconds
166
- */
167
- export function sleep(ms = 500) {
168
- return new Promise(res => setTimeout(res(true), ms));
169
- }
170
-
171
183
  /*
172
184
  * Returns the number pixels something should be rendered at to be ~1n on the users
173
185
  * screen when measured with a ruler.
@@ -179,7 +191,90 @@ export function calcScreenDPI() {
179
191
  const dpi = el.offsetWidth;
180
192
  document.body.removeChild(el);
181
193
 
182
- const screenDPI = dpi * devicePixelRatio;
194
+ // Do you believe in magic... numbers? We tested on some devices, and the displayed
195
+ // size of `width: 1in` was less than desired. On @pezvi's mac, it was ~75% ; on
196
+ // @cdrini's laptop it was ~85%. Since we want to avoid things appearing too small,
197
+ // let's just use a multiplier of 1.25
198
+ const screenDPI = dpi * 1.25;
183
199
  // This will return 0 in testing; never want it to be 0!
184
200
  return screenDPI == 0 ? 100 : screenDPI;
185
201
  }
202
+
203
+ /**
204
+ * @param {number[]} nums
205
+ * @returns {number}
206
+ */
207
+ export function sum(nums) {
208
+ return nums.reduce((cur, acc) => cur + acc, 0);
209
+ }
210
+
211
+ /**
212
+ * @template T
213
+ * @param {Generator<T>} gen
214
+ * @returns {T[]}
215
+ */
216
+ export function genToArray(gen) {
217
+ const result = [];
218
+ for (const item of gen) {
219
+ result.push(item);
220
+ }
221
+ return result;
222
+ }
223
+
224
+ /**
225
+ * Check if arrays contain the same elements. Does reference comparison.
226
+ * @param {Array} arr1
227
+ * @param {Array} arr2
228
+ */
229
+ export function arrEquals(arr1, arr2) {
230
+ return arr1.length == arr2.length && arr1.every((x, i) => x == arr2[i]);
231
+ }
232
+
233
+ /**
234
+ * Check if array has changed; namely to be used with lit's property.hasChanged
235
+ * @param {Array} [arr1]
236
+ * @param {Array} [arr2]
237
+ */
238
+ export function arrChanged(arr1, arr2) {
239
+ return arr1 && arr2 && !arrEquals(arr1, arr2);
240
+ }
241
+
242
+ /**
243
+ * Waits the provided number of ms and then resolves with a promise
244
+ * @param {number} ms
245
+ **/
246
+ export async function sleep(ms) {
247
+ return new Promise(resolve => setTimeout(resolve, ms));
248
+ }
249
+
250
+ /**
251
+ * @template T
252
+ * @param {function(): T} fn
253
+ * @param {Object} options
254
+ * @param {function(T): boolean} [options.until]
255
+ * @return {T | undefined}
256
+ */
257
+ export async function poll(fn, { step = 50, timeout = 500, until = val => Boolean(val), _sleep = sleep } = {}) {
258
+ const startTime = Date.now();
259
+ while (Date.now() - startTime < timeout) {
260
+ const result = fn();
261
+ if (until(result)) return result;
262
+ await _sleep(step);
263
+ }
264
+ }
265
+
266
+ /**
267
+ * Convert a EventTarget style event into a promise
268
+ * @param {EventTarget} target
269
+ * @param {string} eventType
270
+ * @return {Promise<Event>}
271
+ */
272
+ export function promisifyEvent(target, eventType) {
273
+ return new Promise(res => {
274
+ const resolver = ev => {
275
+ target.removeEventListener(eventType, resolver);
276
+ res(ev);
277
+ };
278
+ target.addEventListener(eventType, resolver);
279
+ });
280
+ }