Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

usePageShapes.ts 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { useEffect } from 'react'
  2. import state, { useSelector } from 'state'
  3. import { getShapeUtils } from 'state/shape-utils'
  4. import { PageState, Bounds } from 'types'
  5. import {
  6. boundsCollide,
  7. boundsContain,
  8. debounce,
  9. deepCompareArrays,
  10. getPageState,
  11. getViewport,
  12. } from 'utils'
  13. const viewportCache = new WeakMap<PageState, Bounds>()
  14. export default function usePageShapes(): string[] {
  15. // Reset the viewport cache when the window resizes
  16. useEffect(() => {
  17. const handleResize = debounce(() => state.send('RESIZED_WINDOW'), 32)
  18. window.addEventListener('resize', handleResize)
  19. return () => {
  20. window.removeEventListener('resize', handleResize)
  21. }
  22. }, [])
  23. // Get the shapes that fit into the current window
  24. const visiblePageShapeIds = useSelector((s) => {
  25. const pageState = getPageState(s.data)
  26. if (!viewportCache.has(pageState)) {
  27. const viewport = getViewport(s.data)
  28. viewportCache.set(pageState, viewport)
  29. }
  30. const viewport = viewportCache.get(pageState)
  31. return s.values.currentShapes
  32. .filter((shape) => {
  33. const shapeBounds = getShapeUtils(shape).getBounds(shape)
  34. return (
  35. boundsContain(viewport, shapeBounds) ||
  36. boundsCollide(viewport, shapeBounds)
  37. )
  38. })
  39. .map((shape) => shape.id)
  40. }, deepCompareArrays)
  41. return visiblePageShapeIds
  42. }