Selector.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import React from "react";
  2. import { atom, useRecoilValue } from "recoil";
  3. import { PanZoomRotateState } from "../components/PanZoomRotate";
  4. import { ItemListAtom } from "../components/Items";
  5. import { useRecoilState } from "recoil";
  6. import { insideClass, isPointInsideRect } from "../utils";
  7. export const selectedItemsAtom = atom({
  8. key: "selectedItems",
  9. default: [],
  10. });
  11. const findSelected = (items, rect) => {
  12. return items.filter((item) => {
  13. return (
  14. !item.locked &&
  15. isPointInsideRect({ x: item.x, y: item.y }, rect) &&
  16. isPointInsideRect(
  17. { x: item.x + item.actualWidth, y: item.y + item.actualHeight },
  18. rect
  19. )
  20. );
  21. });
  22. };
  23. const Selector = ({ children }) => {
  24. const panZoomRotate = useRecoilValue(PanZoomRotateState);
  25. const itemList = useRecoilValue(ItemListAtom);
  26. const [selected, setSelected] = useRecoilState(selectedItemsAtom);
  27. const [selector, setSelector] = React.useState({});
  28. const wrapperRef = React.useRef(null);
  29. const stateRef = React.useRef({
  30. moving: false,
  31. });
  32. const onMouseDown = (e) => {
  33. if (e.button === 0 && !insideClass(e.target, "item")) {
  34. const { top, left } = e.currentTarget.getBoundingClientRect();
  35. const displayX = (e.clientX - left) / panZoomRotate.scale;
  36. const displayY = (e.clientY - top) / panZoomRotate.scale;
  37. stateRef.current.moving = true;
  38. stateRef.current.startX = displayX;
  39. stateRef.current.startY = displayY;
  40. setSelector({ ...stateRef.current });
  41. wrapperRef.current.style.cursor = "crosshair";
  42. } else {
  43. if (selected.length) {
  44. /* Should remove selection if clic another item */
  45. /*setSelected([])*/
  46. }
  47. }
  48. };
  49. const onMouseMouve = (e) => {
  50. if (stateRef.current.moving) {
  51. if (selected.length) setSelected([]);
  52. const { top, left } = e.currentTarget.getBoundingClientRect();
  53. const currentX = (e.clientX - left) / panZoomRotate.scale;
  54. const currentY = (e.clientY - top) / panZoomRotate.scale;
  55. if (currentX > stateRef.current.startX) {
  56. stateRef.current.left = stateRef.current.startX;
  57. stateRef.current.width = currentX - stateRef.current.startX;
  58. } else {
  59. stateRef.current.left = currentX;
  60. stateRef.current.width = -currentX + stateRef.current.startX;
  61. }
  62. if (currentY > stateRef.current.startY) {
  63. stateRef.current.top = stateRef.current.startY;
  64. stateRef.current.height = currentY - stateRef.current.startY;
  65. } else {
  66. stateRef.current.top = currentY;
  67. stateRef.current.height = -currentY + stateRef.current.startY;
  68. }
  69. setSelector({ ...stateRef.current });
  70. e.preventDefault();
  71. }
  72. };
  73. const onMouseUp = (e) => {
  74. if (e.button === 0 && stateRef.current.moving) {
  75. const selected = findSelected(itemList, stateRef.current).map(
  76. ({ id }) => id
  77. );
  78. setSelected(selected);
  79. stateRef.current = { moving: false };
  80. setSelector({ ...stateRef.current });
  81. wrapperRef.current.style.cursor = "auto";
  82. }
  83. };
  84. return (
  85. <div
  86. onMouseDown={onMouseDown}
  87. onMouseMove={onMouseMouve}
  88. onMouseUp={onMouseUp}
  89. ref={wrapperRef}
  90. style={{}}
  91. >
  92. {selector.moving && (
  93. <div
  94. style={{
  95. zIndex: 100,
  96. position: "absolute",
  97. backgroundColor: "#FF000050",
  98. top: selector.top,
  99. left: selector.left,
  100. height: selector.height,
  101. width: selector.width,
  102. }}
  103. ></div>
  104. )}
  105. {children}
  106. </div>
  107. );
  108. };
  109. export default Selector;