@logicflow/core 2.2.1 → 2.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/README.md +32 -0
  2. package/dist/docs/api/logicflow-constructor/index.en.md +106 -0
  3. package/dist/docs/api/logicflow-constructor/index.zh.md +106 -0
  4. package/dist/docs/api/logicflow-constructor/use.en.md +61 -0
  5. package/dist/docs/api/logicflow-constructor/use.zh.md +61 -0
  6. package/dist/docs/api/logicflow-instance/canvas.en.md +197 -0
  7. package/dist/docs/api/logicflow-instance/canvas.zh.md +199 -0
  8. package/dist/docs/api/logicflow-instance/edge.en.md +273 -0
  9. package/dist/docs/api/logicflow-instance/edge.zh.md +273 -0
  10. package/dist/docs/api/logicflow-instance/edit-config.en.md +59 -0
  11. package/dist/docs/api/logicflow-instance/edit-config.zh.md +59 -0
  12. package/dist/docs/api/logicflow-instance/element.en.md +375 -0
  13. package/dist/docs/api/logicflow-instance/element.zh.md +379 -0
  14. package/dist/docs/api/logicflow-instance/event.en.md +326 -0
  15. package/dist/docs/api/logicflow-instance/event.zh.md +406 -0
  16. package/dist/docs/api/logicflow-instance/history.en.md +38 -0
  17. package/dist/docs/api/logicflow-instance/history.zh.md +38 -0
  18. package/dist/docs/api/logicflow-instance/index.en.md +41 -0
  19. package/dist/docs/api/logicflow-instance/index.zh.md +41 -0
  20. package/dist/docs/api/logicflow-instance/node.en.md +308 -0
  21. package/dist/docs/api/logicflow-instance/node.zh.md +308 -0
  22. package/dist/docs/api/logicflow-instance/register.en.md +76 -0
  23. package/dist/docs/api/logicflow-instance/register.zh.md +76 -0
  24. package/dist/docs/api/logicflow-instance/render-and-data.en.md +179 -0
  25. package/dist/docs/api/logicflow-instance/render-and-data.zh.md +181 -0
  26. package/dist/docs/api/logicflow-instance/text.en.md +60 -0
  27. package/dist/docs/api/logicflow-instance/text.zh.md +60 -0
  28. package/dist/docs/api/logicflow-instance/theme.en.md +179 -0
  29. package/dist/docs/api/logicflow-instance/theme.zh.md +179 -0
  30. package/dist/docs/api/runtime-model/edgeModel.en.md +29 -0
  31. package/dist/docs/api/runtime-model/edgeModel.zh.md +325 -0
  32. package/dist/docs/api/runtime-model/graphModel.en.md +275 -0
  33. package/dist/docs/api/runtime-model/graphModel.zh.md +1153 -0
  34. package/dist/docs/api/runtime-model/nodeModel.en.md +37 -0
  35. package/dist/docs/api/runtime-model/nodeModel.zh.md +644 -0
  36. package/dist/docs/api/type/MainTypes.en.md +598 -0
  37. package/dist/docs/api/type/MainTypes.zh.md +867 -0
  38. package/dist/docs/api/type/Theme.en.md +187 -0
  39. package/dist/docs/api/type/Theme.zh.md +187 -0
  40. package/dist/docs/api/type/canvas-types.en.md +25 -0
  41. package/dist/docs/api/type/canvas-types.zh.md +25 -0
  42. package/dist/docs/api/type/index.en.md +96 -0
  43. package/dist/docs/api/type/index.zh.md +99 -0
  44. package/dist/docs/api/type/node-types.en.md +21 -0
  45. package/dist/docs/api/type/node-types.zh.md +21 -0
  46. package/dist/docs/api/type/plugin-types.en.md +24 -0
  47. package/dist/docs/api/type/plugin-types.zh.md +24 -0
  48. package/dist/docs/index.md +11 -0
  49. package/dist/docs/tutorial/about.en.md +38 -0
  50. package/dist/docs/tutorial/about.zh.md +65 -0
  51. package/dist/docs/tutorial/advanced/dnd.en.md +62 -0
  52. package/dist/docs/tutorial/advanced/dnd.zh.md +52 -0
  53. package/dist/docs/tutorial/advanced/edge.en.md +64 -0
  54. package/dist/docs/tutorial/advanced/edge.zh.md +66 -0
  55. package/dist/docs/tutorial/advanced/keyboard.en.md +70 -0
  56. package/dist/docs/tutorial/advanced/keyboard.zh.md +67 -0
  57. package/dist/docs/tutorial/advanced/node.en.md +338 -0
  58. package/dist/docs/tutorial/advanced/node.zh.md +338 -0
  59. package/dist/docs/tutorial/advanced/react.en.md +106 -0
  60. package/dist/docs/tutorial/advanced/react.zh.md +114 -0
  61. package/dist/docs/tutorial/advanced/silent-mode.en.md +75 -0
  62. package/dist/docs/tutorial/advanced/silent-mode.zh.md +71 -0
  63. package/dist/docs/tutorial/advanced/snapline.en.md +54 -0
  64. package/dist/docs/tutorial/advanced/vue.en.md +249 -0
  65. package/dist/docs/tutorial/advanced/vue.zh.md +248 -0
  66. package/dist/docs/tutorial/ai.en.md +64 -0
  67. package/dist/docs/tutorial/ai.zh.md +64 -0
  68. package/dist/docs/tutorial/basic/background.en.md +50 -0
  69. package/dist/docs/tutorial/basic/canvas.en.md +164 -0
  70. package/dist/docs/tutorial/basic/canvas.zh.md +183 -0
  71. package/dist/docs/tutorial/basic/class.en.md +106 -0
  72. package/dist/docs/tutorial/basic/class.zh.md +103 -0
  73. package/dist/docs/tutorial/basic/edge.en.md +151 -0
  74. package/dist/docs/tutorial/basic/edge.zh.md +152 -0
  75. package/dist/docs/tutorial/basic/event.en.md +70 -0
  76. package/dist/docs/tutorial/basic/event.zh.md +66 -0
  77. package/dist/docs/tutorial/basic/grid.en.md +77 -0
  78. package/dist/docs/tutorial/basic/node.en.md +358 -0
  79. package/dist/docs/tutorial/basic/node.zh.md +318 -0
  80. package/dist/docs/tutorial/basic/theme.en.md +154 -0
  81. package/dist/docs/tutorial/basic/theme.zh.md +157 -0
  82. package/dist/docs/tutorial/extension/adapter.en.md +446 -0
  83. package/dist/docs/tutorial/extension/adapter.zh.md +429 -0
  84. package/dist/docs/tutorial/extension/bpmn-element.en.md +1427 -0
  85. package/dist/docs/tutorial/extension/bpmn-element.zh.md +1472 -0
  86. package/dist/docs/tutorial/extension/control.en.md +117 -0
  87. package/dist/docs/tutorial/extension/control.zh.md +118 -0
  88. package/dist/docs/tutorial/extension/curved-edge.en.md +46 -0
  89. package/dist/docs/tutorial/extension/curved-edge.zh.md +46 -0
  90. package/dist/docs/tutorial/extension/custom.en.md +142 -0
  91. package/dist/docs/tutorial/extension/custom.zh.md +138 -0
  92. package/dist/docs/tutorial/extension/dnd-panel.en.md +109 -0
  93. package/dist/docs/tutorial/extension/dnd-panel.zh.md +109 -0
  94. package/dist/docs/tutorial/extension/dynamic-group.en.md +606 -0
  95. package/dist/docs/tutorial/extension/dynamic-group.zh.md +606 -0
  96. package/dist/docs/tutorial/extension/group.en.md +217 -0
  97. package/dist/docs/tutorial/extension/group.zh.md +209 -0
  98. package/dist/docs/tutorial/extension/highlight.en.md +50 -0
  99. package/dist/docs/tutorial/extension/highlight.zh.md +50 -0
  100. package/dist/docs/tutorial/extension/insert-node-in-polyline.en.md +52 -0
  101. package/dist/docs/tutorial/extension/insert-node-in-polyline.zh.md +47 -0
  102. package/dist/docs/tutorial/extension/intro.en.md +72 -0
  103. package/dist/docs/tutorial/extension/intro.zh.md +95 -0
  104. package/dist/docs/tutorial/extension/label.en.md +136 -0
  105. package/dist/docs/tutorial/extension/label.zh.md +135 -0
  106. package/dist/docs/tutorial/extension/layout.en.md +156 -0
  107. package/dist/docs/tutorial/extension/layout.zh.md +156 -0
  108. package/dist/docs/tutorial/extension/menu.en.md +319 -0
  109. package/dist/docs/tutorial/extension/menu.zh.md +377 -0
  110. package/dist/docs/tutorial/extension/minimap.en.md +164 -0
  111. package/dist/docs/tutorial/extension/minimap.zh.md +180 -0
  112. package/dist/docs/tutorial/extension/node-resize.en.md +199 -0
  113. package/dist/docs/tutorial/extension/node-resize.zh.md +221 -0
  114. package/dist/docs/tutorial/extension/pool.en.md +227 -0
  115. package/dist/docs/tutorial/extension/pool.zh.md +227 -0
  116. package/dist/docs/tutorial/extension/proximity-connect.en.md +104 -0
  117. package/dist/docs/tutorial/extension/proximity-connect.zh.md +107 -0
  118. package/dist/docs/tutorial/extension/selection.en.md +166 -0
  119. package/dist/docs/tutorial/extension/selection.zh.md +150 -0
  120. package/dist/docs/tutorial/extension/snapshot.en.md +276 -0
  121. package/dist/docs/tutorial/extension/snapshot.zh.md +276 -0
  122. package/dist/docs/tutorial/get-started.en.md +501 -0
  123. package/dist/docs/tutorial/get-started.zh.md +139 -0
  124. package/dist/docs/tutorial/update.en.md +213 -0
  125. package/dist/docs/tutorial/update.zh.md +212 -0
  126. package/package.json +5 -3
  127. package/scripts/postinstall-ai-prompt.js +67 -0
