@@ -8,10 +8,12 @@ type Props = {
8
8
} ;
9
9
10
10
const MobileKeyboard : React . FC < Props > = props => {
11
- const [ isKeyboardShown , setIsKeyoardShown ] = React . useState ( false ) ;
11
+ const [ isKeyboardShown , setIsKeyboardShown ] = React . useState ( false ) ;
12
12
const [ buttonContent , setButtonContent ] = React . useState ( 'ᐸ' ) ;
13
13
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 } ) ;
15
17
16
18
const onDrag : DraggableEventHandler = (
17
19
e : DraggableEvent ,
@@ -31,53 +33,84 @@ const MobileKeyboard: React.FC<Props> = props => {
31
33
document . getElementById ( 'mobile-floating-toggle' ) ! . style . setProperty ( 'width' , '42px' ) ;
32
34
document . getElementById ( 'mobile-floating-toggle' ) ! . style . setProperty ( 'opacity' , '0.6' ) ;
33
35
setButtonContent ( 'ᐸ' ) ;
34
- setIsKeyoardShown ( false ) ;
36
+ setIsKeyboardShown ( false ) ;
35
37
} else {
36
38
//do showing
37
39
document . getElementById ( 'mobile-keyboard-toggle' ) ! . style . setProperty ( 'display' , 'flex' ) ;
38
40
document . getElementById ( 'mobile-floating-toggle' ) ! . style . setProperty ( 'width' , '99%' ) ;
39
41
document . getElementById ( 'mobile-floating-toggle' ) ! . style . setProperty ( 'opacity' , '1' ) ;
40
42
setButtonContent ( 'ᐳ' ) ;
41
- setIsKeyoardShown ( true ) ;
43
+ setIsKeyboardShown ( true ) ;
42
44
}
43
45
} ;
44
46
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
-
52
47
const handleKeyPress = ( button : string ) => {
53
48
if ( ! props . targetKeyboardInput ) {
54
49
return ;
55
50
}
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}' ) {
58
94
editor . navigateLeft ( ) ;
59
- } else if ( button === '{arrowright}' ) {
95
+ } else if ( lastKeyPressed === '{arrowright}' ) {
60
96
editor . navigateRight ( ) ;
61
- } else if ( button === '{bksp}' ) {
97
+ } else if ( lastKeyPressed === '{bksp}' ) {
62
98
editor . remove ( 'left' ) ;
63
- } else if ( button === '{tab}' ) {
99
+ } else if ( lastKeyPressed === '{tab}' ) {
64
100
editor . insert ( '\t' ) ;
65
- } else if ( [ '+' , '-' , '*' , '/' , '%' , '=>' , '===' , '&&' , '||' ] . includes ( button ) ) {
66
- editor . insert ( ' ' + button + ' ' ) ;
101
+ } else if ( [ '+' , '-' , '*' , '/' , '%' , '=>' , '===' , '&&' , '||' ] . includes ( lastKeyPressed ) ) {
102
+ editor . insert ( ' ' + lastKeyPressed + ' ' ) ;
67
103
} else {
68
- editor . insert ( button ) ;
104
+ editor . insert ( lastKeyPressed ) ;
69
105
}
70
106
} ;
71
107
72
108
const keyboardProps = {
73
109
onKeyPress : handleKeyPress ,
110
+ disableButtonHold : true ,
74
111
baseClass : 'simple-keyboard-shortcut' ,
75
112
layout : {
76
- default : [
77
- '{ } ( ) " \' _ => ;' ,
78
- '{tab} && || ! < > = ===' ,
79
- '+ - * / % // {arrowleft} {arrowright}'
80
- ]
113
+ default : [ '{ } ( ) " \' _ => ; {tab} && || ! < > = === + - * / % // {arrowleft} {arrowright}' ]
81
114
} ,
82
115
buttonTheme : [
83
116
{
@@ -112,16 +145,13 @@ const MobileKeyboard: React.FC<Props> = props => {
112
145
</ button >
113
146
114
147
< 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
+ >
116
153
< Keyboard { ...keyboardProps } />
117
154
</ div >
118
- < button
119
- className = "mobile-keyboard-row-toggle"
120
- onClick = { handleRowToggle }
121
- onMouseDown = { handlePreventDefault }
122
- >
123
- ⤸
124
- </ button >
125
155
</ div >
126
156
127
157
< div id = "floating-dragHandle" > :</ div >
0 commit comments