hookehuyr

自定义拖拽功能

...@@ -95,6 +95,11 @@ const router = createRouter({ ...@@ -95,6 +95,11 @@ const router = createRouter({
95 name: 'adv-dnd-panel', 95 name: 'adv-dnd-panel',
96 component: () => import('../views/adv-dnd-panel/index.vue') 96 component: () => import('../views/adv-dnd-panel/index.vue')
97 }, 97 },
98 + {
99 + path: '/adv-dnd-panel-custom',
100 + name: 'adv-dnd-panel-custom',
101 + component: () => import('../views/adv-dnd-panel/custom.vue')
102 + },
98 ] 103 ]
99 }) 104 })
100 105
......
1 +<!--
2 + * @Date: 2025-03-10 16:52:35
3 + * @LastEditors: hookehuyr hookehuyr@gmail.com
4 + * @LastEditTime: 2025-03-13 18:28:05
5 + * @FilePath: /logic-flow2/src/views/adv-dnd-panel/custom.vue
6 + * @Description: 自定义拖拽功能
7 +-->
8 +<template>
9 + <div class="container">
10 + <div ref="container" class="flow-container"></div>
11 + <div style="position: absolute; bottom: 20%; left: 50%; transform: translateX(-50%)">
12 + <el-popover placement="top" :width="400" :visible="visible">
13 + <template #reference>
14 + <el-button style="margin-right: 16px" @mouseover="visible = true"
15 + >hover</el-button
16 + >
17 + </template>
18 + <div style="padding: 10px 0">
19 + <div class="node-panel">
20 + <div
21 + class="node-item"
22 + draggable="true"
23 + @dragstart="handleDragStart($event, { type: 'rect' })"
24 + >
25 + 矩形节点
26 + </div>
27 + <div
28 + class="node-item"
29 + draggable="true"
30 + @dragstart="handleDragStart($event, { type: 'circle' })"
31 + >
32 + 圆形节点
33 + </div>
34 + </div>
35 + </div>
36 + </el-popover>
37 + </div>
38 + </div>
39 +</template>
40 +
41 +<script setup>
42 +import LogicFlow from "@logicflow/core";
43 +
44 +const container = ref(null);
45 +let lf = null;
46 +
47 +const visible = ref(false);
48 +
49 +// 拖拽事件处理函数
50 +const handleDragStart = (e, node) => {
51 + e.dataTransfer.setData("node", JSON.stringify(node));
52 +};
53 +
54 +const handleDragOver = (e) => {
55 + e.preventDefault();
56 +};
57 +
58 +const handleDrop = (e) => {
59 + e.preventDefault();
60 + const data = JSON.parse(e.dataTransfer.getData("node"));
61 + const canvasPoint = lf.getPointByClient(e.clientX, e.clientY);
62 +
63 + lf.addNode({
64 + type: data.type,
65 + x: canvasPoint.canvasOverlayPosition.x,
66 + y: canvasPoint.canvasOverlayPosition.y,
67 + });
68 +};
69 +
70 +// 点击事件处理函数
71 +const handleClickOutside = (e) => {
72 + const popover = document.querySelector(".el-popover");
73 + const button = document.querySelector(".el-button");
74 + if (popover && button && !popover.contains(e.target) && !button.contains(e.target)) {
75 + visible.value = false;
76 + }
77 +};
78 +
79 +onMounted(() => {
80 + lf = new LogicFlow({
81 + container: container.value,
82 + grid: true,
83 + });
84 +
85 + // 添加事件监听
86 + container.value.addEventListener("dragover", handleDragOver);
87 + container.value.addEventListener("drop", handleDrop);
88 +
89 + // 添加点击事件监听
90 + document.addEventListener("click", handleClickOutside);
91 +
92 + lf.render({
93 + nodes: [
94 + { id: "node1", type: "rect", x: 200, y: 100 },
95 + { id: "node2", type: "circle", x: 400, y: 100 },
96 + ],
97 + edges: [{ id: "edge1", sourceNodeId: "node1", targetNodeId: "node2" }],
98 + });
99 +});
100 +
101 +// 组件卸载时移除事件监听
102 +onUnmounted(() => {
103 + if (container.value) {
104 + container.value.removeEventListener("dragover", handleDragOver);
105 + container.value.removeEventListener("drop", handleDrop);
106 +
107 + document.removeEventListener("click", handleClickOutside);
108 + }
109 +});
110 +</script>
111 +
112 +<style scoped>
113 +.container {
114 + width: 100vw;
115 + height: 100vh;
116 + display: flex;
117 + flex-direction: column;
118 +}
119 +
120 +.flow-container {
121 + flex: 1;
122 + width: 100%;
123 + height: 100%;
124 +}
125 +
126 +.node-panel {
127 + display: flex;
128 + gap: 16px;
129 +}
130 +
131 +.node-item {
132 + padding: 8px 16px;
133 + border: 1px solid #dcdfe6;
134 + border-radius: 4px;
135 + cursor: grab; /* 修改这里 */
136 + background: #fff;
137 +
138 + &:hover {
139 + border-color: #409eff;
140 + color: #409eff;
141 + }
142 +
143 + /* 添加拖动时的样式 */
144 + &:active {
145 + cursor: grabbing;
146 + }
147 +}
148 +</style>
1 <!-- 1 <!--
2 * @Date: 2025-03-10 14:37:31 2 * @Date: 2025-03-10 14:37:31
3 * @LastEditors: hookehuyr hookehuyr@gmail.com 3 * @LastEditors: hookehuyr hookehuyr@gmail.com
4 - * @LastEditTime: 2025-03-13 17:25:44 4 + * @LastEditTime: 2025-03-13 17:45:28
5 * @FilePath: /logic-flow2/src/views/home.vue 5 * @FilePath: /logic-flow2/src/views/home.vue
6 * @Description: 文件描述 6 * @Description: 文件描述
7 --> 7 -->
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
23 <el-button type="primary" @click="goTo('adv-edge-animation')">adv-edge-animation</el-button> 23 <el-button type="primary" @click="goTo('adv-edge-animation')">adv-edge-animation</el-button>
24 <el-button type="primary" @click="goTo('adv-menu')">adv-menu</el-button> 24 <el-button type="primary" @click="goTo('adv-menu')">adv-menu</el-button>
25 <el-button type="primary" @click="goTo('adv-dnd-panel')">adv-dnd-panel</el-button> 25 <el-button type="primary" @click="goTo('adv-dnd-panel')">adv-dnd-panel</el-button>
26 + <el-button type="primary" @click="goTo('adv-dnd-panel-custom')">adv-dnd-panel-custom</el-button>
26 </template> 27 </template>
27 28
28 <script setup> 29 <script setup>
......