2020-03-07 10:20:38 -05:00
|
|
|
import React from "react";
|
|
|
|
import { ActionManager } from "../actions/manager";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { getNonDeletedElements } from "../element";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { ExcalidrawElement, PointerType } from "../element/types";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { t } from "../i18n";
|
2022-06-21 20:03:23 +05:00
|
|
|
import { useDevice } from "../components/App";
|
2020-08-15 00:59:43 +09:00
|
|
|
import {
|
|
|
|
canChangeSharpness,
|
2020-12-08 15:02:55 +00:00
|
|
|
canHaveArrowheads,
|
2020-12-07 18:35:16 +02:00
|
|
|
getTargetElements,
|
2021-01-05 20:06:14 +02:00
|
|
|
hasBackground,
|
2021-05-09 16:42:10 +01:00
|
|
|
hasStrokeStyle,
|
|
|
|
hasStrokeWidth,
|
2021-01-05 20:06:14 +02:00
|
|
|
hasText,
|
2020-08-15 00:59:43 +09:00
|
|
|
} from "../scene";
|
2020-03-07 10:20:38 -05:00
|
|
|
import { SHAPES } from "../shapes";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { AppState, Zoom } from "../types";
|
2022-05-06 18:21:22 +05:30
|
|
|
import {
|
|
|
|
capitalizeString,
|
|
|
|
isTransparent,
|
|
|
|
updateActiveTool,
|
|
|
|
setCursorForShape,
|
|
|
|
} from "../utils";
|
2020-03-07 10:20:38 -05:00
|
|
|
import Stack from "./Stack";
|
2021-01-05 20:06:14 +02:00
|
|
|
import { ToolButton } from "./ToolButton";
|
2021-10-21 22:05:48 +02:00
|
|
|
import { hasStrokeColor } from "../scene/comparisons";
|
2022-03-28 14:46:40 +02:00
|
|
|
import { trackEvent } from "../analytics";
|
2022-03-02 20:06:07 +05:30
|
|
|
import { hasBoundTextElement, isBoundToContainer } from "../element/typeChecks";
|
2022-08-22 16:09:24 +05:30
|
|
|
import clsx from "clsx";
|
|
|
|
import { actionToggleZenMode } from "../actions";
|
2022-11-01 17:29:58 +01:00
|
|
|
import "./Actions.scss";
|
|
|
|
import { Tooltip } from "./Tooltip";
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const SelectedShapeActions = ({
|
2020-04-02 16:52:24 +09:00
|
|
|
appState,
|
|
|
|
elements,
|
2020-03-07 10:20:38 -05:00
|
|
|
renderAction,
|
|
|
|
}: {
|
2020-04-02 16:52:24 +09:00
|
|
|
appState: AppState;
|
|
|
|
elements: readonly ExcalidrawElement[];
|
2020-03-07 10:20:38 -05:00
|
|
|
renderAction: ActionManager["renderAction"];
|
2020-05-20 16:21:37 +03:00
|
|
|
}) => {
|
2020-12-07 18:35:16 +02:00
|
|
|
const targetElements = getTargetElements(
|
2020-04-08 09:49:52 -07:00
|
|
|
getNonDeletedElements(elements),
|
|
|
|
appState,
|
|
|
|
);
|
2022-02-28 11:08:28 +05:30
|
|
|
|
|
|
|
let isSingleElementBoundContainer = false;
|
|
|
|
if (
|
|
|
|
targetElements.length === 2 &&
|
|
|
|
(hasBoundTextElement(targetElements[0]) ||
|
|
|
|
hasBoundTextElement(targetElements[1]))
|
|
|
|
) {
|
|
|
|
isSingleElementBoundContainer = true;
|
|
|
|
}
|
2020-04-02 16:52:24 +09:00
|
|
|
const isEditing = Boolean(appState.editingElement);
|
2022-06-21 20:03:23 +05:00
|
|
|
const device = useDevice();
|
2020-11-25 18:21:33 -05:00
|
|
|
const isRTL = document.documentElement.getAttribute("dir") === "rtl";
|
|
|
|
|
2020-11-27 02:13:38 +05:30
|
|
|
const showFillIcons =
|
2022-08-20 22:49:44 +05:30
|
|
|
hasBackground(appState.activeTool.type) ||
|
2020-11-27 02:13:38 +05:30
|
|
|
targetElements.some(
|
|
|
|
(element) =>
|
|
|
|
hasBackground(element.type) && !isTransparent(element.backgroundColor),
|
|
|
|
);
|
|
|
|
const showChangeBackgroundIcons =
|
2022-08-20 22:49:44 +05:30
|
|
|
hasBackground(appState.activeTool.type) ||
|
2020-11-27 02:13:38 +05:30
|
|
|
targetElements.some((element) => hasBackground(element.type));
|
2020-12-08 15:02:55 +00:00
|
|
|
|
2022-03-23 00:45:08 +05:30
|
|
|
const showLinkIcon =
|
|
|
|
targetElements.length === 1 || isSingleElementBoundContainer;
|
|
|
|
|
2021-10-21 22:05:48 +02:00
|
|
|
let commonSelectedType: string | null = targetElements[0]?.type || null;
|
|
|
|
|
|
|
|
for (const element of targetElements) {
|
|
|
|
if (element.type !== commonSelectedType) {
|
|
|
|
commonSelectedType = null;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
return (
|
|
|
|
<div className="panelColumn">
|
2022-11-01 17:29:58 +01:00
|
|
|
<div>
|
|
|
|
{((hasStrokeColor(appState.activeTool.type) &&
|
|
|
|
appState.activeTool.type !== "image" &&
|
|
|
|
commonSelectedType !== "image") ||
|
|
|
|
targetElements.some((element) => hasStrokeColor(element.type))) &&
|
|
|
|
renderAction("changeStrokeColor")}
|
|
|
|
</div>
|
|
|
|
{showChangeBackgroundIcons && (
|
|
|
|
<div>{renderAction("changeBackgroundColor")}</div>
|
|
|
|
)}
|
2020-11-27 02:13:38 +05:30
|
|
|
{showFillIcons && renderAction("changeFillStyle")}
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2022-08-20 22:49:44 +05:30
|
|
|
{(hasStrokeWidth(appState.activeTool.type) ||
|
2021-05-09 16:42:10 +01:00
|
|
|
targetElements.some((element) => hasStrokeWidth(element.type))) &&
|
|
|
|
renderAction("changeStrokeWidth")}
|
|
|
|
|
2022-08-20 22:49:44 +05:30
|
|
|
{(appState.activeTool.type === "freedraw" ||
|
2021-05-09 16:42:10 +01:00
|
|
|
targetElements.some((element) => element.type === "freedraw")) &&
|
|
|
|
renderAction("changeStrokeShape")}
|
|
|
|
|
2022-08-20 22:49:44 +05:30
|
|
|
{(hasStrokeStyle(appState.activeTool.type) ||
|
2021-05-09 16:42:10 +01:00
|
|
|
targetElements.some((element) => hasStrokeStyle(element.type))) && (
|
2020-03-07 10:20:38 -05:00
|
|
|
<>
|
2020-05-14 17:04:33 +02:00
|
|
|
{renderAction("changeStrokeStyle")}
|
2020-03-07 10:20:38 -05:00
|
|
|
{renderAction("changeSloppiness")}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2022-08-20 22:49:44 +05:30
|
|
|
{(canChangeSharpness(appState.activeTool.type) ||
|
2020-08-15 00:59:43 +09:00
|
|
|
targetElements.some((element) => canChangeSharpness(element.type))) && (
|
|
|
|
<>{renderAction("changeSharpness")}</>
|
|
|
|
)}
|
|
|
|
|
2022-08-20 22:49:44 +05:30
|
|
|
{(hasText(appState.activeTool.type) ||
|
2020-03-23 13:05:07 +02:00
|
|
|
targetElements.some((element) => hasText(element.type))) && (
|
2020-03-07 10:20:38 -05:00
|
|
|
<>
|
|
|
|
{renderAction("changeFontSize")}
|
|
|
|
|
|
|
|
{renderAction("changeFontFamily")}
|
2020-04-08 21:00:27 +01:00
|
|
|
|
|
|
|
{renderAction("changeTextAlign")}
|
2020-03-07 10:20:38 -05:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2022-03-02 23:56:20 +01:00
|
|
|
{targetElements.some(
|
2022-03-02 20:06:07 +05:30
|
|
|
(element) =>
|
|
|
|
hasBoundTextElement(element) || isBoundToContainer(element),
|
2022-03-02 23:56:20 +01:00
|
|
|
) && renderAction("changeVerticalAlign")}
|
2022-08-20 22:49:44 +05:30
|
|
|
{(canHaveArrowheads(appState.activeTool.type) ||
|
2020-12-08 15:02:55 +00:00
|
|
|
targetElements.some((element) => canHaveArrowheads(element.type))) && (
|
|
|
|
<>{renderAction("changeArrowhead")}</>
|
|
|
|
)}
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
{renderAction("changeOpacity")}
|
|
|
|
|
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.layers")}</legend>
|
|
|
|
<div className="buttonList">
|
|
|
|
{renderAction("sendToBack")}
|
|
|
|
{renderAction("sendBackward")}
|
|
|
|
{renderAction("bringToFront")}
|
|
|
|
{renderAction("bringForward")}
|
|
|
|
</div>
|
|
|
|
</fieldset>
|
2020-10-31 11:40:06 +01:00
|
|
|
|
2022-02-28 11:08:28 +05:30
|
|
|
{targetElements.length > 1 && !isSingleElementBoundContainer && (
|
2020-10-31 11:40:06 +01:00
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.align")}</legend>
|
|
|
|
<div className="buttonList">
|
2020-11-25 18:21:33 -05:00
|
|
|
{
|
|
|
|
// swap this order for RTL so the button positions always match their action
|
|
|
|
// (i.e. the leftmost button aligns left)
|
|
|
|
}
|
|
|
|
{isRTL ? (
|
|
|
|
<>
|
|
|
|
{renderAction("alignRight")}
|
|
|
|
{renderAction("alignHorizontallyCentered")}
|
|
|
|
{renderAction("alignLeft")}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{renderAction("alignLeft")}
|
|
|
|
{renderAction("alignHorizontallyCentered")}
|
|
|
|
{renderAction("alignRight")}
|
|
|
|
</>
|
|
|
|
)}
|
2020-11-23 18:16:23 +00:00
|
|
|
{targetElements.length > 2 &&
|
|
|
|
renderAction("distributeHorizontally")}
|
2022-11-01 17:29:58 +01:00
|
|
|
{/* breaks the row ˇˇ */}
|
|
|
|
<div style={{ flexBasis: "100%", height: 0 }} />
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: "flex",
|
|
|
|
flexWrap: "wrap",
|
|
|
|
gap: ".5rem",
|
|
|
|
marginTop: "-0.5rem",
|
|
|
|
}}
|
|
|
|
>
|
2020-11-23 18:16:23 +00:00
|
|
|
{renderAction("alignTop")}
|
|
|
|
{renderAction("alignVerticallyCentered")}
|
|
|
|
{renderAction("alignBottom")}
|
|
|
|
{targetElements.length > 2 &&
|
|
|
|
renderAction("distributeVertically")}
|
|
|
|
</div>
|
2020-10-31 11:40:06 +01:00
|
|
|
</div>
|
|
|
|
</fieldset>
|
|
|
|
)}
|
2022-02-16 15:41:35 +05:30
|
|
|
{!isEditing && targetElements.length > 0 && (
|
2020-04-02 00:13:53 +09:00
|
|
|
<fieldset>
|
|
|
|
<legend>{t("labels.actions")}</legend>
|
|
|
|
<div className="buttonList">
|
2022-06-21 20:03:23 +05:00
|
|
|
{!device.isMobile && renderAction("duplicateSelection")}
|
|
|
|
{!device.isMobile && renderAction("deleteSelectedElements")}
|
2020-07-26 00:42:06 +02:00
|
|
|
{renderAction("group")}
|
|
|
|
{renderAction("ungroup")}
|
2022-03-23 00:45:08 +05:30
|
|
|
{showLinkIcon && renderAction("hyperlink")}
|
2020-04-02 00:13:53 +09:00
|
|
|
</div>
|
|
|
|
</fieldset>
|
|
|
|
)}
|
2020-03-07 10:20:38 -05:00
|
|
|
</div>
|
|
|
|
);
|
2020-05-20 16:21:37 +03:00
|
|
|
};
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const ShapesSwitcher = ({
|
2021-03-03 14:04:02 +01:00
|
|
|
canvas,
|
2022-03-25 20:46:01 +05:30
|
|
|
activeTool,
|
2020-03-07 10:20:38 -05:00
|
|
|
setAppState,
|
2021-10-21 22:05:48 +02:00
|
|
|
onImageAction,
|
2022-03-15 20:56:39 +05:30
|
|
|
appState,
|
2020-03-07 10:20:38 -05:00
|
|
|
}: {
|
2021-03-03 14:04:02 +01:00
|
|
|
canvas: HTMLCanvasElement | null;
|
2022-03-25 20:46:01 +05:30
|
|
|
activeTool: AppState["activeTool"];
|
2020-10-16 11:53:40 +02:00
|
|
|
setAppState: React.Component<any, AppState>["setState"];
|
2021-10-21 22:05:48 +02:00
|
|
|
onImageAction: (data: { pointerType: PointerType | null }) => void;
|
2022-03-15 20:56:39 +05:30
|
|
|
appState: AppState;
|
2022-04-15 12:20:51 +02:00
|
|
|
}) => (
|
|
|
|
<>
|
2022-11-01 17:29:58 +01:00
|
|
|
{SHAPES.map(({ value, icon, key, fillable }, index) => {
|
|
|
|
const numberKey = value === "eraser" ? 0 : index + 1;
|
2022-04-15 12:20:51 +02:00
|
|
|
const label = t(`toolBar.${value}`);
|
|
|
|
const letter = key && (typeof key === "string" ? key : key[0]);
|
|
|
|
const shortcut = letter
|
2022-11-01 17:29:58 +01:00
|
|
|
? `${capitalizeString(letter)} ${t("helpDialog.or")} ${numberKey}`
|
|
|
|
: `${numberKey}`;
|
2022-04-15 12:20:51 +02:00
|
|
|
return (
|
|
|
|
<ToolButton
|
2022-11-01 17:29:58 +01:00
|
|
|
className={clsx("Shape", { fillable })}
|
2022-04-15 12:20:51 +02:00
|
|
|
key={value}
|
|
|
|
type="radio"
|
|
|
|
icon={icon}
|
|
|
|
checked={activeTool.type === value}
|
|
|
|
name="editor-current-shape"
|
|
|
|
title={`${capitalizeString(label)} — ${shortcut}`}
|
2022-11-01 17:29:58 +01:00
|
|
|
keyBindingLabel={`${numberKey}`}
|
2022-04-15 12:20:51 +02:00
|
|
|
aria-label={capitalizeString(label)}
|
|
|
|
aria-keyshortcuts={shortcut}
|
|
|
|
data-testid={value}
|
|
|
|
onPointerDown={({ pointerType }) => {
|
|
|
|
if (!appState.penDetected && pointerType === "pen") {
|
|
|
|
setAppState({
|
|
|
|
penDetected: true,
|
|
|
|
penMode: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onChange={({ pointerType }) => {
|
|
|
|
if (appState.activeTool.type !== value) {
|
|
|
|
trackEvent("toolbar", value, "ui");
|
|
|
|
}
|
2022-05-06 18:21:22 +05:30
|
|
|
const nextActiveTool = updateActiveTool(appState, {
|
|
|
|
type: value,
|
|
|
|
});
|
2022-04-15 12:20:51 +02:00
|
|
|
setAppState({
|
|
|
|
activeTool: nextActiveTool,
|
|
|
|
multiElement: null,
|
|
|
|
selectedElementIds: {},
|
|
|
|
});
|
|
|
|
setCursorForShape(canvas, {
|
|
|
|
...appState,
|
|
|
|
activeTool: nextActiveTool,
|
|
|
|
});
|
|
|
|
if (value === "image") {
|
|
|
|
onImageAction({ pointerType });
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2020-05-20 16:21:37 +03:00
|
|
|
export const ZoomActions = ({
|
2020-03-07 10:20:38 -05:00
|
|
|
renderAction,
|
|
|
|
zoom,
|
|
|
|
}: {
|
|
|
|
renderAction: ActionManager["renderAction"];
|
2020-11-04 17:49:15 +00:00
|
|
|
zoom: Zoom;
|
2020-05-20 16:21:37 +03:00
|
|
|
}) => (
|
2022-11-01 17:29:58 +01:00
|
|
|
<Stack.Col gap={1} className="zoom-actions">
|
|
|
|
<Stack.Row align="center">
|
2020-05-20 16:21:37 +03:00
|
|
|
{renderAction("zoomOut")}
|
|
|
|
{renderAction("resetZoom")}
|
2022-11-01 17:29:58 +01:00
|
|
|
{renderAction("zoomIn")}
|
2020-05-20 16:21:37 +03:00
|
|
|
</Stack.Row>
|
|
|
|
</Stack.Col>
|
|
|
|
);
|
2022-08-22 16:09:24 +05:30
|
|
|
|
|
|
|
export const UndoRedoActions = ({
|
|
|
|
renderAction,
|
|
|
|
className,
|
|
|
|
}: {
|
|
|
|
renderAction: ActionManager["renderAction"];
|
|
|
|
className?: string;
|
|
|
|
}) => (
|
|
|
|
<div className={`undo-redo-buttons ${className}`}>
|
2022-11-01 17:29:58 +01:00
|
|
|
<div className="undo-button-container">
|
|
|
|
<Tooltip label={t("buttons.undo")}>{renderAction("undo")}</Tooltip>
|
|
|
|
</div>
|
|
|
|
<div className="redo-button-container">
|
|
|
|
<Tooltip label={t("buttons.redo")}> {renderAction("redo")}</Tooltip>
|
|
|
|
</div>
|
2022-08-22 16:09:24 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const ExitZenModeAction = ({
|
2022-09-09 13:53:38 +02:00
|
|
|
actionManager,
|
2022-08-22 16:09:24 +05:30
|
|
|
showExitZenModeBtn,
|
|
|
|
}: {
|
2022-09-09 13:53:38 +02:00
|
|
|
actionManager: ActionManager;
|
2022-08-22 16:09:24 +05:30
|
|
|
showExitZenModeBtn: boolean;
|
|
|
|
}) => (
|
|
|
|
<button
|
|
|
|
className={clsx("disable-zen-mode", {
|
|
|
|
"disable-zen-mode--visible": showExitZenModeBtn,
|
|
|
|
})}
|
2022-09-09 13:53:38 +02:00
|
|
|
onClick={() => actionManager.executeAction(actionToggleZenMode)}
|
2022-08-22 16:09:24 +05:30
|
|
|
>
|
|
|
|
{t("buttons.exitZenMode")}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
|
|
|
|
export const FinalizeAction = ({
|
|
|
|
renderAction,
|
|
|
|
className,
|
|
|
|
}: {
|
|
|
|
renderAction: ActionManager["renderAction"];
|
|
|
|
className?: string;
|
|
|
|
}) => (
|
|
|
|
<div className={`finalize-button ${className}`}>
|
|
|
|
{renderAction("finalize", { size: "small" })}
|
|
|
|
</div>
|
|
|
|
);
|