types.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /** Ids of (linear) elements that are bound to this element. */
  45. boundElementIds: readonly ExcalidrawLinearElement["id"][] | null;
  46. /** epoch (ms) timestamp of last element update */
  47. updated: number;
  48. }>;
  49. export type ExcalidrawSelectionElement = _ExcalidrawElementBase & {
  50. type: "selection";
  51. };
  52. export type ExcalidrawRectangleElement = _ExcalidrawElementBase & {
  53. type: "rectangle";
  54. };
  55. export type ExcalidrawDiamondElement = _ExcalidrawElementBase & {
  56. type: "diamond";
  57. };
  58. export type ExcalidrawEllipseElement = _ExcalidrawElementBase & {
  59. type: "ellipse";
  60. };
  61. export type ExcalidrawImageElement = _ExcalidrawElementBase &
  62. Readonly<{
  63. type: "image";
  64. fileId: FileId | null;
  65. /** whether respective file is persisted */
  66. status: "pending" | "saved" | "error";
  67. /** X and Y scale factors <-1, 1>, used for image axis flipping */
  68. scale: [number, number];
  69. }>;
  70. export type InitializedExcalidrawImageElement = MarkNonNullable<
  71. ExcalidrawImageElement,
  72. "fileId"
  73. >;
  74. /**
  75. * These are elements that don't have any additional properties.
  76. */
  77. export type ExcalidrawGenericElement =
  78. | ExcalidrawSelectionElement
  79. | ExcalidrawRectangleElement
  80. | ExcalidrawDiamondElement
  81. | ExcalidrawEllipseElement;
  82. /**
  83. * ExcalidrawElement should be JSON serializable and (eventually) contain
  84. * no computed data. The list of all ExcalidrawElements should be shareable
  85. * between peers and contain no state local to the peer.
  86. */
  87. export type ExcalidrawElement =
  88. | ExcalidrawGenericElement
  89. | ExcalidrawTextElement
  90. | ExcalidrawLinearElement
  91. | ExcalidrawFreeDrawElement
  92. | ExcalidrawImageElement;
  93. export type NonDeleted<TElement extends ExcalidrawElement> = TElement & {
  94. isDeleted: boolean;
  95. };
  96. export type NonDeletedExcalidrawElement = NonDeleted<ExcalidrawElement>;
  97. export type ExcalidrawTextElement = _ExcalidrawElementBase &
  98. Readonly<{
  99. type: "text";
  100. fontSize: number;
  101. fontFamily: FontFamilyValues;
  102. text: string;
  103. baseline: number;
  104. textAlign: TextAlign;
  105. verticalAlign: VerticalAlign;
  106. }>;
  107. export type ExcalidrawBindableElement =
  108. | ExcalidrawRectangleElement
  109. | ExcalidrawDiamondElement
  110. | ExcalidrawEllipseElement
  111. | ExcalidrawTextElement
  112. | ExcalidrawImageElement;
  113. export type PointBinding = {
  114. elementId: ExcalidrawBindableElement["id"];
  115. focus: number;
  116. gap: number;
  117. };
  118. export type Arrowhead = "arrow" | "bar" | "dot" | "triangle";
  119. export type ExcalidrawLinearElement = _ExcalidrawElementBase &
  120. Readonly<{
  121. type: "line" | "arrow";
  122. points: readonly Point[];
  123. lastCommittedPoint: Point | null;
  124. startBinding: PointBinding | null;
  125. endBinding: PointBinding | null;
  126. startArrowhead: Arrowhead | null;
  127. endArrowhead: Arrowhead | null;
  128. }>;
  129. export type ExcalidrawFreeDrawElement = _ExcalidrawElementBase &
  130. Readonly<{
  131. type: "freedraw";
  132. points: readonly Point[];
  133. pressures: readonly number[];
  134. simulatePressure: boolean;
  135. lastCommittedPoint: Point | null;
  136. }>;
  137. export type FileId = string & { _brand: "FileId" };