@internetarchive/bookreader 5.0.0-111 → 5.0.0-112
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/BookReader/BookReader.js +1 -1
- package/BookReader/BookReader.js.map +1 -1
- package/BookReader/plugins/plugin.search.js.map +1 -1
- package/BookReader/plugins/plugin.text_selection.js.map +1 -1
- package/BookReader/plugins/plugin.translate.js +1 -1
- package/BookReader/plugins/plugin.translate.js.map +1 -1
- package/BookReader/plugins/plugin.tts.js.map +1 -1
- package/package.json +1 -1
- package/src/BookReader/Navbar/Navbar.js +9 -10
- package/src/plugins/translate/plugin.translate.js +68 -22
- package/src/plugins/tts/utils.js +1 -1
package/package.json
CHANGED
|
@@ -314,7 +314,7 @@ export class Navbar {
|
|
|
314
314
|
const $sliders = this.$root.find('.BRpager').slider({
|
|
315
315
|
animate: true,
|
|
316
316
|
min: 0,
|
|
317
|
-
max: br.book.getNumLeafs() - 1,
|
|
317
|
+
max: Math.max(0, br.book.getNumLeafs() - 1),
|
|
318
318
|
value: br.currentIndex(),
|
|
319
319
|
range: "min",
|
|
320
320
|
});
|
|
@@ -324,8 +324,8 @@ export class Navbar {
|
|
|
324
324
|
$sliders.find('.ui-slider-handle').attr({
|
|
325
325
|
'role': 'slider',
|
|
326
326
|
'aria-label': 'Page navigation slider',
|
|
327
|
-
'aria-valuemin':
|
|
328
|
-
'aria-valuemax': br.book.getNumLeafs(),
|
|
327
|
+
'aria-valuemin': 0,
|
|
328
|
+
'aria-valuemax': Math.max(0, br.book.getNumLeafs() - 1),
|
|
329
329
|
});
|
|
330
330
|
|
|
331
331
|
// Ignore up/down arrow keys and page up/down keys, since they're confusingly different
|
|
@@ -396,7 +396,6 @@ export class Navbar {
|
|
|
396
396
|
*/
|
|
397
397
|
getNavPageNumString(index) {
|
|
398
398
|
const { br } = this;
|
|
399
|
-
// Accessible index starts at 0 (alas) so we add 1 to make human
|
|
400
399
|
const pageNum = br.book.getPageNum(index);
|
|
401
400
|
const pageType = br.book.getPageProp(index, 'pageType');
|
|
402
401
|
const numLeafs = br.book.getNumLeafs();
|
|
@@ -425,7 +424,7 @@ export class Navbar {
|
|
|
425
424
|
updateNavPageNum(index) {
|
|
426
425
|
this.$root.find('.BRcurrentpage').html(this.getNavPageNumString(index));
|
|
427
426
|
this.$root.find('.ui-slider-handle').attr({
|
|
428
|
-
'aria-valuenow': index
|
|
427
|
+
'aria-valuenow': index,
|
|
429
428
|
'aria-valuetext': this.getNavPageNumString(index),
|
|
430
429
|
});
|
|
431
430
|
}
|
|
@@ -446,19 +445,19 @@ export class Navbar {
|
|
|
446
445
|
|
|
447
446
|
/**
|
|
448
447
|
* Renders the html for the page string
|
|
449
|
-
* @param {
|
|
450
|
-
* @param {number} numLeafs
|
|
448
|
+
* @param {PageIndex} index 0-indexed page index (0..numLeafs-1)
|
|
449
|
+
* @param {number} numLeafs total number of leaves
|
|
451
450
|
* @param {number|string} pageNum
|
|
452
451
|
* @param {*} pageType - Deprecated
|
|
453
452
|
* @param {number} maxPageNum
|
|
454
|
-
* @return {string}
|
|
453
|
+
* @return {string} e.g. "Page 14 (3/39)"
|
|
455
454
|
*/
|
|
456
455
|
export function getNavPageNumHtml(index, numLeafs, pageNum, pageType, maxPageNum) {
|
|
457
456
|
const pageIsAsserted = pageNum[0] != 'n';
|
|
458
|
-
const pageIndex = index + 1;
|
|
459
457
|
|
|
460
458
|
if (!pageIsAsserted) {
|
|
461
459
|
pageNum = '—';
|
|
462
460
|
}
|
|
463
|
-
|
|
461
|
+
const lastIndex = Math.max(0, numLeafs - 1);
|
|
462
|
+
return `Page ${pageNum} (${index}/${lastIndex})`;
|
|
464
463
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
// @ts-check
|
|
2
|
-
import { html, LitElement } from 'lit';
|
|
2
|
+
import { css, html, LitElement } from 'lit';
|
|
3
3
|
import { BookReaderPlugin } from '../../BookReaderPlugin.js';
|
|
4
|
-
import { customElement, property } from 'lit/decorators.js';
|
|
4
|
+
import { customElement, property, query } from 'lit/decorators.js';
|
|
5
5
|
import { TranslationManager } from "./TranslationManager.js";
|
|
6
|
-
import { toISO6391 } from '../tts/utils.js';
|
|
6
|
+
import { toISO6391, toNativeName } from '../tts/utils.js';
|
|
7
7
|
import { sortBy } from '../../../src/BookReader/utils.js';
|
|
8
8
|
import { TextSelectionManager } from '../../../src/util/TextSelectionManager.js';
|
|
9
9
|
import '@internetarchive/ia-activity-indicator';
|
|
@@ -63,14 +63,12 @@ export class TranslatePlugin extends BookReaderPlugin {
|
|
|
63
63
|
textSelectionManager = new TextSelectionManager('.BRtranslateLayer', this.br, {selectionElement: [".BRlineElement"]}, 1);
|
|
64
64
|
|
|
65
65
|
async init() {
|
|
66
|
-
|
|
66
|
+
if (!this.options.enabled) return;
|
|
67
|
+
|
|
68
|
+
const bookLanguage = this.br.options.bookLanguage || '';
|
|
69
|
+
const currentLanguage = toISO6391(bookLanguage);
|
|
67
70
|
this.langFromCode = currentLanguage ?? "en";
|
|
68
71
|
this.textSelectionManager.init();
|
|
69
|
-
|
|
70
|
-
if (!this.options.enabled) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
72
|
this.translationManager.publicPath = this.br.options.imagesBaseURL.replace(/\/+$/, '') + '/..';
|
|
75
73
|
|
|
76
74
|
/**
|
|
@@ -307,6 +305,14 @@ export class TranslatePlugin extends BookReaderPlugin {
|
|
|
307
305
|
}
|
|
308
306
|
|
|
309
307
|
handleToggleTranslation = async () => {
|
|
308
|
+
const shouldEnableTranslation = !this.userToggleTranslate;
|
|
309
|
+
if (shouldEnableTranslation && !this.isSourceLanguageSupported()) {
|
|
310
|
+
this.userToggleTranslate = false;
|
|
311
|
+
this.translationManager.active = false;
|
|
312
|
+
this._render();
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
|
|
310
316
|
this.userToggleTranslate = !this.userToggleTranslate;
|
|
311
317
|
this.translationManager.active = this.userToggleTranslate;
|
|
312
318
|
|
|
@@ -325,6 +331,10 @@ export class TranslatePlugin extends BookReaderPlugin {
|
|
|
325
331
|
/**
|
|
326
332
|
* Update translation side menu
|
|
327
333
|
*/
|
|
334
|
+
isSourceLanguageSupported() {
|
|
335
|
+
return this.translationManager.fromLanguages.some((lang) => lang.code == this.langFromCode);
|
|
336
|
+
}
|
|
337
|
+
|
|
328
338
|
_render() {
|
|
329
339
|
this.br.shell.menuProviders['translate'] = {
|
|
330
340
|
id: 'translate',
|
|
@@ -339,6 +349,7 @@ export class TranslatePlugin extends BookReaderPlugin {
|
|
|
339
349
|
this._panel.detectedToLang = this.langToCode;
|
|
340
350
|
this._panel.detectedFromLang = this.langFromCode;
|
|
341
351
|
this._panel.loadingModel = this.loadingModel;
|
|
352
|
+
this._panel.sourceLanguageSupported = this.isSourceLanguageSupported();
|
|
342
353
|
}
|
|
343
354
|
}"
|
|
344
355
|
@langFromChanged="${this.handleFromLangChange}"
|
|
@@ -351,6 +362,7 @@ export class TranslatePlugin extends BookReaderPlugin {
|
|
|
351
362
|
.detectedFromLang=${this.langFromCode}
|
|
352
363
|
.detectedToLang=${this.langToCode}
|
|
353
364
|
.loadingModel=${this.loadingModel}
|
|
365
|
+
.sourceLanguageSupported=${this.isSourceLanguageSupported()}
|
|
354
366
|
class="translate-panel"
|
|
355
367
|
/>`,
|
|
356
368
|
};
|
|
@@ -369,12 +381,27 @@ export class BrTranslatePanel extends LitElement {
|
|
|
369
381
|
@property({ type: String }) detectedFromLang = '';
|
|
370
382
|
@property({ type: String }) detectedToLang = '';
|
|
371
383
|
@property({ type: Boolean }) loadingModel;
|
|
384
|
+
@property({ type: Boolean }) sourceLanguageSupported = true;
|
|
385
|
+
|
|
386
|
+
@query('#lang-from') langFromDropdown;
|
|
387
|
+
@query('#lang-to') langToDropdown;
|
|
388
|
+
|
|
389
|
+
static styles = css`
|
|
390
|
+
.disclaimer {
|
|
391
|
+
padding: 10px;
|
|
392
|
+
border-radius: 8px;
|
|
393
|
+
font-size: 12px;
|
|
394
|
+
margin: 0 0 10px;
|
|
395
|
+
background-color: rgba(255, 255, 255, 0.1);
|
|
396
|
+
color: rgba(255, 255, 255, 0.9);
|
|
397
|
+
}
|
|
372
398
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
399
|
+
.disclaimer--warning {
|
|
400
|
+
margin: 10px 0;
|
|
401
|
+
background-color: rgba(255, 186, 8, 0.18);
|
|
402
|
+
color: #ffe7a3;
|
|
403
|
+
}
|
|
404
|
+
`;
|
|
378
405
|
|
|
379
406
|
connectedCallback() {
|
|
380
407
|
super.connectedCallback();
|
|
@@ -382,11 +409,12 @@ export class BrTranslatePanel extends LitElement {
|
|
|
382
409
|
}
|
|
383
410
|
|
|
384
411
|
render() {
|
|
412
|
+
const statusWarning = this._statusWarning();
|
|
413
|
+
|
|
385
414
|
return html`<div class="app" style="margin-top: 5%;padding-right: 5px;">
|
|
386
415
|
<div
|
|
387
416
|
class="disclaimer"
|
|
388
417
|
id="disclaimerMessage"
|
|
389
|
-
style="background-color: rgba(255,255,255,0.1);padding: 10px;border-radius: 8px;font-size: 12px;margin-bottom: 10px;color: rgba(255,255,255, 0.9);"
|
|
390
418
|
>${this.disclaimerMessage}</div>
|
|
391
419
|
|
|
392
420
|
<div class="panel panel--to" style="padding: 0 10px;">
|
|
@@ -405,7 +433,7 @@ export class BrTranslatePanel extends LitElement {
|
|
|
405
433
|
</div>
|
|
406
434
|
|
|
407
435
|
<div class="panel panel--start" style="text-align: right;padding: 0 10px;/*! font-size: 18px; */margin-top: 10px;">
|
|
408
|
-
|
|
436
|
+
<button class="start-translation-brn" @click="${this._toggleTranslation}" ?disabled=${!this.sourceLanguageSupported}>
|
|
409
437
|
${this.userTranslationActive ? "Stop Translating" : "Translate"}
|
|
410
438
|
</button>
|
|
411
439
|
</div>
|
|
@@ -426,9 +454,14 @@ export class BrTranslatePanel extends LitElement {
|
|
|
426
454
|
})
|
|
427
455
|
}
|
|
428
456
|
</select>
|
|
429
|
-
|
|
457
|
+
</details>
|
|
458
|
+
</div>
|
|
430
459
|
<div class="footer" id="status" style="margin-top:5%">
|
|
431
|
-
|
|
460
|
+
${
|
|
461
|
+
statusWarning ? html`
|
|
462
|
+
<div class="disclaimer disclaimer--warning">${statusWarning}</div>
|
|
463
|
+
` : ''
|
|
464
|
+
}
|
|
432
465
|
</div>
|
|
433
466
|
|
|
434
467
|
<div class="lang-models-loading">
|
|
@@ -469,17 +502,27 @@ export class BrTranslatePanel extends LitElement {
|
|
|
469
502
|
}
|
|
470
503
|
|
|
471
504
|
_getSelectedLang(type) {
|
|
472
|
-
|
|
473
|
-
const dropdown = this.querySelector(`#lang-${type}`);
|
|
505
|
+
const dropdown = type === 'from' ? this.langFromDropdown : this.langToDropdown;
|
|
474
506
|
return dropdown ? dropdown.value : '';
|
|
475
507
|
}
|
|
476
508
|
|
|
509
|
+
/**
|
|
510
|
+
* Resolves a human-readable language name for a language code.
|
|
511
|
+
* Prefers names from loaded translation language lists, then falls back to
|
|
512
|
+
* ISO language metadata via toNativeName.
|
|
513
|
+
* @param {string} code
|
|
514
|
+
* @returns {string}
|
|
515
|
+
*/
|
|
477
516
|
_getLangName(code) {
|
|
478
|
-
const lang = [...this.fromLanguages, ...this.toLanguages]
|
|
479
|
-
|
|
517
|
+
const lang = [...this.fromLanguages, ...this.toLanguages]
|
|
518
|
+
.find(lang => lang.code === code);
|
|
519
|
+
return lang?.name || toNativeName(code) || code;
|
|
480
520
|
}
|
|
481
521
|
|
|
482
522
|
_toggleTranslation(event) {
|
|
523
|
+
if (!this.sourceLanguageSupported) {
|
|
524
|
+
return;
|
|
525
|
+
}
|
|
483
526
|
const toggleTranslateEvent = new CustomEvent('toggleTranslation', {
|
|
484
527
|
detail: {value: event.target.value},
|
|
485
528
|
bubbles: true,
|
|
@@ -491,6 +534,9 @@ export class BrTranslatePanel extends LitElement {
|
|
|
491
534
|
|
|
492
535
|
// TODO: Hardcoded warning message for now but should add more statuses
|
|
493
536
|
_statusWarning() {
|
|
537
|
+
if (!this.sourceLanguageSupported) {
|
|
538
|
+
return `Translating from ${this._getLangName(this.detectedFromLang)} is not supported`;
|
|
539
|
+
}
|
|
494
540
|
if (this.detectedFromLang == this.detectedToLang) {
|
|
495
541
|
return "Translate To language is the same as the Source language";
|
|
496
542
|
}
|