Skip to content

Commit

Permalink
Sort entries by primary reading (yomidevs#1497)
Browse files Browse the repository at this point in the history
* Search term by exact reading when clicking on gloss link

* lint

* update typing

* type

* sort by reading match

* fix lint and test

* add test

* fix typing

* rename

* refactor

* refactor

* rename to primary reading

* remove extract reading implementation
  • Loading branch information
khaitruong922 authored Oct 29, 2024
1 parent e3f279c commit 0fd7009
Show file tree
Hide file tree
Showing 14 changed files with 1,188 additions and 35 deletions.
8 changes: 6 additions & 2 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,9 @@ 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};

/** @type {import('api').FindTermsDetails} */
const details = {matchType: 'exact', deinflect: true};
const findTermsOptions = this._getTranslatorFindTermsOptions(mode, details, options);
/** @type {import('api').ParseTextLine[]} */
const results = [];
Expand Down Expand Up @@ -2455,9 +2457,10 @@ export class Backend {
* @returns {import('translation').FindTermsOptions} An options object.
*/
_getTranslatorFindTermsOptions(mode, details, options) {
let {matchType, deinflect} = details;
let {matchType, deinflect, primaryReading} = details;
if (typeof matchType !== 'string') { matchType = /** @type {import('translation').FindTermsMatchType} */ ('exact'); }
if (typeof deinflect !== 'boolean') { deinflect = true; }
if (typeof primaryReading !== 'string') { primaryReading = ''; }
const enabledDictionaryMap = this._getTranslatorEnabledDictionaryMap(options);
const {
general: {mainDictionary, sortFrequencyDictionary, sortFrequencyDictionaryOrder, language},
Expand All @@ -2484,6 +2487,7 @@ export class Backend {
return {
matchType,
deinflect,
primaryReading,
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} primaryReading
* @param {boolean} wildcardsEnabled
* @param {import('settings').OptionsContext} optionsContext
* @returns {Promise<import('dictionary').DictionaryEntry[]>}
*/
async _findDictionaryEntries(isKanji, source, wildcardsEnabled, optionsContext) {
async _findDictionaryEntries(isKanji, source, primaryReading, wildcardsEnabled, optionsContext) {
/** @type {import('dictionary').DictionaryEntry[]} */
let dictionaryEntries = [];
const {findDetails, source: source2} = this._getFindDetails(source, wildcardsEnabled);
const {findDetails, source: source2} = this._getFindDetails(source, primaryReading, 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} primaryReading
* @param {boolean} wildcardsEnabled
* @returns {{findDetails: import('api').FindTermsDetails, source: string}}
*/
_getFindDetails(source, wildcardsEnabled) {
_getFindDetails(source, primaryReading, wildcardsEnabled) {
/** @type {import('api').FindTermsDetails} */
const findDetails = {};
const findDetails = {primaryReading};
if (wildcardsEnabled) {
const match = /^([*\uff0a]*)([\w\W]*?)([*\uff0a]*)$/.exec(source);
if (match !== null) {
Expand Down Expand Up @@ -1327,6 +1329,7 @@ export class Display extends EventDispatcher {
if (query === null) { query = ''; }
let queryFull = urlSearchParams.get('full');
queryFull = (queryFull !== null ? queryFull : query);
const primaryReading = urlSearchParams.get('primary_reading') ?? '';
const queryOffsetString = urlSearchParams.get('offset');
let queryOffset = 0;
if (queryOffsetString !== null) {
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, primaryReading, wildcardsEnabled, optionsContext) : [];
if (this._setContentToken !== token) { return; }
content.dictionaryEntries = dictionaryEntries;
changeHistory = true;
Expand Down
Loading

0 comments on commit 0fd7009

Please sign in to comment.