shapes.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import * as React from 'react';
  7. export type ShapeName =
  8. | '' | 'set' | 'intersect' | 'union' | 'subtract'
  9. export function Shape(props: {
  10. name?: ShapeName,
  11. style?: React.CSSProperties,
  12. title?: string
  13. }) {
  14. const shape = getShape(props.name)
  15. if (!shape) return null
  16. return <span style={props.style} title={props.title}>
  17. {shape}
  18. </span>;
  19. }
  20. function getShape(name?: ShapeName) {
  21. switch (name) {
  22. case 'union': return <Union />
  23. case 'subtract': return <Subtract />
  24. case 'intersect': return <Intersect />
  25. case 'set': return <Set />
  26. default: return null
  27. }
  28. }
  29. const circleLeft = <circle r="6px" id="circle-left" cy="16px" cx="12px" strokeWidth="1"/>
  30. const circleRight = <circle r="6px" id="circle-right" cy="16px" cx="20px" strokeWidth="1"/>
  31. function Union() {
  32. return <svg width="32px" height="32px">
  33. <defs>
  34. {circleLeft}
  35. {circleRight}
  36. </defs>
  37. <g>
  38. <use href="#circle-left" className="msp-shape-filled"/>
  39. <use href="#circle-right" className="msp-shape-filled"/>
  40. </g>
  41. </svg>;
  42. }
  43. function Subtract() {
  44. return <svg width="32px" height="32px">
  45. <defs>
  46. {circleLeft}
  47. {circleRight}
  48. <mask id="mask-left">
  49. <use href="#circle-left" fill="white" stroke="white"/>
  50. <use href="#circle-right" fill="black" strokeWidth="0" stroke="white"/>
  51. </mask>
  52. </defs>
  53. <g>
  54. <use href="#circle-left" className="msp-shape-filled" mask="url(#mask-left)"/>
  55. <use href="#circle-right" className="msp-shape-empty"/>
  56. </g>
  57. </svg>;
  58. }
  59. function Intersect() {
  60. return <svg width="32px" height="32px">
  61. <defs>
  62. {circleLeft}
  63. {circleRight}
  64. <clipPath id="clip-left">
  65. <use href="#circle-right"/>
  66. </clipPath>
  67. </defs>
  68. <g>
  69. <use href="#circle-left" className="msp-shape-filled" clipPath="url(#clip-left)"/>
  70. <use href="#circle-left" className="msp-shape-empty"/>
  71. <use href="#circle-right" className="msp-shape-empty"/>
  72. </g>
  73. </svg>;
  74. }
  75. function Set() {
  76. return <svg width="32px" height="32px">
  77. <defs>
  78. {circleLeft}
  79. {circleRight}
  80. </defs>
  81. <g>
  82. <use href="#circle-left" className="msp-shape-filled"/>
  83. <use href="#circle-right" className="msp-shape-empty"/>
  84. </g>
  85. </svg>;
  86. }