Skip to content

Commit

Permalink
Fix the bug while editing inbetween texts
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaaibu committed Oct 20, 2015
1 parent c2c5cef commit ec095af
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
30 changes: 26 additions & 4 deletions jquery.thaana.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
* jQuery Thaana Keyboard Handler v1.3
* jQuery Thaana Keyboard Handler v1.4
* http://twitter.com/ajaaibu
* requires jquery v1.11.1+
*
* Copyright 2015, Ahmed Ali (ajaaibu)
* Last Updated: 2015/05/12 22:48 PM
* Last Updated: 2015/10/20 05:41 PM
*
* Dual licensed under the MIT or GPL Version 2 licenses.
*
Expand Down Expand Up @@ -38,15 +38,36 @@
var handleKeyboardInput = function(){

var str = $(this).val(),
key = str.substring(str.length-1),
k = key.charCodeAt(0),
key,
selectionMode = false,
selectionStart = 0;

if(this.selectionEnd < str.length && this.selectionEnd > 0){
selectionMode = true;
selectionStart = this.selectionEnd;
key = str.substring(this.selectionEnd-1,this.selectionEnd);
}
else{
key = str.substring(str.length-1);
}

var k = key.charCodeAt(0),
lastLength = $(this).attr('data-length') ? $(this).attr('data-length') : 0,
diff = str.length - lastLength;

if((typeof(keyboards[settings.keyboard][k]) != "undefined") && (diff == 1 || str.length < lastLength)){
if(selectionMode == true){
current = $(this).val().substr(0,this.selectionStart-1) + keyboards[settings.keyboard][k] + $(this).val().substr(this.selectionStart);
}
else{
current = $(this).val().substr(0,str.length-1);
current += keyboards[settings.keyboard][k];
}
$(this).val(current);
if(selectionMode){
this.selectionStart = selectionStart;
this.selectionEnd = selectionStart;
}
}
};

Expand All @@ -55,6 +76,7 @@
$(this).off('input');
}
else{

if(typeof($._data(this,'events')['input']) == "undefined"){
$(this).on('input',handleKeyboardInput);
}
Expand Down
2 changes: 1 addition & 1 deletion jquery.thaana.min.js

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

0 comments on commit ec095af

Please sign in to comment.