import { useEffect, useRef, useState } from 'react'
import Svg from '../draft/svg'
import Defs from '../draft/defs'
import Path from '../draft/path'
import Point from '../draft/point'
import Snippet from '../draft/snippet'
import { getProps } from '../draft/utils'
import { drag } from 'd3-drag'
import { select } from 'd3-selection'
import { event } from 'd3-dispatch'
const Buttons = ({ transform, flip, rotate, setRotate, resetPart }) => {
const letter = 'F'
const style = { style: {fill: 'white', fontSize: 18, fontWeight: 'bold', textAnchor: 'middle'} }
return (
{rotate
?
:
}
{letter}
flip('y')}>
{letter}
flip('x')}>
{letter}
)
}
const dx = (pointA, pointB) => pointB.x - pointA.x
const dy = (pointA, pointB) => pointB.y - pointA.y
const rad2deg = radians => radians * 57.29577951308232
const angle = (pointA, pointB) => {
let rad = Math.atan2(-1 * dy(pointA, pointB), dx(pointA, pointB))
while (rad < 0) rad += 2 * Math.PI
return rad2deg(rad)
}
const generateTransform = (x, y, rot, flipX, flipY, part) => {
const anchor = {
x: 0,
y: 0
}
const center = {
x: part.topLeft.x + (part.bottomRight.x - part.topLeft.x)/2,
y: part.topLeft.y + (part.bottomRight.y - part.topLeft.y)/2,
}
const dx = part.topLeft.x - center.x
const dy = part.topLeft.y - center.y
const transforms = [`translate(${x},${y})`]
if (flipX) transforms.push(
`translate(${center.x * -1}, ${center.y * -1})`,
'scale(-1, 1)',
`translate(${center.x * -1 + 2 * dx}, ${center.y})`
)
if (flipX) transforms.push(
`translate(${center.x * -1}, ${center.y * -1})`,
'scale(1, -1)',
`translate(${center.x}, ${center.y * -1 + 2 * dy})`,
)
if (rot) transforms.push(
`rotate(${rot}, ${center.x - anchor.x}, ${center.y - anchor.y})`
)
return transforms.join(' ')
}
const Part = props => {
console.log('rendering draft')
const { layout, gist, updateGist, name, part } = props
const partLayout= layout.parts[name]
// Don't just assume this makes sense
if (typeof layout?.parts?.[name]?.move?.x === 'undefined') return null
// Use a ref for direct DOM manipulation
const partRef = useRef(null)
const centerRef = useRef(null)
// State variable to switch between moving or rotating the part
const [rotate, setRotate] = useState(false)
// Initialize drag handler
useEffect(() => {
handleDrag(select(partRef.current))
}, [rotate])
// These are kepts as vars because re-rendering on drag would kill performance
// Managing the difference between re-render and direct DOM updates makes this
// whole thing a bit tricky to wrap your head around
let translateX = partLayout.move.x
let translateY = partLayout.move.y
let rotation = partLayout.rotate || 0
let flipX = partLayout.flipX ? true : false
let flipY = partLayout.flipY ? true : false
let rotStart = { x: 0, y: 0 }
const center = {
x: part.topLeft.x + (part.bottomRight.x - part.topLeft.x)/2,
y: part.topLeft.y + (part.bottomRight.y - part.topLeft.y)/2,
}
const handleDrag = drag()
.subject(function() {
const me = select(this);
return { x: translateX, y: translateY }
})
.on('start', function(event) {
rotStart = { x: event.x, y: event.y }
})
.on('drag', function(event) {
if (rotate) {
rotation = angle(rotStart, { x:event.x, y: event.y }) * -1
} else {
translateX = event.x
translateY = event.y
}
const me = select(this);
me.attr('transform', generateTransform(translateX, translateY, rotation, flipX, flipY, part))
})
.on('end', updateLayout)
const resetPart = () => {
rotation = 0
flipX = 0
flipY = 0
updateLayout()
}
const toggleDragRotate = () => {
updateLayout()
setRotate(!rotate)
}
const updateLayout = () => {
console.log('updating layout')
props.updateGist(
['layout', 'parts', name],
{
move: {
x: translateX,
y: translateY,
},
rotate: rotation,
flipX,
flipY
}
)
}
// Method to flip (mirror) the part along the X or Y axis
const flip = axis => {
if (axis === 'x') flipX = !flipX
else flipY = !flipY
updateLayout()
}
const grid = gist.paperless ? (
) : null
return (
{grid}
{
props.gist?._state?.xray?.enabled &&
props.gist?._state?.xray?.reveal?.[name]
&&
}
{Object.keys(part.paths).map((pathName) => (
))}
{Object.keys(props.part.points).map((pointName) => (
))}
{Object.keys(props.part.snippets).map((snippetName) => (
))}
{props.name !== 'pages' && }
)
}
const Draft = props => {
const { patternProps, gist, app, updateGist, unsetGist, bgProps={} } = props
const { layout=false } = gist
useEffect(() => {
if (!layout) {
// On the initial draft, core does the layout, so we set the layout to the auto-layout
// After this, core won't handle layout anymore. It's up to the user from this point onwards
// FIXME: Allow use the option to clear the layout again
updateGist(['layout'], {
...patternProps.autoLayout,
width: patternProps.width,
height: patternProps.height
})
}
}, [layout])
if (!patternProps || !layout) return null
// We need to make sure the `pages` part is at the bottom of the pile
// so we can drag-drop all parts on top of it.
// Bottom in SVG means we need to draw it first
const partList = Object.keys(patternProps.parts)
return (
)
}
export default Draft