restore selection only if something is selected

This commit is contained in:
nightwing 2012-12-06 02:27:00 +04:00
commit 0cd2faecc5
2 changed files with 26 additions and 25 deletions

View file

@ -34,6 +34,7 @@ define(function(require, exports, module) {
var event = require("../lib/event");
var useragent = require("../lib/useragent");
var dom = require("../lib/dom");
var lang = require("../lib/lang");
var TextInput = function(parentNode, host) {
var text = dom.createElement("textarea");
@ -61,9 +62,17 @@ var TextInput = function(parentNode, host) {
var copied = false;
var pasted = false;
var inCompostion = false;
var resetTimeout = null;
var tempStyle = '';
var isSelectionEmpty = true;
var syncSelection = lang.delayedCall(function() {
resetSelection(isSelectionEmpty);
});
var syncValue = lang.delayedCall(function() {
if (!inCompostion) {
text.value = PLACEHOLDER;
resetSelection();
}
});
resetValue();
if (isFocused())
host.onFocus();
@ -90,7 +99,7 @@ var TextInput = function(parentNode, host) {
}
}
if (useragent.isOldIE) {
var propchangeTimeout;
var syncProperty = lang.delayedCall(onPropertyChange);
var propertyChangeCounter = 0;
var onPropertyChange = function(e){
if (propertyChangeCounter)
@ -99,20 +108,13 @@ var TextInput = function(parentNode, host) {
if (inCompostion || !data || data == PLACEHOLDER)
return;
// can happen either after delete or during insert operation
if (e && data == PLACEHOLDER[0]) {
if (!propchangeTimeout) {
propchangeTimeout = setTimeout(function() {
onPropertyChange();
propchangeTimeout = null;
});
}
return;
}
if (e && data == PLACEHOLDER[0])
return syncProperty.schedule();
sendText(data);
propertyChangeCounter++
propertyChangeCounter++;
resetValue();
propertyChangeCounter--
propertyChangeCounter--;
}
event.addListener(text, "propertychange", onPropertyChange);
@ -132,16 +134,8 @@ var TextInput = function(parentNode, host) {
return;
text.value = PLACEHOLDER;
//http://code.google.com/p/chromium/issues/detail?id=76516
if (!resetTimeout) {
resetTimeout = setTimeout(function(){
resetTimeout = null;
if (inCompostion)
return;
if (useragent.isWebKit)
text.value = PLACEHOLDER;
resetSelection();
});
}
if (useragent.isWebKit)
syncValue.schedule();
};
function resetSelection(isEmpty) {
@ -407,6 +401,13 @@ var TextInput = function(parentNode, host) {
onContextMenuClose();
});
}
host.addEventListener('changeSelection', function(){
if (host.selection.isEmpty() != isSelectionEmpty) {
isSelectionEmpty = !isSelectionEmpty;
syncSelection.schedule();
}
});
};
exports.TextInput = TextInput;

View file

@ -179,7 +179,7 @@ exports.delayedCall = function(fcn, defaultTimeout) {
timer = setTimeout(callback, timeout || defaultTimeout);
};
_self.delay = delayed;
_self.delay = _self;
_self.schedule = function(timeout) {
if (timer == null)
timer = setTimeout(callback, timeout || 0);