diff --git a/packages/g6/src/runtime/controller/data.ts b/packages/g6/src/runtime/controller/data.ts index b66576cacb..bc83db3614 100644 --- a/packages/g6/src/runtime/controller/data.ts +++ b/packages/g6/src/runtime/controller/data.ts @@ -37,7 +37,11 @@ import { getExtension } from '../../util/extension'; import { convertToNumber } from '../../util/type'; import { isTreeLayout } from '../../util/layout'; import { hasTreeBehaviors } from '../../util/behavior'; -import { EdgeCollisionChecker, QuadTree } from '../../util/polyline'; +import { + EdgeCollisionChecker, + QuadTree, + isPolylineWithObstacleAvoidance, +} from '../../util/polyline'; /** * Manages the data transform extensions; @@ -133,13 +137,18 @@ export class DataController { return this.graphCore.getRelatedEdges(nodeId, direction); } - public findNearEdges(nodeId: ID, transientItem?: Node) { + public findNearEdges( + nodeId: ID, + transientItem?: Node, + onlyPolyline?: boolean, + ) { const edges = this.graphCore.getAllEdges(); const canvasBBox = this.graph.getRenderBBox(undefined) as AABB; const quadTree = new QuadTree(canvasBBox, 4); edges.forEach((edge) => { + if (onlyPolyline && !isPolylineWithObstacleAvoidance(edge)) return; const { data: { x: sourceX, y: sourceY }, } = this.graphCore.getNode(edge.source); diff --git a/packages/g6/src/runtime/controller/item.ts b/packages/g6/src/runtime/controller/item.ts index 8699cf2494..d6445a628e 100644 --- a/packages/g6/src/runtime/controller/item.ts +++ b/packages/g6/src/runtime/controller/item.ts @@ -521,18 +521,15 @@ export class ItemController { ); } - const newNearEdges = this.graph.getNearEdgesData(id); - const relatedEdges = graphCore.getRelatedEdges(id); - const adjacentEdgeInnerModels = isPointPreventPolylineOverlap( - innerModel, - ) - ? uniq( - (this.nearEdgesCache.get(id) || []) - .concat(newNearEdges) - .concat(relatedEdges), - ) - : relatedEdges; - this.nearEdgesCache.set(id, newNearEdges); + const adjacentEdgeInnerModels = graphCore.getRelatedEdges(id); + + if (isPointPreventPolylineOverlap(innerModel)) { + const newNearEdges = this.graph.getNearEdgesData(id, true); + const prevNearEdges = this.nearEdgesCache.get(id) || []; + adjacentEdgeInnerModels.push(...newNearEdges); + adjacentEdgeInnerModels.push(...prevNearEdges); + this.nearEdgesCache.set(id, newNearEdges); + } adjacentEdgeInnerModels.forEach((edge) => { edgeIdsToUpdate.add(edge.id); diff --git a/packages/g6/src/runtime/graph.ts b/packages/g6/src/runtime/graph.ts index 2663d80d2f..e8c1eee73a 100644 --- a/packages/g6/src/runtime/graph.ts +++ b/packages/g6/src/runtime/graph.ts @@ -1031,11 +1031,15 @@ export class Graph * @param nodeId id of the start node * @returns nearby edges' data array */ - public getNearEdgesData(nodeId: ID): EdgeModel[] { + public getNearEdgesData(nodeId: ID, onlyPolyline?: boolean): EdgeModel[] { const transientItem = this.itemController.getTransientItem( nodeId, ) as unknown as Node; - return this.dataController.findNearEdges(nodeId, transientItem); + return this.dataController.findNearEdges( + nodeId, + transientItem, + onlyPolyline, + ); } /** * Get one-hop node ids from a start node. diff --git a/packages/g6/src/stdlib/behavior/drag-node.ts b/packages/g6/src/stdlib/behavior/drag-node.ts index 58c47243a5..07f8a10fa1 100644 --- a/packages/g6/src/stdlib/behavior/drag-node.ts +++ b/packages/g6/src/stdlib/behavior/drag-node.ts @@ -171,9 +171,11 @@ export class DragNode extends Behavior { } /** Retrieve the nearby edges for a given node using quadtree collision detection. */ - private getNearEdgesForNodes(nodeIds: ID[]) { + private getNearEdgesForNodes(nodeIds: ID[], onlyPolyline?: boolean) { return uniq( - nodeIds.flatMap((nodeId) => this.graph.getNearEdgesData(nodeId)), + nodeIds.flatMap((nodeId) => + this.graph.getNearEdgesData(nodeId, onlyPolyline), + ), ); } @@ -341,6 +343,7 @@ export class DragNode extends Behavior { this.hiddenNearEdges = this.getNearEdgesForNodes( preventPolylineOverlapNodeIds, + true, ).filter((edge) => !hiddenEdgesIds.includes(edge.id)); const hiddenNearEdgesIds = this.hiddenNearEdges.map((edge) => edge.id); diff --git a/packages/g6/src/types/graph.ts b/packages/g6/src/types/graph.ts index 8b3168121a..5bf6db7c5f 100644 --- a/packages/g6/src/types/graph.ts +++ b/packages/g6/src/types/graph.ts @@ -118,7 +118,7 @@ export interface IGraph< * @param nodeId id of the start node * @returns nearby edges' data array */ - getNearEdgesData: (nodeId: ID) => EdgeModel[]; + getNearEdgesData: (nodeId: ID, onlyPolyline?: boolean) => EdgeModel[]; /** * Get one-hop node ids from a start node. * @param nodeId id of the start node