2020-02-03 21:52:21 +04:00
|
|
|
import React from "react";
|
|
|
|
import { t } from "../i18n";
|
2020-04-08 09:49:52 -07:00
|
|
|
import { NonDeletedExcalidrawElement } from "../element/types";
|
2020-02-16 22:54:50 +01:00
|
|
|
import { getSelectedElements } from "../scene";
|
2020-02-03 21:52:21 +04:00
|
|
|
|
2020-03-13 15:32:47 -04:00
|
|
|
import "./HintViewer.scss";
|
2020-03-08 10:20:55 -07:00
|
|
|
import { AppState } from "../types";
|
2020-03-17 20:55:40 +01:00
|
|
|
import { isLinearElement } from "../element/typeChecks";
|
2020-06-01 11:35:44 +02:00
|
|
|
import { getShortcutKey } from "../utils";
|
2020-02-03 21:52:21 +04:00
|
|
|
|
|
|
|
interface Hint {
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState;
|
2020-04-08 09:49:52 -07:00
|
|
|
elements: readonly NonDeletedExcalidrawElement[];
|
2020-02-03 21:52:21 +04:00
|
|
|
}
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
const getHints = ({ appState, elements }: Hint) => {
|
2020-04-02 17:40:26 +09:00
|
|
|
const { elementType, isResizing, isRotating, lastPointerDownWith } = appState;
|
2020-03-08 10:20:55 -07:00
|
|
|
const multiMode = appState.multiElement !== null;
|
2020-02-03 21:52:21 +04:00
|
|
|
if (elementType === "arrow" || elementType === "line") {
|
|
|
|
if (!multiMode) {
|
|
|
|
return t("hints.linearElement");
|
|
|
|
}
|
|
|
|
return t("hints.linearElementMulti");
|
|
|
|
}
|
|
|
|
|
2020-05-12 20:10:11 +01:00
|
|
|
if (elementType === "draw") {
|
|
|
|
return t("hints.freeDraw");
|
|
|
|
}
|
|
|
|
|
2020-08-20 22:55:44 +03:00
|
|
|
if (elementType === "text") {
|
|
|
|
return t("hints.text");
|
|
|
|
}
|
|
|
|
|
2020-04-07 17:49:59 +09:00
|
|
|
const selectedElements = getSelectedElements(elements, appState);
|
|
|
|
if (
|
|
|
|
isResizing &&
|
|
|
|
lastPointerDownWith === "mouse" &&
|
|
|
|
selectedElements.length === 1
|
|
|
|
) {
|
2020-03-17 20:55:40 +01:00
|
|
|
const targetElement = selectedElements[0];
|
2020-12-22 11:00:51 +01:00
|
|
|
if (isLinearElement(targetElement) && targetElement.points.length === 2) {
|
|
|
|
return t("hints.lockAngle");
|
2020-02-03 21:52:21 +04:00
|
|
|
}
|
|
|
|
return t("hints.resize");
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:40:26 +09:00
|
|
|
if (isRotating && lastPointerDownWith === "mouse") {
|
|
|
|
return t("hints.rotate");
|
|
|
|
}
|
|
|
|
|
2020-06-01 11:35:44 +02:00
|
|
|
if (selectedElements.length === 1 && isLinearElement(selectedElements[0])) {
|
|
|
|
if (appState.editingLinearElement) {
|
|
|
|
return appState.editingLinearElement.activePointIndex
|
|
|
|
? t("hints.lineEditor_pointSelected")
|
|
|
|
: t("hints.lineEditor_nothingSelected");
|
|
|
|
}
|
|
|
|
return t("hints.lineEditor_info");
|
|
|
|
}
|
|
|
|
|
2020-02-03 21:52:21 +04:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
export const HintViewer = ({ appState, elements }: Hint) => {
|
2020-06-01 11:35:44 +02:00
|
|
|
let hint = getHints({
|
2020-03-08 10:20:55 -07:00
|
|
|
appState,
|
2020-02-03 21:52:21 +04:00
|
|
|
elements,
|
|
|
|
});
|
|
|
|
if (!hint) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-01 11:35:44 +02:00
|
|
|
hint = getShortcutKey(hint);
|
|
|
|
|
2020-03-01 14:39:03 -05:00
|
|
|
return (
|
|
|
|
<div className="HintViewer">
|
|
|
|
<span>{hint}</span>
|
|
|
|
</div>
|
|
|
|
);
|
2020-02-03 21:52:21 +04:00
|
|
|
};
|