[bugfix?] End interactions before switching page (#3771)

Looking at #3762 it seemed to have been caused by calling
`setCurrentPage` during a `select.editing_shape` interaction. I wonder
whether we should trigger a `cancel` event before switching pages in
case this happens?

closes #3762 

### Change Type

<!--  Please select a 'Scope' label ️ -->

- [x] `sdk` — Changes the tldraw SDK
- [ ] `dotcom` — Changes the tldraw.com web app
- [ ] `docs` — Changes to the documentation, examples, or templates.
- [ ] `vs code` — Changes to the vscode plugin
- [ ] `internal` — Does not affect user-facing stuff

<!--  Please select a 'Type' label ️ -->

- [x] `bugfix` — Bug fix
- [ ] `feature` — New feature
- [ ] `improvement` — Improving existing features
- [ ] `chore` — Updating dependencies, other boring stuff
- [ ] `galaxy brain` — Architectural changes
- [ ] `tests` — Changes to any test code
- [ ] `tools` — Changes to infrastructure, CI, internal scripts,
debugging tools, etc.
- [ ] `dunno` — I don't know


### 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:
David Sheldrick 2024-05-20 13:45:00 +01:00 committed by GitHub
parent ed63bcead5
commit 2cf8104f5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 0 deletions

View file

@ -3596,6 +3596,8 @@ export class Editor extends EventEmitter<TLEventMap> {
}
this.stopFollowingUser()
// finish off any in-progress interactions
this.complete()
return this.batch(
() => this.store.put([{ ...this.getInstanceState(), currentPageId: pageId }]),

View file

@ -85,4 +85,22 @@ describe('setCurrentPage', () => {
expect(console.error).toHaveBeenCalled()
expect(editor.getCurrentPageId()).toBe(initialPageId)
})
it('cancels any in-progress actions', () => {
const page1Id = editor.getPages()[0].id
const page2Id = PageRecordType.createId('page2')
editor.createPage({ name: 'New Page 2', id: page2Id })
expect(editor.getCurrentPageId()).toBe(page1Id)
const geoId = createShapeId()
editor.createShape({ type: 'geo', id: geoId, props: { w: 100, h: 100 } })
editor.select(geoId)
editor.keyUp('Enter')
expect(editor.isIn('select.editing_shape')).toBe(true)
editor.setCurrentPage(page2Id)
expect(editor.isIn('select.idle')).toBe(true)
})
})