electron/lib/browser/api/menu-utils.js

177 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-03-20 20:28:31 +00:00
'use strict';
function splitArray (arr, predicate) {
const result = arr.reduce((multi, item) => {
2020-03-20 20:28:31 +00:00
const current = multi[multi.length - 1];
if (predicate(item)) {
2020-03-20 20:28:31 +00:00
if (current.length > 0) multi.push([]);
} else {
2020-03-20 20:28:31 +00:00
current.push(item);
}
2020-03-20 20:28:31 +00:00
return multi;
}, [[]]);
if (result[result.length - 1].length === 0) {
2020-03-20 20:28:31 +00:00
return result.slice(0, result.length - 1);
}
2020-03-20 20:28:31 +00:00
return result;
}
function joinArrays (arrays, joinIDs) {
return arrays.reduce((joined, arr, i) => {
if (i > 0 && arr.length) {
if (joinIDs.length > 0) {
2020-03-20 20:28:31 +00:00
joined.push(joinIDs[0]);
joinIDs.splice(0, 1);
} else {
2020-03-20 20:28:31 +00:00
joined.push({ type: 'separator' });
}
}
2020-03-20 20:28:31 +00:00
return joined.concat(arr);
}, []);
}
function pushOntoMultiMap (map, key, value) {
if (!map.has(key)) {
2020-03-20 20:28:31 +00:00
map.set(key, []);
}
2020-03-20 20:28:31 +00:00
map.get(key).push(value);
}
function indexOfGroupContainingID (groups, id, ignoreGroup) {
return groups.findIndex(
candidateGroup =>
candidateGroup !== ignoreGroup &&
candidateGroup.some(
candidateItem => candidateItem.id === id
)
2020-03-20 20:28:31 +00:00
);
}
// Sort nodes topologically using a depth-first approach. Encountered cycles
// are broken.
function sortTopologically (originalOrder, edgesById) {
2020-03-20 20:28:31 +00:00
const sorted = [];
const marked = new Set();
const visit = (mark) => {
2020-03-20 20:28:31 +00:00
if (marked.has(mark)) return;
marked.add(mark);
const edges = edgesById.get(mark);
if (edges != null) {
2020-03-20 20:28:31 +00:00
edges.forEach(visit);
}
2020-03-20 20:28:31 +00:00
sorted.push(mark);
};
2020-03-20 20:28:31 +00:00
originalOrder.forEach(visit);
return sorted;
}
function attemptToMergeAGroup (groups) {
for (let i = 0; i < groups.length; i++) {
2020-03-20 20:28:31 +00:00
const group = groups[i];
for (const item of group) {
2020-03-20 20:28:31 +00:00
const toIDs = [...(item.before || []), ...(item.after || [])];
for (const id of toIDs) {
2020-03-20 20:28:31 +00:00
const index = indexOfGroupContainingID(groups, id, group);
if (index === -1) continue;
const mergeTarget = groups[index];
2020-03-20 20:28:31 +00:00
mergeTarget.push(...group);
groups.splice(i, 1);
return true;
}
}
}
2020-03-20 20:28:31 +00:00
return false;
}
function mergeGroups (groups) {
2020-03-20 20:28:31 +00:00
let merged = true;
while (merged) {
2020-03-20 20:28:31 +00:00
merged = attemptToMergeAGroup(groups);
}
2020-03-20 20:28:31 +00:00
return groups;
}
function sortItemsInGroup (group) {
2020-03-20 20:28:31 +00:00
const originalOrder = group.map((node, i) => i);
const edges = new Map();
const idToIndex = new Map(group.map((item, i) => [item.id, i]));
group.forEach((item, i) => {
if (item.before) {
item.before.forEach(toID => {
2020-03-20 20:28:31 +00:00
const to = idToIndex.get(toID);
if (to != null) {
2020-03-20 20:28:31 +00:00
pushOntoMultiMap(edges, to, i);
}
2020-03-20 20:28:31 +00:00
});
}
if (item.after) {
item.after.forEach(toID => {
2020-03-20 20:28:31 +00:00
const to = idToIndex.get(toID);
if (to != null) {
2020-03-20 20:28:31 +00:00
pushOntoMultiMap(edges, i, to);
}
2020-03-20 20:28:31 +00:00
});
}
2020-03-20 20:28:31 +00:00
});
2020-03-20 20:28:31 +00:00
const sortedNodes = sortTopologically(originalOrder, edges);
return sortedNodes.map(i => group[i]);
}
function findEdgesInGroup (groups, i, edges) {
2020-03-20 20:28:31 +00:00
const group = groups[i];
for (const item of group) {
if (item.beforeGroupContaining) {
for (const id of item.beforeGroupContaining) {
2020-03-20 20:28:31 +00:00
const to = indexOfGroupContainingID(groups, id, group);
if (to !== -1) {
2020-03-20 20:28:31 +00:00
pushOntoMultiMap(edges, to, i);
return;
}
}
}
if (item.afterGroupContaining) {
for (const id of item.afterGroupContaining) {
2020-03-20 20:28:31 +00:00
const to = indexOfGroupContainingID(groups, id, group);
if (to !== -1) {
2020-03-20 20:28:31 +00:00
pushOntoMultiMap(edges, i, to);
return;
}
}
}
}
}
function sortGroups (groups) {
2020-03-20 20:28:31 +00:00
const originalOrder = groups.map((item, i) => i);
const edges = new Map();
for (let i = 0; i < groups.length; i++) {
2020-03-20 20:28:31 +00:00
findEdgesInGroup(groups, i, edges);
}
2020-03-20 20:28:31 +00:00
const sortedGroupIndexes = sortTopologically(originalOrder, edges);
return sortedGroupIndexes.map(i => groups[i]);
}
function sortMenuItems (menuItems) {
2020-03-20 20:28:31 +00:00
const isSeparator = (item) => item.type === 'separator';
const separators = menuItems.filter(i => i.type === 'separator');
// Split the items into their implicit groups based upon separators.
2020-03-20 20:28:31 +00:00
const groups = splitArray(menuItems, isSeparator);
const mergedGroups = mergeGroups(groups);
const mergedGroupsWithSortedItems = mergedGroups.map(sortItemsInGroup);
const sortedGroups = sortGroups(mergedGroupsWithSortedItems);
2020-03-20 20:28:31 +00:00
const joined = joinArrays(sortedGroups, separators);
return joined;
}
2020-03-20 20:28:31 +00:00
module.exports = { sortMenuItems };