typeChecks.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import {
  2. ExcalidrawElement,
  3. ExcalidrawTextElement,
  4. ExcalidrawLinearElement,
  5. ExcalidrawBindableElement,
  6. ExcalidrawGenericElement,
  7. ExcalidrawFreeDrawElement,
  8. } from "./types";
  9. export const isGenericElement = (
  10. element: ExcalidrawElement | null,
  11. ): element is ExcalidrawGenericElement => {
  12. return (
  13. element != null &&
  14. (element.type === "selection" ||
  15. element.type === "rectangle" ||
  16. element.type === "diamond" ||
  17. element.type === "ellipse")
  18. );
  19. };
  20. export const isTextElement = (
  21. element: ExcalidrawElement | null,
  22. ): element is ExcalidrawTextElement => {
  23. return element != null && element.type === "text";
  24. };
  25. export const isFreeDrawElement = (
  26. element?: ExcalidrawElement | null,
  27. ): element is ExcalidrawFreeDrawElement => {
  28. return element != null && isFreeDrawElementType(element.type);
  29. };
  30. export const isFreeDrawElementType = (
  31. elementType: ExcalidrawElement["type"],
  32. ): boolean => {
  33. return elementType === "freedraw";
  34. };
  35. export const isLinearElement = (
  36. element?: ExcalidrawElement | null,
  37. ): element is ExcalidrawLinearElement => {
  38. return element != null && isLinearElementType(element.type);
  39. };
  40. export const isLinearElementType = (
  41. elementType: ExcalidrawElement["type"],
  42. ): boolean => {
  43. return (
  44. elementType === "arrow" || elementType === "line" // || elementType === "freedraw"
  45. );
  46. };
  47. export const isBindingElement = (
  48. element?: ExcalidrawElement | null,
  49. ): element is ExcalidrawLinearElement => {
  50. return element != null && isBindingElementType(element.type);
  51. };
  52. export const isBindingElementType = (
  53. elementType: ExcalidrawElement["type"],
  54. ): boolean => {
  55. return elementType === "arrow";
  56. };
  57. export const isBindableElement = (
  58. element: ExcalidrawElement | null,
  59. ): element is ExcalidrawBindableElement => {
  60. return (
  61. element != null &&
  62. (element.type === "rectangle" ||
  63. element.type === "diamond" ||
  64. element.type === "ellipse" ||
  65. element.type === "text")
  66. );
  67. };
  68. export const isExcalidrawElement = (element: any): boolean => {
  69. return (
  70. element?.type === "text" ||
  71. element?.type === "diamond" ||
  72. element?.type === "rectangle" ||
  73. element?.type === "ellipse" ||
  74. element?.type === "arrow" ||
  75. element?.type === "freedraw" ||
  76. element?.type === "line"
  77. );
  78. };