Update detection of Japanese characters

This commit is contained in:
toasted-nutbread 2019-12-22 18:26:27 -05:00
parent be2e6e0d93
commit 86d96a9036
3 changed files with 25 additions and 8 deletions

View File

@ -265,7 +265,7 @@ class DisplaySearch extends Display {
text !== this.clipboardPreviousText
) {
this.clipboardPreviousText = text;
if (jpIsJapaneseText(text)) {
if (jpIsAnyCharacterJapanese(text)) {
this.setQuery(this.isWanakanaEnabled() ? window.wanakana.toKana(text) : text);
window.history.pushState(null, '', `${window.location.pathname}?query=${encodeURIComponent(text)}`);
this.onSearchQueryUpdated(this.query.value, true);

View File

@ -214,11 +214,9 @@ class Translator {
}
async findTermsInternal(text, dictionaries, details, options) {
if (!options.scanning.alphanumeric && text.length > 0) {
const c = text[0];
if (!jpIsKana(c) && !jpIsKanji(c)) {
return [[], 0];
}
text = Translator.getSearchableText(text, options);
if (text.length === 0) {
return [[], 0];
}
const titles = Object.keys(dictionaries);
@ -587,4 +585,19 @@ class Translator {
yield variant;
}
}
static getSearchableText(text, options) {
if (!options.scanning.alphanumeric) {
const ii = text.length;
for (let i = 0; i < ii; ++i) {
const c = text[i];
if (!jpIsCharacterJapanese(c)) {
text = text.substring(0, i);
break;
}
}
}
return text;
}
}

View File

@ -92,9 +92,13 @@ function jpIsKana(c) {
);
}
function jpIsJapaneseText(text) {
function jpIsCharacterJapanese(c) {
return jpIsKanji(c) || jpIsKana(c);
}
function jpIsAnyCharacterJapanese(text) {
for (const c of text) {
if (jpIsKanji(c) || jpIsKana(c)) {
if (jpIsCharacterJapanese(c)) {
return true;
}
}