mirror of
https://github.com/stablyai/orca.git
synced 2026-10-12 16:03:03 +00:00
perf: measure sidebar row geometry once before sorting delete anchors
This commit is contained in:
@@ -171,9 +171,10 @@ export function preserveDeleteSiblingPosition(scope: HTMLElement | null): () =>
|
||||
if (!(sidebar instanceof HTMLElement) || !(row instanceof HTMLElement)) {
|
||||
return () => {}
|
||||
}
|
||||
const rows = Array.from(
|
||||
sidebar.querySelectorAll<HTMLElement>('[data-worktree-virtual-row]')
|
||||
).sort((a, b) => a.getBoundingClientRect().top - b.getBoundingClientRect().top)
|
||||
const rows = Array.from(sidebar.querySelectorAll<HTMLElement>('[data-worktree-virtual-row]'))
|
||||
.map((element) => ({ element, top: element.getBoundingClientRect().top }))
|
||||
.sort((a, b) => a.top - b.top)
|
||||
.map(({ element }) => element)
|
||||
const rowIndex = rows.indexOf(row)
|
||||
const anchorRow = rows[rowIndex + 1] ?? rows[rowIndex - 1] ?? null
|
||||
const anchorKey = anchorRow?.getAttribute('data-worktree-virtual-row-key')
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { expect, it } from 'vitest'
|
||||
import { preserveDeleteSiblingPosition } from './worktree-context-menu-policy'
|
||||
|
||||
it('measures each mounted sidebar row once when choosing a delete-position anchor', () => {
|
||||
const sidebar = document.createElement('div')
|
||||
sidebar.setAttribute('data-worktree-sidebar', '')
|
||||
let measurements = 0
|
||||
const rows = Array.from({ length: 200 }, (_, index) => {
|
||||
const row = document.createElement('div')
|
||||
row.setAttribute('data-worktree-virtual-row', '')
|
||||
row.setAttribute('data-worktree-virtual-row-key', String(index))
|
||||
row.getBoundingClientRect = () => {
|
||||
measurements += 1
|
||||
return { top: (index * 73) % 200 } as DOMRect
|
||||
}
|
||||
sidebar.append(row)
|
||||
return row
|
||||
})
|
||||
expect(typeof preserveDeleteSiblingPosition(rows[100])).toBe('function')
|
||||
expect(measurements).toBeLessThanOrEqual(201)
|
||||
})
|
||||
Reference in New Issue
Block a user