選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

page.tsx 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { getShapeUtils } from 'lib/shape-utils'
  2. import state, { useSelector } from 'state'
  3. import { Bounds, GroupShape, PageState } from 'types'
  4. import { boundsContain } from 'utils/bounds'
  5. import { deepCompareArrays, getPage, screenToWorld } from 'utils/utils'
  6. import Shape from './shape'
  7. /*
  8. On each state change, compare node ids of all shapes
  9. on the current page. Kind of expensive but only happens
  10. here; and still cheaper than any other pattern I've found.
  11. */
  12. const noOffset = [0, 0]
  13. const viewportCache = new WeakMap<PageState, Bounds>()
  14. export default function Page() {
  15. const currentPageShapeIds = useSelector((s) => {
  16. const page = getPage(s.data)
  17. const pageState = s.data.pageStates[page.id]
  18. // if (!viewportCache.has(pageState)) {
  19. // const [minX, minY] = screenToWorld([0, 0], s.data)
  20. // const [maxX, maxY] = screenToWorld(
  21. // [window.innerWidth, window.innerHeight],
  22. // s.data
  23. // )
  24. // viewportCache.set(pageState, {
  25. // minX,
  26. // minY,
  27. // maxX,
  28. // maxY,
  29. // height: maxX - minX,
  30. // width: maxY - minY,
  31. // })
  32. // }
  33. // const viewport = viewportCache.get(pageState)
  34. return (
  35. Object.values(page.shapes)
  36. .filter((shape) => shape.parentId === page.id)
  37. // .filter((shape) => {
  38. // const shapeBounds = getShapeUtils(shape).getBounds(shape)
  39. // console.log(shapeBounds, viewport)
  40. // return boundsContain(viewport, shapeBounds)
  41. // })
  42. .sort((a, b) => a.childIndex - b.childIndex)
  43. .map((shape) => shape.id)
  44. )
  45. }, deepCompareArrays)
  46. const isSelecting = useSelector((s) => s.isIn('selecting'))
  47. return (
  48. <g pointerEvents={isSelecting ? 'all' : 'none'}>
  49. {currentPageShapeIds.map((shapeId) => (
  50. <Shape
  51. key={shapeId}
  52. id={shapeId}
  53. isSelecting={isSelecting}
  54. parentPoint={noOffset}
  55. />
  56. ))}
  57. </g>
  58. )
  59. }