Skip to content

Commit 9972dc5

Browse files
authored
Merge pull request #1399 from cdrini/feature/hypothesis
Extend BR to work correctly with Hypothesis
2 parents 41c7f68 + 9b683aa commit 9972dc5

File tree

21 files changed

+444
-39
lines changed

21 files changed

+444
-39
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ node_modules/
44
coverage*/
55
.env
66

7+
# Eventually ignore this directory entirely
8+
BookReader/hypothesis
9+
710
.vscode

BookReaderDemo/IADemoBr.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ const initializeBookReader = (brManifest) => {
5757
// urlTrackedParams: ['page', 'search', 'mode'],
5858
/* End url plugin */
5959
enableBookTitleLink: false,
60+
bookUri: `https://archive.org/details/${ocaid}`,
6061
bookUrlText: null,
6162
startFullscreen: openFullImmersionTheater,
6263
// leaving this option commented out bc we change given user agent on archive.org

BookReaderDemo/demo-internetarchive.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<script src="../BookReader/plugins/plugin.resume.js"></script>
3131
<script src="../BookReader/plugins/plugin.archive_analytics.js"></script>
3232
<script src="../BookReader/plugins/plugin.text_selection.js"></script>
33+
<script src="../BookReader/plugins/plugin.experiments.js"></script>
3334

3435
<script type="module" src="../BookReader/ia-bookreader-bundle.js"></script>
3536

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"eslint-plugin-no-jquery": "^2.7.0",
6060
"eslint-plugin-testcafe": "^0.2.1",
6161
"http-server": "14.1.1",
62+
"hypothesis": "^1.1627.0",
6263
"interactjs": "^1.10.18",
6364
"iso-language-codes": "1.1.0",
6465
"jest": "29.7.0",
@@ -100,9 +101,11 @@
100101
"preversion": "npm run test && node scripts/preversion.js",
101102
"version": "node scripts/version.js",
102103
"postversion": "node scripts/postversion.js",
103-
"build": "npm run clean && npx concurrently --group npm:build-js npm:build-css npm:build-assets",
104+
"build": "npm run clean && npx concurrently --group npm:build-js npm:build-css npm:build-assets npm:build-hypothesis",
104105
"build-assets": "npx cpx \"src/assets/**/*\" BookReader && npx svgo -f BookReader/icons && npx svgo -f BookReader/images",
105106
"build-assets:watch": "npx cpx --watch --verbose \"src/assets/**/*\" BookReader",
107+
"build-hypothesis": "npx cpx \"node_modules/hypothesis/**/*\" BookReader/hypothesis",
108+
"build-hypothesis:watch": "npx cpx --watch --verbose \"node_modules/hypothesis/**/*\" BookReader/hypothesis",
106109
"build-js": "npx webpack",
107110
"build-js:watch": "npx webpack --mode=development --watch",
108111
"build-css": "npx sass --no-source-map ./src/css/BookReader.scss ./BookReader/BookReader.css",

src/BookNavigator/book-navigator.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import BookmarksProvider from './bookmarks/bookmarks-provider.js';
1010
import SharingProvider from './sharing.js';
1111
import ViewableFilesProvider from './viewable-files.js';
1212
import iaLogo from './assets/ia-logo.js';
13+
import { sortBy } from '../BookReader/utils.js';
1314
/** @typedef {import('@/src/BookReader.js').default} BookReader */
1415

1516
const events = {
@@ -51,7 +52,20 @@ export class BookNavigator extends LitElement {
5152
this.isAdmin = false;
5253
this.lendingInitialized = false;
5354
this.lendingStatus = {};
54-
this.menuProviders = {};
55+
this.menuProviders = {
56+
/** @type {BookmarksProvider} */
57+
bookmarks: null,
58+
/** @type {SearchProvider} */
59+
search: null,
60+
/** @type {DownloadProvider} */
61+
downloads: null,
62+
/** @type {VisualAdjustmentProvider} */
63+
visualAdjustments: null,
64+
/** @type {SharingProvider} */
65+
share: null,
66+
/** @type {ViewableFilesProvider} */
67+
volumes: null,
68+
};
5569
this.menuShortcuts = [];
5670
this.signedIn = false;
5771
/** @type {ModalManager} */
@@ -73,6 +87,10 @@ export class BookNavigator extends LitElement {
7387
'chapters',
7488
'search',
7589
'bookmarks',
90+
'downloads',
91+
'visualAdjustments',
92+
'share',
93+
'experiments',
7694
];
7795
}
7896

@@ -313,14 +331,20 @@ export class BookNavigator extends LitElement {
313331
* Sets order of menu and emits custom event when done
314332
*/
315333
updateMenuContents() {
316-
const {
317-
search, downloads, visualAdjustments, share, bookmarks, volumes, chapters,
318-
} = this.menuProviders;
319-
const availableMenus = [volumes, chapters, search, bookmarks, visualAdjustments, share].filter((menu) => !!menu);
334+
const availableMenus = sortBy(
335+
Object.entries(this.menuProviders)
336+
.filter(([id, menu]) => !!menu)
337+
.filter(([id, menu]) => {
338+
return id === 'downloads' ? this.shouldShowDownloadsMenu() : true;
339+
}),
340+
([id, menu]) => {
341+
const index = this.shortcutOrder.indexOf(id);
342+
return index === -1 ? this.shortcutOrder.length : index;
343+
},
344+
).map(([id, menu]) => menu);
320345

321346
if (this.shouldShowDownloadsMenu()) {
322-
downloads?.update(this.downloadableTypes);
323-
availableMenus.splice(-2, 0, downloads);
347+
this.menuProviders.downloads?.update(this.downloadableTypes);
324348
}
325349

326350
const event = new CustomEvent(
@@ -387,11 +411,13 @@ export class BookNavigator extends LitElement {
387411
* the id in each iteration over the shortcutOrder array.
388412
*/
389413
sortMenuShortcuts() {
390-
this.menuShortcuts = this.shortcutOrder.reduce((shortcuts, id) => {
391-
const menu = this.menuShortcuts.find((m) => m.id === id);
392-
if (menu) { shortcuts.push(menu); }
393-
return shortcuts;
394-
}, []);
414+
this.menuShortcuts = sortBy(
415+
this.menuShortcuts,
416+
(shortcut) => {
417+
const index = this.shortcutOrder.indexOf(shortcut.id);
418+
return index === -1 ? this.shortcutOrder.length : index;
419+
},
420+
);
395421
}
396422

397423
emitMenuShortcutsUpdated() {

src/BookNavigator/visual-adjustments/visual-adjustments-provider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default class VisualAdjustmentsProvider {
4545
this.icon = html`<ia-icon-visual-adjustment style="width: var(--iconWidth); height: var(--iconHeight);"></ia-icon-visual-adjustment>`;
4646
this.label = 'Visual Adjustments';
4747
this.menuDetails = this.updateOptionsCount();
48-
this.id = 'adjustment';
48+
this.id = 'visualAdjustments';
4949
this.component = html`
5050
<ia-book-visual-adjustments
5151
.options=${visualAdjustmentOptions}

src/BookReader.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,14 +702,28 @@ BookReader.prototype.trigger = function(name, props = this) {
702702
$(document).trigger(eventName, props);
703703
};
704704

705-
BookReader.prototype.bind = function(name, callback) {
705+
BookReader.prototype.on = function(name, callback) {
706706
$(document).on('BookReader:' + name, callback);
707707
};
708708

709-
BookReader.prototype.unbind = function(name, callback) {
709+
BookReader.prototype.off = function(name, callback) {
710710
$(document).off('BookReader:' + name, callback);
711711
};
712712

713+
/**
714+
* @deprecated Use .on and .off instead
715+
*/
716+
BookReader.prototype.bind = function(name, callback) {
717+
return this.on(name, callback);
718+
};
719+
720+
/**
721+
* @deprecated Use .on and .off instead
722+
*/
723+
BookReader.prototype.unbind = function(name, callback) {
724+
return this.off(name, callback);
725+
};
726+
713727
/**
714728
* Resizes based on the container width and height
715729
*/
@@ -1105,7 +1119,7 @@ BookReader.prototype.getPrevReadMode = function(mode) {
11051119

11061120
/**
11071121
* Switches the mode (eg 1up 2up thumb)
1108-
* @param {number}
1122+
* @param {number|'1up' | '2up' | 'thumb'}
11091123
* @param {object} [options]
11101124
* @param {boolean} [options.suppressFragmentChange = false]
11111125
* @param {boolean} [options.onInit = false] - this
@@ -1118,6 +1132,18 @@ BookReader.prototype.switchMode = function(
11181132
pageFound = false,
11191133
} = {},
11201134
) {
1135+
if (typeof mode === 'string') {
1136+
mode = {
1137+
'1up': this.constMode1up,
1138+
'2up': this.constMode2up,
1139+
'thumb': this.constModeThumb,
1140+
}[mode];
1141+
}
1142+
1143+
if (!mode) {
1144+
throw new Error(`Invalid mode: ${mode}`);
1145+
}
1146+
11211147
// Skip checks before init() complete
11221148
if (this.init.initComplete) {
11231149
if (mode === this.mode) {
@@ -1324,7 +1350,7 @@ BookReader.prototype.updateFirstIndex = function(
13241350
// If there's an initial search we stop suppressing global URL changes
13251351
// when local suppression ends
13261352
// This seems to correctly handle multiple calls during mode/1up
1327-
if (this.plugins.search.options.initialSearchTerm && !suppressFragmentChange) {
1353+
if (this.plugins.search?.options.initialSearchTerm && !suppressFragmentChange) {
13281354
this.suppressFragmentChange = false;
13291355
}
13301356

src/BookReader/BookModel.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,15 @@ export class PageModel {
461461
return this.book.getPageURI(this.index, reduce, rotate);
462462
}
463463

464+
/**
465+
* Return the asserted page number for this page, or, if one is not
466+
* present, the n-prefixed page index.
467+
* @returns {PageNumString}
468+
*/
469+
getPageNum() {
470+
return this.book.getPageNum(this.index);
471+
}
472+
464473
/**
465474
* Returns the srcset with correct URIs or void string if out of range
466475
* @param {number} reduce
@@ -539,12 +548,13 @@ export class PageModel {
539548
// There are a few main ways we can reference a specific page in a book:
540549
/**
541550
* @typedef {string} PageNumString
542-
* Possible values: /^n?\d+$/. Example: 'n7', '18'
543-
* Not necessarily unique
551+
* The way page numbers are usually displayed in the UI. Either
552+
* an asserted page number if the book has one (e.g. '18', or 'A-1'),
553+
* or the n-prefixed 0-based index (e.g. 'n7')
544554
*/
545555
/**
546556
* @typedef {number} LeafNum
547-
* No clue if 0 or 1 indexed or consecutive; generally from IA book info.
557+
* Internal number to IA scans. Can be 0 or 1 indexed. Unclear it it's consecutive.
548558
*/
549559
/**
550560
* @typedef {string} PageString

src/BookReader/Mode1Up.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { DragScrollable } from './DragScrollable.js';
66
/** @typedef {import('./BookModel.js').PageIndex} PageIndex */
77

88
export class Mode1Up {
9+
name = '1up'
10+
911
/**
1012
* @param {BookReader} br
1113
* @param {BookModel} bookModel

0 commit comments

Comments
 (0)