typeChecks.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { AppState } from "../types";
  2. import {
  3. ExcalidrawElement,
  4. ExcalidrawTextElement,
  5. ExcalidrawLinearElement,
  6. ExcalidrawBindableElement,
  7. ExcalidrawGenericElement,
  8. ExcalidrawFreeDrawElement,
  9. InitializedExcalidrawImageElement,
  10. ExcalidrawImageElement,
  11. ExcalidrawTextElementWithContainer,
  12. ExcalidrawTextContainer,
  13. } from "./types";
  14. export const isGenericElement = (
  15. element: ExcalidrawElement | null,
  16. ): element is ExcalidrawGenericElement => {
  17. return (
  18. element != null &&
  19. (element.type === "selection" ||
  20. element.type === "rectangle" ||
  21. element.type === "diamond" ||
  22. element.type === "ellipse")
  23. );
  24. };
  25. export const isInitializedImageElement = (
  26. element: ExcalidrawElement | null,
  27. ): element is InitializedExcalidrawImageElement => {
  28. return !!element && element.type === "image" && !!element.fileId;
  29. };
  30. export const isImageElement = (
  31. element: ExcalidrawElement | null,
  32. ): element is ExcalidrawImageElement => {
  33. return !!element && element.type === "image";
  34. };
  35. export const isTextElement = (
  36. element: ExcalidrawElement | null,
  37. ): element is ExcalidrawTextElement => {
  38. return element != null && element.type === "text";
  39. };
  40. export const isFreeDrawElement = (
  41. element?: ExcalidrawElement | null,
  42. ): element is ExcalidrawFreeDrawElement => {
  43. return element != null && isFreeDrawElementType(element.type);
  44. };
  45. export const isFreeDrawElementType = (
  46. elementType: ExcalidrawElement["type"],
  47. ): boolean => {
  48. return elementType === "freedraw";
  49. };
  50. export const isLinearElement = (
  51. element?: ExcalidrawElement | null,
  52. ): element is ExcalidrawLinearElement => {
  53. return element != null && isLinearElementType(element.type);
  54. };
  55. export const isLinearElementType = (
  56. elementType: AppState["activeTool"]["type"],
  57. ): boolean => {
  58. return (
  59. elementType === "arrow" || elementType === "line" // || elementType === "freedraw"
  60. );
  61. };
  62. export const isBindingElement = (
  63. element?: ExcalidrawElement | null,
  64. includeLocked = true,
  65. ): element is ExcalidrawLinearElement => {
  66. return (
  67. element != null &&
  68. (!element.locked || includeLocked === true) &&
  69. isBindingElementType(element.type)
  70. );
  71. };
  72. export const isBindingElementType = (
  73. elementType: AppState["activeTool"]["type"],
  74. ): boolean => {
  75. return elementType === "arrow";
  76. };
  77. export const isBindableElement = (
  78. element: ExcalidrawElement | null,
  79. includeLocked = true,
  80. ): element is ExcalidrawBindableElement => {
  81. return (
  82. element != null &&
  83. (!element.locked || includeLocked === true) &&
  84. (element.type === "rectangle" ||
  85. element.type === "diamond" ||
  86. element.type === "ellipse" ||
  87. element.type === "image" ||
  88. (element.type === "text" && !element.containerId))
  89. );
  90. };
  91. export const isTextBindableContainer = (
  92. element: ExcalidrawElement | null,
  93. includeLocked = true,
  94. ): element is ExcalidrawTextContainer => {
  95. return (
  96. element != null &&
  97. (!element.locked || includeLocked === true) &&
  98. (element.type === "rectangle" ||
  99. element.type === "diamond" ||
  100. element.type === "ellipse" ||
  101. element.type === "image")
  102. );
  103. };
  104. export const isExcalidrawElement = (element: any): boolean => {
  105. return (
  106. element?.type === "text" ||
  107. element?.type === "diamond" ||
  108. element?.type === "rectangle" ||
  109. element?.type === "ellipse" ||
  110. element?.type === "arrow" ||
  111. element?.type === "freedraw" ||
  112. element?.type === "line"
  113. );
  114. };
  115. export const hasBoundTextElement = (
  116. element: ExcalidrawElement | null,
  117. ): element is ExcalidrawBindableElement => {
  118. return (
  119. isBindableElement(element) &&
  120. !!element.boundElements?.some(({ type }) => type === "text")
  121. );
  122. };
  123. export const isBoundToContainer = (
  124. element: ExcalidrawElement | null,
  125. ): element is ExcalidrawTextElementWithContainer => {
  126. return (
  127. element !== null && isTextElement(element) && element.containerId !== null
  128. );
  129. };