Skip to content

Commit 02059bf

Browse files
Johnwz123JovenSoh
andauthoredApr 11, 2024··
Modify mobile keyboard to be scrollable (#2915)
* Modify mobile keyboard to be scrollable * Mobile Keyboard is now swipeable * Remove mobile-keyboard-row-toggle styles * Fix formatting * Remove console.log statements --------- Co-authored-by: Joven Soh <[email protected]>
1 parent 4cc0204 commit 02059bf

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed
 

‎src/commons/mobileWorkspace/MobileKeyboard.tsx

+62-32
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ type Props = {
88
};
99

1010
const 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>

‎src/styles/_mobileworkspace.scss

+1-6
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ $shadow-light: rgba(0, 0, 0, 0.2);
121121

122122
.hg-row {
123123
height: 40px;
124+
overflow-x: auto;
124125

125126
.hg-button {
126127
background: $cadet-color-2;
@@ -145,12 +146,6 @@ $shadow-light: rgba(0, 0, 0, 0.2);
145146
}
146147
}
147148
}
148-
149-
.mobile-keyboard-row-toggle {
150-
width: 28px;
151-
font-size: 28px;
152-
padding: 0 2px 0 0;
153-
}
154149
}
155150

156151
#floating-dragHandle {

0 commit comments

Comments
 (0)