Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: getTargetAnchor方法增加AdjustType类型参数方便确定是获取前序节点还是后序节点的锚点 #1172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/core/src/model/node/BaseNodeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { formatData } from '../../util/compatible';
import { getClosestAnchor, pickNodeConfig } from '../../util/node';
import { getZIndex } from '../../util/zIndex';
import { BaseEdgeModel } from '../edge';
import { AdjustType } from '../../view/edge/AdjustPoint';

export type ConnectRule = {
message: string;
Expand Down Expand Up @@ -437,7 +438,7 @@ export default class BaseNodeModel implements IBaseNodeModel {
* @overridable 子类重写此方法获取手动连接边到节点时,需要连接的锚点
* 手动连接边到节点时,需要连接的锚点
*/
public getTargetAnchor(position: Point): AnchorInfo {
public getTargetAnchor(position: Point, type: AdjustType): AnchorInfo {
return getClosestAnchor(position, this);
}

Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/util/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { isInSegment } from '../algorithm/edge';
import { SegmentDirection } from '../constant/constant';
import { getBytesLength } from './edge';
import { GraphModel } from '..';
import { AdjustType } from '../view/edge/AdjustPoint';

/* 获取所有锚点 */
export const getAnchors = (data): Point[] => {
Expand All @@ -31,14 +32,15 @@ type NodeContaint = {
};

/* 手动边时获取目标节点的信息:目标节点,目标节点的锚点index以及坐标 */
export const targetNodeInfo = (position: Point, graphModel: GraphModel): NodeContaint => {
export const targetNodeInfo = (position: Point, graphModel: GraphModel,
type: AdjustType): NodeContaint => {
const { nodes } = graphModel;
let nodeInfo;
for (let i = nodes.length - 1; i >= 0; i--) {
const targetNode = nodes[i];
const inNode = isInNodeBbox(position, targetNode);
if (inNode) {
const anchorInfo = targetNode.getTargetAnchor(position);
const anchorInfo = targetNode.getTargetAnchor(position, type);
if (anchorInfo) { // 不能连接到没有锚点的节点
const currentNodeInfo = {
node: targetNode,
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/view/Anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import GraphModel from '../model/GraphModel';
import { AnchorConfig } from '../type';
import { BaseNode } from './node';
import { cancelRaf, createRaf } from '../util/raf';
import { AdjustType } from './edge/AdjustPoint';

type TargetNodeId = string;

Expand Down Expand Up @@ -203,7 +204,7 @@ class Anchor extends Component<IProps, IState> {
/* 创建边 */
const { edgeType } = graphModel;
const { endX, endY, dragging } = this.state;
const info = targetNodeInfo({ x: endX, y: endY }, graphModel);
const info = targetNodeInfo({ x: endX, y: endY }, graphModel, AdjustType.TARGET);
// 为了保证鼠标离开的时候,将上一个节点状态重置为正常状态。
if (this.preTargetNode && this.preTargetNode.state !== ElementState.DEFAULT) {
this.preTargetNode.setElementState(ElementState.DEFAULT);
Expand Down Expand Up @@ -255,7 +256,7 @@ class Anchor extends Component<IProps, IState> {
};
moveAnchorEnd(endX: number, endY: number) {
const { graphModel, nodeModel, anchorData } = this.props;
const info = targetNodeInfo({ x: endX, y: endY }, graphModel);
const info = targetNodeInfo({ x: endX, y: endY }, graphModel, AdjustType.TARGET);
if (info) {
const targetNode = info.node;
const anchorId = info.anchor.id;
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/view/edge/AdjustPoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ interface OldEdge {
pointsList: Point[];
}

enum AdjustType {
export enum AdjustType {
SOURCE = 'SOURCE',
TARGET = 'TARGET',
}
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class AdjustPoint extends Component<IProps, IState> {
});
// 调整过程中实时更新路径
const { edgeModel } = this.props;
const info = targetNodeInfo({ x: endX, y: endY }, graphModel);
const info = targetNodeInfo({ x: endX, y: endY }, graphModel, type);
// 如果一定的坐标能够找到目标节点,预结算当前节点与目标节点的路径进行展示
if (info && info.node && this.isAllowAdjust(info).pass) {
let params;
Expand Down Expand Up @@ -149,7 +149,7 @@ export default class AdjustPoint extends Component<IProps, IState> {
// 拖拽AdjustPoint时不修改edgeModel.isHitable,避免偶尔会出现边不能点击问题(https://github.com/didi/LogicFlow/issues/974)
// edgeModel.isHitable = true;
const { endX, endY, dragging } = this.state;
const info = targetNodeInfo({ x: endX, y: endY }, graphModel);
const info = targetNodeInfo({ x: endX, y: endY }, graphModel, type);
// 没有dragging就结束边
if (!dragging) return;
// 如果找到目标节点,删除老边,创建新边
Expand Down