index.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = Object.setPrototypeOf ||
  3. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  5. return function (d, b) {
  6. extendStatics(d, b);
  7. function __() { this.constructor = d; }
  8. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  9. };
  10. })();
  11. var __assign = (this && this.__assign) || Object.assign || function(t) {
  12. for (var s, i = 1, n = arguments.length; i < n; i++) {
  13. s = arguments[i];
  14. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  15. t[p] = s[p];
  16. }
  17. return t;
  18. };
  19. import * as React from "react";
  20. import { int, innerHeight, innerWidth, outerHeight, outerWidth, parseBounds } from '../utils';
  21. var doc = document;
  22. var Dragger = /** @class */ (function (_super) {
  23. __extends(Dragger, _super);
  24. function Dragger(props) {
  25. var _this = _super.call(this, props) || this;
  26. _this.state = {
  27. /** x轴位移,单位是px */
  28. x: 0,
  29. /** y轴位移,单位是px */
  30. y: 0,
  31. /**鼠标点击元素的原始位置,单位是px */
  32. originX: 0,
  33. originY: 0,
  34. isUserMove: true,
  35. /**已经移动的位移,单位是px */
  36. lastX: 0,
  37. lastY: 0,
  38. /**堆叠的层级 */
  39. zIndex: 1,
  40. w: 0,
  41. h: 0,
  42. lastW: 0,
  43. lastH: 0
  44. };
  45. _this.move = function (event) {
  46. var _a = _this.state, lastX = _a.lastX, lastY = _a.lastY;
  47. /* event.client - this.state.origin 表示的是移动的距离,
  48. * elX表示的是原来已经有的位移
  49. */
  50. var deltaX, deltaY;
  51. if (event.type.indexOf('mouse') >= 0) {
  52. deltaX = event.clientX - _this.state.originX + lastX;
  53. deltaY = event.clientY - _this.state.originY + lastY;
  54. }
  55. else {
  56. deltaX = event.touches[0].clientX - _this.state.originX + lastX;
  57. deltaY = event.touches[0].clientY - _this.state.originY + lastY;
  58. }
  59. var bounds = _this.props.bounds;
  60. if (bounds) {
  61. /**
  62. * 如果用户指定一个边界,那么在这里处理
  63. */
  64. var NewBounds = typeof bounds !== 'string' ? parseBounds(bounds) : bounds;
  65. /**
  66. * 网格式移动范围设定,永远移动 n 的倍数
  67. * 注意:设定移动范围的时候,一定要在判断bounds之前,否则会造成bounds不对齐
  68. */
  69. var grid = _this.props.grid;
  70. if (Array.isArray(grid) && grid.length === 2) {
  71. deltaX = Math.round(deltaX / grid[0]) * grid[0];
  72. deltaY = Math.round(deltaY / grid[1]) * grid[1];
  73. }
  74. if (_this.props.bounds === 'parent') {
  75. NewBounds = {
  76. left: int(_this.parent.style.paddingLeft) + int(_this.self.style.marginLeft) - _this.self.offsetLeft,
  77. top: int(_this.parent.style.paddingTop) + int(_this.self.style.marginTop) - _this.self.offsetTop,
  78. right: innerWidth(_this.parent) - outerWidth(_this.self) - _this.self.offsetLeft +
  79. int(_this.parent.style.paddingRight) - int(_this.self.style.marginRight),
  80. bottom: innerHeight(_this.parent) - outerHeight(_this.self) - _this.self.offsetTop +
  81. int(_this.parent.style.paddingBottom) - int(_this.self.style.marginBottom)
  82. };
  83. }
  84. /**
  85. * 保证不超出右边界和底部
  86. * keep element right and bot can not cross the bounds
  87. */
  88. if (NewBounds !== 'parent')
  89. deltaX = Math.min(deltaX, NewBounds.right);
  90. if (NewBounds !== 'parent')
  91. deltaY = Math.min(deltaY, NewBounds.bottom);
  92. /**
  93. * 保证不超出左边和上边
  94. * keep element left and top can not cross the bounds
  95. */
  96. if (NewBounds !== 'parent')
  97. deltaX = Math.max(deltaX, NewBounds.left);
  98. if (NewBounds !== 'parent')
  99. deltaY = Math.max(deltaY, NewBounds.top);
  100. }
  101. /**如果设置了x,y限制 */
  102. deltaX = _this.props.allowX ? deltaX : 0;
  103. deltaY = _this.props.allowY ? deltaY : 0;
  104. /**移动时回调,用于外部控制 */
  105. if (_this.props.onMove)
  106. _this.props.onMove(event, deltaX, deltaY);
  107. _this.setState({
  108. x: deltaX,
  109. y: deltaY
  110. });
  111. };
  112. _this.onDragStart = function (event) {
  113. /** 保证用户在移动元素的时候不会选择到元素内部的东西 */
  114. doc.body.style.userSelect = 'none';
  115. if (_this.props.handle) {
  116. if (event.target.id !== 'dragact-handle')
  117. return;
  118. }
  119. /**
  120. * 把监听事件的回掉函数,绑定在document上
  121. * 当设置边界的时候,用户鼠标会离开元素的范围
  122. * 绑定在document上可以使得其依旧能够监听
  123. * 如果绑定在元素上,则鼠标离开元素,就不会再被监听了
  124. */
  125. if (event.type.indexOf('mouse') >= 0) {
  126. doc.addEventListener('mousemove', _this.move);
  127. doc.addEventListener('mouseup', _this.onDragEnd);
  128. }
  129. else {
  130. doc.addEventListener('touchmove', _this.move);
  131. doc.addEventListener('touchend', _this.onDragEnd);
  132. }
  133. if (_this.props.bounds === 'parent' &&
  134. /**为了让 这段代码不会重复执行 */
  135. (typeof _this.parent === 'undefined' || _this.parent === null)) {
  136. /**
  137. * 在这里我们将父节点缓存下来,保证当用户鼠标离开拖拽区域时,我们仍然能获取到父节点
  138. * what we do here is
  139. * making sure that we still can retrieve our parent when user's mouse left this node.
  140. */
  141. _this.parent = event.currentTarget.offsetParent; //todo
  142. /**
  143. * 我们自己
  144. * ourself
  145. */
  146. _this.self = event.currentTarget;
  147. }
  148. _this.props.onDragStart && _this.props.onDragStart(_this.state.x, _this.state.y);
  149. var originX, originY;
  150. if (event.type.indexOf('mouse') >= 0) {
  151. originX = event.clientX;
  152. originY = event.clientY;
  153. }
  154. else {
  155. originX = event.touches[0].clientX;
  156. originY = event.touches[0].clientY;
  157. }
  158. _this.setState({
  159. originX: originX,
  160. originY: originY,
  161. lastX: _this.state.x,
  162. lastY: _this.state.y,
  163. zIndex: 10
  164. });
  165. };
  166. _this.onDragEnd = function (event) {
  167. /** 取消用户选择限制,用户可以重新选择 */
  168. doc.body.style.userSelect = '';
  169. _this.parent = null;
  170. _this.self = null;
  171. if (event.type.indexOf('mouse') >= 0) {
  172. doc.removeEventListener('mousemove', _this.move);
  173. doc.removeEventListener('mouseup', _this.onDragEnd);
  174. }
  175. else {
  176. doc.removeEventListener('touchmove', _this.move);
  177. doc.removeEventListener('touchend', _this.onDragEnd);
  178. }
  179. _this.setState({
  180. zIndex: 1
  181. });
  182. _this.props.onDragEnd && _this.props.onDragEnd(event, _this.state.x, _this.state.y);
  183. };
  184. _this.onResizeStart = function (event) {
  185. /** 保证用户在移动元素的时候不会选择到元素内部的东西 */
  186. doc.body.style.userSelect = 'none';
  187. doc.addEventListener('mouseup', _this.onResizeEnd);
  188. doc.addEventListener('mousemove', _this.onResizing);
  189. var originX, originY;
  190. originX = event.clientX;
  191. originY = event.clientY;
  192. _this.props.onResizeStart && _this.props.onResizeStart(event, _this.state.w, _this.state.h);
  193. _this.setState({
  194. originX: originX,
  195. originY: originY,
  196. zIndex: 2,
  197. lastW: _this.state.w,
  198. lastH: _this.state.h
  199. });
  200. event.stopPropagation();
  201. };
  202. _this.onResizing = function (event) {
  203. /* event.client - this.state.origin 表示的是移动的距离,
  204. * elX表示的是原来已经有的位移
  205. */
  206. var deltaX, deltaY;
  207. if (event.type.indexOf('mouse') >= 0) {
  208. deltaX = event.clientX - _this.state.originX;
  209. deltaY = event.clientY - _this.state.originY;
  210. }
  211. else {
  212. deltaX = event.touches[0].clientX - _this.state.originX;
  213. deltaY = event.touches[0].clientY - _this.state.originY;
  214. }
  215. /**移动时回调,用于外部控制 */
  216. _this.props.onResizing && _this.props.onResizing(event, _this.state.w, _this.state.h);
  217. _this.setState({
  218. w: deltaX + _this.state.lastW,
  219. h: deltaY + _this.state.lastH
  220. });
  221. };
  222. _this.onResizeEnd = function (event) {
  223. doc.body.style.userSelect = '';
  224. doc.removeEventListener('mousemove', _this.onResizing);
  225. doc.removeEventListener('mouseup', _this.onResizeEnd);
  226. _this.props.onResizeEnd && _this.props.onResizeEnd(event, _this.state.w, _this.state.h);
  227. };
  228. _this.mixin = function () {
  229. var dragMix = {};
  230. if (_this.props.canDrag === void 666 || _this.props.canDrag === true) {
  231. dragMix = {
  232. onMouseDown: _this.onDragStart,
  233. onTouchStart: _this.onDragStart,
  234. onTouchEnd: _this.onDragEnd,
  235. onMouseUp: _this.onDragEnd
  236. };
  237. }
  238. var resizeMix = {};
  239. if (_this.props.canResize === void 666 || _this.props.canDrag === true) {
  240. resizeMix = {
  241. onMouseDown: _this.onResizeStart,
  242. onMouseUp: _this.onResizeEnd
  243. };
  244. }
  245. return {
  246. dragMix: dragMix, resizeMix: resizeMix
  247. };
  248. };
  249. // this.move = this.move.bind(this)
  250. // this.onDragEnd = this.onDragEnd.bind(this)
  251. _this.parent = null;
  252. _this.self = null;
  253. return _this;
  254. }
  255. Dragger.prototype.componentDidMount = function () {
  256. /**
  257. * 这个函数只会调用一次
  258. * 这个只是一个临时的解决方案,因为这样会使得元素进行两次刷新
  259. */
  260. if (typeof this.props.x === 'number' &&
  261. typeof this.props.y === 'number') {
  262. this.setState({
  263. x: this.props.x,
  264. y: this.props.y
  265. });
  266. }
  267. };
  268. Dragger.prototype.componentWillReceiveProps = function (nextProps) {
  269. /**
  270. * 外部props 改变的时候更新元素的内部位置
  271. * 这个api设计其实很不好
  272. * 以后可能会修改掉
  273. */
  274. var isUserMove = nextProps.isUserMove;
  275. if (!isUserMove) {
  276. if (typeof nextProps.x === 'number' &&
  277. typeof nextProps.y === 'number') {
  278. this.setState({
  279. x: nextProps.x,
  280. y: nextProps.y,
  281. lastX: nextProps.x,
  282. lastY: nextProps.y,
  283. w: nextProps.w,
  284. h: nextProps.h
  285. });
  286. }
  287. }
  288. };
  289. Dragger.prototype.render = function () {
  290. var _a = this.state, x = _a.x, y = _a.y, w = _a.w, h = _a.h;
  291. var _b = this.props, style = _b.style, className = _b.className, canResize = _b.canResize;
  292. if (!this.props.isUserMove) {
  293. /**当外部设置其props的x,y初始属性的时候,我们在这里设置元素的初始位移 */
  294. x = this.props.x ? this.props.x : 0;
  295. y = this.props.y ? this.props.y : 0;
  296. if (style) {
  297. w = style.width ? style.width : w;
  298. h = style.height ? style.height : h;
  299. }
  300. }
  301. if (style) {
  302. //使得初始化的时候,不会有从0-1缩放动画
  303. w = w === 0 ? style.width : w;
  304. h = h === 0 ? style.height : h;
  305. }
  306. var _c = this.mixin(), dragMix = _c.dragMix, resizeMix = _c.resizeMix;
  307. /**主要是为了让用户定义自己的className去修改css */
  308. var fixedClassName = typeof className === 'undefined' ? '' : className + ' ';
  309. return (React.createElement("div", __assign({ className: fixedClassName + "WrapDragger", style: __assign({}, style, { touchAction: 'none!important', transform: "translate(" + x + "px," + y + "px)", width: w, height: h }) }, dragMix),
  310. React.Children.only(this.props.children),
  311. canResize !== false ?
  312. React.createElement("span", __assign({}, resizeMix, { style: {
  313. position: 'absolute',
  314. width: 10, height: 10, right: 2, bottom: 2, cursor: 'se-resize',
  315. borderRight: '2px solid rgba(15,15,15,0.2)',
  316. borderBottom: '2px solid rgba(15,15,15,0.2)'
  317. } })) : null));
  318. };
  319. /**
  320. * 初始变量设置
  321. */
  322. Dragger.defaultProps = {
  323. allowX: true,
  324. allowY: true,
  325. isUserMove: true
  326. };
  327. return Dragger;
  328. }(React.Component));
  329. export { Dragger };