summaryrefslogtreecommitdiff
path: root/ext/bg/js/options.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-08-01 16:23:33 -0400
committerGitHub <noreply@github.com>2020-08-01 16:23:33 -0400
commit838fd211c6737ce7e2b6802a43837cf4300b60d2 (patch)
tree24fb7fd7d8e6c494a3e51defc7f32a6c3aa73107 /ext/bg/js/options.js
parent1e839cd230e53f822478f945cb415a8af2b09aef (diff)
Pitch accent Anki field templates (#701)
* Template helper updates * Add pitch data to exported field formatting data * Reuse note data * Add no-op * Set up pitch accent templates * Refactor version update functions * Implement upgrade process for new Anki templates * Consistency * Update README and anki.js to have matching markers
Diffstat (limited to 'ext/bg/js/options.js')
-rw-r--r--ext/bg/js/options.js94
1 files changed, 73 insertions, 21 deletions
diff --git a/ext/bg/js/options.js b/ext/bg/js/options.js
index ffea96f8..0d83f428 100644
--- a/ext/bg/js/options.js
+++ b/ext/bg/js/options.js
@@ -380,31 +380,83 @@ class OptionsUtil {
return [
{
async: false,
- update: (options) => {
- // Version 1 changes:
- // Added options.global.database.prefixWildcardsSupported = false
- options.global = {
- database: {
- prefixWildcardsSupported: false
- }
- };
- return options;
- }
+ update: this._updateVersion1.bind(this)
},
{
async: false,
- update: (options) => {
- // Version 2 changes:
- // Legacy profile update process moved into this upgrade function.
- for (const profile of options.profiles) {
- if (!Array.isArray(profile.conditionGroups)) {
- profile.conditionGroups = [];
- }
- profile.options = this._legacyProfileUpdateUpdateVersion(profile.options);
- }
- return options;
- }
+ update: this._updateVersion2.bind(this)
+ },
+ {
+ async: true,
+ update: this._updateVersion3.bind(this)
}
];
}
+
+ static _updateVersion1(options) {
+ // Version 1 changes:
+ // Added options.global.database.prefixWildcardsSupported = false.
+ options.global = {
+ database: {
+ prefixWildcardsSupported: false
+ }
+ };
+ return options;
+ }
+
+ static _updateVersion2(options) {
+ // Version 2 changes:
+ // Legacy profile update process moved into this upgrade function.
+ for (const profile of options.profiles) {
+ if (!Array.isArray(profile.conditionGroups)) {
+ profile.conditionGroups = [];
+ }
+ profile.options = this._legacyProfileUpdateUpdateVersion(profile.options);
+ }
+ return options;
+ }
+
+ 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);
+ }
+ }
+ 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;
+ }
}