({ children, menus, pluginContext }: {
menus: IPublicTypeContextMenuAction[];
children: React.ReactElement[] | React.ReactElement;
pluginContext: IPublicModelPluginContext;
})
| 4 | import React, { useCallback } from 'react'; |
| 5 | |
| 6 | export function ContextMenu({ children, menus, pluginContext }: { |
| 7 | menus: IPublicTypeContextMenuAction[]; |
| 8 | children: React.ReactElement[] | React.ReactElement; |
| 9 | pluginContext: IPublicModelPluginContext; |
| 10 | }): React.ReactElement<any, string | React.JSXElementConstructor<any>> { |
| 11 | const handleContextMenu = useCallback((event: React.MouseEvent) => { |
| 12 | event.preventDefault(); |
| 13 | event.stopPropagation(); |
| 14 | |
| 15 | let destroyFn: Function | undefined; |
| 16 | const destroy = () => { |
| 17 | destroyFn?.(); |
| 18 | }; |
| 19 | const children: React.ReactNode[] = parseContextMenuAsReactNode(parseContextMenuProperties(menus, { |
| 20 | destroy, |
| 21 | pluginContext, |
| 22 | }), { pluginContext }); |
| 23 | |
| 24 | if (!children?.length) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | destroyFn = createContextMenu(children, { event }); |
| 29 | }, [menus]); |
| 30 | |
| 31 | if (!engineConfig.get('enableContextMenu')) { |
| 32 | return ( |
| 33 | <>{ children }</> |
| 34 | ); |
| 35 | } |
| 36 | |
| 37 | if (!menus) { |
| 38 | return ( |
| 39 | <>{ children }</> |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | // 克隆 children 并添加 onContextMenu 事件处理器 |
| 44 | const childrenWithContextMenu = React.Children.map(children, (child) => |
| 45 | React.cloneElement( |
| 46 | child, |
| 47 | { onContextMenu: handleContextMenu }, |
| 48 | )); |
| 49 | |
| 50 | return ( |
| 51 | <>{childrenWithContextMenu}</> |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | ContextMenu.create = (pluginContext: IPublicModelPluginContext, menus: IPublicTypeContextMenuAction[], event: MouseEvent) => { |
| 56 | event.preventDefault(); |
nothing calls this directly
no test coverage detected
searching dependent graphs…