this repo has no description
1import { 2 InternalItem, 3 MenuElement, 4 MenuProps 5} from "@moonlight-mod/types/coreExtensions/contextMenu"; 6import spacepack from "@moonlight-mod/wp/spacepack_spacepack"; 7import parser from "@moonlight-mod/wp/contextMenu_evilMenu"; 8 9type Patch = { 10 navId: string; 11 item: ( 12 props: any 13 ) => 14 | React.ReactComponentElement<MenuElement> 15 | React.ReactComponentElement<MenuElement>[]; 16 anchorId: string; 17 before: boolean; 18}; 19 20export function addItem<T>( 21 navId: string, 22 item: ( 23 props: T 24 ) => 25 | React.ReactComponentElement<MenuElement> 26 | React.ReactComponentElement<MenuElement>[], 27 anchorId: string, 28 before = false 29) { 30 patches.push({ navId, item, anchorId, before }); 31} 32 33export const patches: Patch[] = []; 34function _patchMenu(props: MenuProps, items: InternalItem[]) { 35 const matches = patches.filter((p) => p.navId === props.navId); 36 if (!matches.length) return; 37 38 for (const patch of matches) { 39 const idx = items.findIndex((i) => i.key === patch.anchorId); 40 if (idx === -1) continue; 41 items.splice(idx + 1 - +patch.before, 0, ...parser(patch.item(menuProps))); 42 } 43} 44 45let menuProps: any; 46function _saveProps(self: any, el: any) { 47 menuProps = el.props; 48 49 const original = self.props.closeContextMenu; 50 self.props.closeContextMenu = function (...args: any[]) { 51 menuProps = undefined; 52 return original?.apply(this, args); 53 }; 54 55 return el; 56} 57 58module.exports = { 59 addItem, 60 _patchMenu, 61 _saveProps 62}; 63 64// Unmangle Menu elements 65const code = 66 spacepack.require.m[ 67 spacepack.findByCode( 68 "Menu API only allows Items and groups of Items as children." 69 )[0].id 70 ].toString(); 71 72let MangledMenu; 73 74const typeRegex = /if\(.\.type===(.)\.(.+?)\).+?type:"(.+?)"/g; 75const typeMap: Record<string, string | undefined> = { 76 checkbox: "MenuCheckboxItem", 77 control: "MenuControlItem", 78 groupstart: "MenuGroup", 79 customitem: "MenuItem", 80 radio: "MenuRadioItem", 81 separator: "MenuSeparator" 82}; 83 84for (const [, modIdent, mangled, type] of code.matchAll(typeRegex)) { 85 if (!MangledMenu) { 86 const modId = code.match(new RegExp(`${modIdent}=.\\((\\d+?)\\)`))![1]; 87 MangledMenu = spacepack.require(modId); 88 } 89 90 const prop = typeMap[type]; 91 if (!prop) continue; 92 module.exports[prop] = MangledMenu[mangled]; 93}