Dragger.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import _extends from 'babel-runtime/helpers/extends';
  2. import _Object$getPrototypeOf from 'babel-runtime/core-js/object/get-prototype-of';
  3. import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
  4. import _createClass from 'babel-runtime/helpers/createClass';
  5. import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
  6. import _inherits from 'babel-runtime/helpers/inherits';
  7. import React from 'react';
  8. import PropTypes from 'prop-types';
  9. import { int, innerHeight, innerWidth, outerHeight, outerWidth, parseBounds, isNumber } from './utils';
  10. var doc = document;
  11. var Dragger = function (_React$Component) {
  12. _inherits(Dragger, _React$Component);
  13. function Dragger() {
  14. var _ref;
  15. _classCallCheck(this, Dragger);
  16. for (var _len = arguments.length, props = Array(_len), _key = 0; _key < _len; _key++) {
  17. props[_key] = arguments[_key];
  18. }
  19. var _this = _possibleConstructorReturn(this, (_ref = Dragger.__proto__ || _Object$getPrototypeOf(Dragger)).call.apply(_ref, [this].concat(props)));
  20. _this.state = {
  21. x: null,
  22. y: null,
  23. originX: 0,
  24. originY: 0,
  25. lastX: 0,
  26. lastY: 0
  27. };
  28. _this.move = _this.move.bind(_this);
  29. _this.onDragEnd = _this.onDragEnd.bind(_this);
  30. return _this;
  31. }
  32. _createClass(Dragger, [{
  33. key: 'componentDidMount',
  34. value: function componentDidMount() {}
  35. }, {
  36. key: 'move',
  37. value: function move(event) {
  38. var _state = this.state,
  39. lastX = _state.lastX,
  40. lastY = _state.lastY;
  41. /* event.client - this.state.origin 表示的是移动的距离,
  42. * elX表示的是原来已经有的位移
  43. */
  44. var deltaX = event.clientX - this.state.originX + lastX;
  45. var deltaY = event.clientY - this.state.originY + lastY;
  46. var bounds = this.props.bounds;
  47. if (bounds) {
  48. /**
  49. * 如果用户指定一个边界,那么在这里处理
  50. */
  51. var NewBounds = typeof bounds !== 'string' ? bounds : parseBounds(bounds);
  52. /**
  53. * 移动范围设定,永远移动 n 的倍数
  54. * 注意:设定移动范围的时候,一定要在判断bounds之前,否则会造成bounds不对齐
  55. */
  56. var grid = this.props.grid;
  57. if (Array.isArray(grid) && grid.length === 2) {
  58. deltaX = Math.round(deltaX / grid[0]) * grid[0];
  59. deltaY = Math.round(deltaY / grid[1]) * grid[1];
  60. }
  61. if (this.props.bounds === 'parent') {
  62. NewBounds = {
  63. left: int(this.parent.style.paddingLeft) + int(this.self.style.marginLeft) - this.self.offsetLeft,
  64. top: int(this.parent.style.paddingTop) + int(this.self.style.marginTop) - this.self.offsetTop,
  65. right: innerWidth(this.parent) - outerWidth(this.self) - this.self.offsetLeft + int(this.parent.style.paddingRight) - int(this.self.style.marginRight),
  66. bottom: innerHeight(this.parent) - outerHeight(this.self) - this.self.offsetTop + int(this.parent.style.paddingBottom) - int(this.self.style.marginBottom)
  67. };
  68. }
  69. /**
  70. * 保证不超出右边界和底部
  71. * keep element right and bot can not cross the bounds
  72. */
  73. if (isNumber(NewBounds.right)) deltaX = Math.min(deltaX, NewBounds.right);
  74. if (isNumber(NewBounds.bottom)) deltaY = Math.min(deltaY, NewBounds.bottom);
  75. /**
  76. * 保证不超出左边和上边
  77. * keep element left and top can not cross the bounds
  78. */
  79. if (isNumber(NewBounds.left)) deltaX = Math.max(deltaX, NewBounds.left);
  80. if (isNumber(NewBounds.top)) deltaY = Math.max(deltaY, NewBounds.top);
  81. }
  82. /**如果设置了x,y限制 */
  83. deltaX = this.props.allowX ? deltaX : 0;
  84. deltaY = this.props.allowY ? deltaY : 0;
  85. this.setState({
  86. x: deltaX,
  87. y: deltaY
  88. });
  89. }
  90. }, {
  91. key: 'onDragStart',
  92. value: function onDragStart(event) {
  93. /** 保证用户在移动元素的时候不会选择到元素内部的东西 */
  94. doc.body.style.userSelect = 'none';
  95. if (this.props.hasDraggerHandle) {
  96. if (event.target.className !== 'handle') return;
  97. }
  98. doc.addEventListener('mousemove', this.move);
  99. doc.addEventListener('mouseup', this.onDragEnd);
  100. if (this.props.bounds === 'parent' && (
  101. //为了让 这段代码不会重复执行
  102. typeof this.parent === 'undefined' || this.parent === null)) {
  103. /**
  104. * 在这里我们将父节点缓存下来,保证当用户鼠标离开拖拽区域时,我们仍然能获取到父节点
  105. * what we do here is
  106. * making sure that we still can retrieve our parent when user's mouse left this node.
  107. */
  108. this.parent = event.currentTarget.offsetParent;
  109. /**
  110. * 我们自己
  111. * ourself
  112. */
  113. this.self = event.currentTarget;
  114. }
  115. this.setState({
  116. originX: event.clientX,
  117. originY: event.clientY,
  118. lastX: this.state.x,
  119. lastY: this.state.y
  120. });
  121. }
  122. }, {
  123. key: 'onDragEnd',
  124. value: function onDragEnd(event) {
  125. /** 取消用户选择限制,用户可以重新选择 */
  126. doc.body.style.userSelect = '';
  127. this.parent = null;
  128. this.self = null;
  129. doc.removeEventListener('mousemove', this.move);
  130. doc.removeEventListener('mouseup', this.onDragEnd);
  131. }
  132. }, {
  133. key: 'render',
  134. value: function render() {
  135. var _state2 = this.state,
  136. x = _state2.x,
  137. y = _state2.y;
  138. var _props = this.props,
  139. bounds = _props.bounds,
  140. style = _props.style,
  141. className = _props.className,
  142. others = _props.others;
  143. /**主要是为了让用户定义自己的className去修改css */
  144. var fixedClassName = typeof className === 'undefined' ? '' : className + ' ';
  145. return React.createElement(
  146. 'div',
  147. _extends({ className: fixedClassName + 'WrapDragger',
  148. style: _extends({}, style, { touchAction: 'none!important', transform: 'translate(' + x + 'px,' + y + 'px)' }),
  149. onMouseDown: this.onDragStart.bind(this),
  150. onMouseUp: this.onDragEnd.bind(this)
  151. }, others),
  152. React.cloneElement(React.Children.only(this.props.children), {})
  153. );
  154. }
  155. }]);
  156. return Dragger;
  157. }(React.Component);
  158. Dragger.propTypes = {
  159. bounds: PropTypes.oneOfType([PropTypes.shape({
  160. left: PropTypes.number,
  161. right: PropTypes.number,
  162. top: PropTypes.number,
  163. bottom: PropTypes.number
  164. }), PropTypes.string]),
  165. grid: PropTypes.arrayOf(PropTypes.number),
  166. allowX: PropTypes.bool,
  167. allowY: PropTypes.bool,
  168. hasDraggerHandle: PropTypes.bool
  169. };
  170. Dragger.defaultProps = {
  171. allowX: true,
  172. allowY: true,
  173. hasDraggerHandle: false
  174. };
  175. var _default = Dragger;
  176. export default _default;
  177. ;
  178. var _temp = function () {
  179. if (typeof __REACT_HOT_LOADER__ === 'undefined') {
  180. return;
  181. }
  182. __REACT_HOT_LOADER__.register(doc, 'doc', 'app/src/Dragger.js');
  183. __REACT_HOT_LOADER__.register(Dragger, 'Dragger', 'app/src/Dragger.js');
  184. __REACT_HOT_LOADER__.register(_default, 'default', 'app/src/Dragger.js');
  185. }();
  186. ;