aboutsummaryrefslogtreecommitdiff
path: root/ext/js/display
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2024-02-25 11:20:44 -0500
committerGitHub <noreply@github.com>2024-02-25 16:20:44 +0000
commit2e9ea19207a7410f929bb908759d48cb2340f29c (patch)
treea6bde1297d693bb8d50e4c93a963aa3179e5a2ce /ext/js/display
parent73169f06dff767020718a5715eba97d3575ba7e1 (diff)
"isJapanese" check move (#730)
* Move isStringPartiallyJapanese out of ClipboardMonitor * Create isStringPartiallyJapanese function * Add textMayBeTranslatable * Rename API function * Rename internal function * Add helper * Update translatable check * Pass language to TextScanner * Pass language explicitly * Use textMayBeTranslatable * No redundant translatable check * Update eslint * Remove double newline * Collapse * Rename
Diffstat (limited to 'ext/js/display')
-rw-r--r--ext/js/display/display-generator.js32
-rw-r--r--ext/js/display/display.js2
-rw-r--r--ext/js/display/query-parser.js3
-rw-r--r--ext/js/display/sandbox/structured-content-generator.js9
-rw-r--r--ext/js/display/search-display-controller.js21
5 files changed, 49 insertions, 18 deletions
diff --git a/ext/js/display/display-generator.js b/ext/js/display/display-generator.js
index 22912e9f..0b3236e9 100644
--- a/ext/js/display/display-generator.js
+++ b/ext/js/display/display-generator.js
@@ -20,7 +20,8 @@ import {ExtensionError} from '../core/extension-error.js';
import {isObject} from '../core/utilities.js';
import {getDisambiguations, getGroupedPronunciations, getTermFrequency, groupKanjiFrequencies, groupTermFrequencies, groupTermTags, isNonNounVerbOrAdjective} from '../dictionary/dictionary-data-util.js';
import {HtmlTemplateCollection} from '../dom/html-template-collection.js';
-import {distributeFurigana, getKanaMorae, getPitchCategory, isCodePointKanji, isStringPartiallyJapanese} from '../language/ja/japanese.js';
+import {distributeFurigana, getKanaMorae, getPitchCategory, isCodePointKanji} from '../language/ja/japanese.js';
+import {getLanguageFromText} from '../language/text-utilities.js';
import {createPronunciationDownstepPosition, createPronunciationGraph, createPronunciationText} from './sandbox/pronunciation-generator.js';
import {StructuredContentGenerator} from './sandbox/structured-content-generator.js';
@@ -991,12 +992,7 @@ export class DisplayGenerator {
* @param {string} [language]
*/
_setTextContent(node, value, language) {
- if (typeof language === 'string') {
- node.lang = language;
- } else if (isStringPartiallyJapanese(value)) {
- node.lang = 'ja';
- }
-
+ this._setElementLanguage(node, language, value);
node.textContent = value;
}
@@ -1008,11 +1004,7 @@ export class DisplayGenerator {
_setMultilineTextContent(node, value, language) {
// This can't just call _setTextContent because the lack of <br> elements will
// cause the text to not copy correctly.
- if (typeof language === 'string') {
- node.lang = language;
- } else if (isStringPartiallyJapanese(value)) {
- node.lang = 'ja';
- }
+ this._setElementLanguage(node, language, value);
let start = 0;
while (true) {
@@ -1029,6 +1021,22 @@ export class DisplayGenerator {
}
/**
+ * @param {HTMLElement} element
+ * @param {string|undefined} language
+ * @param {string} content
+ */
+ _setElementLanguage(element, language, content) {
+ if (typeof language === 'string') {
+ element.lang = language;
+ } else {
+ const language2 = getLanguageFromText(content);
+ if (language2 !== null) {
+ element.lang = language2;
+ }
+ }
+ }
+
+ /**
* @param {string} reading
* @param {import('dictionary').TermPronunciation[]} termPronunciations
* @param {string[]} wordClasses
diff --git a/ext/js/display/display.js b/ext/js/display/display.js
index f6efb5ac..80f5e9ae 100644
--- a/ext/js/display/display.js
+++ b/ext/js/display/display.js
@@ -425,6 +425,7 @@ export class Display extends EventDispatcher {
readingMode: options.parsing.readingMode,
useInternalParser: options.parsing.enableScanningParser,
useMecabParser: options.parsing.enableMecabParser,
+ language: options.general.language,
scanning: {
inputs: scanningOptions.inputs,
deepContentScan: scanningOptions.deepDomScan,
@@ -1834,6 +1835,7 @@ export class Display extends EventDispatcher {
}
const {scanning: scanningOptions, sentenceParsing: sentenceParsingOptions} = options;
+ this._contentTextScanner.language = options.general.language;
this._contentTextScanner.setOptions({
inputs: [{
include: 'mouse0',
diff --git a/ext/js/display/query-parser.js b/ext/js/display/query-parser.js
index d27b9394..f6c26ce7 100644
--- a/ext/js/display/query-parser.js
+++ b/ext/js/display/query-parser.js
@@ -92,7 +92,7 @@ export class QueryParser extends EventDispatcher {
/**
* @param {import('display').QueryParserOptions} display
*/
- setOptions({selectedParser, termSpacing, readingMode, useInternalParser, useMecabParser, scanning}) {
+ setOptions({selectedParser, termSpacing, readingMode, useInternalParser, useMecabParser, language, scanning}) {
let selectedParserChanged = false;
if (selectedParser === null || typeof selectedParser === 'string') {
selectedParserChanged = (this._selectedParser !== selectedParser);
@@ -115,6 +115,7 @@ export class QueryParser extends EventDispatcher {
if (typeof scanLength === 'number') {
this._scanLength = scanLength;
}
+ this._textScanner.language = language;
this._textScanner.setOptions(scanning);
}
this._textScanner.setEnabled(true);
diff --git a/ext/js/display/sandbox/structured-content-generator.js b/ext/js/display/sandbox/structured-content-generator.js
index 1dfde39b..90a47158 100644
--- a/ext/js/display/sandbox/structured-content-generator.js
+++ b/ext/js/display/sandbox/structured-content-generator.js
@@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-import {isStringPartiallyJapanese} from '../../language/ja/japanese.js';
+import {getLanguageFromText} from '../../language/text-utilities.js';
export class StructuredContentGenerator {
/**
@@ -163,8 +163,11 @@ export class StructuredContentGenerator {
if (typeof content === 'string') {
if (content.length > 0) {
container.appendChild(this._createTextNode(content));
- if (language === null && isStringPartiallyJapanese(content)) {
- container.lang = 'ja';
+ if (language === null) {
+ const language2 = getLanguageFromText(content);
+ if (language2 !== null) {
+ container.lang = language2;
+ }
}
}
return;
diff --git a/ext/js/display/search-display-controller.js b/ext/js/display/search-display-controller.js
index e23d5d50..00f5efc6 100644
--- a/ext/js/display/search-display-controller.js
+++ b/ext/js/display/search-display-controller.js
@@ -103,7 +103,7 @@ export class SearchDisplayController {
this._searchBackButton.addEventListener('click', this._onSearchBackButtonClick.bind(this), false);
this._wanakanaEnableCheckbox.addEventListener('change', this._onWanakanaEnableChange.bind(this));
window.addEventListener('copy', this._onCopy.bind(this));
- this._clipboardMonitor.on('change', this._onExternalSearchUpdate.bind(this));
+ this._clipboardMonitor.on('change', this._onClipboardMonitorChange.bind(this));
this._clipboardMonitorEnableCheckbox.addEventListener('change', this._onClipboardMonitorEnableChange.bind(this));
this._display.hotkeyHandler.on('keydownNonHotkey', this._onKeyDown.bind(this));
@@ -271,9 +271,26 @@ export class SearchDisplayController {
}
/** @type {import('application').ApiHandler<'searchDisplayControllerUpdateSearchQuery'>} */
- _onExternalSearchUpdate({text, animate = true}) {
+ _onExternalSearchUpdate({text, animate}) {
+ void this._updateSearchFromClipboard(text, animate, false);
+ }
+
+ /**
+ * @param {import('clipboard-monitor').Events['change']} event
+ */
+ _onClipboardMonitorChange({text}) {
+ void this._updateSearchFromClipboard(text, true, true);
+ }
+
+ /**
+ * @param {string} text
+ * @param {boolean} animate
+ * @param {boolean} checkText
+ */
+ async _updateSearchFromClipboard(text, animate, checkText) {
const options = this._display.getOptions();
if (options === null) { return; }
+ if (checkText && !await this._display.application.api.isTextLookupWorthy(text, options.general.language)) { return; }
const {clipboard: {autoSearchContent, maximumSearchLength}} = options;
if (text.length > maximumSearchLength) {
text = text.substring(0, maximumSearchLength);