change selection instead of text.value

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

View file

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