Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmaa committed Oct 18, 2024
1 parent 51f17d0 commit 9d2497f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 1 deletion.
6 changes: 6 additions & 0 deletions ext/data/default-options-overrides.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"es": [{
"path": "scanning.scanResolution",
"value": "word"
}]
}
30 changes: 29 additions & 1 deletion ext/js/pages/settings/languages-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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?');

Check failure on line 71 in ext/js/pages/settings/languages-controller.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Unexpected confirm
if (yes) {
await this._settingsController.applyLanguageSettingOverrides(setLanguage);
}
}
}
}
15 changes: 15 additions & 0 deletions ext/js/pages/settings/settings-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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} */
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<import('dictionary-importer').Summary[]>}
*/
Expand Down
9 changes: 9 additions & 0 deletions types/ext/settings-controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ export type SettingsModification<THasScope extends boolean> = THasScope extends
export type SettingsExtraFields<THasScope extends boolean> = THasScope extends true ? null : SettingsModifications.OptionsScope;

export type ModifyResult = Core.Response<SettingsModifications.ModificationResult>;

export type LanguageSettingOverrides = {
[key: string]: SettingOverride[];
};

export type SettingOverride = {
path: string;
value: string;
};

0 comments on commit 9d2497f

Please sign in to comment.