index.js 13 KB

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