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