graphModel.vue
2.18 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<template>
<div class="container">
<div ref="container" class="flow-container"></div>
</div>
</template>
<script setup>
import LogicFlow from "@logicflow/core";
import { MiniMap, Control } from "@logicflow/extension";
import CustomNode from './customNode';
const container = ref(null);
let lf = null;
onMounted(() => {
nextTick(() => {
lf = new LogicFlow({
container: container.value,
grid: true,
plugins: [MiniMap, Control],
width: container.value.offsetWidth,
height: container.value.offsetHeight
});
// 注册自定义节点
lf.register(CustomNode);
// 监听节点点击事件,点击时进入编辑模式
lf.on("node:click", ({ data }) => {
lf.graphModel.editText(data.id);
});
// 监听文本编辑完成事件
lf.on("text:update", ({ data }) => {
console.log("文本更新为:", data.text);
});
// 添加节点移动规则
lf.graphModel.addNodeMoveRules((nodeModel, x, y) => {
if (nodeModel.properties.disabled) {
return false; // 禁止移动
}
return true; // 允许移动
});
// 渲染初始数据
lf.render({
nodes: [
{
id: "node1",
type: "rect",
x: 100,
y: 100,
text: "点击编辑文本"
},
{
id: "node2",
type: "custom-rect", // 使用自定义节点类型
x: 200,
y: 200,
text: "点击编辑文本"
},
{
id: "node11",
type: "rect",
x: 300,
y: 300,
text: "可移动节点",
properties: {
disabled: false
}
},
{
id: "node21",
type: "custom-rect",
x: 400,
y: 400,
text: "禁止移动节点",
properties: {
disabled: true
}
}
]
});
});
});
// 判断节点是否可连接
const checkNodeConnectable = (targetNode, sourceNode) => {
return targetNode.type === 'rect';
}
</script>
<style scoped>
.container {
width: 100vw;
height: 100vh;
}
.flow-container {
width: 100%;
height: 100%;
min-height: 500px;
}
</style>