From e814f77eaa9196aab760c8800a41e37e599ce262 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Fri, 11 Feb 2022 10:16:06 +1000 Subject: [PATCH] DEV: Allow focusComposer to reply to existing topic (#15896) Another use case for focusComposer() is if the user is already inside a topic but another component (such as the floating chat window) needs to open the composer. This commit also fixes the appendText option to only prepend 2 new lines if there is text before the text to be appended. Follow up 7850ee318fc480d820d256ab01e39cfc046f2d5b --- .../discourse/app/controllers/composer.js | 45 ++++++++++++++----- .../discourse/app/models/composer.js | 6 ++- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/discourse/app/controllers/composer.js b/app/assets/javascripts/discourse/app/controllers/composer.js index cd81ba155cf..81daa0d7dc5 100644 --- a/app/assets/javascripts/discourse/app/controllers/composer.js +++ b/app/assets/javascripts/discourse/app/controllers/composer.js @@ -402,19 +402,46 @@ export default Controller.extend({ // // opts: // - // - fallbackToNewTopic: if true, and there is no draft, the composer will - // be opened with the create_topic action and a new topic draft key + // - topic: if this is present, the composer will be opened with the reply + // action and the current topic key and draft sequence + // - fallbackToNewTopic: if true, and there is no draft and no topic, + // the composer will be opened with the create_topic action and a new + // topic draft key // - insertText: the text to append to the composer once it is opened // - openOpts: this object will be passed to this.open if fallbackToNewTopic is - // true + // true or topic is provided @action focusComposer(opts = {}) { - if (this.get("model.viewOpen")) { + this._openComposerForFocus(opts).then(() => { this._focusAndInsertText(opts.insertText); + }); + }, + + _openComposerForFocus(opts) { + if (this.get("model.viewOpen")) { + return Promise.resolve(); } else { const opened = this.openIfDraft(); - if (!opened && opts.fallbackToNewTopic) { - this.open( + if (opened) { + return Promise.resolve(); + } + + if (opts.topic) { + return this.open( + Object.assign( + { + action: Composer.REPLY, + draftKey: opts.topic.get("draft_key"), + draftSequence: opts.topic.get("draft_sequence"), + topic: opts.topic, + }, + opts.openOpts || {} + ) + ); + } + + if (opts.fallbackToNewTopic) { + return this.open( Object.assign( { action: Composer.CREATE_TOPIC, @@ -422,11 +449,7 @@ export default Controller.extend({ }, opts.openOpts || {} ) - ).then(() => { - this._focusAndInsertText(opts.insertText); - }); - } else if (opened) { - this._focusAndInsertText(opts.insertText); + ); } } }, diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.js index 98612deefe4..68566db1cb1 100644 --- a/app/assets/javascripts/discourse/app/models/composer.js +++ b/app/assets/javascripts/discourse/app/models/composer.js @@ -661,7 +661,11 @@ const Composer = RestModel.extend({ } if (opts && opts.new_line) { - text = "\n\n" + text.trim(); + if (before.length > 0) { + text = "\n\n" + text.trim(); + } else { + text = text.trim(); + } } this.set("reply", before + text + after);