Cleans up distribution code

This commit is contained in:
Steve Ruiz 2021-05-26 22:53:31 +01:00
parent 21927845a8
commit f0afe62073

View file

@ -48,22 +48,14 @@ export default function distributeCommand(data: Data, type: DistributeType) {
switch (type) { switch (type) {
case DistributeType.Horizontal: { case DistributeType.Horizontal: {
const sortedByCenter = entries.sort( const span = entries.reduce((a, c) => a + c[1].width, 0)
([a], [b]) => centers[a][0] - centers[b][0]
)
const span = sortedByCenter.reduce((a, c) => a + c[1].width, 0)
if (span > commonBounds.width) { if (span > commonBounds.width) {
const left = sortedByCenter.sort( const left = entries.sort((a, b) => a[1].minX - b[1].minX)[0]
(a, b) => a[1].minX - b[1].minX
)[0]
const right = sortedByCenter.sort( const right = entries.sort((a, b) => b[1].maxX - a[1].maxX)[0]
(a, b) => b[1].maxX - a[1].maxX
)[0]
const entriesToMove = sortedByCenter const entriesToMove = entries
.filter((a) => a !== left && a !== right) .filter((a) => a !== left && a !== right)
.sort((a, b) => centers[a[0]][0] - centers[b[0]][0]) .sort((a, b) => centers[a[0]][0] - centers[b[0]][0])
@ -81,8 +73,12 @@ export default function distributeCommand(data: Data, type: DistributeType) {
]) ])
} }
} else { } else {
const step = (commonBounds.width - span) / (len - 1) const sortedByCenter = entries.sort(
(a, b) => centers[a[0]][0] - centers[b[0]][0]
)
let x = commonBounds.minX let x = commonBounds.minX
const step = (commonBounds.width - span) / (len - 1)
for (let i = 0; i < sortedByCenter.length - 1; i++) { for (let i = 0; i < sortedByCenter.length - 1; i++) {
const [id, bounds] = sortedByCenter[i] const [id, bounds] = sortedByCenter[i]
@ -94,22 +90,14 @@ export default function distributeCommand(data: Data, type: DistributeType) {
break break
} }
case DistributeType.Vertical: { case DistributeType.Vertical: {
const sortedByCenter = entries.sort( const span = entries.reduce((a, c) => a + c[1].height, 0)
([a], [b]) => centers[a][1] - centers[b][1]
)
const span = sortedByCenter.reduce((a, c) => a + c[1].height, 0)
if (span > commonBounds.height) { if (span > commonBounds.height) {
const top = sortedByCenter.sort( const top = entries.sort((a, b) => a[1].minY - b[1].minY)[0]
(a, b) => a[1].minY - b[1].minY
)[0]
const bottom = sortedByCenter.sort( const bottom = entries.sort((a, b) => b[1].maxY - a[1].maxY)[0]
(a, b) => b[1].maxY - a[1].maxY
)[0]
const entriesToMove = sortedByCenter const entriesToMove = entries
.filter((a) => a !== top && a !== bottom) .filter((a) => a !== top && a !== bottom)
.sort((a, b) => centers[a[0]][1] - centers[b[0]][1]) .sort((a, b) => centers[a[0]][1] - centers[b[0]][1])
@ -127,8 +115,12 @@ export default function distributeCommand(data: Data, type: DistributeType) {
]) ])
} }
} else { } else {
const step = (commonBounds.height - span) / (len - 1) const sortedByCenter = entries.sort(
(a, b) => centers[a[0]][1] - centers[b[0]][1]
)
let y = commonBounds.minY let y = commonBounds.minY
const step = (commonBounds.height - span) / (len - 1)
for (let i = 0; i < sortedByCenter.length - 1; i++) { for (let i = 0; i < sortedByCenter.length - 1; i++) {
const [id, bounds] = sortedByCenter[i] const [id, bounds] = sortedByCenter[i]