Merge pull request #158 from moonlight-mod/kasimir/ctx-anchor-regex
@@ -11,12 +11,14 @@type Patch = {navId: string;item: React.FC<any>;- anchorId: string;+ anchor: string | RegExp;before: boolean;};-function addItem<T = any>(navId: string, item: React.FC<T>, anchorId: string, before = false) {- patches.push({ navId, item, anchorId, before });+function addItem<T = any>(navId: string, item: React.FC<T>, anchor: string | RegExp, before = false) {+ if (anchor instanceof RegExp && anchor.flags.includes("g"))+ throw new Error("anchor regular expression should not be global");+ patches.push({ navId, item, anchor, before });}const patches: Patch[] = [];@@ -25,7 +27,9 @@ const matches = patches.filter((p) => p.navId === props.navId);if (!matches.length) return items;for (const patch of matches) {- const idx = items.findIndex((i) => i.key === patch.anchorId);+ const idx = items.findIndex((i) =>+ typeof patch.anchor === "string" ? i.key === patch.anchor : patch.anchor.test(i.key!)+ );if (idx === -1) continue;items.splice(idx + 1 - +patch.before, 0, ...parser(patch.item(menuProps) as ReturnType));}
@@ -14,10 +14,10 @@ /*** Registers a new context menu item for a given context menu type.* @param navId The navigation ID for the target context menu (e.g. "user-context", "message")* @param item A React component- * @param anchorId An existing item's ID to anchor the new item to+ * @param anchor An existing item's ID to anchor the new item to* @param before Whether to insert the new item before the anchor item*/- addItem: (navId: string, item: React.FC<any>, anchorId: string, before?: boolean) => void;+ addItem: (navId: string, item: React.FC<any>, anchor: string | RegExp, before?: boolean) => void;MenuCheckboxItem: MenuCheckboxItem;MenuControlItem: MenuControlItem;