@@ -0,0 +1,117 @@
1
+ ---
2
+ nav: Guide
3
+ group:
4
+ title: Plug-in functionality
5
+ order: 3
6
+ title: Control
7
+ order: 3
8
+ toc: content
9
+ ---
10
+
11
+ ### start using
12
+
13
+ ```tsx | pure
14
+ import LogicFlow from "@logicflow/core";
15
+ import { Control } from "@logicflow/extension";
16
+ import "@logicflow/extension/lib/style/index.css";
17
+
18
+ LogicFlow.use(Control);
19
+ ```
20
+
21
+ After registering the `Control` component, LogicFlow creates a control panel at the top right of
22
+ the canvas, as shown here
23
+
24
+ The control panel provides the common ability to zoom in and out or adapt to the canvas, and also
25
+ has built-in redo and undo functionality, of course if you don't like the UI or the functionality,
26
+ you can define your own based on the [API](../../api/logicflow-instance/index.en.md) provided by `LogicFlow`.
27
+
28
+
29
+ <details>
30
+ <summary>The following controls are built into the control panel</summary>
31
+ <pre><code style="background-color: #282c34; color: #7ec798">
32
+ private controlItems: ControlItem[] = [
33
+ {
34
+ key: 'zoom-out',
35
+ iconClass: 'lf-control-zoomOut',
36
+ title: 'Reduced Flowchart',
37
+ text: 'zoom-out',
38
+ onClick: () => {
39
+ this.lf.zoom(false);
40
+ },
41
+ },
42
+ {
43
+ key: 'zoom-in',
44
+ iconClass: 'lf-control-zoomIn',
45
+ title: 'Enlarge Flowchart',
46
+ text: 'zoom in',
47
+ onClick: () => {
48
+ this.lf.zoom(true);
49
+ },
50
+ },
51
+ {
52
+ key: 'reset',
53
+ iconClass: 'lf-control-fit',
54
+ title: 'Restore the original size of the process',
55
+ text: 'adaptive',
56
+ onClick: () => {
57
+ this.lf.resetZoom();
58
+ },
59
+ },
60
+ {
61
+ key: 'undo',
62
+ iconClass: 'lf-control-undo',
63
+ title: 'Go back to the previous step',
64
+ text: 'back',
65
+ onClick: () => {
66
+ this.lf.undo();
67
+ },
68
+ },
69
+ {
70
+ key: 'redo',
71
+ iconClass: 'lf-control-redo',
72
+ title: 'Move to next step',
73
+ text: 'next',
74
+ onClick: () => {
75
+ this.lf.redo();
76
+ },
77
+ },
78
+ ];</code></pre>
79
+ </details>
80
+
81
+ ### example
82
+
83
+ <a href="https://codesandbox.io/embed/intelligent-matsumoto-t1dc5?fontsize=14&hidenavigation=1&theme=dark&view=preview" target="_blank"> Go to CodeSandbox for examples</a>
84
+
85
+ ### Add Options
86
+
87
+ ```tsx | pure
88
+ lf.extension.control.addItem({
89
+ key: 'mini-map',
90
+ iconClass: 'custom-minimap',
91
+ title: '',
92
+ text: 'nav',
93
+ onMouseEnter: (lf, ev) => {
94
+ const position = lf.getPointByClient(ev.x, ev.y)
95
+ lf.extension.miniMap.show(
96
+ position.domOverlayPosition.x - 120,
97
+ position.domOverlayPosition.y + 35,
98
+ )
99
+ },
100
+ onClick: (lf, ev) => {
101
+ const position = lf.getPointByClient(ev.x, ev.y)
102
+ lf.extension.miniMap.show(
103
+ position.domOverlayPosition.x - 120,
104
+ position.domOverlayPosition.y + 35,
105
+ )
106
+ },
107
+ })
108
+ ```
109
+
110
+ ### Removing Options
111
+
112
+ For example, to remove the navigation controls added above
113
+
114
+ ```tsx | pure
115
+ lf.extension.control.removeItem('mini-map')
116
+ ```
117
+
@@ -0,0 +1,118 @@
1
+ ---
2
+ nav: 指南
3
+ group:
4
+ title: 插件功能
5
+ order: 3
6
+ title: 控制面板 (Control)
7
+ order: 3
8
+ toc: contents
9
+ ---
10
+
11
+ ### 启用
12
+
13
+ ```tsx | pure
14
+ import LogicFlow from "@logicflow/core";
15
+ import { Control } from "@logicflow/extension";
16
+ import "@logicflow/extension/lib/style/index.css";
17
+
18
+ LogicFlow.use(Control);
19
+ ```
20
+
21
+ 注册`Control`组件后,LogicFlow 会在画布右上方创建一个控制面板,如下所示
22
+
23
+ 控制面板提供了常见的能力,放大缩小或者自适应画布的能力,同时也内置了 redo 和 undo 的功能,当然如果你不喜欢这样的
24
+ UI 或功能,也可以基于`LogicFlow`提供的 [API](../../api/logicflow-instance/index.zh.md) 自己定义。
25
+
26
+
27
+ <details>
28
+ <summary>控制面板内置以下控制项</summary>
29
+ <pre><code style="background-color: #282c34; color: #7ec798">
30
+ private controlItems: ControlItem[] = [
31
+ {
32
+ key: 'zoom-out',
33
+ iconClass: 'lf-control-zoomOut',
34
+ title: '缩小流程图',
35
+ text: '缩小',
36
+ onClick: () => {
37
+ this.lf.zoom(false);
38
+ },
39
+ },
40
+ {
41
+ key: 'zoom-in',
42
+ iconClass: 'lf-control-zoomIn',
43
+ title: '放大流程图',
44
+ text: '放大',
45
+ onClick: () => {
46
+ this.lf.zoom(true);
47
+ },
48
+ },
49
+ {
50
+ key: 'reset',
51
+ iconClass: 'lf-control-fit',
52
+ title: '恢复流程原有尺寸',
53
+ text: '适应',
54
+ onClick: () => {
55
+ this.lf.resetZoom();
56
+ },
57
+ },
58
+ {
59
+ key: 'undo',
60
+ iconClass: 'lf-control-undo',
61
+ title: '回到上一步',
62
+ text: '上一步',
63
+ onClick: () => {
64
+ this.lf.undo();
65
+ },
66
+ },
67
+ {
68
+ key: 'redo',
69
+ iconClass: 'lf-control-redo',
70
+ title: '移到下一步',
71
+ text: '下一步',
72
+ onClick: () => {
73
+ this.lf.redo();
74
+ },
75
+ },
76
+ ];</code></pre>
77
+ </details>
78
+
79
+ ### 示例
80
+
81
+ <a href="https://codesandbox.io/embed/intelligent-matsumoto-t1dc5?fontsize=14&hidenavigation=1&theme=dark&view=preview" target="_blank"> 去 CodeSandbox 查看示例</a>
82
+
83
+ ### 添加选项
84
+
85
+ ```tsx | pure
86
+ lf.extension.control.addItem({
87
+ key: 'mini-map',
88
+ iconClass: 'custom-minimap',
89
+ title: '',
90
+ text: '导航',
91
+ onMouseEnter: (lf, ev) => {
92
+ const position = lf.getPointByClient(ev.x, ev.y)
93
+ lf.extension.miniMap.show(
94
+ position.domOverlayPosition.x - 120,
95
+ position.domOverlayPosition.y + 35,
96
+ )
97
+ },
98
+ onClick: (lf, ev) => {
99
+ const position = lf.getPointByClient(ev.x, ev.y)
100
+ lf.extension.miniMap.show(
101
+ position.domOverlayPosition.x - 120,
102
+ position.domOverlayPosition.y + 35,
103
+ )
104
+ },
105
+ })
106
+ ```
107
+
108
+ ### 删除选项
109
+
110
+ 例如, 移除上面添加的导航控制项
111
+
112
+ ```tsx | pure
113
+ /**
114
+ * @params key 需要删除的选项的key
115
+ */
116
+ lf.extension.control.removeItem('mini-map')
117
+ ```
118
+
@@ -0,0 +1,46 @@
1
+ ---
2
+ nav: Guide
3
+ group:
4
+ title: Plug-in functionality
5
+ order: 3
6
+ title: CurvedEdge
7
+ order: 12
8
+ toc: content
9
+ ---
10
+
11
+ The LogicFlow core package has three built-in baselines: line, polyline, and bezier. Since SVG polyline does not have rounded corners, we provide CurvedEdge in the extension package.
12
+
13
+ Rounded edges are used in the same way as LogicFlow's custom edges, by inheriting CurvedEdgeModel and CurvedEdge.
14
+
15
+ ```tsx | pure
16
+ import { CurvedEdge, CurvedEdgeModel } from '@logicflow/extension'
17
+
18
+ class myCurvedEdge extends CurvedEdge {
19
+ }
20
+
21
+ class myCurvedEdgeModel extends CurvedEdgeModel {
22
+ initEdgeData(data) {
23
+ super.initEdgeData(data)
24
+ this.radius = 5
25
+ }
26
+
27
+ getEdgeStyle() {
28
+ const style = super.getEdgeStyle()
29
+ style.strokeWidth = 3
30
+ style.stroke = 'rgb(130, 179, 102)'
31
+ return style
32
+ }
33
+
34
+ setAttributes() {
35
+ this.isAnimation = true
36
+ }
37
+
38
+ getEdgeAnimationStyle() {
39
+ const style = super.getEdgeAnimationStyle()
40
+ style.strokeDasharray = '15 5'
41
+ style.animationDuration = '10s'
42
+ style.stroke = 'rgb(130, 179, 102)'
43
+ return style
44
+ }
45
+ }
46
+ ```
@@ -0,0 +1,46 @@
1
+ ---
2
+ nav: 指南
3
+ group:
4
+ title: 插件功能
5
+ order: 3
6
+ title: 圆角折线 (CurvedEdge)
7
+ order: 12
8
+ toc: content
9
+ ---
10
+
11
+ LogicFlow core 包中内置了 直线(line)、直角折线(polyline)、贝塞尔曲线(bezier)三种基础连线。由于 svg 的 polyline 是不带圆角的,所以我们在 extension 包中提供了圆角弧线 CurvedEdge。
12
+
13
+ 圆角折线的使用方式和 LogicFlow 的自定义连线一样,开发者可以通过继承 CurvedEdgeModel 和 CurvedEdge
14
+
15
+ ```tsx | pure
16
+ import { CurvedEdge, CurvedEdgeModel } from '@logicflow/extension'
17
+
18
+ class myCurvedEdge extends CurvedEdge {
19
+ }
20
+
21
+ class myCurvedEdgeModel extends CurvedEdgeModel {
22
+ initEdgeData(data) {
23
+ super.initEdgeData(data)
24
+ this.radius = 5
25
+ }
26
+
27
+ getEdgeStyle() {
28
+ const style = super.getEdgeStyle()
29
+ style.strokeWidth = 3
30
+ style.stroke = 'rgb(130, 179, 102)'
31
+ return style
32
+ }
33
+
34
+ setAttributes() {
35
+ this.isAnimation = true
36
+ }
37
+
38
+ getEdgeAnimationStyle() {
39
+ const style = super.getEdgeAnimationStyle()
40
+ style.strokeDasharray = '15 5'
41
+ style.animationDuration = '10s'
42
+ style.stroke = 'rgb(130, 179, 102)'
43
+ return style
44
+ }
45
+ }
46
+ ```
@@ -0,0 +1,142 @@
1
+ ---
2
+ nav: Guide
3
+ group:
4
+ title: Plug-in functionality
5
+ order: 3
6
+ title: Custom Plugin
7
+ order: 17
8
+ toc: content
9
+ ---
10
+
11
+ LogicFlow provides a lot of plug-ins, but these plug-ins are some of the plug-ins with universal,
12
+ not always meet the business needs. You can customize the plug-ins based on your business scenarios.
13
+
14
+ ## Plug-in base format
15
+
16
+ ```tsx | pure
17
+ class PluginCls {
18
+ static pluginName = 'pluginName'
19
+
20
+ constructor({ lf, LogicFlow }) {
21
+ // do anything
22
+ }
23
+
24
+ render(lf, toolOverlay) {
25
+ // do anything
26
+ }
27
+
28
+ destroy() {
29
+ // do anything
30
+ }
31
+ }
32
+ ```
33
+
34
+ - A plugin is a class.
35
+ - The class has a static property `pluginName` that identifies the name of the plugin. Plugins with
36
+ the same name are overridden when initializing an instance of `lf`, also, the user can get an
37
+ instance of the class plugin by `lf.extension.pluginName`.
38
+ - When initializing `lf` instances, plugin instances are initialized at the same time, and the
39
+ parameters `lf` and `LogicFlow` are passed in.
40
+ - After `lf` is rendered, the `render` method of the plugin instance is called (if any). The second
41
+ parameter, toolOverlay, is a node representing the `LogicFlow` Dom layer. Plugin developers can
42
+ insert html content directly into this node.
43
+ - `destroy` is the method that is called to destroy the plugin. It can be left out in most cases.
44
+
45
+ ## Implementing the context-pad plugin
46
+
47
+ The following is an example of implementing a `context-pad` plugin to show you how to define a
48
+ plugin that fits your business. A `context-pad` plugin is a node that, when clicked on, displays an
49
+ optional shortcut action next to the node, which can be thought of as a menu that appears when
50
+ left-clicked on.
51
+
52
+ ### Add plug-in option method
53
+
54
+ LogicFlow mounts instances of plugins as plugin names on `lf.extension` so that our methods
55
+ in `class` can be called with `lf.extension.plugin-name. PluginMethods` calls.
56
+
57
+ ```tsx | pure
58
+ class ContextPad {
59
+ setContextMenuItems(items) {
60
+ this.commonMenuItems = items;
61
+ }
62
+ }
63
+
64
+ ContextPad.pluginName = "contextPad";
65
+
66
+ lf.extension.contextPad.setContextMenuItems([
67
+ {
68
+ icon: "...",
69
+ callback: () => {},
70
+ },
71
+ ]);
72
+ ```
73
+
74
+ ### Listening for nodes to be clicked
75
+
76
+ When the plugin is initialized, `lf` is passed to the plugin as a parameter, which can then be used
77
+ to listen for events occurring on the canvas.
78
+
79
+ ```tsx | pure
80
+ class ContextPad {
81
+ constructor({ lf }) {
82
+ lf.on("node:click", (data) => {
83
+ this.showContextPad(data);
84
+ });
85
+ }
86
+
87
+ showContextPad() {
88
+ // ...
89
+ }
90
+ }
91
+ ```
92
+
93
+ ### Displaying HTML content at a specified location on the canvas
94
+
95
+ The plugin's render function takes two arguments, `lf` and `toolOverlay`, which is the component
96
+ layer; LogicFlow's canvas is made up of multiple layers, and the component layer is dedicated to
97
+ rendering custom components.
98
+
99
+ **LogicFlow's diagram layers**
100
+
101
+ <img src="../../../public/overlay.png" alt="overlay info" style="width: 300px">
102
+
103
+ So here we just need to insert the menu into `toolOverlay`, and then move its menu to the
104
+ corresponding position.
105
+
106
+ ```tsx | pure
107
+ class ContextPad {
108
+ render(lf, toolOverlay) {
109
+ this.toolOverlay = toolOverlay;
110
+ }
111
+
112
+ createMenu() {
113
+ this.__menuDOM = document.createElement("div");
114
+ }
115
+
116
+ // Calculate where the menu should be displayed (top right corner of the node)
117
+ getContextMenuPosition() {
118
+ const data = this._activeData;
119
+ const Model = this.lf.graphModel.getElement(data.id);
120
+ let x;
121
+ let y;
122
+ if (Model.BaseType === "node") {
123
+ x = data.x + Model.width / 2;
124
+ y = data.y - Model.height / 2;
125
+ }
126
+ return this.lf.graphModel.transformModel.CanvasPointToHtmlPoint([x, y]);
127
+ }
128
+
129
+ showMenu() {
130
+ const [x, y] = this.getContextMenuPosition();
131
+ this.__menuDOM.style.display = "flex";
132
+ // Display the menu to the corresponding position
133
+ this.__menuDOM.style.top = `${y}px`;
134
+ this.__menuDOM.style.left = `${x + 10}px`;
135
+ this.toolOverlay.appendChild(this.__menuDOM);
136
+ }
137
+ }
138
+ ```
139
+
140
+ ## example
141
+
142
+ <a href="https://codesandbox.io/embed/logicflow-base22-rl301?fontsize=14&hidenavigation=1&theme=dark&view=preview" target="_blank"> Go to CodeSandbox for examples</a>
@@ -0,0 +1,138 @@
1
+ ---
2
+ nav: 指南
3
+ group:
4
+ title: 插件功能
5
+ order: 3
6
+ title: 自定义插件 (Custom Plugin)
7
+ order: 17
8
+ toc: content
9
+ ---
10
+
11
+ LogicFlow 提供了很多的插件,但是这些插件都是一些具有普适性的插件,不一定都符合业务需求。这时候可以基于自己的业务场景进行自定义插件。
12
+
13
+ ## 插件的基础格式
14
+
15
+ ```tsx | pure
16
+ class PluginCls {
17
+ static pluginName = 'pluginName'
18
+
19
+ constructor({ lf, LogicFlow }) {
20
+ // do anything
21
+ }
22
+
23
+ render(lf, toolOverlay) {
24
+ // do anything
25
+ }
26
+
27
+ destroy() {
28
+ // do anything
29
+ }
30
+ }
31
+ ```
32
+
33
+ - 插件是一个类。
34
+ - 这个类有个静态属性`pluginName`用于标识插件的名称。同名的插件在初始化`lf`
35
+ 实例的时候会覆盖。同时使用方可以通过`lf.extension.插件名称`获取插件这个类的实例。
36
+ - 在初始化`lf`实例的时候,会同时初始化插件实例,此时会传入参数`lf`和`LogicFlow`。
37
+ - 在`lf`渲染完成后,会调用插件实例的`render`方法(如有)。第二个参数 toolOverlay 是表示`LogicFlow` Dom
38
+ 层的节点。插件开发者可以直接在这个节点插入 html 内容。
39
+ - `destroy`是销毁插件是调用的方法。大多数情况下可以不写。
40
+
41
+ ## 实现 context-pad 插件
42
+
43
+ 下面实现一个`context-pad`示例,向大家介绍如何定义符合自己业务的插件。`context-pad`
44
+ 插件是一个点击节点后,在节点旁边出现可选的快捷操作,可以看做是左键点击出现的菜单。
45
+
46
+ ### 增加插入选项方法
47
+
48
+ LogicFlow 会将插件的实例以插件名称的形式挂载到`lf.extension`上,这样我们在`class`
49
+ 中的方法就可以用`lf.extension.插件名称.插件方法`调用了。
50
+
51
+ ```tsx | pure
52
+ class ContextPad {
53
+ /**
54
+ * 设置通用的菜单选项
55
+ */
56
+ setContextMenuItems(items) {
57
+ this.commonMenuItems = items;
58
+ }
59
+ }
60
+
61
+ ContextPad.pluginName = "contextPad";
62
+
63
+ // 调用方法
64
+
65
+ lf.extension.contextPad.setContextMenuItems([
66
+ {
67
+ icon: "...",
68
+ callback: () => {},
69
+ },
70
+ ]);
71
+ ```
72
+
73
+ ### 监听节点被点击
74
+
75
+ 在插件被初始化时,会将`lf`以参数的形式传递给插件,这时可以利用`lf`监听画布上发生的事件。
76
+
77
+ ```tsx | pure
78
+ class ContextPad {
79
+ constructor({ lf }) {
80
+ lf.on("node:click", (data) => {
81
+ this.showContextPad(data);
82
+ });
83
+ }
84
+
85
+ showContextPad() {
86
+ // ...
87
+ }
88
+ }
89
+ ```
90
+
91
+ ### 在画布指定位置显示 HTML 内容
92
+
93
+ 插件的 render 函数有两个参数,一个是`lf`, 第二个参数是`toolOverlay`, 也就是组件层。LogicFlow
94
+ 的画布是由多个图层组成,而组件层则是专门用来渲染自定义的组件。
95
+
96
+ **LogicFlow 的图分层**
97
+
98
+ <img src="../../../public/overlay.png" alt="图层说明" style="width: 300px">
99
+
100
+ 所以这里我们只需要将菜单插入到`toolOverlay`, 然后将其菜单移动到对应的位置即可。
101
+
102
+ ```tsx | pure
103
+ class ContextPad {
104
+ render(lf, toolOverlay) {
105
+ this.toolOverlay = toolOverlay
106
+ }
107
+
108
+ createMenu() {
109
+ this.__menuDOM = document.createElement('div')
110
+ }
111
+
112
+ // 计算出菜单应该显示的位置(节点的右上角)
113
+ getContextMenuPosition() {
114
+ const data = this._activeData
115
+ const Model = this.lf.graphModel.getElement(data.id)
116
+ let x
117
+ let y
118
+ if (Model.BaseType === 'node') {
119
+ x = data.x + Model.width / 2
120
+ y = data.y - Model.height / 2
121
+ }
122
+ return this.lf.graphModel.transformModel.CanvasPointToHtmlPoint([x, y])
123
+ }
124
+
125
+ showMenu() {
126
+ const [x, y] = this.getContextMenuPosition()
127
+ this.__menuDOM.style.display = 'flex'
128
+ // 将菜单显示到对应的位置
129
+ this.__menuDOM.style.top = `${y}px`
130
+ this.__menuDOM.style.left = `${x + 10}px`
131
+ this.toolOverlay.appendChild(this.__menuDOM)
132
+ }
133
+ }
134
+ ```
135
+
136
+ ## 完整示例
137
+
138
+ <a href="https://codesandbox.io/embed/logicflow-base22-rl301?fontsize=14&hidenavigation=1&theme=dark&view=preview" target="_blank"> 去 CodeSandbox 查看示例</a>