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-03-16 15:59:30 +01:00
|
|
|
import { useDeviceType } 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-03-22 13:29:27 +01:00
|
|
|
import {
|
|
|
|
capitalizeString,
|
|
|
|
isTransparent,
|
|
|
|
setCursorForShape,
|
|
|
|
withBatchedUpdates,
|
|
|
|
} 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-02 20:06:07 +05:30
|
|
|
import { hasBoundTextElement, isBoundToContainer } from "../element/typeChecks";
|
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,
|
|
|
|
elementType,
|
|
|
|
}: {
|
2020-04-02 16:52:24 +09:00
|
|
|
appState: AppState;
|
|
|
|
elements: readonly ExcalidrawElement[];
|
2020-03-07 10:20:38 -05:00
|
|
|
renderAction: ActionManager["renderAction"];
|
2022-03-11 19:53:42 +05:30
|
|
|
elementType: AppState["elementType"];
|
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-03-16 15:59:30 +01:00
|
|
|
const deviceType = useDeviceType();
|
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 =
|
|
|
|
hasBackground(elementType) ||
|
|
|
|
targetElements.some(
|
|
|
|
(element) =>
|
|
|
|
hasBackground(element.type) && !isTransparent(element.backgroundColor),
|
|
|
|
);
|
|
|
|
const showChangeBackgroundIcons =
|
|
|
|
hasBackground(elementType) ||
|
|
|
|
targetElements.some((element) => hasBackground(element.type));
|
2020-12-08 15:02:55 +00:00
|
|
|
|
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">
|
2021-10-21 22:05:48 +02:00
|
|
|
{((hasStrokeColor(elementType) &&
|
|
|
|
elementType !== "image" &&
|
|
|
|
commonSelectedType !== "image") ||
|
|
|
|
targetElements.some((element) => hasStrokeColor(element.type))) &&
|
|
|
|
renderAction("changeStrokeColor")}
|
2020-11-27 02:13:38 +05:30
|
|
|
{showChangeBackgroundIcons && renderAction("changeBackgroundColor")}
|
|
|
|
{showFillIcons && renderAction("changeFillStyle")}
|
2020-03-07 10:20:38 -05:00
|
|
|
|
2021-05-09 16:42:10 +01:00
|
|
|
{(hasStrokeWidth(elementType) ||
|
|
|
|
targetElements.some((element) => hasStrokeWidth(element.type))) &&
|
|
|
|
renderAction("changeStrokeWidth")}
|
|
|
|
|
|
|
|
{(elementType === "freedraw" ||
|
|
|
|
targetElements.some((element) => element.type === "freedraw")) &&
|
|
|
|
renderAction("changeStrokeShape")}
|
|
|
|
|
|
|
|
{(hasStrokeStyle(elementType) ||
|
|
|
|
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")}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2020-08-15 00:59:43 +09:00
|
|
|
{(canChangeSharpness(elementType) ||
|
|
|
|
targetElements.some((element) => canChangeSharpness(element.type))) && (
|
|
|
|
<>{renderAction("changeSharpness")}</>
|
|
|
|
)}
|
|
|
|
|
2020-03-07 10:20:38 -05:00
|
|
|
{(hasText(elementType) ||
|
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")}
|
2020-12-08 15:02:55 +00:00
|
|
|
{(canHaveArrowheads(elementType) ||
|
|
|
|
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")}
|
|
|
|
<div className="iconRow">
|
|
|
|
{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-03-16 15:59:30 +01:00
|
|
|
{!deviceType.isMobile && renderAction("duplicateSelection")}
|
|
|
|
{!deviceType.isMobile && renderAction("deleteSelectedElements")}
|
2020-07-26 00:42:06 +02:00
|
|
|
{renderAction("group")}
|
|
|
|
{renderAction("ungroup")}
|
2022-02-21 17:44:28 +05:30
|
|
|
{targetElements.length === 1 && 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,
|
2020-03-07 10:20:38 -05:00
|
|
|
elementType,
|
|
|
|
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-11 19:53:42 +05:30
|
|
|
elementType: AppState["elementType"];
|
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-03-22 13:29:27 +01:00
|
|
|
}) => {
|
|
|
|
const onChange = withBatchedUpdates(
|
|
|
|
({
|
|
|
|
elementType,
|
|
|
|
pointerType,
|
|
|
|
}: {
|
|
|
|
elementType: typeof SHAPES[number]["value"];
|
|
|
|
pointerType: PointerType | null;
|
|
|
|
}) => {
|
|
|
|
if (!appState.penDetected && pointerType === "pen") {
|
|
|
|
setAppState({
|
|
|
|
penDetected: true,
|
|
|
|
penMode: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setAppState({
|
|
|
|
elementType,
|
|
|
|
multiElement: null,
|
|
|
|
selectedElementIds: {},
|
|
|
|
});
|
|
|
|
setCursorForShape(canvas, { ...appState, elementType });
|
|
|
|
if (elementType === "image") {
|
|
|
|
onImageAction({ pointerType });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{SHAPES.map(({ value, icon, key }, index) => {
|
|
|
|
const label = t(`toolBar.${value}`);
|
|
|
|
const letter = key && (typeof key === "string" ? key : key[0]);
|
|
|
|
const shortcut = letter
|
|
|
|
? `${capitalizeString(letter)} ${t("helpDialog.or")} ${index + 1}`
|
|
|
|
: `${index + 1}`;
|
|
|
|
return (
|
|
|
|
<ToolButton
|
|
|
|
className="Shape"
|
|
|
|
key={value}
|
|
|
|
type="radio"
|
|
|
|
icon={icon}
|
|
|
|
checked={elementType === value}
|
|
|
|
name="editor-current-shape"
|
|
|
|
title={`${capitalizeString(label)} — ${shortcut}`}
|
|
|
|
keyBindingLabel={`${index + 1}`}
|
|
|
|
aria-label={capitalizeString(label)}
|
|
|
|
aria-keyshortcuts={shortcut}
|
|
|
|
data-testid={value}
|
|
|
|
onPointerDown={({ pointerType }) => {
|
|
|
|
onChange({ elementType: value, pointerType });
|
|
|
|
}}
|
|
|
|
onChange={({ pointerType }) => {
|
|
|
|
onChange({ elementType: value, 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
|
|
|
}) => (
|
|
|
|
<Stack.Col gap={1}>
|
|
|
|
<Stack.Row gap={1} align="center">
|
|
|
|
{renderAction("zoomOut")}
|
2021-07-16 23:47:43 +02:00
|
|
|
{renderAction("zoomIn")}
|
2020-05-20 16:21:37 +03:00
|
|
|
{renderAction("resetZoom")}
|
|
|
|
</Stack.Row>
|
|
|
|
</Stack.Col>
|
|
|
|
);
|