2021-06-17 10:43:55 +00:00
|
|
|
import React, { useRef, memo, useEffect } from 'react'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { 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-23 14:39:14 +00:00
|
|
|
import { getPage, getSelectedIds, isMobile } from 'utils/utils'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { Shape as _Shape } from 'types'
|
2021-05-28 16:25:43 +00:00
|
|
|
import useShapeEvents from 'hooks/useShapeEvents'
|
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-05-28 14:37:23 +00:00
|
|
|
|
2021-06-18 15:19:10 +00:00
|
|
|
const isMobileDevice = isMobile()
|
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
interface ShapeProps {
|
|
|
|
id: string
|
|
|
|
isSelecting: boolean
|
|
|
|
parentPoint: number[]
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
function Shape({ id, isSelecting, parentPoint }: ShapeProps): JSX.Element {
|
2021-06-17 10:43:55 +00:00
|
|
|
const rGroup = useRef<SVGGElement>(null)
|
|
|
|
const rFocusable = useRef<HTMLTextAreaElement>(null)
|
2021-05-09 21:22:25 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
const isEditing = useSelector((s) => s.data.editingId === id)
|
|
|
|
|
2021-06-23 14:39:14 +00:00
|
|
|
const isSelected = useSelector((s) => getSelectedIds(s.data).has(id))
|
|
|
|
|
2021-06-17 10:43:55 +00:00
|
|
|
const shape = useSelector((s) => getPage(s.data).shapes[id])
|
2021-05-28 13:08:51 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
const events = useShapeEvents(id, getShapeUtils(shape)?.isParent, rGroup)
|
2021-05-14 22:56:41 +00:00
|
|
|
|
2021-06-17 10:43:55 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (isEditing) {
|
2021-06-18 13:55:36 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
const elm = rFocusable.current
|
|
|
|
if (!elm) return
|
|
|
|
elm.focus()
|
|
|
|
}, 0)
|
2021-06-17 10:43:55 +00:00
|
|
|
}
|
|
|
|
}, [isEditing])
|
|
|
|
|
2021-05-15 21:04:28 +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.
|
2021-06-06 07:33:30 +00:00
|
|
|
if (!shape) return null
|
2021-05-15 21:04:28 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
const style = getShapeStyle(shape.style)
|
|
|
|
const shapeUtils = getShapeUtils(shape)
|
2021-06-17 10:43:55 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
const { isShy, isParent, isForeignObject } = shapeUtils
|
2021-06-04 16:08:43 +00:00
|
|
|
|
2021-06-15 11:58:51 +00:00
|
|
|
const bounds = shapeUtils.getBounds(shape)
|
|
|
|
const center = shapeUtils.getCenter(shape)
|
2021-06-06 20:49:15 +00:00
|
|
|
const rotation = shape.rotation * (180 / Math.PI)
|
2021-06-02 11:50:34 +00:00
|
|
|
|
2021-05-28 16:25:43 +00:00
|
|
|
const transform = `
|
2021-06-17 10:43:55 +00:00
|
|
|
translate(${vec.neg(parentPoint)})
|
|
|
|
rotate(${rotation}, ${center})
|
|
|
|
translate(${shape.point})
|
2021-05-31 19:13:43 +00:00
|
|
|
`
|
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}
|
2021-06-23 14:39:14 +00:00
|
|
|
isSelected={isSelected}
|
2021-06-18 15:19:10 +00:00
|
|
|
device={isMobileDevice ? 'mobile' : 'desktop'}
|
2021-06-23 14:39:14 +00:00
|
|
|
{...events}
|
2021-06-18 15:19:10 +00:00
|
|
|
>
|
2021-06-23 15:51:22 +00:00
|
|
|
{isSelecting && !isShy && (
|
2021-06-18 15:19:10 +00:00
|
|
|
<>
|
|
|
|
{isForeignObject ? (
|
|
|
|
<HoverIndicator
|
|
|
|
as="rect"
|
|
|
|
width={bounds.width}
|
|
|
|
height={bounds.height}
|
|
|
|
strokeWidth={1.5}
|
|
|
|
variant={'ghost'}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<HoverIndicator
|
|
|
|
as="use"
|
|
|
|
href={'#' + id}
|
2021-06-23 14:39:14 +00:00
|
|
|
strokeWidth={+style.strokeWidth + 5}
|
2021-06-18 15:19:10 +00:00
|
|
|
variant={getShapeUtils(shape).canStyleFill ? 'filled' : 'hollow'}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
2021-06-15 11:58:51 +00:00
|
|
|
|
|
|
|
{!shape.isHidden &&
|
|
|
|
(isForeignObject ? (
|
2021-06-17 10:43:55 +00:00
|
|
|
shapeUtils.render(shape, { isEditing, ref: rFocusable })
|
2021-06-15 11:58:51 +00:00
|
|
|
) : (
|
|
|
|
<RealShape
|
|
|
|
isParent={isParent}
|
|
|
|
id={id}
|
2021-06-20 22:01:40 +00:00
|
|
|
shape={shape}
|
2021-06-15 11:58:51 +00:00
|
|
|
style={style}
|
|
|
|
isEditing={isEditing}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
|
|
|
|
{isParent &&
|
2021-06-04 21:21:03 +00:00
|
|
|
shape.children.map((shapeId) => (
|
|
|
|
<Shape
|
|
|
|
key={shapeId}
|
|
|
|
id={shapeId}
|
|
|
|
isSelecting={isSelecting}
|
|
|
|
parentPoint={shape.point}
|
2021-06-04 16:08:43 +00:00
|
|
|
/>
|
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
|
|
|
|
style: Partial<React.SVGProps<SVGUseElement>>
|
2021-06-15 11:58:51 +00:00
|
|
|
isParent: boolean
|
2021-06-20 22:01:40 +00:00
|
|
|
shape: _Shape
|
2021-06-15 11:58:51 +00:00
|
|
|
isEditing: boolean
|
2021-06-05 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
const RealShape = memo(function RealShape({ id, isParent }: RealShapeProps) {
|
2021-06-20 22:01:40 +00:00
|
|
|
return <StyledShape as="use" data-shy={isParent} href={'#' + id} />
|
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-05-28 14:37:23 +00:00
|
|
|
const HoverIndicator = styled('path', {
|
2021-06-02 11:50:34 +00:00
|
|
|
stroke: '$selected',
|
2021-05-28 14:37:23 +00:00
|
|
|
strokeLinecap: 'round',
|
|
|
|
strokeLinejoin: 'round',
|
2021-06-02 11:50:34 +00:00
|
|
|
fill: 'transparent',
|
|
|
|
filter: 'url(#expand)',
|
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',
|
|
|
|
[`& *[data-shy="true"]`]: {
|
|
|
|
opacity: '0',
|
|
|
|
},
|
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
|
|
|
},
|
|
|
|
variants: {
|
2021-06-18 15:19:10 +00:00
|
|
|
device: {
|
|
|
|
mobile: {},
|
|
|
|
desktop: {},
|
|
|
|
},
|
2021-05-12 21:11:17 +00:00
|
|
|
isSelected: {
|
2021-06-02 11:50:34 +00:00
|
|
|
true: {
|
2021-06-04 21:21:03 +00:00
|
|
|
[`& *[data-shy="true"]`]: {
|
|
|
|
opacity: '1',
|
|
|
|
},
|
2021-05-14 22:56:41 +00:00
|
|
|
[`& ${HoverIndicator}`]: {
|
2021-06-02 11:50:34 +00:00
|
|
|
opacity: '0.2',
|
2021-05-12 21:11:17 +00:00
|
|
|
},
|
2021-05-15 13:02:13 +00:00
|
|
|
},
|
2021-06-02 11:50:34 +00:00
|
|
|
false: {
|
2021-05-15 13:02:13 +00:00
|
|
|
[`& ${HoverIndicator}`]: {
|
2021-06-02 11:50:34 +00:00
|
|
|
opacity: '0',
|
|
|
|
},
|
2021-05-15 13:02:13 +00:00
|
|
|
},
|
|
|
|
},
|
2021-06-02 11:50:34 +00:00
|
|
|
},
|
2021-06-18 15:19:10 +00:00
|
|
|
compoundVariants: [
|
|
|
|
{
|
|
|
|
device: 'desktop',
|
|
|
|
isSelected: 'false',
|
|
|
|
css: {
|
|
|
|
[`&:hover ${HoverIndicator}`]: {
|
|
|
|
opacity: '0.16',
|
|
|
|
},
|
|
|
|
[`&:hover *[data-shy="true"]`]: {
|
|
|
|
opacity: '1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
device: 'desktop',
|
|
|
|
isSelected: 'true',
|
|
|
|
css: {
|
|
|
|
[`&:hover ${HoverIndicator}`]: {
|
2021-06-23 14:39:14 +00:00
|
|
|
opacity: '0.25',
|
2021-06-18 15:19:10 +00:00
|
|
|
},
|
|
|
|
[`&:active ${HoverIndicator}`]: {
|
2021-06-23 14:39:14 +00:00
|
|
|
opacity: '0.25',
|
2021-06-18 15:19:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2021-05-12 21:11:17 +00:00
|
|
|
})
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
// function Label({ children }: { children: React.ReactNode }) {
|
|
|
|
// return (
|
|
|
|
// <text
|
|
|
|
// y={4}
|
|
|
|
// x={4}
|
|
|
|
// fontSize={12}
|
|
|
|
// fill="black"
|
|
|
|
// stroke="none"
|
|
|
|
// alignmentBaseline="text-before-edge"
|
|
|
|
// pointerEvents="none"
|
|
|
|
// >
|
|
|
|
// {children}
|
|
|
|
// </text>
|
|
|
|
// )
|
|
|
|
// }
|
|
|
|
|
|
|
|
// function pp(n: number[]) {
|
|
|
|
// return '[' + n.map((v) => v.toFixed(1)).join(', ') + ']'
|
|
|
|
// }
|
2021-06-06 10:50:01 +00:00
|
|
|
|
|
|
|
export { HoverIndicator }
|
|
|
|
|
|
|
|
export default memo(Shape)
|