this repo has no description

contextMenu: allow regex anchor

Changed files
+10 -6
packages
core-extensions
src
contextMenu
webpackModules
types
src
coreExtensions
+8 -4
packages/core-extensions/src/contextMenu/webpackModules/contextMenu.ts
···
type Patch = {
navId: string;
item: React.FC<any>;
-
anchorId: string;
before: boolean;
};
-
function addItem<T = any>(navId: string, item: React.FC<T>, anchorId: string, before = false) {
-
patches.push({ navId, item, anchorId, before });
}
const patches: Patch[] = [];
···
if (!matches.length) return items;
for (const patch of matches) {
-
const idx = items.findIndex((i) => i.key === patch.anchorId);
if (idx === -1) continue;
items.splice(idx + 1 - +patch.before, 0, ...parser(patch.item(menuProps) as ReturnType));
}
···
type Patch = {
navId: string;
item: React.FC<any>;
+
anchor: string | RegExp;
before: boolean;
};
+
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[] = [];
···
if (!matches.length) return items;
for (const patch of matches) {
+
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));
}
+2 -2
packages/types/src/coreExtensions/contextMenu.ts
···
* 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 before Whether to insert the new item before the anchor item
*/
-
addItem: (navId: string, item: React.FC<any>, anchorId: string, before?: boolean) => void;
MenuCheckboxItem: MenuCheckboxItem;
MenuControlItem: MenuControlItem;
···
* 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 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>, anchor: string | RegExp, before?: boolean) => void;
MenuCheckboxItem: MenuCheckboxItem;
MenuControlItem: MenuControlItem;