types.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import {
  2. PointerType,
  3. ExcalidrawLinearElement,
  4. NonDeletedExcalidrawElement,
  5. NonDeleted,
  6. TextAlign,
  7. ExcalidrawElement,
  8. FontFamily,
  9. GroupId,
  10. ExcalidrawBindableElement,
  11. Arrowhead,
  12. ChartType,
  13. } from "./element/types";
  14. import { SHAPES } from "./shapes";
  15. import { Point as RoughPoint } from "roughjs/bin/geometry";
  16. import { LinearElementEditor } from "./element/linearElementEditor";
  17. import { SuggestedBinding } from "./element/binding";
  18. import { ImportedDataState } from "./data/types";
  19. import { ExcalidrawImperativeAPI } from "./components/App";
  20. import type { ResolvablePromise } from "./utils";
  21. import { Spreadsheet } from "./charts";
  22. import { Language } from "./i18n";
  23. export type Point = Readonly<RoughPoint>;
  24. export type Collaborator = {
  25. pointer?: {
  26. x: number;
  27. y: number;
  28. };
  29. button?: "up" | "down";
  30. selectedElementIds?: AppState["selectedElementIds"];
  31. username?: string | null;
  32. userState?: UserIdleState;
  33. color?: {
  34. background: string;
  35. stroke: string;
  36. };
  37. };
  38. export type AppState = {
  39. isLoading: boolean;
  40. errorMessage: string | null;
  41. draggingElement: NonDeletedExcalidrawElement | null;
  42. resizingElement: NonDeletedExcalidrawElement | null;
  43. multiElement: NonDeleted<ExcalidrawLinearElement> | null;
  44. selectionElement: NonDeletedExcalidrawElement | null;
  45. isBindingEnabled: boolean;
  46. startBoundElement: NonDeleted<ExcalidrawBindableElement> | null;
  47. suggestedBindings: SuggestedBinding[];
  48. // element being edited, but not necessarily added to elements array yet
  49. // (e.g. text element when typing into the input)
  50. editingElement: NonDeletedExcalidrawElement | null;
  51. editingLinearElement: LinearElementEditor | null;
  52. elementType: typeof SHAPES[number]["value"];
  53. elementLocked: boolean;
  54. exportBackground: boolean;
  55. exportEmbedScene: boolean;
  56. exportWithDarkMode: boolean;
  57. shouldAddWatermark: boolean;
  58. currentItemStrokeColor: string;
  59. currentItemBackgroundColor: string;
  60. currentItemFillStyle: ExcalidrawElement["fillStyle"];
  61. currentItemStrokeWidth: number;
  62. currentItemStrokeStyle: ExcalidrawElement["strokeStyle"];
  63. currentItemRoughness: number;
  64. currentItemOpacity: number;
  65. currentItemFontFamily: FontFamily;
  66. currentItemFontSize: number;
  67. currentItemTextAlign: TextAlign;
  68. currentItemStrokeSharpness: ExcalidrawElement["strokeSharpness"];
  69. currentItemStartArrowhead: Arrowhead | null;
  70. currentItemEndArrowhead: Arrowhead | null;
  71. currentItemLinearStrokeSharpness: ExcalidrawElement["strokeSharpness"];
  72. viewBackgroundColor: string;
  73. scrollX: number;
  74. scrollY: number;
  75. cursorButton: "up" | "down";
  76. scrolledOutside: boolean;
  77. name: string;
  78. isResizing: boolean;
  79. isRotating: boolean;
  80. zoom: Zoom;
  81. openMenu: "canvas" | "shape" | null;
  82. lastPointerDownWith: PointerType;
  83. selectedElementIds: { [id: string]: boolean };
  84. previousSelectedElementIds: { [id: string]: boolean };
  85. shouldCacheIgnoreZoom: boolean;
  86. showHelpDialog: boolean;
  87. toastMessage: string | null;
  88. zenModeEnabled: boolean;
  89. theme: "light" | "dark";
  90. gridSize: number | null;
  91. viewModeEnabled: boolean;
  92. /** top-most selected groups (i.e. does not include nested groups) */
  93. selectedGroupIds: { [groupId: string]: boolean };
  94. /** group being edited when you drill down to its constituent element
  95. (e.g. when you double-click on a group's element) */
  96. editingGroupId: GroupId | null;
  97. width: number;
  98. height: number;
  99. offsetTop: number;
  100. offsetLeft: number;
  101. isLibraryOpen: boolean;
  102. fileHandle: import("browser-fs-access").FileSystemHandle | null;
  103. collaborators: Map<string, Collaborator>;
  104. showStats: boolean;
  105. currentChartType: ChartType;
  106. pasteDialog:
  107. | {
  108. shown: false;
  109. data: null;
  110. }
  111. | {
  112. shown: true;
  113. data: Spreadsheet;
  114. };
  115. };
  116. export type NormalizedZoomValue = number & { _brand: "normalizedZoom" };
  117. export type Zoom = Readonly<{
  118. value: NormalizedZoomValue;
  119. translation: Readonly<{
  120. x: number;
  121. y: number;
  122. }>;
  123. }>;
  124. export type PointerCoords = Readonly<{
  125. x: number;
  126. y: number;
  127. }>;
  128. export type Gesture = {
  129. pointers: Map<number, PointerCoords>;
  130. lastCenter: { x: number; y: number } | null;
  131. initialDistance: number | null;
  132. initialScale: number | null;
  133. };
  134. export declare class GestureEvent extends UIEvent {
  135. readonly rotation: number;
  136. readonly scale: number;
  137. }
  138. export type LibraryItem = readonly NonDeleted<ExcalidrawElement>[];
  139. export type LibraryItems = readonly LibraryItem[];
  140. // NOTE ready/readyPromise props are optional for host apps' sake (our own
  141. // implem guarantees existence)
  142. export type ExcalidrawAPIRefValue =
  143. | ExcalidrawImperativeAPI
  144. | {
  145. readyPromise?: ResolvablePromise<ExcalidrawImperativeAPI>;
  146. ready?: false;
  147. };
  148. export interface ExcalidrawProps {
  149. width?: number;
  150. height?: number;
  151. onChange?: (
  152. elements: readonly ExcalidrawElement[],
  153. appState: AppState,
  154. ) => void;
  155. initialData?: ImportedDataState | null | Promise<ImportedDataState | null>;
  156. excalidrawRef?: ForwardRef<ExcalidrawAPIRefValue>;
  157. onCollabButtonClick?: () => void;
  158. isCollaborating?: boolean;
  159. onPointerUpdate?: (payload: {
  160. pointer: { x: number; y: number };
  161. button: "down" | "up";
  162. pointersMap: Gesture["pointers"];
  163. }) => void;
  164. onExportToBackend?: (
  165. exportedElements: readonly NonDeletedExcalidrawElement[],
  166. appState: AppState,
  167. canvas: HTMLCanvasElement | null,
  168. ) => void;
  169. renderFooter?: (isMobile: boolean) => JSX.Element;
  170. langCode?: Language["code"];
  171. viewModeEnabled?: boolean;
  172. zenModeEnabled?: boolean;
  173. gridModeEnabled?: boolean;
  174. libraryReturnUrl?: string;
  175. theme?: "dark" | "light";
  176. name?: string;
  177. }
  178. export type SceneData = {
  179. elements?: ImportedDataState["elements"];
  180. appState?: ImportedDataState["appState"];
  181. collaborators?: Map<string, Collaborator>;
  182. commitToHistory?: boolean;
  183. };
  184. export enum UserIdleState {
  185. ACTIVE = "active",
  186. AWAY = "away",
  187. IDLE = "idle",
  188. }