Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

speed up nearestParent layout #275

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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