diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index d07e1bcde10..07738722eb5 100644 --- a/app/assets/javascripts/discourse/controllers/composer.js.es6 +++ b/app/assets/javascripts/discourse/controllers/composer.js.es6 @@ -70,6 +70,11 @@ export default Discourse.Controller.extend({ if (c) { c.appendText(text); } }, + appendBlockAtCursor: function(text) { + var c = this.get('model'); + if (c) { c.appendText(text, $('#wmd-input').caret(), {block: true}); } + }, + categories: function() { return Discourse.Category.list(); }.property(), diff --git a/app/assets/javascripts/discourse/controllers/quote-button.js.es6 b/app/assets/javascripts/discourse/controllers/quote-button.js.es6 index e694e07de15..d51318f7d93 100644 --- a/app/assets/javascripts/discourse/controllers/quote-button.js.es6 +++ b/app/assets/javascripts/discourse/controllers/quote-button.js.es6 @@ -119,7 +119,7 @@ export default Discourse.Controller.extend({ var quotedText = Discourse.Quote.build(post, buffer); composerOpts.quote = quotedText; if (composerController.get('content.viewOpen') || composerController.get('content.viewDraft')) { - composerController.appendText(quotedText); + composerController.appendBlockAtCursor(quotedText.trim()); } else { composerController.open(composerOpts); } diff --git a/app/assets/javascripts/discourse/lib/caret_position.js b/app/assets/javascripts/discourse/lib/caret_position.js index c08127728f5..e3f94b3597d 100644 --- a/app/assets/javascripts/discourse/lib/caret_position.js +++ b/app/assets/javascripts/discourse/lib/caret_position.js @@ -19,6 +19,10 @@ getCaret = function(el) { clone = null; +$.fn.caret = function(){ + return getCaret(this[0]); +}; + /** This is a jQuery plugin to retrieve the caret position in a textarea diff --git a/app/assets/javascripts/discourse/models/composer.js b/app/assets/javascripts/discourse/models/composer.js index a53519cec05..cc0b90119b7 100644 --- a/app/assets/javascripts/discourse/models/composer.js +++ b/app/assets/javascripts/discourse/models/composer.js @@ -266,8 +266,36 @@ Discourse.Composer = Discourse.Model.extend({ @method appendText @param {String} text the text to append **/ - appendText: function(text) { - this.set('reply', (this.get('reply') || '') + text); + appendText: function(text,position,opts) { + var reply = (this.get('reply') || ''); + position = typeof(position) === "number" ? position : reply.length; + + var before = reply.slice(0, position) || ''; + var after = reply.slice(position) || ''; + + var stripped, i; + + if(opts && opts.block){ + if(before.trim() !== ""){ + stripped = before.replace(/\r/g, ""); + for(i=0; i<2; i++){ + if(stripped[stripped.length - 1 - i] !== "\n"){ + before += "\n"; + position++; + } + } + } + if(after.trim() !== ""){ + stripped = after.replace(/\r/g, ""); + for(i=0; i<2; i++){ + if(stripped[i] !== "\n"){ + after = "\n" + after; + } + } + } + } + + this.set('reply', before + text + after); }, togglePreview: function() { diff --git a/test/javascripts/models/composer_test.js b/test/javascripts/models/composer_test.js index 56ada6026b6..f9b219cff79 100644 --- a/test/javascripts/models/composer_test.js +++ b/test/javascripts/models/composer_test.js @@ -57,6 +57,23 @@ test("appendText", function() { composer.appendText(" world"); equal(composer.get('reply'), "hello world", "it appends text to existing text"); + composer.clearState(); + composer.appendText("a\n\n\n\nb"); + composer.appendText("c",3,{block: true}); + + equal(composer.get("reply"), "a\n\nc\n\nb"); + + composer.clearState(); + composer.appendText("ab"); + composer.appendText("c",1,{block: true}); + + equal(composer.get("reply"), "a\n\nc\n\nb"); + + composer.clearState(); + composer.appendText("\nab"); + composer.appendText("c",0,{block: true}); + + equal(composer.get("reply"), "c\n\nab"); }); test("Title length for regular topics", function() {