aboutsummaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-12-14 16:59:44 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2019-12-29 12:33:03 -0500
commit3033fea31e4bc9ba7198d2d31d6f6046813926d1 (patch)
tree4d6e212260ee9bca9f4807fe6d6fefbe9ed12f22 /ext
parent024f969bfd884e43cd40f408d0e3ba32839be45f (diff)
Treat null templates as the default value
Diffstat (limited to 'ext')
-rw-r--r--ext/bg/js/backend.js11
-rw-r--r--ext/bg/js/dictionary.js8
-rw-r--r--ext/bg/js/options.js2
-rw-r--r--ext/bg/js/settings/anki-templates.js4
-rw-r--r--ext/bg/js/settings/main.js10
-rw-r--r--ext/bg/js/util.js2
6 files changed, 27 insertions, 10 deletions
diff --git a/ext/bg/js/backend.js b/ext/bg/js/backend.js
index ca03f94d..1a874dc8 100644
--- a/ext/bg/js/backend.js
+++ b/ext/bg/js/backend.js
@@ -321,6 +321,7 @@ class Backend {
async _onApiDefinitionAdd({definition, mode, context, optionsContext}) {
const options = await this.getOptions(optionsContext);
+ const templates = Backend._getTemplates(options);
if (mode !== 'kanji') {
await audioInject(
@@ -339,19 +340,20 @@ class Backend {
);
}
- const note = await dictNoteFormat(definition, mode, options);
+ const note = await dictNoteFormat(definition, mode, options, templates);
return this.anki.addNote(note);
}
async _onApiDefinitionsAddable({definitions, modes, optionsContext}) {
const options = await this.getOptions(optionsContext);
+ const templates = Backend._getTemplates(options);
const states = [];
try {
const notes = [];
for (const definition of definitions) {
for (const mode of modes) {
- const note = await dictNoteFormat(definition, mode, options);
+ const note = await dictNoteFormat(definition, mode, options, templates);
notes.push(note);
}
}
@@ -672,6 +674,11 @@ class Backend {
return 'chrome';
}
}
+
+ static _getTemplates(options) {
+ const templates = options.anki.fieldTemplates;
+ return typeof templates === 'string' ? templates : profileOptionsGetDefaultFieldTemplates();
+ }
}
Backend._messageHandlers = new Map([
diff --git a/ext/bg/js/dictionary.js b/ext/bg/js/dictionary.js
index 0b35e32e..28705513 100644
--- a/ext/bg/js/dictionary.js
+++ b/ext/bg/js/dictionary.js
@@ -310,7 +310,7 @@ function dictFieldSplit(field) {
return field.length === 0 ? [] : field.split(' ');
}
-async function dictFieldFormat(field, definition, mode, options, exceptions) {
+async function dictFieldFormat(field, definition, mode, options, templates, exceptions) {
const data = {
marker: null,
definition,
@@ -329,7 +329,7 @@ async function dictFieldFormat(field, definition, mode, options, exceptions) {
}
data.marker = marker;
try {
- return await apiTemplateRender(options.anki.fieldTemplates, data, true);
+ return await apiTemplateRender(templates, data, true);
} catch (e) {
if (exceptions) { exceptions.push(e); }
return `{${marker}-render-error}`;
@@ -357,7 +357,7 @@ dictFieldFormat.markers = new Set([
'url'
]);
-async function dictNoteFormat(definition, mode, options) {
+async function dictNoteFormat(definition, mode, options, templates) {
const note = {fields: {}, tags: options.anki.tags};
let fields = [];
@@ -391,7 +391,7 @@ async function dictNoteFormat(definition, mode, options) {
}
for (const name in fields) {
- note.fields[name] = await dictFieldFormat(fields[name], definition, mode, options);
+ note.fields[name] = await dictFieldFormat(fields[name], definition, mode, options, templates);
}
return note;
diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js
index 2d13f6d9..7f540a70 100644
--- a/ext/bg/js/options.js
+++ b/ext/bg/js/options.js
@@ -326,7 +326,7 @@ function profileOptionsCreateDefaults() {
screenshot: {format: 'png', quality: 92},
terms: {deck: '', model: '', fields: {}},
kanji: {deck: '', model: '', fields: {}},
- fieldTemplates: profileOptionsGetDefaultFieldTemplates()
+ fieldTemplates: null
}
};
}
diff --git a/ext/bg/js/settings/anki-templates.js b/ext/bg/js/settings/anki-templates.js
index 9cdfc134..0e6e3cbd 100644
--- a/ext/bg/js/settings/anki-templates.js
+++ b/ext/bg/js/settings/anki-templates.js
@@ -73,7 +73,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);
diff --git a/ext/bg/js/settings/main.js b/ext/bg/js/settings/main.js
index 870769e5..c0b2deb6 100644
--- a/ext/bg/js/settings/main.js
+++ b/ext/bg/js/settings/main.js
@@ -145,7 +145,11 @@ async function formWrite(options) {
$('#interface-server').val(options.anki.server);
$('#screenshot-format').val(options.anki.screenshot.format);
$('#screenshot-quality').val(options.anki.screenshot.quality);
- $('#field-templates').val(options.anki.fieldTemplates);
+
+ let templates = options.anki.fieldTemplates;
+ if (typeof templates !== 'string') { templates = profileOptionsGetDefaultFieldTemplates(); }
+
+ $('#field-templates').val(templates);
onAnkiTemplatesValidateCompile();
await onAnkiOptionsChanged(options);
@@ -166,7 +170,9 @@ function formUpdateVisibility(options) {
if (options.general.debugInfo) {
const temp = utilIsolate(options);
- temp.anki.fieldTemplates = '...';
+ if (typeof temp.anki.fieldTemplates === 'string') {
+ temp.anki.fieldTemplates = '...';
+ }
const text = JSON.stringify(temp, null, 4);
$('#debug').text(text);
}
diff --git a/ext/bg/js/util.js b/ext/bg/js/util.js
index c88c2768..0527dc0b 100644
--- a/ext/bg/js/util.js
+++ b/ext/bg/js/util.js
@@ -88,6 +88,8 @@ function utilSetDifference(setA, setB) {
function utilStringHashCode(string) {
let hashCode = 0;
+ if (typeof string !== 'string') { return hashCode; }
+
for (let i = 0, charCode = string.charCodeAt(i); i < string.length; charCode = string.charCodeAt(++i)) {
hashCode = ((hashCode << 5) - hashCode) + charCode;
hashCode |= 0;