types.ts 2.6 KB

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