From 1721ca8a0cfba112440a18011e1dcc8004fff4d2 Mon Sep 17 00:00:00 2001 From: nightwing Date: Thu, 13 Nov 2014 23:57:10 +0400 Subject: [PATCH 1/3] fix unwanted scrolling into view of hidden textarea --- lib/ace/keyboard/textinput.js | 9 ++++++++- lib/ace/mouse/mouse_handler.js | 6 ++---- lib/ace/virtual_renderer.js | 14 ++++++++------ 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/ace/keyboard/textinput.js b/lib/ace/keyboard/textinput.js index 1e6b057e..c0902e27 100644 --- a/lib/ace/keyboard/textinput.js +++ b/lib/ace/keyboard/textinput.js @@ -75,7 +75,14 @@ var TextInput = function(parentNode, host) { host.onFocus(e); resetSelection(); }); - this.focus = function() { text.focus(); }; + this.focus = function() { + text.style.position = "fixed"; + text.style.top = "-10000000px"; + text.focus(); + setTimeout(function() { + text.style.position = ""; + }, 0); + }; this.blur = function() { text.blur(); }; this.isFocused = function() { return isFocused; diff --git a/lib/ace/mouse/mouse_handler.js b/lib/ace/mouse/mouse_handler.js index 9cf26721..d032e687 100644 --- a/lib/ace/mouse/mouse_handler.js +++ b/lib/ace/mouse/mouse_handler.js @@ -47,10 +47,8 @@ var MouseHandler = function(editor) { new DefaultGutterHandler(this); new DragdropHandler(this); - var focusEditor = function(e) { - if (!editor.isFocused() && editor.textInput) - editor.textInput.moveToMouse(e); - editor.focus() + var focusEditor = function(e) { + editor.focus(); }; var mouseTarget = editor.renderer.getMouseEventTarget(); diff --git a/lib/ace/virtual_renderer.js b/lib/ace/virtual_renderer.js index 820b054d..99f41731 100644 --- a/lib/ace/virtual_renderer.js +++ b/lib/ace/virtual_renderer.js @@ -634,9 +634,12 @@ var VirtualRenderer = function(container, theme) { var posLeft = this.$cursorLayer.$pixelPos.left; posTop -= config.offset; + var style = this.textarea.style; var h = this.lineHeight; - if (posTop < 0 || posTop > config.height - h) + if (posTop < 0 || posTop > config.height - h) { + style.top = style.left = "0"; return; + } var w = this.characterWidth; if (this.$composition) { @@ -649,11 +652,10 @@ var VirtualRenderer = function(container, theme) { posLeft = this.$size.scrollerWidth - w; posLeft += this.gutterWidth; - - this.textarea.style.height = h + "px"; - this.textarea.style.width = w + "px"; - this.textarea.style.left = Math.min(posLeft, this.$size.scrollerWidth - w) + "px"; - this.textarea.style.top = Math.min(posTop, this.$size.height - h) + "px"; + style.height = h + "px"; + style.width = w + "px"; + style.left = Math.min(posLeft, this.$size.scrollerWidth - w) + "px"; + style.top = Math.min(posTop, this.$size.height - h) + "px"; }; /** From 7326de2a486cae5a5246c18ff7ace53de28bf3d5 Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 14 Nov 2014 00:56:28 +0400 Subject: [PATCH 2/3] reset cached scroll values when resizing this is needed since scrollTop of scrollbar element can be reset to 0 when ace is moved in the document. --- lib/ace/virtual_renderer.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/ace/virtual_renderer.js b/lib/ace/virtual_renderer.js index 99f41731..ae438ae8 100644 --- a/lib/ace/virtual_renderer.js +++ b/lib/ace/virtual_renderer.js @@ -375,6 +375,9 @@ var VirtualRenderer = function(container, theme) { if (this.resizing) this.resizing = 0; + // reset cached values on scrollbars, needs to be removed when switching to non-native scrollbars + // see https://github.com/ajaxorg/ace/issues/2195 + this.scrollBarV.scrollLeft = this.scrollBarV.scrollTop = null; }; this.$updateCachedSize = function(force, gutterWidth, width, height) { From bf7eba22535aed74e4fff3d60aeafff8ada39b1c Mon Sep 17 00:00:00 2001 From: nightwing Date: Fri, 14 Nov 2014 01:27:59 +0400 Subject: [PATCH 3/3] fix focusing window with click on ie --- lib/ace/mouse/mouse_handler.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/ace/mouse/mouse_handler.js b/lib/ace/mouse/mouse_handler.js index d032e687..6b331eee 100644 --- a/lib/ace/mouse/mouse_handler.js +++ b/lib/ace/mouse/mouse_handler.js @@ -48,6 +48,10 @@ var MouseHandler = function(editor) { new DragdropHandler(this); var focusEditor = function(e) { + // because we have to call event.preventDefault() any window on ie and iframes + // on other browsers do not get focus, so we have to call window.focus() here + if (!document.hasFocus || !document.hasFocus()) + window.focus(); editor.focus(); };