Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: a natural mode for an Android Chrome devices #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions src/input.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {InputService} from "./input.service";
import {CurrencyMaskInputMode} from "./currency-mask.config";

export class InputHandler {

Expand All @@ -19,40 +20,47 @@ export class InputHandler {
}

handleInput(event: any): void {
let keyCode = this.inputService.rawValue.charCodeAt(this.inputService.rawValue.length - 1);
let keyPressed = event.data;
let rawValueLength = this.inputService.rawValue.length;
let rawValueSelectionEnd = this.inputService.inputSelection.selectionEnd;
let storedRawValueLength = this.inputService.storedRawValue.length;
this.inputService.rawValue = this.inputService.storedRawValue;
let inputSelection = this.inputService.inputSelection;

if (rawValueLength != rawValueSelectionEnd || Math.abs(rawValueLength - storedRawValueLength) != 1) {
this.setCursorPosition(event);
return;
this.setInputFieldValueAndResetCursor(this.inputService.storedRawValue);

if (this.isFinancialMode()) {
if (rawValueLength != inputSelection.selectionEnd ||
Math.abs(rawValueLength - storedRawValueLength) != 1) {
this.setCursorPositionToEnd(event);
this.fixateCursorAfterZeroTimeout();
return;
}
} else {
this.adjustCursorForNaturalMode(keyPressed === null, inputSelection.selectionStart);
}

if (rawValueLength < storedRawValueLength) {
this.inputService.removeNumber(8);
}

if (rawValueLength > storedRawValueLength) {
switch (keyCode) {
case 43:
switch (keyPressed) {
case "+":
this.inputService.changeToPositive();
break;
case 45:
case "-":
this.inputService.changeToNegative();
break;
default:
if (!this.inputService.canInputMoreNumbers) {
return;
}

this.inputService.addNumber(keyCode);
this.inputService.addNumber(keyPressed.charCodeAt(0));
break;
}
}

this.setCursorPosition(event);
this.fixateCursorAfterZeroTimeout();
this.onModelChange(this.inputService.value);
}

Expand Down Expand Up @@ -142,9 +150,30 @@ export class InputHandler {
this.inputService.value = value;
}

private setCursorPosition(event: any): void {
setTimeout(function () {
event.target.setSelectionRange(event.target.value.length, event.target.value.length);
}, 0);
private adjustCursorForNaturalMode(isRemoval: boolean, savedPosition: number): void {
if (isRemoval) {
this.inputService.setCursorPosition(savedPosition + 1);
} else {
this.inputService.setCursorPosition(savedPosition - 1);
}
}

private setInputFieldValueAndResetCursor(rawValue) {
this.inputService.rawValue = rawValue;
}

private fixateCursorAfterZeroTimeout(): void {
let position = this.inputService.inputSelection.selectionStart;
setTimeout(() =>
this.inputService.setCursorPosition(position),
0);
}

private isFinancialMode(): boolean {
return this.inputService.getOptions().inputMode === CurrencyMaskInputMode.FINANCIAL;
}

private setCursorPositionToEnd(event: any): void {
event.target.setSelectionRange(event.target.value.length, event.target.value.length);
}
}
12 changes: 10 additions & 2 deletions src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export class InputService {
selectionEnd = Math.min(suffixStart, Math.max(selectionEnd, prefix.length));
selectionStart = Math.min(suffixStart, Math.max(selectionStart, prefix.length));

// Check if selection was entirely in the prefix or suffix.
// Check if selection was entirely in the prefix or suffix.
if (selectionStart === selectionEnd &&
this.inputSelection.selectionStart !== this.inputSelection.selectionEnd) {
this.updateFieldValue(selectionStart);
Expand All @@ -226,7 +226,7 @@ export class InputService {
}

let shiftSelection = 0;
let insertChars = '';
let insertChars = '';
if (selectionEnd === selectionStart) {
if (keyCode == 8) {
if (selectionStart <= prefix.length) {
Expand Down Expand Up @@ -303,6 +303,14 @@ export class InputService {
return this.options.nullable;
}

getOptions() {
return this.options;
}

setCursorPosition(position) {
this.inputManager.setCursorAt(position);
}

get canInputMoreNumbers(): boolean {
return this.inputManager.canInputMoreNumbers;
}
Expand Down