menu-utils.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. function splitArray<T> (arr: T[], predicate: (x: T) => boolean) {
  2. const result = arr.reduce((multi, item) => {
  3. const current = multi[multi.length - 1];
  4. if (predicate(item)) {
  5. if (current.length > 0) multi.push([]);
  6. } else {
  7. current.push(item);
  8. }
  9. return multi;
  10. }, [[]] as T[][]);
  11. if (result[result.length - 1].length === 0) {
  12. return result.slice(0, result.length - 1);
  13. }
  14. return result;
  15. }
  16. function joinArrays (arrays: any[][], joinIDs: any[]) {
  17. return arrays.reduce((joined, arr, i) => {
  18. if (i > 0 && arr.length) {
  19. if (joinIDs.length > 0) {
  20. joined.push(joinIDs[0]);
  21. joinIDs.splice(0, 1);
  22. } else {
  23. joined.push({ type: 'separator' });
  24. }
  25. }
  26. return joined.concat(arr);
  27. }, []);
  28. }
  29. function pushOntoMultiMap<K, V> (map: Map<K, V[]>, key: K, value: V) {
  30. if (!map.has(key)) {
  31. map.set(key, []);
  32. }
  33. map.get(key)!.push(value);
  34. }
  35. function indexOfGroupContainingID<T> (groups: {id?: T}[][], id: T, ignoreGroup: {id?: T}[]) {
  36. return groups.findIndex(
  37. candidateGroup =>
  38. candidateGroup !== ignoreGroup &&
  39. candidateGroup.some(
  40. candidateItem => candidateItem.id === id
  41. )
  42. );
  43. }
  44. // Sort nodes topologically using a depth-first approach. Encountered cycles
  45. // are broken.
  46. function sortTopologically<T> (originalOrder: T[], edgesById: Map<T, T[]>) {
  47. const sorted = [] as T[];
  48. const marked = new Set<T>();
  49. const visit = (mark: T) => {
  50. if (marked.has(mark)) return;
  51. marked.add(mark);
  52. const edges = edgesById.get(mark);
  53. if (edges != null) {
  54. edges.forEach(visit);
  55. }
  56. sorted.push(mark);
  57. };
  58. originalOrder.forEach(visit);
  59. return sorted;
  60. }
  61. function attemptToMergeAGroup<T> (groups: {before?: T[], after?: T[], id?: T}[][]) {
  62. for (let i = 0; i < groups.length; i++) {
  63. const group = groups[i];
  64. for (const item of group) {
  65. const toIDs = [...(item.before || []), ...(item.after || [])];
  66. for (const id of toIDs) {
  67. const index = indexOfGroupContainingID(groups, id, group);
  68. if (index === -1) continue;
  69. const mergeTarget = groups[index];
  70. mergeTarget.push(...group);
  71. groups.splice(i, 1);
  72. return true;
  73. }
  74. }
  75. }
  76. return false;
  77. }
  78. function mergeGroups<T> (groups: {before?: T[], after?: T[], id?: T}[][]) {
  79. let merged = true;
  80. while (merged) {
  81. merged = attemptToMergeAGroup(groups);
  82. }
  83. return groups;
  84. }
  85. function sortItemsInGroup<T> (group: {before?: T[], after?: T[], id?: T}[]) {
  86. const originalOrder = group.map((node, i) => i);
  87. const edges = new Map();
  88. const idToIndex = new Map(group.map((item, i) => [item.id, i]));
  89. group.forEach((item, i) => {
  90. if (item.before) {
  91. item.before.forEach(toID => {
  92. const to = idToIndex.get(toID);
  93. if (to != null) {
  94. pushOntoMultiMap(edges, to, i);
  95. }
  96. });
  97. }
  98. if (item.after) {
  99. item.after.forEach(toID => {
  100. const to = idToIndex.get(toID);
  101. if (to != null) {
  102. pushOntoMultiMap(edges, i, to);
  103. }
  104. });
  105. }
  106. });
  107. const sortedNodes = sortTopologically(originalOrder, edges);
  108. return sortedNodes.map(i => group[i]);
  109. }
  110. function findEdgesInGroup<T> (groups: {beforeGroupContaining?: T[], afterGroupContaining?: T[], id?: T}[][], i: number, edges: Map<any, any>) {
  111. const group = groups[i];
  112. for (const item of group) {
  113. if (item.beforeGroupContaining) {
  114. for (const id of item.beforeGroupContaining) {
  115. const to = indexOfGroupContainingID(groups, id, group);
  116. if (to !== -1) {
  117. pushOntoMultiMap(edges, to, i);
  118. return;
  119. }
  120. }
  121. }
  122. if (item.afterGroupContaining) {
  123. for (const id of item.afterGroupContaining) {
  124. const to = indexOfGroupContainingID(groups, id, group);
  125. if (to !== -1) {
  126. pushOntoMultiMap(edges, i, to);
  127. return;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. function sortGroups<T> (groups: {id?: T}[][]) {
  134. const originalOrder = groups.map((item, i) => i);
  135. const edges = new Map();
  136. for (let i = 0; i < groups.length; i++) {
  137. findEdgesInGroup(groups, i, edges);
  138. }
  139. const sortedGroupIndexes = sortTopologically(originalOrder, edges);
  140. return sortedGroupIndexes.map(i => groups[i]);
  141. }
  142. export function sortMenuItems (menuItems: (Electron.MenuItemConstructorOptions | Electron.MenuItem)[]) {
  143. const isSeparator = (i: Electron.MenuItemConstructorOptions | Electron.MenuItem) => {
  144. const opts = i as Electron.MenuItemConstructorOptions;
  145. return i.type === 'separator' && !opts.before && !opts.after && !opts.beforeGroupContaining && !opts.afterGroupContaining;
  146. };
  147. const separators = menuItems.filter(isSeparator);
  148. // Split the items into their implicit groups based upon separators.
  149. const groups = splitArray(menuItems, isSeparator);
  150. const mergedGroups = mergeGroups(groups);
  151. const mergedGroupsWithSortedItems = mergedGroups.map(sortItemsInGroup);
  152. const sortedGroups = sortGroups(mergedGroupsWithSortedItems);
  153. const joined = joinArrays(sortedGroups, separators);
  154. return joined;
  155. }