actionLinearEditor.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { getNonDeletedElements } from "../element";
  2. import { LinearElementEditor } from "../element/linearElementEditor";
  3. import { isLinearElement } from "../element/typeChecks";
  4. import { ExcalidrawLinearElement } from "../element/types";
  5. import { getSelectedElements } from "../scene";
  6. import { register } from "./register";
  7. export const actionToggleLinearEditor = register({
  8. name: "toggleLinearEditor",
  9. trackEvent: {
  10. category: "element",
  11. },
  12. contextItemPredicate: (elements, appState) => {
  13. const selectedElements = getSelectedElements(elements, appState);
  14. if (selectedElements.length === 1 && isLinearElement(selectedElements[0])) {
  15. return true;
  16. }
  17. return false;
  18. },
  19. perform(elements, appState, _, app) {
  20. const selectedElement = getSelectedElements(
  21. getNonDeletedElements(elements),
  22. appState,
  23. true,
  24. )[0] as ExcalidrawLinearElement;
  25. const editingLinearElement =
  26. appState.editingLinearElement?.elementId === selectedElement.id
  27. ? null
  28. : new LinearElementEditor(selectedElement, app.scene);
  29. return {
  30. appState: {
  31. ...appState,
  32. editingLinearElement,
  33. },
  34. commitToHistory: false,
  35. };
  36. },
  37. contextItemLabel: (elements, appState) => {
  38. const selectedElement = getSelectedElements(
  39. getNonDeletedElements(elements),
  40. appState,
  41. true,
  42. )[0] as ExcalidrawLinearElement;
  43. return appState.editingLinearElement?.elementId === selectedElement.id
  44. ? "labels.lineEditor.exit"
  45. : "labels.lineEditor.edit";
  46. },
  47. });