From e5b307d0569b8657765b651264c98ce958afb95d Mon Sep 17 00:00:00 2001 From: nightwing Date: Thu, 6 Dec 2012 13:53:03 +0400 Subject: [PATCH] do not change selection of blurred textarea --- lib/ace/keyboard/textinput.js | 117 +++++++++++++++++----------------- 1 file changed, 60 insertions(+), 57 deletions(-) diff --git a/lib/ace/keyboard/textinput.js b/lib/ace/keyboard/textinput.js index be5202d3..db2a6468 100644 --- a/lib/ace/keyboard/textinput.js +++ b/lib/ace/keyboard/textinput.js @@ -64,17 +64,69 @@ var TextInput = function(parentNode, host) { var inCompostion = false; var tempStyle = ''; var isSelectionEmpty = true; + + // FOCUS + var isFocused = document.activeElement === text; + event.addListener(text, "blur", function() { + host.onBlur(); + isFocused = false; + }); + event.addListener(text, "focus", function() { + isFocused = true; + host.onFocus(); + resetSelection(); + }); + this.focus = function() { + text.focus(); + }; + this.blur = function() { + text.blur(); + }; + this.isFocused = function() { + return isFocused; + }; + + // modifying selection of blured textarea can focus it (chrome mac/linux) var syncSelection = lang.delayedCall(function() { - resetSelection(isSelectionEmpty); + isFocused && resetSelection(isSelectionEmpty); }); var syncValue = lang.delayedCall(function() { if (!inCompostion) { - text.value = PLACEHOLDER; - resetSelection(); + text.value = PLACEHOLDER; + isFocused && resetSelection(); } }); + + function resetSelection(isEmpty) { + if (inCompostion) + return; + var selectionStart = isEmpty ? 2 : 1; + var selectionEnd = 2; + // on firefox this throws if textarea is hidden + try { + text.setSelectionRange(selectionStart, selectionEnd); + } catch(e){} + }; + + function resetValue() { + if (inCompostion) + return; + text.value = PLACEHOLDER; + //http://code.google.com/p/chromium/issues/detail?id=76516 + if (useragent.isWebKit) + syncValue.schedule(); + }; + + useragent.isWebKit || host.addEventListener('changeSelection', function() { + if (host.selection.isEmpty() != isSelectionEmpty) { + isSelectionEmpty = !isSelectionEmpty; + syncSelection.schedule(); + } + }); + + resetValue(); - if (isFocused()) + if (isFocused) host.onFocus(); @@ -129,26 +181,6 @@ var TextInput = function(parentNode, host) { }); } - function resetValue() { - if (inCompostion) - return; - text.value = PLACEHOLDER; - //http://code.google.com/p/chromium/issues/detail?id=76516 - if (useragent.isWebKit) - syncValue.schedule(); - }; - - function resetSelection(isEmpty) { - if (inCompostion) - return; - var selectionStart = isEmpty ? 2 : 1; - var selectionEnd = 2; - // on firefox this throws if textarea is hidden - try { - text.setSelectionRange(selectionStart, selectionEnd); - } catch(e){} - }; - var onSelect = function(e) { if (cut) { cut = false; @@ -321,35 +353,14 @@ var TextInput = function(parentNode, host) { } - + // COMPOSITION event.addListener(text, "compositionstart", onCompositionStart); - if (useragent.isGecko) { + if (useragent.isGecko) event.addListener(text, "text", onCompositionUpdate); - } - if (useragent.isWebKit) { + if (useragent.isWebKit) event.addListener(text, "keyup", onCompositionUpdate); - } event.addListener(text, "compositionend", onCompositionEnd); - event.addListener(text, "blur", function() { - host.onBlur(); - }); - event.addListener(text, "focus", function() { - host.onFocus(); - resetSelection(); - }); - this.focus = function() { - text.focus(); - }; - this.blur = function() { - text.blur(); - }; - - function isFocused() { - return document.activeElement === text; - } - this.isFocused = isFocused; - // CONTEXTMENU this.getElement = function() { return text; @@ -382,7 +393,7 @@ var TextInput = function(parentNode, host) { event.capture(host.container, move, onContextMenuClose); }; - function onContextMenuClose() { + this.onContextMenuClose = function() { setTimeout(function () { if (tempStyle) { text.style.cssText = tempStyle; @@ -394,7 +405,6 @@ var TextInput = function(parentNode, host) { } }, 0); }; - this.onContextMenuClose = onContextMenuClose; // firefox fires contextmenu event after opening it if (!useragent.isGecko) { @@ -403,13 +413,6 @@ var TextInput = function(parentNode, host) { onContextMenuClose(); }); } - - host.addEventListener('changeSelection', function(){ - if (host.selection.isEmpty() != isSelectionEmpty) { - isSelectionEmpty = !isSelectionEmpty; - syncSelection.schedule(); - } - }); }; exports.TextInput = TextInput;