[fix] intersection bug (#452)
* Update TLShapeUtil.tsx * Add tests * Update tsconfig.json
This commit is contained in:
parent
34492f2f01
commit
58b8968f12
4 changed files with 21 additions and 5 deletions
|
@ -2,7 +2,7 @@
|
||||||
import * as React from 'react'
|
import * as React from 'react'
|
||||||
import Utils from '../utils'
|
import Utils from '../utils'
|
||||||
import type { TLBounds, TLComponentProps, TLForwardedRef, TLShape, TLUser } from '../types'
|
import type { TLBounds, TLComponentProps, TLForwardedRef, TLShape, TLUser } from '../types'
|
||||||
import { intersectPolylineBounds } from '@tldraw/intersect'
|
import { intersectPolygonBounds, intersectPolylineBounds } from '@tldraw/intersect'
|
||||||
|
|
||||||
export abstract class TLShapeUtil<T extends TLShape, E extends Element = any, M = any> {
|
export abstract class TLShapeUtil<T extends TLShape, E extends Element = any, M = any> {
|
||||||
refMap = new Map<string, React.RefObject<E>>()
|
refMap = new Map<string, React.RefObject<E>>()
|
||||||
|
@ -42,7 +42,7 @@ export abstract class TLShapeUtil<T extends TLShape, E extends Element = any, M
|
||||||
const corners = Utils.getRotatedCorners(shapeBounds, shape.rotation)
|
const corners = Utils.getRotatedCorners(shapeBounds, shape.rotation)
|
||||||
return (
|
return (
|
||||||
corners.every((point) => Utils.pointInBounds(point, bounds)) ||
|
corners.every((point) => Utils.pointInBounds(point, bounds)) ||
|
||||||
intersectPolylineBounds(corners, bounds).length > 0
|
intersectPolygonBounds(corners, bounds).length > 0
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -664,7 +664,9 @@ describe('TldrawTestApp', () => {
|
||||||
const shapes = mockDocument.pages.page1.shapes
|
const shapes = mockDocument.pages.page1.shapes
|
||||||
const bindings = mockDocument.pages.page1.bindings
|
const bindings = mockDocument.pages.page1.bindings
|
||||||
const app = new TldrawTestApp('multiplayer', {
|
const app = new TldrawTestApp('multiplayer', {
|
||||||
onChangePage: () => {},
|
onChangePage: () => {
|
||||||
|
//
|
||||||
|
},
|
||||||
}).createPage()
|
}).createPage()
|
||||||
app.replacePageContent(shapes, bindings)
|
app.replacePageContent(shapes, bindings)
|
||||||
|
|
||||||
|
@ -676,7 +678,9 @@ describe('TldrawTestApp', () => {
|
||||||
const shapes = mockDocument.pages.page1.shapes
|
const shapes = mockDocument.pages.page1.shapes
|
||||||
const bindings = mockDocument.pages.page1.bindings
|
const bindings = mockDocument.pages.page1.bindings
|
||||||
const app = new TldrawTestApp('multiplayer', {
|
const app = new TldrawTestApp('multiplayer', {
|
||||||
onChangePage: () => {},
|
onChangePage: () => {
|
||||||
|
//
|
||||||
|
},
|
||||||
}).createPage()
|
}).createPage()
|
||||||
app.setSetting('isDebugMode', true)
|
app.setSetting('isDebugMode', true)
|
||||||
app.replacePageContent(shapes, bindings)
|
app.replacePageContent(shapes, bindings)
|
||||||
|
@ -685,4 +689,14 @@ describe('TldrawTestApp', () => {
|
||||||
expect(app.bindings).toEqual(Object.values(bindings))
|
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'])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -8,7 +8,7 @@ describe('Brush session', () => {
|
||||||
.selectNone()
|
.selectNone()
|
||||||
.movePointer([-48, -48])
|
.movePointer([-48, -48])
|
||||||
.startSession(SessionType.Brush)
|
.startSession(SessionType.Brush)
|
||||||
.movePointer([48, 48])
|
.movePointer([10, 10])
|
||||||
.completeSession()
|
.completeSession()
|
||||||
expect(app.status).toBe(TDStatus.Idle)
|
expect(app.status).toBe(TDStatus.Idle)
|
||||||
expect(app.selectedIds.length).toBe(1)
|
expect(app.selectedIds.length).toBe(1)
|
||||||
|
|
|
@ -33,6 +33,8 @@ export class BrushSession extends BaseSession {
|
||||||
bounds: this.app.getShapeUtil(shape).getBounds(shape),
|
bounds: this.app.getShapeUtil(shape).getBounds(shape),
|
||||||
selectId: shape.id, //TLDR.getTopParentId(data, shape.id, currentPageId),
|
selectId: shape.id, //TLDR.getTopParentId(data, shape.id, currentPageId),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
this.update()
|
||||||
}
|
}
|
||||||
|
|
||||||
start = (): TldrawPatch | undefined => void null
|
start = (): TldrawPatch | undefined => void null
|
||||||
|
|
Loading…
Reference in a new issue