types.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { Point } from "../types";
  2. import { FONT_FAMILY, THEME, VERTICAL_ALIGN } from "../constants";
  3. export type ChartType = "bar" | "line";
  4. export type FillStyle = "hachure" | "cross-hatch" | "solid";
  5. export type FontFamilyKeys = keyof typeof FONT_FAMILY;
  6. export type FontFamilyValues = typeof FONT_FAMILY[FontFamilyKeys];
  7. export type Theme = typeof THEME[keyof typeof THEME];
  8. export type FontString = string & { _brand: "fontString" };
  9. export type GroupId = string;
  10. export type PointerType = "mouse" | "pen" | "touch";
  11. export type StrokeSharpness = "round" | "sharp";
  12. export type StrokeStyle = "solid" | "dashed" | "dotted";
  13. export type TextAlign = "left" | "center" | "right";
  14. type VerticalAlignKeys = keyof typeof VERTICAL_ALIGN;
  15. export type VerticalAlign = typeof VERTICAL_ALIGN[VerticalAlignKeys];
  16. type _ExcalidrawElementBase = Readonly<{
  17. id: string;
  18. x: number;
  19. y: number;
  20. strokeColor: string;
  21. backgroundColor: string;
  22. fillStyle: FillStyle;
  23. strokeWidth: number;
  24. strokeStyle: StrokeStyle;
  25. strokeSharpness: StrokeSharpness;
  26. roughness: number;
  27. opacity: number;
  28. width: number;
  29. height: number;
  30. angle: number;
  31. /** Random integer used to seed shape generation so that the roughjs shape
  32. doesn't differ across renders. */
  33. seed: number;
  34. /** Integer that is sequentially incremented on each change. Used to reconcile
  35. elements during collaboration or when saving to server. */
  36. version: number;
  37. /** Random integer that is regenerated on each change.
  38. Used for deterministic reconciliation of updates during collaboration,
  39. in case the versions (see above) are identical. */
  40. versionNonce: number;
  41. isDeleted: boolean;
  42. /** List of groups the element belongs to.
  43. Ordered from deepest to shallowest. */
  44. groupIds: readonly GroupId[];
  45. /** other elements that are bound to this element */
  46. boundElements:
  47. | readonly Readonly<{
  48. id: ExcalidrawLinearElement["id"];
  49. type: "arrow" | "text";
  50. }>[]
  51. | null;
  52. /** epoch (ms) timestamp of last element update */
  53. updated: number;
  54. link: string | null;
  55. locked: boolean;
  56. customData?: Record<string, any>;
  57. }>;
  58. export type ExcalidrawSelectionElement = _ExcalidrawElementBase & {
  59. type: "selection";
  60. };
  61. export type ExcalidrawRectangleElement = _ExcalidrawElementBase & {
  62. type: "rectangle";
  63. };
  64. export type ExcalidrawDiamondElement = _ExcalidrawElementBase & {
  65. type: "diamond";
  66. };
  67. export type ExcalidrawEllipseElement = _ExcalidrawElementBase & {
  68. type: "ellipse";
  69. };
  70. export type ExcalidrawImageElement = _ExcalidrawElementBase &
  71. Readonly<{
  72. type: "image";
  73. fileId: FileId | null;
  74. /** whether respective file is persisted */
  75. status: "pending" | "saved" | "error";
  76. /** X and Y scale factors <-1, 1>, used for image axis flipping */
  77. scale: [number, number];
  78. }>;
  79. export type InitializedExcalidrawImageElement = MarkNonNullable<
  80. ExcalidrawImageElement,
  81. "fileId"
  82. >;
  83. /**
  84. * These are elements that don't have any additional properties.
  85. */
  86. export type ExcalidrawGenericElement =
  87. | ExcalidrawSelectionElement
  88. | ExcalidrawRectangleElement
  89. | ExcalidrawDiamondElement
  90. | ExcalidrawEllipseElement;
  91. /**
  92. * ExcalidrawElement should be JSON serializable and (eventually) contain
  93. * no computed data. The list of all ExcalidrawElements should be shareable
  94. * between peers and contain no state local to the peer.
  95. */
  96. export type ExcalidrawElement =
  97. | ExcalidrawGenericElement
  98. | ExcalidrawTextElement
  99. | ExcalidrawLinearElement
  100. | ExcalidrawFreeDrawElement
  101. | ExcalidrawImageElement;
  102. export type NonDeleted<TElement extends ExcalidrawElement> = TElement & {
  103. isDeleted: boolean;
  104. };
  105. export type NonDeletedExcalidrawElement = NonDeleted<ExcalidrawElement>;
  106. export type ExcalidrawTextElement = _ExcalidrawElementBase &
  107. Readonly<{
  108. type: "text";
  109. fontSize: number;
  110. fontFamily: FontFamilyValues;
  111. text: string;
  112. baseline: number;
  113. textAlign: TextAlign;
  114. verticalAlign: VerticalAlign;
  115. containerId: ExcalidrawGenericElement["id"] | null;
  116. originalText: string;
  117. }>;
  118. export type ExcalidrawBindableElement =
  119. | ExcalidrawRectangleElement
  120. | ExcalidrawDiamondElement
  121. | ExcalidrawEllipseElement
  122. | ExcalidrawTextElement
  123. | ExcalidrawImageElement;
  124. export type ExcalidrawTextContainer =
  125. | ExcalidrawRectangleElement
  126. | ExcalidrawDiamondElement
  127. | ExcalidrawEllipseElement
  128. | ExcalidrawImageElement;
  129. export type ExcalidrawTextElementWithContainer = {
  130. containerId: ExcalidrawTextContainer["id"];
  131. } & ExcalidrawTextElement;
  132. export type PointBinding = {
  133. elementId: ExcalidrawBindableElement["id"];
  134. focus: number;
  135. gap: number;
  136. };
  137. export type Arrowhead = "arrow" | "bar" | "dot" | "triangle";
  138. export type ExcalidrawLinearElement = _ExcalidrawElementBase &
  139. Readonly<{
  140. type: "line" | "arrow";
  141. points: readonly Point[];
  142. lastCommittedPoint: Point | null;
  143. startBinding: PointBinding | null;
  144. endBinding: PointBinding | null;
  145. startArrowhead: Arrowhead | null;
  146. endArrowhead: Arrowhead | null;
  147. }>;
  148. export type ExcalidrawFreeDrawElement = _ExcalidrawElementBase &
  149. Readonly<{
  150. type: "freedraw";
  151. points: readonly Point[];
  152. pressures: readonly number[];
  153. simulatePressure: boolean;
  154. lastCommittedPoint: Point | null;
  155. }>;
  156. export type FileId = string & { _brand: "FileId" };