From 32d8e1cea83eb3835e9156d7833c8830084533dd Mon Sep 17 00:00:00 2001 From: Dymonelewis <120695700@qq.com> Date: Tue, 26 Nov 2024 15:02:58 +0800 Subject: [PATCH] =?UTF-8?q?fix(core):=20=E4=BF=AE=E5=A4=8Dpolyline=20point?= =?UTF-8?q?s=E5=86=85=E5=AE=B9=E5=8F=98=E5=8C=96=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E6=97=A0=E6=B3=95=E8=A7=A3=E6=9E=90=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20&=20=E4=BC=98=E5=8C=96=E5=88=9D=E5=A7=8B=E5=8C=96=E8=BE=B9?= =?UTF-8?q?=E5=AF=B9=E9=BD=90=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/src/model/GraphModel.ts | 49 +++++++++++++------ .../core/src/model/edge/PolylineEdgeModel.ts | 9 +--- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/packages/core/src/model/GraphModel.ts b/packages/core/src/model/GraphModel.ts index 0a843efd7..9a42a59dd 100644 --- a/packages/core/src/model/GraphModel.ts +++ b/packages/core/src/model/GraphModel.ts @@ -41,6 +41,7 @@ import Position = LogicFlow.Position import PointTuple = LogicFlow.PointTuple import GraphData = LogicFlow.GraphData import NodeConfig = LogicFlow.NodeConfig +import AnchorConfig = Model.AnchorConfig import BaseNodeModelCtor = LogicFlow.BaseNodeModelCtor import BaseEdgeModelCtor = LogicFlow.BaseEdgeModelCtor @@ -444,36 +445,52 @@ export class GraphModel { const { sourceNodeId, targetNodeId, - sourceAnchorId, - targetAnchorId, + sourceAnchorId = '', + targetAnchorId = '', startPoint, endPoint, text, textPosition, } = edgeModel + const updateAnchorPoint = ( + node: BaseNodeModel | undefined, + anchorId: string, + point: Position, + updatePoint: (anchor: AnchorConfig) => void, + ) => { + const anchor = node?.anchors.find((anchor) => anchor.id === anchorId) + if (anchor && !isEqual(anchor, point)) { + updatePoint(anchor) + } + } + const sourceNode = this.getNodeModelById(sourceNodeId) const targetNode = this.getNodeModelById(targetNodeId) - const sourceAnchor = sourceNode?.anchors.find( - (anchor) => anchor.id === sourceAnchorId, + + updateAnchorPoint( + sourceNode, + sourceAnchorId, + startPoint, + edgeModel.updateStartPoint.bind(edgeModel), ) - const targetAnchor = targetNode?.anchors.find( - (anchor) => anchor.id === targetAnchorId, + updateAnchorPoint( + targetNode, + targetAnchorId, + endPoint, + edgeModel.updateEndPoint.bind(edgeModel), ) - if (sourceAnchor && !isEqual(sourceAnchor, startPoint)) { - edgeModel.updateStartPoint(sourceAnchor) - } - if (targetAnchor && !isEqual(targetAnchor, endPoint)) { - edgeModel.updateEndPoint(targetAnchor) - } + // 而文本需要先算一下文本与默认文本位置之间的相对位置差 // 再计算新路径的文本默认位置,加上相对位置差,得到调整后边的文本的位置 if (text) { const { x, y } = text const { x: defaultX, y: defaultY } = textPosition - const deltaX = x - defaultX - const deltaY = y - defaultY - edgeModel.resetTextPosition() - edgeModel.moveText(deltaX, deltaY) + if (x && y && defaultX && defaultY) { + const deltaX = x - defaultX + const deltaY = y - defaultY + edgeModel.resetTextPosition() + edgeModel.moveText(deltaX, deltaY) + } } this.edgeModelMap.set(edgeModel.id, edgeModel) this.elementsModelMap.set(edgeModel.id, edgeModel) diff --git a/packages/core/src/model/edge/PolylineEdgeModel.ts b/packages/core/src/model/edge/PolylineEdgeModel.ts index 7533817d2..d55b5c085 100644 --- a/packages/core/src/model/edge/PolylineEdgeModel.ts +++ b/packages/core/src/model/edge/PolylineEdgeModel.ts @@ -327,14 +327,7 @@ export class PolylineEdgeModel extends BaseEdgeModel { } getPath(points: Point[]): string { - return points - .map((point, index) => { - if (!index) { - return `M ${point.x} ${point.y}` - } - return `L ${point.x} ${point.y}` - }) - .join(' ') + return points.map((point) => `${point.x},${point.y}`).join(' ') } @action