HintViewer.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { t } from "../i18n";
  2. import { NonDeletedExcalidrawElement } from "../element/types";
  3. import { getSelectedElements } from "../scene";
  4. import "./HintViewer.scss";
  5. import { AppState } from "../types";
  6. import { isLinearElement, isTextElement } from "../element/typeChecks";
  7. import { getShortcutKey } from "../utils";
  8. interface Hint {
  9. appState: AppState;
  10. elements: readonly NonDeletedExcalidrawElement[];
  11. }
  12. const getHints = ({ appState, elements }: Hint) => {
  13. const { elementType, isResizing, isRotating, lastPointerDownWith } = appState;
  14. const multiMode = appState.multiElement !== null;
  15. if (elementType === "arrow" || elementType === "line") {
  16. if (!multiMode) {
  17. return t("hints.linearElement");
  18. }
  19. return t("hints.linearElementMulti");
  20. }
  21. if (elementType === "freedraw") {
  22. return t("hints.freeDraw");
  23. }
  24. if (elementType === "text") {
  25. return t("hints.text");
  26. }
  27. const selectedElements = getSelectedElements(elements, appState);
  28. if (
  29. isResizing &&
  30. lastPointerDownWith === "mouse" &&
  31. selectedElements.length === 1
  32. ) {
  33. const targetElement = selectedElements[0];
  34. if (isLinearElement(targetElement) && targetElement.points.length === 2) {
  35. return t("hints.lockAngle");
  36. }
  37. return t("hints.resize");
  38. }
  39. if (isRotating && lastPointerDownWith === "mouse") {
  40. return t("hints.rotate");
  41. }
  42. if (selectedElements.length === 1 && isLinearElement(selectedElements[0])) {
  43. if (appState.editingLinearElement) {
  44. return appState.editingLinearElement.activePointIndex
  45. ? t("hints.lineEditor_pointSelected")
  46. : t("hints.lineEditor_nothingSelected");
  47. }
  48. return t("hints.lineEditor_info");
  49. }
  50. if (selectedElements.length === 1 && isTextElement(selectedElements[0])) {
  51. return t("hints.text_selected");
  52. }
  53. if (appState.editingElement && isTextElement(appState.editingElement)) {
  54. return t("hints.text_editing");
  55. }
  56. return null;
  57. };
  58. export const HintViewer = ({ appState, elements }: Hint) => {
  59. let hint = getHints({
  60. appState,
  61. elements,
  62. });
  63. if (!hint) {
  64. return null;
  65. }
  66. hint = getShortcutKey(hint);
  67. return (
  68. <div className="HintViewer">
  69. <span>{hint}</span>
  70. </div>
  71. );
  72. };