Skip to content

Commit 3fd36c0

Browse files
committed
fix(snapline): snapline cannot show when zoomed & add a build line to CONTRIBUTING.md
1 parent b7b08fb commit 3fd36c0

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

packages/core/src/LogicFlow.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,10 @@ export class LogicFlow {
127127
})
128128

129129
if (initOptions.snapline !== false) {
130-
this.snaplineModel = new SnaplineModel(this.graphModel)
130+
this.snaplineModel = new SnaplineModel(
131+
this.graphModel,
132+
initOptions.snaplineEpsilon,
133+
)
131134
snapline(eventCenter, this.snaplineModel)
132135
}
133136
if (!initOptions.isSilentMode) {

packages/core/src/model/SnaplineModel.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ export class SnaplineModel {
1919
@observable isShowVertical: boolean
2020
// 对齐线的中心位置,目前仅展示中心对齐的情况,后面可以考虑多种对齐策略
2121
@observable position: Position
22+
// 对齐线显示的阈值距离
23+
epsilon: number
2224

23-
constructor(graphModel: GraphModel) {
25+
constructor(graphModel: GraphModel, epsilon: number = 1) {
2426
this.isShowHorizontal = false
2527
this.isShowVertical = false
2628
this.position = {
2729
x: 0,
2830
y: 0,
2931
}
3032
this.graphModel = graphModel
33+
this.epsilon = epsilon
3134
}
3235

3336
getStyle() {
@@ -48,10 +51,10 @@ export class SnaplineModel {
4851
const item = nodes[i]
4952
// 排除当前节点
5053
if (item.id !== draggingNode.id) {
51-
if (x === item.x) {
54+
if (equal(x, item.x, this.epsilon)) {
5255
isShowVertical = true
5356
}
54-
if (y === item.y) {
57+
if (equal(y, item.y, this.epsilon)) {
5558
isShowHorizontal = true
5659
}
5760
// 如果水平垂直都显示,则停止循环。减少不必要的遍历
@@ -95,19 +98,20 @@ export class SnaplineModel {
9598
// 排除当前节点
9699
if (item.id !== draggingNode.id) {
97100
const itemData = getNodeBBox(item)
101+
98102
// 如果节点的最大最小Y轴坐标与节点的最大最小Y轴坐标相等,展示水平线
99103
if (
100-
itemData.minY === draggingData?.minY ||
101-
itemData.maxY === draggingData?.minY
104+
equal(itemData.minY, draggingData?.minY, this.epsilon) ||
105+
equal(itemData.maxY, draggingData?.minY, this.epsilon)
102106
) {
103107
// 找到则停止循环。减少不必要的遍历
104108
isShowHorizontal = true
105109
horizontalY = draggingData.minY
106110
break
107111
}
108112
if (
109-
itemData.minY === draggingData?.maxY ||
110-
itemData.maxY === draggingData?.maxY
113+
equal(itemData.minY, draggingData?.maxY, this.epsilon) ||
114+
equal(itemData.maxY, draggingData?.maxY, this.epsilon)
111115
) {
112116
isShowHorizontal = true
113117
horizontalY = draggingData.maxY
@@ -145,24 +149,30 @@ export class SnaplineModel {
145149
}
146150
}
147151
}
152+
148153
for (let i = 0; i < nodes.length; i++) {
149154
const item = nodes[i]
150155
// 排除当前节点
151156
if (item.id !== draggingNode.id) {
152157
const itemData = getNodeBBox(item)
153158
// 如果节点的最大最小X轴坐标与节点的最大最小X轴坐标相等,展示垂直线
159+
if (equal(itemData.minX, draggingData?.minX, this.epsilon)) {
160+
isShowVertical = true
161+
verticalX = draggingData.minX
162+
break
163+
}
154164
if (
155-
itemData.minX === draggingData?.minX ||
156-
itemData.maxX === draggingData?.minX
165+
equal(itemData.minX, draggingData?.minX, this.epsilon) ||
166+
equal(itemData.maxX, draggingData?.minX, this.epsilon)
157167
) {
158168
// 找到则停止循环。减少不必要的遍历
159169
isShowVertical = true
160170
verticalX = draggingData.minX
161171
break
162172
}
163173
if (
164-
itemData.minX === draggingData?.maxX ||
165-
itemData.maxX === draggingData?.maxX
174+
equal(itemData.minX, draggingData?.maxX, this.epsilon) ||
175+
equal(itemData.maxX, draggingData?.maxX, this.epsilon)
166176
) {
167177
isShowVertical = true
168178
verticalX = draggingData.maxX
@@ -235,4 +245,12 @@ export class SnaplineModel {
235245
}
236246
}
237247

248+
function equal(num1: number, num2: number, epsilon: number) {
249+
if (Math.abs(num1 - num2) <= epsilon) {
250+
return true
251+
} else {
252+
return false
253+
}
254+
}
255+
238256
export default SnaplineModel

packages/core/src/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export namespace Options {
8888
history?: boolean
8989
outline?: boolean
9090
snapline?: boolean
91+
snaplineEpsilon?: number
9192
textEdit?: boolean
9293

9394
guards?: GuardsConfig

0 commit comments

Comments
 (0)