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[] = []; 34export function _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; 46export function _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 58// Unmangle Menu elements 59const code = 60 spacepack.require.m[ 61 spacepack.findByCode( 62 "Menu API only allows Items and groups of Items as children." 63 )[0].id 64 ].toString(); 65 66let MangledMenu; 67 68const typeRegex = /if\(.\.type===(.)\.(.+?)\).+?type:"(.+?)"/g; 69const typeMap: Record<string, string | undefined> = { 70 checkbox: "MenuCheckboxItem", 71 control: "MenuControlItem", 72 groupstart: "MenuGroup", 73 customitem: "MenuItem", 74 radio: "MenuRadioItem", 75 separator: "MenuSeparator" 76}; 77 78for (const [, modIdent, mangled, type] of code.matchAll(typeRegex)) { 79 if (!MangledMenu) { 80 const modId = code.match(new RegExp(`${modIdent}=.\\((\\d+?)\\)`))![1]; 81 MangledMenu = spacepack.require(modId); 82 } 83 84 const prop = typeMap[type]; 85 if (!prop) continue; 86 module.exports[prop] = MangledMenu[mangled]; 87}