Skip to content

Commit 1a00261

Browse files
committed
wip: tmp
1 parent b7b08fb commit 1a00261

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

packages/core/src/LogicFlow.tsx

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

129129
if (initOptions.snapline !== false) {
130-
this.snaplineModel = new SnaplineModel(this.graphModel)
130+
this.snaplineModel = new SnaplineModel(this.graphModel, initOptions.snaplineEpsilon)
131131
snapline(eventCenter, this.snaplineModel)
132132
}
133133
if (!initOptions.isSilentMode) {

packages/core/src/model/SnaplineModel.ts

Lines changed: 56 additions & 24 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,29 +149,49 @@ 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)
153-
// 如果节点的最大最小X轴坐标与节点的最大最小X轴坐标相等,展示垂直线
154-
if (
155-
itemData.minX === draggingData?.minX ||
156-
itemData.maxX === draggingData?.minX
157-
) {
158-
// 找到则停止循环。减少不必要的遍历
159-
isShowVertical = true
160-
verticalX = draggingData.minX
161-
break
162-
}
163-
if (
164-
itemData.minX === draggingData?.maxX ||
165-
itemData.maxX === draggingData?.maxX
166-
) {
167-
isShowVertical = true
168-
verticalX = draggingData.maxX
169-
break
158+
// 如果拖拽对象与另外的节点 x 方向靠近,将拖拽对象和鼠标的位置设置为 节点的 x 值(吸附效果)
159+
const draggingBoundings = [draggingData?.minX, draggingData?.maxX]
160+
const itemBoundings = [itemData.minX, itemData.maxX]
161+
162+
outerLoop: for (let i = 0; i < 2; i++) {
163+
for (let j = 0; j < 2; j++) {
164+
if (equal(draggingBoundings[i], itemBoundings[j], this.epsilon)) {
165+
isShowVertical = true
166+
verticalX = itemBoundings[i]
167+
168+
break outerLoop
169+
}
170+
}
170171
}
172+
173+
// if (equal(itemData.minX, draggingData?.minX, this.epsilon)) {
174+
// isShowVertical = true
175+
// verticalX = draggingData.minX
176+
// break;
177+
// }
178+
// if (
179+
// equal(itemData.minX, draggingData?.minX, this.epsilon) ||
180+
// equal(itemData.maxX, draggingData?.minX, this.epsilon)
181+
// ) {
182+
// // 找到则停止循环。减少不必要的遍历
183+
// isShowVertical = true
184+
// verticalX = draggingData.minX
185+
// break
186+
// }
187+
// if (
188+
// equal(itemData.minX, draggingData?.maxX, this.epsilon) ||
189+
// equal(itemData.maxX, draggingData?.maxX, this.epsilon)
190+
// ) {
191+
// isShowVertical = true
192+
// verticalX = draggingData.maxX
193+
// break
194+
// }
171195
}
172196
}
173197
return {
@@ -235,4 +259,12 @@ export class SnaplineModel {
235259
}
236260
}
237261

262+
function equal(num1: number, num2: number, epsilon: number) {
263+
if (Math.abs(num1 - num2) <= epsilon) {
264+
return true
265+
} else {
266+
return false
267+
}
268+
}
269+
238270
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)