From 142098aad2946426971f04177d75e6a792374a1e Mon Sep 17 00:00:00 2001 From: Khai Truong Date: Thu, 25 Jul 2024 19:18:43 +0700 Subject: [PATCH] Able to edit dictionary alias on settings --- ext/css/settings.css | 3 +++ ext/data/schemas/options-schema.json | 5 ++++ ext/js/background/backend.js | 2 ++ ext/js/data/options-util.js | 15 +++++++++++ .../pages/settings/dictionary-controller.js | 27 ++++++++++++++++--- ext/templates-settings.html | 3 ++- test/data/translator-test-inputs.json | 1 + test/database.test.js | 4 +-- test/options-util.test.js | 2 ++ types/ext/settings.d.ts | 1 + types/ext/translation.d.ts | 4 +++ 11 files changed, 61 insertions(+), 6 deletions(-) diff --git a/ext/css/settings.css b/ext/css/settings.css index 651d805b8c..2faea64f87 100644 --- a/ext/css/settings.css +++ b/ext/css/settings.css @@ -2322,6 +2322,9 @@ button.hotkey-list-item-enabled-button[data-scope-count='0'] { .dictionary-item[data-enabled=false] .dictionary-title { color: var(--text-color-light2); } +input[type=text].dictionary-alias-hidden { + display: none; +} input[type=number].dictionary-priority { margin-top: 0; margin-right: 0.5em; diff --git a/ext/data/schemas/options-schema.json b/ext/data/schemas/options-schema.json index 7deec5ffd9..7112ee4d09 100644 --- a/ext/data/schemas/options-schema.json +++ b/ext/data/schemas/options-schema.json @@ -826,6 +826,7 @@ "type": "object", "required": [ "name", + "alias", "priority", "enabled", "allowSecondarySearches", @@ -838,6 +839,10 @@ "type": "string", "default": "" }, + "alias": { + "type": "string", + "default": "" + }, "priority": { "type": "number", "default": 0 diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index 96e8206bee..2117f3833c 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -2471,6 +2471,7 @@ export class Backend { if (mode === 'merge' && !enabledDictionaryMap.has(mainDictionary)) { enabledDictionaryMap.set(mainDictionary, { index: enabledDictionaryMap.size, + alias: mainDictionary, priority: 0, allowSecondarySearches: false, partsOfSpeechFilter: true, @@ -2518,6 +2519,7 @@ export class Backend { const {name, priority, allowSecondarySearches, partsOfSpeechFilter, useDeinflections} = dictionary; enabledDictionaryMap.set(name, { index: enabledDictionaryMap.size, + alias: name, priority, allowSecondarySearches, partsOfSpeechFilter, diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index 40fc20ddf3..1a3032dbdf 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -559,6 +559,7 @@ export class OptionsUtil { this._updateVersion45, this._updateVersion46, this._updateVersion47, + this._updateVersion48, ]; /* eslint-enable @typescript-eslint/unbound-method */ if (typeof targetVersion === 'number' && targetVersion < result.length) { @@ -1434,6 +1435,20 @@ export class OptionsUtil { } } + /** + * - Added scanning.scanWithoutMousemove + * @type {import('options-util').UpdateFunction} + */ + async _updateVersion48(options) { + for (const {options: profileOptions} of options.profiles) { + if (Array.isArray(profileOptions.dictionaries)) { + for (const dictionary of profileOptions.dictionaries) { + dictionary.alias = dictionary.name; + } + } + } + } + /** * @param {string} url * @returns {Promise} diff --git a/ext/js/pages/settings/dictionary-controller.js b/ext/js/pages/settings/dictionary-controller.js index 5099d1f42c..7225c89019 100644 --- a/ext/js/pages/settings/dictionary-controller.js +++ b/ext/js/pages/settings/dictionary-controller.js @@ -63,7 +63,9 @@ class DictionaryEntry { /** @type {HTMLButtonElement} */ this._updatesAvailable = querySelectorNotNull(fragment, '.dictionary-update-available'); /** @type {HTMLElement} */ - this._titleNode = querySelectorNotNull(fragment, '.dictionary-title'); + this._aliasNode = querySelectorNotNull(fragment, '.dictionary-alias'); + /** @type {HTMLInputElement} */ + this._aliasHiddenNode = querySelectorNotNull(fragment, '.dictionary-alias-hidden'); /** @type {HTMLElement} */ this._versionNode = querySelectorNotNull(fragment, '.dictionary-revision'); /** @type {HTMLElement} */ @@ -79,9 +81,12 @@ class DictionaryEntry { prepare() { // const index = this._index; - const {title, revision, version} = this._dictionaryInfo; + const {revision, version} = this._dictionaryInfo; + + this._aliasHiddenNode.dataset.setting = `dictionaries[${index}].alias`; + this._eventListeners.addEventListener(this._aliasNode, 'input', this._onAliasInput.bind(this), false); + this._eventListeners.addEventListener(this._aliasHiddenNode, 'settingChanged', this._onAliasInitialized.bind(this), false); - this._titleNode.textContent = title; this._versionNode.textContent = `rev.${revision}`; this._outdatedButton.hidden = (version >= 3); this._priorityInput.dataset.setting = `dictionaries[${index}].priority`; @@ -180,6 +185,21 @@ class DictionaryEntry { } } + /** */ + _onAliasInput() { + if (this._aliasNode.textContent === '') this._aliasNode.textContent = this.dictionaryTitle; + this._aliasHiddenNode.value = `${this._aliasNode.textContent}`; + this._aliasHiddenNode.dispatchEvent(new Event('change')); + } + + /** + * @param {import('dom-data-binder').SettingChangedEvent} e + */ + _onAliasInitialized(e) { + const {detail: {value}} = e; + this._aliasNode.textContent = value === '' ? this.dictionaryTitle : `${value}`; + } + /** * @param {import('dom-data-binder').SettingChangedEvent} e */ @@ -600,6 +620,7 @@ export class DictionaryController { static createDefaultDictionarySettings(name, enabled, styles) { return { name, + alias: name, priority: 0, enabled, allowSecondarySearches: false, diff --git a/ext/templates-settings.html b/ext/templates-settings.html index 4ae665107c..5c931cff34 100644 --- a/ext/templates-settings.html +++ b/ext/templates-settings.html @@ -52,8 +52,9 @@
- + + diff --git a/test/data/translator-test-inputs.json b/test/data/translator-test-inputs.json index ed0282731f..77e6536464 100644 --- a/test/data/translator-test-inputs.json +++ b/test/data/translator-test-inputs.json @@ -30,6 +30,7 @@ "${title}", { "index": 0, + "alias": "${title}", "priority": 0, "allowSecondarySearches": false, "partsOfSpeechFilter": true, diff --git a/test/database.test.js b/test/database.test.js index 143462bb69..74b5526021 100644 --- a/test/database.test.js +++ b/test/database.test.js @@ -114,7 +114,7 @@ describe('Database', () => { const title = testDictionaryIndex.title; const titles = new Map([ - [title, {priority: 0, allowSecondarySearches: false}], + [title, {alias: title, priority: 0, allowSecondarySearches: false}], ]); // Setup database @@ -184,7 +184,7 @@ describe('Database', () => { const title = testDictionaryIndex.title; const titles = new Map([ - [title, {priority: 0, allowSecondarySearches: false}], + [title, {alias: title, priority: 0, allowSecondarySearches: false}], ]); // Setup database diff --git a/test/options-util.test.js b/test/options-util.test.js index 9ccfabbafd..a0b0f3b4d4 100644 --- a/test/options-util.test.js +++ b/test/options-util.test.js @@ -107,6 +107,7 @@ function createProfileOptionsTestData1() { }, dictionaries: { 'Test Dictionary': { + alias: 'Test Dictionary', priority: 0, enabled: true, allowSecondarySearches: false, @@ -438,6 +439,7 @@ function createProfileOptionsUpdatedTestData1() { dictionaries: [ { name: 'Test Dictionary', + alias: 'Test Dictionary', priority: 0, enabled: true, allowSecondarySearches: false, diff --git a/types/ext/settings.d.ts b/types/ext/settings.d.ts index bd40d73673..3a6b7bcb70 100644 --- a/types/ext/settings.d.ts +++ b/types/ext/settings.d.ts @@ -263,6 +263,7 @@ export type DictionariesOptions = DictionaryOptions[]; export type DictionaryOptions = { name: string; + alias: string; priority: number; enabled: boolean; allowSecondarySearches: boolean; diff --git a/types/ext/translation.d.ts b/types/ext/translation.d.ts index 2e4d1a660d..8bc6ade5cd 100644 --- a/types/ext/translation.d.ts +++ b/types/ext/translation.d.ts @@ -140,6 +140,10 @@ export type FindTermDictionary = { * The index of the dictionary */ index: number; + /** + * The alias of the dictionary + */ + alias: string; /** * The priority of the dictionary */