@internetarchive/bookreader 5.0.0-93 → 5.0.0-95

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.
Files changed (224) hide show
  1. package/.github/workflows/npm-publish.yml +2 -12
  2. package/BookReaderDemo/IADemoBr.js +1 -24
  3. package/BookReaderDemo/demo-internetarchive.html +1 -0
  4. package/CHANGELOG.md +19 -1
  5. package/README.md +0 -2
  6. package/package.json +8 -4
  7. package/scripts/postversion.js +3 -2
  8. package/scripts/preversion.js +3 -1
  9. package/scripts/version.js +4 -6
  10. package/src/BookNavigator/book-navigator.js +38 -12
  11. package/src/BookNavigator/downloads/downloads-provider.js +2 -2
  12. package/src/BookNavigator/search/search-provider.js +5 -5
  13. package/src/BookNavigator/search/search-results.js +1 -1
  14. package/src/BookNavigator/sharing.js +2 -2
  15. package/src/BookNavigator/viewable-files.js +2 -2
  16. package/src/BookNavigator/visual-adjustments/visual-adjustments-provider.js +3 -3
  17. package/src/BookNavigator/visual-adjustments/visual-adjustments.js +2 -2
  18. package/src/BookReader.js +57 -31
  19. package/src/assets/images/hypothesis.ico +0 -0
  20. package/src/css/_TextSelection.scss +3 -1
  21. package/src/plugins/plugin.autoplay.js +3 -3
  22. package/src/plugins/plugin.chapters.js +2 -2
  23. package/src/plugins/plugin.experiments.js +294 -0
  24. package/src/plugins/plugin.iiif.js +1 -1
  25. package/src/plugins/plugin.text_selection.js +112 -1
  26. package/src/plugins/search/view.js +5 -5
  27. package/src/plugins/tts/plugin.tts.js +3 -3
  28. package/src/plugins/url/plugin.url.js +2 -2
  29. package/tests/e2e/autoplay.test.js +1 -1
  30. package/tests/e2e/base.test.js +4 -4
  31. package/tests/e2e/helpers/base.js +2 -2
  32. package/tests/e2e/models/BookReader.js +1 -1
  33. package/tests/e2e/rightToLeft.test.js +4 -4
  34. package/tests/e2e/viewmode.test.js +2 -2
  35. package/tests/jest/BookNavigator/book-navigator.test.js +0 -13
  36. package/tests/jest/BookNavigator/downloads/downloads-provider.test.js +1 -1
  37. package/tests/jest/BookNavigator/downloads/downloads.test.js +1 -1
  38. package/tests/jest/BookNavigator/search/search-provider.test.js +5 -5
  39. package/tests/jest/BookReader.test.js +10 -10
  40. package/tests/jest/plugins/plugin.autoplay.test.js +6 -6
  41. package/tests/jest/plugins/plugin.chapters.test.js +2 -2
  42. package/tests/jest/plugins/plugin.resume.test.js +13 -13
  43. package/tests/jest/plugins/plugin.text_selection.test.js +155 -24
  44. package/tests/jest/plugins/search/plugin.search.test.js +7 -7
  45. package/tests/jest/plugins/search/plugin.search.view.test.js +8 -8
  46. package/tests/jest/plugins/search/utils.js +1 -1
  47. package/tests/jest/plugins/tts/PageChunkIterator.test.js +2 -2
  48. package/tests/jest/plugins/url/UrlPlugin.test.js +1 -1
  49. package/webpack.config.js +8 -3
  50. package/BookReader/BookReader.css +0 -2250
  51. package/BookReader/BookReader.js +0 -3
  52. package/BookReader/BookReader.js.LICENSE.txt +0 -72
  53. package/BookReader/BookReader.js.map +0 -1
  54. package/BookReader/ia-bookreader-bundle.js +0 -1782
  55. package/BookReader/ia-bookreader-bundle.js.LICENSE.txt +0 -7
  56. package/BookReader/ia-bookreader-bundle.js.map +0 -1
  57. package/BookReader/icons/1up.svg +0 -1
  58. package/BookReader/icons/2up.svg +0 -1
  59. package/BookReader/icons/advance.svg +0 -3
  60. package/BookReader/icons/chevron-right.svg +0 -1
  61. package/BookReader/icons/close-circle-dark.svg +0 -1
  62. package/BookReader/icons/close-circle.svg +0 -1
  63. package/BookReader/icons/fullscreen.svg +0 -1
  64. package/BookReader/icons/fullscreen_exit.svg +0 -1
  65. package/BookReader/icons/hamburger.svg +0 -1
  66. package/BookReader/icons/left-arrow.svg +0 -1
  67. package/BookReader/icons/magnify-minus.svg +0 -1
  68. package/BookReader/icons/magnify-plus.svg +0 -1
  69. package/BookReader/icons/magnify.svg +0 -1
  70. package/BookReader/icons/pause.svg +0 -1
  71. package/BookReader/icons/play.svg +0 -1
  72. package/BookReader/icons/playback-speed.svg +0 -1
  73. package/BookReader/icons/read-aloud.svg +0 -1
  74. package/BookReader/icons/review.svg +0 -3
  75. package/BookReader/icons/thumbnails.svg +0 -1
  76. package/BookReader/icons/voice.svg +0 -1
  77. package/BookReader/icons/volume-full.svg +0 -1
  78. package/BookReader/images/BRicons.png +0 -0
  79. package/BookReader/images/BRicons.svg +0 -5
  80. package/BookReader/images/BRicons_ia.png +0 -0
  81. package/BookReader/images/back_pages.png +0 -0
  82. package/BookReader/images/book_bottom_icon.png +0 -0
  83. package/BookReader/images/book_down_icon.png +0 -0
  84. package/BookReader/images/book_left_icon.png +0 -0
  85. package/BookReader/images/book_leftmost_icon.png +0 -0
  86. package/BookReader/images/book_right_icon.png +0 -0
  87. package/BookReader/images/book_rightmost_icon.png +0 -0
  88. package/BookReader/images/book_top_icon.png +0 -0
  89. package/BookReader/images/book_up_icon.png +0 -0
  90. package/BookReader/images/books_graphic.svg +0 -1
  91. package/BookReader/images/booksplit.png +0 -0
  92. package/BookReader/images/control_pause_icon.png +0 -0
  93. package/BookReader/images/control_play_icon.png +0 -0
  94. package/BookReader/images/embed_icon.png +0 -0
  95. package/BookReader/images/icon-home-ia.png +0 -0
  96. package/BookReader/images/icon_OL-logo-xs.png +0 -0
  97. package/BookReader/images/icon_alert-xs.png +0 -0
  98. package/BookReader/images/icon_book.svg +0 -1
  99. package/BookReader/images/icon_bookmark.svg +0 -1
  100. package/BookReader/images/icon_close-pop.png +0 -0
  101. package/BookReader/images/icon_download.png +0 -0
  102. package/BookReader/images/icon_gear.svg +0 -1
  103. package/BookReader/images/icon_hamburger.svg +0 -1
  104. package/BookReader/images/icon_home.png +0 -0
  105. package/BookReader/images/icon_home.svg +0 -1
  106. package/BookReader/images/icon_home_ia.png +0 -0
  107. package/BookReader/images/icon_indicator.png +0 -0
  108. package/BookReader/images/icon_info.svg +0 -1
  109. package/BookReader/images/icon_one_page.svg +0 -1
  110. package/BookReader/images/icon_pause.svg +0 -1
  111. package/BookReader/images/icon_play.svg +0 -1
  112. package/BookReader/images/icon_playback-rate.svg +0 -1
  113. package/BookReader/images/icon_return.png +0 -0
  114. package/BookReader/images/icon_search_button.svg +0 -1
  115. package/BookReader/images/icon_share.svg +0 -1
  116. package/BookReader/images/icon_skip-ahead.svg +0 -1
  117. package/BookReader/images/icon_skip-back.svg +0 -2
  118. package/BookReader/images/icon_speaker.svg +0 -1
  119. package/BookReader/images/icon_speaker_open.svg +0 -1
  120. package/BookReader/images/icon_thumbnails.svg +0 -1
  121. package/BookReader/images/icon_toc.svg +0 -1
  122. package/BookReader/images/icon_two_pages.svg +0 -1
  123. package/BookReader/images/icon_zoomer.png +0 -0
  124. package/BookReader/images/loading.gif +0 -0
  125. package/BookReader/images/logo_icon.png +0 -0
  126. package/BookReader/images/marker_chap-off.png +0 -0
  127. package/BookReader/images/marker_chap-off.svg +0 -1
  128. package/BookReader/images/marker_chap-off_ia.png +0 -0
  129. package/BookReader/images/marker_chap-on.png +0 -0
  130. package/BookReader/images/marker_chap-on.svg +0 -1
  131. package/BookReader/images/marker_srch-on.svg +0 -1
  132. package/BookReader/images/marker_srchchap-off.png +0 -0
  133. package/BookReader/images/marker_srchchap-on.png +0 -0
  134. package/BookReader/images/nav_control-dn.png +0 -0
  135. package/BookReader/images/nav_control-dn_ia.png +0 -0
  136. package/BookReader/images/nav_control-up.png +0 -0
  137. package/BookReader/images/nav_control-up_ia.png +0 -0
  138. package/BookReader/images/nav_control.png +0 -0
  139. package/BookReader/images/one_page_mode_icon.png +0 -0
  140. package/BookReader/images/paper-badge.png +0 -0
  141. package/BookReader/images/print_icon.png +0 -0
  142. package/BookReader/images/progressbar.gif +0 -0
  143. package/BookReader/images/right_edges.png +0 -0
  144. package/BookReader/images/slider.png +0 -0
  145. package/BookReader/images/slider_ia.png +0 -0
  146. package/BookReader/images/thumbnail_mode_icon.png +0 -0
  147. package/BookReader/images/transparent.png +0 -0
  148. package/BookReader/images/two_page_mode_icon.png +0 -0
  149. package/BookReader/images/unviewable_page.png +0 -0
  150. package/BookReader/images/zoom_in_icon.png +0 -0
  151. package/BookReader/images/zoom_out_icon.png +0 -0
  152. package/BookReader/jquery-3.js +0 -2
  153. package/BookReader/jquery-3.js.LICENSE.txt +0 -24
  154. package/BookReader/plugins/plugin.archive_analytics.js +0 -2
  155. package/BookReader/plugins/plugin.archive_analytics.js.map +0 -1
  156. package/BookReader/plugins/plugin.autoplay.js +0 -2
  157. package/BookReader/plugins/plugin.autoplay.js.map +0 -1
  158. package/BookReader/plugins/plugin.chapters.js +0 -26
  159. package/BookReader/plugins/plugin.chapters.js.LICENSE.txt +0 -1
  160. package/BookReader/plugins/plugin.chapters.js.map +0 -1
  161. package/BookReader/plugins/plugin.iframe.js +0 -2
  162. package/BookReader/plugins/plugin.iframe.js.map +0 -1
  163. package/BookReader/plugins/plugin.iiif.js +0 -2
  164. package/BookReader/plugins/plugin.iiif.js.map +0 -1
  165. package/BookReader/plugins/plugin.resume.js +0 -2
  166. package/BookReader/plugins/plugin.resume.js.map +0 -1
  167. package/BookReader/plugins/plugin.search.js +0 -3
  168. package/BookReader/plugins/plugin.search.js.LICENSE.txt +0 -1
  169. package/BookReader/plugins/plugin.search.js.map +0 -1
  170. package/BookReader/plugins/plugin.text_selection.js +0 -3
  171. package/BookReader/plugins/plugin.text_selection.js.LICENSE.txt +0 -1
  172. package/BookReader/plugins/plugin.text_selection.js.map +0 -1
  173. package/BookReader/plugins/plugin.tts.js +0 -3
  174. package/BookReader/plugins/plugin.tts.js.LICENSE.txt +0 -29
  175. package/BookReader/plugins/plugin.tts.js.map +0 -1
  176. package/BookReader/plugins/plugin.url.js +0 -2
  177. package/BookReader/plugins/plugin.url.js.map +0 -1
  178. package/BookReader/plugins/plugin.vendor-fullscreen.js +0 -2
  179. package/BookReader/plugins/plugin.vendor-fullscreen.js.map +0 -1
  180. package/BookReader/webcomponents-bundle.js +0 -3
  181. package/BookReader/webcomponents-bundle.js.LICENSE.txt +0 -9
  182. package/BookReader/webcomponents-bundle.js.map +0 -1
  183. package/src/BookReader/BookModel.js +0 -554
  184. package/src/BookReader/DragScrollable.js +0 -233
  185. package/src/BookReader/ImageCache.js +0 -149
  186. package/src/BookReader/Mode1Up.js +0 -108
  187. package/src/BookReader/Mode1UpLit.js +0 -388
  188. package/src/BookReader/Mode2Up.js +0 -105
  189. package/src/BookReader/Mode2UpLit.js +0 -777
  190. package/src/BookReader/ModeCoordinateSpace.js +0 -29
  191. package/src/BookReader/ModeSmoothZoom.js +0 -312
  192. package/src/BookReader/ModeThumb.js +0 -342
  193. package/src/BookReader/Navbar/Navbar.js +0 -355
  194. package/src/BookReader/PageContainer.js +0 -169
  195. package/src/BookReader/ReduceSet.js +0 -26
  196. package/src/BookReader/Toolbar/Toolbar.js +0 -362
  197. package/src/BookReader/events.js +0 -19
  198. package/src/BookReader/options.js +0 -382
  199. package/src/BookReader/utils/HTMLDimensionsCacher.js +0 -44
  200. package/src/BookReader/utils/ScrollClassAdder.js +0 -31
  201. package/src/BookReader/utils/SelectionObserver.js +0 -45
  202. package/src/BookReader/utils/classes.js +0 -36
  203. package/src/BookReader/utils.js +0 -300
  204. package/tests/jest/BookReader/BookModel.test.js +0 -372
  205. package/tests/jest/BookReader/BookReaderPublicFunctions.test.js +0 -263
  206. package/tests/jest/BookReader/ImageCache.test.js +0 -150
  207. package/tests/jest/BookReader/Mode1UpLit.test.js +0 -73
  208. package/tests/jest/BookReader/Mode2Up.test.js +0 -98
  209. package/tests/jest/BookReader/Mode2UpLit.test.js +0 -190
  210. package/tests/jest/BookReader/ModeCoordinateSpace.test.js +0 -16
  211. package/tests/jest/BookReader/ModeSmoothZoom.test.js +0 -218
  212. package/tests/jest/BookReader/ModeThumb.test.js +0 -71
  213. package/tests/jest/BookReader/Navbar/Navbar.test.js +0 -182
  214. package/tests/jest/BookReader/PageContainer.test.js +0 -238
  215. package/tests/jest/BookReader/ReduceSet.test.js +0 -38
  216. package/tests/jest/BookReader/Toolbar/Toolbar.test.js +0 -26
  217. package/tests/jest/BookReader/utils/HTMLDimensionsCacher.test.js +0 -59
  218. package/tests/jest/BookReader/utils/ScrollClassAdder.test.js +0 -49
  219. package/tests/jest/BookReader/utils/SelectionObserver.test.js +0 -57
  220. package/tests/jest/BookReader/utils/classes.test.js +0 -88
  221. package/tests/jest/BookReader/utils.test.js +0 -250
  222. /package/{.eslintrc.js → .eslintrc.cjs} +0 -0
  223. /package/{.testcaferc.js → .testcaferc.cjs} +0 -0
  224. /package/{babel.config.js → babel.config.cjs} +0 -0
