types.ts 3.3 KB

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