From 0604b753108a15bcb508aa54358b1cadbbccf7e8 Mon Sep 17 00:00:00 2001 From: DanyaPostfactum Date: Sat, 20 Oct 2012 19:21:32 +1100 Subject: [PATCH] change selection instead of text.value --- lib/ace/keyboard/textinput.js | 81 +++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/lib/ace/keyboard/textinput.js b/lib/ace/keyboard/textinput.js index 876872f0..7889a09a 100644 --- a/lib/ace/keyboard/textinput.js +++ b/lib/ace/keyboard/textinput.js @@ -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;