ables.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { MoveableManagerInterface, Renderer } from 'moveable';
  2. import { HorizontalConstraint, VerticalConstraint } from '../types';
  3. import { Scene } from './scene';
  4. export const dimensionViewable = {
  5. name: 'dimensionViewable',
  6. props: {},
  7. events: {},
  8. render(moveable: MoveableManagerInterface<any, any>, React: Renderer) {
  9. const rect = moveable.getRect();
  10. return (
  11. <div
  12. key={'dimension-viewable'}
  13. className={'moveable-dimension'}
  14. style={{
  15. position: 'absolute',
  16. left: `${rect.width / 2}px`,
  17. top: `${rect.height + 20}px`,
  18. background: '#4af',
  19. borderRadius: '2px',
  20. padding: '2px 4px',
  21. color: 'white',
  22. fontSize: '13px',
  23. whiteSpace: 'nowrap',
  24. fontWeight: 'bold',
  25. willChange: 'transform',
  26. transform: 'translate(-50%, 0px)',
  27. zIndex: 100,
  28. }}
  29. >
  30. {Math.round(rect.offsetWidth)} x {Math.round(rect.offsetHeight)}
  31. </div>
  32. );
  33. },
  34. };
  35. export const constraintViewable = (scene: Scene) => ({
  36. name: 'constraintViewable',
  37. props: {},
  38. events: {},
  39. render(moveable: MoveableManagerInterface<any, any>, React: Renderer) {
  40. const rect = moveable.getRect();
  41. const targetElement = scene.findElementByTarget(moveable.state.target);
  42. // If target is currently in motion or selection is more than 1 element don't display constraint visualizations
  43. if (
  44. targetElement?.isMoving ||
  45. (scene.selecto?.getSelectedTargets() && scene.selecto?.getSelectedTargets().length > 1)
  46. ) {
  47. return;
  48. }
  49. let verticalConstraintVisualization = null;
  50. let horizontalConstraintVisualization = null;
  51. const constraint = targetElement?.tempConstraint ?? targetElement?.options.constraint ?? {};
  52. const borderStyle = '1px dashed #4af';
  53. const centerIndicatorLineOne = React.createElement('div', {
  54. style: {
  55. position: 'absolute',
  56. left: `${rect.width / 2}px`,
  57. top: `${rect.height / 2 - rect.height / 16}px`,
  58. borderLeft: borderStyle,
  59. height: `${rect.height / 8}px`,
  60. transform: 'rotate(45deg)',
  61. },
  62. });
  63. const centerIndicatorLineTwo = React.createElement('div', {
  64. style: {
  65. position: 'absolute',
  66. left: `${rect.width / 2}px`,
  67. top: `${rect.height / 2 - rect.height / 16}px`,
  68. borderLeft: borderStyle,
  69. height: `${rect.height / 8}px`,
  70. transform: 'rotate(-45deg)',
  71. },
  72. });
  73. const centerIndicator = React.createElement('div', {}, [centerIndicatorLineOne, centerIndicatorLineTwo]);
  74. const verticalConstraintTop = React.createElement('div', {
  75. style: {
  76. position: 'absolute',
  77. left: `${rect.width / 2}px`,
  78. bottom: '0px',
  79. borderLeft: borderStyle,
  80. height: '100vh',
  81. },
  82. });
  83. const verticalConstraintBottom = React.createElement('div', {
  84. style: {
  85. position: 'absolute',
  86. left: `${rect.width / 2}px`,
  87. top: `${rect.height}px`,
  88. borderLeft: borderStyle,
  89. height: '100vh',
  90. },
  91. });
  92. const verticalConstraintTopBottom = React.createElement('div', {}, [
  93. verticalConstraintTop,
  94. verticalConstraintBottom,
  95. ]);
  96. const verticalConstraintCenterLine = React.createElement('div', {
  97. style: {
  98. position: 'absolute',
  99. left: `${rect.width / 2}px`,
  100. top: `${rect.height / 4}px`,
  101. borderLeft: borderStyle,
  102. height: `${rect.height / 2}px`,
  103. },
  104. });
  105. const verticalConstraintCenter = React.createElement('div', {}, [verticalConstraintCenterLine, centerIndicator]);
  106. switch (constraint.vertical) {
  107. case VerticalConstraint.Top:
  108. verticalConstraintVisualization = verticalConstraintTop;
  109. break;
  110. case VerticalConstraint.Bottom:
  111. verticalConstraintVisualization = verticalConstraintBottom;
  112. break;
  113. case VerticalConstraint.TopBottom:
  114. verticalConstraintVisualization = verticalConstraintTopBottom;
  115. break;
  116. case VerticalConstraint.Center:
  117. verticalConstraintVisualization = verticalConstraintCenter;
  118. break;
  119. }
  120. const horizontalConstraintLeft = React.createElement('div', {
  121. style: {
  122. position: 'absolute',
  123. right: '0px',
  124. top: `${rect.height / 2}px`,
  125. borderTop: borderStyle,
  126. width: '100vw',
  127. },
  128. });
  129. const horizontalConstraintRight = React.createElement('div', {
  130. style: {
  131. position: 'absolute',
  132. left: `${rect.width}px`,
  133. top: `${rect.height / 2}px`,
  134. borderTop: borderStyle,
  135. width: '100vw',
  136. },
  137. });
  138. const horizontalConstraintLeftRight = React.createElement('div', {}, [
  139. horizontalConstraintLeft,
  140. horizontalConstraintRight,
  141. ]);
  142. const horizontalConstraintCenterLine = React.createElement('div', {
  143. style: {
  144. position: 'absolute',
  145. left: `${rect.width / 4}px`,
  146. top: `${rect.height / 2}px`,
  147. borderTop: borderStyle,
  148. width: `${rect.width / 2}px`,
  149. },
  150. });
  151. const horizontalConstraintCenter = React.createElement('div', {}, [
  152. horizontalConstraintCenterLine,
  153. centerIndicator,
  154. ]);
  155. switch (constraint.horizontal) {
  156. case HorizontalConstraint.Left:
  157. horizontalConstraintVisualization = horizontalConstraintLeft;
  158. break;
  159. case HorizontalConstraint.Right:
  160. horizontalConstraintVisualization = horizontalConstraintRight;
  161. break;
  162. case HorizontalConstraint.LeftRight:
  163. horizontalConstraintVisualization = horizontalConstraintLeftRight;
  164. break;
  165. case HorizontalConstraint.Center:
  166. horizontalConstraintVisualization = horizontalConstraintCenter;
  167. break;
  168. }
  169. const constraintVisualization = React.createElement('div', {}, [
  170. verticalConstraintVisualization,
  171. horizontalConstraintVisualization,
  172. ]);
  173. return constraintVisualization;
  174. },
  175. });