Skip to content
Snippets Groups Projects
Unverified Commit 824560dd authored by vector's avatar vector Committed by GitHub
Browse files

feat: add snapToGrid options for manhattan router (#3062)

* docs: update github url

* fix: debounce update to improve performance

* feat: add snapToGrid options for manhattan router
parent 9e37ea9f
No related branches found
No related tags found
Loading
......@@ -12,3 +12,5 @@ export * from './edge-anchor'
export * from './connection-point'
export * from './router'
export * from './connector'
export * from './registry'
......@@ -71,15 +71,17 @@ export class ObstacleMap {
const mapGridSize = this.mapGridSize
model.getNodes().reduce((map, node) => {
const shape = node.shape
const excludeShapes = options.excludeShapes
const excType = shape ? excludeShapes.includes(shape) : false
const excTerminal = excludedTerminals.some((cell) => cell.id === node.id)
const excNode = options.excludeNodes.includes(node)
const excAncestor = excludedAncestors.includes(node.id)
const excHidden = options.excludeHiddenNodes && !node.isVisible()
const excluded =
excType || excTerminal || excNode || excAncestor || excHidden
const excludedTerminal = excludedTerminals.some(
(cell) => cell.id === node.id,
)
const excludedNode = options.excludeNodes.some((item) => {
if (typeof item === 'string') {
return node.id === item
}
return item === node
})
const excludedAncestor = excludedAncestors.includes(node.id)
const excluded = excludedTerminal || excludedNode || excludedAncestor
if (!excluded) {
const bbox = node.getBBox().moveAndExpand(options.paddingBox)
......
......@@ -41,20 +41,10 @@ export interface ResolvedOptions {
*/
excludeTerminals: Edge.TerminalType[]
/**
* Should certain types of nodes not be considered as obstacles?
*/
excludeShapes: string[]
/**
* Should certain hidden nodes not be considered as obstacles?
*/
excludeHiddenNodes: boolean
/**
* Should certain nodes not be considered as obstacles?
*/
excludeNodes: Node[]
excludeNodes: (Node | string)[]
/**
* Possible starting directions from a node.
......@@ -132,6 +122,9 @@ export interface ResolvedOptions {
) => Point[] | null
previousDirectionAngle?: number | null
// Whether the calculation results are aligned with the grid
snapToGrid?: boolean
}
export type ManhattanRouterOptions = {
......@@ -145,9 +138,7 @@ export const defaults: ManhattanRouterOptions = {
maxDirectionChange: 90,
perpendicular: true,
excludeTerminals: [],
excludeShapes: [], // ['text']
excludeNodes: [],
excludeHiddenNodes: false,
startDirections: ['top', 'right', 'bottom', 'left'],
endDirections: ['top', 'right', 'bottom', 'left'],
directionMap: {
......@@ -195,6 +186,7 @@ export const defaults: ManhattanRouterOptions = {
fallbackRouter: orth,
draggingRouter: null,
snapToGrid: true,
}
export function resolve<T>(
......
......@@ -282,7 +282,7 @@ export const router: Router.Definition<ManhattanRouterOptions> = function (
)
const oldVertices = vertices.map((p) => Point.create(p))
const newVertices: Point[] = []
let newVertices: Point[] = []
// The origin of first route's grid, does not need snapping
let tailPoint = sourceEndpoint
......@@ -350,5 +350,12 @@ export const router: Router.Definition<ManhattanRouterOptions> = function (
newVertices.push(...partialRoute)
}
if (options.snapToGrid) {
newVertices = newVertices.map((vertice) => {
const gridSize = edgeView.graph.grid.getGridSize()
return vertice.snapToGrid(gridSize)
})
}
return newVertices
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment