fix editor loosing focus on input

This commit is contained in:
nightwing 2012-11-26 23:02:32 +04:00
commit 3c57b1b415

View file

@ -73,35 +73,38 @@ var TextInput = function(parentNode, host) {
var inCompostion = false;
var isSelectionEmpty = true;
var resetTimeout = true;
var tempStyle = '';
function resetValue() {
text.value = PLACEHOLDER;
//http://code.google.com/p/chromium/issues/detail?id=76516
if (useragent.isWebKit)
setTimeout(function(){
if (useragent.isWebKit && !resetTimeout)
resetTimeout = setTimeout(function(){
text.value = PLACEHOLDER;
resetSelection();
resetTimeout = null;
});
};
function resetSelection() {
var selectionStart = isSelectionEmpty ? 2 : 1;
function resetSelection(isEmpty) {
var selectionStart = isEmpty ? 2 : 1;
var selectionEnd = 2;
if (text.setSelectionRange) {
text.setSelectionRange(selectionStart, selectionEnd);
}
// IE8 does not support setSelectionRange
else if (text.createTextRange) {
var range = text.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
// on firefox this throws if textarea is hidden
try {
if (text.setSelectionRange) {
text.setSelectionRange(selectionStart, selectionEnd);
}
// IE8 does not support setSelectionRange
else if (text.createTextRange) {
var range = text.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
} catch(e){}
};
var onSelect = function(e) {
@ -326,6 +329,8 @@ var TextInput = function(parentNode, host) {
(useragent.isIE ? "background:rgba(0, 0, 0, 0.03); opacity:0.1;" : "") + //"background:rgba(250, 0, 0, 0.3); opacity:1;" +
"left:" + (e.clientX - 2) + "px; top:" + (e.clientY - 2) + "px;";
resetSelection(host.selection.isEmpty());
if (e.type != "mousedown")
return;
@ -360,14 +365,6 @@ var TextInput = function(parentNode, host) {
host.textInput.onContextMenu(e);
onContextMenuClose();
});
host.addEventListener('changeSelection', function(){
if (host.selection.isEmpty() != isSelectionEmpty) {
isSelectionEmpty = !isSelectionEmpty;
resetSelection();
}
});
};
exports.TextInput = TextInput;