Backup update (#582)

* Add function to assign all settings

* Update how settings backups are restored

* Remove page reload

* Update profile index after importing
This commit is contained in:
toasted-nutbread 2020-05-30 16:23:56 -04:00 committed by GitHub
parent c8810bc929
commit 976a200ffc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 23 deletions

View File

@ -122,7 +122,8 @@ class Backend {
['logIndicatorClear', {async: false, contentScript: true, handler: this._onApiLogIndicatorClear.bind(this)}], ['logIndicatorClear', {async: false, contentScript: true, handler: this._onApiLogIndicatorClear.bind(this)}],
['createActionPort', {async: false, contentScript: true, handler: this._onApiCreateActionPort.bind(this)}], ['createActionPort', {async: false, contentScript: true, handler: this._onApiCreateActionPort.bind(this)}],
['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}], ['modifySettings', {async: true, contentScript: true, handler: this._onApiModifySettings.bind(this)}],
['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}] ['getSettings', {async: false, contentScript: true, handler: this._onApiGetSettings.bind(this)}],
['setAllSettings', {async: true, contentScript: false, handler: this._onApiSetAllSettings.bind(this)}]
]); ]);
this._messageHandlersWithProgress = new Map([ this._messageHandlersWithProgress = new Map([
['importDictionaryArchive', {async: true, contentScript: false, handler: this._onApiImportDictionaryArchive.bind(this)}], ['importDictionaryArchive', {async: true, contentScript: false, handler: this._onApiImportDictionaryArchive.bind(this)}],
@ -317,15 +318,6 @@ class Backend {
return useSchema ? JsonSchema.createProxy(options, this.optionsSchema) : options; return useSchema ? JsonSchema.createProxy(options, this.optionsSchema) : options;
} }
setFullOptions(options) {
try {
this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, utilIsolate(options));
} catch (e) {
// This shouldn't happen, but catch errors just in case of bugs
yomichan.logError(e);
}
}
getOptions(optionsContext, useSchema=false) { getOptions(optionsContext, useSchema=false) {
return this.getProfile(optionsContext, useSchema).options; return this.getProfile(optionsContext, useSchema).options;
} }
@ -860,6 +852,11 @@ class Backend {
return results; return results;
} }
async _onApiSetAllSettings({value, source}) {
this.options = JsonSchema.getValidValueOrDefault(this.optionsSchema, value);
await this._onApiOptionsSave({source});
}
// Command handlers // Command handlers
_createActionListenerPort(port, sender, handlers) { _createActionListenerPort(port, sender, handlers) {

View File

@ -141,7 +141,7 @@ class SettingsBackup {
// Importing // Importing
async _settingsImportSetOptionsFull(optionsFull) { async _settingsImportSetOptionsFull(optionsFull) {
await this._settingsController.setOptionsFull(optionsFull); await this._settingsController.setAllSettings(optionsFull);
} }
_showSettingsImportError(error) { _showSettingsImportError(error) {
@ -340,9 +340,6 @@ class SettingsBackup {
// Assign options // Assign options
await this._settingsImportSetOptionsFull(optionsFull); await this._settingsImportSetOptionsFull(optionsFull);
// Reload settings page
window.location.reload();
} }
_onSettingsImportClick() { _onSettingsImportClick() {
@ -376,8 +373,5 @@ class SettingsBackup {
// Assign options // Assign options
await this._settingsImportSetOptionsFull(optionsFull); await this._settingsImportSetOptionsFull(optionsFull);
// Reload settings page
window.location.reload();
} }
} }

View File

@ -38,9 +38,7 @@ class SettingsController extends EventDispatcher {
set profileIndex(value) { set profileIndex(value) {
if (this._profileIndex === value) { return; } if (this._profileIndex === value) { return; }
this._profileIndex = value; this._setProfileIndex(value);
this.trigger('optionsContextChanged');
this._onOptionsUpdatedInternal();
} }
prepare() { prepare() {
@ -69,9 +67,10 @@ class SettingsController extends EventDispatcher {
return utilBackend().getFullOptions(); return utilBackend().getFullOptions();
} }
async setOptionsFull(optionsFull) { async setAllSettings(value) {
utilBackend().setFullOptions(utilBackgroundIsolate(optionsFull)); const profileIndex = value.profileCurrent;
await this.save(); await api.setAllSettings(value, this._source);
this._setProfileIndex(profileIndex);
} }
async getGlobalSettings(targets) { async getGlobalSettings(targets) {
@ -104,6 +103,12 @@ class SettingsController extends EventDispatcher {
// Private // Private
_setProfileIndex(value) {
this._profileIndex = value;
this.trigger('optionsContextChanged');
this._onOptionsUpdatedInternal();
}
_onOptionsUpdated({source}) { _onOptionsUpdated({source}) {
if (source === this._source) { return; } if (source === this._source) { return; }
this._onOptionsUpdatedInternal(); this._onOptionsUpdatedInternal();

View File

@ -176,6 +176,10 @@ const api = (() => {
return this._invoke('getSettings', {targets}); return this._invoke('getSettings', {targets});
} }
setAllSettings(value, source) {
return this._invoke('setAllSettings', {value, source});
}
// Invoke functions with progress // Invoke functions with progress
importDictionaryArchive(archiveContent, details, onProgress) { importDictionaryArchive(archiveContent, details, onProgress) {