actionFinalize.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { KEYS } from "../keys";
  2. import { isInvisiblySmallElement } from "../element";
  3. import { resetCursor } from "../utils";
  4. import React from "react";
  5. import { ToolButton } from "../components/ToolButton";
  6. import { done } from "../components/icons";
  7. import { t } from "../i18n";
  8. import { register } from "./register";
  9. import { mutateElement } from "../element/mutateElement";
  10. export const actionFinalize = register({
  11. name: "finalize",
  12. perform: (elements, appState) => {
  13. let newElements = elements;
  14. if (window.document.activeElement instanceof HTMLElement) {
  15. window.document.activeElement.blur();
  16. }
  17. if (appState.multiElement) {
  18. // pen and mouse have hover
  19. if (appState.lastPointerDownWith !== "touch") {
  20. const { points, lastCommittedPoint } = appState.multiElement;
  21. if (
  22. !lastCommittedPoint ||
  23. points[points.length - 1] !== lastCommittedPoint
  24. ) {
  25. mutateElement(appState.multiElement, {
  26. points: appState.multiElement.points.slice(0, -1),
  27. });
  28. }
  29. }
  30. if (isInvisiblySmallElement(appState.multiElement)) {
  31. newElements = newElements.slice(0, -1);
  32. }
  33. if (!appState.elementLocked) {
  34. appState.selectedElementIds[appState.multiElement.id] = true;
  35. }
  36. }
  37. if (!appState.elementLocked || !appState.multiElement) {
  38. resetCursor();
  39. }
  40. return {
  41. elements: newElements,
  42. appState: {
  43. ...appState,
  44. elementType:
  45. appState.elementLocked && appState.multiElement
  46. ? appState.elementType
  47. : "selection",
  48. draggingElement: null,
  49. multiElement: null,
  50. editingElement: null,
  51. selectedElementIds: {},
  52. },
  53. commitToHistory: false,
  54. };
  55. },
  56. keyTest: (event, appState) =>
  57. (event.key === KEYS.ESCAPE &&
  58. !appState.draggingElement &&
  59. appState.multiElement === null) ||
  60. ((event.key === KEYS.ESCAPE || event.key === KEYS.ENTER) &&
  61. appState.multiElement !== null),
  62. PanelComponent: ({ appState, updateData }) => (
  63. <ToolButton
  64. type="button"
  65. icon={done}
  66. title={t("buttons.done")}
  67. aria-label={t("buttons.done")}
  68. onClick={updateData}
  69. visible={appState.multiElement != null}
  70. />
  71. ),
  72. });