Skip to content

Commit

Permalink
speed up nearestParent layout
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonreid committed Oct 26, 2023
1 parent b7bcd28 commit 63897f1
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions src/workers/layouts/nearestParentVertical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export async function getVerticalNearestParentLayout(message: ClientLayoutMessag
const defaultNearestParentPosition = 0
const minimumNodeEdgeGap = 16
const nodeShoveRecords = new Map<string, NodeShoveDirection>()
const rowTracker = new Map<number, Set<string>>()
let lowestRow = 0

const layout: VerticalLayout = new Map()
Expand Down Expand Up @@ -160,15 +161,17 @@ export async function getVerticalNearestParentLayout(message: ClientLayoutMessag

let positionTaken = false

for (const [nodeId] of layout) {
const rowNodes = rowTracker.get(row) ?? []

for (const nodeId of rowNodes) {
const firstNodePosition = layout.get(nodeId)!
const firstNodeEndX = getNodeEndX(nodeId)

const overlapping = isNodesOverlapping({
firstNodeEndX,
firstNodePosition,
firstNodeRow: firstNodePosition,
lastNodeStartX: nodeStartX,
lastNodePosition: row,
lastNodeRow: row,
})

if (overlapping) {
Expand Down Expand Up @@ -216,37 +219,39 @@ export async function getVerticalNearestParentLayout(message: ClientLayoutMessag

type IsNodesOverlappingProps = {
firstNodeEndX: number,
firstNodePosition: number,
firstNodeRow: number,
lastNodeStartX: number,
lastNodePosition: number,
lastNodeRow: number,
}
function isNodesOverlapping({
firstNodeEndX,
firstNodePosition,
firstNodeRow,
lastNodeStartX,
lastNodePosition,
lastNodeRow,
}: IsNodesOverlappingProps): boolean {
return firstNodePosition === lastNodePosition
return firstNodeRow === lastNodeRow
&& firstNodeEndX + minimumNodeEdgeGap >= lastNodeStartX
}

function getOverlappingNodeIds(nodeStartX: number, position: number): string[] | undefined {
function getOverlappingNodeIds(nodeStartX: number, row: number): string[] | undefined {
const overlappingNodeIds: string[] = []

for (const [nodeId] of layout) {
const rowNodes = rowTracker.get(row) ?? []

for (const nodeId of rowNodes) {
const firstNodeEndX = getNodeEndX(nodeId)
const firstNodePosition = layout.get(nodeId)
const firstNodeRow = layout.get(nodeId)

if (firstNodePosition === undefined) {
if (firstNodeRow === undefined) {
console.warn('NearestParentLayout - getOverlappingNodeIds: Node was not found in the layout', nodeId)
return
}

const isItemOverlapping = isNodesOverlapping({
firstNodeEndX,
firstNodePosition,
firstNodeRow,
lastNodeStartX: nodeStartX,
lastNodePosition: position,
lastNodeRow: row,
})

if (isItemOverlapping) {
Expand Down Expand Up @@ -426,6 +431,17 @@ export async function getVerticalNearestParentLayout(message: ClientLayoutMessag
lowestRow = row
}

if (layout.has(nodeId)) {
const previousRow = layout.get(nodeId)!
rowTracker.get(previousRow)?.delete(nodeId)
}

if (!rowTracker.has(row)) {
rowTracker.set(row, new Set())
}

rowTracker.get(row)?.add(nodeId)

layout.set(nodeId, row)
}

Expand Down

0 comments on commit 63897f1

Please sign in to comment.