2020-07-06 20:24:54 +00:00
|
|
|
function splitArray<T> (arr: T[], predicate: (x: T) => boolean) {
|
2018-05-05 16:37:29 +00:00
|
|
|
const result = arr.reduce((multi, item) => {
|
|
|
|
const current = multi[multi.length - 1];
|
|
|
|
if (predicate(item)) {
|
|
|
|
if (current.length > 0) multi.push([]);
|
|
|
|
} else {
|
|
|
|
current.push(item);
|
|
|
|
}
|
|
|
|
return multi;
|
2020-07-06 20:24:54 +00:00
|
|
|
}, [[]] as T[][]);
|
2018-05-05 16:37:29 +00:00
|
|
|
|
|
|
|
if (result[result.length - 1].length === 0) {
|
|
|
|
return result.slice(0, result.length - 1);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
function joinArrays (arrays: any[][], joinIDs: any[]) {
|
2018-05-05 16:37:29 +00:00
|
|
|
return arrays.reduce((joined, arr, i) => {
|
|
|
|
if (i > 0 && arr.length) {
|
2018-10-19 20:09:40 +00:00
|
|
|
if (joinIDs.length > 0) {
|
|
|
|
joined.push(joinIDs[0]);
|
|
|
|
joinIDs.splice(0, 1);
|
|
|
|
} else {
|
|
|
|
joined.push({ type: 'separator' });
|
|
|
|
}
|
2018-05-05 16:37:29 +00:00
|
|
|
}
|
|
|
|
return joined.concat(arr);
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
function pushOntoMultiMap<K, V> (map: Map<K, V[]>, key: K, value: V) {
|
2018-05-05 16:37:29 +00:00
|
|
|
if (!map.has(key)) {
|
|
|
|
map.set(key, []);
|
|
|
|
}
|
2020-07-06 20:24:54 +00:00
|
|
|
map.get(key)!.push(value);
|
2018-05-05 16:37:29 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
function indexOfGroupContainingID<T> (groups: {id?: T}[][], id: T, ignoreGroup: {id?: T}[]) {
|
2018-05-05 16:37:29 +00:00
|
|
|
return groups.findIndex(
|
|
|
|
candidateGroup =>
|
|
|
|
candidateGroup !== ignoreGroup &&
|
|
|
|
candidateGroup.some(
|
|
|
|
candidateItem => candidateItem.id === id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort nodes topologically using a depth-first approach. Encountered cycles
|
|
|
|
// are broken.
|
2020-07-06 20:24:54 +00:00
|
|
|
function sortTopologically<T> (originalOrder: T[], edgesById: Map<T, T[]>) {
|
|
|
|
const sorted = [] as T[];
|
|
|
|
const marked = new Set<T>();
|
2018-05-05 16:37:29 +00:00
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
const visit = (mark: T) => {
|
2018-05-05 16:37:29 +00:00
|
|
|
if (marked.has(mark)) return;
|
|
|
|
marked.add(mark);
|
|
|
|
const edges = edgesById.get(mark);
|
|
|
|
if (edges != null) {
|
|
|
|
edges.forEach(visit);
|
|
|
|
}
|
|
|
|
sorted.push(mark);
|
|
|
|
};
|
|
|
|
|
|
|
|
originalOrder.forEach(visit);
|
|
|
|
return sorted;
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
function attemptToMergeAGroup<T> (groups: {before?: T[], after?: T[], id?: T}[][]) {
|
2018-05-05 16:37:29 +00:00
|
|
|
for (let i = 0; i < groups.length; i++) {
|
|
|
|
const group = groups[i];
|
|
|
|
for (const item of group) {
|
|
|
|
const toIDs = [...(item.before || []), ...(item.after || [])];
|
|
|
|
for (const id of toIDs) {
|
|
|
|
const index = indexOfGroupContainingID(groups, id, group);
|
|
|
|
if (index === -1) continue;
|
|
|
|
const mergeTarget = groups[index];
|
|
|
|
|
|
|
|
mergeTarget.push(...group);
|
|
|
|
groups.splice(i, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
function mergeGroups<T> (groups: {before?: T[], after?: T[], id?: T}[][]) {
|
2018-05-05 16:37:29 +00:00
|
|
|
let merged = true;
|
|
|
|
while (merged) {
|
|
|
|
merged = attemptToMergeAGroup(groups);
|
|
|
|
}
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
function sortItemsInGroup<T> (group: {before?: T[], after?: T[], id?: T}[]) {
|
2018-05-05 16:37:29 +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 => {
|
|
|
|
const to = idToIndex.get(toID);
|
|
|
|
if (to != null) {
|
|
|
|
pushOntoMultiMap(edges, to, i);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (item.after) {
|
|
|
|
item.after.forEach(toID => {
|
|
|
|
const to = idToIndex.get(toID);
|
|
|
|
if (to != null) {
|
|
|
|
pushOntoMultiMap(edges, i, to);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const sortedNodes = sortTopologically(originalOrder, edges);
|
|
|
|
return sortedNodes.map(i => group[i]);
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
function findEdgesInGroup<T> (groups: {beforeGroupContaining?: T[], afterGroupContaining?: T[], id?: T}[][], i: number, edges: Map<any, any>) {
|
2018-05-05 16:37:29 +00:00
|
|
|
const group = groups[i];
|
|
|
|
for (const item of group) {
|
|
|
|
if (item.beforeGroupContaining) {
|
|
|
|
for (const id of item.beforeGroupContaining) {
|
|
|
|
const to = indexOfGroupContainingID(groups, id, group);
|
|
|
|
if (to !== -1) {
|
|
|
|
pushOntoMultiMap(edges, to, i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (item.afterGroupContaining) {
|
|
|
|
for (const id of item.afterGroupContaining) {
|
|
|
|
const to = indexOfGroupContainingID(groups, id, group);
|
|
|
|
if (to !== -1) {
|
|
|
|
pushOntoMultiMap(edges, i, to);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-06 20:24:54 +00:00
|
|
|
function sortGroups<T> (groups: {id?: T}[][]) {
|
2018-05-05 16:37:29 +00:00
|
|
|
const originalOrder = groups.map((item, i) => i);
|
|
|
|
const edges = new Map();
|
|
|
|
|
|
|
|
for (let i = 0; i < groups.length; i++) {
|
|
|
|
findEdgesInGroup(groups, i, edges);
|
|
|
|
}
|
|
|
|
|
|
|
|
const sortedGroupIndexes = sortTopologically(originalOrder, edges);
|
|
|
|
return sortedGroupIndexes.map(i => groups[i]);
|
|
|
|
}
|
|
|
|
|
2020-09-23 17:39:08 +00:00
|
|
|
export function sortMenuItems (menuItems: (Electron.MenuItemConstructorOptions | Electron.MenuItem)[]) {
|
|
|
|
const isSeparator = (i: Electron.MenuItemConstructorOptions | Electron.MenuItem) => {
|
|
|
|
const opts = i as Electron.MenuItemConstructorOptions;
|
|
|
|
return i.type === 'separator' && !opts.before && !opts.after && !opts.beforeGroupContaining && !opts.afterGroupContaining;
|
|
|
|
};
|
|
|
|
const separators = menuItems.filter(isSeparator);
|
2018-05-05 16:37:29 +00:00
|
|
|
|
|
|
|
// Split the items into their implicit groups based upon separators.
|
|
|
|
const groups = splitArray(menuItems, isSeparator);
|
|
|
|
const mergedGroups = mergeGroups(groups);
|
|
|
|
const mergedGroupsWithSortedItems = mergedGroups.map(sortItemsInGroup);
|
|
|
|
const sortedGroups = sortGroups(mergedGroupsWithSortedItems);
|
|
|
|
|
2018-10-19 20:09:40 +00:00
|
|
|
const joined = joinArrays(sortedGroups, separators);
|
2018-05-05 16:37:29 +00:00
|
|
|
return joined;
|
|
|
|
}
|