typeChecks.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import {
  2. ExcalidrawElement,
  3. ExcalidrawTextElement,
  4. ExcalidrawLinearElement,
  5. ExcalidrawBindableElement,
  6. } from "./types";
  7. export const isTextElement = (
  8. element: ExcalidrawElement | null,
  9. ): element is ExcalidrawTextElement => {
  10. return element != null && element.type === "text";
  11. };
  12. export const isLinearElement = (
  13. element?: ExcalidrawElement | null,
  14. ): element is ExcalidrawLinearElement => {
  15. return element != null && isLinearElementType(element.type);
  16. };
  17. export const isLinearElementType = (
  18. elementType: ExcalidrawElement["type"],
  19. ): boolean => {
  20. return (
  21. elementType === "arrow" || elementType === "line" || elementType === "draw"
  22. );
  23. };
  24. export const isBindingElement = (
  25. element?: ExcalidrawElement | null,
  26. ): element is ExcalidrawLinearElement => {
  27. return element != null && isBindingElementType(element.type);
  28. };
  29. export const isBindingElementType = (
  30. elementType: ExcalidrawElement["type"],
  31. ): boolean => {
  32. return elementType === "arrow";
  33. };
  34. export const isBindableElement = (
  35. element: ExcalidrawElement | null,
  36. ): element is ExcalidrawBindableElement => {
  37. return (
  38. element != null &&
  39. (element.type === "rectangle" ||
  40. element.type === "diamond" ||
  41. element.type === "ellipse" ||
  42. element.type === "text")
  43. );
  44. };
  45. export const isExcalidrawElement = (element: any): boolean => {
  46. return (
  47. element?.type === "text" ||
  48. element?.type === "diamond" ||
  49. element?.type === "rectangle" ||
  50. element?.type === "ellipse" ||
  51. element?.type === "arrow" ||
  52. element?.type === "draw" ||
  53. element?.type === "line"
  54. );
  55. };