2021-06-06 10:50:01 +00:00
|
|
|
import { getShapeUtils } from 'lib/shape-utils'
|
|
|
|
import state, { useSelector } from 'state'
|
|
|
|
import { Bounds, GroupShape, PageState } from 'types'
|
2021-06-13 13:04:20 +00:00
|
|
|
import { boundsCollide, boundsContain } from 'utils/bounds'
|
2021-06-06 10:50:01 +00:00
|
|
|
import { deepCompareArrays, getPage, screenToWorld } from 'utils/utils'
|
2021-05-28 14:37:23 +00:00
|
|
|
import Shape from './shape'
|
2021-05-09 21:22:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
On each state change, compare node ids of all shapes
|
|
|
|
on the current page. Kind of expensive but only happens
|
|
|
|
here; and still cheaper than any other pattern I've found.
|
|
|
|
*/
|
|
|
|
|
2021-06-04 16:08:43 +00:00
|
|
|
const noOffset = [0, 0]
|
|
|
|
|
2021-06-06 10:50:01 +00:00
|
|
|
const viewportCache = new WeakMap<PageState, Bounds>()
|
|
|
|
|
2021-05-09 21:22:25 +00:00
|
|
|
export default function Page() {
|
2021-06-06 10:50:01 +00:00
|
|
|
const currentPageShapeIds = useSelector((s) => {
|
|
|
|
const page = getPage(s.data)
|
|
|
|
const pageState = s.data.pageStates[page.id]
|
|
|
|
|
2021-06-13 13:04:20 +00:00
|
|
|
if (!viewportCache.has(pageState)) {
|
|
|
|
const [minX, minY] = screenToWorld([0, 0], s.data)
|
|
|
|
const [maxX, maxY] = screenToWorld(
|
|
|
|
[window.innerWidth, window.innerHeight],
|
|
|
|
s.data
|
|
|
|
)
|
2021-06-15 11:58:51 +00:00
|
|
|
|
2021-06-13 13:04:20 +00:00
|
|
|
viewportCache.set(pageState, {
|
|
|
|
minX,
|
|
|
|
minY,
|
|
|
|
maxX,
|
|
|
|
maxY,
|
|
|
|
height: maxX - minX,
|
|
|
|
width: maxY - minY,
|
|
|
|
})
|
|
|
|
}
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-13 13:04:20 +00:00
|
|
|
const viewport = viewportCache.get(pageState)
|
2021-06-06 10:50:01 +00:00
|
|
|
|
2021-06-13 13:04:20 +00:00
|
|
|
return Object.values(page.shapes)
|
|
|
|
.filter((shape) => shape.parentId === page.id)
|
|
|
|
.filter((shape) => {
|
|
|
|
const shapeBounds = getShapeUtils(shape).getBounds(shape)
|
|
|
|
return (
|
|
|
|
boundsContain(viewport, shapeBounds) ||
|
|
|
|
boundsCollide(viewport, shapeBounds)
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.sort((a, b) => a.childIndex - b.childIndex)
|
|
|
|
.map((shape) => shape.id)
|
2021-05-23 13:46:04 +00:00
|
|
|
}, deepCompareArrays)
|
2021-05-09 21:22:25 +00:00
|
|
|
|
2021-05-28 14:37:23 +00:00
|
|
|
const isSelecting = useSelector((s) => s.isIn('selecting'))
|
|
|
|
|
2021-05-09 21:22:25 +00:00
|
|
|
return (
|
2021-05-30 13:20:25 +00:00
|
|
|
<g pointerEvents={isSelecting ? 'all' : 'none'}>
|
2021-05-09 21:22:25 +00:00
|
|
|
{currentPageShapeIds.map((shapeId) => (
|
2021-06-04 16:08:43 +00:00
|
|
|
<Shape
|
|
|
|
key={shapeId}
|
|
|
|
id={shapeId}
|
|
|
|
isSelecting={isSelecting}
|
|
|
|
parentPoint={noOffset}
|
|
|
|
/>
|
2021-05-09 21:22:25 +00:00
|
|
|
))}
|
2021-05-30 13:20:25 +00:00
|
|
|
</g>
|
2021-05-09 21:22:25 +00:00
|
|
|
)
|
|
|
|
}
|