@internetarchive/bookreader 5.0.0-96 → 5.0.0-98

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.
Files changed (47) hide show
  1. package/BookReader/474.js +2 -0
  2. package/BookReader/474.js.map +1 -0
  3. package/BookReader/BookReader.css +39 -34
  4. package/BookReader/BookReader.js +1 -1
  5. package/BookReader/BookReader.js.map +1 -1
  6. package/BookReader/bergamot-translator-worker.js +2966 -0
  7. package/BookReader/bergamot-translator-worker.wasm +0 -0
  8. package/BookReader/ia-bookreader-bundle.js +1 -1
  9. package/BookReader/ia-bookreader-bundle.js.map +1 -1
  10. package/BookReader/images/icon_experiment.svg +1 -0
  11. package/BookReader/images/translate.svg +1 -0
  12. package/BookReader/plugins/plugin.experiments.js +1 -1
  13. package/BookReader/plugins/plugin.experiments.js.map +1 -1
  14. package/BookReader/plugins/plugin.text_selection.js +1 -1
  15. package/BookReader/plugins/plugin.text_selection.js.map +1 -1
  16. package/BookReader/plugins/plugin.translate.js +3 -0
  17. package/BookReader/plugins/plugin.translate.js.LICENSE.txt +1 -0
  18. package/BookReader/plugins/plugin.translate.js.map +1 -0
  19. package/BookReader/plugins/plugin.tts.js +1 -1
  20. package/BookReader/plugins/plugin.tts.js.map +1 -1
  21. package/BookReader/plugins/translator-worker.js +2 -0
  22. package/BookReader/plugins/translator-worker.js.map +1 -0
  23. package/BookReader/silence.mp3 +0 -0
  24. package/BookReader/translator-worker.js +475 -0
  25. package/package.json +6 -3
  26. package/src/BookNavigator/book-navigator.js +1 -0
  27. package/src/BookReader/Mode1UpLit.js +6 -1
  28. package/src/BookReader/Mode2UpLit.js +11 -1
  29. package/src/BookReader/Navbar/Navbar.js +61 -0
  30. package/src/BookReader/options.js +12 -8
  31. package/src/BookReader.js +67 -140
  32. package/src/assets/images/icon_experiment.svg +1 -0
  33. package/src/assets/images/translate.svg +1 -0
  34. package/src/assets/silence.mp3 +0 -0
  35. package/src/css/_BRnav.scss +0 -24
  36. package/src/css/_BRsearch.scss +1 -5
  37. package/src/css/_TextSelection.scss +38 -9
  38. package/src/plugins/plugin.experiments.js +34 -9
  39. package/src/plugins/plugin.text_selection.js +17 -20
  40. package/src/plugins/translate/TranslationManager.js +170 -0
  41. package/src/plugins/translate/plugin.translate.js +489 -0
  42. package/src/plugins/tts/AbstractTTSEngine.js +3 -4
  43. package/src/plugins/tts/PageChunk.js +28 -9
  44. package/src/plugins/tts/WebTTSEngine.js +5 -7
  45. package/src/plugins/tts/plugin.tts.js +40 -4
  46. package/src/plugins/tts/utils.js +21 -22
  47. package/src/util/cache.js +20 -0
@@ -1,4 +1,4 @@
1
- import langs from 'iso-language-codes/js/data.js';
1
+ import langs from 'iso-language-codes';
2
2
 
3
3
  /**
4
4
  * Use regex to approximate word count in a string
@@ -19,49 +19,48 @@ export function isAndroid(userAgent = navigator.userAgent) {
19
19
  return /android/i.test(userAgent);
20
20
  }
21
21
 
22
+ /** @type {{[lang: string]: string}} */
23
+ // Handle odd one-off language pairs that might show up over time
24
+ const specialLangs = {
25
+ "zh-hans": "中文 (Zhōngwén)",
26
+ };
22
27
  /**
23
28
  * @typedef {string} ISO6391
24
29
  * Language code in ISO 639-1 format. e.g. en, fr, zh
30
+ * Can also retrieve language native name + ISO 639-1 code
25
31
  **/
26
32
 
27
- /** Each lang is an array, with each index mapping to a different property */
28
- const COLUMN_TO_LANG_INDEX = {
29
- 'Name': 0,
30
- 'Endonym': 1,
31
- 'ISO 639-1': 2,
32
- 'ISO 639-2/T': 3,
33
- 'ISO 639-2/B': 4,
34
- };
35
-
36
33
  /**
37
34
  * @param {string} language in some format
35
+ * @param {boolean?} getName gets Native Name + language code if true
38
36
  * @return {ISO6391?}
39
37
  */
40
- export function toISO6391(language) {
38
+ export function toISO6391(language, getName = false) {
41
39
  if (!language) return null;
42
40
  language = language.toLowerCase();
43
41
 
44
- return searchForISO6391(language, ['ISO 639-1']) ||
45
- searchForISO6391(language, ['ISO 639-2/B']) ||
46
- searchForISO6391(language, ['ISO 639-2/T', 'Endonym', 'Name']);
42
+ return searchForISO6391(language, ['iso639_1'], getName) ||
43
+ searchForISO6391(language, ['iso639_2T'], getName) ||
44
+ searchForISO6391(language, ['iso639_2B', 'nativeName', 'name'], getName);
47
45
  }
48
46
 
49
47
  /**
50
48
  * Searches for the given long in the given columns.
51
49
  * @param {string} language
52
- * @param {Array<keyof COLUMN_TO_LANG_INDEX>} columnsToSearch
50
+ * @param {Array<keyof import('iso-language-codes').Code>} columnsToSearch
51
+ * @param {boolean?} getName
53
52
  * @return {ISO6391?}
54
53
  */
55
- function searchForISO6391(language, columnsToSearch) {
56
- for (let i = 0; i < langs.length; i++) {
57
- for (let colI = 0; colI < columnsToSearch.length; colI++) {
58
- const column = columnsToSearch[colI];
59
- const columnValue = langs[i][COLUMN_TO_LANG_INDEX[column]];
60
- if (columnValue.split(', ').map(x => x.toLowerCase()).indexOf(language) != -1) {
61
- return langs[i][COLUMN_TO_LANG_INDEX['ISO 639-1']];
54
+ function searchForISO6391(language, columnsToSearch, getName) {
55
+ for (const lang of langs) {
56
+ for (const colName of columnsToSearch) {
57
+ if (lang[colName].split(', ').map(x => x.toLowerCase()).indexOf(language) != -1) {
58
+ if (getName) return `${lang.nativeName.split(", ")[0]} (${lang.iso639_1})`;
59
+ return lang.iso639_1;
62
60
  }
63
61
  }
64
62
  }
63
+ if (specialLangs[language]) return `${specialLangs[language]} (${language})`;
65
64
  return null;
66
65
  }
67
66
 
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @template T
3
+ */
4
+ export class Cache {
5
+ constructor(maxSize = 10) {
6
+ this.maxSize = maxSize;
7
+ /** @type {T[]} */
8
+ this.entries = [];
9
+ }
10
+
11
+ /**
12
+ * @param {T} entry
13
+ */
14
+ add(entry) {
15
+ if (this.entries.length >= this.maxSize) {
16
+ this.entries.shift();
17
+ }
18
+ this.entries.push(entry);
19
+ }
20
+ }