Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort entries by primary reading #1497

Merged
merged 15 commits into from
Oct 29, 2024
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
Loading