(
blocks: Record<string, BlockState>,
verticalSpacing: number,
options: ResolveNoteOverlapsOptions = {}
)
| 401 | * overlaps triggered the relocation. |
| 402 | */ |
| 403 | export function resolveNoteOverlaps( |
| 404 | blocks: Record<string, BlockState>, |
| 405 | verticalSpacing: number, |
| 406 | options: ResolveNoteOverlapsOptions = {} |
| 407 | ): void { |
| 408 | const { previousBlocks } = options |
| 409 | const { root, children } = getBlocksByParent(blocks) |
| 410 | const groups: string[][] = [root, ...Array.from(children.values())] |
| 411 | |
| 412 | for (const groupIds of groups) { |
| 413 | const obstacles: Array<{ id: string; box: BoundingBox }> = [] |
| 414 | const noteIds: string[] = [] |
| 415 | let maxBottom = Number.NEGATIVE_INFINITY |
| 416 | let minX = Number.POSITIVE_INFINITY |
| 417 | |
| 418 | for (const id of groupIds) { |
| 419 | const block = blocks[id] |
| 420 | // Skip non-finite positions so corrupted coordinates never propagate into |
| 421 | // minX / maxBottom (and therefore into relocated note positions). |
| 422 | if (!block || !hasFinitePosition(block)) continue |
| 423 | |
| 424 | if (block.type === NOTE_BLOCK_TYPE) { |
| 425 | noteIds.push(id) |
| 426 | const { height } = getNoteDimensions(block) |
| 427 | maxBottom = Math.max(maxBottom, block.position.y + height) |
| 428 | continue |
| 429 | } |
| 430 | |
| 431 | if (shouldSkipAutoLayout(block)) continue |
| 432 | |
| 433 | const box = createBoundingBox(block.position, getBlockMetrics(block)) |
| 434 | obstacles.push({ id, box }) |
| 435 | maxBottom = Math.max(maxBottom, box.y + box.height) |
| 436 | minX = Math.min(minX, box.x) |
| 437 | } |
| 438 | |
| 439 | if (noteIds.length === 0 || obstacles.length === 0) continue |
| 440 | |
| 441 | noteIds.sort((a, b) => { |
| 442 | const posA = blocks[a].position |
| 443 | const posB = blocks[b].position |
| 444 | return posA.y - posB.y || posA.x - posB.x |
| 445 | }) |
| 446 | |
| 447 | let stackY = maxBottom + verticalSpacing |
| 448 | |
| 449 | for (const id of noteIds) { |
| 450 | const note = blocks[id] |
| 451 | const dimensions = getNoteDimensions(note) |
| 452 | const noteBox = createBoundingBox(note.position, dimensions) |
| 453 | |
| 454 | const needsRelocation = obstacles.some(({ id: blockId, box }) => { |
| 455 | if (!boxesOverlap(box, noteBox)) return false |
| 456 | if (previousBlocks) { |
| 457 | return !noteOverlappedBlockBefore(previousBlocks, id, blockId) |
| 458 | } |
| 459 | return true |
| 460 | }) |
no test coverage detected