@internetarchive/bookreader 5.0.0-38 → 5.0.0-39

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. package/BookReader/BookReader.css +8 -0
  2. package/BookReader/BookReader.js +1 -1
  3. package/BookReader/BookReader.js.map +1 -1
  4. package/BookReader/ia-bookreader-bundle.js +99 -75
  5. package/BookReader/ia-bookreader-bundle.js.map +1 -1
  6. package/BookReader/icons/magnify-minus.svg +1 -1
  7. package/BookReader/icons/magnify-plus.svg +1 -1
  8. package/BookReader/plugins/plugin.autoplay.js +1 -1
  9. package/BookReader/plugins/plugin.autoplay.js.map +1 -1
  10. package/BookReader/plugins/plugin.chapters.js +1 -1
  11. package/BookReader/plugins/plugin.chapters.js.map +1 -1
  12. package/BookReader/plugins/plugin.mobile_nav.js +1 -1
  13. package/BookReader/plugins/plugin.mobile_nav.js.map +1 -1
  14. package/BookReader/plugins/plugin.resume.js +1 -1
  15. package/BookReader/plugins/plugin.resume.js.map +1 -1
  16. package/BookReader/plugins/plugin.search.js +1 -1
  17. package/BookReader/plugins/plugin.search.js.map +1 -1
  18. package/BookReader/plugins/plugin.text_selection.js +1 -1
  19. package/BookReader/plugins/plugin.text_selection.js.map +1 -1
  20. package/BookReader/plugins/plugin.tts.js +1 -1
  21. package/BookReader/plugins/plugin.tts.js.map +1 -1
  22. package/BookReader/plugins/plugin.url.js +1 -1
  23. package/BookReader/plugins/plugin.url.js.map +1 -1
  24. package/CHANGELOG.md +5 -0
  25. package/README.md +13 -0
  26. package/package.json +14 -14
  27. package/renovate.json +1 -1
  28. package/src/BookReader/Mode1UpLit.js +7 -1
  29. package/src/BookReader/Mode2Up.js +11 -0
  30. package/src/BookReader/ModeSmoothZoom.js +2 -0
  31. package/src/BookReader/PageContainer.js +10 -4
  32. package/src/BookReader/utils/ScrollClassAdder.js +31 -0
  33. package/src/assets/icons/magnify-minus.svg +3 -7
  34. package/src/assets/icons/magnify-plus.svg +3 -7
  35. package/src/css/_TextSelection.scss +13 -0
  36. package/tests/jest/BookReader/PageContainer.test.js +5 -4
  37. package/tests/jest/BookReader/utils/ScrollClassAdder.test.js +49 -0
  38. package/.husky/_/husky.sh +0 -30
  39. package/stat/BookNavigator/BookModel.js +0 -14
  40. package/stat/BookNavigator/BookNavigator.js +0 -524
  41. package/stat/BookNavigator/assets/bookmark-colors.js +0 -15
  42. package/stat/BookNavigator/assets/button-base.js +0 -61
  43. package/stat/BookNavigator/assets/ia-logo.js +0 -17
  44. package/stat/BookNavigator/assets/icon_checkmark.js +0 -6
  45. package/stat/BookNavigator/assets/icon_close.js +0 -3
  46. package/stat/BookNavigator/assets/icon_sort_asc.js +0 -5
  47. package/stat/BookNavigator/assets/icon_sort_desc.js +0 -5
  48. package/stat/BookNavigator/assets/icon_sort_neutral.js +0 -5
  49. package/stat/BookNavigator/assets/icon_volumes.js +0 -11
  50. package/stat/BookNavigator/bookmarks/bookmark-button.js +0 -64
  51. package/stat/BookNavigator/bookmarks/bookmark-edit.js +0 -215
  52. package/stat/BookNavigator/bookmarks/bookmarks-list.js +0 -285
  53. package/stat/BookNavigator/bookmarks/bookmarks-loginCTA.js +0 -28
  54. package/stat/BookNavigator/bookmarks/bookmarks-provider.js +0 -56
  55. package/stat/BookNavigator/bookmarks/ia-bookmarks.js +0 -523
  56. package/stat/BookNavigator/br-fullscreen-mgr.js +0 -82
  57. package/stat/BookNavigator/delete-modal-actions.js +0 -49
  58. package/stat/BookNavigator/downloads/downloads-provider.js +0 -72
  59. package/stat/BookNavigator/downloads/downloads.js +0 -139
  60. package/stat/BookNavigator/provider-config.js +0 -0
  61. package/stat/BookNavigator/search/a-search-result.js +0 -55
  62. package/stat/BookNavigator/search/search-provider.js +0 -180
  63. package/stat/BookNavigator/search/search-results.js +0 -360
  64. package/stat/BookNavigator/sharing.js +0 -31
  65. package/stat/BookNavigator/visual-adjustments/visual-adjustments-provider.js +0 -94
  66. package/stat/BookNavigator/visual-adjustments/visual-adjustments.js +0 -280
  67. package/stat/BookNavigator/volumes/volumes-provider.js +0 -83
  68. package/stat/BookNavigator/volumes/volumes.js +0 -178
  69. package/stat/BookReader/BookModel.js +0 -518
  70. package/stat/BookReader/DebugConsole.js +0 -54
  71. package/stat/BookReader/DragScrollable.js +0 -233
  72. package/stat/BookReader/ImageCache.js +0 -116
  73. package/stat/BookReader/Mode1Up.js +0 -102
  74. package/stat/BookReader/Mode1UpLit.js +0 -434
  75. package/stat/BookReader/Mode2Up.js +0 -1372
  76. package/stat/BookReader/ModeSmoothZoom.js +0 -177
  77. package/stat/BookReader/ModeThumb.js +0 -344
  78. package/stat/BookReader/Navbar/Navbar.js +0 -310
  79. package/stat/BookReader/PageContainer.js +0 -120
  80. package/stat/BookReader/ReduceSet.js +0 -26
  81. package/stat/BookReader/Toolbar/Toolbar.js +0 -384
  82. package/stat/BookReader/events.js +0 -20
  83. package/stat/BookReader/options.js +0 -324
  84. package/stat/BookReader/utils/HTMLDimensionsCacher.js +0 -44
  85. package/stat/BookReader/utils/classes.js +0 -36
  86. package/stat/BookReader/utils.js +0 -240
  87. package/stat/BookReader.js +0 -2550
  88. package/stat/BookReaderComponent/BookReaderComponent.js +0 -117
  89. package/stat/assets/icons/1up.svg +0 -12
  90. package/stat/assets/icons/2up.svg +0 -15
  91. package/stat/assets/icons/advance.svg +0 -26
  92. package/stat/assets/icons/chevron-right.svg +0 -1
  93. package/stat/assets/icons/close-circle-dark.svg +0 -1
  94. package/stat/assets/icons/close-circle.svg +0 -1
  95. package/stat/assets/icons/fullscreen.svg +0 -17
  96. package/stat/assets/icons/fullscreen_exit.svg +0 -17
  97. package/stat/assets/icons/hamburger.svg +0 -15
  98. package/stat/assets/icons/left-arrow.svg +0 -12
  99. package/stat/assets/icons/magnify-minus.svg +0 -16
  100. package/stat/assets/icons/magnify-plus.svg +0 -17
  101. package/stat/assets/icons/magnify.svg +0 -15
  102. package/stat/assets/icons/pause.svg +0 -23
  103. package/stat/assets/icons/play.svg +0 -22
  104. package/stat/assets/icons/playback-speed.svg +0 -34
  105. package/stat/assets/icons/read-aloud.svg +0 -22
  106. package/stat/assets/icons/review.svg +0 -22
  107. package/stat/assets/icons/thumbnails.svg +0 -17
  108. package/stat/assets/icons/voice.svg +0 -1
  109. package/stat/assets/icons/volume-full.svg +0 -22
  110. package/stat/assets/images/BRicons.png +0 -0
  111. package/stat/assets/images/BRicons.svg +0 -94
  112. package/stat/assets/images/BRicons_ia.png +0 -0
  113. package/stat/assets/images/back_pages.png +0 -0
  114. package/stat/assets/images/book_bottom_icon.png +0 -0
  115. package/stat/assets/images/book_down_icon.png +0 -0
  116. package/stat/assets/images/book_left_icon.png +0 -0
  117. package/stat/assets/images/book_leftmost_icon.png +0 -0
  118. package/stat/assets/images/book_right_icon.png +0 -0
  119. package/stat/assets/images/book_rightmost_icon.png +0 -0
  120. package/stat/assets/images/book_top_icon.png +0 -0
  121. package/stat/assets/images/book_up_icon.png +0 -0
  122. package/stat/assets/images/books_graphic.svg +0 -177
  123. package/stat/assets/images/booksplit.png +0 -0
  124. package/stat/assets/images/control_pause_icon.png +0 -0
  125. package/stat/assets/images/control_play_icon.png +0 -0
  126. package/stat/assets/images/embed_icon.png +0 -0
  127. package/stat/assets/images/icon-home-ia.png +0 -0
  128. package/stat/assets/images/icon_OL-logo-xs.png +0 -0
  129. package/stat/assets/images/icon_alert-xs.png +0 -0
  130. package/stat/assets/images/icon_book.svg +0 -12
  131. package/stat/assets/images/icon_bookmark.svg +0 -12
  132. package/stat/assets/images/icon_close-pop.png +0 -0
  133. package/stat/assets/images/icon_download.png +0 -0
  134. package/stat/assets/images/icon_gear.svg +0 -14
  135. package/stat/assets/images/icon_hamburger.svg +0 -20
  136. package/stat/assets/images/icon_home.png +0 -0
  137. package/stat/assets/images/icon_home.svg +0 -21
  138. package/stat/assets/images/icon_home_ia.png +0 -0
  139. package/stat/assets/images/icon_indicator.png +0 -0
  140. package/stat/assets/images/icon_info.svg +0 -11
  141. package/stat/assets/images/icon_one_page.svg +0 -8
  142. package/stat/assets/images/icon_pause.svg +0 -1
  143. package/stat/assets/images/icon_play.svg +0 -1
  144. package/stat/assets/images/icon_playback-rate.svg +0 -15
  145. package/stat/assets/images/icon_return.png +0 -0
  146. package/stat/assets/images/icon_search_button.svg +0 -8
  147. package/stat/assets/images/icon_share.svg +0 -9
  148. package/stat/assets/images/icon_skip-ahead.svg +0 -6
  149. package/stat/assets/images/icon_skip-back.svg +0 -13
  150. package/stat/assets/images/icon_speaker.svg +0 -18
  151. package/stat/assets/images/icon_speaker_open.svg +0 -10
  152. package/stat/assets/images/icon_thumbnails.svg +0 -12
  153. package/stat/assets/images/icon_toc.svg +0 -5
  154. package/stat/assets/images/icon_two_pages.svg +0 -9
  155. package/stat/assets/images/icon_zoomer.png +0 -0
  156. package/stat/assets/images/loading.gif +0 -0
  157. package/stat/assets/images/logo_icon.png +0 -0
  158. package/stat/assets/images/marker_chap-off.png +0 -0
  159. package/stat/assets/images/marker_chap-off.svg +0 -11
  160. package/stat/assets/images/marker_chap-off_ia.png +0 -0
  161. package/stat/assets/images/marker_chap-on.png +0 -0
  162. package/stat/assets/images/marker_chap-on.svg +0 -11
  163. package/stat/assets/images/marker_srch-on.svg +0 -11
  164. package/stat/assets/images/marker_srchchap-off.png +0 -0
  165. package/stat/assets/images/marker_srchchap-on.png +0 -0
  166. package/stat/assets/images/nav_control-dn.png +0 -0
  167. package/stat/assets/images/nav_control-dn_ia.png +0 -0
  168. package/stat/assets/images/nav_control-up.png +0 -0
  169. package/stat/assets/images/nav_control-up_ia.png +0 -0
  170. package/stat/assets/images/nav_control.png +0 -0
  171. package/stat/assets/images/one_page_mode_icon.png +0 -0
  172. package/stat/assets/images/paper-badge.png +0 -0
  173. package/stat/assets/images/print_icon.png +0 -0
  174. package/stat/assets/images/progressbar.gif +0 -0
  175. package/stat/assets/images/right_edges.png +0 -0
  176. package/stat/assets/images/slider.png +0 -0
  177. package/stat/assets/images/slider_ia.png +0 -0
  178. package/stat/assets/images/thumbnail_mode_icon.png +0 -0
  179. package/stat/assets/images/transparent.png +0 -0
  180. package/stat/assets/images/two_page_mode_icon.png +0 -0
  181. package/stat/assets/images/zoom_in_icon.png +0 -0
  182. package/stat/assets/images/zoom_out_icon.png +0 -0
  183. package/stat/css/BookReader.scss +0 -89
  184. package/stat/css/_BRBookmarks.scss +0 -29
  185. package/stat/css/_BRComponent.scss +0 -13
  186. package/stat/css/_BRfloat.scss +0 -197
  187. package/stat/css/_BRicon.scss +0 -48
  188. package/stat/css/_BRmain.scss +0 -251
  189. package/stat/css/_BRnav.scss +0 -359
  190. package/stat/css/_BRpages.scss +0 -139
  191. package/stat/css/_BRsearch.scss +0 -226
  192. package/stat/css/_BRtoolbar.scss +0 -84
  193. package/stat/css/_BRvendor.scss +0 -5
  194. package/stat/css/_MobileNav.scss +0 -194
  195. package/stat/css/_TextSelection.scss +0 -32
  196. package/stat/css/_colorbox.scss +0 -52
  197. package/stat/css/_controls.scss +0 -253
  198. package/stat/css/_icons.scss +0 -121
  199. package/stat/jquery-wrapper.js +0 -4
  200. package/stat/plugins/plugin.archive_analytics.js +0 -86
  201. package/stat/plugins/plugin.autoplay.js +0 -129
  202. package/stat/plugins/plugin.chapters.js +0 -248
  203. package/stat/plugins/plugin.iframe.js +0 -48
  204. package/stat/plugins/plugin.mobile_nav.js +0 -288
  205. package/stat/plugins/plugin.resume.js +0 -68
  206. package/stat/plugins/plugin.text_selection.js +0 -291
  207. package/stat/plugins/plugin.url.js +0 -198
  208. package/stat/plugins/plugin.vendor-fullscreen.js +0 -247
  209. package/stat/plugins/search/plugin.search.js +0 -439
  210. package/stat/plugins/search/view.js +0 -439
  211. package/stat/plugins/tts/AbstractTTSEngine.js +0 -249
  212. package/stat/plugins/tts/FestivalTTSEngine.js +0 -169
  213. package/stat/plugins/tts/PageChunk.js +0 -107
  214. package/stat/plugins/tts/PageChunkIterator.js +0 -163
  215. package/stat/plugins/tts/WebTTSEngine.js +0 -357
  216. package/stat/plugins/tts/plugin.tts.js +0 -357
  217. package/stat/plugins/tts/tooltip_dict.js +0 -15
  218. package/stat/plugins/tts/utils.js +0 -91
  219. package/stat/util/browserSniffing.js +0 -30
  220. package/stat/util/debouncer.js +0 -26
  221. package/stat/util/docCookies.js +0 -67
  222. package/stat/util/strings.js +0 -34
@@ -1,434 +0,0 @@
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
- }