@internetarchive/bookreader 5.0.0-26 → 5.0.0-29

Sign up to get free protection for your applications and to get access to all the features.
Files changed (236) hide show
  1. package/.husky/_/husky.sh +30 -0
  2. package/BookReader/BookReader.css +1 -1
  3. package/BookReader/BookReader.js +1 -1
  4. package/BookReader/BookReader.js.map +1 -1
  5. package/BookReader/bookreader-component-bundle.js +570 -542
  6. package/BookReader/bookreader-component-bundle.js.LICENSE.txt +23 -0
  7. package/BookReader/bookreader-component-bundle.js.map +1 -1
  8. package/BookReader/plugins/plugin.search.js +1 -1
  9. package/BookReader/plugins/plugin.search.js.map +1 -1
  10. package/BookReader/plugins/plugin.tts.js.map +1 -1
  11. package/BookReader/plugins/plugin.url.js +1 -1
  12. package/BookReader/plugins/plugin.url.js.map +1 -1
  13. package/BookReaderDemo/BookReaderDemo.css +14 -1
  14. package/BookReaderDemo/IADemoBr.js +104 -0
  15. package/BookReaderDemo/demo-internetarchive.html +65 -98
  16. package/CHANGELOG.md +10 -0
  17. package/package.json +9 -6
  18. package/src/BookNavigator/assets/ia-logo.js +17 -0
  19. package/src/BookNavigator/book-navigator.js +521 -0
  20. package/src/BookNavigator/bookmarks/bookmark-button.js +2 -1
  21. package/src/BookNavigator/bookmarks/bookmarks-provider.js +20 -8
  22. package/src/BookNavigator/bookmarks/ia-bookmarks.js +84 -51
  23. package/src/BookNavigator/downloads/downloads-provider.js +5 -9
  24. package/src/BookNavigator/downloads/downloads.js +1 -0
  25. package/src/BookNavigator/search/search-provider.js +15 -8
  26. package/src/BookNavigator/sharing.js +27 -0
  27. package/src/BookNavigator/visual-adjustments/visual-adjustments-provider.js +9 -8
  28. package/src/BookNavigator/volumes/volumes-provider.js +44 -13
  29. package/src/BookNavigator/volumes/volumes.js +14 -3
  30. package/src/BookReader/options.js +6 -0
  31. package/src/BookReader.js +20 -8
  32. package/src/BookReaderComponent/BookReaderComponent.js +53 -32
  33. package/src/css/_BRComponent.scss +1 -1
  34. package/src/plugins/search/plugin.search.js +10 -9
  35. package/src/plugins/tts/FestivalTTSEngine.js +1 -1
  36. package/src/plugins/url/UrlPlugin.js +184 -0
  37. package/src/plugins/url/plugin.url.js +220 -0
  38. package/{src → stat}/BookNavigator/BookModel.js +0 -0
  39. package/{src → stat}/BookNavigator/BookNavigator.js +109 -102
  40. package/stat/BookNavigator/assets/bookmark-colors.js +15 -0
  41. package/stat/BookNavigator/assets/button-base.js +61 -0
  42. package/stat/BookNavigator/assets/ia-logo.js +17 -0
  43. package/stat/BookNavigator/assets/icon_checkmark.js +6 -0
  44. package/stat/BookNavigator/assets/icon_close.js +3 -0
  45. package/stat/BookNavigator/assets/icon_sort_asc.js +5 -0
  46. package/stat/BookNavigator/assets/icon_sort_desc.js +5 -0
  47. package/stat/BookNavigator/assets/icon_sort_neutral.js +5 -0
  48. package/stat/BookNavigator/assets/icon_volumes.js +11 -0
  49. package/stat/BookNavigator/bookmarks/bookmark-button.js +64 -0
  50. package/stat/BookNavigator/bookmarks/bookmark-edit.js +215 -0
  51. package/stat/BookNavigator/bookmarks/bookmarks-list.js +285 -0
  52. package/stat/BookNavigator/bookmarks/bookmarks-loginCTA.js +28 -0
  53. package/stat/BookNavigator/bookmarks/bookmarks-provider.js +56 -0
  54. package/stat/BookNavigator/bookmarks/ia-bookmarks.js +523 -0
  55. package/{src → stat}/BookNavigator/br-fullscreen-mgr.js +1 -2
  56. package/stat/BookNavigator/delete-modal-actions.js +49 -0
  57. package/stat/BookNavigator/downloads/downloads-provider.js +72 -0
  58. package/stat/BookNavigator/downloads/downloads.js +139 -0
  59. package/stat/BookNavigator/provider-config.js +0 -0
  60. package/stat/BookNavigator/search/a-search-result.js +55 -0
  61. package/stat/BookNavigator/search/search-provider.js +180 -0
  62. package/stat/BookNavigator/search/search-results.js +360 -0
  63. package/{src/ItemNavigator/providers → stat/BookNavigator}/sharing.js +3 -5
  64. package/stat/BookNavigator/visual-adjustments/visual-adjustments-provider.js +94 -0
  65. package/stat/BookNavigator/visual-adjustments/visual-adjustments.js +280 -0
  66. package/stat/BookNavigator/volumes/volumes-provider.js +83 -0
  67. package/stat/BookNavigator/volumes/volumes.js +178 -0
  68. package/stat/BookReader/BookModel.js +518 -0
  69. package/stat/BookReader/DebugConsole.js +54 -0
  70. package/stat/BookReader/DragScrollable.js +233 -0
  71. package/stat/BookReader/ImageCache.js +116 -0
  72. package/stat/BookReader/Mode1Up.js +102 -0
  73. package/stat/BookReader/Mode1UpLit.js +434 -0
  74. package/stat/BookReader/Mode2Up.js +1372 -0
  75. package/stat/BookReader/ModeSmoothZoom.js +177 -0
  76. package/stat/BookReader/ModeThumb.js +344 -0
  77. package/stat/BookReader/Navbar/Navbar.js +310 -0
  78. package/stat/BookReader/PageContainer.js +120 -0
  79. package/stat/BookReader/ReduceSet.js +26 -0
  80. package/stat/BookReader/Toolbar/Toolbar.js +384 -0
  81. package/stat/BookReader/events.js +20 -0
  82. package/stat/BookReader/options.js +324 -0
  83. package/stat/BookReader/utils/HTMLDimensionsCacher.js +44 -0
  84. package/stat/BookReader/utils/classes.js +36 -0
  85. package/stat/BookReader/utils.js +240 -0
  86. package/stat/BookReader.js +2550 -0
  87. package/stat/BookReaderComponent/BookReaderComponent.js +117 -0
  88. package/stat/assets/icons/1up.svg +12 -0
  89. package/stat/assets/icons/2up.svg +15 -0
  90. package/stat/assets/icons/advance.svg +26 -0
  91. package/stat/assets/icons/chevron-right.svg +1 -0
  92. package/stat/assets/icons/close-circle-dark.svg +1 -0
  93. package/stat/assets/icons/close-circle.svg +1 -0
  94. package/stat/assets/icons/fullscreen.svg +17 -0
  95. package/stat/assets/icons/fullscreen_exit.svg +17 -0
  96. package/stat/assets/icons/hamburger.svg +15 -0
  97. package/stat/assets/icons/left-arrow.svg +12 -0
  98. package/stat/assets/icons/magnify-minus.svg +16 -0
  99. package/stat/assets/icons/magnify-plus.svg +17 -0
  100. package/stat/assets/icons/magnify.svg +15 -0
  101. package/stat/assets/icons/pause.svg +23 -0
  102. package/stat/assets/icons/play.svg +22 -0
  103. package/stat/assets/icons/playback-speed.svg +34 -0
  104. package/stat/assets/icons/read-aloud.svg +22 -0
  105. package/stat/assets/icons/review.svg +22 -0
  106. package/stat/assets/icons/thumbnails.svg +17 -0
  107. package/stat/assets/icons/voice.svg +1 -0
  108. package/stat/assets/icons/volume-full.svg +22 -0
  109. package/stat/assets/images/BRicons.png +0 -0
  110. package/stat/assets/images/BRicons.svg +94 -0
  111. package/stat/assets/images/BRicons_ia.png +0 -0
  112. package/stat/assets/images/back_pages.png +0 -0
  113. package/stat/assets/images/book_bottom_icon.png +0 -0
  114. package/stat/assets/images/book_down_icon.png +0 -0
  115. package/stat/assets/images/book_left_icon.png +0 -0
  116. package/stat/assets/images/book_leftmost_icon.png +0 -0
  117. package/stat/assets/images/book_right_icon.png +0 -0
  118. package/stat/assets/images/book_rightmost_icon.png +0 -0
  119. package/stat/assets/images/book_top_icon.png +0 -0
  120. package/stat/assets/images/book_up_icon.png +0 -0
  121. package/stat/assets/images/books_graphic.svg +177 -0
  122. package/stat/assets/images/booksplit.png +0 -0
  123. package/stat/assets/images/control_pause_icon.png +0 -0
  124. package/stat/assets/images/control_play_icon.png +0 -0
  125. package/stat/assets/images/embed_icon.png +0 -0
  126. package/stat/assets/images/icon-home-ia.png +0 -0
  127. package/stat/assets/images/icon_OL-logo-xs.png +0 -0
  128. package/stat/assets/images/icon_alert-xs.png +0 -0
  129. package/stat/assets/images/icon_book.svg +12 -0
  130. package/stat/assets/images/icon_bookmark.svg +12 -0
  131. package/stat/assets/images/icon_close-pop.png +0 -0
  132. package/stat/assets/images/icon_download.png +0 -0
  133. package/stat/assets/images/icon_gear.svg +14 -0
  134. package/stat/assets/images/icon_hamburger.svg +20 -0
  135. package/stat/assets/images/icon_home.png +0 -0
  136. package/stat/assets/images/icon_home.svg +21 -0
  137. package/stat/assets/images/icon_home_ia.png +0 -0
  138. package/stat/assets/images/icon_indicator.png +0 -0
  139. package/stat/assets/images/icon_info.svg +11 -0
  140. package/stat/assets/images/icon_one_page.svg +8 -0
  141. package/stat/assets/images/icon_pause.svg +1 -0
  142. package/stat/assets/images/icon_play.svg +1 -0
  143. package/stat/assets/images/icon_playback-rate.svg +15 -0
  144. package/stat/assets/images/icon_return.png +0 -0
  145. package/stat/assets/images/icon_search_button.svg +8 -0
  146. package/stat/assets/images/icon_share.svg +9 -0
  147. package/stat/assets/images/icon_skip-ahead.svg +6 -0
  148. package/stat/assets/images/icon_skip-back.svg +13 -0
  149. package/stat/assets/images/icon_speaker.svg +18 -0
  150. package/stat/assets/images/icon_speaker_open.svg +10 -0
  151. package/stat/assets/images/icon_thumbnails.svg +12 -0
  152. package/stat/assets/images/icon_toc.svg +5 -0
  153. package/stat/assets/images/icon_two_pages.svg +9 -0
  154. package/stat/assets/images/icon_zoomer.png +0 -0
  155. package/stat/assets/images/loading.gif +0 -0
  156. package/stat/assets/images/logo_icon.png +0 -0
  157. package/stat/assets/images/marker_chap-off.png +0 -0
  158. package/stat/assets/images/marker_chap-off.svg +11 -0
  159. package/stat/assets/images/marker_chap-off_ia.png +0 -0
  160. package/stat/assets/images/marker_chap-on.png +0 -0
  161. package/stat/assets/images/marker_chap-on.svg +11 -0
  162. package/stat/assets/images/marker_srch-on.svg +11 -0
  163. package/stat/assets/images/marker_srchchap-off.png +0 -0
  164. package/stat/assets/images/marker_srchchap-on.png +0 -0
  165. package/stat/assets/images/nav_control-dn.png +0 -0
  166. package/stat/assets/images/nav_control-dn_ia.png +0 -0
  167. package/stat/assets/images/nav_control-up.png +0 -0
  168. package/stat/assets/images/nav_control-up_ia.png +0 -0
  169. package/stat/assets/images/nav_control.png +0 -0
  170. package/stat/assets/images/one_page_mode_icon.png +0 -0
  171. package/stat/assets/images/paper-badge.png +0 -0
  172. package/stat/assets/images/print_icon.png +0 -0
  173. package/stat/assets/images/progressbar.gif +0 -0
  174. package/stat/assets/images/right_edges.png +0 -0
  175. package/stat/assets/images/slider.png +0 -0
  176. package/stat/assets/images/slider_ia.png +0 -0
  177. package/stat/assets/images/thumbnail_mode_icon.png +0 -0
  178. package/stat/assets/images/transparent.png +0 -0
  179. package/stat/assets/images/two_page_mode_icon.png +0 -0
  180. package/stat/assets/images/zoom_in_icon.png +0 -0
  181. package/stat/assets/images/zoom_out_icon.png +0 -0
  182. package/stat/css/BookReader.scss +89 -0
  183. package/stat/css/_BRBookmarks.scss +29 -0
  184. package/stat/css/_BRComponent.scss +13 -0
  185. package/stat/css/_BRfloat.scss +197 -0
  186. package/stat/css/_BRicon.scss +48 -0
  187. package/stat/css/_BRmain.scss +251 -0
  188. package/stat/css/_BRnav.scss +359 -0
  189. package/stat/css/_BRpages.scss +139 -0
  190. package/stat/css/_BRsearch.scss +226 -0
  191. package/stat/css/_BRtoolbar.scss +84 -0
  192. package/stat/css/_BRvendor.scss +5 -0
  193. package/stat/css/_MobileNav.scss +194 -0
  194. package/stat/css/_TextSelection.scss +32 -0
  195. package/stat/css/_colorbox.scss +52 -0
  196. package/stat/css/_controls.scss +253 -0
  197. package/stat/css/_icons.scss +121 -0
  198. package/stat/jquery-wrapper.js +4 -0
  199. package/stat/plugins/plugin.archive_analytics.js +86 -0
  200. package/stat/plugins/plugin.autoplay.js +129 -0
  201. package/stat/plugins/plugin.chapters.js +248 -0
  202. package/stat/plugins/plugin.iframe.js +48 -0
  203. package/stat/plugins/plugin.mobile_nav.js +288 -0
  204. package/stat/plugins/plugin.resume.js +68 -0
  205. package/stat/plugins/plugin.text_selection.js +291 -0
  206. package/{src → stat}/plugins/plugin.url.js +0 -0
  207. package/stat/plugins/plugin.vendor-fullscreen.js +247 -0
  208. package/stat/plugins/search/plugin.search.js +439 -0
  209. package/stat/plugins/search/view.js +439 -0
  210. package/stat/plugins/tts/AbstractTTSEngine.js +249 -0
  211. package/stat/plugins/tts/FestivalTTSEngine.js +169 -0
  212. package/stat/plugins/tts/PageChunk.js +107 -0
  213. package/stat/plugins/tts/PageChunkIterator.js +163 -0
  214. package/stat/plugins/tts/WebTTSEngine.js +357 -0
  215. package/stat/plugins/tts/plugin.tts.js +357 -0
  216. package/stat/plugins/tts/tooltip_dict.js +15 -0
  217. package/stat/plugins/tts/utils.js +91 -0
  218. package/stat/util/browserSniffing.js +30 -0
  219. package/stat/util/debouncer.js +26 -0
  220. package/stat/util/docCookies.js +67 -0
  221. package/stat/util/strings.js +34 -0
  222. package/tests/e2e/viewmode.test.js +30 -30
  223. package/tests/jest/BookReader/BookReaderPublicFunctions.test.js +64 -52
  224. package/tests/jest/BookReader.test.js +1 -1
  225. package/tests/jest/plugins/url/UrlPlugin.test.js +175 -0
  226. package/tests/jest/plugins/{plugin.url.test.js → url/plugin.url.test.js} +3 -2
  227. package/tests/karma/BookNavigator/book-navigator.test.js +413 -108
  228. package/tests/karma/BookNavigator/bookmarks/bookmark-button.test.js +44 -0
  229. package/tests/karma/BookNavigator/downloads/downloads-provider.test.js +6 -3
  230. package/tests/karma/BookNavigator/search/search-provider.test.js +106 -6
  231. package/tests/karma/BookNavigator/search/search-results.test.js +0 -2
  232. package/tests/karma/BookNavigator/sharing/sharing-provider.test.js +29 -20
  233. package/tests/karma/BookNavigator/volumes/volumes-provider.test.js +46 -22
  234. package/webpack.config.js +1 -1
  235. package/src/BookNavigator/assets/book-loader.js +0 -27
  236. package/src/ItemNavigator/ItemNavigator.js +0 -377
