forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcontext-menu.tsx
More file actions
85 lines (73 loc) · 2 KB
/
Copy pathcontext-menu.tsx
File metadata and controls
85 lines (73 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import {
createContextMenu,
parseContextMenuAsReactNode,
parseContextMenuProperties,
} from '@felce/lowcode-utils';
import { engineConfig } from '@felce/lowcode-editor-core';
import { IPublicModelPluginContext, IPublicTypeContextMenuAction } from '@felce/lowcode-types';
import React, { useCallback } from 'react';
export function ContextMenu({
children,
menus,
pluginContext,
}: {
menus: IPublicTypeContextMenuAction[];
children: React.ReactElement[] | React.ReactElement;
pluginContext: IPublicModelPluginContext;
}): React.ReactElement<any, string | React.JSXElementConstructor<any>> {
const handleContextMenu = useCallback(
(event: React.MouseEvent) => {
event.preventDefault();
event.stopPropagation();
let destroyFn: Function | undefined;
const destroy = () => {
destroyFn?.();
};
const children: React.ReactNode[] = parseContextMenuAsReactNode(
parseContextMenuProperties(menus, {
destroy,
pluginContext,
}),
{ pluginContext },
);
if (!children?.length) {
return;
}
destroyFn = createContextMenu(children, { event });
},
[menus],
);
if (!engineConfig.get('enableContextMenu')) {
return <>{children}</>;
}
if (!menus) {
return <>{children}</>;
}
// 克隆 children 并添加 onContextMenu 事件处理器
const childrenWithContextMenu = React.Children.map(children, (child) =>
React.cloneElement(child, { onContextMenu: handleContextMenu }),
);
return <>{childrenWithContextMenu}</>;
}
ContextMenu.create = (
pluginContext: IPublicModelPluginContext,
menus: IPublicTypeContextMenuAction[],
event: MouseEvent,
) => {
event.preventDefault();
event.stopPropagation();
const children: React.ReactNode[] = parseContextMenuAsReactNode(
parseContextMenuProperties(menus, {
pluginContext,
}),
{
pluginContext,
},
);
if (!children?.length) {
return;
}
return createContextMenu(children, {
event,
});
};