简化合并

This commit is contained in:
satori 2025-01-15 19:26:44 +08:00
parent 9c44a4d425
commit 04c3f57218

View File

@ -111,13 +111,10 @@ element.onclick = (event) => {
return { index: i, rect: range.getBoundingClientRect() } return { index: i, rect: range.getBoundingClientRect() }
}) })
const closest = positions.reduce( const closest = positions.reduce((closest, pos) => {
(closest, pos) => {
const dist = Math.abs(x - pos.rect.left) const dist = Math.abs(x - pos.rect.left)
return dist < closest.distance ? { ...pos, distance: dist } : closest return dist < closest.distance ? { ...pos, distance: dist } : closest
}, }, { index: -1, distance: Infinity })
{ index: -1, distance: Infinity }
)
const rect = closest.rect const rect = closest.rect
const insertBefore = x < rect.left + rect.width / 2 const insertBefore = x < rect.left + rect.width / 2