tldraw/components/canvas/canvas.tsx

90 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-07-07 14:42:17 +00:00
import * as Sentry from '@sentry/node'
import { ErrorBoundary } from 'react-error-boundary'
2021-05-28 14:37:23 +00:00
import Bounds from './bounds/bounding-box'
import BoundsBg from './bounds/bounds-bg'
2021-06-30 20:31:29 +00:00
import Brush from './brush'
import ContextMenu from './context-menu/context-menu'
import Coop from './coop/coop'
import Defs from './defs'
2021-05-31 19:13:43 +00:00
import Handles from './bounds/handles'
2021-06-30 20:31:29 +00:00
import Page from './page'
import React, { useRef } from 'react'
import state, { useSelector } from 'state'
import styled from 'styles'
import useCamera from 'hooks/useCamera'
import useCanvasEvents from 'hooks/useCanvasEvents'
2021-06-30 20:31:29 +00:00
import useZoomEvents from 'hooks/useZoomEvents'
2021-05-09 13:04:42 +00:00
function resetError() {
null
}
2021-06-21 21:35:28 +00:00
export default function Canvas(): JSX.Element {
2021-05-09 13:04:42 +00:00
const rCanvas = useRef<SVGSVGElement>(null)
const rGroup = useRef<SVGGElement>(null)
2021-05-09 21:22:25 +00:00
useCamera(rGroup)
2021-05-28 16:25:43 +00:00
useZoomEvents()
const events = useCanvasEvents(rCanvas)
2021-05-10 12:16:57 +00:00
const isReady = useSelector((s) => s.isIn('ready'))
2021-05-10 12:16:57 +00:00
2021-05-09 13:04:42 +00:00
return (
2021-06-10 09:49:16 +00:00
<ContextMenu>
<MainSVG ref={rCanvas} {...events}>
<ErrorBoundary FallbackComponent={ErrorFallback} onReset={resetError}>
<Defs />
{isReady && (
<g ref={rGroup} id="shapes">
<BoundsBg />
<Page />
2021-06-30 20:31:29 +00:00
<Coop />
<Bounds />
<Handles />
<Brush />
</g>
)}
</ErrorBoundary>
2021-06-10 09:49:16 +00:00
</MainSVG>
</ContextMenu>
2021-05-09 13:04:42 +00:00
)
}
2021-05-28 14:37:23 +00:00
const MainSVG = styled('svg', {
position: 'fixed',
2021-06-18 14:39:07 +00:00
overflow: 'hidden',
2021-05-09 13:04:42 +00:00
top: 0,
left: 0,
2021-05-28 14:37:23 +00:00
width: '100%',
height: '100%',
touchAction: 'none',
2021-05-09 13:04:42 +00:00
zIndex: 100,
2021-05-30 13:49:33 +00:00
backgroundColor: '$canvas',
pointerEvents: 'all',
2021-06-07 21:12:14 +00:00
// cursor: 'none',
2021-05-23 13:46:04 +00:00
2021-05-28 14:37:23 +00:00
'& *': {
userSelect: 'none',
2021-05-23 13:46:04 +00:00
},
2021-05-09 13:04:42 +00:00
})
function ErrorFallback({ error, resetErrorBoundary }) {
React.useEffect(() => {
2021-07-07 14:42:17 +00:00
const copy =
'Sorry, something went wrong. Press Ok to reset the document, or press cancel to continue and see if it resolves itself.'
console.error(error)
2021-07-07 14:42:17 +00:00
Sentry.captureException(error)
if (window.confirm(copy)) {
2021-07-07 14:42:17 +00:00
state.send('RESET_DOCUMENT_STATE')
resetErrorBoundary()
}
}, [])
2021-07-07 14:42:17 +00:00
return <g />
}