Add support for Microsoft Surface Pen eraser (#1009)

This commit is contained in:
Thai Pangsakulyanont 2022-10-26 19:55:53 +07:00 committed by GitHub
parent 586c320138
commit a85e80961d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 0 deletions

View file

@ -797,3 +797,22 @@ describe('When space panning', () => {
expect(app.currentTool.status).toBe('idle') expect(app.currentTool.status).toBe('idle')
}) })
}) })
describe('When using a pen eraser', () => {
it('switches to eraser tool when using the draw tool and back when letting go', () => {
const app = new TldrawTestApp()
app.selectTool(TDShapeType.Draw)
app.pressEraser()
expect(app.currentTool.type).toBe('erase')
app.releaseEraser()
expect(app.currentTool.type).toBe(TDShapeType.Draw)
})
it('does not switch when using other tools', () => {
const app = new TldrawTestApp()
app.selectTool('select')
app.pressEraser()
expect(app.currentTool.type).toBe('select')
app.releaseEraser()
expect(app.currentTool.type).toBe('select')
})
})

View file

@ -240,6 +240,8 @@ export class TldrawApp extends StateManager<TDSnapshot> {
isForcePanning = false isForcePanning = false
isErasingWithPen = false
isPastePrevented = false isPastePrevented = false
editingStartTime = -1 editingStartTime = -1
@ -3782,6 +3784,10 @@ export class TldrawApp extends StateManager<TDSnapshot> {
this.originPoint = this.getPagePoint(info.point).concat(info.pressure) this.originPoint = this.getPagePoint(info.point).concat(info.pressure)
this.updateInputs(info, e) this.updateInputs(info, e)
if (this.isForcePanning) return if (this.isForcePanning) return
if (this.currentTool.type === TDShapeType.Draw && e.pointerType === 'pen' && e.button === 5) {
this.selectTool('erase')
this.isErasingWithPen = true
}
this.currentTool.onPointerDown?.(info, e) this.currentTool.onPointerDown?.(info, e)
} }
@ -3790,6 +3796,10 @@ export class TldrawApp extends StateManager<TDSnapshot> {
if (!this.shiftKey) this.isForcePanning = false if (!this.shiftKey) this.isForcePanning = false
this.updateInputs(info, e) this.updateInputs(info, e)
this.currentTool.onPointerUp?.(info, e) this.currentTool.onPointerUp?.(info, e)
if (this.isErasingWithPen && e.pointerType === 'pen' && e.button === 5) {
this.selectTool(TDShapeType.Draw)
this.isErasingWithPen = false
}
} }
// Canvas (background) // Canvas (background)

View file

@ -401,6 +401,7 @@ TldrawTestApp {
"insertContent": [Function], "insertContent": [Function],
"isCreating": false, "isCreating": false,
"isDirty": false, "isDirty": false,
"isErasingWithPen": false,
"isForcePanning": false, "isForcePanning": false,
"isPastePrevented": false, "isPastePrevented": false,
"isPaused": false, "isPaused": false,
@ -496,6 +497,7 @@ TldrawTestApp {
"pointCanvas": [Function], "pointCanvas": [Function],
"pointShape": [Function], "pointShape": [Function],
"pointer": -1, "pointer": -1,
"pressEraser": [Function],
"pressKey": [Function], "pressKey": [Function],
"prevSelectedIds": Array [], "prevSelectedIds": Array [],
"preventPaste": [Function], "preventPaste": [Function],
@ -508,6 +510,7 @@ TldrawTestApp {
"redo": [Function], "redo": [Function],
"redoSelect": [Function], "redoSelect": [Function],
"refreshBoundingBoxes": [Function], "refreshBoundingBoxes": [Function],
"releaseEraser": [Function],
"releaseKey": [Function], "releaseKey": [Function],
"removeUser": [Function], "removeUser": [Function],
"renamePage": [Function], "renamePage": [Function],

View file

@ -166,6 +166,13 @@ export class TldrawTestApp extends TldrawApp {
} as PointerEvent } as PointerEvent
} }
getEraser(): React.PointerEvent {
return {
pointerType: 'pen',
button: 5,
} as React.PointerEvent
}
expectSelectedIdsToBe = (b: string[]) => { expectSelectedIdsToBe = (b: string[]) => {
expect(new Set(this.selectedIds)).toEqual(new Set(b)) expect(new Set(this.selectedIds)).toEqual(new Set(b))
return this return this
@ -199,4 +206,12 @@ export class TldrawTestApp extends TldrawApp {
this.onKeyUp(key, inputs.keyup(e), e) this.onKeyUp(key, inputs.keyup(e), e)
return this return this
} }
pressEraser = () => {
this.onPointerDown(inputs.pointerDown(this.getPoint(), 'canvas'), this.getEraser())
}
releaseEraser = () => {
this.onPointerUp(inputs.pointerUp(this.getPoint(), 'canvas'), this.getEraser())
}
} }