types.ts 4.9 KB

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