Skip to content

Commit

Permalink
Add profile id
Browse files Browse the repository at this point in the history
  • Loading branch information
khaitruong922 committed Nov 2, 2024
1 parent 45a29a4 commit b33bc94
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 9 deletions.
11 changes: 11 additions & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<chrome.tabs.Tab>}
Expand Down
7 changes: 4 additions & 3 deletions ext/js/pages/settings/dictionary-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 3 additions & 3 deletions ext/js/pages/settings/dictionary-import-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 14 additions & 1 deletion ext/js/pages/settings/profile-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -594,6 +595,18 @@ export class ProfileController {
}
}
}
/**
* @param {import('settings').Profile[]} profiles
* @returns {string}
*/
_createId(profiles) {

Check failure on line 602 in ext/js/pages/settings/profile-controller.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Expected blank line between class members
const ids = new Set(profiles.map((profile) => profile.id));
let id = '';
while (id === '' || ids.has(id)) {
id = generateId(16);
}
return id;
}

/**
* @template [T=unknown]
Expand Down
3 changes: 2 additions & 1 deletion test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ function createOptionsUpdatedTestData1() {
return {
profiles: [
{
id: 'profile-0',
name: 'Default',
options: createProfileOptionsUpdatedTestData1(),
conditionGroups: [
Expand Down Expand Up @@ -644,7 +645,7 @@ function createOptionsUpdatedTestData1() {
},
],
profileCurrent: 0,
version: 52,
version: 53,
global: {
database: {
prefixWildcardsSupported: false,
Expand Down
2 changes: 1 addition & 1 deletion types/ext/settings-controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions types/ext/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type GlobalDatabaseOptions = {
};

export type Profile = {
id: string;
name: string;
conditionGroups: ProfileConditionGroup[];
options: ProfileOptions;
Expand Down

0 comments on commit b33bc94

Please sign in to comment.