Skip to content

Commit

Permalink
Able to edit dictionary alias on settings
Browse files Browse the repository at this point in the history
  • Loading branch information
khaitruong922 committed Jul 25, 2024
1 parent 4a93130 commit 142098a
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ext/css/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions ext/data/schemas/options-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@
"type": "object",
"required": [
"name",
"alias",
"priority",
"enabled",
"allowSecondarySearches",
Expand All @@ -838,6 +839,10 @@
"type": "string",
"default": ""
},
"alias": {
"type": "string",
"default": ""
},
"priority": {
"type": "number",
"default": 0
Expand Down
2 changes: 2 additions & 0 deletions ext/js/background/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions ext/js/data/options-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<chrome.tabs.Tab>}
Expand Down
27 changes: 24 additions & 3 deletions ext/js/pages/settings/dictionary-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand All @@ -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`;
Expand Down Expand Up @@ -180,6 +185,21 @@ class DictionaryEntry {
}
}

/** */
_onAliasInput() {
if (this._aliasNode.textContent === '') this._aliasNode.textContent = this.dictionaryTitle;

Check failure on line 190 in ext/js/pages/settings/dictionary-controller.js

View workflow job for this annotation

GitHub Actions / Static Analysis

Expected { after 'if' condition
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
*/
Expand Down Expand Up @@ -600,6 +620,7 @@ export class DictionaryController {
static createDefaultDictionarySettings(name, enabled, styles) {
return {
name,
alias: name,
priority: 0,
enabled,
allowSecondarySearches: false,
Expand Down
3 changes: 2 additions & 1 deletion ext/templates-settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
<label class="toggle dictionary-item-enabled-toggle-container"><input type="checkbox" class="dictionary-enabled"><span class="toggle-body"><span class="toggle-track"></span><span class="toggle-knob"></span></span></label>
<div class="dictionary-item-title-container">
<span>
<strong class="dictionary-title"></strong> <span class="light dictionary-revision"></span>
<strong contenteditable="true" spellcheck="false" class="dictionary-alias"></strong> <span class="light dictionary-revision"></span>
</span>
<input type="text" class="dictionary-alias-hidden">
<button type="button" class="dictionary-outdated-button" hidden>
<div class="badge warning-badge"><span class="icon" data-icon="exclamation-point-short"></span></div>
</button>
Expand Down
1 change: 1 addition & 0 deletions test/data/translator-test-inputs.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"${title}",
{
"index": 0,
"alias": "${title}",
"priority": 0,
"allowSecondarySearches": false,
"partsOfSpeechFilter": true,
Expand Down
4 changes: 2 additions & 2 deletions test/database.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/options-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ function createProfileOptionsTestData1() {
},
dictionaries: {
'Test Dictionary': {
alias: 'Test Dictionary',
priority: 0,
enabled: true,
allowSecondarySearches: false,
Expand Down Expand Up @@ -438,6 +439,7 @@ function createProfileOptionsUpdatedTestData1() {
dictionaries: [
{
name: 'Test Dictionary',
alias: 'Test Dictionary',
priority: 0,
enabled: true,
allowSecondarySearches: false,
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 @@ -263,6 +263,7 @@ export type DictionariesOptions = DictionaryOptions[];

export type DictionaryOptions = {
name: string;
alias: string;
priority: number;
enabled: boolean;
allowSecondarySearches: boolean;
Expand Down
4 changes: 4 additions & 0 deletions types/ext/translation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 142098a

Please sign in to comment.