Backend options update (#1274)

* Update how _onCommandToggleTextScanning changes settings

* Make getFullOptions/getOptions private

* Remove unused isPrepared
This commit is contained in:
toasted-nutbread 2021-01-18 17:25:49 -05:00 committed by GitHub
parent e72152bf61
commit 85c723b85f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -158,6 +158,8 @@ class Backend {
return this._prepareCompletePromise; return this._prepareCompletePromise;
} }
// Private
_prepareInternalSync() { _prepareInternalSync() {
if (isObject(chrome.commands) && isObject(chrome.commands.onCommand)) { if (isObject(chrome.commands) && isObject(chrome.commands.onCommand)) {
const onCommand = this._onWebExtensionEventWrapper(this._onCommand.bind(this)); const onCommand = this._onWebExtensionEventWrapper(this._onCommand.bind(this));
@ -208,7 +210,7 @@ class Backend {
this._applyOptions('background'); this._applyOptions('background');
const options = this.getOptions({current: true}); const options = this._getProfileOptions({current: true});
if (options.general.showGuide) { if (options.general.showGuide) {
this._openWelcomeGuidePage(); this._openWelcomeGuidePage();
} }
@ -228,23 +230,10 @@ class Backend {
} }
} }
isPrepared() {
return this._isPrepared;
}
getFullOptions(useSchema=false) {
const options = this._options;
return useSchema ? this._optionsUtil.createValidatingProxy(options) : options;
}
getOptions(optionsContext, useSchema=false) {
return this._getProfile(optionsContext, useSchema).options;
}
// Event handlers // Event handlers
async _onClipboardTextChange({text}) { async _onClipboardTextChange({text}) {
const {general: {maximumClipboardSearchLength}} = this.getOptions({current: true}); const {general: {maximumClipboardSearchLength}} = this._getProfileOptions({current: true});
if (text.length > maximumClipboardSearchLength) { if (text.length > maximumClipboardSearchLength) {
text = text.substring(0, maximumClipboardSearchLength); text = text.substring(0, maximumClipboardSearchLength);
} }
@ -385,15 +374,15 @@ class Backend {
} }
_onApiOptionsGet({optionsContext}) { _onApiOptionsGet({optionsContext}) {
return this.getOptions(optionsContext); return this._getProfileOptions(optionsContext);
} }
_onApiOptionsGetFull() { _onApiOptionsGetFull() {
return this.getFullOptions(); return this._getOptionsFull();
} }
async _onApiKanjiFind({text, optionsContext}) { async _onApiKanjiFind({text, optionsContext}) {
const options = this.getOptions(optionsContext); const options = this._getProfileOptions(optionsContext);
const {general: {maxResults}} = options; const {general: {maxResults}} = options;
const findKanjiOptions = this._getTranslatorFindKanjiOptions(options); const findKanjiOptions = this._getTranslatorFindKanjiOptions(options);
const definitions = await this._translator.findKanji(text, findKanjiOptions); const definitions = await this._translator.findKanji(text, findKanjiOptions);
@ -402,7 +391,7 @@ class Backend {
} }
async _onApiTermsFind({text, details, optionsContext}) { async _onApiTermsFind({text, details, optionsContext}) {
const options = this.getOptions(optionsContext); const options = this._getProfileOptions(optionsContext);
const {general: {resultOutputMode: mode, maxResults}} = options; const {general: {resultOutputMode: mode, maxResults}} = options;
const findTermsOptions = this._getTranslatorFindTermsOptions(details, options); const findTermsOptions = this._getTranslatorFindTermsOptions(details, options);
const [definitions, length] = await this._translator.findTerms(mode, text, findTermsOptions); const [definitions, length] = await this._translator.findTerms(mode, text, findTermsOptions);
@ -411,7 +400,7 @@ class Backend {
} }
async _onApiTextParse({text, optionsContext}) { async _onApiTextParse({text, optionsContext}) {
const options = this.getOptions(optionsContext); const options = this._getProfileOptions(optionsContext);
const results = []; const results = [];
if (options.parsing.enableScanningParser) { if (options.parsing.enableScanningParser) {
@ -672,18 +661,8 @@ class Backend {
return details; return details;
} }
async _onApiModifySettings({targets, source}) { _onApiModifySettings({targets, source}) {
const results = []; return this._modifySettings(targets, source);
for (const target of targets) {
try {
const result = this._modifySetting(target);
results.push({result: clone(result)});
} catch (e) {
results.push({error: serializeError(e)});
}
}
await this._saveOptions(source);
return results;
} }
_onApiGetSettings({targets}) { _onApiGetSettings({targets}) {
@ -784,10 +763,14 @@ class Backend {
} }
async _onCommandToggleTextScanning() { async _onCommandToggleTextScanning() {
const source = 'popup'; const options = this._getProfileOptions({current: true});
const options = this.getOptions({current: true}); await this._modifySettings([{
options.general.enable = !options.general.enable; action: 'set',
await this._saveOptions(source); path: 'general.enable',
value: !options.general.enable,
scope: 'profile',
optionsContext: {current: true}
}], 'backend');
} }
async _onCommandOpenPopupWindow() { async _onCommandOpenPopupWindow() {
@ -796,6 +779,20 @@ class Backend {
// Utilities // Utilities
async _modifySettings(targets, source) {
const results = [];
for (const target of targets) {
try {
const result = this._modifySetting(target);
results.push({result: clone(result)});
} catch (e) {
results.push({error: serializeError(e)});
}
}
await this._saveOptions(source);
return results;
}
_getOrCreateSearchPopup() { _getOrCreateSearchPopup() {
if (this._searchPopupTabCreatePromise === null) { if (this._searchPopupTabCreatePromise === null) {
const promise = this._getOrCreateSearchPopup2(); const promise = this._getOrCreateSearchPopup2();
@ -822,7 +819,7 @@ class Backend {
} }
// Create a new window // Create a new window
const options = this.getOptions({current: true}); const options = this._getProfileOptions({current: true});
const createData = this._getSearchPopupWindowCreateData(baseUrl, options); const createData = this._getSearchPopupWindowCreateData(baseUrl, options);
const {popupWindow: {windowState}} = options; const {popupWindow: {windowState}} = options;
const popupWindow = await this._createWindow(createData); const popupWindow = await this._createWindow(createData);
@ -903,7 +900,7 @@ class Backend {
} }
_applyOptions(source) { _applyOptions(source) {
const options = this.getOptions({current: true}); const options = this._getProfileOptions({current: true});
this._updateBadge(); this._updateBadge();
this._anki.server = options.anki.server; this._anki.server = options.anki.server;
@ -924,8 +921,17 @@ class Backend {
this._sendMessageAllTabsIgnoreResponse('optionsUpdated', {source}); this._sendMessageAllTabsIgnoreResponse('optionsUpdated', {source});
} }
_getOptionsFull(useSchema=false) {
const options = this._options;
return useSchema ? this._optionsUtil.createValidatingProxy(options) : options;
}
_getProfileOptions(optionsContext, useSchema=false) {
return this._getProfile(optionsContext, useSchema).options;
}
_getProfile(optionsContext, useSchema=false) { _getProfile(optionsContext, useSchema=false) {
const options = this.getFullOptions(useSchema); const options = this._getOptionsFull(useSchema);
const profiles = options.profiles; const profiles = options.profiles;
if (optionsContext.current) { if (optionsContext.current) {
return profiles[options.profileCurrent]; return profiles[options.profileCurrent];
@ -1132,9 +1138,9 @@ class Backend {
switch (scope) { switch (scope) {
case 'profile': case 'profile':
if (!isObject(target.optionsContext)) { throw new Error('Invalid optionsContext'); } if (!isObject(target.optionsContext)) { throw new Error('Invalid optionsContext'); }
return this.getOptions(target.optionsContext, true); return this._getProfileOptions(target.optionsContext, true);
case 'global': case 'global':
return this.getFullOptions(true); return this._getOptionsFull(true);
default: default:
throw new Error(`Invalid scope: ${scope}`); throw new Error(`Invalid scope: ${scope}`);
} }
@ -1689,7 +1695,7 @@ class Backend {
async _saveOptions(source) { async _saveOptions(source) {
this._clearProfileConditionsSchemaCache(); this._clearProfileConditionsSchemaCache();
const options = this.getFullOptions(); const options = this._getOptionsFull();
await this._optionsUtil.save(options); await this._optionsUtil.save(options);
this._applyOptions(source); this._applyOptions(source);
} }