Fix validation errors for duplicateProps (#3065)

Should fix `At instance.duplicateProps.offset.x: Expected a number, got
NaN` validation errors.

Wasn't able to reproduce. We only assign the offset here, so
`Vec.Averge` is the most likely offender here and for that to happen I
guess `movingShapes` might not contain any shapes.

### Change Type

- [x] `patch` — Bug fix
- [ ] `minor` — New feature
- [ ] `major` — Breaking change
- [ ] `dependencies` — Changes to package dependencies[^1]
- [ ] `documentation` — Changes to the documentation only[^2]
- [ ] `tests` — Changes to any test code only[^2]
- [ ] `internal` — Any other changes that don't affect the published
package[^2]
- [ ] I don't know

[^1]: publishes a `patch` release, for devDependencies use `internal`
[^2]: will not publish a new version

### Test Plan

1. Add a step-by-step description of how to test your PR here.
2.

- [ ] Unit Tests
- [ ] End to end tests

### Release Notes

- Add a brief release note for your PR here.
This commit is contained in:
Mitja Bezenšek 2024-03-04 18:48:35 +01:00 committed by GitHub
parent 33d111f93e
commit 0813e54ca2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 7 deletions

View file

@ -531,6 +531,9 @@ export class Vec {
static Average(arr: VecLike[]) {
const len = arr.length
const avg = new Vec(0, 0)
if (len === 0) {
return avg
}
for (let i = 0; i < len; i++) {
avg.add(arr[i])
}

View file

@ -213,17 +213,19 @@ export class Translating extends StateNode {
protected handleEnd() {
const { movingShapes } = this.snapshot
if (this.isCloning) {
if (this.isCloning && movingShapes.length > 0) {
const currentAveragePagePoint = Vec.Average(
movingShapes.map((s) => this.editor.getShapePageTransform(s.id)!.point())
)
const offset = Vec.Sub(currentAveragePagePoint, this.selectionSnapshot.averagePagePoint)
this.editor.updateInstanceState({
duplicateProps: {
shapeIds: movingShapes.map((s) => s.id),
offset: { x: offset.x, y: offset.y },
},
})
if (!Vec.IsNaN(offset)) {
this.editor.updateInstanceState({
duplicateProps: {
shapeIds: movingShapes.map((s) => s.id),
offset: { x: offset.x, y: offset.y },
},
})
}
}
const changes: TLShapePartial[] = []