Auto-fill Anki card fields when possible (#1355)

This commit is contained in:
toasted-nutbread 2021-02-08 17:53:07 -05:00 committed by GitHub
parent bb8a521dff
commit 7b28c2c532
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 '';
}
}