Update AnkiTemplatesController (#580)

* Use this._defaultFieldTemplates

* Don't use mutable options

* Remove some use of jQuery
This commit is contained in:
toasted-nutbread 2020-05-30 16:22:05 -04:00 committed by GitHub
parent 395a0f4096
commit ad8df26b6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,10 +44,10 @@ class AnkiTemplatesController {
node.addEventListener('click', this._onMarkerClicked.bind(this), false); node.addEventListener('click', this._onMarkerClicked.bind(this), false);
} }
$('#field-templates').on('change', this._onChanged.bind(this)); document.querySelector('#field-templates').addEventListener('change', this._onChanged.bind(this), false);
$('#field-template-render').on('click', this._onRender.bind(this)); document.querySelector('#field-template-render').addEventListener('click', this._onRender.bind(this), false);
$('#field-templates-reset').on('click', this._onReset.bind(this)); document.querySelector('#field-templates-reset').addEventListener('click', this._onReset.bind(this), false);
$('#field-templates-reset-confirm').on('click', this._onResetConfirm.bind(this)); document.querySelector('#field-templates-reset-confirm').addEventListener('click', this._onResetConfirm.bind(this), false);
this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this)); this._settingsController.on('optionsChanged', this._onOptionsChanged.bind(this));
@ -60,7 +60,7 @@ class AnkiTemplatesController {
_onOptionsChanged({options}) { _onOptionsChanged({options}) {
let templates = options.anki.fieldTemplates; let templates = options.anki.fieldTemplates;
if (typeof templates !== 'string') { templates = this._defaultFieldTemplates; } if (typeof templates !== 'string') { templates = this._defaultFieldTemplates; }
$('#field-templates').val(templates); document.querySelector('#field-templates').value = templates;
this._onValidateCompile(); this._onValidateCompile();
} }
@ -70,12 +70,12 @@ class AnkiTemplatesController {
$('#field-template-reset-modal').modal('show'); $('#field-template-reset-modal').modal('show');
} }
async _onResetConfirm(e) { _onResetConfirm(e) {
e.preventDefault(); e.preventDefault();
$('#field-template-reset-modal').modal('hide'); $('#field-template-reset-modal').modal('hide');
const value = await api.getDefaultAnkiFieldTemplates(); const value = this._defaultFieldTemplates;
const element = document.querySelector('#field-templates'); const element = document.querySelector('#field-templates');
element.value = value; element.value = value;
@ -85,15 +85,13 @@ class AnkiTemplatesController {
async _onChanged(e) { async _onChanged(e) {
// Get value // Get value
let templates = e.currentTarget.value; let templates = e.currentTarget.value;
if (templates === await api.getDefaultAnkiFieldTemplates()) { if (templates === this._defaultFieldTemplates) {
// Default // Default
templates = null; templates = null;
} }
// Overwrite // Overwrite
const options = await this._settingsController.getOptionsMutable(); await this._settingsController.setProfileSetting('anki.fieldTemplates', templates);
options.anki.fieldTemplates = templates;
await this._settingsController.save();
// Compile // Compile
this._onValidateCompile(); this._onValidateCompile();
@ -144,7 +142,7 @@ class AnkiTemplatesController {
} }
}; };
let templates = options.anki.fieldTemplates; let templates = options.anki.fieldTemplates;
if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); } if (typeof templates !== 'string') { templates = this._defaultFieldTemplates; }
const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: api.templateRender.bind(api)}); const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: api.templateRender.bind(api)});
result = await ankiNoteBuilder.formatField(field, definition, mode, context, options, templates, exceptions); result = await ankiNoteBuilder.formatField(field, definition, mode, context, options, templates, exceptions);
} }