@internetarchive/bookreader 5.0.0-4 → 5.0.0-40-a1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +17 -15
- package/.github/workflows/node.js.yml +75 -4
- package/.github/workflows/npm-publish.yml +2 -16
- package/.testcaferc.js +10 -0
- package/BookReader/BookReader.css +83 -323
- package/BookReader/BookReader.js +1 -1
- package/BookReader/BookReader.js.LICENSE.txt +24 -0
- package/BookReader/BookReader.js.map +1 -1
- package/BookReader/ia-bookreader-bundle.js +1623 -0
- package/BookReader/{bookreader-component-bundle.js.LICENSE.txt → ia-bookreader-bundle.js.LICENSE.txt} +14 -10
- package/BookReader/ia-bookreader-bundle.js.map +1 -0
- package/BookReader/icons/close-circle-dark.svg +1 -0
- package/BookReader/icons/magnify-minus.svg +1 -1
- package/BookReader/icons/magnify-plus.svg +1 -1
- package/BookReader/icons/voice.svg +1 -0
- package/BookReader/plugins/plugin.archive_analytics.js +1 -1
- package/BookReader/plugins/plugin.archive_analytics.js.map +1 -1
- package/BookReader/plugins/plugin.autoplay.js +1 -1
- package/BookReader/plugins/plugin.autoplay.js.map +1 -1
- package/BookReader/plugins/plugin.chapters.js +1 -1
- package/BookReader/plugins/plugin.chapters.js.map +1 -1
- package/BookReader/plugins/plugin.iframe.js +1 -1
- package/BookReader/plugins/plugin.iframe.js.map +1 -1
- package/BookReader/plugins/plugin.mobile_nav.js +1 -1
- package/BookReader/plugins/plugin.mobile_nav.js.map +1 -1
- package/BookReader/plugins/plugin.resume.js +1 -1
- package/BookReader/plugins/plugin.resume.js.map +1 -1
- package/BookReader/plugins/plugin.search.js +1 -1
- package/BookReader/plugins/plugin.search.js.map +1 -1
- package/BookReader/plugins/plugin.text_selection.js +1 -1
- package/BookReader/plugins/plugin.text_selection.js.map +1 -1
- package/BookReader/plugins/plugin.tts.js +1 -1
- package/BookReader/plugins/plugin.tts.js.map +1 -1
- package/BookReader/plugins/plugin.url.js +1 -1
- package/BookReader/plugins/plugin.url.js.map +1 -1
- package/BookReader/plugins/plugin.vendor-fullscreen.js +1 -1
- package/BookReader/plugins/plugin.vendor-fullscreen.js.map +1 -1
- package/BookReader/webcomponents-bundle.js +3 -0
- package/BookReader/webcomponents-bundle.js.LICENSE.txt +9 -0
- package/BookReader/webcomponents-bundle.js.map +1 -0
- package/BookReaderDemo/BookReaderDemo.css +14 -1
- package/BookReaderDemo/IADemoBr.js +120 -0
- package/BookReaderDemo/demo-advanced.html +1 -1
- package/BookReaderDemo/demo-autoplay.html +1 -0
- package/BookReaderDemo/demo-embed-iframe-src.html +1 -0
- package/BookReaderDemo/demo-fullscreen-mobile.html +1 -0
- package/BookReaderDemo/demo-fullscreen.html +1 -0
- package/BookReaderDemo/demo-iiif.html +1 -0
- package/BookReaderDemo/demo-internetarchive.html +74 -17
- package/BookReaderDemo/demo-multiple.html +1 -0
- package/BookReaderDemo/demo-preview-pages.html +1 -0
- package/BookReaderDemo/demo-simple.html +1 -0
- package/BookReaderDemo/demo-vendor-fullscreen.html +1 -0
- package/BookReaderDemo/ia-multiple-volumes-manifest.js +170 -0
- package/BookReaderDemo/immersion-1up.html +1 -0
- package/BookReaderDemo/immersion-mode.html +1 -0
- package/BookReaderDemo/toggle_controls.html +1 -0
- package/BookReaderDemo/view_mode.html +1 -0
- package/BookReaderDemo/viewmode-cycle.html +1 -2
- package/CHANGELOG.md +166 -0
- package/README.md +14 -1
- package/babel.config.js +18 -0
- package/codecov.yml +6 -0
- package/index.html +3 -0
- package/jsconfig.json +19 -0
- package/package.json +62 -47
- package/renovate.json +43 -0
- package/src/BookNavigator/assets/bookmark-colors.js +1 -1
- package/src/BookNavigator/assets/button-base.js +9 -2
- package/src/BookNavigator/assets/ia-logo.js +17 -0
- package/src/BookNavigator/assets/icon_checkmark.js +1 -1
- package/src/BookNavigator/assets/icon_close.js +1 -1
- package/src/BookNavigator/assets/icon_sort_asc.js +5 -0
- package/src/BookNavigator/assets/icon_sort_desc.js +5 -0
- package/src/BookNavigator/assets/icon_sort_neutral.js +5 -0
- package/src/BookNavigator/assets/icon_volumes.js +11 -0
- package/src/BookNavigator/book-navigator.js +556 -0
- package/src/BookNavigator/bookmarks/bookmark-button.js +3 -2
- package/src/BookNavigator/bookmarks/bookmark-edit.js +4 -4
- package/src/BookNavigator/bookmarks/bookmarks-list.js +3 -3
- package/src/BookNavigator/bookmarks/bookmarks-loginCTA.js +3 -8
- package/src/BookNavigator/bookmarks/bookmarks-provider.js +23 -12
- package/src/BookNavigator/bookmarks/ia-bookmarks.js +98 -62
- package/src/BookNavigator/delete-modal-actions.js +1 -1
- package/src/BookNavigator/downloads/downloads-provider.js +23 -17
- package/src/BookNavigator/downloads/downloads.js +17 -25
- package/src/BookNavigator/search/a-search-result.js +3 -3
- package/src/BookNavigator/search/search-provider.js +57 -24
- package/src/BookNavigator/search/search-results.js +8 -20
- package/src/BookNavigator/sharing.js +27 -0
- package/src/BookNavigator/visual-adjustments/visual-adjustments-provider.js +11 -13
- package/src/BookNavigator/visual-adjustments/visual-adjustments.js +4 -3
- package/src/BookNavigator/volumes/volumes-provider.js +114 -0
- package/src/BookNavigator/volumes/volumes.js +188 -0
- package/src/BookReader/DebugConsole.js +3 -3
- package/src/BookReader/DragScrollable.js +233 -0
- package/src/BookReader/Mode1Up.js +51 -351
- package/src/BookReader/Mode1UpLit.js +441 -0
- package/src/BookReader/Mode2Up.js +104 -71
- package/src/BookReader/ModeSmoothZoom.js +179 -0
- package/src/BookReader/ModeThumb.js +16 -8
- package/src/BookReader/Navbar/Navbar.js +2 -31
- package/src/BookReader/PageContainer.js +57 -6
- package/src/BookReader/ReduceSet.js +1 -1
- package/src/BookReader/Toolbar/Toolbar.js +7 -7
- package/src/BookReader/options.js +10 -0
- package/src/BookReader/utils/HTMLDimensionsCacher.js +44 -0
- package/src/BookReader/utils/ScrollClassAdder.js +31 -0
- package/src/BookReader/utils.js +68 -13
- package/src/BookReader.js +375 -289
- package/src/assets/icons/close-circle-dark.svg +1 -0
- package/src/assets/icons/magnify-minus.svg +3 -7
- package/src/assets/icons/magnify-plus.svg +3 -7
- package/src/assets/icons/voice.svg +1 -0
- package/src/css/BookReader.scss +0 -12
- package/src/css/_BRComponent.scss +1 -1
- package/src/css/_BRmain.scss +19 -24
- package/src/css/_BRnav.scss +4 -26
- package/src/css/_BRpages.scss +35 -0
- package/src/css/_BRsearch.scss +11 -215
- package/src/css/_TextSelection.scss +14 -17
- package/src/css/_colorbox.scss +2 -2
- package/src/css/_controls.scss +16 -3
- package/src/css/_icons.scss +6 -0
- package/src/ia-bookreader/ia-bookreader.js +224 -0
- package/src/plugins/plugin.chapters.js +26 -33
- package/src/plugins/plugin.mobile_nav.js +11 -10
- package/src/plugins/plugin.resume.js +3 -3
- package/src/plugins/plugin.text_selection.js +26 -39
- package/src/plugins/plugin.vendor-fullscreen.js +4 -4
- package/src/plugins/search/plugin.search.js +106 -107
- package/src/plugins/search/view.js +50 -163
- package/src/plugins/tts/AbstractTTSEngine.js +46 -37
- package/src/plugins/tts/FestivalTTSEngine.js +12 -13
- package/src/plugins/tts/PageChunk.js +15 -21
- package/src/plugins/tts/PageChunkIterator.js +8 -12
- package/src/plugins/tts/WebTTSEngine.js +64 -68
- package/src/plugins/tts/plugin.tts.js +79 -108
- package/src/plugins/url/UrlPlugin.js +184 -0
- package/src/plugins/{plugin.url.js → url/plugin.url.js} +28 -6
- package/tests/e2e/README.md +37 -0
- package/tests/e2e/autoplay.test.js +2 -2
- package/tests/e2e/base.test.js +7 -7
- package/tests/e2e/helpers/base.js +8 -3
- package/tests/e2e/helpers/debug.js +1 -1
- package/tests/e2e/helpers/desktopSearch.js +14 -13
- package/tests/e2e/helpers/mobileSearch.js +3 -3
- package/tests/e2e/helpers/params.js +17 -0
- package/tests/e2e/models/Navigation.js +12 -3
- package/tests/e2e/rightToLeft.test.js +4 -5
- package/tests/e2e/viewmode.test.js +38 -33
- package/tests/{BookReader → jest/BookReader}/BookModel.test.js +3 -3
- package/tests/jest/BookReader/BookReaderPublicFunctions.test.js +176 -0
- package/tests/{BookReader → jest/BookReader}/DebugConsole.test.js +1 -1
- package/tests/{BookReader → jest/BookReader}/ImageCache.test.js +4 -4
- package/tests/jest/BookReader/Mode1UpLit.test.js +88 -0
- package/tests/{BookReader → jest/BookReader}/Mode2Up.test.js +5 -7
- package/tests/jest/BookReader/ModeSmoothZoom.test.js +149 -0
- package/tests/jest/BookReader/ModeThumb.test.js +71 -0
- package/tests/{BookReader → jest/BookReader}/Navbar/Navbar.test.js +7 -7
- package/tests/{BookReader → jest/BookReader}/PageContainer.test.js +79 -6
- package/tests/{BookReader → jest/BookReader}/ReduceSet.test.js +1 -1
- package/tests/{BookReader → jest/BookReader}/Toolbar/Toolbar.test.js +2 -2
- package/tests/jest/BookReader/utils/HTMLDimensionsCacher.test.js +59 -0
- package/tests/jest/BookReader/utils/ScrollClassAdder.test.js +49 -0
- package/tests/{BookReader → jest/BookReader}/utils/classes.test.js +1 -1
- package/tests/jest/BookReader/utils.test.js +136 -0
- package/tests/jest/BookReader.keyboard.test.js +190 -0
- package/tests/{BookReader.options.test.js → jest/BookReader.options.test.js} +9 -1
- package/tests/{BookReader.test.js → jest/BookReader.test.js} +20 -4
- package/tests/{plugins → jest/plugins}/plugin.archive_analytics.test.js +2 -2
- package/tests/{plugins → jest/plugins}/plugin.autoplay.test.js +2 -2
- package/tests/{plugins → jest/plugins}/plugin.chapters.test.js +8 -8
- package/tests/{plugins → jest/plugins}/plugin.iframe.test.js +2 -2
- package/tests/{plugins → jest/plugins}/plugin.mobile_nav.test.js +5 -5
- package/tests/{plugins → jest/plugins}/plugin.resume.test.js +3 -3
- package/tests/{plugins → jest/plugins}/plugin.text_selection.test.js +39 -47
- package/tests/{plugins → jest/plugins}/plugin.vendor-fullscreen.test.js +2 -2
- package/tests/{plugins → jest/plugins}/search/plugin.search.test.js +24 -25
- package/tests/{plugins → jest/plugins}/search/plugin.search.view.test.js +6 -6
- package/tests/{plugins → jest/plugins}/tts/AbstractTTSEngine.test.js +6 -6
- package/tests/{plugins → jest/plugins}/tts/FestivalTTSEngine.test.js +4 -4
- package/tests/{plugins → jest/plugins}/tts/PageChunk.test.js +1 -1
- package/tests/{plugins → jest/plugins}/tts/PageChunkIterator.test.js +3 -3
- package/tests/{plugins → jest/plugins}/tts/WebTTSEngine.test.js +1 -1
- package/tests/{plugins → jest/plugins}/tts/utils.test.js +3 -3
- package/tests/jest/plugins/url/UrlPlugin.test.js +190 -0
- package/tests/{plugins → jest/plugins/url}/plugin.url.test.js +33 -14
- package/tests/{util → jest/util}/browserSniffing.test.js +1 -1
- package/tests/{util → jest/util}/docCookies.test.js +1 -1
- package/tests/{util → jest/util}/strings.test.js +1 -1
- package/tests/{utils.js → jest/utils.js} +38 -0
- package/tests/karma/BookNavigator/book-navigator.test.js +501 -0
- package/tests/karma/BookNavigator/bookmarks/bookmark-button.test.js +44 -0
- package/tests/karma/BookNavigator/bookmarks/bookmark-edit.test.js +1 -3
- package/tests/karma/BookNavigator/bookmarks/bookmarks-list.test.js +3 -4
- package/tests/karma/BookNavigator/bookmarks/ia-bookmarks.test.js +57 -0
- package/tests/karma/BookNavigator/downloads/downloads-provider.test.js +67 -0
- package/tests/karma/BookNavigator/downloads/downloads.test.js +54 -0
- package/tests/karma/BookNavigator/search/search-provider.test.js +123 -0
- package/tests/karma/BookNavigator/{search-results.test.js → search/search-results.test.js} +1 -4
- package/tests/karma/BookNavigator/sharing/sharing-provider.test.js +49 -0
- package/tests/karma/BookNavigator/visual-adjustments.test.js +0 -2
- package/tests/karma/BookNavigator/volumes/volumes-provider.test.js +184 -0
- package/tests/karma/BookNavigator/volumes/volumes.test.js +98 -0
- package/webpack.config.js +10 -4
- package/.babelrc +0 -12
- package/.dependabot/config.yml +0 -6
- package/.testcaferc.json +0 -5
- package/BookReader/bookreader-component-bundle.js +0 -1450
- package/BookReader/bookreader-component-bundle.js.map +0 -1
- package/BookReader/plugins/plugin.menu_toggle.js +0 -2
- package/BookReader/plugins/plugin.menu_toggle.js.map +0 -1
- package/BookReaderDemo/bookreader-template-bundle.js +0 -7178
- package/BookReaderDemo/demo-plugin-menu-toggle.html +0 -34
- package/src/BookNavigator/BookModel.js +0 -14
- package/src/BookNavigator/BookNavigator.js +0 -435
- package/src/BookNavigator/assets/book-loader.js +0 -27
- package/src/BookNavigator/br-fullscreen-mgr.js +0 -83
- package/src/BookReaderComponent/BookReaderComponent.js +0 -112
- package/src/ItemNavigator/ItemNavigator.js +0 -372
- package/src/ItemNavigator/providers/sharing.js +0 -29
- package/src/dragscrollable-br.js +0 -261
- package/src/plugins/menu_toggle/plugin.menu_toggle.js +0 -324
- package/tests/BookReader/BookReaderPublicFunctions.test.js +0 -171
- package/tests/BookReader/Mode1Up.test.js +0 -164
- package/tests/BookReader/utils.test.js +0 -109
- package/tests/plugins/menu_toggle/plugin.menu_toggle.test.js +0 -68
@@ -54,7 +54,7 @@ export class Toolbar {
|
|
54
54
|
.attr({href: br.bookUrl, title: br.bookUrlTitle})
|
55
55
|
.addClass('BRreturn')
|
56
56
|
.html(br.bookUrlText || br.bookTitle)
|
57
|
-
)
|
57
|
+
);
|
58
58
|
} else if (br.bookTitle) {
|
59
59
|
$titleSectionEl.append(br.bookUrlText || br.bookTitle);
|
60
60
|
}
|
@@ -212,7 +212,7 @@ export class Toolbar {
|
|
212
212
|
$form.appendTo($shareDiv);
|
213
213
|
|
214
214
|
$form.find('.fieldset-embed input').on('change', event => {
|
215
|
-
const form = $(event.target).parents('form
|
215
|
+
const form = $(event.target).parents('form').first();
|
216
216
|
const params = {};
|
217
217
|
params.mode = $(form.find('.fieldset-embed input[name=pages]:checked')).val();
|
218
218
|
if (form.find('.fieldset-embed input[name=thispage]').prop('checked')) {
|
@@ -232,12 +232,12 @@ export class Toolbar {
|
|
232
232
|
// Bind share buttons
|
233
233
|
|
234
234
|
// Use url without hashes
|
235
|
-
$form.find('.facebook-share-button').click(
|
235
|
+
$form.find('.facebook-share-button').on("click", () => {
|
236
236
|
const params = $.param({ u: this._getSocialShareUrl() });
|
237
237
|
const url = 'https://www.facebook.com/sharer.php?' + params;
|
238
238
|
createPopup(url, 600, 400, 'Share');
|
239
239
|
});
|
240
|
-
$form.find('.twitter-share-button').click(
|
240
|
+
$form.find('.twitter-share-button').on("click", () => {
|
241
241
|
const params = $.param({
|
242
242
|
url: this._getSocialShareUrl(),
|
243
243
|
text: br.bookTitle
|
@@ -245,7 +245,7 @@ export class Toolbar {
|
|
245
245
|
const url = 'https://twitter.com/intent/tweet?' + params;
|
246
246
|
createPopup(url, 600, 400, 'Share');
|
247
247
|
});
|
248
|
-
$form.find('.email-share-button').click(
|
248
|
+
$form.find('.email-share-button').on("click", () => {
|
249
249
|
const body = `${br.bookTitle}\n\n${this._getSocialShareUrl()}`;
|
250
250
|
window.location.href = `mailto:?subject=${encodeURI(br.bookTitle)}&body=${encodeURI(body)}`;
|
251
251
|
});
|
@@ -335,7 +335,7 @@ export function blankInfoDiv() {
|
|
335
335
|
return $(`
|
336
336
|
<div class="BRfloat BRinfo">
|
337
337
|
<div class="BRfloatHead">About this book
|
338
|
-
<button class="floatShut" href="javascript:;" onclick="$.fn.colorbox.close();"><span class="shift">Close</span></button>
|
338
|
+
<button class="floatShut" href="javascript:;" onclick="$.fn.colorbox.close();"><span class="br-colorbox-shift">Close</span></button>
|
339
339
|
</div>
|
340
340
|
<div class="BRfloatBody">
|
341
341
|
<div class="BRfloatCover"></div>
|
@@ -356,7 +356,7 @@ export function blankShareDiv() {
|
|
356
356
|
<div class="BRfloat BRshare">
|
357
357
|
<div class="BRfloatHead">
|
358
358
|
Share
|
359
|
-
<button class="floatShut" href="javascript:;" onclick="$.fn.colorbox.close();"><span class="shift">Close</span></button>
|
359
|
+
<button class="floatShut" href="javascript:;" onclick="$.fn.colorbox.close();"><span class="br-colorbox-shift">Close</span></button>
|
360
360
|
</div>
|
361
361
|
</div>`);
|
362
362
|
}
|
@@ -25,6 +25,10 @@ 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
34
|
flipSpeed: 'fast',
|
@@ -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
|
@@ -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
|
+
}
|
package/src/BookReader/utils.js
CHANGED
@@ -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 {
|
108
|
+
* @param {T} func
|
90
109
|
* @param {number} wait
|
91
110
|
* @param {boolean} immediate
|
92
|
-
* @return {
|
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 {
|
133
|
+
* @param {T} fn
|
114
134
|
* @param {number} threshold
|
115
135
|
* @param {boolean} delay
|
116
|
-
* @return {
|
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,50 @@ export function calcScreenDPI() {
|
|
179
191
|
const dpi = el.offsetWidth;
|
180
192
|
document.body.removeChild(el);
|
181
193
|
|
182
|
-
|
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
|
+
}
|