2021-03-29 20:06:34 +05:30
|
|
|
import React from "react";
|
2020-12-07 18:35:16 +02:00
|
|
|
import { getCommonBounds } from "../element/bounds";
|
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
|
|
|
import { t } from "../i18n";
|
2021-03-29 17:09:20 +03:00
|
|
|
import { useIsMobile } from "../is-mobile";
|
2020-12-07 18:35:16 +02:00
|
|
|
import { getTargetElements } from "../scene";
|
2021-03-29 20:06:34 +05:30
|
|
|
import { AppState, ExcalidrawProps } from "../types";
|
2020-12-07 18:35:16 +02:00
|
|
|
import { close } from "./icons";
|
|
|
|
import { Island } from "./Island";
|
|
|
|
import "./Stats.scss";
|
|
|
|
|
|
|
|
export const Stats = (props: {
|
|
|
|
appState: AppState;
|
2021-02-04 14:47:46 +02:00
|
|
|
setAppState: React.Component<any, AppState>["setState"];
|
2020-12-07 18:35:16 +02:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[];
|
|
|
|
onClose: () => void;
|
2021-03-29 20:06:34 +05:30
|
|
|
renderCustomStats: ExcalidrawProps["renderCustomStats"];
|
2020-12-07 18:35:16 +02:00
|
|
|
}) => {
|
2020-12-11 22:12:36 +01:00
|
|
|
const isMobile = useIsMobile();
|
2020-12-07 18:35:16 +02:00
|
|
|
|
|
|
|
const boundingBox = getCommonBounds(props.elements);
|
|
|
|
const selectedElements = getTargetElements(props.elements, props.appState);
|
|
|
|
const selectedBoundingBox = getCommonBounds(selectedElements);
|
|
|
|
|
2020-12-11 22:12:36 +01:00
|
|
|
if (isMobile && props.appState.openMenu) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-07 18:35:16 +02:00
|
|
|
return (
|
|
|
|
<div className="Stats">
|
|
|
|
<Island padding={2}>
|
|
|
|
<div className="close" onClick={props.onClose}>
|
|
|
|
{close}
|
|
|
|
</div>
|
|
|
|
<h3>{t("stats.title")}</h3>
|
|
|
|
<table>
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<th colSpan={2}>{t("stats.scene")}</th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{t("stats.elements")}</td>
|
|
|
|
<td>{props.elements.length}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{t("stats.width")}</td>
|
|
|
|
<td>{Math.round(boundingBox[2]) - Math.round(boundingBox[0])}</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{t("stats.height")}</td>
|
|
|
|
<td>{Math.round(boundingBox[3]) - Math.round(boundingBox[1])}</td>
|
|
|
|
</tr>
|
2021-03-29 20:06:34 +05:30
|
|
|
|
2020-12-07 18:35:16 +02:00
|
|
|
{selectedElements.length === 1 && (
|
|
|
|
<tr>
|
|
|
|
<th colSpan={2}>{t("stats.element")}</th>
|
|
|
|
</tr>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{selectedElements.length > 1 && (
|
|
|
|
<>
|
|
|
|
<tr>
|
|
|
|
<th colSpan={2}>{t("stats.selected")}</th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{t("stats.elements")}</td>
|
|
|
|
<td>{selectedElements.length}</td>
|
|
|
|
</tr>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{selectedElements.length > 0 && (
|
|
|
|
<>
|
|
|
|
<tr>
|
|
|
|
<td>{"x"}</td>
|
2021-03-26 21:56:27 +01:00
|
|
|
<td>{Math.round(selectedBoundingBox[0])}</td>
|
2020-12-07 18:35:16 +02:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{"y"}</td>
|
2021-03-26 21:56:27 +01:00
|
|
|
<td>{Math.round(selectedBoundingBox[1])}</td>
|
2020-12-07 18:35:16 +02:00
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{t("stats.width")}</td>
|
|
|
|
<td>
|
|
|
|
{Math.round(
|
2021-03-26 21:56:27 +01:00
|
|
|
selectedBoundingBox[2] - selectedBoundingBox[0],
|
2020-12-07 18:35:16 +02:00
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
<td>{t("stats.height")}</td>
|
|
|
|
<td>
|
|
|
|
{Math.round(
|
2021-03-26 21:56:27 +01:00
|
|
|
selectedBoundingBox[3] - selectedBoundingBox[1],
|
2020-12-07 18:35:16 +02:00
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{selectedElements.length === 1 && (
|
|
|
|
<tr>
|
|
|
|
<td>{t("stats.angle")}</td>
|
|
|
|
<td>
|
|
|
|
{`${Math.round(
|
|
|
|
(selectedElements[0].angle * 180) / Math.PI,
|
|
|
|
)}°`}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
)}
|
2021-03-29 20:06:34 +05:30
|
|
|
{props.renderCustomStats?.(props.elements, props.appState)}
|
2020-12-07 18:35:16 +02:00
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</Island>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|