tldraw/components/canvas/page.tsx

63 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-06-21 21:35:28 +00:00
import { useSelector } from 'state'
import { ShapeTreeNode } from 'types'
2021-07-09 20:04:41 +00:00
import ShapeComponent from './shape'
2021-05-09 21:22:25 +00:00
/*
2021-07-09 20:04:41 +00:00
On each state change, populate a tree structure with all of
the shapes that we need to render..
2021-05-09 21:22:25 +00:00
*/
2021-06-21 21:35:28 +00:00
export default function Page(): JSX.Element {
2021-07-09 20:04:41 +00:00
// Get a tree of shapes to render
const shapesToRender = useSelector((s) => s.values.shapesToRender)
const allowHovers = useSelector((s) =>
s.isInAny('selecting', 'text', 'editingShape')
)
2021-05-09 21:22:25 +00:00
return (
2021-07-09 16:15:27 +00:00
<>
{shapesToRender.map((node) => (
<ShapeNode key={node.shape.id} node={node} allowHovers={allowHovers} />
2021-07-09 16:15:27 +00:00
))}
</>
)
}
2021-07-09 20:04:41 +00:00
interface ShapeNodeProps {
node: ShapeTreeNode
allowHovers: boolean
2021-07-09 16:15:27 +00:00
}
2021-07-09 20:04:41 +00:00
const ShapeNode = ({
2021-07-10 20:39:29 +00:00
node: {
shape,
children,
isEditing,
isHovered,
isDarkMode,
isSelected,
isCurrentParent,
},
allowHovers,
2021-07-09 20:04:41 +00:00
}: ShapeNodeProps) => {
return (
<>
<ShapeComponent
shape={shape}
isEditing={isEditing}
isHovered={allowHovers && isHovered}
2021-07-09 20:04:41 +00:00
isSelected={isSelected}
2021-07-10 20:39:29 +00:00
isDarkMode={isDarkMode}
2021-07-09 20:04:41 +00:00
isCurrentParent={isCurrentParent}
/>
{children.map((childNode) => (
<ShapeNode
key={childNode.shape.id}
node={childNode}
allowHovers={allowHovers}
/>
2021-07-09 20:04:41 +00:00
))}
</>
)
}