typeChecks.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. ): element is ExcalidrawLinearElement => {
  65. return element != null && isBindingElementType(element.type);
  66. };
  67. export const isBindingElementType = (
  68. elementType: AppState["activeTool"]["type"],
  69. ): boolean => {
  70. return elementType === "arrow";
  71. };
  72. export const isBindableElement = (
  73. element: ExcalidrawElement | null,
  74. ): element is ExcalidrawBindableElement => {
  75. return (
  76. element != null &&
  77. (element.type === "rectangle" ||
  78. element.type === "diamond" ||
  79. element.type === "ellipse" ||
  80. element.type === "image" ||
  81. (element.type === "text" && !element.containerId))
  82. );
  83. };
  84. export const isTextBindableContainer = (
  85. element: ExcalidrawElement | null,
  86. ): element is ExcalidrawTextContainer => {
  87. return (
  88. element != null &&
  89. (element.type === "rectangle" ||
  90. element.type === "diamond" ||
  91. element.type === "ellipse" ||
  92. element.type === "image")
  93. );
  94. };
  95. export const isExcalidrawElement = (element: any): boolean => {
  96. return (
  97. element?.type === "text" ||
  98. element?.type === "diamond" ||
  99. element?.type === "rectangle" ||
  100. element?.type === "ellipse" ||
  101. element?.type === "arrow" ||
  102. element?.type === "freedraw" ||
  103. element?.type === "line"
  104. );
  105. };
  106. export const hasBoundTextElement = (
  107. element: ExcalidrawElement | null,
  108. ): element is ExcalidrawBindableElement => {
  109. return (
  110. isBindableElement(element) &&
  111. !!element.boundElements?.some(({ type }) => type === "text")
  112. );
  113. };
  114. export const isBoundToContainer = (
  115. element: ExcalidrawElement | null,
  116. ): element is ExcalidrawTextElementWithContainer => {
  117. return (
  118. element !== null && isTextElement(element) && element.containerId !== null
  119. );
  120. };