Actions.tsx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import React from "react";
  2. import { ActionManager } from "../actions/manager";
  3. import { getNonDeletedElements } from "../element";
  4. import { ExcalidrawElement, PointerType } from "../element/types";
  5. import { t } from "../i18n";
  6. import { useDevice } from "../components/App";
  7. import {
  8. canChangeRoundness,
  9. canHaveArrowheads,
  10. getTargetElements,
  11. hasBackground,
  12. hasStrokeStyle,
  13. hasStrokeWidth,
  14. hasText,
  15. } from "../scene";
  16. import { SHAPES } from "../shapes";
  17. import { AppState, Zoom } from "../types";
  18. import {
  19. capitalizeString,
  20. isTransparent,
  21. updateActiveTool,
  22. setCursorForShape,
  23. } from "../utils";
  24. import Stack from "./Stack";
  25. import { ToolButton } from "./ToolButton";
  26. import { hasStrokeColor } from "../scene/comparisons";
  27. import { trackEvent } from "../analytics";
  28. import { hasBoundTextElement } from "../element/typeChecks";
  29. import clsx from "clsx";
  30. import { actionToggleZenMode } from "../actions";
  31. import "./Actions.scss";
  32. import { Tooltip } from "./Tooltip";
  33. import { shouldAllowVerticalAlign } from "../element/textElement";
  34. export const SelectedShapeActions = ({
  35. appState,
  36. elements,
  37. renderAction,
  38. }: {
  39. appState: AppState;
  40. elements: readonly ExcalidrawElement[];
  41. renderAction: ActionManager["renderAction"];
  42. }) => {
  43. const targetElements = getTargetElements(
  44. getNonDeletedElements(elements),
  45. appState,
  46. );
  47. let isSingleElementBoundContainer = false;
  48. if (
  49. targetElements.length === 2 &&
  50. (hasBoundTextElement(targetElements[0]) ||
  51. hasBoundTextElement(targetElements[1]))
  52. ) {
  53. isSingleElementBoundContainer = true;
  54. }
  55. const isEditing = Boolean(appState.editingElement);
  56. const device = useDevice();
  57. const isRTL = document.documentElement.getAttribute("dir") === "rtl";
  58. const showFillIcons =
  59. hasBackground(appState.activeTool.type) ||
  60. targetElements.some(
  61. (element) =>
  62. hasBackground(element.type) && !isTransparent(element.backgroundColor),
  63. );
  64. const showChangeBackgroundIcons =
  65. hasBackground(appState.activeTool.type) ||
  66. targetElements.some((element) => hasBackground(element.type));
  67. const showLinkIcon =
  68. targetElements.length === 1 || isSingleElementBoundContainer;
  69. let commonSelectedType: string | null = targetElements[0]?.type || null;
  70. for (const element of targetElements) {
  71. if (element.type !== commonSelectedType) {
  72. commonSelectedType = null;
  73. break;
  74. }
  75. }
  76. return (
  77. <div className="panelColumn">
  78. <div>
  79. {((hasStrokeColor(appState.activeTool.type) &&
  80. appState.activeTool.type !== "image" &&
  81. commonSelectedType !== "image") ||
  82. targetElements.some((element) => hasStrokeColor(element.type))) &&
  83. renderAction("changeStrokeColor")}
  84. </div>
  85. {showChangeBackgroundIcons && (
  86. <div>{renderAction("changeBackgroundColor")}</div>
  87. )}
  88. {showFillIcons && renderAction("changeFillStyle")}
  89. {(hasStrokeWidth(appState.activeTool.type) ||
  90. targetElements.some((element) => hasStrokeWidth(element.type))) &&
  91. renderAction("changeStrokeWidth")}
  92. {(appState.activeTool.type === "freedraw" ||
  93. targetElements.some((element) => element.type === "freedraw")) &&
  94. renderAction("changeStrokeShape")}
  95. {(hasStrokeStyle(appState.activeTool.type) ||
  96. targetElements.some((element) => hasStrokeStyle(element.type))) && (
  97. <>
  98. {renderAction("changeStrokeStyle")}
  99. {renderAction("changeSloppiness")}
  100. </>
  101. )}
  102. {(canChangeRoundness(appState.activeTool.type) ||
  103. targetElements.some((element) => canChangeRoundness(element.type))) && (
  104. <>{renderAction("changeRoundness")}</>
  105. )}
  106. {(hasText(appState.activeTool.type) ||
  107. targetElements.some((element) => hasText(element.type))) && (
  108. <>
  109. {renderAction("changeFontSize")}
  110. {renderAction("changeFontFamily")}
  111. {renderAction("changeTextAlign")}
  112. </>
  113. )}
  114. {shouldAllowVerticalAlign(targetElements) &&
  115. renderAction("changeVerticalAlign")}
  116. {(canHaveArrowheads(appState.activeTool.type) ||
  117. targetElements.some((element) => canHaveArrowheads(element.type))) && (
  118. <>{renderAction("changeArrowhead")}</>
  119. )}
  120. {renderAction("changeOpacity")}
  121. <fieldset>
  122. <legend>{t("labels.layers")}</legend>
  123. <div className="buttonList">
  124. {renderAction("sendToBack")}
  125. {renderAction("sendBackward")}
  126. {renderAction("bringToFront")}
  127. {renderAction("bringForward")}
  128. </div>
  129. </fieldset>
  130. {targetElements.length > 1 && !isSingleElementBoundContainer && (
  131. <fieldset>
  132. <legend>{t("labels.align")}</legend>
  133. <div className="buttonList">
  134. {
  135. // swap this order for RTL so the button positions always match their action
  136. // (i.e. the leftmost button aligns left)
  137. }
  138. {isRTL ? (
  139. <>
  140. {renderAction("alignRight")}
  141. {renderAction("alignHorizontallyCentered")}
  142. {renderAction("alignLeft")}
  143. </>
  144. ) : (
  145. <>
  146. {renderAction("alignLeft")}
  147. {renderAction("alignHorizontallyCentered")}
  148. {renderAction("alignRight")}
  149. </>
  150. )}
  151. {targetElements.length > 2 &&
  152. renderAction("distributeHorizontally")}
  153. {/* breaks the row ˇˇ */}
  154. <div style={{ flexBasis: "100%", height: 0 }} />
  155. <div
  156. style={{
  157. display: "flex",
  158. flexWrap: "wrap",
  159. gap: ".5rem",
  160. marginTop: "-0.5rem",
  161. }}
  162. >
  163. {renderAction("alignTop")}
  164. {renderAction("alignVerticallyCentered")}
  165. {renderAction("alignBottom")}
  166. {targetElements.length > 2 &&
  167. renderAction("distributeVertically")}
  168. </div>
  169. </div>
  170. </fieldset>
  171. )}
  172. {!isEditing && targetElements.length > 0 && (
  173. <fieldset>
  174. <legend>{t("labels.actions")}</legend>
  175. <div className="buttonList">
  176. {!device.isMobile && renderAction("duplicateSelection")}
  177. {!device.isMobile && renderAction("deleteSelectedElements")}
  178. {renderAction("group")}
  179. {renderAction("ungroup")}
  180. {showLinkIcon && renderAction("hyperlink")}
  181. </div>
  182. </fieldset>
  183. )}
  184. </div>
  185. );
  186. };
  187. export const ShapesSwitcher = ({
  188. canvas,
  189. activeTool,
  190. setAppState,
  191. onImageAction,
  192. appState,
  193. }: {
  194. canvas: HTMLCanvasElement | null;
  195. activeTool: AppState["activeTool"];
  196. setAppState: React.Component<any, AppState>["setState"];
  197. onImageAction: (data: { pointerType: PointerType | null }) => void;
  198. appState: AppState;
  199. }) => (
  200. <>
  201. {SHAPES.map(({ value, icon, key, numericKey, fillable }, index) => {
  202. const label = t(`toolBar.${value}`);
  203. const letter =
  204. key && capitalizeString(typeof key === "string" ? key : key[0]);
  205. const shortcut = letter
  206. ? `${letter} ${t("helpDialog.or")} ${numericKey}`
  207. : `${numericKey}`;
  208. return (
  209. <ToolButton
  210. className={clsx("Shape", { fillable })}
  211. key={value}
  212. type="radio"
  213. icon={icon}
  214. checked={activeTool.type === value}
  215. name="editor-current-shape"
  216. title={`${capitalizeString(label)} — ${shortcut}`}
  217. keyBindingLabel={numericKey || letter}
  218. aria-label={capitalizeString(label)}
  219. aria-keyshortcuts={shortcut}
  220. data-testid={`toolbar-${value}`}
  221. onPointerDown={({ pointerType }) => {
  222. if (!appState.penDetected && pointerType === "pen") {
  223. setAppState({
  224. penDetected: true,
  225. penMode: true,
  226. });
  227. }
  228. }}
  229. onChange={({ pointerType }) => {
  230. if (appState.activeTool.type !== value) {
  231. trackEvent("toolbar", value, "ui");
  232. }
  233. const nextActiveTool = updateActiveTool(appState, {
  234. type: value,
  235. });
  236. setAppState({
  237. activeTool: nextActiveTool,
  238. multiElement: null,
  239. selectedElementIds: {},
  240. });
  241. setCursorForShape(canvas, {
  242. ...appState,
  243. activeTool: nextActiveTool,
  244. });
  245. if (value === "image") {
  246. onImageAction({ pointerType });
  247. }
  248. }}
  249. />
  250. );
  251. })}
  252. </>
  253. );
  254. export const ZoomActions = ({
  255. renderAction,
  256. zoom,
  257. }: {
  258. renderAction: ActionManager["renderAction"];
  259. zoom: Zoom;
  260. }) => (
  261. <Stack.Col gap={1} className="zoom-actions">
  262. <Stack.Row align="center">
  263. {renderAction("zoomOut")}
  264. {renderAction("resetZoom")}
  265. {renderAction("zoomIn")}
  266. </Stack.Row>
  267. </Stack.Col>
  268. );
  269. export const UndoRedoActions = ({
  270. renderAction,
  271. className,
  272. }: {
  273. renderAction: ActionManager["renderAction"];
  274. className?: string;
  275. }) => (
  276. <div className={`undo-redo-buttons ${className}`}>
  277. <div className="undo-button-container">
  278. <Tooltip label={t("buttons.undo")}>{renderAction("undo")}</Tooltip>
  279. </div>
  280. <div className="redo-button-container">
  281. <Tooltip label={t("buttons.redo")}> {renderAction("redo")}</Tooltip>
  282. </div>
  283. </div>
  284. );
  285. export const ExitZenModeAction = ({
  286. actionManager,
  287. showExitZenModeBtn,
  288. }: {
  289. actionManager: ActionManager;
  290. showExitZenModeBtn: boolean;
  291. }) => (
  292. <button
  293. className={clsx("disable-zen-mode", {
  294. "disable-zen-mode--visible": showExitZenModeBtn,
  295. })}
  296. onClick={() => actionManager.executeAction(actionToggleZenMode)}
  297. >
  298. {t("buttons.exitZenMode")}
  299. </button>
  300. );
  301. export const FinalizeAction = ({
  302. renderAction,
  303. className,
  304. }: {
  305. renderAction: ActionManager["renderAction"];
  306. className?: string;
  307. }) => (
  308. <div className={`finalize-button ${className}`}>
  309. {renderAction("finalize", { size: "small" })}
  310. </div>
  311. );