Update the functions for upgrading anki templates to be more reusable (#779)

This commit is contained in:
toasted-nutbread 2020-09-06 14:39:18 -04:00 committed by GitHub
parent 115afb63b9
commit 7a59ca2926
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -345,6 +345,47 @@ class OptionsUtil {
// Private // Private
static async _addFieldTemplatesToOptions(options, additionSourceUrl) {
let addition = null;
for (const {options: profileOptions} of options.profiles) {
const fieldTemplates = profileOptions.anki.fieldTemplates;
if (fieldTemplates !== null) {
if (addition === null) {
addition = await this._readFile(additionSourceUrl);
}
profileOptions.anki.fieldTemplates = this._addFieldTemplatesBeforeEnd(fieldTemplates, addition);
}
}
}
static async _addFieldTemplatesBeforeEnd(fieldTemplates, addition) {
const pattern = /[ \t]*\{\{~?>\s*\(\s*lookup\s*\.\s*"marker"\s*\)\s*~?\}\}/;
const newline = '\n';
let replaced = false;
fieldTemplates = fieldTemplates.replace(pattern, (g0) => {
replaced = true;
return `${addition}${newline}${g0}`;
});
if (!replaced) {
fieldTemplates += newline;
fieldTemplates += addition;
}
return fieldTemplates;
}
static async _readFile(url) {
url = chrome.runtime.getURL(url);
const response = await fetch(url, {
method: 'GET',
mode: 'no-cors',
cache: 'default',
credentials: 'omit',
redirect: 'follow',
referrerPolicy: 'no-referrer'
});
return await response.text();
}
static _getStringHashCode(string) { static _getStringHashCode(string) {
let hashCode = 0; let hashCode = 0;
@ -423,47 +464,10 @@ class OptionsUtil {
static async _updateVersion3(options) { static async _updateVersion3(options) {
// Version 3 changes: // Version 3 changes:
// Pitch accent Anki field templates added. // Pitch accent Anki field templates added.
let addition = null; await this._addFieldTemplatesToOptions(options, '/bg/data/anki-field-templates-upgrade-v2.handlebars');
for (const {options: profileOptions} of options.profiles) {
const fieldTemplates = profileOptions.anki.fieldTemplates;
if (fieldTemplates !== null) {
if (addition === null) {
addition = await this._updateVersion3GetAnkiFieldTemplates();
}
profileOptions.anki.fieldTemplates = this._addFieldTemplatesBeforeEnd(fieldTemplates, addition);
}
}
return options; return options;
} }
static async _updateVersion3GetAnkiFieldTemplates() {
const url = chrome.runtime.getURL('/bg/data/anki-field-templates-upgrade-v2.handlebars');
const response = await fetch(url, {
method: 'GET',
mode: 'no-cors',
cache: 'default',
credentials: 'omit',
redirect: 'follow',
referrerPolicy: 'no-referrer'
});
return await response.text();
}
static async _addFieldTemplatesBeforeEnd(fieldTemplates, addition) {
const pattern = /[ \t]*\{\{~?>\s*\(\s*lookup\s*\.\s*"marker"\s*\)\s*~?\}\}/;
const newline = '\n';
let replaced = false;
fieldTemplates = fieldTemplates.replace(pattern, (g0) => {
replaced = true;
return `${addition}${newline}${g0}`;
});
if (!replaced) {
fieldTemplates += newline;
fieldTemplates += addition;
}
return fieldTemplates;
}
static _updateVersion4(options) { static _updateVersion4(options) {
// Version 4 changes: // Version 4 changes:
// Options conditions converted to string representations. // Options conditions converted to string representations.