From 0813e54ca299c467c7a1b928ae31079ab19b8876 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mitja=20Bezen=C5=A1ek?= Date: Mon, 4 Mar 2024 18:48:35 +0100 Subject: [PATCH] Fix validation errors for `duplicateProps` (#3065) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- packages/editor/src/lib/primitives/Vec.ts | 3 +++ .../tools/SelectTool/childStates/Translating.ts | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/editor/src/lib/primitives/Vec.ts b/packages/editor/src/lib/primitives/Vec.ts index bd0f0373f..82bf47395 100644 --- a/packages/editor/src/lib/primitives/Vec.ts +++ b/packages/editor/src/lib/primitives/Vec.ts @@ -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]) } diff --git a/packages/tldraw/src/lib/tools/SelectTool/childStates/Translating.ts b/packages/tldraw/src/lib/tools/SelectTool/childStates/Translating.ts index 62d54b6bc..dc3c7e1d3 100644 --- a/packages/tldraw/src/lib/tools/SelectTool/childStates/Translating.ts +++ b/packages/tldraw/src/lib/tools/SelectTool/childStates/Translating.ts @@ -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[] = []