tldraw/components/canvas/page.tsx

32 lines
917 B
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
import { useSelector } from 'state'
2021-05-28 14:37:23 +00:00
import Shape from './shape'
import HoveredShape from './hovered-shape'
import usePageShapes from 'hooks/usePageShapes'
2021-05-09 21:22:25 +00:00
/*
On each state change, compare node ids of all shapes
on the current page. Kind of expensive but only happens
here; and still cheaper than any other pattern I've found.
*/
2021-06-21 21:35:28 +00:00
export default function Page(): JSX.Element {
2021-05-28 14:37:23 +00:00
const isSelecting = useSelector((s) => s.isIn('selecting'))
const visiblePageShapeIds = usePageShapes()
const hoveredShapeId = useSelector((s) => {
return visiblePageShapeIds.find((id) => id === s.data.hoveredId)
})
2021-05-09 21:22:25 +00:00
return (
2021-05-30 13:20:25 +00:00
<g pointerEvents={isSelecting ? 'all' : 'none'}>
{isSelecting && hoveredShapeId && (
<HoveredShape key={hoveredShapeId} id={hoveredShapeId} />
)}
{visiblePageShapeIds.map((id) => (
<Shape key={id} id={id} isSelecting={isSelecting} />
2021-05-09 21:22:25 +00:00
))}
2021-05-30 13:20:25 +00:00
</g>
2021-05-09 21:22:25 +00:00
)
}