Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

page.tsx 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { getShapeUtils } from 'lib/shape-utils'
  2. import state, { useSelector } from 'state'
  3. import { Bounds, GroupShape, PageState } from 'types'
  4. import { boundsCollide, 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 Object.values(page.shapes)
  35. .filter((shape) => shape.parentId === page.id)
  36. .filter((shape) => {
  37. const shapeBounds = getShapeUtils(shape).getBounds(shape)
  38. return (
  39. boundsContain(viewport, shapeBounds) ||
  40. boundsCollide(viewport, shapeBounds)
  41. )
  42. })
  43. .sort((a, b) => a.childIndex - b.childIndex)
  44. .map((shape) => shape.id)
  45. }, deepCompareArrays)
  46. console.log(currentPageShapeIds.length)
  47. const isSelecting = useSelector((s) => s.isIn('selecting'))
  48. return (
  49. <g pointerEvents={isSelecting ? 'all' : 'none'}>
  50. {currentPageShapeIds.map((shapeId) => (
  51. <Shape
  52. key={shapeId}
  53. id={shapeId}
  54. isSelecting={isSelecting}
  55. parentPoint={noOffset}
  56. />
  57. ))}
  58. </g>
  59. )
  60. }