@@ -1,7 +1,11 @@
|
||||
import { ref } from 'vue'
|
||||
import { ref } from "vue"
|
||||
import { useVueFlow } from "@vue-flow/core"
|
||||
import { nanoid } from "nanoid"
|
||||
import { getNodeTypeConfig, createNodeStyle, getNodeDimensions } from "./useNodeStyles"
|
||||
import {
|
||||
getNodeTypeConfig,
|
||||
createNodeStyle,
|
||||
getNodeDimensions,
|
||||
} from "./useNodeStyles"
|
||||
|
||||
/**
|
||||
* 简化的拖拽处理
|
||||
@@ -25,39 +29,39 @@ export function useDnD() {
|
||||
const onDrop = (event: DragEvent) => {
|
||||
event.preventDefault()
|
||||
isDragOver.value = false
|
||||
|
||||
|
||||
const type = event.dataTransfer?.getData("application/vueflow")
|
||||
if (!type) return
|
||||
|
||||
|
||||
// 获取鼠标在画布中的坐标
|
||||
const position = screenToFlowCoordinate({
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
})
|
||||
|
||||
|
||||
// 根据节点类型获取实际尺寸
|
||||
const dimensions = getNodeDimensions(type)
|
||||
|
||||
|
||||
// 调整位置,使节点中心点对齐到鼠标位置
|
||||
const adjustedPosition = {
|
||||
x: position.x - dimensions.width / 2,
|
||||
y: position.y - dimensions.height / 2
|
||||
y: position.y - dimensions.height / 2,
|
||||
}
|
||||
|
||||
|
||||
const nodeId = `node-${nanoid()}`
|
||||
const config = getNodeTypeConfig(type)
|
||||
const newNode = {
|
||||
id: nodeId,
|
||||
type: 'custom',
|
||||
type: "custom",
|
||||
position: adjustedPosition,
|
||||
data: {
|
||||
label: config.label,
|
||||
color: config.color,
|
||||
originalType: type
|
||||
originalType: type,
|
||||
},
|
||||
style: createNodeStyle(type)
|
||||
style: createNodeStyle(type),
|
||||
}
|
||||
|
||||
|
||||
addNodes([newNode])
|
||||
return newNode
|
||||
}
|
||||
@@ -66,6 +70,6 @@ export function useDnD() {
|
||||
isDragOver,
|
||||
onDragOver,
|
||||
onDragLeave,
|
||||
onDrop
|
||||
onDrop,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user