From 0a252d77857659d92952a4e1a3e856d6f0fe3b7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Mon, 2 Feb 2015 19:08:28 +0100 Subject: [PATCH] FIX: quote whote post should insert at cursor position --- .../discourse/controllers/composer.js.es6 | 29 ++++++++++++- .../javascripts/discourse/models/composer.js | 30 ------------- app/assets/javascripts/pagedown_custom.js | 1 - test/javascripts/models/composer-test.js.es6 | 43 ------------------- 4 files changed, 28 insertions(+), 75 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index b22a7b739..b1bf16cf2 100644 --- a/app/assets/javascripts/discourse/controllers/composer.js.es6 +++ b/app/assets/javascripts/discourse/controllers/composer.js.es6 @@ -39,7 +39,34 @@ export default DiscourseController.extend({ // Import a quote from the post importQuote: function() { - this.get('model').importQuote(); + var postStream = this.get('topic.postStream'), + postId = this.get('model.post.id'); + + // If there is no current post, use the first post id from the stream + if (!postId && postStream) { + postId = postStream.get('firstPostId'); + } + + // If we're editing a post, fetch the reply when importing a quote + if (this.get('model.editingPost')) { + var replyToPostNumber = this.get('model.post.reply_to_post_number'); + if (replyToPostNumber) { + var replyPost = postStream.get('posts').findBy('post_number', replyToPostNumber); + if (replyPost) { + postId = replyPost.get('id'); + } + } + } + + if (postId) { + this.set('model.loading', true); + var composer = this; + return Discourse.Post.load(postId).then(function(post) { + var quote = Discourse.Quote.build(post, post.get("raw")) + composer.appendBlockAtCursor(quote); + composer.set('model.loading', false); + }); + } }, cancel: function() { diff --git a/app/assets/javascripts/discourse/models/composer.js b/app/assets/javascripts/discourse/models/composer.js index d14680808..3103c80b1 100644 --- a/app/assets/javascripts/discourse/models/composer.js +++ b/app/assets/javascripts/discourse/models/composer.js @@ -328,36 +328,6 @@ Discourse.Composer = Discourse.Model.extend({ Discourse.KeyValueStore.set({ key: 'composer.showPreview', value: this.get('showPreview') }); }, - importQuote: function() { - var postStream = this.get('topic.postStream'), - postId = this.get('post.id'); - - if (!postId && postStream) { - postId = postStream.get('firstPostId'); - } - - // If we're editing a post, fetch the reply when importing a quote - if (this.get('editingPost')) { - var replyToPostNumber = this.get('post.reply_to_post_number'); - if (replyToPostNumber) { - var replyPost = postStream.get('posts').findBy('post_number', replyToPostNumber); - if (replyPost) { - postId = replyPost.get('id'); - } - } - } - - // If there is no current post, use the post id from the stream - if (postId) { - this.set('loading', true); - var composer = this; - return Discourse.Post.load(postId).then(function(post) { - composer.appendText(Discourse.Quote.build(post, post.get('raw'))); - composer.set('loading', false); - }); - } - }, - /* Open a composer diff --git a/app/assets/javascripts/pagedown_custom.js b/app/assets/javascripts/pagedown_custom.js index e435ebf16..8dec51681 100644 --- a/app/assets/javascripts/pagedown_custom.js +++ b/app/assets/javascripts/pagedown_custom.js @@ -4,7 +4,6 @@ window.PagedownCustom = { id: 'wmd-quote-post', description: I18n.t("composer.quote_post_title"), execute: function() { - // AWFUL but I can't figure out how to call a controller method from outside our app return Discourse.__container__.lookup('controller:composer').send('importQuote'); } } diff --git a/test/javascripts/models/composer-test.js.es6 b/test/javascripts/models/composer-test.js.es6 index afcb156cd..cf359e997 100644 --- a/test/javascripts/models/composer-test.js.es6 +++ b/test/javascripts/models/composer-test.js.es6 @@ -129,19 +129,6 @@ test("Title length for private messages", function() { ok(composer.get('titleLengthValid'), "in the range is okay"); }); -test('importQuote with no data', function() { - sandbox.stub(Discourse.Post, 'load'); - var composer = Discourse.Composer.create(); - composer.importQuote(); - blank(composer.get('reply'), 'importing with no topic adds nothing'); - ok(!Discourse.Post.load.calledOnce, "load is not called"); - - composer = Discourse.Composer.create({topic: Discourse.Topic.create()}); - composer.importQuote(); - blank(composer.get('reply'), 'importing with no posts in a topic adds nothing'); - ok(!Discourse.Post.load.calledOnce, "load is not called"); -}); - test('editingFirstPost', function() { var composer = Discourse.Composer.create(); ok(!composer.get('editingFirstPost'), "it's false by default"); @@ -155,36 +142,6 @@ test('editingFirstPost', function() { }); -asyncTestDiscourse('importQuote with a post', function() { - expect(1); - - sandbox.stub(Discourse.Post, 'load').withArgs(123).returns(new Ember.RSVP.Promise(function (resolve) { - resolve(Discourse.Post.create({raw: "let's quote"})); - })); - - var composer = Discourse.Composer.create({post: Discourse.Post.create({id: 123})}); - composer.importQuote().then(function () { - start(); - ok(composer.get('reply').indexOf("let's quote") > -1, "it quoted the post"); - }); -}); - -asyncTestDiscourse('importQuote with no post', function() { - expect(1); - - sandbox.stub(Discourse.Post, 'load').withArgs(4).returns(new Ember.RSVP.Promise(function (resolve) { - resolve(Discourse.Post.create({raw: 'quote me'})); - })); - - var composer = Discourse.Composer.create({topic: Discourse.Topic.create()}); - composer.set('topic.postStream.stream', [4, 5]); - composer.importQuote().then(function () { - start(); - ok(composer.get('reply').indexOf('quote me') > -1, "it contains the word quote me"); - }); - -}); - test('clearState', function() { var composer = Discourse.Composer.create({ originalText: 'asdf',