Module 712960

5,475 characters · in no known chunk
Dependencies
Required by (2)
Lazily required by (0)none
Requires (3)
Lazily requires (0)none
Source Raw
function(e,t,n){"use strict";n.d(t,{A:()=>m});var r=n(627968),i=n(64700),s=n(652215);let a=(e,t)=>{let{x:n,y:r}=e,{x:i,y:s}=t;return Math.pow(n-i,2)+Math.pow(r-s,2)},o=9;var l=function(e){return e[e.DISABLED=0]="DISABLED",e[e.DEFAULT=1]="DEFAULT",e[e.OFFSET=2]="OFFSET",e}(l||{});function u(e,t,n,r,i){let{top:s,bottom:a,left:o,right:l}=e;if(null==s&&null==a?(s=0,a=n-i):null==s&&null!=a?s=n-(a+i):null!=s&&(a=n-(s+i)),null==o&&null==l?(o=0,l=t-r):null==o&&null!=l?o=t-(l+r):null==l&&null!=o&&(l=t-(o+r)),null==s||null==a||null==o||null==l)throw Error("Logically this can never happen based on our if/else statements");return{top:s,left:o,bottom:a,right:l}}function c(e){let{top:t,left:n}=e;return{x:n,y:t}}function d(e){return[null==e.bottom?"TOP":"BOTTOM",null==e.right?"LEFT":"RIGHT"]}function _(e){let{top:t,left:n,bottom:r,right:i}=e;return 0===t||t<=r?r=null:t=null,0===n||n<=i?i=null:n=null,{top:t,left:n,bottom:r,right:i}}function f(e){let{top:t,left:n,bottom:r,right:i}=e;return null==n&&null==i&&(n=0),null!=n&&null!=i&&(i=null),null==t&&null==r&&(t=0),null!=t&&null!=r&&(r=null),{top:t,left:n,bottom:r,right:i}}function p(e){let{top:t,left:n,bottom:r,right:i}=e;return r<0&&(t+=r,r=0),t<0&&(r+=t,t=0),i<0&&(n+=i,i=0),n<0&&(i+=n,n=0),{top:t,left:n,bottom:r,right:i}}function h(e){return null!=e?`${e}px`:"auto"}class m extends i.PureComponent{static FlipTypes=l;static defaultProps={snapOrientation:!1,maxX:0,maxY:0,flipVertical:0,flipHorizontal:0,dragAnywhere:!1};draggableRef=i.createRef();_dragStart={x:0,y:0};_offsetX;_offsetY;position;constructor(e){super(e);const[t,n]=d(e.initialPosition);this.position=f(e.initialPosition),this.state={dragging:!1,verticalOrientation:t,horizontalOrientation:n,atTopEdge:0===this.position.top}}componentDidMount(){this.checkBounds(),this.setDOMPositions(this.position)}componentDidUpdate(e,t){let{state:{verticalOrientation:n,horizontalOrientation:r,atTopEdge:i},props:{maxX:s,maxY:a,onRelocated:o,snapOrientation:l}}=this;(e.maxX!==s||e.maxY!==a||e.onRelocated!==o||e.snapOrientation!==l||t.verticalOrientation!==n||t.horizontalOrientation!==r||t.atTopEdge!==i)&&this.checkBounds()}componentWillUnmount(){window.removeEventListener("mousemove",this.handleMouseMove),window.removeEventListener("mouseup",this.handleMouseUp)}setDOMPositions(e){let{top:t,left:n,bottom:r,right:i}=e,{draggableRef:{current:s}}=this;this.position={top:t,left:n,bottom:r,right:i},null!=s&&(s.style.top=h(t),s.style.bottom=h(r),s.style.left=h(n),s.style.right=h(i))}handleMouseDown=e=>{let{top:t,left:n,bottom:r,right:i}=this.position,{dragAnywhere:a,selector:o,maxX:l,maxY:d}=this.props,{target:_}=e,{current:f}=this.draggableRef;if(null!=f&&e.button===s.hE4.PRIMARY&&(a||null!=_&&null!=o&&""!==o&&null!=_.closest(o))){let{x:s,y:a}=c(u({top:t,left:n,bottom:r,right:i},l,d,f.clientWidth,f.clientHeight));this._dragStart={x:e.clientX,y:e.clientY},this._offsetX=e.clientX-s,this._offsetY=e.clientY-a,window.addEventListener("mousemove",this.handleMouseMove),window.addEventListener("mouseup",this.handleMouseUp)}};handleMouseMove=e=>{e.preventDefault();let{clientX:t,clientY:n}=e,{current:r}=this.draggableRef,{maxX:i,maxY:s,flipVertical:l,flipHorizontal:c,onDragStart:f,onDrag:h,onFlip:m,snapOrientation:E}=this.props,{dragging:g,dragging:A,verticalOrientation:I,horizontalOrientation:T}=this.state,S=!1;if(null==r||!g&&a(this._dragStart,{x:t,y:n})<o)return;g=!0;let{clientWidth:y,clientHeight:v}=r,N=p(u({top:n-this._offsetY,left:t-this._offsetX},i,s,y,v));if(E){let e=d(N=_(N));I=e[0],T=e[1]}else N={top:N.top,left:N.left};2===c&&T!==this.state.horizontalOrientation&&(this._offsetX=y-this._offsetX,S=!0),2===l&&I!==this.state.verticalOrientation&&(this._offsetY=v-this._offsetY,S=!0),this.setDOMPositions(N),A||f?.(N,r),h?.(N,r),S&&m?.([I,T]),this.setState({dragging:g,verticalOrientation:I,horizontalOrientation:T,atTopEdge:0===N.top})};handleMouseUp=()=>{window.removeEventListener("mousemove",this.handleMouseMove),window.removeEventListener("mouseup",this.handleMouseUp),this.state.dragging&&this.setState({dragging:!1},()=>{let{onDragEnd:e}=this.props,{current:t}=this.draggableRef;null!=t&&null!=e&&e(this.position,t)})};_checkBoundsRAF=null;checkBounds=()=>{null!=this._checkBoundsRAF&&cancelAnimationFrame(this._checkBoundsRAF),this._checkBoundsRAF=requestAnimationFrame(this._performCheckBounds)};_performCheckBounds=()=>{let e,{position:t,state:n,draggableRef:{current:r},props:{maxX:i,maxY:s,onRelocated:a,snapOrientation:o}}=this,{verticalOrientation:l,horizontalOrientation:c}=this.state;if(null==r)return;let{clientHeight:f,clientWidth:h}=r,m=u(t,i,s,h,f);if(m=p(m),o){let t=d(e=_(m));l=t[0],c=t[1]}else e={top:m.top,left:m.left,right:null,bottom:null};if(t.left===e.left&&t.right===e.right&&t.top===e.top&&t.bottom===e.bottom)return;a?.(e,r),this.setDOMPositions(e);let E=0===e.top;(l!==n.verticalOrientation||c!==n.horizontalOrientation||E!==n.atTopEdge)&&this.setState({verticalOrientation:l,horizontalOrientation:c,atTopEdge:E})};render(){let{children:e,className:t}=this.props,{dragging:n,verticalOrientation:s,horizontalOrientation:a,atTopEdge:o}=this.state,{top:l,left:u,right:c,bottom:d}=this.position,_={pointerEvents:n?"none":"auto",position:"absolute",top:l,left:u,bottom:d,right:c};return(0,r.jsx)("div",{ref:this.draggableRef,className:t,onMouseDown:this.handleMouseDown,style:_,children:i.cloneElement(i.Children.only(e),{flipped:"RIGHT"===a,verticalFlipped:"BOTTOM"===s,checkBounds:this.checkBounds,atTopEdge:o})})}}}