Skip to content

[Bug]: 设置连接桩后,behaviors的drag-element无法区分点击节点还是点击连接桩,他们的event.targetType都是node #7456

@yrx100

Description

@yrx100

Describe the bug / 问题描述

<template>
	<div ref="containerRef" class="viewport"></div>
	<TeleportContainer />
</template>

<script setup lang="tsx">
import { useFlowCanvasStore } from '@/store/modules/flowCanvas'
import { ExtensionCategory, register, Graph } from '@antv/g6'
import { VueNode } from 'g6-extension-vue'
import { computed, defineComponent, onMounted, useTemplateRef } from 'vue'
import CustomFlowNode from './CustomFlowNode.vue'
const containerRef = useTemplateRef('containerRef')

const flowCanvasStore = useFlowCanvasStore()

// 注册自定义节点
register(ExtensionCategory.NODE, 'vue-node', VueNode)

onMounted(() => {
	const graph = new Graph({
		container: containerRef.value as HTMLElement,
		autoResize: true,
		data: {
			nodes: [
				{
					id: 'node1',
					style: { x: 100, y: 200 },
					ports: [
						{ key: 'custom1', placement: [0.2, 0] }, // 相对位置:左上角20%处
						{ key: 'custom2', placement: [0.8, 0] }, // 相对位置:右上角80%处
						{ key: 'custom3', placement: [1, 0.5] } // 相对位置:右边中央
					],
					data: { label: 'node1' }
				},
				{
					id: 'node2',
					style: { x: 300, y: 200 },
					data: { label: 'node2' }
				}
			],
			edges: []
		},
		node: {
			type: 'vue-node',
			style: {
				size: [186, 36], // 节点大小
				ports: [
					{
						key: 'top',
						placement: 'top',
						fill: '#7E92B5',
						cursor: 'crosshair',
						draggable: false
					},

					{
						key: 'bottom',
						placement: 'bottom',
						fill: '#FFBE3A',
						cursor: 'crosshair'
					}
				], // 连接桩
				portR: 4, // 连接桩圆角
				portLineWidth: 1, // 连接桩线宽
				portStroke: '#fff', // 连接桩颜色
				component: (data: any) => (
					<CustomFlowNode {...{ data: Object.assign({}, data) }} />
				)
			}
		},
		edge: {
			type: 'cubic-vertical' // 边的类型,可选:'line', 'cubic', 'cubic-vertical' 等
		},
		behaviors: [
			'hover-activate',
			'drag-canvas',
			'zoom-canvas',
			'click-select',
			{
				type: 'drag-element',
				key: 'drag-node-only',
				enable: event => {
					console.log(
						event.target,
						event.originalTarget,
						event.currentTarget,
						event.originalEvent,
						'event'
					)

					return event.targetType === 'node'
				} // 只允许拖拽节点
			}
		]
	})

	graph.render()

	// 监听连线成功事件(可选)
	graph.on('edge:created', (event: any) => {
		console.log('新增连线:', event)
		// 在这里可以处理新增的连线逻辑
	})

	// 监听连线错误事件(可选)
	graph.on('edge:create-error', (event: any) => {
		console.log('连线创建失败:', event)
	})
})
</script>
<style scoped lang="scss">
.viewport {
	width: 100%;
	height: 100%;
	background-color: #f7f7f9;
}
</style>

Reproduction link / 复现链接

No response

Steps to Reproduce the Bug or Issue / 重现步骤

No response

Version / 版本

Please select / 请选择

OS / 操作系统

  • macOS
  • Windows
  • Linux
  • Others / 其他

Browser / 浏览器

  • Chrome
  • Edge
  • Firefox
  • Safari (Limited support / 有限支持)
  • IE (Nonsupport / 不支持)
  • Others / 其他

Metadata

Metadata

Assignees

No one assigned

    Labels

    question 💬This issue is just a question. It will be converted into discussion automatically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions