11import * as React from 'react' ;
22import './Styles.css' ;
3+ import { debounce } from './Globals/Utils' ;
34
45export enum ResizeType {
56 Left = "resize-left" ,
@@ -20,40 +21,56 @@ interface IProps {
2021export const Resizable : React . FC < IProps > = ( props ) => {
2122 const [ adjustedSize , setAdjustedSize ] = React . useState ( 0 ) ;
2223
24+ const resize = ( originalX : number , originalY : number , x : number , y : number ) => {
25+ const adjustmentX =
26+ Math . min (
27+ Math . max ( props . type === ResizeType . Left ? originalX - x : x - originalX , props . minimumAdjust ) ,
28+ props . maximumAdjust || 999999
29+ ) ;
30+ const adjustmentY =
31+ Math . min (
32+ Math . max ( props . type === ResizeType . Top ? originalY - y : y - originalY , props . minimumAdjust ) ,
33+ props . maximumAdjust || 999999
34+ ) ;
35+ const adjustment = props . type === ResizeType . Left || props . type === ResizeType . Right ? adjustmentX : adjustmentY ;
36+
37+ if ( adjustment !== adjustedSize ) {
38+ setAdjustedSize ( adjustment ) ;
39+ props . onResize ( adjustment ) ;
40+ }
41+ }
42+
43+ const startTouchResize = ( e : React . TouchEvent < HTMLDivElement > ) => {
44+ const originalTouchX = props . type === ResizeType . Left ? e . touches [ 0 ] . pageX + adjustedSize : e . touches [ 0 ] . pageX - adjustedSize ;
45+ const originalTouchY = props . type === ResizeType . Top ? e . touches [ 0 ] . pageY + adjustedSize : e . touches [ 0 ] . pageY - adjustedSize ;
46+
47+ const touchResize = ( e : TouchEvent ) => resize ( originalTouchX , originalTouchY , e . touches [ 0 ] . pageX , e . touches [ 0 ] . pageY ) ;
48+ const debouncedTouchResize = debounce < typeof touchResize > ( touchResize , 10 ) ;
49+ const withPreventDefault = ( e : TouchEvent ) => { e . preventDefault ( ) ; e . stopImmediatePropagation ( ) ; debouncedTouchResize ( e ) ; } ;
50+ window . addEventListener ( 'touchmove' , withPreventDefault ) ;
51+ window . addEventListener ( 'touchend' , ( ) => window . removeEventListener ( 'touchmove' , withPreventDefault ) ) ;
52+ e . preventDefault ( ) ;
53+ e . stopPropagation ( ) ;
54+ }
55+
2356 const startResize = ( e : React . MouseEvent < HTMLDivElement , MouseEvent > ) => {
2457 const originalMouseX = props . type === ResizeType . Left ? e . pageX + adjustedSize : e . pageX - adjustedSize ;
2558 const originalMouseY = props . type === ResizeType . Top ? e . pageY + adjustedSize : e . pageY - adjustedSize ;
2659
27- const resize = ( e : MouseEvent ) => {
28- const adjustmentX =
29- Math . min (
30- Math . max ( props . type === ResizeType . Left ? originalMouseX - e . pageX : e . pageX - originalMouseX , props . minimumAdjust ) ,
31- props . maximumAdjust || 999999
32- ) ;
33- const adjustmentY =
34- Math . min (
35- Math . max ( props . type === ResizeType . Top ? originalMouseY - e . pageY : e . pageY - originalMouseY , props . minimumAdjust ) ,
36- props . maximumAdjust || 999999
37- ) ;
38- const adjustment = props . type === ResizeType . Left || props . type === ResizeType . Right ? adjustmentX : adjustmentY ;
39-
40- if ( adjustment !== adjustedSize ) {
41- setAdjustedSize ( adjustment ) ;
42- props . onResize ( adjustment ) ;
43- }
44- }
45-
46- const stopResize = ( ) => window . removeEventListener ( 'mousemove' , resize ) ;
47-
60+ const mouseResize = ( e : MouseEvent ) => resize ( originalMouseX , originalMouseY , e . pageX , e . pageY ) ;
61+ const debouncedMouseResize = debounce < typeof mouseResize > ( mouseResize , 10 ) ;
62+ const withPreventDefault = ( e : MouseEvent ) => { e . preventDefault ( ) ; e . stopImmediatePropagation ( ) ; debouncedMouseResize ( e ) ; } ;
63+ window . addEventListener ( 'mousemove' , withPreventDefault ) ;
64+ window . addEventListener ( 'mouseup' , ( ) => window . removeEventListener ( 'mousemove' , withPreventDefault ) ) ;
4865 e . preventDefault ( ) ;
49- window . addEventListener ( 'mousemove' , resize ) ;
50- window . addEventListener ( 'mouseup' , stopResize ) ;
66+ e . stopPropagation ( ) ;
5167 }
5268
5369 return (
5470 < div
5571 style = { { width : props . width , height : props . height } }
5672 className = { `spaces-resize-handle ${ props . type } ` }
57- onMouseDown = { startResize } />
73+ onMouseDown = { startResize }
74+ onTouchStart = { startTouchResize } />
5875 )
5976}
0 commit comments