summaryrefslogtreecommitdiff
path: root/ext/bg/js/settings
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-02-08 17:53:07 -0500
committerGitHub <noreply@github.com>2021-02-08 17:53:07 -0500
commit7b28c2c5329b0bb0fa74872f160ec6b2974654d0 (patch)
tree217509f07517dd04211bf0c8999d78c09b56c5a9 /ext/bg/js/settings
parentbb8a521dff8ee314ab71bceb4bb2dcbe82b7a547 (diff)
Auto-fill Anki card fields when possible (#1355)
Diffstat (limited to 'ext/bg/js/settings')
-rw-r--r--ext/bg/js/settings/anki-controller.js60
1 files changed, 58 insertions, 2 deletions
diff --git a/ext/bg/js/settings/anki-controller.js b/ext/bg/js/settings/anki-controller.js
index a594fc8b..cb6922b8 100644
--- a/ext/bg/js/settings/anki-controller.js
+++ b/ext/bg/js/settings/anki-controller.js
@@ -52,6 +52,10 @@ class AnkiController {
this._validateFieldsToken = null;
}
+ get settingsController() {
+ return this._settingsController;
+ }
+
async prepare() {
this._ankiErrorContainer = document.querySelector('#anki-error');
this._ankiErrorMessageNode = document.querySelector('#anki-error-message');
@@ -606,9 +610,11 @@ class AnkiCardController {
if (this._model === value) { return; }
let fieldNames;
+ let options;
try {
this._modelChangingTo = value;
fieldNames = await this._ankiController.getModelFieldNames(value);
+ options = await this._ankiController.settingsController.getOptions();
} catch (e) {
// Revert
this._ankiCardModelSelect.value = this._model;
@@ -617,9 +623,14 @@ class AnkiCardController {
this._modelChangingTo = null;
}
+ const cardType = this._cardType;
+ const cardOptions = this._getCardOptions(options.anki, cardType);
+ const oldFields = cardOptions !== null ? cardOptions.fields : null;
+
const fields = {};
- for (const fieldName of fieldNames) {
- fields[fieldName] = '';
+ for (let i = 0, ii = fieldNames.length; i < ii; ++i) {
+ const fieldName = fieldNames[i];
+ fields[fieldName] = this._getDefaultFieldValue(fieldName, i, cardType, oldFields);
}
const targets = [
@@ -690,4 +701,49 @@ class AnkiCardController {
this._validateField(inputField, i);
}
}
+
+ _getDefaultFieldValue(fieldName, index, cardType, oldFields) {
+ if (
+ typeof oldFields === 'object' &&
+ oldFields !== null &&
+ Object.prototype.hasOwnProperty.call(oldFields, fieldName)
+ ) {
+ return oldFields[fieldName];
+ }
+
+ if (index === 0) {
+ return (cardType === 'kanji' ? '{character}' : '{expression}');
+ }
+
+ const markers = this._ankiController.getFieldMarkers(cardType);
+ const markerAliases = new Map([
+ ['glossary', ['definition', 'meaning']],
+ ['audio', ['sound']],
+ ['dictionary', ['dict']]
+ ]);
+
+ const hyphenPattern = /-/g;
+ for (const marker of markers) {
+ const names = [marker];
+ const aliases = markerAliases.get(marker);
+ if (typeof aliases !== 'undefined') {
+ names.push(...aliases);
+ }
+
+ let pattern = '^(?:';
+ for (let i = 0, ii = names.length; i < ii; ++i) {
+ const name = names[i];
+ if (i > 0) { pattern += '|'; }
+ pattern += name.replace(hyphenPattern, '[-_ ]*');
+ }
+ pattern += ')$';
+ pattern = new RegExp(pattern, 'i');
+
+ if (pattern.test(fieldName)) {
+ return `{${marker}}`;
+ }
+ }
+
+ return '';
+ }
}