Remove extra menu separators (#11827)
* add function to remove leading/trailing separators * change const name for clarity * add spec to check filtered separators * clean method and add edge case spec per review
This commit is contained in:
parent
e6a5990b95
commit
5240352326
2 changed files with 73 additions and 12 deletions
|
@ -184,11 +184,13 @@ Menu.buildFromTemplate = function (template) {
|
|||
}
|
||||
|
||||
const menu = new Menu()
|
||||
const filtered = removeExtraSeparators(template)
|
||||
|
||||
const positioned = []
|
||||
let idx = 0
|
||||
|
||||
// sort template by position
|
||||
template.forEach(item => {
|
||||
filtered.forEach(item => {
|
||||
idx = (item.position) ? indexToInsertByPosition(positioned, item.position) : idx += 1
|
||||
positioned.splice(idx, 0, item)
|
||||
})
|
||||
|
@ -262,6 +264,17 @@ function indexToInsertByPosition (items, position) {
|
|||
return (query in queries) ? queries[query](idx) : idx
|
||||
}
|
||||
|
||||
function removeExtraSeparators (items) {
|
||||
// remove invisible items
|
||||
let ret = items.filter(e => e.visible !== false)
|
||||
|
||||
// fold adjacent separators together
|
||||
ret = ret.filter((e, idx, arr) => e.type !== 'separator' || idx === 0 || arr[idx - 1].type !== 'separator')
|
||||
|
||||
// remove edge separators
|
||||
return ret.filter((e, idx, arr) => e.type !== 'separator' || (idx !== 0 && idx !== arr.length - 1))
|
||||
}
|
||||
|
||||
function insertItemByType (item, pos) {
|
||||
const types = {
|
||||
normal: () => this.insertItem(pos, item.commandId, item.label),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue