From 9d2497f6bd5c4f01280ff81a5de8caf1462e94bf Mon Sep 17 00:00:00 2001 From: jamesmaa Date: Wed, 28 Aug 2024 11:31:11 -0700 Subject: [PATCH] WIP --- ext/data/default-options-overrides.json | 6 ++++ ext/js/pages/settings/languages-controller.js | 30 ++++++++++++++++++- ext/js/pages/settings/settings-controller.js | 15 ++++++++++ types/ext/settings-controller.d.ts | 9 ++++++ 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 ext/data/default-options-overrides.json diff --git a/ext/data/default-options-overrides.json b/ext/data/default-options-overrides.json new file mode 100644 index 0000000000..01ec7740a3 --- /dev/null +++ b/ext/data/default-options-overrides.json @@ -0,0 +1,6 @@ +{ + "es": [{ + "path": "scanning.scanResolution", + "value": "word" + }] +} diff --git a/ext/js/pages/settings/languages-controller.js b/ext/js/pages/settings/languages-controller.js index 707230bef4..35863a5016 100755 --- a/ext/js/pages/settings/languages-controller.js +++ b/ext/js/pages/settings/languages-controller.js @@ -25,17 +25,25 @@ export class LanguagesController { constructor(settingsController) { /** @type {import('./settings-controller.js').SettingsController} */ this._settingsController = settingsController; + /** @type {string} */ + this._lastSelectedLanguage = ''; } /** */ async prepare() { const languages = await this._settingsController.application.api.getLanguageSummaries(); languages.sort((a, b) => a.name.localeCompare(b.name, 'en')); - this._fillSelect(languages); + const languageSelect = this._fillSelect(languages); + languageSelect.addEventListener( + /** @type {string} */ ('settingChanged'), + /** @type {EventListener} */ (this._onLanguageSelectChanged.bind(this)), + false, + ); } /** * @param {import('language').LanguageSummary[]} languages + * @returns {Element} */ _fillSelect(languages) { const selectElement = querySelectorNotNull(document, '#language-select'); @@ -45,5 +53,25 @@ export class LanguagesController { option.text = `${name} (${iso})`; selectElement.appendChild(option); } + return selectElement; + } + + /** + * @param {import('dom-data-binder').SettingChangedEvent} settingChangedEvent + */ + async _onLanguageSelectChanged(settingChangedEvent) { + const existingSettings = await this._settingsController.getProfileSettings([{path: 'general.language'}]); + const existingLanguage = existingSettings[0].result; + const setLanguage = settingChangedEvent.detail.value; + if (typeof existingLanguage !== 'string' || typeof setLanguage !== 'string') { return; } + if (this._lastSelectedLanguage === '') { + this._lastSelectedLanguage = setLanguage; + } else if (this._lastSelectedLanguage !== setLanguage) { + this._lastSelectedLanguage = setLanguage; + const yes = confirm('Changing language to: ' + setLanguage + '. Continue?'); + if (yes) { + await this._settingsController.applyLanguageSettingOverrides(setLanguage); + } + } } } diff --git a/ext/js/pages/settings/settings-controller.js b/ext/js/pages/settings/settings-controller.js index ee44f875f3..861f5b4da1 100644 --- a/ext/js/pages/settings/settings-controller.js +++ b/ext/js/pages/settings/settings-controller.js @@ -18,6 +18,7 @@ import {EventDispatcher} from '../../core/event-dispatcher.js'; import {EventListenerCollection} from '../../core/event-listener-collection.js'; +import {fetchJson} from '../../core/fetch-utilities.js'; import {isObjectNotArray} from '../../core/object-utilities.js'; import {generateId} from '../../core/utilities.js'; import {OptionsUtil} from '../../data/options-util.js'; @@ -45,6 +46,8 @@ export class SettingsController extends EventDispatcher { this._pageExitPreventionEventListeners = new EventListenerCollection(); /** @type {HtmlTemplateCollection} */ this._templates = new HtmlTemplateCollection(); + /** @type {import('settings-controller').LanguageSettingOverrides} */ + this._languageSettingOverrides = {}; } /** @type {import('../../application.js').Application} */ @@ -75,6 +78,7 @@ export class SettingsController extends EventDispatcher { /** */ async prepare() { await this._templates.loadFromFiles(['/templates-settings.html']); + this._languageSettingOverrides = await fetchJson('/data/default-options-overrides.json'); this._application.on('optionsUpdated', this._onOptionsUpdated.bind(this)); if (this._canObservePermissionsChanges()) { chrome.permissions.onAdded.addListener(this._onPermissionsChanged.bind(this)); @@ -182,6 +186,17 @@ export class SettingsController extends EventDispatcher { return await this.modifyProfileSettings([{action: 'set', path, value}]); } + /** + * @param {string} language + */ + async applyLanguageSettingOverrides(language) { + const settingOverrides = this._languageSettingOverrides[language]; + if (typeof settingOverrides === 'undefined') { return; } + /** @type {import('settings-modifications').Modification[]} */ + const modifications = settingOverrides.map(({path, value}) => ({action: 'set', path, value})); + await this.modifyProfileSettings(modifications); + } + /** * @returns {Promise} */ diff --git a/types/ext/settings-controller.d.ts b/types/ext/settings-controller.d.ts index e12651ebea..4a4296ba88 100644 --- a/types/ext/settings-controller.d.ts +++ b/types/ext/settings-controller.d.ts @@ -57,3 +57,12 @@ export type SettingsModification = THasScope extends export type SettingsExtraFields = THasScope extends true ? null : SettingsModifications.OptionsScope; export type ModifyResult = Core.Response; + +export type LanguageSettingOverrides = { + [key: string]: SettingOverride[]; +}; + +export type SettingOverride = { + path: string; + value: string; +};