summaryrefslogtreecommitdiff
path: root/ext/bg/js/settings/anki-templates.js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2020-01-04 11:54:54 -0800
committerAlex Yatskov <alex@foosoft.net>2020-01-04 11:54:54 -0800
commit2a12036ca305044291f1f4105d6a8d249848b210 (patch)
tree5cfd4a3d837bf99730233a805d72395c8c61fc07 /ext/bg/js/settings/anki-templates.js
parent9105cb5618cfdd14c2bc37cd22db2b360fe8cd52 (diff)
parent174b92366577b0a638003b15e2d73fdc91cd62c3 (diff)
Merge branch 'master' into testing
Diffstat (limited to 'ext/bg/js/settings/anki-templates.js')
-rw-r--r--ext/bg/js/settings/anki-templates.js40
1 files changed, 36 insertions, 4 deletions
diff --git a/ext/bg/js/settings/anki-templates.js b/ext/bg/js/settings/anki-templates.js
index 9cdfc134..5e74358f 100644
--- a/ext/bg/js/settings/anki-templates.js
+++ b/ext/bg/js/settings/anki-templates.js
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2019 Alex Yatskov <alex@foosoft.net>
+ * Copyright (C) 2019-2020 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* This program is free software: you can redistribute it and/or modify
@@ -13,7 +13,7 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
@@ -42,10 +42,22 @@ function ankiTemplatesInitialize() {
node.addEventListener('click', onAnkiTemplateMarkerClicked, false);
}
- $('#field-templates').on('change', (e) => onAnkiTemplatesValidateCompile(e));
+ $('#field-templates').on('change', (e) => onAnkiFieldTemplatesChanged(e));
$('#field-template-render').on('click', (e) => onAnkiTemplateRender(e));
$('#field-templates-reset').on('click', (e) => onAnkiFieldTemplatesReset(e));
$('#field-templates-reset-confirm').on('click', (e) => onAnkiFieldTemplatesResetConfirm(e));
+
+ ankiTemplatesUpdateValue();
+}
+
+async function ankiTemplatesUpdateValue() {
+ const optionsContext = getOptionsContext();
+ const options = await apiOptionsGet(optionsContext);
+ let templates = options.anki.fieldTemplates;
+ if (typeof templates !== 'string') { templates = profileOptionsGetDefaultFieldTemplates(); }
+ $('#field-templates').val(templates);
+
+ onAnkiTemplatesValidateCompile();
}
const ankiTemplatesValidateGetDefinition = (() => {
@@ -73,7 +85,9 @@ async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, i
const definition = await ankiTemplatesValidateGetDefinition(text, optionsContext);
if (definition !== null) {
const options = await apiOptionsGet(optionsContext);
- result = await dictFieldFormat(field, definition, mode, options, exceptions);
+ let templates = options.anki.fieldTemplates;
+ if (typeof templates !== 'string') { templates = profileOptionsGetDefaultFieldTemplates(); }
+ result = await dictFieldFormat(field, definition, mode, options, templates, exceptions);
}
} catch (e) {
exceptions.push(e);
@@ -89,6 +103,24 @@ async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, i
}
}
+async function onAnkiFieldTemplatesChanged(e) {
+ // Get value
+ let templates = e.currentTarget.value;
+ if (templates === profileOptionsGetDefaultFieldTemplates()) {
+ // Default
+ templates = null;
+ }
+
+ // Overwrite
+ const optionsContext = getOptionsContext();
+ const options = await getOptionsMutable(optionsContext);
+ options.anki.fieldTemplates = templates;
+ await settingsSaveOptions();
+
+ // Compile
+ onAnkiTemplatesValidateCompile();
+}
+
function onAnkiTemplatesValidateCompile() {
const infoNode = document.querySelector('#field-template-compile-result');
ankiTemplatesValidate(infoNode, '{expression}', 'term-kanji', false, true);