@@ -0,0 +1,434 @@
1
+ // @ts-check
2
+ import { customElement, html, LitElement, property, query } from 'lit-element';
3
+ import { styleMap } from 'lit-html/directives/style-map';
4
+ import { ModeSmoothZoom } from './ModeSmoothZoom';
5
+ import { arrChanged, calcScreenDPI, genToArray, sum, throttle } from './utils';
6
+ import { HTMLDimensionsCacher } from "./utils/HTMLDimensionsCacher";
7
+ /** @typedef {import('./BookModel').BookModel} BookModel */
8
+ /** @typedef {import('./BookModel').PageIndex} PageIndex */
9
+ /** @typedef {import('./BookModel').PageModel} PageModel */
10
+ /** @typedef {import('./ModeSmoothZoom').SmoothZoomable} SmoothZoomable */
11
+ /** @typedef {import('./PageContainer').PageContainer} PageContainer */
12
+ /** @typedef {import('../BookReader').default} BookReader */
13
+
14
+ // I _have_ to make this globally public, otherwise it won't let me call
15
+ // it's constructor :/
16
+ /** @implements {SmoothZoomable} */
17
+ @customElement('br-mode-1up')
18
+ export class Mode1UpLit extends LitElement {
19
+ /****************************************/
20
+ /************** PROPERTIES **************/
21
+ /****************************************/
22
+
23
+ /** @type {BookReader} */
24
+ br;
25
+
26
+ /************** BOOK-RELATED PROPERTIES **************/
27
+
28
+ /** @type {BookModel} */
29
+ @property({ type: Object })
30
+ book;
31
+
32
+ /** @type {PageModel[]} */
33
+ @property({ type: Array })
34
+ pages = [];
35
+
36
+ /** @type {Record<PageIndex, number>} in world coordinates (inches) */
37
+ @property({ type: Object })
38
+ pageTops = {};
39
+
40
+ /************** SCALE-RELATED PROPERTIES **************/
41
+
42
+ /** @private */
43
+ screenDPI = calcScreenDPI();
44
+
45
+ /**
46
+ * How much smaller the rendered pages are than the real-world item
47
+ *
48
+ * Mode1Up doesn't use the br.reduce because it is DPI aware. The reduction factor
49
+ * of a given leaf can change (since leaves can have different DPIs), but the real-world
50
+ * reduction is constant throughout.
51
+ */
52
+ realWorldReduce = 1;
53
+
54
+ @property({ type: Number })
55
+ scale = 1;
56
+ /** Position (in unit-less, [0, 1] coordinates) in client to scale around */
57
+ @property({ type: Object })
58
+ scaleCenter = { x: 0.5, y: 0.5 };
59
+
60
+ /************** VIRTUAL-SCROLLING PROPERTIES **************/
61
+
62
+ /** in world coordinates (inches) */
63
+ @property({ type: Object })
64
+ visibleRegion = {
65
+ top: 0,
66
+ left: 0,
67
+ width: 100,
68
+ height: 100,
69
+ };
70
+
71
+ /** @type {PageModel[]} */
72
+ @property({ type: Array, hasChanged: arrChanged })
73
+ visiblePages = [];
74
+
75
+ /** @type {PageModel[]} */
76
+ @property({ type: Array })
77
+ renderedPages = [];
78
+
79
+ /** @type {Record<PageIndex, PageContainer>} position in inches */
80
+ pageContainerCache = {};
81
+
82
+ /************** WORLD-RELATED PROPERTIES **************/
83
+ /**
84
+ * The world is an imaginary giant document that contains all the pages.
85
+ * The "world"'s size is used to determine how long the scroll bar should
86
+ * be, for example.
87
+ */
88
+
89
+ /** @type {HTMLElement} */
90
+ @query('.br-mode-1up__world')
91
+ $world;
92
+
93
+ worldDimensions = { width: 100, height: 100 };
94
+
95
+ get worldStyle() {
96
+ const wToR = this.worldUnitsToRenderedPixels;
97
+ return {
98
+ width: wToR(this.worldDimensions.width) + "px",
99
+ height: wToR(this.worldDimensions.height) + "px",
100
+ };
101
+ }
102
+
103
+ /** @type {HTMLElement} */
104
+ get $container() { return this; }
105
+
106
+ /** @type {HTMLElement} */
107
+ @query('.br-mode-1up__visible-world')
108
+ $visibleWorld;
109
+
110
+ /************** DOM-RELATED PROPERTIES **************/
111
+
112
+ /** @type {HTMLDimensionsCacher} Cache things like clientWidth to reduce repaints */
113
+ htmlDimensionsCacher = new HTMLDimensionsCacher(this);
114
+
115
+ smoothZoomer = new ModeSmoothZoom(this);
116
+
117
+ /************** CONSTANT PROPERTIES **************/
118
+
119
+ /** Vertical space between/around the pages in inches */
120
+ SPACING_IN = 0.2;
121
+
122
+ /** How much to zoom when zoom button pressed */
123
+ ZOOM_FACTOR = 1.1;
124
+
125
+ /****************************************/
126
+ /************** PUBLIC API **************/
127
+ /****************************************/
128
+
129
+ /************** MAIN PUBLIC METHODS **************/
130
+
131
+ /**
132
+ * @param {PageIndex} index
133
+ */
134
+ jumpToIndex(index, { smooth = false } = {}) {
135
+ if (smooth) {
136
+ this.style.scrollBehavior = 'smooth';
137
+ }
138
+ this.scrollTop = this.worldUnitsToVisiblePixels(this.pageTops[index] - this.SPACING_IN / 2);
139
+ // TODO: Also h center?
140
+ if (smooth) {
141
+ setTimeout(() => this.style.scrollBehavior = '', 100);
142
+ }
143
+ }
144
+
145
+ zoomIn() {
146
+ this.scale *= this.ZOOM_FACTOR;
147
+ }
148
+
149
+ zoomOut() {
150
+ this.scale *= 1 / this.ZOOM_FACTOR;
151
+ }
152
+
153
+ /********************************************/
154
+ /************** INTERNAL STUFF **************/
155
+ /********************************************/
156
+
157
+ /************** LIFE CYCLE **************/
158
+
159
+ /**
160
+ * @param {BookModel} book
161
+ * @param {BookReader} br
162
+ */
163
+ constructor(book, br) {
164
+ super();
165
+ this.book = book;
166
+
167
+ /** @type {BookReader} */
168
+ this.br = br;
169
+ }
170
+
171
+ /** @override */
172
+ firstUpdated(changedProps) {
173
+ super.firstUpdated(changedProps);
174
+ this.htmlDimensionsCacher.updateClientSizes();
175
+ this.smoothZoomer.attach();
176
+ }
177
+
178
+ /**
179
+ * @param {PageIndex} startIndex
180
+ */
181
+ initFirstRender(startIndex) {
182
+ const page = this.book.getPage(startIndex);
183
+ this.scale = this.computeDefaultScale(page);
184
+ }
185
+
186
+ /** @override */
187
+ updated(changedProps) {
188
+ // this.X is the new value
189
+ // changedProps.get('X') is the old value
190
+ if (changedProps.has('book')) {
191
+ this.pages = genToArray(this.book.pagesIterator({ combineConsecutiveUnviewables: true }));
192
+ }
193
+ if (changedProps.has('pages')) {
194
+ this.worldDimensions = this.computeWorldDimensions();
195
+ this.pageTops = this.computePageTops(this.pages, this.SPACING_IN);
196
+ }
197
+ if (changedProps.has('visibleRegion')) {
198
+ this.visiblePages = this.computeVisiblePages();
199
+ }
200
+ if (changedProps.has('visiblePages')) {
201
+ this.throttledUpdateRenderedPages();
202
+ this.br.displayedIndices = this.visiblePages.map(p => p.index);
203
+ this.br.updateFirstIndex(this.br.displayedIndices[0]);
204
+ this.br.updateNavIndexThrottled();
205
+ }
206
+ if (changedProps.has('scale')) {
207
+ const oldVal = changedProps.get('scale');
208
+ // Need to set this scale to actually scale the pages
209
+ this.$visibleWorld.style.transform = `scale(${this.scale})`;
210
+ this.updateViewportOnZoom(this.scale, oldVal);
211
+ // Need to set this scale to update the world size, so the scrollbar gets the correct size
212
+ this.$world.style.transform = `scale(${this.scale})`;
213
+ }
214
+ }
215
+
216
+ /** @override */
217
+ connectedCallback() {
218
+ super.connectedCallback();
219
+ this.htmlDimensionsCacher.attachResizeListener();
220
+ this.attachScrollListeners();
221
+ this.smoothZoomer.attach();
222
+ }
223
+
224
+ /** @override */
225
+ disconnectedCallback() {
226
+ this.htmlDimensionsCacher.detachResizeListener();
227
+ this.detachScrollListeners();
228
+ this.smoothZoomer.detach();
229
+ super.disconnectedCallback();
230
+ }
231
+
232
+ /************** LIT CONFIGS **************/
233
+
234
+ /** @override */
235
+ createRenderRoot() {
236
+ // Disable shadow DOM; that would require a huge rejiggering of CSS
237
+ return this;
238
+ }
239
+
240
+ /************** COORDINATE SPACE CONVERTERS **************/
241
+ /**
242
+ * There are a few different "coordinate spaces" at play in BR:
243
+ * (1) World units: i.e. inches. Unless otherwise stated, all computations
244
+ * are done in world units.
245
+ * (2) Rendered Pixels: i.e. img.width = '300'. Note this does _not_ take
246
+ * into account zoom scaling.
247
+ * (3) Visible Pixels: Just rendered pixels, but taking into account scaling.
248
+ */
249
+
250
+ worldUnitsToRenderedPixels = (/** @type {number} */inches) => inches * this.screenDPI / this.realWorldReduce;
251
+ renderedPixelsToWorldUnits = (/** @type {number} */px) => px * this.realWorldReduce / this.screenDPI;
252
+
253
+ renderedPixelsToVisiblePixels = (/** @type {number} */px) => px * this.scale;
254
+ visiblePixelsToRenderedPixels = (/** @type {number} */px) => px / this.scale;
255
+
256
+ worldUnitsToVisiblePixels = (/** @type {number} */px) => this.renderedPixelsToVisiblePixels(this.worldUnitsToRenderedPixels(px));
257
+ visiblePixelsToWorldUnits = (/** @type {number} */px) => this.renderedPixelsToWorldUnits(this.visiblePixelsToRenderedPixels(px));
258
+
259
+ /************** RENDERING **************/
260
+
261
+ /** @override */
262
+ render() {
263
+ return html`
264
+ <div class="br-mode-1up__world" style=${styleMap(this.worldStyle)}></div>
265
+ <div class="br-mode-1up__visible-world">
266
+ ${this.renderedPages.map(p => this.renderPage(p))}
267
+ </div>`;
268
+ }
269
+
270
+ /** @param {PageModel} page */
271
+ createPageContainer = (page) => {
272
+ return this.pageContainerCache[page.index] || (
273
+ this.pageContainerCache[page.index] = (
274
+ // @ts-ignore I know it's protected, TS! But Mode1Up and BookReader are friends.
275
+ this.br._createPageContainer(page.index)
276
+ )
277
+ );
278
+ }
279
+
280
+ /** @param {PageModel} page */
281
+ renderPage = (page) => {
282
+ const wToR = this.worldUnitsToRenderedPixels;
283
+ const wToV = this.worldUnitsToVisiblePixels;
284
+ const containerWidth = this.visiblePixelsToWorldUnits(this.htmlDimensionsCacher.clientWidth);
285
+
286
+ const width = wToR(page.widthInches);
287
+ const height = wToR(page.heightInches);
288
+ const left = Math.max(this.SPACING_IN, (containerWidth - page.widthInches) / 2);
289
+ const top = this.pageTops[page.index];
290
+
291
+ const transform = `translate(${wToR(left)}px, ${wToR(top)}px)`;
292
+ const pageContainerEl = this.createPageContainer(page)
293
+ .update({
294
+ dimensions: {
295
+ width,
296
+ height,
297
+ top: 0,
298
+ left: 0,
299
+ },
300
+ reduce: page.width / wToV(page.widthInches),
301
+ }).$container[0];
302
+
303
+ pageContainerEl.style.transform = transform;
304
+ return pageContainerEl;
305
+ }
306
+
307
+ /************** VIRTUAL SCROLLING LOGIC **************/
308
+
309
+ updateVisibleRegion = () => {
310
+ const { scrollTop, scrollLeft } = this;
311
+ // clientHeight excludes scrollbars, which is good.
312
+ const clientWidth = this.htmlDimensionsCacher.clientWidth;
313
+ const clientHeight = this.htmlDimensionsCacher.clientHeight;
314
+
315
+ // Note: scrollTop, and clientWidth all are in visible space;
316
+ // i.e. they are affects by the CSS transforms.
317
+
318
+ const vToW = this.visiblePixelsToWorldUnits;
319
+ this.visibleRegion = {
320
+ top: vToW(scrollTop),
321
+ height: vToW(clientHeight),
322
+ // TODO: These are very likely wrong
323
+ left: vToW(scrollLeft),
324
+ width: vToW(clientWidth),
325
+ };
326
+ }
327
+
328
+ /**
329
+ * @returns {PageModel[]}
330
+ */
331
+ computeRenderedPages() {
332
+ // Also render 1 page before/after
333
+ // @ts-ignore TS doesn't understand the filtering out of null values
334
+ return [
335
+ this.visiblePages[0]?.prev,
336
+ ...this.visiblePages,
337
+ this.visiblePages[this.visiblePages.length - 1]?.next,
338
+ ]
339
+ .filter(p => p)
340
+ // Never render more than 10 pages! Usually means something is wrong
341
+ .slice(0, 10);
342
+ }
343
+
344
+ throttledUpdateRenderedPages = throttle(() => {
345
+ this.renderedPages = this.computeRenderedPages();
346
+ this.requestUpdate();
347
+ }, 100, null)
348
+
349
+ /**
350
+ * @param {PageModel[]} pages
351
+ * @param {number} spacing
352
+ */
353
+ computePageTops(pages, spacing) {
354
+ /** @type {{ [pageIndex: string]: number }} */
355
+ const result = {};
356
+ let top = spacing;
357
+ for (const page of pages) {
358
+ result[page.index] = top;
359
+ top += page.heightInches + spacing;
360
+ }
361
+ return result;
362
+ }
363
+
364
+ /**
365
+ * @param {PageModel} page
366
+ * @returns {number}
367
+ */
368
+ computeDefaultScale(page) {
369
+ // Default to real size if it fits, otherwise default to full width
370
+ const containerWidthIn = this.visiblePixelsToWorldUnits(this.htmlDimensionsCacher.clientWidth);
371
+ return Math.min(1, containerWidthIn / (page.widthInches + 2 * this.SPACING_IN));
372
+ }
373
+
374
+ computeWorldDimensions() {
375
+ return {
376
+ width: Math.max(...this.pages.map(p => p.widthInches)) + 2 * this.SPACING_IN,
377
+ height:
378
+ sum(this.pages.map(p => p.heightInches)) +
379
+ (this.pages.length + 1) * this.SPACING_IN,
380
+ };
381
+ }
382
+
383
+ computeVisiblePages() {
384
+ return this.pages.filter(page => {
385
+ const PT = this.pageTops[page.index];
386
+ const PB = PT + page.heightInches;
387
+
388
+ const VT = this.visibleRegion.top;
389
+ const VB = VT + this.visibleRegion.height;
390
+ return PT <= VB && PB >= VT;
391
+ });
392
+ }
393
+
394
+ /************** ZOOMING LOGIC **************/
395
+
396
+ /**
397
+ * @param {number} newScale
398
+ * @param {number} oldScale
399
+ */
400
+ updateViewportOnZoom(newScale, oldScale) {
401
+ const container = this;
402
+ const { scrollTop: T, scrollLeft: L } = container;
403
+ const W = this.htmlDimensionsCacher.clientWidth;
404
+ const H = this.htmlDimensionsCacher.clientHeight;
405
+
406
+ // Scale factor change
407
+ const F = newScale / oldScale;
408
+
409
+ // Where in the viewport the zoom is centered on
410
+ const XPOS = this.scaleCenter.x;
411
+ const YPOS = this.scaleCenter.y;
412
+ const oldCenter = {
413
+ x: L + XPOS * W,
414
+ y: T + YPOS * H,
415
+ };
416
+ const newCenter = {
417
+ x: F * oldCenter.x,
418
+ y: F * oldCenter.y,
419
+ };
420
+ container.scrollTop = newCenter.y - YPOS * H;
421
+ container.scrollLeft = newCenter.x - XPOS * W;
422
+ this.updateVisibleRegion();
423
+ }
424
+
425
+ /************** INPUT HANDLERS **************/
426
+
427
+ attachScrollListeners = () => {
428
+ this.addEventListener("scroll", this.updateVisibleRegion, { passive: true });
429
+ }
430
+
431
+ detachScrollListeners = () => {
432
+ this.removeEventListener("scroll", this.updateVisibleRegion);
433
+ }
434
+ }