From d9866106071ffe917dc77f82a3ae9100caf12d5a Mon Sep 17 00:00:00 2001 From: Khai Truong Date: Fri, 23 Aug 2024 00:00:39 +0700 Subject: [PATCH 1/3] Disable enabled button when dictionary is already installed --- ext/js/pages/settings/dictionary-import-controller.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ext/js/pages/settings/dictionary-import-controller.js b/ext/js/pages/settings/dictionary-import-controller.js index 1c1b7720c4..08c4d78aa7 100644 --- a/ext/js/pages/settings/dictionary-import-controller.js +++ b/ext/js/pages/settings/dictionary-import-controller.js @@ -176,8 +176,11 @@ export class DictionaryImportController { return; } + const installedDictionaries = await this._settingsController.getDictionaryInfo(); + const installedDictionaryNames = new Set(installedDictionaries.map((dictionary) => dictionary.title)); + for (const {property, element} of recommendedDictionaryCategories) { - this._renderRecommendedDictionaryGroup(recommendedDictionaries[language][property], element); + this._renderRecommendedDictionaryGroup(recommendedDictionaries[language][property], element, installedDictionaryNames); } /** @type {NodeListOf} */ @@ -191,8 +194,9 @@ export class DictionaryImportController { * * @param {import('dictionary-recommended.js').Dictionary[]} recommendedDictionaries * @param {HTMLElement} dictionariesList + * @param {Set} installedDictionaryNames */ - _renderRecommendedDictionaryGroup(recommendedDictionaries, dictionariesList) { + _renderRecommendedDictionaryGroup(recommendedDictionaries, dictionariesList, installedDictionaryNames) { const dictionariesListParent = dictionariesList.parentElement; dictionariesList.innerHTML = ''; for (const dictionary of recommendedDictionaries) { @@ -202,7 +206,9 @@ export class DictionaryImportController { } const template = this._settingsController.instantiateTemplate('recommended-dictionaries-list-item'); const label = querySelectorNotNull(template, '.settings-item-label'); + /** @type {HTMLButtonElement} */ const button = querySelectorNotNull(template, '.action-button[data-action=import-recommended-dictionary]'); + button.disabled = installedDictionaryNames.has(dictionary.name); const urlAttribute = document.createAttribute('data-import-url'); urlAttribute.value = dictionary.url; From 5c8239626abe0f074d257068ef49f96b8805a66e Mon Sep 17 00:00:00 2001 From: Khai Truong Date: Fri, 23 Aug 2024 00:19:01 +0700 Subject: [PATCH 2/3] Disable button if downloadUrl exists in installed dictionaries --- .../settings/dictionary-import-controller.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/ext/js/pages/settings/dictionary-import-controller.js b/ext/js/pages/settings/dictionary-import-controller.js index 08c4d78aa7..5cfd90e17b 100644 --- a/ext/js/pages/settings/dictionary-import-controller.js +++ b/ext/js/pages/settings/dictionary-import-controller.js @@ -177,10 +177,17 @@ export class DictionaryImportController { } const installedDictionaries = await this._settingsController.getDictionaryInfo(); - const installedDictionaryNames = new Set(installedDictionaries.map((dictionary) => dictionary.title)); + const installedDictionaryNames = new Set(); + const installedDictionaryDownloadUrls = new Set(); + for (const dictionary of installedDictionaries) { + installedDictionaryNames.add(dictionary.title); + if (dictionary.downloadUrl) { + installedDictionaryDownloadUrls.add(dictionary.downloadUrl); + } + } for (const {property, element} of recommendedDictionaryCategories) { - this._renderRecommendedDictionaryGroup(recommendedDictionaries[language][property], element, installedDictionaryNames); + this._renderRecommendedDictionaryGroup(recommendedDictionaries[language][property], element, installedDictionaryNames, installedDictionaryDownloadUrls); } /** @type {NodeListOf} */ @@ -195,8 +202,9 @@ export class DictionaryImportController { * @param {import('dictionary-recommended.js').Dictionary[]} recommendedDictionaries * @param {HTMLElement} dictionariesList * @param {Set} installedDictionaryNames + * @param {Set} installedDictionaryDownloadUrls */ - _renderRecommendedDictionaryGroup(recommendedDictionaries, dictionariesList, installedDictionaryNames) { + _renderRecommendedDictionaryGroup(recommendedDictionaries, dictionariesList, installedDictionaryNames, installedDictionaryDownloadUrls) { const dictionariesListParent = dictionariesList.parentElement; dictionariesList.innerHTML = ''; for (const dictionary of recommendedDictionaries) { @@ -208,7 +216,7 @@ export class DictionaryImportController { const label = querySelectorNotNull(template, '.settings-item-label'); /** @type {HTMLButtonElement} */ const button = querySelectorNotNull(template, '.action-button[data-action=import-recommended-dictionary]'); - button.disabled = installedDictionaryNames.has(dictionary.name); + button.disabled = installedDictionaryNames.has(dictionary.name) || installedDictionaryDownloadUrls.has(dictionary.url); const urlAttribute = document.createAttribute('data-import-url'); urlAttribute.value = dictionary.url; From 9f4bb90f05b0bc53a8f398dfcf5d34b0fc7d87ab Mon Sep 17 00:00:00 2001 From: Khai Truong Date: Fri, 23 Aug 2024 00:26:04 +0700 Subject: [PATCH 3/3] lint --- ext/js/pages/settings/dictionary-import-controller.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ext/js/pages/settings/dictionary-import-controller.js b/ext/js/pages/settings/dictionary-import-controller.js index 5cfd90e17b..12fb5a4bb8 100644 --- a/ext/js/pages/settings/dictionary-import-controller.js +++ b/ext/js/pages/settings/dictionary-import-controller.js @@ -177,7 +177,9 @@ export class DictionaryImportController { } const installedDictionaries = await this._settingsController.getDictionaryInfo(); + /** @type {Set} */ const installedDictionaryNames = new Set(); + /** @type {Set} */ const installedDictionaryDownloadUrls = new Set(); for (const dictionary of installedDictionaries) { installedDictionaryNames.add(dictionary.title);