Skip to content

Instantly share code, notes, and snippets.

@swapnilshrikhande
Last active August 29, 2015 14:10
Show Gist options
  • Save swapnilshrikhande/ef93d119a268b2d1cae4 to your computer and use it in GitHub Desktop.
Save swapnilshrikhande/ef93d119a268b2d1cae4 to your computer and use it in GitHub Desktop.
group Delete On Backspace
var BKSPKEY = 8;
handleCommandDelete = function(event,component){
var messageBox = $(component);
var valueText = messageBox.val();
//get current cursor position
if(event.keyCode != BKSPKEY){
return true;
}
var positionObj = updateCursorPosition(messageBox.get(0)) ;
var startIndex = positionObj.start;
var endIndex = positionObj.end;
//if selection then backspace will delete the selection
if(startIndex!=endIndex){
return true;
}
//get back to last space
for(var index=endIndex;valueText[index]!=' ' && index>=0;index--);
startIndex=index;
var keyword = valueText.substring(startIndex+1, endIndex);
// console.log('valueText',valueText);
// console.log('keyword',keyword);
//check if keyword is special command just select the keyword
createSelection(messageBox.get(0),startIndex+1,endIndex);
//if stopPropagation method supported
if (event && event.stopPropagation){
event.stopPropagation();
}
else{
event.cancelBubble=true
}
return false;
return true;
};
var createSelection = function(field, start, end) {
if( field.createTextRange ) {
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end);
selRange.select();
field.focus();
} else if( field.setSelectionRange ) {
field.focus();
field.setSelectionRange(start, end);
} else if( typeof field.selectionStart != 'undefined' ) {
field.selectionStart = start;
field.selectionEnd = end;
field.focus();
}
}
var updateCursorPosition = function(textBox){
var positionObj = getCursorPositionInTextBox(textBox);
$(".mainForm div.message-entry").attr('start',positionObj.start);
$(".mainForm div.message-entry").attr('end',positionObj.end);
return positionObj;
};
var getCursorPositionInTextBox = function(el) {
/*if(!jqueryElem || jqueryElem.length==0){
return null;
}
el = jqueryElem[0];*/
var start = 0, end = 0, normalizedValue, range,
textInputRange, len, endRange;
if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") {
start = el.selectionStart;
end = el.selectionEnd;
} else {
range = document.selection.createRange();
if (range && range.parentElement() == el) {
len = el.value.length;
normalizedValue = el.value.replace(/\r\n/g, "\n");
// Create a working TextRange that lives only in the input
textInputRange = el.createTextRange();
textInputRange.moveToBookmark(range.getBookmark());
// Check if the start and end of the selection are at the very end
// of the input, since moveStart/moveEnd doesn't return what we want
// in those cases
endRange = el.createTextRange();
endRange.collapse(false);
if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) {
start = end = len;
} else {
start = -textInputRange.moveStart("character", -len);
start += normalizedValue.slice(0, start).split("\n").length - 1;
if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) {
end = len;
} else {
end = -textInputRange.moveEnd("character", -len);
end += normalizedValue.slice(0, end).split("\n").length - 1;
}
}
}
}
return {
start: start,
end: end
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment