2021-06-29 16:04:45 +00:00
|
|
|
import React, { useRef, memo, useEffect } from 'react'
|
2021-06-28 20:45:06 +00:00
|
|
|
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'
|
2021-06-29 12:00:59 +00:00
|
|
|
import { deepCompareArrays } from 'utils'
|
|
|
|
import tld from 'utils/tld'
|
2021-05-28 16:25:43 +00:00
|
|
|
import useShapeEvents from 'hooks/useShapeEvents'
|
2021-07-03 16:30:06 +00:00
|
|
|
import useShape from 'hooks/useShape'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { getShapeStyle } from 'state/shape-styles'
|
2021-06-18 15:19:10 +00:00
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
interface ShapeProps {
|
|
|
|
id: string
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:48:49 +00:00
|
|
|
function Shape({ id }: ShapeProps): JSX.Element {
|
2021-06-17 10:43:55 +00:00
|
|
|
const rGroup = useRef<SVGGElement>(null)
|
2021-05-15 21:04:28 +00:00
|
|
|
|
2021-06-28 12:13:34 +00:00
|
|
|
const isHidden = useSelector((s) => {
|
2021-06-29 12:00:59 +00:00
|
|
|
const shape = tld.getShape(s.data, id)
|
2021-06-29 18:29:33 +00:00
|
|
|
if (shape === undefined) return true
|
|
|
|
return shape?.isHidden
|
2021-06-28 12:13:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const children = useSelector((s) => {
|
2021-06-29 12:00:59 +00:00
|
|
|
const shape = tld.getShape(s.data, id)
|
2021-06-29 18:29:33 +00:00
|
|
|
if (shape === undefined) return []
|
|
|
|
return shape?.children
|
2021-07-02 10:48:49 +00:00
|
|
|
})
|
2021-06-28 12:13:34 +00:00
|
|
|
|
|
|
|
const strokeWidth = useSelector((s) => {
|
2021-06-29 12:00:59 +00:00
|
|
|
const shape = tld.getShape(s.data, id)
|
2021-06-29 18:29:33 +00:00
|
|
|
if (shape === undefined) return 0
|
2021-06-28 22:22:23 +00:00
|
|
|
const style = getShapeStyle(shape?.style)
|
2021-06-28 12:13:34 +00:00
|
|
|
return +style.strokeWidth
|
|
|
|
})
|
|
|
|
|
|
|
|
const transform = useSelector((s) => {
|
2021-06-29 12:00:59 +00:00
|
|
|
const shape = tld.getShape(s.data, id)
|
2021-06-29 18:29:33 +00:00
|
|
|
if (shape === undefined) return ''
|
2021-06-28 20:45:06 +00:00
|
|
|
const center = getShapeUtils(shape).getCenter(shape)
|
2021-06-28 12:13:34 +00:00
|
|
|
const rotation = shape.rotation * (180 / Math.PI)
|
2021-06-29 12:00:59 +00:00
|
|
|
const parentPoint = tld.getShape(s.data, shape.parentId)?.point || [0, 0]
|
2021-06-28 12:13:34 +00:00
|
|
|
|
|
|
|
return `
|
|
|
|
translate(${vec.neg(parentPoint)})
|
|
|
|
rotate(${rotation}, ${center})
|
|
|
|
translate(${shape.point})
|
2021-05-31 19:13:43 +00:00
|
|
|
`
|
2021-06-28 12:13:34 +00:00
|
|
|
})
|
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
const isCurrentParent = useSelector((s) => {
|
|
|
|
return s.data.currentParentId === id
|
|
|
|
})
|
2021-06-28 20:45:06 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
const events = useShapeEvents(id, isCurrentParent, rGroup)
|
2021-06-28 20:45:06 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
const shape = tld.getShape(state.data, id)
|
2021-06-28 20:45:06 +00:00
|
|
|
|
2021-07-02 10:48:49 +00:00
|
|
|
if (!shape) {
|
|
|
|
console.warn('Could not find that shape:', id)
|
|
|
|
return null
|
|
|
|
}
|
2021-06-28 20:45:06 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
// From here on, not reactive—if we're here, we can trust that the
|
|
|
|
// shape in state is a shape with changes that we need to render.
|
2021-05-28 16:25:43 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
const { isParent, isForeignObject, canStyleFill } = getShapeUtils(shape)
|
2021-06-29 18:29:33 +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}
|
2021-07-01 14:03:02 +00:00
|
|
|
isCurrentParent={isCurrentParent}
|
2021-06-23 14:39:14 +00:00
|
|
|
{...events}
|
2021-06-18 15:19:10 +00:00
|
|
|
>
|
2021-07-02 10:48:49 +00:00
|
|
|
{isForeignObject ? (
|
|
|
|
<ForeignObjectHover id={id} />
|
|
|
|
) : (
|
|
|
|
<EventSoak
|
|
|
|
as="use"
|
|
|
|
href={'#' + id}
|
|
|
|
strokeWidth={strokeWidth + 8}
|
|
|
|
variant={canStyleFill ? 'filled' : 'hollow'}
|
|
|
|
/>
|
|
|
|
)}
|
2021-06-28 12:13:34 +00:00
|
|
|
|
|
|
|
{!isHidden &&
|
2021-06-15 11:58:51 +00:00
|
|
|
(isForeignObject ? (
|
2021-06-28 12:13:34 +00:00
|
|
|
<ForeignObjectRender id={id} />
|
2021-06-15 11:58:51 +00:00
|
|
|
) : (
|
2021-06-28 12:13:34 +00:00
|
|
|
<RealShape id={id} isParent={isParent} strokeWidth={strokeWidth} />
|
2021-06-15 11:58:51 +00:00
|
|
|
))}
|
|
|
|
|
|
|
|
{isParent &&
|
2021-07-02 10:48:49 +00:00
|
|
|
children.map((shapeId) => <Shape key={shapeId} id={shapeId} />)}
|
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-29 18:29:33 +00:00
|
|
|
export default memo(Shape)
|
2021-06-29 16:04:45 +00:00
|
|
|
|
2021-06-05 19:36:46 +00:00
|
|
|
interface RealShapeProps {
|
|
|
|
id: string
|
2021-06-27 19:19:57 +00:00
|
|
|
isParent: boolean
|
2021-06-28 12:13:34 +00:00
|
|
|
strokeWidth: number
|
2021-06-05 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 12:13:34 +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) => {
|
2021-06-29 12:00:59 +00:00
|
|
|
const shape = tld.getPage(s.data).shapes[id]
|
2021-06-30 22:44:25 +00:00
|
|
|
if (shape === undefined) return [0, 0]
|
2021-06-28 12:13:34 +00:00
|
|
|
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
|
|
|
|
}) {
|
2021-07-03 16:30:06 +00:00
|
|
|
const shape = useShape(id)
|
2021-06-28 12:13:34 +00:00
|
|
|
|
|
|
|
const rFocusable = useRef<HTMLTextAreaElement>(null)
|
|
|
|
|
|
|
|
const isEditing = useSelector((s) => s.data.editingId === id)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isEditing) {
|
|
|
|
setTimeout(() => {
|
|
|
|
const elm = rFocusable.current
|
|
|
|
if (!elm) return
|
|
|
|
elm.focus()
|
|
|
|
}, 0)
|
|
|
|
}
|
|
|
|
}, [isEditing])
|
|
|
|
|
2021-06-30 22:44:25 +00:00
|
|
|
if (shape === undefined) return null
|
|
|
|
|
|
|
|
return getShapeUtils(shape).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',
|
2021-06-02 11:50:34 +00:00
|
|
|
pointerEvents: 'none',
|
2021-06-01 21:49:32 +00:00
|
|
|
})
|
2021-05-15 17:11:08 +00:00
|
|
|
|
2021-06-28 12:13:34 +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: {
|
2021-06-15 11:58:51 +00:00
|
|
|
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-07-01 14:03:02 +00:00
|
|
|
|
|
|
|
'& > *[data-shy=true]': {
|
|
|
|
opacity: 0,
|
|
|
|
},
|
|
|
|
|
|
|
|
'&:hover': {
|
|
|
|
'& > *[data-shy=true]': {
|
|
|
|
opacity: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
variants: {
|
|
|
|
isCurrentParent: {
|
|
|
|
true: {
|
|
|
|
'& > *[data-shy=true]': {
|
|
|
|
opacity: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-12 21:11:17 +00:00
|
|
|
})
|