SelectedItemsPane.jsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import React from "react";
  2. import styled from "styled-components";
  3. import { toast } from "react-toastify";
  4. import { useRecoilValue, useRecoilCallback } from "recoil";
  5. import debounce from "lodash.debounce";
  6. import { useTranslation } from "react-i18next";
  7. import { insideClass, hasClass } from "./utils";
  8. import SidePanel from "./ui/SidePanel";
  9. import { useItemActions } from "./board/Items/useItemActions";
  10. import {
  11. SelectedItemsAtom,
  12. PanZoomRotateAtom,
  13. BoardStateAtom,
  14. ItemMapAtom,
  15. } from "./board";
  16. import ItemFormFactory from "./board/Items/ItemFormFactory";
  17. // import { confirmAlert } from "react-confirm-alert";
  18. const ActionPane = styled.div.attrs(({ top, left, height }) => {
  19. if (top < 120) {
  20. return {
  21. style: {
  22. transform: `translate(${left}px, ${top + height + 5}px)`,
  23. },
  24. };
  25. } else {
  26. return {
  27. style: {
  28. transform: `translate(${left}px, ${top - 60}px)`,
  29. },
  30. };
  31. }
  32. })`
  33. top: 0;
  34. left: 0;
  35. user-select: none;
  36. touch-action: none;
  37. position: absolute;
  38. display: flex;
  39. background-color: var(--color-blueGrey);
  40. justify-content: center;
  41. align-items: center;
  42. border-radius: 4px;
  43. padding: 0.1em 0.5em;
  44. transition: opacity 100ms;
  45. opacity: ${({ hide }) => (hide ? 0 : 0.9)};
  46. box-shadow: 2px 2px 10px 0.3px rgba(0, 0, 0, 0.5);
  47. &:hover{
  48. opacity: 1;
  49. }
  50. & button{
  51. margin 0 4px;
  52. padding: 0em;
  53. height: 50px
  54. }
  55. & .button.icon-only{
  56. padding: 0em;
  57. opacity: 0.5;
  58. }
  59. & button.icon-only:hover{
  60. opacity: 1;
  61. }
  62. & .count{
  63. color: var(--color-secondary);
  64. display: flex;
  65. flex-direction: column;
  66. align-items: center;
  67. line-height: 0.8em;
  68. }
  69. & .number{
  70. font-size: 1.5em;
  71. line-height: 1em;
  72. }
  73. `;
  74. const CardContent = styled.div.attrs(() => ({ className: "content" }))`
  75. display: flex;
  76. flex-direction: column;
  77. padding: 0.5em;
  78. `;
  79. const BoundingBoxZone = styled.div.attrs(({ top, left, height, width }) => ({
  80. style: {
  81. transform: `translate(${left}px, ${top}px)`,
  82. height: `${height}px`,
  83. width: `${width}px`,
  84. },
  85. }))`
  86. top: 0;
  87. left: 0;
  88. z-index: 210;
  89. position: absolute;
  90. background-color: hsla(0, 40%, 50%, 0%);
  91. border: 1px dashed hsl(20, 55%, 40%);
  92. pointer-events: none;
  93. `;
  94. const BoundingBox = ({
  95. boundingBoxLast,
  96. setBoundingBoxLast,
  97. selectedItems,
  98. }) => {
  99. const panZoomRotate = useRecoilValue(PanZoomRotateAtom);
  100. const itemMap = useRecoilValue(ItemMapAtom);
  101. // Update selection bounding box
  102. const updateBox = useRecoilCallback(
  103. ({ snapshot }) => async () => {
  104. const selectedItems = await snapshot.getPromise(SelectedItemsAtom);
  105. if (selectedItems.length === 0) {
  106. setBoundingBoxLast(null);
  107. return;
  108. }
  109. let boundingBox = null;
  110. selectedItems.forEach((itemId) => {
  111. const elem = document.getElementById(itemId);
  112. if (!elem) return;
  113. const {
  114. right: x2,
  115. bottom: y2,
  116. top: y,
  117. left: x,
  118. } = elem.getBoundingClientRect();
  119. if (!boundingBox) {
  120. boundingBox = { x, y, x2, y2 };
  121. } else {
  122. if (x < boundingBox.x) {
  123. boundingBox.x = x;
  124. }
  125. if (y < boundingBox.y) {
  126. boundingBox.y = y;
  127. }
  128. if (x2 > boundingBox.x2) {
  129. boundingBox.x2 = x2;
  130. }
  131. if (y2 > boundingBox.y2) {
  132. boundingBox.y2 = y2;
  133. }
  134. }
  135. });
  136. if (!boundingBox) {
  137. setBoundingBoxLast(null);
  138. return;
  139. }
  140. const newBB = {
  141. top: boundingBox.y,
  142. left: boundingBox.x,
  143. height: boundingBox.y2 - boundingBox.y,
  144. width: boundingBox.x2 - boundingBox.x,
  145. };
  146. setBoundingBoxLast((prevBB) => {
  147. if (
  148. !prevBB ||
  149. prevBB.top !== newBB.top ||
  150. prevBB.left !== newBB.left ||
  151. prevBB.width !== newBB.width ||
  152. prevBB.height !== newBB.height
  153. ) {
  154. return newBB;
  155. }
  156. return prevBB;
  157. });
  158. },
  159. [setBoundingBoxLast]
  160. );
  161. // Debounced version of update box
  162. // eslint-disable-next-line react-hooks/exhaustive-deps
  163. const updateBoxDelay = React.useCallback(
  164. debounce(() => {
  165. updateBox();
  166. }, 300),
  167. [updateBox]
  168. );
  169. React.useEffect(() => {
  170. // Update selected elements bounding box
  171. updateBox();
  172. updateBoxDelay(); // Delay to update after board item animation like tap/untap.
  173. }, [selectedItems, itemMap, panZoomRotate, updateBox, updateBoxDelay]);
  174. if (!boundingBoxLast || selectedItems.length < 2) return null;
  175. return <BoundingBoxZone {...boundingBoxLast} />;
  176. };
  177. export const SelectedItemsPane = ({
  178. hideMenu = false,
  179. actionMap,
  180. itemMap,
  181. ItemFormComponent,
  182. }) => {
  183. const { availableActions } = useItemActions(itemMap);
  184. const [showEdit, setShowEdit] = React.useState(false);
  185. const { t } = useTranslation();
  186. const selectedItems = useRecoilValue(SelectedItemsAtom);
  187. const boardState = useRecoilValue(BoardStateAtom);
  188. const [boundingBoxLast, setBoundingBoxLast] = React.useState(null);
  189. React.useEffect(() => {
  190. const onKeyUp = (e) => {
  191. // Block shortcut if we are typing in a textarea or input
  192. if (["INPUT", "TEXTAREA"].includes(e.target.tagName)) return;
  193. Object.keys(actionMap).forEach((key) => {
  194. const { shortcut, action, edit: whileEdit } = actionMap[key];
  195. if (
  196. availableActions.includes(key) &&
  197. e.key === shortcut &&
  198. showEdit === !!whileEdit
  199. ) {
  200. action();
  201. }
  202. });
  203. };
  204. document.addEventListener("keyup", onKeyUp);
  205. return () => {
  206. document.removeEventListener("keyup", onKeyUp);
  207. };
  208. }, [actionMap, availableActions, showEdit]);
  209. const onDblClick = React.useCallback(
  210. (e) => {
  211. const foundElement = insideClass(e.target, "item");
  212. // We dblclick outside of an element
  213. if (!foundElement) return;
  214. if (hasClass(foundElement, "locked")) {
  215. toast.info(t("Long click to select locked elements"));
  216. return;
  217. }
  218. const filteredActions = availableActions.filter(
  219. (action) => !actionMap[action].disableDblclick
  220. );
  221. if (e.ctrlKey && filteredActions.length > 1) {
  222. // Use second action
  223. actionMap[filteredActions[1]].action();
  224. } else {
  225. if (filteredActions.length > 0) {
  226. actionMap[filteredActions[0]].action();
  227. }
  228. }
  229. },
  230. [actionMap, availableActions, t]
  231. );
  232. React.useEffect(() => {
  233. document.addEventListener("dblclick", onDblClick);
  234. return () => {
  235. document.removeEventListener("dblclick", onDblClick);
  236. };
  237. }, [onDblClick]);
  238. if (hideMenu || selectedItems.length === 0) {
  239. return null;
  240. }
  241. // Keep this code for later
  242. /*const onRemove = () => {
  243. confirmAlert({
  244. title: t("Confirmation"),
  245. message: t("Do you really want to remove selected items ?"),
  246. buttons: [
  247. {
  248. label: t("Yes"),
  249. onClick: remove,
  250. },
  251. {
  252. label: t("No"),
  253. onClick: () => {},
  254. },
  255. ],
  256. });
  257. };*/
  258. let title = "";
  259. if (selectedItems.length === 1) {
  260. title = t("Edit item");
  261. }
  262. if (selectedItems.length > 1) {
  263. title = t("Edit all items");
  264. }
  265. return (
  266. <>
  267. <SidePanel
  268. key={selectedItems[0]}
  269. open={showEdit && !boardState.selecting}
  270. onClose={() => {
  271. setShowEdit(false);
  272. }}
  273. title={title}
  274. width="25%"
  275. >
  276. <CardContent>
  277. <ItemFormFactory ItemFormComponent={ItemFormComponent} />
  278. </CardContent>
  279. </SidePanel>
  280. {selectedItems.length && !hideMenu && (
  281. <ActionPane
  282. {...boundingBoxLast}
  283. hide={
  284. boardState.zooming || boardState.panning || boardState.movingItems
  285. }
  286. >
  287. {(selectedItems.length > 1 || boardState.selecting) && (
  288. <div className="count">
  289. <span className="number">{selectedItems.length}</span>
  290. <span>{t("Items")}</span>
  291. </div>
  292. )}
  293. {!boardState.selecting &&
  294. availableActions.map((action) => {
  295. const {
  296. label,
  297. action: handler,
  298. multiple,
  299. edit: onlyEdit,
  300. shortcut,
  301. icon,
  302. } = actionMap[action];
  303. if (multiple && selectedItems.length < 2) return null;
  304. if (onlyEdit && !showEdit) return null;
  305. return (
  306. <button
  307. className="button clear icon-only"
  308. key={action}
  309. onClick={() => handler()}
  310. title={label + (shortcut ? ` (${shortcut})` : "")}
  311. >
  312. <img
  313. src={icon}
  314. style={{ width: "32px", height: "32px" }}
  315. alt={label}
  316. />
  317. </button>
  318. );
  319. })}
  320. {!boardState.selecting && (
  321. <button
  322. className="button clear icon-only"
  323. onClick={() => setShowEdit((prev) => !prev)}
  324. title={t("Edit")}
  325. >
  326. {!showEdit && (
  327. <img
  328. src="https://icongr.am/feather/edit.svg?size=32&color=ffffff"
  329. alt={t("Edit")}
  330. />
  331. )}
  332. {showEdit && (
  333. <img
  334. src="https://icongr.am/feather/edit.svg?size=32&color=db5034"
  335. alt={t("Edit")}
  336. />
  337. )}
  338. </button>
  339. )}
  340. </ActionPane>
  341. )}
  342. {!boardState.movingItems && (
  343. <BoundingBox
  344. boundingBoxLast={boundingBoxLast}
  345. setBoundingBoxLast={setBoundingBoxLast}
  346. selectedItems={selectedItems}
  347. />
  348. )}
  349. </>
  350. );
  351. };
  352. export default SelectedItemsPane;