diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2021-02-27 23:11:41 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-27 23:11:41 -0500 |
commit | 0decabd768ca607d6fe374991806f5ef9b14ff1b (patch) | |
tree | 749cffab9182d232b73461074f9e2c4cf9c63ecd /ext/js/background/backend.js | |
parent | e4a4e5f85f61775ff61ae741d3ba6f28924637cb (diff) |
Improve definition ordering (#1456)
* Update dictionary priority
* Replace dictionaryPriority with dictionaryOrder
* Update tests
Diffstat (limited to 'ext/js/background/backend.js')
-rw-r--r-- | ext/js/background/backend.js | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/ext/js/background/backend.js b/ext/js/background/backend.js index f04006bc..484473c7 100644 --- a/ext/js/background/backend.js +++ b/ext/js/background/backend.js @@ -1872,10 +1872,29 @@ class Backend { } _getTranslatorEnabledDictionaryMap(options) { + const dictionaries = []; + const {dictionaries: optionsDictionaries} = options; + for (const title in optionsDictionaries) { + if (!Object.prototype.hasOwnProperty.call(optionsDictionaries, title)) { continue; } + const dictionary = optionsDictionaries[title]; + if (!dictionary.enabled) { continue; } + dictionaries.push({ + title, + index: dictionaries.length, + priority: dictionary.priority, + allowSecondarySearches: dictionary.allowSecondarySearches + }); + } + + dictionaries.sort((v1, v2) => { + const i = v2.priority - v1.priority; + return i !== 0 ? i : v1.index - v2.index; + }); + const enabledDictionaryMap = new Map(); - for (const [title, {enabled, priority, allowSecondarySearches}] of Object.entries(options.dictionaries)) { - if (!enabled) { continue; } - enabledDictionaryMap.set(title, {priority, allowSecondarySearches}); + for (let i = 0, ii = dictionaries.length; i < ii; ++i) { + const {title, allowSecondarySearches} = dictionaries[i]; + enabledDictionaryMap.set(title, {order: i, allowSecondarySearches}); } return enabledDictionaryMap; } |