types.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import type React from "react";
  2. import type {
  3. ExcalidrawElement,
  4. OrderedExcalidrawElement,
  5. } from "../element/types";
  6. import type {
  7. AppClassProperties,
  8. AppState,
  9. ExcalidrawProps,
  10. BinaryFiles,
  11. UIAppState,
  12. } from "../types";
  13. import type { MarkOptional } from "../utility-types";
  14. import type { StoreActionType } from "../store";
  15. export type ActionSource =
  16. | "ui"
  17. | "keyboard"
  18. | "contextMenu"
  19. | "api"
  20. | "commandPalette";
  21. /** if false, the action should be prevented */
  22. export type ActionResult =
  23. | {
  24. elements?: readonly ExcalidrawElement[] | null;
  25. appState?: MarkOptional<
  26. AppState,
  27. "offsetTop" | "offsetLeft" | "width" | "height"
  28. > | null;
  29. files?: BinaryFiles | null;
  30. storeAction: StoreActionType;
  31. replaceFiles?: boolean;
  32. }
  33. | false;
  34. type ActionFn = (
  35. elements: readonly OrderedExcalidrawElement[],
  36. appState: Readonly<AppState>,
  37. formData: any,
  38. app: AppClassProperties,
  39. ) => ActionResult | Promise<ActionResult>;
  40. export type UpdaterFn = (res: ActionResult) => void;
  41. export type ActionFilterFn = (action: Action) => void;
  42. export type ActionName =
  43. | "copy"
  44. | "cut"
  45. | "paste"
  46. | "copyAsPng"
  47. | "copyAsSvg"
  48. | "copyText"
  49. | "sendBackward"
  50. | "bringForward"
  51. | "sendToBack"
  52. | "bringToFront"
  53. | "copyStyles"
  54. | "selectAll"
  55. | "pasteStyles"
  56. | "gridMode"
  57. | "zenMode"
  58. | "objectsSnapMode"
  59. | "stats"
  60. | "changeStrokeColor"
  61. | "changeBackgroundColor"
  62. | "changeFillStyle"
  63. | "changeStrokeWidth"
  64. | "changeStrokeShape"
  65. | "changeSloppiness"
  66. | "changeStrokeStyle"
  67. | "changeArrowhead"
  68. | "changeOpacity"
  69. | "changeFontSize"
  70. | "toggleCanvasMenu"
  71. | "toggleEditMenu"
  72. | "undo"
  73. | "redo"
  74. | "finalize"
  75. | "changeProjectName"
  76. | "changeExportBackground"
  77. | "changeExportEmbedScene"
  78. | "changeExportScale"
  79. | "saveToActiveFile"
  80. | "saveFileToDisk"
  81. | "loadScene"
  82. | "duplicateSelection"
  83. | "deleteSelectedElements"
  84. | "changeViewBackgroundColor"
  85. | "clearCanvas"
  86. | "zoomIn"
  87. | "zoomOut"
  88. | "resetZoom"
  89. | "zoomToFit"
  90. | "zoomToFitSelection"
  91. | "zoomToFitSelectionInViewport"
  92. | "changeFontFamily"
  93. | "changeTextAlign"
  94. | "changeVerticalAlign"
  95. | "toggleFullScreen"
  96. | "toggleShortcuts"
  97. | "group"
  98. | "ungroup"
  99. | "goToCollaborator"
  100. | "addToLibrary"
  101. | "changeRoundness"
  102. | "alignTop"
  103. | "alignBottom"
  104. | "alignLeft"
  105. | "alignRight"
  106. | "alignVerticallyCentered"
  107. | "alignHorizontallyCentered"
  108. | "distributeHorizontally"
  109. | "distributeVertically"
  110. | "flipHorizontal"
  111. | "flipVertical"
  112. | "viewMode"
  113. | "exportWithDarkMode"
  114. | "toggleTheme"
  115. | "increaseFontSize"
  116. | "decreaseFontSize"
  117. | "unbindText"
  118. | "hyperlink"
  119. | "bindText"
  120. | "unlockAllElements"
  121. | "toggleElementLock"
  122. | "toggleLinearEditor"
  123. | "toggleEraserTool"
  124. | "toggleHandTool"
  125. | "selectAllElementsInFrame"
  126. | "removeAllElementsFromFrame"
  127. | "updateFrameRendering"
  128. | "setFrameAsActiveTool"
  129. | "setEmbeddableAsActiveTool"
  130. | "createContainerFromText"
  131. | "wrapTextInContainer"
  132. | "commandPalette";
  133. export type PanelComponentProps = {
  134. elements: readonly ExcalidrawElement[];
  135. appState: AppState;
  136. updateData: <T = any>(formData?: T) => void;
  137. appProps: ExcalidrawProps;
  138. data?: Record<string, any>;
  139. app: AppClassProperties;
  140. };
  141. export interface Action {
  142. name: ActionName;
  143. label:
  144. | string
  145. | ((
  146. elements: readonly ExcalidrawElement[],
  147. appState: Readonly<AppState>,
  148. app: AppClassProperties,
  149. ) => string);
  150. keywords?: string[];
  151. icon?:
  152. | React.ReactNode
  153. | ((
  154. appState: UIAppState,
  155. elements: readonly ExcalidrawElement[],
  156. ) => React.ReactNode);
  157. PanelComponent?: React.FC<PanelComponentProps>;
  158. perform: ActionFn;
  159. keyPriority?: number;
  160. keyTest?: (
  161. event: React.KeyboardEvent | KeyboardEvent,
  162. appState: AppState,
  163. elements: readonly ExcalidrawElement[],
  164. app: AppClassProperties,
  165. ) => boolean;
  166. predicate?: (
  167. elements: readonly ExcalidrawElement[],
  168. appState: AppState,
  169. appProps: ExcalidrawProps,
  170. app: AppClassProperties,
  171. ) => boolean;
  172. checked?: (appState: Readonly<AppState>) => boolean;
  173. trackEvent:
  174. | false
  175. | {
  176. category:
  177. | "toolbar"
  178. | "element"
  179. | "canvas"
  180. | "export"
  181. | "history"
  182. | "menu"
  183. | "collab"
  184. | "hyperlink";
  185. action?: string;
  186. predicate?: (
  187. appState: Readonly<AppState>,
  188. elements: readonly ExcalidrawElement[],
  189. value: any,
  190. ) => boolean;
  191. };
  192. /** if set to `true`, allow action to be performed in viewMode.
  193. * Defaults to `false` */
  194. viewMode?: boolean;
  195. }