@internetarchive/bookreader 5.0.0-110 → 5.0.0-111

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/BookReader.js CHANGED
@@ -1970,10 +1970,25 @@ BookReader.prototype.queryStringFromParams = function(
1970
1970
  if (params.search && urlMode === 'history') {
1971
1971
  newParams.set('q', params.search);
1972
1972
  }
1973
+
1974
+ let textFragmentParam = '';
1975
+ // Need to pull out text separately to avoid the spaces becoming encoded as +, which
1976
+ // the browser seems not to handle with the text fragment
1977
+ if (newParams.get('text')) {
1978
+ newParams.delete('text');
1979
+ textFragmentParam = `text=${this.urlPlugin.retrieveTextFragment(currQueryString)}`;
1980
+ }
1981
+
1973
1982
  // https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/toString
1974
1983
  // Note: This method returns the query string without the question mark.
1975
- const result = newParams.toString();
1976
- return result ? '?' + result : '';
1984
+ let result = newParams.toString();
1985
+ if (textFragmentParam) {
1986
+ if (result) result += '&';
1987
+ result += textFragmentParam;
1988
+ }
1989
+ if (result) result = '?' + result;
1990
+
1991
+ return result;
1977
1992
  };
1978
1993
 
1979
1994
  /**
@@ -150,4 +150,63 @@
150
150
 
151
151
  .BRtranslateLayer .BRparagraphElement.BRtranslateHidden {
152
152
  display: none;
153
+ }
154
+
155
+ .br-select-menu__root {
156
+ display: none;
157
+ border-radius: 20px;
158
+ background-color: #333;
159
+ color-scheme: dark;
160
+ padding: 2px;
161
+ overflow: hidden;
162
+ transition: border-radius 0.2s;
163
+ }
164
+ .br-select-menu__root:hover {
165
+ border-radius: 8px;
166
+ }
167
+
168
+ .br-select-menu__root:hover .br-select-menu__option {
169
+ border-radius: 6px;
170
+ }
171
+
172
+ .br-select-menu__option {
173
+ --iconWidth: 15px;
174
+ --iconHeight: 15px;
175
+ --iconFillColor: currentColor;
176
+ background: transparent;
177
+ display: flex;
178
+ align-items: center;
179
+ border-radius: 20px;
180
+ font-family: inherit;
181
+ border: 0;
182
+ transition: background-color 0.2s, border-radius 0.2s;
183
+ cursor: pointer;
184
+ text-wrap: nowrap;
185
+ padding: 4px;
186
+ }
187
+
188
+ .br-select-menu__option:hover {
189
+ background-color: rgba(255, 255, 255, 0.1);
190
+ }
191
+
192
+ .br-select-menu__icon {
193
+ display: block;
194
+ flex-shrink: 0;
195
+ width: 17px;
196
+ height: 17px;
197
+ }
198
+
199
+ .br-select-menu__label {
200
+ width: 0px;
201
+ opacity: 0;
202
+ interpolate-size: allow-keywords;
203
+ transition: width 0.2s;
204
+ font-size: 12px;
205
+ }
206
+
207
+
208
+ .br-select-menu__root:hover .br-select-menu__label {
209
+ width: auto;
210
+ margin-left: 4px;
211
+ opacity: 1;
153
212
  }
@@ -6,6 +6,7 @@ import { styleMap } from 'lit/directives/style-map.js';
6
6
  import '@internetarchive/icon-toc/icon-toc.js';
7
7
  import { BookReaderPlugin } from "../BookReaderPlugin.js";
8
8
  import { applyVariables } from "../util/strings.js";
9
+ import { promisifyEvent } from "../BookReader/utils.js";
9
10
  /** @typedef {import('@/src/BookReader/BookModel.js').PageIndex} PageIndex */
10
11
  /** @typedef {import('@/src/BookReader/BookModel.js').PageString} PageString */
11
12
  /** @typedef {import('@/src/BookReader/BookModel.js').LeafNum} LeafNum */
@@ -94,11 +95,15 @@ export class ChaptersPlugin extends BookReaderPlugin {
94
95
  this._tocEntries = rawTableOfContents
95
96
  .map(rawTOCEntry => (Object.assign({}, rawTOCEntry, {
96
97
  pageIndex: (
97
- typeof(rawTOCEntry.leaf) == 'number' ? this.br.book.leafNumToIndex(rawTOCEntry.leaf) :
98
- rawTOCEntry.pagenum ? this.br.book.getPageIndex(rawTOCEntry.pagenum) :
99
- undefined
98
+ typeof rawTOCEntry.page_index === 'number' ? rawTOCEntry.page_index :
99
+ typeof(rawTOCEntry.leaf) == 'number' ? this.br.book.leafNumToIndex(rawTOCEntry.leaf) :
100
+ rawTOCEntry.pagenum ? this.br.book.getPageIndex(rawTOCEntry.pagenum) :
101
+ undefined
100
102
  ),
101
103
  })));
104
+ if (!this.br.shell) {
105
+ await promisifyEvent(window, 'BookReader:PostInit');
106
+ }
102
107
  this._render();
103
108
  this.br.bind(BookReader.eventNames.pageChanged, () => this._updateCurrent());
104
109
  }
