From e5765fe1f59377cd89fffb2a30b295236253180f Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 24 Jan 2019 15:19:19 +0100 Subject: [PATCH] FIX: ensures correct scroll position of textarea after autocomplete (#6942) --- .../discourse/components/composer-editor.js.es6 | 6 +++++- .../discourse/components/d-editor.js.es6 | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6 index c84ebc91908..059bddbb547 100644 --- a/app/assets/javascripts/discourse/components/composer-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6 @@ -163,7 +163,11 @@ export default Ember.Component.extend({ includeMentionableGroups: true }), key: "@", - transformComplete: v => v.username || v.name + transformComplete: v => v.username || v.name, + afterComplete() { + // ensures textarea scroll position is correct + Ember.run.scheduleOnce("afterRender", () => $input.blur().focus()); + } }); } diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6 index 04db1ef745b..fca09f4288d 100644 --- a/app/assets/javascripts/discourse/components/d-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/d-editor.js.es6 @@ -385,10 +385,14 @@ export default Ember.Component.extend({ _applyCategoryHashtagAutocomplete() { const siteSettings = this.siteSettings; + const self = this; this.$(".d-editor-input").autocomplete({ template: findRawTemplate("category-tag-autocomplete"), key: "#", + afterComplete() { + self._focusTextArea(); + }, transformComplete(obj) { return obj.text; }, @@ -416,6 +420,7 @@ export default Ember.Component.extend({ key: ":", afterComplete(text) { self.set("value", text); + self._focusTextArea(); }, onKeyUp(text, cp) { @@ -722,7 +727,7 @@ export default Ember.Component.extend({ $textarea.prop("selectionStart", (pre + text).length + 2); $textarea.prop("selectionEnd", (pre + text).length + 2); - Ember.run.scheduleOnce("afterRender", () => $textarea.focus()); + this._focusTextArea(); }, _addText(sel, text, options) { @@ -747,7 +752,8 @@ export default Ember.Component.extend({ $textarea.val(value); $textarea.prop("selectionStart", insert.length); $textarea.prop("selectionEnd", insert.length); - Ember.run.scheduleOnce("afterRender", () => $textarea.focus()); + + this._focusTextArea(); }, _extractTable(text) { @@ -838,6 +844,12 @@ export default Ember.Component.extend({ } }, + // ensures textarea scroll position is correct + _focusTextArea() { + const $textarea = this.$("textarea.d-editor-input"); + Ember.run.scheduleOnce("afterRender", () => $textarea.blur().focus()); + }, + actions: { emoji() { if (this.get("disabled")) { @@ -850,6 +862,7 @@ export default Ember.Component.extend({ emojiSelected(code) { let selected = this._getSelected(); const captures = selected.pre.match(/\B:(\w*)$/); + if (_.isEmpty(captures)) { this._addText(selected, `:${code}:`); } else {