Skip to content

Commit

Permalink
address Android 12 Google Keyboard issue
Browse files Browse the repository at this point in the history
  • Loading branch information
aharen committed Apr 13, 2022
1 parent 27a0c51 commit a33dc43
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
23 changes: 11 additions & 12 deletions ThaanaKeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ var ThaanaKeyboard = /** @class */ (function () {
function ThaanaKeyboard(className, autoStart) {
if (className === void 0) { className = '.thaana-keyboard'; }
if (autoStart === void 0) { autoStart = true; }
this.selectionChange = function () {
var activeElement = document.activeElement;
activeElement.dataset.start = activeElement.selectionStart;
activeElement.dataset.end = activeElement.selectionEnd;
};
this.className = className;
if (true === autoStart) {
this.run();
Expand All @@ -12,23 +17,18 @@ var ThaanaKeyboard = /** @class */ (function () {
var inputs = document.querySelectorAll(this.className);
inputs.forEach(function (input) { return input.addEventListener('beforeinput', function (e) { return _this.beforeInputEvent(e); }); });
inputs.forEach(function (input) { return input.addEventListener('input', function (e) { return _this.inputEvent(e); }); });
document.addEventListener('selectionchange', _this.selectionChange )
document.addEventListener('selectionchange', this.selectionChange);
};
ThaanaKeyboard.prototype.beforeInputEvent = function (event) {
var e = event;
var t = e.target;
if (-1 !== ['insertCompositionText', 'insertText'].indexOf(e.inputType)) {
this.latinChar = e.data;
this.latinChar = e.data.charAt(e.data.length - 1);
this.char = this.getChar(this.latinChar);
this.oldValue = t.value;
}
return;
};
ThaanaKeyboard.prototype.selectionChange = function () {
const activeElement = document.activeElement
activeElement.dataset.start = activeElement.selectionStart
activeElement.dataset.end = activeElement.selectionEnd
};
ThaanaKeyboard.prototype.inputEvent = function (event) {
var e = event;
var t = e.target;
Expand All @@ -44,12 +44,11 @@ var ThaanaKeyboard = /** @class */ (function () {
// remove the original latin char
target.value = ''; // reset the value first
target.value = this.oldValue.split(this.latinChar).join('');

//remove selection
const selectionStart = target.dataset.start
const selectionEnd = target.dataset.end
if( ( selectionEnd - selectionStart ) > 0 )
target.value = target.value.substring(0, selectionStart) + target.value.substring(selectionEnd)
var selectionStart = Number(target.dataset.start);
var selectionEnd = Number(target.dataset.end);
if ((selectionEnd - selectionStart) > 0)
target.value = target.value.substring(0, selectionStart) + target.value.substring(selectionEnd);
// recreate text with newChar
var newValue = target.value.substring(0, cursorStart - 1);
newValue += this.char;
Expand Down
2 changes: 1 addition & 1 deletion ThaanaKeyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ThaanaKeyboard {
const t = e.target as HTMLInputElement

if (-1 !== ['insertCompositionText', 'insertText'].indexOf(e.inputType)) {
this.latinChar = e.data
this.latinChar = e.data.charAt(e.data.length - 1);
this.char = this.getChar(this.latinChar)
this.oldValue = t.value
}
Expand Down
2 changes: 1 addition & 1 deletion dist/ThaanaKeyboard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a33dc43

Please sign in to comment.