ensure split indices are sorted

This commit is contained in:
Preet 2023-10-23 17:29:12 -07:00
parent 3700cf2d10
commit 539071fcfe

View File

@ -1042,13 +1042,15 @@ export class LinearElementEditor {
let offsetX = 0; let offsetX = 0;
let offsetY = 0; let offsetY = 0;
const isDeletingOriginPoint = pointIndices.includes(0); const indexSet = new Set(pointIndices);
const isDeletingOriginPoint = indexSet.has(0);
// if deleting first point, make the next to be [0,0] and recalculate // if deleting first point, make the next to be [0,0] and recalculate
// positions of the rest with respect to it // positions of the rest with respect to it
if (isDeletingOriginPoint) { if (isDeletingOriginPoint) {
const firstNonDeletedPoint = element.points.find((point, idx) => { const firstNonDeletedPoint = element.points.find((point, idx) => {
return !pointIndices.includes(idx); return !indexSet.has(idx);
}); });
if (firstNonDeletedPoint) { if (firstNonDeletedPoint) {
offsetX = firstNonDeletedPoint[0]; offsetX = firstNonDeletedPoint[0];
@ -1057,7 +1059,7 @@ export class LinearElementEditor {
} }
const nextPoints = element.points.reduce((acc: Point[], point, idx) => { const nextPoints = element.points.reduce((acc: Point[], point, idx) => {
if (!pointIndices.includes(idx)) { if (!indexSet.has(idx)) {
acc.push( acc.push(
!acc.length ? [0, 0] : [point[0] - offsetX, point[1] - offsetY], !acc.length ? [0, 0] : [point[0] - offsetX, point[1] - offsetY],
); );
@ -1067,7 +1069,7 @@ export class LinearElementEditor {
const splits: number[] = []; const splits: number[] = [];
(element.segmentSplitIndices || []).forEach((index) => { (element.segmentSplitIndices || []).forEach((index) => {
if (!pointIndices.includes(index)) { if (!indexSet.has(index)) {
let shift = 0; let shift = 0;
for (const pointIndex of pointIndices) { for (const pointIndex of pointIndices) {
if (index > pointIndex) { if (index > pointIndex) {
@ -1079,7 +1081,7 @@ export class LinearElementEditor {
}); });
LinearElementEditor._updatePoints(element, nextPoints, offsetX, offsetY, { LinearElementEditor._updatePoints(element, nextPoints, offsetX, offsetY, {
segmentSplitIndices: splits, segmentSplitIndices: splits.sort((a, b) => a - b),
}); });
} }
@ -1225,7 +1227,7 @@ export class LinearElementEditor {
mutateElement(element, { mutateElement(element, {
points, points,
segmentSplitIndices: splits, segmentSplitIndices: splits.sort((a, b) => a - b),
}); });
ret.pointerDownState = { ret.pointerDownState = {
@ -1513,7 +1515,7 @@ export class LinearElementEditor {
} }
mutateElement(element, { mutateElement(element, {
segmentSplitIndices: splitIndices.sort(), segmentSplitIndices: splitIndices.sort((a, b) => a - b),
}); });
} }
} }