From a2a07a98522f43af9520115841741ce4d57b5be9 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 16 May 2014 12:19:56 -0400 Subject: [PATCH] Allow reply as new topic to use the selected text as a quote. --- .../discourse/controllers/topic_controller.js | 40 ++++++++++--------- .../discourse/views/quote_button_view.js | 9 ++++- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/topic_controller.js b/app/assets/javascripts/discourse/controllers/topic_controller.js index b721c52a2ea..2ed6ccb6bb3 100644 --- a/app/assets/javascripts/discourse/controllers/topic_controller.js +++ b/app/assets/javascripts/discourse/controllers/topic_controller.js @@ -217,19 +217,21 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected replyAsNewTopic: function(post) { var composerController = this.get('controllers.composer'), - promise = composerController.open({ - action: Discourse.Composer.CREATE_TOPIC, - draftKey: Discourse.Composer.REPLY_AS_NEW_TOPIC_KEY - }), - postUrl = "" + location.protocol + "//" + location.host + (post.get('url')), - postLink = "[" + (this.get('title')) + "](" + postUrl + ")"; + quoteController = this.get('controllers.quote-button'), + quotedText = Discourse.Quote.build(quoteController.get('post'), quoteController.get('buffer')), + self = this; - promise.then(function() { - Discourse.Post.loadQuote(post.get('id')).then(function(q) { - composerController.appendText(I18n.t("post.continue_discussion", { - postLink: postLink - }) + "\n\n" + q); - }); + quoteController.deselectText(); + + composerController.open({ + action: Discourse.Composer.CREATE_TOPIC, + draftKey: Discourse.Composer.REPLY_AS_NEW_TOPIC_KEY + }).then(function() { + return Em.isEmpty(quotedText) ? Discourse.Post.loadQuote(post.get('id')) : quotedText; + }).then(function(q) { + var postUrl = "" + location.protocol + "//" + location.host + (post.get('url')), + postLink = "[" + self.get('title') + "](" + postUrl + ")"; + composerController.appendText(I18n.t("post.continue_discussion", { postLink: postLink }) + "\n\n" + q); }); }, @@ -423,11 +425,10 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected // Post related methods replyToPost: function(post) { - var composerController = this.get('controllers.composer'); - var quoteController = this.get('controllers.quote-button'); - var quotedText = Discourse.Quote.build(quoteController.get('post'), quoteController.get('buffer')); - - var topic = post ? post.get('topic') : this.get('model'); + var composerController = this.get('controllers.composer'), + quoteController = this.get('controllers.quote-button'), + quotedText = Discourse.Quote.build(quoteController.get('post'), quoteController.get('buffer')), + topic = post ? post.get('topic') : this.get('model'); quoteController.set('buffer', ''); @@ -450,8 +451,9 @@ Discourse.TopicController = Discourse.ObjectController.extend(Discourse.Selected opts.topic = topic; } - var promise = composerController.open(opts); - promise.then(function() { composerController.appendText(quotedText); }); + composerController.open(opts).then(function() { + composerController.appendText(quotedText); + }); } return false; }, diff --git a/app/assets/javascripts/discourse/views/quote_button_view.js b/app/assets/javascripts/discourse/views/quote_button_view.js index ab13c1c249c..dc90ab438e3 100644 --- a/app/assets/javascripts/discourse/views/quote_button_view.js +++ b/app/assets/javascripts/discourse/views/quote_button_view.js @@ -46,8 +46,13 @@ Discourse.QuoteButtonView = Discourse.View.extend({ $(document) .on("mousedown.quote-button", function(e) { view.set('isMouseDown', true); - // we don't want to deselect when we click on the quote button or the reply button - if ($(e.target).hasClass('quote-button') || $(e.target).closest('.create').length > 0) return; + + var $target = $(e.target); + // we don't want to deselect when we click on buttons that use it + if ($target.hasClass('quote-button') || + $target.closest('.create').length || + $target.closest('.reply-new').length) return; + // deselects only when the user left click // (allows anyone to `extend` their selection using shift+click) if (e.which === 1 && !e.shiftKey) controller.deselectText();