Fix missing shapes when resizing window

This commit is contained in:
Steve Ruiz 2021-06-27 13:12:28 +01:00
parent 61399fb9d0
commit ec7ec06a6d
5 changed files with 44 additions and 15 deletions

View file

@ -12,6 +12,7 @@ import BoundsBg from './bounds/bounds-bg'
import Handles from './bounds/handles' import Handles from './bounds/handles'
import useCanvasEvents from 'hooks/useCanvasEvents' import useCanvasEvents from 'hooks/useCanvasEvents'
import ContextMenu from './context-menu/context-menu' import ContextMenu from './context-menu/context-menu'
import useWindowResize from 'hooks/useWindowResize'
export default function Canvas(): JSX.Element { export default function Canvas(): JSX.Element {
const rCanvas = useRef<SVGSVGElement>(null) const rCanvas = useRef<SVGSVGElement>(null)
@ -21,6 +22,8 @@ export default function Canvas(): JSX.Element {
useZoomEvents() useZoomEvents()
useWindowResize()
const events = useCanvasEvents(rCanvas) const events = useCanvasEvents(rCanvas)
const isReady = useSelector((s) => s.isIn('ready')) const isReady = useSelector((s) => s.isIn('ready'))

15
hooks/useWindowResize.ts Normal file
View file

@ -0,0 +1,15 @@
import { useEffect } from 'react'
import state from 'state'
export default function useWindowResize(): void {
useEffect(() => {
function handleResize() {
state.send('RESIZED_WINDOW')
}
window.addEventListener('resize', handleResize)
return () => {
window.removeEventListener('resize', handleResize)
}
}, [])
}

View file

@ -5,7 +5,8 @@ import { getShapeUtils } from 'state/shape-utils'
import { getBoundsFromPoints, getPage, getShape, updateParents } from 'utils' import { getBoundsFromPoints, getPage, getShape, updateParents } from 'utils'
import vec from 'utils/vec' import vec from 'utils/vec'
import commands from 'state/commands' import commands from 'state/commands'
export default class BrushSession extends BaseSession {
export default class DrawSession extends BaseSession {
origin: number[] origin: number[]
previous: number[] previous: number[]
last: number[] last: number[]

View file

@ -173,12 +173,13 @@ const state = createState({
else: ['zoomCameraToActual'], else: ['zoomCameraToActual'],
}, },
on: { on: {
RESIZED_WINDW: 'updateOnResize',
RESET_PAGE: 'resetPage', RESET_PAGE: 'resetPage',
TOGGLED_READ_ONLY: 'toggleReadOnly', TOGGLED_READ_ONLY: 'toggleReadOnly',
LOADED_FONTS: 'resetShapes', LOADED_FONTS: 'resetShapes',
USED_PEN_DEVICE: 'enablePenLock', USED_PEN_DEVICE: 'enablePenLock',
DISABLED_PEN_LOCK: 'disablePenLock', DISABLED_PEN_LOCK: 'disablePenLock',
TOGGLED_CODE_PANEL_OPEN: 'toggleCodePanel', TOGGLED_CODE_PANEL_OPEN: ['toggleCodePanel', 'saveAppState'],
TOGGLED_STYLE_PANEL_OPEN: 'toggleStylePanel', TOGGLED_STYLE_PANEL_OPEN: 'toggleStylePanel',
PANNED_CAMERA: 'panCamera', PANNED_CAMERA: 'panCamera',
POINTED_CANVAS: ['closeStylePanel', 'clearCurrentParentId'], POINTED_CANVAS: ['closeStylePanel', 'clearCurrentParentId'],
@ -1123,6 +1124,10 @@ const state = createState({
}, },
}, },
actions: { actions: {
updateOnResize(data) {
getPageState(data).camera.point = { ...getPageState(data).camera.point }
},
toggleReadOnly(data) { toggleReadOnly(data) {
data.isReadOnly = !data.isReadOnly data.isReadOnly = !data.isReadOnly
}, },
@ -1838,6 +1843,10 @@ const state = createState({
storage.loadDocumentFromJson(data, payload.json) storage.loadDocumentFromJson(data, payload.json)
}, },
saveAppState(data) {
storage.saveAppStateToLocalStorage(data)
},
forceSave(data) { forceSave(data) {
storage.saveToFileSystem(data) storage.saveToFileSystem(data)
}, },

View file

@ -49,6 +49,13 @@ class Storage {
} }
} }
saveAppStateToLocalStorage = (data: Data) => {
localStorage.setItem(
storageId(data.document.id, 'document-state', data.document.id),
compress(JSON.stringify(data))
)
}
saveDocumentToLocalStorage(data: Data) { saveDocumentToLocalStorage(data: Data) {
const document = this.getCompleteDocument(data) const document = this.getCompleteDocument(data)
@ -56,10 +63,6 @@ class Storage {
storageId(data.document.id, 'document', data.document.id), storageId(data.document.id, 'document', data.document.id),
compress(JSON.stringify(document)) compress(JSON.stringify(document))
) )
localStorage.setItem(
storageId(data.document.id, 'document-state', data.document.id),
compress(JSON.stringify(data))
)
} }
getCompleteDocument = (data: Data) => { getCompleteDocument = (data: Data) => {
@ -162,18 +165,14 @@ class Storage {
data.currentPageId = pageState.id data.currentPageId = pageState.id
} }
// 4. Save the current document // 4. Save the current app state / document
// The document is now "full" and ready. Whether we've restored a // The document is now "full" and ready. Whether we've restored a
// document or created a new one, save the entire current document. // document or created a new one, save the entire current document.
localStorage.setItem( this.saveDocumentToLocalStorage(data)
storageId(data.document.id, 'document', data.document.id),
compress(JSON.stringify(data.document))
)
localStorage.setItem( // 4.1
storageId(data.document.id, 'document-state', data.document.id), // Also save the app state.
compress(JSON.stringify(data)) this.saveAppStateToLocalStorage(data)
)
// 4.1 // 4.1
// Also save out copies of each page separately. // Also save out copies of each page separately.
@ -311,11 +310,13 @@ class Storage {
/* ------------------- File System ------------------ */ /* ------------------- File System ------------------ */
saveToFileSystem = (data: Data) => { saveToFileSystem = (data: Data) => {
this.saveAppStateToLocalStorage(data)
this.saveDocumentToLocalStorage(data) this.saveDocumentToLocalStorage(data)
this.saveDataToFileSystem(data, data.document.id, false) this.saveDataToFileSystem(data, data.document.id, false)
} }
saveAsToFileSystem = (data: Data) => { saveAsToFileSystem = (data: Data) => {
this.saveAppStateToLocalStorage(data)
this.saveDocumentToLocalStorage(data) this.saveDocumentToLocalStorage(data)
this.saveDataToFileSystem(data, uniqueId(), true) this.saveDataToFileSystem(data, uniqueId(), true)
} }