diff --git a/ext/js/pages/settings/backup-controller.js b/ext/js/pages/settings/backup-controller.js index 933f0e2a..9b80f0b4 100644 --- a/ext/js/pages/settings/backup-controller.js +++ b/ext/js/pages/settings/backup-controller.js @@ -367,6 +367,9 @@ class BackupController { } } + // Update dictionaries + await DictionaryController.ensureDictionarySettings(this._settingsController, void 0, optionsFull, true, false); + // Assign options await this._settingsImportSetOptionsFull(optionsFull); } @@ -401,12 +404,7 @@ class BackupController { const optionsFull = this._optionsUtil.getDefault(); // Update dictionaries - const dictionaries = await this._settingsController.getDictionaryInfo(); - for (const {options: {dictionaries: optionsDictionaries}} of optionsFull.profiles) { - for (const {title} of dictionaries) { - optionsDictionaries[title] = DictionaryController.createDefaultDictionarySettings(false); - } - } + await DictionaryController.ensureDictionarySettings(this._settingsController, void 0, optionsFull, true, false); // Assign options try { diff --git a/ext/js/pages/settings/dictionary-controller.js b/ext/js/pages/settings/dictionary-controller.js index 65edcb67..7862b207 100644 --- a/ext/js/pages/settings/dictionary-controller.js +++ b/ext/js/pages/settings/dictionary-controller.js @@ -257,6 +257,36 @@ class DictionaryController { }; } + static async ensureDictionarySettings(settingsController, dictionaries, optionsFull, modifyOptionsFull, newDictionariesEnabled) { + if (typeof dictionaries === 'undefined') { + dictionaries = await settingsController.getDictionaryInfo(); + } + if (typeof optionsFull === 'undefined') { + optionsFull = await settingsController.getOptionsFull(); + } + + const targets = []; + const {profiles} = optionsFull; + for (const {title} of dictionaries) { + for (let i = 0, ii = profiles.length; i < ii; ++i) { + const {options: {dictionaries: dictionaryOptions}} = profiles[i]; + if (Object.prototype.hasOwnProperty.call(dictionaryOptions, title)) { continue; } + + const value = DictionaryController.createDefaultDictionarySettings(newDictionariesEnabled); + if (modifyOptionsFull) { + dictionaryOptions[title] = value; + } else { + const path = ObjectPropertyAccessor.getPathString(['profiles', i, 'options', 'dictionaries', title]); + targets.push({action: 'set', path, value}); + } + } + } + + if (!modifyOptionsFull && targets.length > 0) { + await settingsController.modifyGlobalSettings(targets); + } + } + // Private _onOptionsChanged({options}) { @@ -290,7 +320,7 @@ class DictionaryController { this._updateDictionariesEnabledWarnings(options); - await this._ensureDictionarySettings(dictionaries); + await DictionaryController.ensureDictionarySettings(this._settingsController, dictionaries, void 0, false, false); for (const dictionary of dictionaries) { this._createDictionaryEntry(dictionary); } @@ -531,29 +561,6 @@ class DictionaryController { await this._settingsController.modifyGlobalSettings(targets); } - async _ensureDictionarySettings(dictionaries2) { - const optionsFull = await this._settingsController.getOptionsFull(); - const {profiles} = optionsFull; - const targets = []; - for (const {title} of dictionaries2) { - for (let i = 0, ii = profiles.length; i < ii; ++i) { - const {options: {dictionaries: dictionaryOptions}} = profiles[i]; - if (Object.prototype.hasOwnProperty.call(dictionaryOptions, title)) { continue; } - - const path = ObjectPropertyAccessor.getPathString(['profiles', i, 'options', 'dictionaries', title]); - targets.push({ - action: 'set', - path, - value: DictionaryController.createDefaultDictionarySettings(false) - }); - } - } - - if (targets.length > 0) { - await this._settingsController.modifyGlobalSettings(targets); - } - } - _triggerStorageChanged() { yomichan.trigger('storageChanged'); }