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-14 12:44:23 +00:00
|
|
|
import { getShapeUtils } from "lib/shapes"
|
2021-05-12 22:08:53 +00:00
|
|
|
import styled from "styles"
|
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-13 10:13:14 +00:00
|
|
|
const shape = useSelector(
|
|
|
|
({ data: { currentPageId, document } }) =>
|
|
|
|
document.pages[currentPageId].shapes[id]
|
|
|
|
)
|
2021-05-09 21:22:25 +00:00
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
const isSelected = useSelector((state) => state.values.selectedIds.has(id))
|
|
|
|
|
|
|
|
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-13 06:44:52 +00:00
|
|
|
(e: React.PointerEvent) => state.send("HOVERED_SHAPE", { id }),
|
2021-05-12 21:11:17 +00:00
|
|
|
[id]
|
|
|
|
)
|
|
|
|
|
|
|
|
const handlePointerLeave = useCallback(
|
2021-05-13 06:44:52 +00:00
|
|
|
(e: React.PointerEvent) => state.send("UNHOVERED_SHAPE", { id }),
|
2021-05-12 21:11:17 +00:00
|
|
|
[id]
|
|
|
|
)
|
|
|
|
return (
|
|
|
|
<StyledGroup
|
|
|
|
ref={rGroup}
|
|
|
|
isSelected={isSelected}
|
|
|
|
transform={`translate(${shape.point})`}
|
|
|
|
onPointerDown={handlePointerDown}
|
|
|
|
onPointerUp={handlePointerUp}
|
|
|
|
onPointerEnter={handlePointerEnter}
|
|
|
|
onPointerLeave={handlePointerLeave}
|
|
|
|
>
|
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} />
|
|
|
|
<use xlinkHref={"#" + id} {...shape.style} />
|
|
|
|
<Indicator as="use" xlinkHref={"#" + id} />
|
|
|
|
</StyledGroup>
|
|
|
|
)
|
2021-05-09 21:22:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 21:11:17 +00:00
|
|
|
const Indicator = styled("path", {
|
|
|
|
fill: "none",
|
|
|
|
stroke: "transparent",
|
2021-05-14 12:44:23 +00:00
|
|
|
zStrokeWidth: [1, 1],
|
2021-05-12 21:11:17 +00:00
|
|
|
pointerEvents: "none",
|
|
|
|
strokeLineCap: "round",
|
|
|
|
strokeLinejoin: "round",
|
|
|
|
})
|
|
|
|
|
|
|
|
const HoverIndicator = styled("path", {
|
|
|
|
fill: "none",
|
|
|
|
stroke: "transparent",
|
2021-05-13 10:13:14 +00:00
|
|
|
zStrokeWidth: [8, 4],
|
2021-05-12 21:11:17 +00:00
|
|
|
pointerEvents: "all",
|
|
|
|
strokeLinecap: "round",
|
|
|
|
strokeLinejoin: "round",
|
|
|
|
})
|
|
|
|
|
|
|
|
const StyledGroup = styled("g", {
|
|
|
|
[`& ${HoverIndicator}`]: {
|
|
|
|
opacity: "0",
|
|
|
|
},
|
|
|
|
variants: {
|
|
|
|
isSelected: {
|
|
|
|
true: {
|
|
|
|
[`& ${Indicator}`]: {
|
|
|
|
stroke: "$selected",
|
|
|
|
},
|
|
|
|
[`&:hover ${HoverIndicator}`]: {
|
|
|
|
opacity: "1",
|
|
|
|
stroke: "$hint",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
false: {
|
|
|
|
[`&:hover ${HoverIndicator}`]: {
|
|
|
|
opacity: "1",
|
|
|
|
stroke: "$hint",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
export { Indicator, HoverIndicator }
|
|
|
|
|
2021-05-09 21:22:25 +00:00
|
|
|
export default memo(Shape)
|