@@ -51,11 +51,27 @@ export class ExperimentsPlugin extends BookReaderPlugin {
51
51
  localStorageKey: 'BrExperiments',
52
52
 
53
53
  /** The experiments that should be shown in the experiments panel */
54
- enabledExperiments: ['translate'],
54
+ enabledExperiments: ['translate', 'copyLinkToHighlight'],
55
55
  }
56
56
 
57
57
  /** @type {ExperimentModel[]} */
58
58
  allExperiments = [
59
+ new class extends ExperimentModel {
60
+ name = 'copyLinkToHighlight';
61
+ title = 'Copy to Selection URL';
62
+ description = 'Share text selection via URL';
63
+ learnMore = 'none';
64
+ icon = null;
65
+ enabled = false;
66
+ async enable ({ manual = false }) {
67
+ this.br.plugins.textSelection.enableSelectionMenu();
68
+ }
69
+ async disable() {
70
+ sleep(0).then(() => {
71
+ window.location.reload();
72
+ });
73
+ }
74
+ }(),
59
75
  new class extends ExperimentModel {
60
76
  name = 'translate';
61
77
  title = 'Translate Plugin';
@@ -123,7 +139,9 @@ export class ExperimentsPlugin extends BookReaderPlugin {
123
139
  for (const experiment of this.allExperiments) {
124
140
  // TODO: imagesBaseURL should be replaced with assetRoot everywhere
125
141
  experiment.assetRoot = this.br.options.imagesBaseURL.replace(/images\/$/, '');
126
- experiment.icon = experiment.buildAssetPath(experiment.icon);
142
+ if (experiment.icon) {
143
+ experiment.icon = experiment.buildAssetPath(experiment.icon);
144
+ }
127
145
  experiment.br = this.br;
128
146
  }
129
147
 
@@ -269,7 +287,7 @@ export class BrExperimentToggle extends LitElement {
269
287
  return html`
270
288
  <div class="experiment-card" style="margin-bottom: 10px;">
271
289
  <div style="display: flex; align-items: center; gap: 10px;">
272
- <img src="${this.icon}" style="width: 20px; height: 20px;" alt="" />
290
+ ${this.icon ? html`<img src="${this.icon}" style="width: 20px; height: 20px;" alt="" />` : ''}
273
291
  <div style="flex-grow: 1; font-weight: bold;">${this.title}</div>
274
292
  </div>
275
293
  <p style="opacity: 0.9">
@@ -53,10 +53,21 @@ export class TextSelectionPlugin extends BookReaderPlugin {
53
53
  init() {
54
54
  if (!this.options.enabled) return;
55
55
 
56
+ this.br.on('pageVisible', (_, {pageContainerEl}) => {
57
+ if (pageContainerEl.querySelector('.BRtextLayer')) {
58
+ this.br.trigger('textLayerVisible', {pageContainerEl});
59
+ }
60
+ });
61
+
56
62
  this.loadData();
57
63
  this.textSelectionManager.init();
58
64
  }
59
65
 
66
+ enableSelectionMenu() {
67
+ this.textSelectionManager.selectionMenuEnabled = true;
68
+ this.textSelectionManager.renderSelectionMenu();
69
+ }
70
+
60
71
  /**
61
72
  * @override
62
73
  * @param {PageContainer} pageContainer
@@ -188,6 +199,7 @@ export class TextSelectionPlugin extends BookReaderPlugin {
188
199
  paragEl.style.marginTop = `${newTop}px`;
189
200
  yAdded += newTop;
190
201
  textLayer.appendChild(paragEl);
202
+ textLayer.appendChild(document.createTextNode('\n'));
191
203
  }
192
204
  $container.append(textLayer);
193
205
  this.textSelectionManager.stopPageFlip($container);
@@ -195,6 +207,11 @@ export class TextSelectionPlugin extends BookReaderPlugin {
195
207
  pageIndex,
196
208
  pageContainer,
197
209
  });
210
+
211
+ // Check if page is visible
212
+ if ($container.hasClass('BRpage-visible')) {
213
+ this.br.trigger('textLayerVisible', {pageContainerEl: $container[0]});
214
+ }
198
215
  }
199
216
 
200
217
  /**
@@ -161,7 +161,7 @@ export class UrlPlugin {
161
161
  * If it was changeed, update the urlState
162
162
  */
163
163
  listenForHashChanges() {
164
- this.oldLocationHash = window.location.hash.substr(1);
164
+ this.oldLocationHash = this.getHash();
165
165
  if (this.urlLocationPollId) {
166
166
  clearInterval(this.urlLocationPollId);
167
167
  this.urlLocationPollId = null;
@@ -169,7 +169,7 @@ export class UrlPlugin {
169
169
 
170
170
  // check if the URL changes
171
171
  const updateHash = () => {
172
- const newFragment = window.location.hash.substr(1);
172
+ const newFragment = this.getHash();
173
173
  const hasFragmentChange = newFragment != this.oldLocationHash;
174
174
 
175
175
  if (!hasFragmentChange) { return; }
@@ -182,10 +182,30 @@ export class UrlPlugin {
182
182
  /**
183
183
  * Will read either the hash or URL and return the bookreader fragment
184
184
  */
185
- pullFromAddressBar (location = window.location) {
185
+ pullFromAddressBar(location = window.location) {
186
186
  const path = this.urlMode === 'history'
187
187
  ? (location.pathname.substr(this.urlHistoryBasePath.length) + location.search)
188
188
  : location.hash.substr(1);
189
189
  this.urlState = this.urlStringToUrlState(path);
190
190
  }
191
+
192
+ /**
193
+ * Get the hash out of the current URL. Also augments it with the text
194
+ * from the main part of the URL, since that is not readable by JS
195
+ * from the actual hash
196
+ * @returns
197
+ */
198
+ getHash() {
199
+ const text = this.retrieveTextFragment(window.location.search);
200
+ const textFragment = text ? `:~:text=${text[0]}` : '';
201
+ return `${window.location.hash.slice(1)}${textFragment}`;
202
+ }
203
+
204
+ /**
205
+ * @param {string} urlString
206
+ * @returns {string}
207
+ */
208
+ retrieveTextFragment(urlString) {
209
+ return urlString.match(/(?<=[&?]?text=)[^&]*/);
210
+ }
191
211
  }
@@ -1,6 +1,7 @@
1
1
  /* global BookReader */
2
2
 
3
3
  import { UrlPlugin } from "./UrlPlugin.js";
4
+ import { sleep } from "../../BookReader/utils.js";
4
5
 
5
6
  /**
6
7
  * Plugin for URL management in BookReader
@@ -25,7 +26,7 @@ jQuery.extend(BookReader.defaultOptions, {
25
26
  urlHistoryBasePath: '/',
26
27
 
27
28
  /** Only these params will be reflected onto the URL */
28
- urlTrackedParams: ['page', 'search', 'mode', 'region', 'highlight', 'view'],
29
+ urlTrackedParams: ['page', 'search', 'mode', 'region', 'highlight', 'view', 'text'],
29
30
 
30
31
  /** If true, don't update the URL when `page == n0 (eg "/page/n0")` */
31
32
  urlTrackIndex0: false,
@@ -42,6 +43,10 @@ BookReader.prototype.setup = (function(super_) {
42
43
  this.locationPollId = null;
43
44
  this.oldLocationHash = null;
44
45
  this.oldUserHash = null;
46
+ // Should include the :~:text= prefix
47
+ this.textFragment = null;
48
+ // Tracks the original textFragment page num when first loaded
49
+ this.textFragmentPage = null;
45
50
  };
46
51
  })(BookReader.prototype.setup);
47
52
 
@@ -140,11 +145,23 @@ BookReader.prototype.urlUpdateFragment = function() {
140
145
  return validParams;
141
146
  }, {});
142
147
 
148
+ // eg 'page/3/mode/2up'; no query params (in hash mode, it might have /search/term)
149
+ // Does NOT have the :~:text fragment
143
150
  const newFragment = this.fragmentFromParams(params, this.options.urlMode);
151
+ const newFragmentWithSlash = newFragment === '' ? '' : `/${newFragment}`;
152
+ // eg 'page/3/mode/2up'; no query params
153
+ // WILL CONTAIN the :~:text fragment in hash mode (!)
144
154
  const currFragment = this.urlReadFragment();
155
+ // This should have both ?q=foo&text=bar (and any other params) as an encoded string
145
156
  const currQueryString = this.getLocationSearch();
157
+ // Eg ?q=foo&text=bar; only query params, no fragment
146
158
  const newQueryString = this.queryStringFromParams(params, currQueryString, this.options.urlMode);
147
- if (currFragment === newFragment && currQueryString === newQueryString) {
159
+
160
+ // NOTE: If ?text is in the URL, we will fire fragment change events on every render; which is
161
+ // not desireable, but currently don't have a way to handle re-writing ?text to the hash text
162
+ // fragment form, :~:text=foo.
163
+ const hasTextParam = this.urlPlugin.retrieveTextFragment(currQueryString);
164
+ if (currFragment === newFragment && currQueryString === newQueryString && !hasTextParam) {
148
165
  return;
149
166
  }
150
167
 
@@ -153,12 +170,19 @@ BookReader.prototype.urlUpdateFragment = function() {
153
170
  this.options.urlMode = 'hash';
154
171
  } else {
155
172
  const baseWithoutSlash = this.options.urlHistoryBasePath.replace(/\/+$/, '');
156
- const newFragmentWithSlash = newFragment === '' ? '' : `/${newFragment}`;
157
-
173
+ const textFragment = this.urlPlugin.retrieveTextFragment(newQueryString);
158
174
  const newUrlPath = `${baseWithoutSlash}${newFragmentWithSlash}${newQueryString}`;
175
+ const extractedPage = this.urlPlugin.urlStringToUrlState(newFragmentWithSlash)?.page;
176
+ if (!this.textFragmentPage && textFragment) {
177
+ this.textFragmentPage = extractedPage ? extractedPage : null;
178
+ this.textFragment = `:~:text=${textFragment}`;
179
+ }
159
180
  try {
160
181
  window.history.replaceState({}, null, newUrlPath);
161
182
  this.oldLocationHash = newFragment + newQueryString;
183
+ if (textFragment) {
184
+ this.oldLocationHash += `:~:text=${textFragment[0]}`;
185
+ }
162
186
  } catch (e) {
163
187
  // DOMException on Chrome when in sandboxed iframe
164
188
  this.options.urlMode = 'hash';
@@ -168,8 +192,22 @@ BookReader.prototype.urlUpdateFragment = function() {
168
192
 
169
193
  if (this.options.urlMode === 'hash') {
170
194
  const newQueryStringSearch = this.urlParamsFiltersOnlySearch(this.readQueryString());
171
- window.location.replace('#' + newFragment + newQueryStringSearch);
172
- this.oldLocationHash = newFragment + newQueryStringSearch;
195
+ let textFragment = this.urlPlugin.retrieveTextFragment(this.readQueryString());
196
+ const extractedPage = this.urlPlugin.urlStringToUrlState(newFragmentWithSlash)?.page;
197
+
198
+ if (textFragment) {
199
+ textFragment = `:~:text=${textFragment[0]}`;
200
+ } else {
201
+ textFragment = '';
202
+ }
203
+ if (!this.textFragmentPage && textFragment) {
204
+ this.textFragmentPage = extractedPage ? extractedPage : null;
205
+ this.textFragment = textFragment;
206
+ } else if (this.textFragmentPage && extractedPage != this.textFragmentPage) {
207
+ textFragment = '';
208
+ }
209
+ window.location.replace('#' + newFragment + newQueryStringSearch + textFragment);
210
+ this.oldLocationHash = newFragment + newQueryStringSearch + textFragment;
173
211
  }
174
212
  };
175
213
 
@@ -195,7 +233,7 @@ BookReader.prototype.urlReadFragment = function() {
195
233
  if (urlMode === 'history') {
196
234
  return window.location.pathname.substr(urlHistoryBasePath.length);
197
235
  } else {
198
- return window.location.hash.substr(1);
236
+ return this.urlPlugin.getHash();
199
237
  }
200
238
  };
201
239
 
@@ -210,6 +248,24 @@ export class BookreaderUrlPlugin extends BookReader {
210
248
  init() {
211
249
  if (this.options.enableUrlPlugin) {
212
250
  this.urlPlugin = new UrlPlugin(this.options);
251
+ const location = this.getLocationSearch();
252
+ if (location.includes("text=")) {
253
+ this.on('textLayerVisible', async (_, {pageContainerEl}) => {
254
+ const visiblePageNum = pageContainerEl.getAttribute('data-page-num');
255
+
256
+ // Hack: More time mode 1up page "settle down" from user scrolling
257
+ await sleep(this.mode === 1 ? 900 : 100);
258
+
259
+ // No textFragment found or the textFragment stored doesn't match current visible page loaded
260
+ if (!this.textFragment || this.textFragmentPage !== visiblePageNum) return;
261
+ if (this.options.urlMode === 'history') {
262
+ window.location.replace(`#${this.textFragment}`);
263
+ } else {
264
+ // for urlMode hash, textFragment is stored in oldLocationHash already
265
+ window.location.replace(`#${this.oldLocationHash}`);
266
+ }
267
+ });
268
+ }
213
269
  this.bind(BookReader.eventNames.PostInit, () => {
214
270
  const { urlMode } = this.options;
215
271