Initial commit
This commit is contained in:
30
client/src/input/textInput.ts
Normal file
30
client/src/input/textInput.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export function applyTextInput(
|
||||
key: string,
|
||||
currentString: string,
|
||||
cursorPos: number,
|
||||
maxLength: number,
|
||||
): { newString: string; newCursorPos: number } {
|
||||
let newString = currentString;
|
||||
let newCursorPos = cursorPos;
|
||||
const lowerKey = key.toLowerCase();
|
||||
|
||||
if (lowerKey === 'arrowleft') {
|
||||
newCursorPos = Math.max(0, cursorPos - 1);
|
||||
} else if (lowerKey === 'arrowright') {
|
||||
newCursorPos = Math.min(newString.length, cursorPos + 1);
|
||||
} else if (lowerKey === 'backspace') {
|
||||
if (cursorPos > 0) {
|
||||
newString = newString.slice(0, cursorPos - 1) + newString.slice(cursorPos);
|
||||
newCursorPos = cursorPos - 1;
|
||||
}
|
||||
} else if (lowerKey === 'home') {
|
||||
newCursorPos = 0;
|
||||
} else if (lowerKey === 'end') {
|
||||
newCursorPos = newString.length;
|
||||
} else if (key.length === 1 && newString.length < maxLength) {
|
||||
newString = newString.slice(0, cursorPos) + key + newString.slice(cursorPos);
|
||||
newCursorPos = cursorPos + 1;
|
||||
}
|
||||
|
||||
return { newString, newCursorPos };
|
||||
}
|
||||
Reference in New Issue
Block a user