typeChecks.ts 3.5 KB

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