@@ -8,6 +8,7 @@
8
8
  // Make it so right-clicking on "blank" part of text layer sends events to the image (for saving)
9
9
  pointer-events: none;
10
10
  cursor: text;
11
+ mix-blend-mode: multiply;
11
12
  }
12
13
 
13
14
  .BRparagraphElement {
@@ -58,7 +59,8 @@
58
59
 
59
60
  // Hide text layer for performance during zooming & scrolling
60
61
  .BRsmooth-zooming, .BRscrolling-active {
61
- .BRpagecontainer:not(.BRpagecontainer--hasSelection) .BRtextLayer {
62
+ // If the text layer has an annotation, don't hide it
63
+ .BRpagecontainer:not(.BRpagecontainer--hasSelection):not(:has(hypothesis-highlight)) .BRtextLayer {
62
64
  display: none;
63
65
  }
64
66
  }
@@ -1,7 +1,7 @@
1
1
  // @ts-check
2
- import { EVENTS } from "../BookReader/events";
3
- import { parseAnimationSpeed } from "../BookReader/utils";
4
- import { BookReaderPlugin } from "../BookReaderPlugin";
2
+ import { EVENTS } from "../BookReader/events.js";
3
+ import { parseAnimationSpeed } from "../BookReader/utils.js";
4
+ import { BookReaderPlugin } from "../BookReaderPlugin.js";
5
5
 
6
6
  /**
7
7
  * Plugin which adds an autoplay feature. Useful for kiosk situations.
@@ -3,8 +3,8 @@ import { css, html, LitElement, nothing } from "lit";
3
3
  import { customElement, property } from 'lit/decorators.js';
4
4
  import { ifDefined } from 'lit/directives/if-defined.js';
5
5
  import { styleMap } from 'lit/directives/style-map.js';
6
- import '@internetarchive/icon-toc/icon-toc';
7
- import { BookReaderPlugin } from "../BookReaderPlugin";
6
+ import '@internetarchive/icon-toc/icon-toc.js';
7
+ import { BookReaderPlugin } from "../BookReaderPlugin.js";
8
8
  import { applyVariables } from "../util/strings.js";
9
9
  /** @typedef {import('@/src/BookReader/BookModel.js').PageIndex} PageIndex */
10
10
  /** @typedef {import('@/src/BookReader/BookModel.js').PageString} PageString */
@@ -0,0 +1,294 @@
1
+ // @ts-check
2
+ import { css, html, LitElement } from 'lit';
3
+ import { BookReaderPlugin } from '../BookReaderPlugin.js';
4
+ import { customElement, property, state } from 'lit/decorators.js';
5
+ import { sleep } from '../BookReader/utils.js';
6
+
7
+ // @ts-ignore
8
+ const BookReader = /** @type {typeof import('@/src/BookReader.js').default} */(window.BookReader);
9
+
10
+ class ExperimentModel {
11
+ /** @type {string} test */
12
+ name;
13
+ /** @type {string} */
14
+ title;
15
+ /** @type {string} */
16
+ description;
17
+ /** @type {string} */
18
+ icon;
19
+ /** @type {boolean} */
20
+ enabled;
21
+ /** @type {string} */
22
+ learnMore;
23
+
24
+ assetRoot = '/BookReader/';
25
+
26
+ enabledLoading = false;
27
+
28
+ /**
29
+ * @param {string} path
30
+ */
31
+ buildAssetPath(path) {
32
+ return `${this.assetRoot}${path}`;
33
+ }
34
+
35
+ /**
36
+ * @param {object} param0
37
+ * @param {boolean} param0.manual Whether the experiment was enabled manually
38
+ */
39
+ async enable({ manual }) { }
40
+ async disable() { }
41
+ }
42
+
43
+ export class ExperimentsPlugin extends BookReaderPlugin {
44
+ options = {
45
+ enabled: true,
46
+
47
+ /** Where the state of this plugin is saved in localStorage */
48
+ localStorageKey: 'BrExperiments',
49
+ }
50
+
51
+ /** @type {ExperimentModel[]} */
52
+ experiments = [
53
+ new class extends ExperimentModel {
54
+ name = 'hypothesis';
55
+ title = 'Hypothes.is';
56
+ description = 'Create public, collaborative, or fully private annotations on books and the web.';
57
+ learnMore = 'https://web.hypothes.is/about/';
58
+ icon = 'images/hypothesis.ico';
59
+ enabled = false;
60
+
61
+ async enable({ manual = false }) {
62
+ // Hypothesis configs ; see https://h.readthedocs.io/projects/client/en/latest/publishers/config.html
63
+ const configScript = document.createElement('script');
64
+ configScript.type = 'application/json';
65
+ configScript.className = 'js-hypothesis-config';
66
+ configScript.textContent = JSON.stringify({
67
+ // Open the sidebar if this is the first time enabling
68
+ openSidebar: manual,
69
+ assetRoot: this.buildAssetPath('hypothesis/'),
70
+ });
71
+
72
+ document.head.appendChild(configScript);
73
+ return importAsScript(this.buildAssetPath('hypothesis/build/boot.js'));
74
+ // For testing
75
+ // return importAsScript('http://localhost:3001/hypothesis');
76
+ }
77
+
78
+ async disable() {
79
+ // need to reload to remove the Hypothesis script
80
+ // Sleep so that the event loop can finish processing before the reload
81
+ sleep(0).then(() => {
82
+ window.location.reload();
83
+ });
84
+ }
85
+ }(),
86
+ ]
87
+
88
+ /** @type {BrExperimentsPanel} */
89
+ _panel;
90
+
91
+ async init() {
92
+ if (!this.options.enabled) {
93
+ return;
94
+ }
95
+
96
+ for (const experiment of this.experiments) {
97
+ // TODO: imagesBaseURL should be replaced with assetRoot everywhere
98
+ experiment.assetRoot = this.br.options.imagesBaseURL.replace(/images\/$/, '');
99
+ experiment.icon = experiment.buildAssetPath(experiment.icon);
100
+ }
101
+
102
+ this._loadExperimentStates();
103
+ await Promise.resolve();
104
+ this._render();
105
+ }
106
+
107
+ _loadExperimentStates() {
108
+ const savedStates = JSON.parse(localStorage.getItem(this.options.localStorageKey) || '{}');
109
+ this.experiments.forEach(experiment => {
110
+ if (savedStates[experiment.name] !== undefined) {
111
+ experiment.enabled = savedStates[experiment.name];
112
+ if (experiment.enabled) {
113
+ experiment.enable({ manual: false });
114
+ }
115
+ }
116
+ });
117
+ }
118
+
119
+ _saveExperimentStates() {
120
+ const states = Object.fromEntries(
121
+ this.experiments.map(experiment => [experiment.name, experiment.enabled]),
122
+ );
123
+ localStorage.setItem(this.options.localStorageKey, JSON.stringify(states));
124
+ }
125
+
126
+ /**
127
+ * @param {ExperimentModel} experiment
128
+ * @param {boolean} enabled
129
+ */
130
+ async _toggleExperiment(experiment, enabled) {
131
+ experiment.enabledLoading = true;
132
+ this.br.plugins.archiveAnalytics?.sendEvent(`BRExperiment-${experiment.name}`, enabled ? 'Enable' : 'Disable');
133
+ this._panel.requestUpdate();
134
+
135
+ if (enabled) {
136
+ await experiment.enable({ manual: true });
137
+ } else {
138
+ await experiment.disable();
139
+ }
140
+ experiment.enabledLoading = false;
141
+ experiment.enabled = enabled;
142
+ this._panel.requestUpdate();
143
+ }
144
+
145
+ _render() {
146
+ this.br.shell.menuProviders['experiments'] = {
147
+ id: 'experiments',
148
+ // https://icon-sets.iconify.design/hugeicons/?icon-filter=eco-lab-02&query=lab
149
+ icon: html`
150
+ <svg xmlns="http://www.w3.org/2000/svg" width="34" viewBox="0 0 24 24"><path fill="currentColor" d="M10 5.75h1.25v2.5H9.5c-.41 0-.75.34-.75.75s.34.75.75.75h.25v1.96A5.72 5.72 0 0 0 6.25 17c0 3.17 2.58 5.75 5.75 5.75s5.75-2.58 5.75-5.75c0-2.33-1.39-4.4-3.5-5.29V9.75h.25c.41 0 .75-.34.75-.75s-.34-.75-.75-.75h-1.75v-1.5H14c1.52 0 2.75-1.23 2.75-2.75V3c0-.41-.34-.75-.75-.75h-2c-.579 0-1.115.178-1.558.483A2.75 2.75 0 0 0 10 1.25H8c-.41 0-.75.34-.75.75v1c0 1.52 1.23 2.75 2.75 2.75m2.75-.5V5c0-.69.56-1.25 1.25-1.25h1.25V4c0 .69-.56 1.25-1.25 1.25zm-1.5 6.98V9.75h1.5v2.48c0 .33.22.62.53.72c1.77.56 2.97 2.19 2.97 4.06A4.26 4.26 0 0 1 12 21.26a4.26 4.26 0 0 1-4.25-4.25c0-1.87 1.19-3.5 2.97-4.06c.32-.1.53-.39.53-.72m-2.5-9.48H10c.69 0 1.25.56 1.25 1.25v.25H10c-.69 0-1.25-.56-1.25-1.25z" color="currentColor"/></svg>
151
+ `,
152
+ label: 'Experiments',
153
+ component: html`<br-experiments-panel
154
+ .experiments="${this.experiments}"
155
+ @connected="${e => this._panel = e.target}"
156
+ @toggle="${async e => {
157
+ await this._toggleExperiment(e.detail.experiment, e.detail.enabled);
158
+ this._saveExperimentStates();
159
+ }}"
160
+ />`,
161
+ };
162
+ this.br.shell.updateMenuContents();
163
+ }
164
+ }
165
+ BookReader?.registerPlugin('experiments', ExperimentsPlugin);
166
+
167
+ @customElement('br-experiments-panel')
168
+ export class BrExperimentsPanel extends LitElement {
169
+
170
+ /** @type {ExperimentModel[]} */
171
+ @property({ type: Array }) experiments = [];
172
+
173
+ render() {
174
+ return html`
175
+ <div style="padding: 10px">
176
+ ${this.experiments.map(
177
+ experiment => html`
178
+ <br-experiment-toggle
179
+ .icon="${experiment.icon}"
180
+ .title="${experiment.title}"
181
+ .description="${experiment.description}"
182
+ .enabled="${experiment.enabled}"
183
+ .loading="${experiment.enabledLoading}"
184
+ .learnMore="${experiment.learnMore}"
185
+ @toggle="${e => this._dispatchToggle(experiment, e.detail.enabled)}"
186
+ ></br-experiment-toggle>
187
+ `,
188
+ )}
189
+ </div>
190
+ `;
191
+ }
192
+
193
+ /**
194
+ * @param {ExperimentModel} experiment
195
+ * @param {boolean} enabled
196
+ */
197
+ async _dispatchToggle(experiment, enabled) {
198
+ this.dispatchEvent(new CustomEvent('toggle', {
199
+ detail: { experiment, enabled },
200
+ }));
201
+ }
202
+
203
+ connectedCallback() {
204
+ super.connectedCallback();
205
+ this.dispatchEvent(new CustomEvent('connected'));
206
+ }
207
+ }
208
+
209
+ @customElement('br-experiment-toggle')
210
+ export class BrExperimentToggle extends LitElement {
211
+ @property({ type: String }) icon = '';
212
+ @property({ type: String }) title = '';
213
+ @property({ type: String }) description = '';
214
+ @property({ type: Boolean }) enabled = false;
215
+ @property({ type: Boolean }) loading = false;
216
+ @property({ type: String }) learnMore = '';
217
+
218
+ /**
219
+ * We want to disable the button immediately if loading, but only display
220
+ * the loading indicator after 200ms.
221
+ */
222
+ @state() _longLoading = false;
223
+
224
+ /** @override */
225
+ update(changedProperties) {
226
+ super.update(changedProperties);
227
+ if (changedProperties.has('loading')) {
228
+ if (this.loading) {
229
+ sleep(500).then(() => {
230
+ if (this.loading) {
231
+ this._longLoading = true;
232
+ this.requestUpdate();
233
+ }
234
+ });
235
+ } else {
236
+ this._longLoading = false;
237
+ }
238
+ }
239
+ }
240
+
241
+ render() {
242
+ return html`
243
+ <div class="experiment-card">
244
+ <div style="display: flex; align-items: center; gap: 10px;">
245
+ <img src="${this.icon}" style="width: 20px; height: 20px;" alt="" />
246
+ <div style="flex-grow: 1; font-weight: bold;">${this.title}</div>
247
+ </div>
248
+ <p style="opacity: 0.9">
249
+ ${this.description}
250
+ <a href="${this.learnMore}" target="_blank">Learn more</a>.
251
+ </p>
252
+ <div style="display: flex">
253
+ <div style="flex-grow: 1;"></div>
254
+ <button @click="${this._dispatchToggle}" .disabled="${this.loading}">
255
+ ${this._longLoading ? 'Loading…' : this.enabled ? 'Disable' : 'Enable'}
256
+ </button>
257
+ </div>
258
+ </div>
259
+ `;
260
+ }
261
+
262
+ static get styles() {
263
+ return css`
264
+ .experiment-card {
265
+ border-radius: 8px;
266
+ background-color: #fff2;
267
+ padding: 10px;
268
+ display: flex;
269
+ flex-direction: column;
270
+ }
271
+
272
+ .experiment-card p {
273
+ margin: 6px 0;
274
+ }
275
+ `;
276
+ }
277
+
278
+ _dispatchToggle() {
279
+ this.dispatchEvent(new CustomEvent('toggle', { detail: { enabled: !this.enabled } }));
280
+ }
281
+ }
282
+
283
+ /**
284
+ * @param {string} url
285
+ */
286
+ async function importAsScript(url) {
287
+ return new Promise((resolve, reject) => {
288
+ const script = document.createElement('script');
289
+ script.src = url;
290
+ script.onload = () => resolve();
291
+ script.onerror = () => reject(new Error(`Failed to load script: ${url}`));
292
+ document.head.appendChild(script);
293
+ });
294
+ }
@@ -1,5 +1,5 @@
1
1
  // @ts-check
2
- import { BookReaderPlugin } from '../BookReaderPlugin';
2
+ import { BookReaderPlugin } from '../BookReaderPlugin.js';
3
3
 
4
4
  const BookReader = /** @type {typeof import('../BookReader').default} */(window.BookReader);
5
5
 
@@ -38,6 +38,8 @@ export class TextSelectionPlugin extends BookReaderPlugin {
38
38
  singlePageDjvuXmlUrl: null,
39
39
  /** Whether to fetch the XML as a jsonp */
40
40
  jsonp: false,
41
+ /** Mox words tha can be selected when the text layer is protected */
42
+ maxProtectedWords: 200,
41
43
  }
42
44
 
43
45
  /**@type {PromiseLike<JQuery<HTMLElement>|undefined>} */
@@ -75,7 +77,7 @@ export class TextSelectionPlugin extends BookReaderPlugin {
75
77
  new SelectionObserver('.BRtextLayer', (selectEvent) => {
76
78
  // Track how often selection is used
77
79
  if (selectEvent == 'started') {
78
- this.br._plugins.archiveAnalytics?.sendEvent('BookReader', 'SelectStart');
80
+ this.br.plugins.archiveAnalytics?.sendEvent('BookReader', 'SelectStart');
79
81
 
80
82
  // Set a class on the page to avoid hiding it when zooming/etc
81
83
  this.br.refs.$br.find('.BRpagecontainer--hasSelection').removeClass('BRpagecontainer--hasSelection');
@@ -84,6 +86,7 @@ export class TextSelectionPlugin extends BookReaderPlugin {
84
86
  }).attach();
85
87
 
86
88
  if (this.br.protected) {
89
+ document.addEventListener('selectionchange', this._limitSelection);
87
90
  // Prevent right clicking when selected text
88
91
  $(document.body).on('contextmenu dragstart copy', (e) => {
89
92
  const selection = document.getSelection();
@@ -100,6 +103,41 @@ export class TextSelectionPlugin extends BookReaderPlugin {
100
103
  }
101
104
  }
102
105
 
106
+ _limitSelection = () => {
107
+ const selection = window.getSelection();
108
+ if (!selection.rangeCount) return;
109
+
110
+ const range = selection.getRangeAt(0);
111
+
112
+ // Check if range.startContainer is inside the sub-tree of .BRContainer
113
+ const startInBr = !!range.startContainer.parentElement.closest('.BRcontainer');
114
+ const endInBr = !!range.endContainer.parentElement.closest('.BRcontainer');
115
+ if (!startInBr && !endInBr) return;
116
+ if (!startInBr || !endInBr) {
117
+ // weird case, just clear the selection
118
+ selection.removeAllRanges();
119
+ return;
120
+ }
121
+
122
+ // Find the last allowed word in the selection
123
+ const lastAllowedWord = genAt(
124
+ genFilter(
125
+ walkBetweenNodes(range.startContainer, range.endContainer),
126
+ (node) => node.classList?.contains('BRwordElement'),
127
+ ),
128
+ this.options.maxProtectedWords - 1,
129
+ );
130
+
131
+ if (!lastAllowedWord || range.endContainer.parentNode == lastAllowedWord) return;
132
+
133
+ const newRange = document.createRange();
134
+ newRange.setStart(range.startContainer, range.startOffset);
135
+ newRange.setEnd(lastAllowedWord.firstChild, lastAllowedWord.textContent.length);
136
+
137
+ selection.removeAllRanges();
138
+ selection.addRange(newRange);
139
+ };
140
+
103
141
  /**
104
142
  * @override
105
143
  * @param {PageContainer} pageContainer
@@ -511,6 +549,33 @@ function augmentLine(line) {
511
549
  };
512
550
  }
513
551
 
552
+ /**
553
+ * @template T
554
+ * Get the i-th element of an iterable
555
+ * @param {Iterable<T>} iterable
556
+ * @param {number} index
557
+ */
558
+ export function genAt(iterable, index) {
559
+ let i = 0;
560
+ for (const x of iterable) {
561
+ if (i == index) return x;
562
+ i++;
563
+ }
564
+ return undefined;
565
+ }
566
+
567
+ /**
568
+ * @template T
569
+ * Generator version of filter
570
+ * @param {Iterable<T>} iterable
571
+ * @param {function(T): boolean} fn
572
+ */
573
+ export function* genFilter(iterable, fn) {
574
+ for (const x of iterable) {
575
+ if (fn(x)) yield x;
576
+ }
577
+ }
578
+
514
579
  /**
515
580
  * @template TFrom, TTo
516
581
  * Generator version of map
@@ -647,3 +712,49 @@ class Rect {
647
712
  get top() { return this.y; }
648
713
  get left() { return this.x; }
649
714
  }
715
+
716
+ /**
717
+ * Depth traverse the DOM tree starting at `start`, and ending at `end`.
718
+ * @param {Node} start
719
+ * @param {Node} end
720
+ * @returns {Generator<Node>}
721
+ */
722
+ export function* walkBetweenNodes(start, end) {
723
+ let done = false;
724
+
725
+ /**
726
+ * @param {Node} node
727
+ */
728
+ function* walk(node, {children = true, parents = true, siblings = true} = {}) {
729
+ if (node === end) {
730
+ done = true;
731
+ yield node;
732
+ return;
733
+ }
734
+
735
+ // yield self
736
+ yield node;
737
+
738
+ // First iterate children (depth-first traversal)
739
+ if (children && node.firstChild) {
740
+ yield* walk(node.firstChild, {children: true, parents: false, siblings: true});
741
+ if (done) return;
742
+ }
743
+
744
+ // Then iterate siblings
745
+ if (siblings) {
746
+ for (let sib = node.nextSibling; sib; sib = sib.nextSibling) {
747
+ yield* walk(sib, {children: true, parents: false, siblings: false});
748
+ if (done) return;
749
+ }
750
+ }
751
+
752
+ // Finally, move up the tree
753
+ if (parents && node.parentNode) {
754
+ yield* walk(node.parentNode, {children: false, parents: true, siblings: true});
755
+ if (done) return;
756
+ }
757
+ }
758
+
759
+ yield* walk(start);
760
+ }
@@ -44,7 +44,7 @@ class SearchView {
44
44
  }
45
45
 
46
46
  clearSearchFieldAndResults(dispatchEventWhenComplete = true) {
47
- this.br._plugins.search.removeSearchResults();
47
+ this.br.plugins.search.removeSearchResults();
48
48
  this.removeResultPins();
49
49
  this.emptyMatches();
50
50
  this.setQuery('');
@@ -218,7 +218,7 @@ class SearchView {
218
218
  <form class="BRbooksearch desktop">
219
219
  <input type="search" name="query" class="BRsearchInput" value="" placeholder="Search inside"/>
220
220
  <button type="submit" class="BRsearchSubmit">
221
- <img src="${this.br.imagesBaseURL}icon_search_button.svg" />
221
+ <img src="${this.br.options.imagesBaseURL}icon_search_button.svg" />
222
222
  </button>
223
223
  </form>
224
224
  `;
@@ -273,7 +273,7 @@ class SearchView {
273
273
  $(event.target).addClass('front');
274
274
  })
275
275
  .on("mouseleave", (event) => $(event.target).removeClass('front'))
276
- .on("click", () => { this.br._plugins.search.jumpToMatch(match.matchIndex); });
276
+ .on("click", () => { this.br.plugins.search.jumpToMatch(match.matchIndex); });
277
277
  });
278
278
  }
279
279
 
@@ -379,11 +379,11 @@ class SearchView {
379
379
 
380
380
  handleSearchStarted() {
381
381
  this.emptyMatches();
382
- this.br._plugins.search.removeSearchHilites();
382
+ this.br.plugins.search.removeSearchHilites();
383
383
  this.removeResultPins();
384
384
  this.toggleSearchPending(true);
385
385
  this.teardownSearchNavigation();
386
- this.setQuery(this.br._plugins.search.searchTerm);
386
+ this.setQuery(this.br.plugins.search.searchTerm);
387
387
  }
388
388
 
389
389
  /**
@@ -199,7 +199,7 @@ export class TtsPlugin extends BookReaderPlugin {
199
199
  }
200
200
 
201
201
  toggle() {
202
- this.br._plugins.autoplay?.stop();
202
+ this.br.plugins.autoplay?.stop();
203
203
  if (this.ttsEngine.playing) {
204
204
  this.stop();
205
205
  } else {
@@ -338,11 +338,11 @@ export class TtsPlugin extends BookReaderPlugin {
338
338
  * @param {number} [value]
339
339
  */
340
340
  sendAnalyticsEvent(action, value) {
341
- if (this.br._plugins.archiveAnalytics) {
341
+ if (this.br.plugins.archiveAnalytics) {
342
342
  const extraValues = {};
343
343
  const mediaLanguage = this.ttsEngine.opts.bookLanguage;
344
344
  if (mediaLanguage) extraValues.mediaLanguage = mediaLanguage;
345
- this.br._plugins.archiveAnalytics.sendEvent('BRReadAloud', action, value, extraValues);
345
+ this.br.plugins.archiveAnalytics.sendEvent('BRReadAloud', action, value, extraValues);
346
346
  }
347
347
  }
348
348
  }
@@ -1,6 +1,6 @@
1
1
  /* global BookReader */
2
2
 
3
- import { UrlPlugin } from "./UrlPlugin";
3
+ import { UrlPlugin } from "./UrlPlugin.js";
4
4
 
5
5
  /**
6
6
  * Plugin for URL management in BookReader
@@ -106,7 +106,7 @@ BookReader.prototype.urlStartLocationPolling = function() {
106
106
  this.trigger(BookReader.eventNames.stop);
107
107
  if (this.animating) {
108
108
  // Queue change if animating
109
- this._plugins.autoplay?.stop();
109
+ this.plugins.autoplay?.stop();
110
110
  this.animationFinishedCallback = updateParams;
111
111
  } else {
112
112
  // update immediately
@@ -1,5 +1,5 @@
1
1
  import { ClientFunction } from 'testcafe';
2
- import params from './helpers/params';
2
+ import params from './helpers/params.js';
3
3
 
4
4
  const getLocationHref = ClientFunction(() => window.location.href.toString());
5
5
  const FLIP_SPEED = 200;
@@ -1,7 +1,7 @@
1
- import { runBaseTests } from './helpers/base';
2
- import BookReader from './models/BookReader';
3
- import { runSearchTests } from './helpers/search';
4
- import params from './helpers/params';
1
+ import { runBaseTests } from './helpers/base.js';
2
+ import BookReader from './models/BookReader.js';
3
+ import { runSearchTests } from './helpers/search.js';
4
+ import params from './helpers/params.js';
5
5
 
6
6
  const ocaids = params.ocaids || [
7
7
  'theworksofplato01platiala',
@@ -78,8 +78,8 @@ export function runBaseTests (br) {
78
78
 
79
79
  // Check if uses plugin.resume.js
80
80
  const usesResume = ClientFunction(() => {
81
- const hasResumePlugin = typeof(br._plugins.resume) !== "undefined";
82
- const hasResumeValue = hasResumePlugin ? br._plugins.resume.getResumeValue() : false;
81
+ const hasResumePlugin = typeof(br.plugins.resume) !== "undefined";
82
+ const hasResumeValue = hasResumePlugin ? br.plugins.resume.getResumeValue() : false;
83
83
  return hasResumeValue;
84
84
  });
85
85
 
@@ -1,5 +1,5 @@
1
1
  import { Selector } from 'testcafe';
2
- import Nav from './Navigation';
2
+ import Nav from './Navigation.js';
3
3
 
4
4
  /** Model defining BookReader base elements */
5
5
  export default class BookReader {
@@ -1,8 +1,8 @@
1
- import { runBaseTests } from './helpers/base';
2
- import { runRightToLeftTests } from './helpers/rightToLeft';
3
- import params from './helpers/params';
1
+ import { runBaseTests } from './helpers/base.js';
2
+ import { runRightToLeftTests } from './helpers/rightToLeft.js';
3
+ import params from './helpers/params.js';
4
4
 
5
- import BookReader from './models/BookReader';
5
+ import BookReader from './models/BookReader.js';
6
6
 
7
7
  const ocaids = params.ocaids || [
8
8
  'gendaitankashu00meijuoft', // Right to Left book
@@ -1,6 +1,6 @@
1
1
  import { Selector } from 'testcafe';
2
- import BookReader from './models/BookReader';
3
- import params from './helpers/params';
2
+ import BookReader from './models/BookReader.js';
3
+ import params from './helpers/params.js';
4
4
 
5
5
  const ocaids = params.ocaids || ['goody'];
6
6
 
@@ -323,19 +323,6 @@ describe('<book-navigator>', () => {
323
323
  });
324
324
  });
325
325
 
326
- describe('Shortcuts', () => {
327
- test('has specific order of menu shortcuts to show', () => {
328
- const el = fixtureSync(container());
329
- expect(el.shortcutOrder).toEqual([
330
- 'fullscreen',
331
- 'volumes',
332
- 'chapters',
333
- 'search',
334
- 'bookmarks',
335
- ]);
336
- });
337
- });
338
-
339
326
  describe('Behaviors for specific menus', () => {
340
327
  describe('Search menu - ref: plugin.search.js', () => {
341
328
  test('Event: listens for `BookReader:ToggleSearchMenu to open search side panel', async () => {
@@ -1,6 +1,6 @@
1
1
  import { fixtureCleanup, fixtureSync } from '@open-wc/testing-helpers';
2
2
  import sinon from 'sinon';
3
- import DownloadsProvider from '@/src/BookNavigator/downloads/downloads-provider';
3
+ import DownloadsProvider from '@/src/BookNavigator/downloads/downloads-provider.js';
4
4
 
5
5
  const downloadableTypes = [
6
6
  ["PDF", "//archive.org/download/theworksofplato01platiala/theworksofplato01platiala.pdf"],
@@ -4,7 +4,7 @@ import {
4
4
  fixtureCleanup,
5
5
  } from '@open-wc/testing-helpers';
6
6
  import sinon from 'sinon';
7
- import '@/src/BookNavigator/downloads/downloads';
7
+ import '@/src/BookNavigator/downloads/downloads.js';
8
8
 
9
9
 
10
10
  const downloads = [