types.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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?: AppState | null;
  9. commitToHistory: boolean;
  10. syncHistory?: boolean;
  11. }
  12. | false;
  13. type ActionFn = (
  14. elements: readonly ExcalidrawElement[],
  15. appState: AppState,
  16. formData: any,
  17. ) => ActionResult;
  18. export type UpdaterFn = (res: ActionResult, commitToHistory?: boolean) => 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. | "changeShouldAddWatermark"
  44. | "saveScene"
  45. | "saveAsScene"
  46. | "loadScene"
  47. | "duplicateSelection"
  48. | "deleteSelectedElements"
  49. | "changeViewBackgroundColor"
  50. | "clearCanvas"
  51. | "zoomIn"
  52. | "zoomOut"
  53. | "resetZoom"
  54. | "zoomToFit"
  55. | "changeFontFamily"
  56. | "changeTextAlign"
  57. | "toggleFullScreen"
  58. | "toggleShortcuts"
  59. | "group"
  60. | "ungroup"
  61. | "goToCollaborator"
  62. | "addToLibrary";
  63. export interface Action {
  64. name: ActionName;
  65. PanelComponent?: React.FC<{
  66. elements: readonly ExcalidrawElement[];
  67. appState: AppState;
  68. updateData: (formData?: any) => void;
  69. id?: string;
  70. }>;
  71. perform: ActionFn;
  72. keyPriority?: number;
  73. keyTest?: (
  74. event: KeyboardEvent,
  75. appState: AppState,
  76. elements: readonly ExcalidrawElement[],
  77. ) => boolean;
  78. contextItemLabel?: string;
  79. contextMenuOrder?: number;
  80. contextItemPredicate?: (
  81. elements: readonly ExcalidrawElement[],
  82. appState: AppState,
  83. ) => boolean;
  84. }
  85. export interface ActionsManagerInterface {
  86. actions: {
  87. [actionName in ActionName]: Action;
  88. };
  89. registerAction: (action: Action) => void;
  90. handleKeyDown: (event: KeyboardEvent) => boolean;
  91. getContextMenuItems: (
  92. actionFilter: ActionFilterFn,
  93. ) => { label: string; action: () => void }[];
  94. renderAction: (name: ActionName) => React.ReactElement | null;
  95. }