tldraw/components/canvas/shape.tsx

213 lines
4.6 KiB
TypeScript
Raw Normal View History

2021-06-28 20:45:06 +00:00
import React, { useRef, memo, useEffect, useState } from 'react'
import state, { useSelector } from 'state'
2021-05-28 14:37:23 +00:00
import styled from 'styles'
2021-06-21 21:35:28 +00:00
import { getShapeUtils } from 'state/shape-utils'
import { deepCompareArrays, getPage, getShape } from 'utils'
2021-05-28 16:25:43 +00:00
import useShapeEvents from 'hooks/useShapeEvents'
import vec from 'utils/vec'
2021-06-21 21:35:28 +00:00
import { getShapeStyle } from 'state/shape-styles'
import useShapeDef from 'hooks/useShape'
2021-06-18 15:19:10 +00:00
2021-06-04 16:08:43 +00:00
interface ShapeProps {
id: string
isSelecting: boolean
}
function Shape({ id, isSelecting }: ShapeProps): JSX.Element {
2021-06-17 10:43:55 +00:00
const rGroup = useRef<SVGGElement>(null)
const isHidden = useSelector((s) => {
const shape = getShape(s.data, id)
2021-06-28 20:45:06 +00:00
return shape?.isHidden || false
})
const children = useSelector((s) => {
const shape = getShape(s.data, id)
2021-06-28 20:45:06 +00:00
return shape?.children || []
}, deepCompareArrays)
const strokeWidth = useSelector((s) => {
const shape = getShape(s.data, id)
2021-06-28 22:22:23 +00:00
const style = getShapeStyle(shape?.style)
return +style.strokeWidth
})
2021-06-28 20:45:06 +00:00
const shapeUtils = useSelector((s) => {
const shape = getShape(s.data, id)
return getShapeUtils(shape)
})
const transform = useSelector((s) => {
const shape = getShape(s.data, id)
2021-06-28 20:45:06 +00:00
const center = getShapeUtils(shape).getCenter(shape)
const rotation = shape.rotation * (180 / Math.PI)
const parentPoint = getShape(s.data, shape.parentId)?.point || [0, 0]
return `
translate(${vec.neg(parentPoint)})
rotate(${rotation}, ${center})
translate(${shape.point})
2021-05-31 19:13:43 +00:00
`
})
2021-06-28 20:45:06 +00:00
const events = useShapeEvents(id, shapeUtils?.isParent, rGroup)
const hasShape = useMissingShapeTest(id)
if (!hasShape) return null
const isParent = shapeUtils.isParent
const isForeignObject = shapeUtils.isForeignObject
2021-05-28 16:25:43 +00:00
2021-05-12 21:11:17 +00:00
return (
2021-06-18 15:19:10 +00:00
<StyledGroup
2021-06-20 22:01:40 +00:00
id={id + '-group'}
2021-06-18 15:19:10 +00:00
ref={rGroup}
transform={transform}
{...events}
2021-06-18 15:19:10 +00:00
>
{isSelecting &&
(isForeignObject ? (
<ForeignObjectHover id={id} />
) : (
<EventSoak
as="use"
href={'#' + id}
strokeWidth={strokeWidth + 8}
variant={shapeUtils.canStyleFill ? 'filled' : 'hollow'}
/>
))}
{!isHidden &&
(isForeignObject ? (
<ForeignObjectRender id={id} />
) : (
<RealShape id={id} isParent={isParent} strokeWidth={strokeWidth} />
))}
{isParent &&
children.map((shapeId) => (
<Shape key={shapeId} id={shapeId} isSelecting={isSelecting} />
2021-06-04 21:21:03 +00:00
))}
</StyledGroup>
2021-05-12 21:11:17 +00:00
)
2021-05-09 21:22:25 +00:00
}
2021-06-05 19:36:46 +00:00
interface RealShapeProps {
id: string
isParent: boolean
strokeWidth: number
2021-06-05 19:36:46 +00:00
}
const RealShape = memo(function RealShape({
id,
isParent,
strokeWidth,
}: RealShapeProps) {
return (
<StyledShape
as="use"
data-shy={isParent}
href={'#' + id}
strokeWidth={strokeWidth}
/>
)
})
const ForeignObjectHover = memo(function ForeignObjectHover({
id,
}: {
id: string
}) {
const size = useSelector((s) => {
const shape = getPage(s.data).shapes[id]
const bounds = getShapeUtils(shape).getBounds(shape)
return [bounds.width, bounds.height]
}, deepCompareArrays)
return (
<EventSoak
as="rect"
width={size[0]}
height={size[1]}
strokeWidth={1.5}
variant={'ghost'}
/>
)
})
const ForeignObjectRender = memo(function ForeignObjectRender({
id,
}: {
id: string
}) {
const shape = useShapeDef(id)
const rFocusable = useRef<HTMLTextAreaElement>(null)
const isEditing = useSelector((s) => s.data.editingId === id)
const shapeUtils = getShapeUtils(shape)
useEffect(() => {
if (isEditing) {
setTimeout(() => {
const elm = rFocusable.current
if (!elm) return
elm.focus()
}, 0)
}
}, [isEditing])
return shapeUtils.render(shape, { isEditing, ref: rFocusable })
2021-06-05 19:36:46 +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
const EventSoak = styled('use', {
opacity: 0,
2021-05-28 14:37:23 +00:00
strokeLinecap: 'round',
strokeLinejoin: 'round',
2021-06-01 21:49:32 +00:00
variants: {
variant: {
ghost: {
pointerEvents: 'all',
filter: 'none',
opacity: 0,
},
2021-06-01 21:49:32 +00:00
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-06-04 21:21:03 +00:00
outline: 'none',
2021-05-12 21:11:17 +00:00
})
export default memo(Shape)
2021-06-28 20:45:06 +00:00
function useMissingShapeTest(id: string) {
const [isShape, setIsShape] = useState(true)
useEffect(() => {
return state.onUpdate((s) => {
if (isShape && !getShape(s.data, id)) {
setIsShape(false)
}
})
}, [isShape, id])
return isShape
}