Skip to content

Commit 30c2ed2

Browse files
authored
fix: optimize snapToGrid options for manhattan (#3071)
1 parent 3a020d1 commit 30c2ed2

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

packages/x6/src/registry/router/manhattan/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export const defaults: ManhattanRouterOptions = {
186186

187187
fallbackRouter: orth,
188188
draggingRouter: null,
189-
snapToGrid: false,
189+
snapToGrid: true,
190190
}
191191

192192
export function resolve<T>(

packages/x6/src/registry/router/manhattan/router.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,32 @@ function findRoute(
265265
return null
266266
}
267267

268+
function snap(vertices: Point[], gridSize = 10) {
269+
if (vertices.length <= 1) {
270+
return vertices
271+
}
272+
273+
for (let i = 0, len = vertices.length; i < len - 1; i += 1) {
274+
const first = vertices[i]
275+
const second = vertices[i + 1]
276+
if (first.x === second.x) {
277+
const x = gridSize * Math.round(first.x / gridSize)
278+
if (first.x !== x) {
279+
first.x = x
280+
second.x = x
281+
}
282+
} else if (first.y === second.y) {
283+
const y = gridSize * Math.round(first.y / gridSize)
284+
if (first.y !== y) {
285+
first.y = y
286+
second.y = y
287+
}
288+
}
289+
}
290+
291+
return vertices
292+
}
293+
268294
export const router: Router.Definition<ManhattanRouterOptions> = function (
269295
vertices,
270296
optionsRaw,
@@ -282,7 +308,7 @@ export const router: Router.Definition<ManhattanRouterOptions> = function (
282308
)
283309

284310
const oldVertices = vertices.map((p) => Point.create(p))
285-
let newVertices: Point[] = []
311+
const newVertices: Point[] = []
286312

287313
// The origin of first route's grid, does not need snapping
288314
let tailPoint = sourceEndpoint
@@ -351,10 +377,7 @@ export const router: Router.Definition<ManhattanRouterOptions> = function (
351377
}
352378

353379
if (options.snapToGrid) {
354-
newVertices = newVertices.map((vertice) => {
355-
const gridSize = edgeView.graph.grid.getGridSize()
356-
return vertice.snapToGrid(gridSize)
357-
})
380+
return snap(newVertices, edgeView.graph.grid.getGridSize())
358381
}
359382

360383
return newVertices

0 commit comments

Comments
 (0)