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. canChangeSharpness,
  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, isBoundToContainer } from "../element/typeChecks";
  29. import clsx from "clsx";
  30. import { actionToggleZenMode } from "../actions";
  31. import "./Actions.scss";
  32. import { Tooltip } from "./Tooltip";
  33. export const SelectedShapeActions = ({
  34. appState,
  35. elements,
  36. renderAction,
  37. }: {
  38. appState: AppState;
  39. elements: readonly ExcalidrawElement[];
  40. renderAction: ActionManager["renderAction"];
  41. }) => {
  42. const targetElements = getTargetElements(
  43. getNonDeletedElements(elements),
  44. appState,
  45. );
  46. let isSingleElementBoundContainer = false;
  47. if (
  48. targetElements.length === 2 &&
  49. (hasBoundTextElement(targetElements[0]) ||
  50. hasBoundTextElement(targetElements[1]))
  51. ) {
  52. isSingleElementBoundContainer = true;
  53. }
  54. const isEditing = Boolean(appState.editingElement);
  55. const device = useDevice();
  56. const isRTL = document.documentElement.getAttribute("dir") === "rtl";
  57. const showFillIcons =
  58. hasBackground(appState.activeTool.type) ||
  59. targetElements.some(
  60. (element) =>
  61. hasBackground(element.type) && !isTransparent(element.backgroundColor),
  62. );
  63. const showChangeBackgroundIcons =
  64. hasBackground(appState.activeTool.type) ||
  65. targetElements.some((element) => hasBackground(element.type));
  66. const showLinkIcon =
  67. targetElements.length === 1 || isSingleElementBoundContainer;
  68. let commonSelectedType: string | null = targetElements[0]?.type || null;
  69. for (const element of targetElements) {
  70. if (element.type !== commonSelectedType) {
  71. commonSelectedType = null;
  72. break;
  73. }
  74. }
  75. return (
  76. <div className="panelColumn">
  77. <div>
  78. {((hasStrokeColor(appState.activeTool.type) &&
  79. appState.activeTool.type !== "image" &&
  80. commonSelectedType !== "image") ||
  81. targetElements.some((element) => hasStrokeColor(element.type))) &&
  82. renderAction("changeStrokeColor")}
  83. </div>
  84. {showChangeBackgroundIcons && (
  85. <div>{renderAction("changeBackgroundColor")}</div>
  86. )}
  87. {showFillIcons && renderAction("changeFillStyle")}
  88. {(hasStrokeWidth(appState.activeTool.type) ||
  89. targetElements.some((element) => hasStrokeWidth(element.type))) &&
  90. renderAction("changeStrokeWidth")}
  91. {(appState.activeTool.type === "freedraw" ||
  92. targetElements.some((element) => element.type === "freedraw")) &&
  93. renderAction("changeStrokeShape")}
  94. {(hasStrokeStyle(appState.activeTool.type) ||
  95. targetElements.some((element) => hasStrokeStyle(element.type))) && (
  96. <>
  97. {renderAction("changeStrokeStyle")}
  98. {renderAction("changeSloppiness")}
  99. </>
  100. )}
  101. {(canChangeSharpness(appState.activeTool.type) ||
  102. targetElements.some((element) => canChangeSharpness(element.type))) && (
  103. <>{renderAction("changeSharpness")}</>
  104. )}
  105. {(hasText(appState.activeTool.type) ||
  106. targetElements.some((element) => hasText(element.type))) && (
  107. <>
  108. {renderAction("changeFontSize")}
  109. {renderAction("changeFontFamily")}
  110. {renderAction("changeTextAlign")}
  111. </>
  112. )}
  113. {targetElements.some(
  114. (element) =>
  115. hasBoundTextElement(element) || isBoundToContainer(element),
  116. ) && renderAction("changeVerticalAlign")}
  117. {(canHaveArrowheads(appState.activeTool.type) ||
  118. targetElements.some((element) => canHaveArrowheads(element.type))) && (
  119. <>{renderAction("changeArrowhead")}</>
  120. )}
  121. {renderAction("changeOpacity")}
  122. <fieldset>
  123. <legend>{t("labels.layers")}</legend>
  124. <div className="buttonList">
  125. {renderAction("sendToBack")}
  126. {renderAction("sendBackward")}
  127. {renderAction("bringToFront")}
  128. {renderAction("bringForward")}
  129. </div>
  130. </fieldset>
  131. {targetElements.length > 1 && !isSingleElementBoundContainer && (
  132. <fieldset>
  133. <legend>{t("labels.align")}</legend>
  134. <div className="buttonList">
  135. {
  136. // swap this order for RTL so the button positions always match their action
  137. // (i.e. the leftmost button aligns left)
  138. }
  139. {isRTL ? (
  140. <>
  141. {renderAction("alignRight")}
  142. {renderAction("alignHorizontallyCentered")}
  143. {renderAction("alignLeft")}
  144. </>
  145. ) : (
  146. <>
  147. {renderAction("alignLeft")}
  148. {renderAction("alignHorizontallyCentered")}
  149. {renderAction("alignRight")}
  150. </>
  151. )}
  152. {targetElements.length > 2 &&
  153. renderAction("distributeHorizontally")}
  154. {/* breaks the row ˇˇ */}
  155. <div style={{ flexBasis: "100%", height: 0 }} />
  156. <div
  157. style={{
  158. display: "flex",
  159. flexWrap: "wrap",
  160. gap: ".5rem",
  161. marginTop: "-0.5rem",
  162. }}
  163. >
  164. {renderAction("alignTop")}
  165. {renderAction("alignVerticallyCentered")}
  166. {renderAction("alignBottom")}
  167. {targetElements.length > 2 &&
  168. renderAction("distributeVertically")}
  169. </div>
  170. </div>
  171. </fieldset>
  172. )}
  173. {!isEditing && targetElements.length > 0 && (
  174. <fieldset>
  175. <legend>{t("labels.actions")}</legend>
  176. <div className="buttonList">
  177. {!device.isMobile && renderAction("duplicateSelection")}
  178. {!device.isMobile && renderAction("deleteSelectedElements")}
  179. {renderAction("group")}
  180. {renderAction("ungroup")}
  181. {showLinkIcon && renderAction("hyperlink")}
  182. </div>
  183. </fieldset>
  184. )}
  185. </div>
  186. );
  187. };
  188. export const ShapesSwitcher = ({
  189. canvas,
  190. activeTool,
  191. setAppState,
  192. onImageAction,
  193. appState,
  194. }: {
  195. canvas: HTMLCanvasElement | null;
  196. activeTool: AppState["activeTool"];
  197. setAppState: React.Component<any, AppState>["setState"];
  198. onImageAction: (data: { pointerType: PointerType | null }) => void;
  199. appState: AppState;
  200. }) => (
  201. <>
  202. {SHAPES.map(({ value, icon, key, numericKey, fillable }, index) => {
  203. const label = t(`toolBar.${value}`);
  204. const letter = key && (typeof key === "string" ? key : key[0]);
  205. const shortcut = letter
  206. ? `${capitalizeString(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}
  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. );