From 23163422e677df8cd1a80d02e8f9ab2d70ac97f1 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 8 Dec 2014 14:22:54 +1100 Subject: [PATCH] correct positioning when emoji is added --- .../discourse/controllers/composer.js.es6 | 24 +++++++++++++++---- .../javascripts/discourse/models/composer.js | 2 ++ .../emoji/assets/javascripts/emoji-toolbar.js | 4 ++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index 9ff339ace75..b15f3ec9e49 100644 --- a/app/assets/javascripts/discourse/controllers/composer.js.es6 +++ b/app/assets/javascripts/discourse/controllers/composer.js.es6 @@ -75,12 +75,28 @@ export default DiscourseController.extend({ appendText: function(text, opts) { var c = this.get('model'); - if (c) { c.appendText(text, undefined , opts); } + if (c) { + var wmd = $('#wmd-input'); + var position = opts.position === "cursor" ? wmd.caret() : wmd.val().length; + + var caret = c.appendText(text, position, opts); + Em.run.next(function(){ + Discourse.Utilities.setCaretPosition(wmd[0], caret); + }); + } }, - appendBlockAtCursor: function(text) { - var c = this.get('model'); - if (c) { c.appendText(text, $('#wmd-input').caret(), {block: true}); } + appendTextAtCursor: function(text, opts) { + opts = opts || {}; + opts.position = "cursor"; + this.appendText(text, opts); + }, + + appendBlockAtCursor: function(text, opts) { + opts = opts || {}; + opts.position = "cursor"; + opts.block = true; + this.appendText(text, opts); }, categories: function() { diff --git a/app/assets/javascripts/discourse/models/composer.js b/app/assets/javascripts/discourse/models/composer.js index 312ab7dd0eb..2d9523cace3 100644 --- a/app/assets/javascripts/discourse/models/composer.js +++ b/app/assets/javascripts/discourse/models/composer.js @@ -312,6 +312,8 @@ Discourse.Composer = Discourse.Model.extend({ } this.set('reply', before + text + after); + + return before.length + text.length; }, togglePreview: function() { diff --git a/plugins/emoji/assets/javascripts/emoji-toolbar.js b/plugins/emoji/assets/javascripts/emoji-toolbar.js index 7d72e9f6b52..036a3790660 100644 --- a/plugins/emoji/assets/javascripts/emoji-toolbar.js +++ b/plugins/emoji/assets/javascripts/emoji-toolbar.js @@ -33,7 +33,6 @@ var renderPage = Handlebars.compile( var closeSelector = function(){ $('.emoji-modal, .emoji-modal-wrapper').remove(); $('body, textarea').off('keydown.emoji'); - $('#wmd-input').focus(); }; var showSelector = function(){ @@ -61,8 +60,9 @@ var showSelector = function(){ var composerController = Discourse.__container__.lookup('controller:composer'); $('.emoji-page a').click(function(){ - composerController.appendText(":" + $(this).attr('title') + ":", {space: true}); + composerController.appendTextAtCursor(":" + $(this).attr('title') + ":", {space: true}); closeSelector(); + return false; }); $('body, textarea').on('keydown.emoji', function(e){