From b33bc945b483dcc219e3d1f2c9f96d4f408ca34f Mon Sep 17 00:00:00 2001 From: Khai Truong Date: Sat, 2 Nov 2024 13:18:45 +0700 Subject: [PATCH] Add profile id --- ext/js/data/options-util.js | 11 +++++++++++ ext/js/pages/settings/dictionary-controller.js | 7 ++++--- .../settings/dictionary-import-controller.js | 6 +++--- ext/js/pages/settings/profile-controller.js | 15 ++++++++++++++- test/options-util.test.js | 3 ++- types/ext/settings-controller.d.ts | 2 +- types/ext/settings.d.ts | 1 + 7 files changed, 36 insertions(+), 9 deletions(-) diff --git a/ext/js/data/options-util.js b/ext/js/data/options-util.js index ad49ab7886..28aca32475 100644 --- a/ext/js/data/options-util.js +++ b/ext/js/data/options-util.js @@ -564,6 +564,7 @@ export class OptionsUtil { this._updateVersion50, this._updateVersion51, this._updateVersion52, + this._updateVersion53, ]; /* eslint-enable @typescript-eslint/unbound-method */ if (typeof targetVersion === 'number' && targetVersion < result.length) { @@ -1498,6 +1499,16 @@ export class OptionsUtil { } } + /** + * - Added profile id + * @type {import('options-util').UpdateFunction} + */ + async _updateVersion53(options) { + for (let i = 0; i < options.profiles.length; i++) { + options.profiles[i].id = `profile-${i}`; + } + } + /** * @param {string} url * @returns {Promise} diff --git a/ext/js/pages/settings/dictionary-controller.js b/ext/js/pages/settings/dictionary-controller.js index 81983125d4..9a27bd9a44 100644 --- a/ext/js/pages/settings/dictionary-controller.js +++ b/ext/js/pages/settings/dictionary-controller.js @@ -1143,13 +1143,14 @@ export class DictionaryController { const {profiles} = options; /** @type {import('settings-controller.js').ProfilesDictionarySettings} */ - const profilesDictionarySettings = []; + const profilesDictionarySettings = {}; for (let i = 0, ii = profiles.length; i < ii; ++i) { - const dictionaries = profiles[i].options.dictionaries; + const profile = profiles[i]; + const dictionaries = profile.options.dictionaries; for (let j = 0, jj = dictionaries.length; j < jj; ++j) { if (dictionaries[j].name === dictionaryTitle) { - profilesDictionarySettings.push({...dictionaries[j], index: j}); + profilesDictionarySettings[profile.id] = {...dictionaries[j], index: j}; break; } } diff --git a/ext/js/pages/settings/dictionary-import-controller.js b/ext/js/pages/settings/dictionary-import-controller.js index 175b62f718..ed358ff9b3 100644 --- a/ext/js/pages/settings/dictionary-import-controller.js +++ b/ext/js/pages/settings/dictionary-import-controller.js @@ -668,15 +668,15 @@ export class DictionaryImportController { const targets = []; const profileCount = optionsFull.profiles.length; for (let i = 0; i < profileCount; ++i) { - const {options} = optionsFull.profiles[i]; + const {options, id: profileId} = optionsFull.profiles[i]; const enabled = profileIndex === i; const defaultSettings = DictionaryController.createDefaultDictionarySettings(title, enabled, styles); const path1 = `profiles[${i}].options.dictionaries`; - if (profilesDictionarySettings === null || typeof profilesDictionarySettings[i] === 'undefined') { + if (profilesDictionarySettings === null || typeof profilesDictionarySettings[profileId] === 'undefined') { targets.push({action: 'push', path: path1, items: [defaultSettings]}); } else { - const {index, ...currentSettings} = profilesDictionarySettings[i]; + const {index, ...currentSettings} = profilesDictionarySettings[profileId]; targets.push({ action: 'splice', path: path1, diff --git a/ext/js/pages/settings/profile-controller.js b/ext/js/pages/settings/profile-controller.js index 558b45bedc..ab1a6e780e 100644 --- a/ext/js/pages/settings/profile-controller.js +++ b/ext/js/pages/settings/profile-controller.js @@ -17,7 +17,7 @@ */ import {EventListenerCollection} from '../../core/event-listener-collection.js'; -import {clone} from '../../core/utilities.js'; +import {clone, generateId} from '../../core/utilities.js'; import {querySelectorNotNull} from '../../dom/query-selector.js'; import {ProfileConditionsUI} from './profile-conditions-ui.js'; @@ -184,6 +184,7 @@ export class ProfileController { // Create new profile const newProfile = clone(profile); newProfile.name = this._createCopyName(profile.name, this._profiles, 100); + newProfile.id = this._createId(this._profiles); // Update state const index = this._profiles.length; @@ -594,6 +595,18 @@ export class ProfileController { } } } + /** + * @param {import('settings').Profile[]} profiles + * @returns {string} + */ + _createId(profiles) { + const ids = new Set(profiles.map((profile) => profile.id)); + let id = ''; + while (id === '' || ids.has(id)) { + id = generateId(16); + } + return id; + } /** * @template [T=unknown] diff --git a/test/options-util.test.js b/test/options-util.test.js index d4ac27f663..3f37f8ee80 100644 --- a/test/options-util.test.js +++ b/test/options-util.test.js @@ -565,6 +565,7 @@ function createOptionsUpdatedTestData1() { return { profiles: [ { + id: 'profile-0', name: 'Default', options: createProfileOptionsUpdatedTestData1(), conditionGroups: [ @@ -644,7 +645,7 @@ function createOptionsUpdatedTestData1() { }, ], profileCurrent: 0, - version: 52, + version: 53, global: { database: { prefixWildcardsSupported: false, diff --git a/types/ext/settings-controller.d.ts b/types/ext/settings-controller.d.ts index d0a6f04d36..e48a05c4db 100644 --- a/types/ext/settings-controller.d.ts +++ b/types/ext/settings-controller.d.ts @@ -29,7 +29,7 @@ export type PageExitPrevention = { type ProfileDictionarySettings = Settings.DictionaryOptions & {index: number}; -export type ProfilesDictionarySettings = ProfileDictionarySettings[] | null; +export type ProfilesDictionarySettings = {[profileId: string]: ProfileDictionarySettings} | null; export type Events = { optionsChanged: { diff --git a/types/ext/settings.d.ts b/types/ext/settings.d.ts index ceffe1e00f..2d03f0f1c3 100644 --- a/types/ext/settings.d.ts +++ b/types/ext/settings.d.ts @@ -70,6 +70,7 @@ export type GlobalDatabaseOptions = { }; export type Profile = { + id: string; name: string; conditionGroups: ProfileConditionGroup[]; options: ProfileOptions;