Skip to content

Commit

Permalink
Search term by exact reading when clicking on gloss link
Browse files Browse the repository at this point in the history
  • Loading branch information
khaitruong922 committed Oct 17, 2024
1 parent 4e64c5d commit c8a68f8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 15 deletions.
5 changes: 3 additions & 2 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ export class Backend {
/** @type {import('translator').FindTermsMode} */
const mode = 'simple';
const options = this._getProfileOptions(optionsContext, false);
const details = {matchType: /** @type {import('translation').FindTermsMatchType} */ ('exact'), deinflect: true};
const details = {matchType: /** @type {import('translation').FindTermsMatchType} */ ('exact'), deinflect: true, reading: null};
const findTermsOptions = this._getTranslatorFindTermsOptions(mode, details, options);
/** @type {import('api').ParseTextLine[]} */
const results = [];
Expand Down Expand Up @@ -2455,7 +2455,7 @@ export class Backend {
* @returns {import('translation').FindTermsOptions} An options object.
*/
_getTranslatorFindTermsOptions(mode, details, options) {
let {matchType, deinflect} = details;
let {matchType, deinflect, reading} = details;
if (typeof matchType !== 'string') { matchType = /** @type {import('translation').FindTermsMatchType} */ ('exact'); }
if (typeof deinflect !== 'boolean') { deinflect = true; }
const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options);
Expand Down Expand Up @@ -2484,6 +2484,7 @@ export class Backend {
return {
matchType,
deinflect,
reading,
mainDictionary,
sortFrequencyDictionary,
sortFrequencyDictionaryOrder,
Expand Down
13 changes: 8 additions & 5 deletions ext/js/display/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,14 +1265,15 @@ export class Display extends EventDispatcher {
/**
* @param {boolean} isKanji
* @param {string} source
* @param {string | null} reading
* @param {boolean} wildcardsEnabled
* @param {import('settings').OptionsContext} optionsContext
* @returns {Promise<import('dictionary').DictionaryEntry[]>}
*/
async _findDictionaryEntries(isKanji, source, wildcardsEnabled, optionsContext) {
async _findDictionaryEntries(isKanji, source, reading, wildcardsEnabled, optionsContext) {
/** @type {import('dictionary').DictionaryEntry[]} */
let dictionaryEntries = [];
const {findDetails, source: source2} = this._getFindDetails(source, wildcardsEnabled);
const {findDetails, source: source2} = this._getFindDetails(source, reading, wildcardsEnabled);
if (isKanji) {
dictionaryEntries = await this._application.api.kanjiFind(source, optionsContext);
if (dictionaryEntries.length > 0) { return dictionaryEntries; }
Expand All @@ -1289,12 +1290,13 @@ export class Display extends EventDispatcher {

/**
* @param {string} source
* @param {string | null} reading
* @param {boolean} wildcardsEnabled
* @returns {{findDetails: import('api').FindTermsDetails, source: string}}
*/
_getFindDetails(source, wildcardsEnabled) {
_getFindDetails(source, reading, wildcardsEnabled) {
/** @type {import('api').FindTermsDetails} */
const findDetails = {};
const findDetails = {reading};
if (wildcardsEnabled) {
const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(source);
if (match !== null) {
Expand All @@ -1319,6 +1321,7 @@ export class Display extends EventDispatcher {
async _setContentTermsOrKanji(type, urlSearchParams, token) {
const lookup = (urlSearchParams.get('lookup') !== 'false');
const wildcardsEnabled = (urlSearchParams.get('wildcards') !== 'off');
const reading = urlSearchParams.get('reading');
const hasEnabledDictionaries = this._options ? this._options.dictionaries.some(({enabled}) => enabled) : false;

// Set query
Expand Down Expand Up @@ -1358,7 +1361,7 @@ export class Display extends EventDispatcher {

let {dictionaryEntries} = content;
if (!Array.isArray(dictionaryEntries)) {
dictionaryEntries = hasEnabledDictionaries && lookup && query.length > 0 ? await this._findDictionaryEntries(type === 'kanji', query, wildcardsEnabled, optionsContext) : [];
dictionaryEntries = hasEnabledDictionaries && lookup && query.length > 0 ? await this._findDictionaryEntries(type === 'kanji', query, reading, wildcardsEnabled, optionsContext) : [];
if (this._setContentToken !== token) { return; }
content.dictionaryEntries = dictionaryEntries;
changeHistory = true;
Expand Down
29 changes: 25 additions & 4 deletions ext/js/display/structured-content-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,6 @@ export class StructuredContentGenerator {
_createLinkElement(content, dictionary, language) {
let {href} = content;
const internal = href.startsWith('?');
if (internal) {
href = `${location.protocol}//${location.host}/search.html${href.length > 1 ? href : ''}`;
}

const node = /** @type {HTMLAnchorElement} */ (this._createElement('a', 'gloss-link'));
node.dataset.external = `${!internal}`;

Expand All @@ -460,6 +456,31 @@ export class StructuredContentGenerator {
node.appendChild(icon);
}

if (internal) {
let hasFurigana = false;
let reading = '';
for (const childNode of text.childNodes) {
if (childNode instanceof HTMLElement) {
const nodeReading = childNode.querySelector('.gloss-sc-rt')?.textContent;
if (nodeReading && nodeReading.length > 0) {
reading += nodeReading;
hasFurigana = true;
}
} else {
reading += childNode.textContent ?? '';
}
}

let query = '';
if (href.length > 1) {
query = href;
if (reading.length > 0 && hasFurigana) {
query += `&reading=${reading}`;
}
}
href = `${location.protocol}//${location.host}/search.html${query}`;
}

this._contentManager.prepareLink(node, href, internal);
return node;
}
Expand Down
15 changes: 11 additions & 4 deletions ext/js/language/translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Translator {
* @returns {Promise<{dictionaryEntries: import('dictionary').TermDictionaryEntry[], originalTextLength: number}>} An object containing dictionary entries and the length of the original source text.
*/
async findTerms(mode, text, options) {
const {enabledDictionaryMap, excludeDictionaryDefinitions, sortFrequencyDictionary, sortFrequencyDictionaryOrder, language} = options;
const {reading, enabledDictionaryMap, excludeDictionaryDefinitions, sortFrequencyDictionary, sortFrequencyDictionaryOrder, language} = options;

Check failure on line 79 in ext/js/language/translator.js

View workflow job for this annotation

GitHub Actions / Static Analysis

'reading' is assigned a value but never used
const tagAggregator = new TranslatorTagAggregator();
let {dictionaryEntries, originalTextLength} = await this._findTermsInternal(text, options, tagAggregator);

Expand Down Expand Up @@ -220,7 +220,7 @@ export class Translator {
* @returns {Promise<{dictionaryEntries: import('translation-internal').TermDictionaryEntry[], originalTextLength: number}>}
*/
async _findTermsInternal(text, options, tagAggregator) {
const {removeNonJapaneseCharacters, enabledDictionaryMap} = options;
const {reading, removeNonJapaneseCharacters, enabledDictionaryMap} = options;
if (removeNonJapaneseCharacters && (['ja', 'zh', 'yue'].includes(options.language))) {
text = this._getJapaneseChineseOnlyText(text);
}
Expand All @@ -230,16 +230,17 @@ export class Translator {

const deinflections = await this._getDeinflections(text, options);

return this._getDictionaryEntries(deinflections, enabledDictionaryMap, tagAggregator);
return this._getDictionaryEntries(deinflections, enabledDictionaryMap, tagAggregator, reading);
}

/**
* @param {import('translation-internal').DatabaseDeinflection[]} deinflections
* @param {import('translation').TermEnabledDictionaryMap} enabledDictionaryMap
* @param {TranslatorTagAggregator} tagAggregator
* @param {string | null} reading
* @returns {{dictionaryEntries: import('translation-internal').TermDictionaryEntry[], originalTextLength: number}}
*/
_getDictionaryEntries(deinflections, enabledDictionaryMap, tagAggregator) {
_getDictionaryEntries(deinflections, enabledDictionaryMap, tagAggregator, reading) {
let originalTextLength = 0;
/** @type {import('translation-internal').TermDictionaryEntry[]} */
const dictionaryEntries = [];
Expand Down Expand Up @@ -268,6 +269,12 @@ export class Translator {
}
} else {
const dictionaryEntry = this._createTermDictionaryEntryFromDatabaseEntry(databaseEntry, originalText, transformedText, deinflectedText, textProcessorRuleChainCandidates, inflectionRuleChainCandidates, true, enabledDictionaryMap, tagAggregator);
if (reading) {
const headwordReadings = new Set(dictionaryEntry.headwords.map((headword) => headword.reading));
if (!headwordReadings.has(reading)) {
continue;
}
}
dictionaryEntries.push(dictionaryEntry);
ids.add(id);
}
Expand Down
1 change: 1 addition & 0 deletions types/ext/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import type {
export type FindTermsDetails = {
matchType?: Translation.FindTermsMatchType;
deinflect?: boolean;
reading: string | null;
};

export type ParseTextResultItem = {
Expand Down
4 changes: 4 additions & 0 deletions types/ext/translation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export type FindTermsOptions = {
* Whether or not deinflection should be performed.
*/
deinflect: boolean;
/**
* Exact reading of the term.
*/
reading: string | null;
/**
* The name of the primary dictionary to search.
*/
Expand Down

0 comments on commit c8a68f8

Please sign in to comment.