From 58b8968f12b1d0218e41256ce030bdb77cc09145 Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Fri, 17 Dec 2021 16:29:08 +0000 Subject: [PATCH] [fix] intersection bug (#452) * Update TLShapeUtil.tsx * Add tests * Update tsconfig.json --- packages/core/src/TLShapeUtil/TLShapeUtil.tsx | 4 ++-- packages/tldraw/src/state/TldrawApp.spec.ts | 18 ++++++++++++++++-- .../sessions/BrushSession/BrushSession.spec.ts | 2 +- .../sessions/BrushSession/BrushSession.ts | 2 ++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/core/src/TLShapeUtil/TLShapeUtil.tsx b/packages/core/src/TLShapeUtil/TLShapeUtil.tsx index 95c57ca86..aec954353 100644 --- a/packages/core/src/TLShapeUtil/TLShapeUtil.tsx +++ b/packages/core/src/TLShapeUtil/TLShapeUtil.tsx @@ -2,7 +2,7 @@ import * as React from 'react' import Utils from '../utils' import type { TLBounds, TLComponentProps, TLForwardedRef, TLShape, TLUser } from '../types' -import { intersectPolylineBounds } from '@tldraw/intersect' +import { intersectPolygonBounds, intersectPolylineBounds } from '@tldraw/intersect' export abstract class TLShapeUtil { refMap = new Map>() @@ -42,7 +42,7 @@ export abstract class TLShapeUtil Utils.pointInBounds(point, bounds)) || - intersectPolylineBounds(corners, bounds).length > 0 + intersectPolygonBounds(corners, bounds).length > 0 ) } diff --git a/packages/tldraw/src/state/TldrawApp.spec.ts b/packages/tldraw/src/state/TldrawApp.spec.ts index d76cc7808..41dcd2085 100644 --- a/packages/tldraw/src/state/TldrawApp.spec.ts +++ b/packages/tldraw/src/state/TldrawApp.spec.ts @@ -664,7 +664,9 @@ describe('TldrawTestApp', () => { const shapes = mockDocument.pages.page1.shapes const bindings = mockDocument.pages.page1.bindings const app = new TldrawTestApp('multiplayer', { - onChangePage: () => {}, + onChangePage: () => { + // + }, }).createPage() app.replacePageContent(shapes, bindings) @@ -676,7 +678,9 @@ describe('TldrawTestApp', () => { const shapes = mockDocument.pages.page1.shapes const bindings = mockDocument.pages.page1.bindings const app = new TldrawTestApp('multiplayer', { - onChangePage: () => {}, + onChangePage: () => { + // + }, }).createPage() app.setSetting('isDebugMode', true) app.replacePageContent(shapes, bindings) @@ -685,4 +689,14 @@ describe('TldrawTestApp', () => { expect(app.bindings).toEqual(Object.values(bindings)) }) }) + + describe('When selecting a box', () => { + const app = new TldrawTestApp() + app + .createShapes({ id: 'box1', type: TDShapeType.Rectangle, point: [0, 0], size: [100, 100] }) + .pointCanvas([-50, 20]) + .movePointer([50, 50]) + .movePointer([50, 51]) + .expectSelectedIdsToBe(['box1']) + }) }) diff --git a/packages/tldraw/src/state/sessions/BrushSession/BrushSession.spec.ts b/packages/tldraw/src/state/sessions/BrushSession/BrushSession.spec.ts index 434be1b8a..b966214aa 100644 --- a/packages/tldraw/src/state/sessions/BrushSession/BrushSession.spec.ts +++ b/packages/tldraw/src/state/sessions/BrushSession/BrushSession.spec.ts @@ -8,7 +8,7 @@ describe('Brush session', () => { .selectNone() .movePointer([-48, -48]) .startSession(SessionType.Brush) - .movePointer([48, 48]) + .movePointer([10, 10]) .completeSession() expect(app.status).toBe(TDStatus.Idle) expect(app.selectedIds.length).toBe(1) diff --git a/packages/tldraw/src/state/sessions/BrushSession/BrushSession.ts b/packages/tldraw/src/state/sessions/BrushSession/BrushSession.ts index 8753a0ec1..3781d2693 100644 --- a/packages/tldraw/src/state/sessions/BrushSession/BrushSession.ts +++ b/packages/tldraw/src/state/sessions/BrushSession/BrushSession.ts @@ -33,6 +33,8 @@ export class BrushSession extends BaseSession { bounds: this.app.getShapeUtil(shape).getBounds(shape), selectId: shape.id, //TLDR.getTopParentId(data, shape.id, currentPageId), })) + + this.update() } start = (): TldrawPatch | undefined => void null