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

Only show dictionaries with at least one term for single-glossary handlebars #1811

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions ext/js/data/anki-template-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ export function getStandardFieldMarkers(type) {

/**
* @param {import('settings').ProfileOptions} options
* @param {import('dictionary-importer').Summary[]} dictionaryInfo
* @returns {string}
*/
export function getDynamicTemplates(options) {
export function getDynamicTemplates(options, dictionaryInfo) {
let dynamicTemplates = '\n';
for (const dictionary of options.dictionaries) {
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary.name);
if (!dictionary.enabled) { continue; }
const totalTerms = currentDictionaryInfo?.counts?.terms?.total;
if (!totalTerms || totalTerms === 0) { continue; }
dynamicTemplates += `
{{#*inline "single-glossary-${getKebabCase(dictionary.name)}"}}
{{~> glossary selectedDictionary='${escapeDictName(dictionary.name)}'}}
Expand All @@ -127,12 +131,16 @@ export function getDynamicTemplates(options) {

/**
* @param {import('settings').DictionariesOptions} dictionaries
* @param {import('dictionary-importer').Summary[]} dictionaryInfo
* @returns {string[]} The list of field markers.
*/
export function getDynamicFieldMarkers(dictionaries) {
export function getDynamicFieldMarkers(dictionaries, dictionaryInfo) {
const markers = [];
for (const dictionary of dictionaries) {
const currentDictionaryInfo = dictionaryInfo.find(({title}) => title === dictionary.name);
if (!dictionary.enabled) { continue; }
const totalTerms = currentDictionaryInfo?.counts?.terms?.total;
if (!totalTerms || totalTerms === 0) { continue; }
markers.push(`single-glossary-${getKebabCase(dictionary.name)}`);
}
return markers;
Expand Down
9 changes: 8 additions & 1 deletion ext/js/display/display-anki.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,8 +833,9 @@ export class DisplayAnki {
* @returns {Promise<string>}
*/
async _getAnkiFieldTemplates(options) {
const dictionaryInfo = await this._display.application.api.getDictionaryInfo();
const staticTemplates = await this._getStaticAnkiFieldTemplates(options);
const dynamicTemplates = getDynamicTemplates(options);
const dynamicTemplates = getDynamicTemplates(options, dictionaryInfo);
return staticTemplates + dynamicTemplates;
}

Expand Down Expand Up @@ -935,6 +936,12 @@ export class DisplayAnki {
if (context === null) { throw new Error('Note context not initialized'); }
const modeOptions = this._modeOptions.get(mode);
if (typeof modeOptions === 'undefined') { throw new Error(`Unsupported note type: ${mode}`); }
if (!this._ankiFieldTemplates) {
const options = this._display.getOptions();
if (options) {
await this._updateAnkiFieldTemplates(options);
}
}
const template = this._ankiFieldTemplates;
if (typeof template !== 'string') { throw new Error('Invalid template'); }
const {deck: deckName, model: modelName} = modeOptions;
Expand Down
12 changes: 8 additions & 4 deletions ext/js/pages/settings/anki-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ import {ObjectPropertyAccessor} from '../../general/object-property-accessor.js'
export class AnkiController {
/**
* @param {import('./settings-controller.js').SettingsController} settingsController
* @param {import('../../application.js').Application} application
*/
constructor(settingsController) {
constructor(settingsController, application) {
/** @type {import('../../application.js').Application} */
this._application = application;
/** @type {import('./settings-controller.js').SettingsController} */
this._settingsController = settingsController;
/** @type {AnkiConnect} */
Expand Down Expand Up @@ -181,7 +184,7 @@ export class AnkiController {

this._updateDuplicateOverwriteWarning(anki.duplicateBehavior);

this._setupFieldMenus(dictionaries);
void this._setupFieldMenus(dictionaries);
}

/** */
Expand Down Expand Up @@ -309,7 +312,7 @@ export class AnkiController {
/**
* @param {import('settings').DictionariesOptions} dictionaries
*/
_setupFieldMenus(dictionaries) {
async _setupFieldMenus(dictionaries) {
/** @type {[types: import('dictionary').DictionaryEntryType[], templateName: string][]} */
const fieldMenuTargets = [
[['term'], 'anki-card-terms-field-menu'],
Expand Down Expand Up @@ -339,7 +342,8 @@ export class AnkiController {
markers.push(...getStandardFieldMarkers(type));
}
if (types.includes('term')) {
markers.push(...getDynamicFieldMarkers(dictionaries));
const dictionaryInfo = await this._application.api.getDictionaryInfo();
markers.push(...getDynamicFieldMarkers(dictionaries, dictionaryInfo));
}
markers = [...new Set(markers.sort())];

Expand Down
9 changes: 5 additions & 4 deletions ext/js/pages/settings/anki-deck-generator-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export class AnkiDeckGeneratorController {
query: sentenceText,
fullQuery: sentenceText,
};
const template = this._getAnkiTemplate(options);
const template = await this._getAnkiTemplate(options);
const deckOptionsFields = options.anki.terms.fields;
const {general: {resultOutputMode, glossaryLayoutMode, compactTags}} = options;
const fields = [];
Expand Down Expand Up @@ -512,12 +512,13 @@ export class AnkiDeckGeneratorController {

/**
* @param {import('settings').ProfileOptions} options
* @returns {string}
* @returns {Promise<string>}
*/
_getAnkiTemplate(options) {
async _getAnkiTemplate(options) {
let staticTemplates = options.anki.fieldTemplates;
if (typeof staticTemplates !== 'string') { staticTemplates = this._defaultFieldTemplates; }
const dynamicTemplates = getDynamicTemplates(options);
const dictionaryInfo = await this._application.api.getDictionaryInfo();
const dynamicTemplates = getDynamicTemplates(options, dictionaryInfo);
return staticTemplates + '\n' + dynamicTemplates;
}

Expand Down
9 changes: 5 additions & 4 deletions ext/js/pages/settings/anki-templates-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export class AnkiTemplatesController {
query: sentenceText,
fullQuery: sentenceText,
};
const template = this._getAnkiTemplate(options);
const template = await this._getAnkiTemplate(options);
const {general: {resultOutputMode, glossaryLayoutMode, compactTags}} = options;
const {note, errors} = await this._ankiNoteBuilder.createNote(/** @type {import('anki-note-builder').CreateNoteDetails} */ ({
dictionaryEntry,
Expand Down Expand Up @@ -315,12 +315,13 @@ export class AnkiTemplatesController {

/**
* @param {import('settings').ProfileOptions} options
* @returns {string}
* @returns {Promise<string>}
*/
_getAnkiTemplate(options) {
async _getAnkiTemplate(options) {
let staticTemplates = options.anki.fieldTemplates;
if (typeof staticTemplates !== 'string') { staticTemplates = this._defaultFieldTemplates; }
const dynamicTemplates = getDynamicTemplates(options);
const dictionaryInfo = await this._application.api.getDictionaryInfo();
const dynamicTemplates = getDynamicTemplates(options, dictionaryInfo);
return staticTemplates + '\n' + dynamicTemplates;
}
}
2 changes: 1 addition & 1 deletion ext/js/pages/settings/settings-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ await Application.main(true, async (application) => {
const settingsBackup = new BackupController(settingsController, modalController);
preparePromises.push(settingsBackup.prepare());

const ankiController = new AnkiController(settingsController);
const ankiController = new AnkiController(settingsController, application);
preparePromises.push(ankiController.prepare());

const ankiDeckGeneratorController = new AnkiDeckGeneratorController(application, settingsController, modalController, ankiController);
Expand Down