Backend options update (#1274)
* Update how _onCommandToggleTextScanning changes settings * Make getFullOptions/getOptions private * Remove unused isPrepared
This commit is contained in:
parent
e72152bf61
commit
85c723b85f
@ -158,6 +158,8 @@ class Backend {
|
||||
return this._prepareCompletePromise;
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
_prepareInternalSync() {
|
||||
if (isObject(chrome.commands) && isObject(chrome.commands.onCommand)) {
|
||||
const onCommand = this._onWebExtensionEventWrapper(this._onCommand.bind(this));
|
||||
@ -208,7 +210,7 @@ class Backend {
|
||||
|
||||
this._applyOptions('background');
|
||||
|
||||
const options = this.getOptions({current: true});
|
||||
const options = this._getProfileOptions({current: true});
|
||||
if (options.general.showGuide) {
|
||||
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
|
||||
|
||||
async _onClipboardTextChange({text}) {
|
||||
const {general: {maximumClipboardSearchLength}} = this.getOptions({current: true});
|
||||
const {general: {maximumClipboardSearchLength}} = this._getProfileOptions({current: true});
|
||||
if (text.length > maximumClipboardSearchLength) {
|
||||
text = text.substring(0, maximumClipboardSearchLength);
|
||||
}
|
||||
@ -385,15 +374,15 @@ class Backend {
|
||||
}
|
||||
|
||||
_onApiOptionsGet({optionsContext}) {
|
||||
return this.getOptions(optionsContext);
|
||||
return this._getProfileOptions(optionsContext);
|
||||
}
|
||||
|
||||
_onApiOptionsGetFull() {
|
||||
return this.getFullOptions();
|
||||
return this._getOptionsFull();
|
||||
}
|
||||
|
||||
async _onApiKanjiFind({text, optionsContext}) {
|
||||
const options = this.getOptions(optionsContext);
|
||||
const options = this._getProfileOptions(optionsContext);
|
||||
const {general: {maxResults}} = options;
|
||||
const findKanjiOptions = this._getTranslatorFindKanjiOptions(options);
|
||||
const definitions = await this._translator.findKanji(text, findKanjiOptions);
|
||||
@ -402,7 +391,7 @@ class Backend {
|
||||
}
|
||||
|
||||
async _onApiTermsFind({text, details, optionsContext}) {
|
||||
const options = this.getOptions(optionsContext);
|
||||
const options = this._getProfileOptions(optionsContext);
|
||||
const {general: {resultOutputMode: mode, maxResults}} = options;
|
||||
const findTermsOptions = this._getTranslatorFindTermsOptions(details, options);
|
||||
const [definitions, length] = await this._translator.findTerms(mode, text, findTermsOptions);
|
||||
@ -411,7 +400,7 @@ class Backend {
|
||||
}
|
||||
|
||||
async _onApiTextParse({text, optionsContext}) {
|
||||
const options = this.getOptions(optionsContext);
|
||||
const options = this._getProfileOptions(optionsContext);
|
||||
const results = [];
|
||||
|
||||
if (options.parsing.enableScanningParser) {
|
||||
@ -672,18 +661,8 @@ class Backend {
|
||||
return details;
|
||||
}
|
||||
|
||||
async _onApiModifySettings({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;
|
||||
_onApiModifySettings({targets, source}) {
|
||||
return this._modifySettings(targets, source);
|
||||
}
|
||||
|
||||
_onApiGetSettings({targets}) {
|
||||
@ -784,10 +763,14 @@ class Backend {
|
||||
}
|
||||
|
||||
async _onCommandToggleTextScanning() {
|
||||
const source = 'popup';
|
||||
const options = this.getOptions({current: true});
|
||||
options.general.enable = !options.general.enable;
|
||||
await this._saveOptions(source);
|
||||
const options = this._getProfileOptions({current: true});
|
||||
await this._modifySettings([{
|
||||
action: 'set',
|
||||
path: 'general.enable',
|
||||
value: !options.general.enable,
|
||||
scope: 'profile',
|
||||
optionsContext: {current: true}
|
||||
}], 'backend');
|
||||
}
|
||||
|
||||
async _onCommandOpenPopupWindow() {
|
||||
@ -796,6 +779,20 @@ class Backend {
|
||||
|
||||
// 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() {
|
||||
if (this._searchPopupTabCreatePromise === null) {
|
||||
const promise = this._getOrCreateSearchPopup2();
|
||||
@ -822,7 +819,7 @@ class Backend {
|
||||
}
|
||||
|
||||
// Create a new window
|
||||
const options = this.getOptions({current: true});
|
||||
const options = this._getProfileOptions({current: true});
|
||||
const createData = this._getSearchPopupWindowCreateData(baseUrl, options);
|
||||
const {popupWindow: {windowState}} = options;
|
||||
const popupWindow = await this._createWindow(createData);
|
||||
@ -903,7 +900,7 @@ class Backend {
|
||||
}
|
||||
|
||||
_applyOptions(source) {
|
||||
const options = this.getOptions({current: true});
|
||||
const options = this._getProfileOptions({current: true});
|
||||
this._updateBadge();
|
||||
|
||||
this._anki.server = options.anki.server;
|
||||
@ -924,8 +921,17 @@ class Backend {
|
||||
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) {
|
||||
const options = this.getFullOptions(useSchema);
|
||||
const options = this._getOptionsFull(useSchema);
|
||||
const profiles = options.profiles;
|
||||
if (optionsContext.current) {
|
||||
return profiles[options.profileCurrent];
|
||||
@ -1132,9 +1138,9 @@ class Backend {
|
||||
switch (scope) {
|
||||
case 'profile':
|
||||
if (!isObject(target.optionsContext)) { throw new Error('Invalid optionsContext'); }
|
||||
return this.getOptions(target.optionsContext, true);
|
||||
return this._getProfileOptions(target.optionsContext, true);
|
||||
case 'global':
|
||||
return this.getFullOptions(true);
|
||||
return this._getOptionsFull(true);
|
||||
default:
|
||||
throw new Error(`Invalid scope: ${scope}`);
|
||||
}
|
||||
@ -1689,7 +1695,7 @@ class Backend {
|
||||
|
||||
async _saveOptions(source) {
|
||||
this._clearProfileConditionsSchemaCache();
|
||||
const options = this.getFullOptions();
|
||||
const options = this._getOptionsFull();
|
||||
await this._optionsUtil.save(options);
|
||||
this._applyOptions(source);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user