@@ -8,10 +8,12 @@ type Props = {
88} ;
99
1010const MobileKeyboard : React . FC < Props > = props => {
11- const [ isKeyboardShown , setIsKeyoardShown ] = React . useState ( false ) ;
11+ const [ isKeyboardShown , setIsKeyboardShown ] = React . useState ( false ) ;
1212 const [ buttonContent , setButtonContent ] = React . useState ( 'ᐸ' ) ;
1313 const [ keyboardPosition , setKeyboardPosition ] = React . useState ( { x : 0 , y : 0 } ) ;
14- const [ selectedIndex , setSelectedIndex ] = React . useState ( 1 ) ;
14+ const [ targetKeyboardInput , setTargetKeyboardInput ] = React . useState < Ace . Editor | null > ( null ) ;
15+ const [ lastKeyPressed , setLastKeyPressed ] = React . useState < string > ( '' ) ;
16+ const [ touchStartInfo , setTouchStartInfo ] = React . useState ( { x : 0 , y : 0 , time : 0 } ) ;
1517
1618 const onDrag : DraggableEventHandler = (
1719 e : DraggableEvent ,
@@ -31,53 +33,84 @@ const MobileKeyboard: React.FC<Props> = props => {
3133 document . getElementById ( 'mobile-floating-toggle' ) ! . style . setProperty ( 'width' , '42px' ) ;
3234 document . getElementById ( 'mobile-floating-toggle' ) ! . style . setProperty ( 'opacity' , '0.6' ) ;
3335 setButtonContent ( 'ᐸ' ) ;
34- setIsKeyoardShown ( false ) ;
36+ setIsKeyboardShown ( false ) ;
3537 } else {
3638 //do showing
3739 document . getElementById ( 'mobile-keyboard-toggle' ) ! . style . setProperty ( 'display' , 'flex' ) ;
3840 document . getElementById ( 'mobile-floating-toggle' ) ! . style . setProperty ( 'width' , '99%' ) ;
3941 document . getElementById ( 'mobile-floating-toggle' ) ! . style . setProperty ( 'opacity' , '1' ) ;
4042 setButtonContent ( 'ᐳ' ) ;
41- setIsKeyoardShown ( true ) ;
43+ setIsKeyboardShown ( true ) ;
4244 }
4345 } ;
4446
45- const handleRowToggle = ( event : React . MouseEvent < HTMLButtonElement , MouseEvent > ) => {
46- setSelectedIndex ( ( selectedIndex + 1 ) % 3 ) ;
47- const keyboardClass = document . getElementsByClassName ( 'simple-keyboard-shortcut' ) ;
48- Array . from ( keyboardClass as HTMLCollectionOf < HTMLElement > ) [ 0 ] . style . top =
49- - selectedIndex * 45 + 'px' ;
50- } ;
51-
5247 const handleKeyPress = ( button : string ) => {
5348 if ( ! props . targetKeyboardInput ) {
5449 return ;
5550 }
56- const editor = props . targetKeyboardInput ;
57- if ( button === '{arrowleft}' ) {
51+
52+ setTargetKeyboardInput ( props . targetKeyboardInput ) ;
53+ setLastKeyPressed ( button ) ;
54+ } ;
55+
56+ const handleTouchStart = ( e : React . TouchEvent < HTMLDivElement > ) => {
57+ // Get the touch coordinates and current time
58+ const touch = e . touches [ 0 ] ;
59+ setTouchStartInfo ( {
60+ x : touch . clientX ,
61+ y : touch . clientY ,
62+ time : Date . now ( )
63+ } ) ;
64+ } ;
65+
66+ const handleTouchEnd = ( e : React . TouchEvent < HTMLDivElement > ) => {
67+ // Compare the end position with the start position
68+ const touch = e . changedTouches [ 0 ] ;
69+ const deltaX = touch . clientX - touchStartInfo . x ;
70+ const deltaY = touch . clientY - touchStartInfo . y ;
71+ const deltaTime = Date . now ( ) - touchStartInfo . time ;
72+
73+ // Define thresholds for what you consider a swipe vs a tap
74+ const swipeThreshold = 30 ; // pixels
75+ const tapThresholdTime = 200 ; // milliseconds
76+
77+ if (
78+ Math . abs ( deltaX ) < swipeThreshold &&
79+ Math . abs ( deltaY ) < swipeThreshold &&
80+ deltaTime < tapThresholdTime
81+ ) {
82+ handleKeyRelease ( ) ;
83+ }
84+ } ;
85+
86+ const handleKeyRelease = ( ) => {
87+ if ( ! targetKeyboardInput ) {
88+ return ;
89+ }
90+
91+ const editor = targetKeyboardInput ;
92+
93+ if ( lastKeyPressed === '{arrowleft}' ) {
5894 editor . navigateLeft ( ) ;
59- } else if ( button === '{arrowright}' ) {
95+ } else if ( lastKeyPressed === '{arrowright}' ) {
6096 editor . navigateRight ( ) ;
61- } else if ( button === '{bksp}' ) {
97+ } else if ( lastKeyPressed === '{bksp}' ) {
6298 editor . remove ( 'left' ) ;
63- } else if ( button === '{tab}' ) {
99+ } else if ( lastKeyPressed === '{tab}' ) {
64100 editor . insert ( '\t' ) ;
65- } else if ( [ '+' , '-' , '*' , '/' , '%' , '=>' , '===' , '&&' , '||' ] . includes ( button ) ) {
66- editor . insert ( ' ' + button + ' ' ) ;
101+ } else if ( [ '+' , '-' , '*' , '/' , '%' , '=>' , '===' , '&&' , '||' ] . includes ( lastKeyPressed ) ) {
102+ editor . insert ( ' ' + lastKeyPressed + ' ' ) ;
67103 } else {
68- editor . insert ( button ) ;
104+ editor . insert ( lastKeyPressed ) ;
69105 }
70106 } ;
71107
72108 const keyboardProps = {
73109 onKeyPress : handleKeyPress ,
110+ disableButtonHold : true ,
74111 baseClass : 'simple-keyboard-shortcut' ,
75112 layout : {
76- default : [
77- '{ } ( ) " \' _ => ;' ,
78- '{tab} && || ! < > = ===' ,
79- '+ - * / % // {arrowleft} {arrowright}'
80- ]
113+ default : [ '{ } ( ) " \' _ => ; {tab} && || ! < > = === + - * / % // {arrowleft} {arrowright}' ]
81114 } ,
82115 buttonTheme : [
83116 {
@@ -112,16 +145,13 @@ const MobileKeyboard: React.FC<Props> = props => {
112145 </ button >
113146
114147 < div className = "mobile-keyboard-toggle-container" id = "mobile-keyboard-toggle" >
115- < div className = "mobile-keyboard-container" >
148+ < div
149+ className = "mobile-keyboard-container"
150+ onTouchStart = { handleTouchStart }
151+ onTouchEnd = { handleTouchEnd }
152+ >
116153 < Keyboard { ...keyboardProps } />
117154 </ div >
118- < button
119- className = "mobile-keyboard-row-toggle"
120- onClick = { handleRowToggle }
121- onMouseDown = { handlePreventDefault }
122- >
123- ⤸
124- </ button >
125155 </ div >
126156
127157 < div id = "floating-dragHandle" > :</ div >
0 commit comments