types.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import React from "react";
  2. import { ExcalidrawElement } from "../element/types";
  3. import { AppState, ExcalidrawProps } from "../types";
  4. import Library from "../data/library";
  5. /** if false, the action should be prevented */
  6. export type ActionResult =
  7. | {
  8. elements?: readonly ExcalidrawElement[] | null;
  9. appState?: MarkOptional<
  10. AppState,
  11. "offsetTop" | "offsetLeft" | "width" | "height"
  12. > | null;
  13. commitToHistory: boolean;
  14. syncHistory?: boolean;
  15. }
  16. | false;
  17. type AppAPI = {
  18. canvas: HTMLCanvasElement | null;
  19. focusContainer(): void;
  20. library: Library;
  21. };
  22. type ActionFn = (
  23. elements: readonly ExcalidrawElement[],
  24. appState: Readonly<AppState>,
  25. formData: any,
  26. app: AppAPI,
  27. ) => ActionResult | Promise<ActionResult>;
  28. export type UpdaterFn = (res: ActionResult) => void;
  29. export type ActionFilterFn = (action: Action) => void;
  30. export type ActionName =
  31. | "copy"
  32. | "cut"
  33. | "paste"
  34. | "copyAsPng"
  35. | "copyAsSvg"
  36. | "sendBackward"
  37. | "bringForward"
  38. | "sendToBack"
  39. | "bringToFront"
  40. | "copyStyles"
  41. | "selectAll"
  42. | "pasteStyles"
  43. | "gridMode"
  44. | "zenMode"
  45. | "stats"
  46. | "changeStrokeColor"
  47. | "changeBackgroundColor"
  48. | "changeFillStyle"
  49. | "changeStrokeWidth"
  50. | "changeSloppiness"
  51. | "changeStrokeStyle"
  52. | "changeArrowhead"
  53. | "changeOpacity"
  54. | "changeFontSize"
  55. | "toggleCanvasMenu"
  56. | "toggleEditMenu"
  57. | "undo"
  58. | "redo"
  59. | "finalize"
  60. | "changeProjectName"
  61. | "changeExportBackground"
  62. | "changeExportEmbedScene"
  63. | "changeShouldAddWatermark"
  64. | "saveScene"
  65. | "saveAsScene"
  66. | "loadScene"
  67. | "duplicateSelection"
  68. | "deleteSelectedElements"
  69. | "changeViewBackgroundColor"
  70. | "clearCanvas"
  71. | "zoomIn"
  72. | "zoomOut"
  73. | "resetZoom"
  74. | "zoomToFit"
  75. | "zoomToSelection"
  76. | "changeFontFamily"
  77. | "changeTextAlign"
  78. | "toggleFullScreen"
  79. | "toggleShortcuts"
  80. | "group"
  81. | "ungroup"
  82. | "goToCollaborator"
  83. | "addToLibrary"
  84. | "changeSharpness"
  85. | "alignTop"
  86. | "alignBottom"
  87. | "alignLeft"
  88. | "alignRight"
  89. | "alignVerticallyCentered"
  90. | "alignHorizontallyCentered"
  91. | "distributeHorizontally"
  92. | "distributeVertically"
  93. | "flipHorizontal"
  94. | "flipVertical"
  95. | "viewMode"
  96. | "exportWithDarkMode";
  97. export interface Action {
  98. name: ActionName;
  99. PanelComponent?: React.FC<{
  100. elements: readonly ExcalidrawElement[];
  101. appState: AppState;
  102. updateData: (formData?: any) => void;
  103. appProps: ExcalidrawProps;
  104. id?: string;
  105. }>;
  106. perform: ActionFn;
  107. keyPriority?: number;
  108. keyTest?: (
  109. event: React.KeyboardEvent | KeyboardEvent,
  110. appState: AppState,
  111. elements: readonly ExcalidrawElement[],
  112. ) => boolean;
  113. contextItemLabel?: string;
  114. contextItemPredicate?: (
  115. elements: readonly ExcalidrawElement[],
  116. appState: AppState,
  117. ) => boolean;
  118. checked?: (appState: Readonly<AppState>) => boolean;
  119. }
  120. export interface ActionsManagerInterface {
  121. actions: Record<ActionName, Action>;
  122. registerAction: (action: Action) => void;
  123. handleKeyDown: (event: React.KeyboardEvent | KeyboardEvent) => boolean;
  124. renderAction: (name: ActionName) => React.ReactElement | null;
  125. }