2021-07-01 14:03:02 +00:00
|
|
|
import { uniqueId } from 'utils/utils'
|
2021-06-16 12:09:45 +00:00
|
|
|
import vec from 'utils/vec'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { GroupShape, ShapeType } from 'types'
|
|
|
|
import { getShapeUtils } from './index'
|
2021-06-24 08:18:14 +00:00
|
|
|
import { getCommonBounds, translateBounds } from 'utils'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { defaultStyle } from 'state/shape-styles'
|
2021-06-04 16:08:43 +00:00
|
|
|
import styled from 'styles'
|
2021-06-21 21:35:28 +00:00
|
|
|
import { registerShapeUtils } from './register'
|
2021-06-04 16:08:43 +00:00
|
|
|
|
|
|
|
const group = registerShapeUtils<GroupShape>({
|
|
|
|
boundsCache: new WeakMap([]),
|
2021-06-15 11:58:51 +00:00
|
|
|
isShy: true,
|
|
|
|
isParent: true,
|
2021-06-04 16:08:43 +00:00
|
|
|
|
2021-07-01 14:03:02 +00:00
|
|
|
defaultProps: {
|
|
|
|
id: uniqueId(),
|
|
|
|
type: ShapeType.Group,
|
|
|
|
name: 'Group',
|
|
|
|
parentId: 'page1',
|
|
|
|
childIndex: 0,
|
|
|
|
point: [0, 0],
|
|
|
|
size: [1, 1],
|
|
|
|
rotation: 0,
|
|
|
|
style: defaultStyle,
|
|
|
|
children: [],
|
2021-06-04 16:08:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
render(shape) {
|
|
|
|
const { id, size } = shape
|
|
|
|
|
|
|
|
return (
|
2021-06-04 21:21:03 +00:00
|
|
|
<StyledGroupShape
|
|
|
|
id={id}
|
|
|
|
width={size[0]}
|
|
|
|
height={size[1]}
|
|
|
|
data-shy={true}
|
2021-07-07 12:46:46 +00:00
|
|
|
fill="none"
|
2021-06-04 21:21:03 +00:00
|
|
|
/>
|
2021-06-04 16:08:43 +00:00
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
getBounds(shape) {
|
|
|
|
if (!this.boundsCache.has(shape)) {
|
|
|
|
const [width, height] = shape.size
|
|
|
|
const bounds = {
|
|
|
|
minX: 0,
|
|
|
|
maxX: width,
|
|
|
|
minY: 0,
|
|
|
|
maxY: height,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
}
|
|
|
|
|
|
|
|
this.boundsCache.set(shape, bounds)
|
|
|
|
}
|
|
|
|
|
|
|
|
return translateBounds(this.boundsCache.get(shape), shape.point)
|
|
|
|
},
|
|
|
|
|
|
|
|
hitTest() {
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
|
2021-06-21 21:35:28 +00:00
|
|
|
hitTestBounds() {
|
2021-06-04 16:08:43 +00:00
|
|
|
return false
|
|
|
|
},
|
|
|
|
|
|
|
|
transform(shape, bounds, { initialShape, transformOrigin, scaleX, scaleY }) {
|
|
|
|
if (shape.rotation === 0 && !shape.isAspectRatioLocked) {
|
|
|
|
shape.size = [bounds.width, bounds.height]
|
|
|
|
shape.point = [bounds.minX, bounds.minY]
|
|
|
|
} else {
|
|
|
|
shape.size = vec.mul(
|
|
|
|
initialShape.size,
|
|
|
|
Math.min(Math.abs(scaleX), Math.abs(scaleY))
|
|
|
|
)
|
|
|
|
|
|
|
|
shape.point = [
|
|
|
|
bounds.minX +
|
|
|
|
(bounds.width - shape.size[0]) *
|
|
|
|
(scaleX < 0 ? 1 - transformOrigin[0] : transformOrigin[0]),
|
|
|
|
bounds.minY +
|
|
|
|
(bounds.height - shape.size[1]) *
|
|
|
|
(scaleY < 0 ? 1 - transformOrigin[1] : transformOrigin[1]),
|
|
|
|
]
|
|
|
|
|
|
|
|
shape.rotation =
|
|
|
|
(scaleX < 0 && scaleY >= 0) || (scaleY < 0 && scaleX >= 0)
|
|
|
|
? -initialShape.rotation
|
|
|
|
: initialShape.rotation
|
|
|
|
}
|
|
|
|
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
|
|
|
transformSingle(shape, bounds) {
|
|
|
|
shape.size = [bounds.width, bounds.height]
|
|
|
|
shape.point = [bounds.minX, bounds.minY]
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
|
|
|
|
onChildrenChange(shape, children) {
|
2021-06-26 11:52:36 +00:00
|
|
|
if (shape.children.length === 0) return
|
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
const childBounds = getCommonBounds(
|
|
|
|
...children.map((child) => getShapeUtils(child).getRotatedBounds(child))
|
|
|
|
)
|
|
|
|
|
|
|
|
// const c1 = this.getCenter(shape)
|
|
|
|
// const c2 = getBoundsCenter(childBounds)
|
|
|
|
|
|
|
|
// const [x0, y0] = vec.rotWith(shape.point, c1, shape.rotation)
|
|
|
|
// const [w0, h0] = vec.rotWith(shape.size, c1, shape.rotation)
|
|
|
|
// const [x1, y1] = vec.rotWith(
|
|
|
|
// [childBounds.minX, childBounds.minY],
|
|
|
|
// c2,
|
|
|
|
// shape.rotation
|
|
|
|
// )
|
|
|
|
// const [w1, h1] = vec.rotWith(
|
|
|
|
// [childBounds.width, childBounds.height],
|
|
|
|
// c2,
|
|
|
|
// shape.rotation
|
|
|
|
// )
|
|
|
|
|
|
|
|
// let delta: number[]
|
|
|
|
|
|
|
|
// if (h0 === h1 && w0 !== w1) {
|
|
|
|
// if (x0 < x1) {
|
|
|
|
// // moving left edge, pin right edge
|
|
|
|
// delta = vec.sub([x1 + w1, y1 + h1 / 2], [x0 + w0, y0 + h0 / 2])
|
|
|
|
// } else {
|
|
|
|
// // moving right edge, pin left edge
|
|
|
|
// delta = vec.sub([x1, y1 + h1 / 2], [x0, y0 + h0 / 2])
|
|
|
|
// }
|
|
|
|
// } else if (h0 !== h1 && w0 === w1) {
|
|
|
|
// if (y0 < y1) {
|
|
|
|
// // moving top edge, pin bottom edge
|
|
|
|
// delta = vec.sub([x1 + w1 / 2, y1 + h1], [x0 + w0 / 2, y0 + h0])
|
|
|
|
// } else {
|
|
|
|
// // moving bottom edge, pin top edge
|
|
|
|
// delta = vec.sub([x1 + w1 / 2, y1], [x0 + w0 / 2, y0])
|
|
|
|
// }
|
|
|
|
// } else if (x0 !== x1) {
|
|
|
|
// if (y0 !== y1) {
|
|
|
|
// // moving top left, pin bottom right
|
|
|
|
// delta = vec.sub([x1 + w1, y1 + h1], [x0 + w0, y0 + h0])
|
|
|
|
// } else {
|
|
|
|
// // moving bottom left, pin top right
|
|
|
|
// delta = vec.sub([x1 + w1, y1], [x0 + w0, y0])
|
|
|
|
// }
|
|
|
|
// } else if (y0 !== y1) {
|
|
|
|
// // moving top right, pin bottom left
|
|
|
|
// delta = vec.sub([x1, y1 + h1], [x0, y0 + h0])
|
|
|
|
// } else {
|
|
|
|
// // moving bottom right, pin top left
|
|
|
|
// delta = vec.sub([x1, y1], [x0, y0])
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (shape.rotation !== 0) {
|
|
|
|
// shape.point = vec.sub(shape.point, delta)
|
|
|
|
// }
|
|
|
|
|
|
|
|
shape.point = [childBounds.minX, childBounds.minY] //vec.add([x1, y1], delta)
|
|
|
|
shape.size = [childBounds.width, childBounds.height]
|
|
|
|
|
|
|
|
return this
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const StyledGroupShape = styled('rect', {
|
|
|
|
zDash: 5,
|
|
|
|
zStrokeWidth: 1,
|
2021-07-07 12:46:46 +00:00
|
|
|
stroke: '$text',
|
2021-06-04 16:08:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
export default group
|