1
1
import * as React from 'react' ;
2
2
import './Styles.css' ;
3
+ import { debounce } from './Globals/Utils' ;
3
4
4
5
export enum ResizeType {
5
6
Left = "resize-left" ,
@@ -20,40 +21,56 @@ interface IProps {
20
21
export const Resizable : React . FC < IProps > = ( props ) => {
21
22
const [ adjustedSize , setAdjustedSize ] = React . useState ( 0 ) ;
22
23
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
+
23
56
const startResize = ( e : React . MouseEvent < HTMLDivElement , MouseEvent > ) => {
24
57
const originalMouseX = props . type === ResizeType . Left ? e . pageX + adjustedSize : e . pageX - adjustedSize ;
25
58
const originalMouseY = props . type === ResizeType . Top ? e . pageY + adjustedSize : e . pageY - adjustedSize ;
26
59
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 ) ) ;
48
65
e . preventDefault ( ) ;
49
- window . addEventListener ( 'mousemove' , resize ) ;
50
- window . addEventListener ( 'mouseup' , stopResize ) ;
66
+ e . stopPropagation ( ) ;
51
67
}
52
68
53
69
return (
54
70
< div
55
71
style = { { width : props . width , height : props . height } }
56
72
className = { `spaces-resize-handle ${ props . type } ` }
57
- onMouseDown = { startResize } />
73
+ onMouseDown = { startResize }
74
+ onTouchStart = { startTouchResize } />
58
75
)
59
76
}
0 commit comments