summaryrefslogtreecommitdiff
path: root/ext/js
diff options
context:
space:
mode:
authorStefanVukovic99 <stefanvukovic44@gmail.com>2023-12-29 04:02:51 +0100
committerGitHub <noreply@github.com>2023-12-29 03:02:51 +0000
commit580983b9b8a17965db1466aa08cad3c96b06022e (patch)
tree914760e326ffc4b1acf8ddcddbd5006ae762babf /ext/js
parent8d5d2152e4295fdcefa6ef283204c92df1f81305 (diff)
search resolution option (#436)
* add search resolution setting * move finding next substring to method * use regex literal * fix comments * fix comments * add options update function * update test
Diffstat (limited to 'ext/js')
-rw-r--r--ext/js/background/backend.js4
-rw-r--r--ext/js/data/options-util.js15
-rw-r--r--ext/js/language/translator.js22
3 files changed, 37 insertions, 4 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js
index ae78a97b..68d4e0c8 100644
--- a/ext/js/background/backend.js
+++ b/ext/js/background/backend.js
@@ -2417,7 +2417,8 @@ export class Backend {
convertHiraganaToKatakana,
convertKatakanaToHiragana,
collapseEmphaticSequences,
- textReplacements: textReplacementsOptions
+ textReplacements: textReplacementsOptions,
+ searchResolution
}
} = options;
const textReplacements = this._getTranslatorTextReplacements(textReplacementsOptions);
@@ -2444,6 +2445,7 @@ export class Backend {
convertHiraganaToKatakana,
convertKatakanaToHiragana,
collapseEmphaticSequences,
+ searchResolution,
textReplacements,
enabledDictionaryMap,
excludeDictionaryDefinitions
diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js
index c219bed3..eb9af9b6 100644
--- a/ext/js/data/options-util.js
+++ b/ext/js/data/options-util.js
@@ -555,7 +555,8 @@ export class OptionsUtil {
{async: false, update: this._updateVersion18.bind(this)},
{async: false, update: this._updateVersion19.bind(this)},
{async: false, update: this._updateVersion20.bind(this)},
- {async: true, update: this._updateVersion21.bind(this)}
+ {async: true, update: this._updateVersion21.bind(this)},
+ {async: false, update: this._updateVersion22.bind(this)}
];
/* eslint-enable no-multi-spaces */
if (typeof targetVersion === 'number' && targetVersion < result.length) {
@@ -1174,6 +1175,18 @@ export class OptionsUtil {
}
/**
+ * @type {import('options-util').ModernUpdateFunctionAsync}
+ */
+ _updateVersion22(options) {
+ // Added translation.searchResolution
+ for (const {options: profileOptions} of options.profiles) {
+ profileOptions.translation.searchResolution = 'letter';
+ }
+
+ return options;
+ }
+
+ /**
* @param {string} url
* @returns {Promise<chrome.tabs.Tab>}
*/
diff --git a/ext/js/language/translator.js b/ext/js/language/translator.js
index 733955c2..36ed8b43 100644
--- a/ext/js/language/translator.js
+++ b/ext/js/language/translator.js
@@ -329,8 +329,12 @@ export class Translator {
text2 = jp.collapseEmphaticSequences(text2, collapseEmphaticFull, sourceMap);
}
- for (let i = text2.length; i > 0; --i) {
- const source = text2.substring(0, i);
+ for (
+ let source = text2, i = text2.length;
+ i > 0;
+ i = this._getNextSubstringLength(options.searchResolution, i, source)
+ ) {
+ source = text2.substring(0, i);
if (used.has(source)) { break; }
used.add(source);
const rawSource = sourceMap.source.substring(0, sourceMap.getSourceLength(i));
@@ -343,6 +347,20 @@ export class Translator {
}
/**
+ * @param {string} searchResolution
+ * @param {number} currentLength
+ * @param {string} source
+ * @returns {number}
+ */
+ _getNextSubstringLength(searchResolution, currentLength, source) {
+ if (searchResolution === 'word') {
+ return source.search(/[^\p{Letter}][\p{Letter}\p{Number}]*$/u);
+ } else {
+ return currentLength - 1;
+ }
+ }
+
+ /**
* @param {string} text
* @param {TextSourceMap} sourceMap
* @param {import('translation').FindTermsTextReplacement[]} replacements