Update the functions for upgrading anki templates to be more reusable (#779)
This commit is contained in:
parent
115afb63b9
commit
7a59ca2926
@ -345,6 +345,47 @@ class OptionsUtil {
|
||||
|
||||
// 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) {
|
||||
let hashCode = 0;
|
||||
|
||||
@ -423,47 +464,10 @@ class OptionsUtil {
|
||||
static async _updateVersion3(options) {
|
||||
// Version 3 changes:
|
||||
// Pitch accent Anki field templates added.
|
||||
let addition = null;
|
||||
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);
|
||||
}
|
||||
}
|
||||
await this._addFieldTemplatesToOptions(options, '/bg/data/anki-field-templates-upgrade-v2.handlebars');
|
||||
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) {
|
||||
// Version 4 changes:
|
||||
// Options conditions converted to string representations.
|
||||
|
Loading…
Reference in New Issue
Block a user