nodeModel.vue
2.76 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!--
* @Date: 2025-03-10 16:52:35
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-03-18 17:18:57
* @FilePath: /logic-flow2/src/views/api/nodeModel.vue
* @Description: 拖拽面板
-->
<template>
<div class="container">
<div ref="container" class="flow-container"></div>
</div>
</template>
<script setup>
import LogicFlow from "@logicflow/core";
import "@logicflow/extension/lib/style/index.css";
import CustomNode from "./draggable-text-node";
const container = ref(null);
let lf = null;
/**
* 先必须初始化的时把配置项打开,之后再进行单独的数据设置
* 比如你如果要文本移动,那么你必须先打开文本移动的配置项,然后再进行单独的数据设置
*/
onMounted(() => {
lf = new LogicFlow({
container: container.value,
grid: true,
// plugins: [Label], // 引入 Label 插件
nodeTextDraggable: true, // 开启节点文本拖拽
});
// 注册自定义节点
lf.register(CustomNode);
// 设置拖拽面板配置
lf.setPatternItems([
{
type: "custom-rect",
text: "自定义节点",
label: "拖拽生成节点",
className: "custom-node",
},
]);
// 监听节点创建事件,自定义ID规则
lf.on("node:dnd-add", ({ data }) => {
const prefix = "custom_node_";
const timestamp = Date.now();
data.id = `${prefix}${timestamp}`;
console.log("新创建的节点ID:", data.id);
});
// 添加连线事件监听
lf.on('edge:connect', ({ data }) => {
console.log('连线成功', data);
});
lf.on('connection:not-allowed', (data) => {
console.log('连线被阻止', data);
});
lf.render({
nodes: [
{
id: "node1",
type: "custom-rect",
x: 200,
y: 100,
// text: {
// x: 250,
// y: 150,
// value: 'Node 1',
// },
text: "Node 1",
},
{
id: "node2",
type: "circle",
x: 400,
y: 100,
text: {
x: 450,
y: 150,
value: "Node 2",
draggable: false,
},
},
],
edges: [{ id: "edge1", sourceNodeId: "node1", targetNodeId: "node2" }],
});
lf.translateCenter();
lf.on("node:click", ({data}) => {
lf.getNodeModelById(data.id).setProperties({
disabled: !data.properties.disabled,
scale: 2,
});
console.warn(lf.getGraphData());
});
const nodeModel = lf.getNodeModelById("node1");
// const { anchors } = nodeModel;
// nodeModel.setIsShowAnchor(true)
console.warn("nodeModel", nodeModel.getConnectedTargetRules());
});
</script>
<style scoped>
.container {
width: 100vw;
height: 100vh;
display: flex;
flex-direction: column;
}
.flow-container {
flex: 1;
width: 100%;
height: 100%;
}
</style>