2021-05-12 22:08:53 +00:00
|
|
|
import React, { useCallback, useRef, memo } from "react"
|
2021-05-12 21:11:17 +00:00
|
|
|
import state, { useSelector } from "state"
|
2021-05-13 06:44:52 +00:00
|
|
|
import inputs from "state/inputs"
|
2021-05-12 22:08:53 +00:00
|
|
|
import styled from "styles"
|
2021-05-23 13:46:04 +00:00
|
|
|
import { getShapeUtils } from "lib/shape-utils"
|
2021-05-22 15:45:24 +00:00
|
|
|
import { getPage } from "utils/utils"
|
2021-05-09 21:22:25 +00:00
|
|
|
|
|
|
|
function Shape({ id }: { id: string }) {
|
2021-05-12 21:11:17 +00:00
|
|
|
const rGroup = useRef<SVGGElement>(null)
|
|
|
|
|
2021-05-14 22:56:41 +00:00
|
|
|
const isHovered = useSelector((state) => state.data.hoveredId === id)
|
2021-05-15 13:02:13 +00:00
|
|
|
|
2021-05-14 21:05:21 +00:00
|
|
|
const isSelected = useSelector((state) => state.values.selectedIds.has(id))
|
|
|
|
|
2021-05-22 15:45:24 +00:00
|
|
|
const shape = useSelector(({ data }) => getPage(data).shapes[id])
|
2021-05-09 21:22:25 +00:00
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
const handlePointerDown = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
rGroup.current.setPointerCapture(e.pointerId)
|
2021-05-13 10:13:14 +00:00
|
|
|
state.send("POINTED_SHAPE", inputs.pointerDown(e, id))
|
2021-05-12 21:11:17 +00:00
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerUp = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
e.stopPropagation()
|
|
|
|
rGroup.current.releasePointerCapture(e.pointerId)
|
2021-05-13 10:13:14 +00:00
|
|
|
state.send("STOPPED_POINTING", inputs.pointerUp(e))
|
2021-05-12 21:11:17 +00:00
|
|
|
},
|
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerEnter = useCallback(
|
2021-05-14 22:56:41 +00:00
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
state.send("HOVERED_SHAPE", inputs.pointerEnter(e, id))
|
|
|
|
},
|
|
|
|
[id, shape]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerMove = useCallback(
|
|
|
|
(e: React.PointerEvent) => {
|
|
|
|
state.send("MOVED_OVER_SHAPE", inputs.pointerEnter(e, id))
|
|
|
|
},
|
|
|
|
[id, shape]
|
2021-05-12 21:11:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerLeave = useCallback(
|
2021-05-14 22:56:41 +00:00
|
|
|
() => state.send("UNHOVERED_SHAPE", { target: id }),
|
2021-05-12 21:11:17 +00:00
|
|
|
[id]
|
|
|
|
)
|
2021-05-14 22:56:41 +00:00
|
|
|
|
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.
|
|
|
|
if (!shape) return null
|
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
return (
|
|
|
|
<StyledGroup
|
|
|
|
ref={rGroup}
|
2021-05-14 22:56:41 +00:00
|
|
|
isHovered={isHovered}
|
2021-05-12 21:11:17 +00:00
|
|
|
isSelected={isSelected}
|
2021-05-17 21:27:18 +00:00
|
|
|
transform={`rotate(${shape.rotation * (180 / Math.PI)},${getShapeUtils(
|
|
|
|
shape
|
|
|
|
).getCenter(shape)}) translate(${shape.point})`}
|
2021-05-12 21:11:17 +00:00
|
|
|
onPointerDown={handlePointerDown}
|
|
|
|
onPointerUp={handlePointerUp}
|
|
|
|
onPointerEnter={handlePointerEnter}
|
|
|
|
onPointerLeave={handlePointerLeave}
|
2021-05-14 22:56:41 +00:00
|
|
|
onPointerMove={handlePointerMove}
|
2021-05-12 21:11:17 +00:00
|
|
|
>
|
2021-05-14 12:44:23 +00:00
|
|
|
<defs>{getShapeUtils(shape).render(shape)}</defs>
|
2021-05-12 21:11:17 +00:00
|
|
|
<HoverIndicator as="use" xlinkHref={"#" + id} />
|
2021-05-15 17:11:08 +00:00
|
|
|
<MainShape as="use" xlinkHref={"#" + id} {...shape.style} />
|
2021-05-12 21:11:17 +00:00
|
|
|
<Indicator as="use" xlinkHref={"#" + id} />
|
|
|
|
</StyledGroup>
|
|
|
|
)
|
2021-05-09 21:22:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-15 17:11:08 +00:00
|
|
|
const MainShape = styled("use", {
|
|
|
|
zStrokeWidth: 1,
|
|
|
|
})
|
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
const Indicator = styled("path", {
|
|
|
|
fill: "none",
|
|
|
|
stroke: "transparent",
|
2021-05-15 17:11:08 +00:00
|
|
|
zStrokeWidth: 1,
|
2021-05-12 21:11:17 +00:00
|
|
|
pointerEvents: "none",
|
|
|
|
strokeLineCap: "round",
|
|
|
|
strokeLinejoin: "round",
|
|
|
|
})
|
|
|
|
|
|
|
|
const HoverIndicator = styled("path", {
|
|
|
|
fill: "none",
|
|
|
|
stroke: "transparent",
|
|
|
|
pointerEvents: "all",
|
|
|
|
strokeLinecap: "round",
|
|
|
|
strokeLinejoin: "round",
|
2021-05-15 13:02:13 +00:00
|
|
|
transform: "all .2s",
|
2021-05-12 21:11:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const StyledGroup = styled("g", {
|
|
|
|
[`& ${HoverIndicator}`]: {
|
|
|
|
opacity: "0",
|
|
|
|
},
|
|
|
|
variants: {
|
|
|
|
isSelected: {
|
|
|
|
true: {
|
|
|
|
[`& ${Indicator}`]: {
|
|
|
|
stroke: "$selected",
|
|
|
|
},
|
|
|
|
},
|
2021-05-14 22:56:41 +00:00
|
|
|
false: {},
|
|
|
|
},
|
|
|
|
isHovered: {
|
2021-05-15 13:02:13 +00:00
|
|
|
true: {},
|
|
|
|
false: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
compoundVariants: [
|
|
|
|
{
|
|
|
|
isSelected: true,
|
|
|
|
isHovered: true,
|
|
|
|
css: {
|
2021-05-14 22:56:41 +00:00
|
|
|
[`& ${HoverIndicator}`]: {
|
2021-05-12 21:11:17 +00:00
|
|
|
opacity: "1",
|
|
|
|
stroke: "$hint",
|
2021-05-15 13:02:13 +00:00
|
|
|
zStrokeWidth: [8, 4],
|
2021-05-12 21:11:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-15 13:02:13 +00:00
|
|
|
{
|
|
|
|
isSelected: true,
|
|
|
|
isHovered: false,
|
|
|
|
css: {
|
|
|
|
[`& ${HoverIndicator}`]: {
|
|
|
|
opacity: "1",
|
|
|
|
stroke: "$hint",
|
|
|
|
zStrokeWidth: [6, 3],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
isSelected: false,
|
|
|
|
isHovered: true,
|
|
|
|
css: {
|
|
|
|
[`& ${HoverIndicator}`]: {
|
|
|
|
opacity: "1",
|
|
|
|
stroke: "$hint",
|
|
|
|
zStrokeWidth: [8, 4],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2021-05-12 21:11:17 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export { Indicator, HoverIndicator }
|
|
|
|
|
2021-05-09 21:22:25 +00:00
|
|
|
export default memo(Shape)
|