types.ts 5.5 KB

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