tldraw/components/canvas/shape.tsx

147 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-05-28 16:25:43 +00:00
import React, { useRef, memo } from 'react'
import { useSelector } from 'state'
2021-05-28 14:37:23 +00:00
import styled from 'styles'
import { getShapeUtils } from 'lib/shape-utils'
import { getPage } from 'utils/utils'
2021-06-04 16:08:43 +00:00
import { ShapeType } from 'types'
2021-05-28 16:25:43 +00:00
import useShapeEvents from 'hooks/useShapeEvents'
2021-06-04 16:08:43 +00:00
import * as vec from 'utils/vec'
import { getShapeStyle } from 'lib/shape-styles'
2021-05-28 14:37:23 +00:00
2021-06-04 16:08:43 +00:00
interface ShapeProps {
id: string
isSelecting: boolean
parentPoint: number[]
parentRotation: number
}
function Shape({ id, isSelecting, parentPoint, parentRotation }: ShapeProps) {
const shape = useSelector(({ data }) => getPage(data).shapes[id])
2021-05-09 21:22:25 +00:00
2021-05-28 13:08:51 +00:00
const rGroup = useRef<SVGGElement>(null)
2021-06-04 16:08:43 +00:00
const events = useShapeEvents(id, shape?.type === ShapeType.Group, rGroup)
2021-05-14 22:56:41 +00:00
// This is a problem with deleted shapes. The hooks in this component
// may sometimes run before the hook in the Page component, which means
// a deleted shape will still be pulled here before the page component
// detects the change and pulls this component.
if (!shape) return null
2021-06-04 16:08:43 +00:00
const isGroup = shape.type === ShapeType.Group
2021-05-28 20:30:27 +00:00
const center = getShapeUtils(shape).getCenter(shape)
2021-05-28 16:25:43 +00:00
const transform = `
2021-06-04 16:08:43 +00:00
rotate(${shape.rotation * (180 / Math.PI)}, ${vec.sub(center, parentPoint)})
translate(${vec.sub(shape.point, parentPoint)})
2021-05-31 19:13:43 +00:00
`
2021-05-28 16:25:43 +00:00
const style = getShapeStyle(shape.style)
2021-05-12 21:11:17 +00:00
return (
2021-06-04 16:08:43 +00:00
<>
<StyledGroup ref={rGroup} transform={transform}>
{isSelecting && !isGroup && (
<HoverIndicator
as="use"
href={'#' + id}
strokeWidth={+style.strokeWidth + 4}
variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
{...events}
/>
)}
{!shape.isHidden && <StyledShape as="use" href={'#' + id} {...style} />}
{isGroup &&
shape.children.map((shapeId) => (
<Shape
key={shapeId}
id={shapeId}
isSelecting={isSelecting}
parentPoint={shape.point}
parentRotation={shape.rotation}
/>
))}
</StyledGroup>
</>
2021-05-12 21:11:17 +00:00
)
2021-05-09 21:22:25 +00:00
}
2021-06-01 21:49:32 +00:00
const StyledShape = styled('path', {
strokeLinecap: 'round',
strokeLinejoin: 'round',
pointerEvents: 'none',
2021-06-01 21:49:32 +00:00
})
2021-05-15 17:11:08 +00:00
2021-05-28 14:37:23 +00:00
const HoverIndicator = styled('path', {
stroke: '$selected',
2021-05-28 14:37:23 +00:00
strokeLinecap: 'round',
strokeLinejoin: 'round',
transform: 'all .2s',
fill: 'transparent',
filter: 'url(#expand)',
2021-06-01 21:49:32 +00:00
variants: {
variant: {
hollow: {
pointerEvents: 'stroke',
},
filled: {
pointerEvents: 'all',
},
},
},
2021-05-12 21:11:17 +00:00
})
2021-05-28 14:37:23 +00:00
const StyledGroup = styled('g', {
2021-05-12 21:11:17 +00:00
[`& ${HoverIndicator}`]: {
2021-05-28 14:37:23 +00:00
opacity: '0',
2021-05-12 21:11:17 +00:00
},
[`&:hover ${HoverIndicator}`]: {
opacity: '0.16',
},
2021-05-12 21:11:17 +00:00
variants: {
isSelected: {
true: {
2021-05-14 22:56:41 +00:00
[`& ${HoverIndicator}`]: {
opacity: '0.2',
2021-05-12 21:11:17 +00:00
},
[`&:hover ${HoverIndicator}`]: {
opacity: '0.3',
},
[`&:active ${HoverIndicator}`]: {
opacity: '0.3',
2021-05-15 13:02:13 +00:00
},
},
false: {
2021-05-15 13:02:13 +00:00
[`& ${HoverIndicator}`]: {
opacity: '0',
},
2021-05-15 13:02:13 +00:00
},
},
},
2021-05-12 21:11:17 +00:00
})
2021-06-04 16:08:43 +00:00
function Label({ children }: { children: React.ReactNode }) {
2021-05-31 19:13:43 +00:00
return (
<text
y={4}
x={4}
2021-06-04 16:08:43 +00:00
fontSize={12}
2021-05-31 19:13:43 +00:00
fill="black"
stroke="none"
alignmentBaseline="text-before-edge"
pointerEvents="none"
>
2021-06-04 16:08:43 +00:00
{children}
2021-05-31 19:13:43 +00:00
</text>
)
}
2021-05-28 14:37:23 +00:00
export { HoverIndicator }
2021-05-12 21:11:17 +00:00
2021-05-09 21:22:25 +00:00
export default memo(Shape)
2021-06-04 16:08:43 +00:00
function pp(n: number[]) {
return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
}