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 items;
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 return items;
28}
29
30let menuProps: any;
31function _saveProps(self: any, el: any) {
32 menuProps = el.props;
33
34 const original = self.props.closeContextMenu;
35 self.props.closeContextMenu = function (...args: any[]) {
36 menuProps = undefined;
37 return original?.apply(this, args);
38 };
39
40 return el;
41}
42
43module.exports = {
44 patches,
45 addItem,
46 _patchMenu,
47 _saveProps
48};
49
50// Unmangle Menu elements
51const code =
52 spacepack.require.m[
53 spacepack.findByCode("Menu API only allows Items and groups of Items as children.")[0].id
54 ].toString();
55
56let MangledMenu;
57
58const typeRegex = /if\(.\.type===(.)\.(.+?)\).+?type:"(.+?)"/g;
59const typeMap: Record<string, string | undefined> = {
60 checkbox: "MenuCheckboxItem",
61 control: "MenuControlItem",
62 groupstart: "MenuGroup",
63 customitem: "MenuItem",
64 radio: "MenuRadioItem",
65 separator: "MenuSeparator"
66};
67
68for (const [, modIdent, mangled, type] of code.matchAll(typeRegex)) {
69 if (!MangledMenu) {
70 const modId = code.match(new RegExp(`${modIdent}=.\\((\\d+?)\\)`))![1];
71 MangledMenu = spacepack.require(modId);
72 }
73
74 const prop = typeMap[type];
75 if (!prop) continue;
76 module.exports[prop] = MangledMenu[mangled];
77}