From d60bb5c6ee3a22bbb7f416af276bbd9f0cb4176c Mon Sep 17 00:00:00 2001 From: Steve Ruiz Date: Mon, 27 Dec 2021 18:44:47 +0000 Subject: [PATCH] Fix bug where reloading could clear the state (#465) * Fix bug where reloading could clear the state * fix test * Fix image and video positions --- .../src/state/StateManager/StateManager.ts | 2 ++ packages/tldraw/src/state/TldrawApp.spec.ts | 31 +++++-------------- packages/tldraw/src/state/TldrawApp.ts | 11 ++++--- .../src/state/shapes/ImageUtil/ImageUtil.tsx | 4 +++ .../src/state/shapes/VideoUtil/VideoUtil.tsx | 9 +++++- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/packages/tldraw/src/state/StateManager/StateManager.ts b/packages/tldraw/src/state/StateManager/StateManager.ts index 57c49a70e..1d3ae2e68 100644 --- a/packages/tldraw/src/state/StateManager/StateManager.ts +++ b/packages/tldraw/src/state/StateManager/StateManager.ts @@ -120,6 +120,8 @@ export class StateManager> { * Save the current state to indexdb. */ protected persist = (id?: string): void | Promise => { + if (this._status !== 'ready') return + if (this.onPersist) { this.onPersist(this._state, id) } diff --git a/packages/tldraw/src/state/TldrawApp.spec.ts b/packages/tldraw/src/state/TldrawApp.spec.ts index 9e2093401..07a420f80 100644 --- a/packages/tldraw/src/state/TldrawApp.spec.ts +++ b/packages/tldraw/src/state/TldrawApp.spec.ts @@ -634,40 +634,25 @@ describe('TldrawTestApp', () => { jest.setTimeout(10000) describe('When changing versions', () => { - it('migrates correctly', (done) => { + it('migrates correctly', async () => { const defaultState = TldrawTestApp.defaultState - const withoutRoom = { ...defaultState, } - delete withoutRoom.room - TldrawTestApp.defaultState = withoutRoom - const app = new TldrawTestApp('migrate_1') - + await app.ready app.createShapes({ id: 'rect1', type: TDShapeType.Rectangle, }) - - setTimeout(() => { - // TODO: Force the version to change and restore room. - TldrawTestApp.version = 100 - TldrawTestApp.defaultState.room = defaultState.room - - const app2 = new TldrawTestApp('migrate_1') - - setTimeout(() => { - try { - expect(app2.getShape('rect1')).toBeTruthy() - done() - } catch (e) { - done(e) - } - }, 100) - }, 100) + TldrawTestApp.version = 100 + TldrawTestApp.defaultState.room = defaultState.room + const app2 = new TldrawTestApp('migrate_1') + await app2.ready + expect(app2.getShape('rect1')).toBeTruthy() + return }) }) diff --git a/packages/tldraw/src/state/TldrawApp.ts b/packages/tldraw/src/state/TldrawApp.ts index 15ee871b8..d2d379fc3 100644 --- a/packages/tldraw/src/state/TldrawApp.ts +++ b/packages/tldraw/src/state/TldrawApp.ts @@ -360,8 +360,12 @@ export class TldrawApp extends StateManager { const toShape = page.shapes[binding.toId] const fromShape = page.shapes[binding.fromId] - const toUtils = TLDR.getShapeUtil(toShape) + if (!(toShape && fromShape)) { + delete next.document.pages[pageId].bindings[binding.id] + return + } + const toUtils = TLDR.getShapeUtil(toShape) const fromUtils = TLDR.getShapeUtil(fromShape) // We only need to update the binding's "from" shape @@ -1011,7 +1015,6 @@ export class TldrawApp extends StateManager { setDisableAssets = (disableAssets: boolean): this => { this.patchState({ appState: { disableAssets } }, 'ui:toggled_disable_images') - this.persist() return this } @@ -3361,12 +3364,12 @@ export class TldrawApp extends StateManager { getShapeUtil = TLDR.getShapeUtil - static version = 15 + static version = 15.2 static defaultDocument: TDDocument = { id: 'doc', name: 'New Document', - version: 15, + version: TldrawApp.version, pages: { page: { id: 'page', diff --git a/packages/tldraw/src/state/shapes/ImageUtil/ImageUtil.tsx b/packages/tldraw/src/state/shapes/ImageUtil/ImageUtil.tsx index 51849d957..07cf6b9fe 100644 --- a/packages/tldraw/src/state/shapes/ImageUtil/ImageUtil.tsx +++ b/packages/tldraw/src/state/shapes/ImageUtil/ImageUtil.tsx @@ -141,6 +141,7 @@ const Wrapper = styled('div', { width: '100%', borderRadius: '3px', perspective: '800px', + overflow: 'hidden', p: { userSelect: 'none', }, @@ -166,6 +167,9 @@ const Wrapper = styled('div', { }) const ImageElement = styled('img', { + position: 'absolute', + top: 0, + left: 0, width: '100%', height: '100%', maxWidth: '100%', diff --git a/packages/tldraw/src/state/shapes/VideoUtil/VideoUtil.tsx b/packages/tldraw/src/state/shapes/VideoUtil/VideoUtil.tsx index 6d4b71ed4..8e04d8a2f 100644 --- a/packages/tldraw/src/state/shapes/VideoUtil/VideoUtil.tsx +++ b/packages/tldraw/src/state/shapes/VideoUtil/VideoUtil.tsx @@ -181,6 +181,7 @@ const Wrapper = styled('div', { width: '100%', borderRadius: '3px', perspective: '800px', + overflow: 'hidden', p: { userSelect: 'none', }, @@ -206,8 +207,14 @@ const Wrapper = styled('div', { }) const VideoElement = styled('video', { + position: 'absolute', + top: 0, + left: 0, + width: '100%', + height: '100%', maxWidth: '100%', minWidth: '100%', - pointerEvents: 'all', + pointerEvents: 'none', + objectFit: 'cover', borderRadius: 2, })