diff options
Diffstat (limited to 'ext/js/pages')
-rw-r--r-- | ext/js/pages/settings/anki-controller.js | 28 | ||||
-rw-r--r-- | ext/js/pages/settings/anki-templates-controller.js | 15 |
2 files changed, 32 insertions, 11 deletions
diff --git a/ext/js/pages/settings/anki-controller.js b/ext/js/pages/settings/anki-controller.js index d166d3f0..9640ed62 100644 --- a/ext/js/pages/settings/anki-controller.js +++ b/ext/js/pages/settings/anki-controller.js @@ -21,7 +21,7 @@ import {EventListenerCollection} from '../../core/event-listener-collection.js'; import {ExtensionError} from '../../core/extension-error.js'; import {log} from '../../core/log.js'; import {toError} from '../../core/to-error.js'; -import {getStandardFieldMarkers} from '../../data/anki-template-util.js'; +import {getDynamicFieldMarkers, getStandardFieldMarkers} from '../../data/anki-template-util.js'; import {stringContainsAnyFieldMarker} from '../../data/anki-util.js'; import {getRequiredPermissionsForAnkiFieldValue, hasPermissions, setPermissionsGranted} from '../../data/permissions-util.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; @@ -85,7 +85,6 @@ export class AnkiController { /** @type {HTMLElement} */ const ankiErrorLog = querySelectorNotNull(document, '#anki-error-log'); - this._setupFieldMenus(); this._ankiErrorMessageDetailsToggle.addEventListener('click', this._onAnkiErrorMessageDetailsToggleClick.bind(this), false); if (this._ankiEnableCheckbox !== null) { @@ -110,14 +109,14 @@ export class AnkiController { ankiApiKeyInput.addEventListener('focus', this._onApiKeyInputFocus.bind(this)); ankiApiKeyInput.addEventListener('blur', this._onApiKeyInputBlur.bind(this)); + await this._updateOptions(); + this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); + const onAnkiSettingChanged = () => { void this._updateOptions(); }; const nodes = [ankiApiKeyInput, ...document.querySelectorAll('[data-setting="anki.enable"]')]; for (const node of nodes) { node.addEventListener('settingChanged', onAnkiSettingChanged); } - - await this._updateOptions(); - this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); } /** @@ -161,7 +160,7 @@ export class AnkiController { /** * @param {import('settings-controller').EventArgument<'optionsChanged'>} details */ - _onOptionsChanged({options: {anki}}) { + _onOptionsChanged({options: {anki, dictionaries}}) { /** @type {?string} */ let apiKey = anki.apiKey; if (apiKey === '') { apiKey = null; } @@ -171,6 +170,8 @@ export class AnkiController { this._selectorObserver.disconnect(); this._selectorObserver.observe(document.documentElement, true); + + this._setupFieldMenus(dictionaries); } /** */ @@ -279,8 +280,10 @@ export class AnkiController { return cardController.isStale(); } - /** */ - _setupFieldMenus() { + /** + * @param {import('settings').DictionariesOptions} dictionaries + */ + _setupFieldMenus(dictionaries) { /** @type {[types: import('dictionary').DictionaryEntryType[], templateName: string][]} */ const fieldMenuTargets = [ [['term'], 'anki-card-terms-field-menu'], @@ -301,11 +304,18 @@ export class AnkiController { return; } + while (container.firstChild) { + container.removeChild(container.firstChild); + } + let markers = []; for (const type of types) { markers.push(...getStandardFieldMarkers(type)); } - markers = [...new Set(markers)]; + if (types.includes('term')) { + markers.push(...getDynamicFieldMarkers(dictionaries)); + } + markers = [...new Set(markers.sort())]; const fragment = document.createDocumentFragment(); for (const marker of markers) { diff --git a/ext/js/pages/settings/anki-templates-controller.js b/ext/js/pages/settings/anki-templates-controller.js index a0a6bd6c..dd96f69c 100644 --- a/ext/js/pages/settings/anki-templates-controller.js +++ b/ext/js/pages/settings/anki-templates-controller.js @@ -19,6 +19,7 @@ import {ExtensionError} from '../../core/extension-error.js'; import {toError} from '../../core/to-error.js'; import {AnkiNoteBuilder} from '../../data/anki-note-builder.js'; +import {getDynamicTemplates} from '../../data/anki-template-util.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; import {TemplateRendererProxy} from '../../templates/template-renderer-proxy.js'; @@ -244,8 +245,7 @@ export class AnkiTemplatesController { query: sentenceText, fullQuery: sentenceText }; - let template = options.anki.fieldTemplates; - if (typeof template !== 'string') { template = this._defaultFieldTemplates; } + const template = this._getAnkiTemplate(options); const {general: {resultOutputMode, glossaryLayoutMode, compactTags}} = options; const {note, errors} = await this._ankiNoteBuilder.createNote(/** @type {import('anki-note-builder').CreateNoteDetails} */ ({ dictionaryEntry, @@ -293,4 +293,15 @@ export class AnkiTemplatesController { /** @type {HTMLTextAreaElement} */ (this._fieldTemplatesTextarea).dataset.invalid = `${hasError}`; } } + + /** + * @param {import('settings').ProfileOptions} options + * @returns {string} + */ + _getAnkiTemplate(options) { + let staticTemplates = options.anki.fieldTemplates; + if (typeof staticTemplates !== 'string') { staticTemplates = this._defaultFieldTemplates; } + const dynamicTemplates = getDynamicTemplates(options); + return staticTemplates + '\n' + dynamicTemplates; + } } |