change selection instead of text.value

This commit is contained in:
DanyaPostfactum 2012-10-20 19:21:32 +11:00 committed by nightwing
commit 0604b75310

View file

@ -53,22 +53,54 @@ var TextInput = function(parentNode, host) {
text.style.top = "-2em";
parentNode.insertBefore(text, parentNode.firstChild);
var PLACEHOLDER = useragent.isIE ? "\x01" : "\x00";
var PLACEHOLDER = useragent.isIE || useragent.isOpera ? "\x01" : "\x00";
resetValue();
if (isFocused())
host.onFocus();
// Somehow fixes problem with firing onpropertychange on first typed char
if (useragent.isOldIE) {
resetSelection();
resetValue();
setTimeout(resetSelection);
}
var pasted = false;
var inCompostion = false;
var isSelectionEmpty = true;
var tempStyle = '';
host.addEventListener('changeSelection', function(){
if (host.selection.isEmpty() != isSelectionEmpty) {
isSelectionEmpty = !isSelectionEmpty;
text.value = isSelectionEmpty ? '' : PLACEHOLDER;
text.select();
function resetValue() {
//http://code.google.com/p/chromium/issues/detail?id=76516
if (!useragent.isWebKit)
text.value = PLACEHOLDER;
else
setTimeout(function(){
text.value = PLACEHOLDER;
});
};
function resetSelection() {
var selectionStart = isSelectionEmpty ? 1 : 0;
var selectionEnd = 1;
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();
}
};
var onInput = function(e) {
if (inCompostion)
@ -76,24 +108,19 @@ var TextInput = function(parentNode, host) {
if (pasted) {
var data = text.value;
resetValue();
if (data)
host.onPaste(data);
pasted = false;
} else {
var data = text.value;
if (data)
host.onTextInput(data);
else
host.onDelete();
return;
}
text.value = "";
//http://code.google.com/p/chromium/issues/detail?id=76516
if (useragent.isWebKit)
setTimeout(function(){
text.blur();
text.focus();
});
var data = text.value.substring(isSelectionEmpty ? 1 : 0);
resetValue();
if (data)
host.onTextInput(data);
else
host.onDelete();
};
var onCompositionStart = function(e) {
@ -173,6 +200,7 @@ var TextInput = function(parentNode, host) {
event.preventDefault(e);
}
else {
text.value = "";
pasted = true;
}
};
@ -238,11 +266,10 @@ var TextInput = function(parentNode, host) {
event.addListener(text, "focus", function() {
host.onFocus();
text.select();
resetSelection();
});
this.focus = function() {
text.select();
text.focus();
};
@ -300,8 +327,16 @@ var TextInput = function(parentNode, host) {
if (!useragent.isGecko)
event.addListener(text, "contextmenu", function(e) {
host.textInput.onContextMenu(e);
onContextMenuClose()
onContextMenuClose();
});
host.addEventListener('changeSelection', function(){
if (host.selection.isEmpty() != isSelectionEmpty) {
isSelectionEmpty = !isSelectionEmpty;
resetSelection();
}
});
};
exports.TextInput = TextInput;