From e7764b586506e080fe63039d49cbc86a41534eff Mon Sep 17 00:00:00 2001 From: David Taylor Date: Fri, 17 Mar 2023 17:16:42 +0000 Subject: [PATCH] Convert composer controller to service with backwards-compatibility shims --- .../discourse-common/addon/resolver.js | 6 + .../app/components/composer-container.hbs | 302 ++++++++++-------- .../app/components/composer-container.js | 6 + .../discourse/app/controllers/composer.js | 19 ++ .../discourse/app/routes/application.js | 8 - .../discourse/app/services/composer.js | 28 +- .../discourse/app/templates/application.hbs | 2 +- 7 files changed, 215 insertions(+), 156 deletions(-) create mode 100644 app/assets/javascripts/discourse/app/components/composer-container.js create mode 100644 app/assets/javascripts/discourse/app/controllers/composer.js diff --git a/app/assets/javascripts/discourse-common/addon/resolver.js b/app/assets/javascripts/discourse-common/addon/resolver.js index 45c0655643..026c4fe0be 100644 --- a/app/assets/javascripts/discourse-common/addon/resolver.js +++ b/app/assets/javascripts/discourse-common/addon/resolver.js @@ -101,6 +101,12 @@ const DEPRECATED_MODULES = new Map( dropFrom: "3.0.0", silent: true, }, + "controller:composer": { + newName: "service:composer", + since: "3.1.0.beta3", + dropFrom: "3.2.0", + silent: true, + }, }) ); diff --git a/app/assets/javascripts/discourse/app/components/composer-container.hbs b/app/assets/javascripts/discourse/app/components/composer-container.hbs index 3afebb8cf9..7491163353 100644 --- a/app/assets/javascripts/discourse/app/components/composer-container.hbs +++ b/app/assets/javascripts/discourse/app/components/composer-container.hbs @@ -1,75 +1,76 @@
- {{#if this.visible}} + {{#if this.composer.visible}} - {{#if this.showFullScreenPrompt}} + {{#if this.composer.showFullScreenPrompt}} {{/if}} - {{#if this.model.viewOpenOrFullscreen}} + {{#if this.composer.model.viewOpenOrFullscreen}}
- {{#unless this.model.viewFullscreen}} + {{#unless this.composer.model.viewFullscreen}}
- {{#unless this.site.mobileView}} - {{#if this.model.unlistTopic}} + {{#unless this.composer.site.mobileView}} + {{#if this.composer.model.unlistTopic}} ({{i18n "composer.unlist"}}) {{/if}} - {{#if this.isWhispering}} - {{#if this.model.noBump}} + {{#if this.composer.isWhispering}} + {{#if this.composer.model.noBump}} {{d-icon "anchor"}} {{/if}} {{/if}} {{/unless}} - {{#if this.canEdit}} + {{#if this.composer.canEdit}}
- {{#unless this.model.viewFullscreen}} - {{#if this.model.canEditTitle}} - {{#if this.model.creatingPrivateMessage}} + {{#unless this.composer.model.viewFullscreen}} + {{#if this.composer.model.canEditTitle}} + {{#if this.composer.model.creatingPrivateMessage}}
- {{#if this.showWarning}} + {{#if this.composer.showWarning}} @@ -154,49 +155,55 @@
- {{#if this.model.showCategoryChooser}} + {{#if this.composer.model.showCategoryChooser}}
- +
{{/if}} - {{#if this.canEditTags}} + {{#if this.composer.canEditTags}} - + {{/if}}
@@ -207,8 +214,8 @@ @name="composer-fields" @connectorTagName="div" @outletArgs={{hash - model=this.model - showPreview=this.showPreview + model=this.composer.model + showPreview=this.composer.showPreview }} /> @@ -219,7 +226,7 @@ @@ -228,53 +235,55 @@
- {{#if this.site.mobileView}} + {{#if this.composer.site.mobileView}} - {{#if this.canEdit}} + {{#if this.composer.canEdit}} {{d-icon "times"}} {{else}} {{d-icon "far-trash-alt"}} {{/if}} {{else}} - {{i18n + {{i18n "close" }} {{/if}} - {{#if this.site.mobileView}} - {{#if this.whisperOrUnlistTopic}} + {{#if this.composer.site.mobileView}} + {{#if this.composer.whisperOrUnlistTopic}} {{d-icon "far-eye-slash"}} {{/if}} - {{#if this.model.noBump}} + {{#if this.composer.model.noBump}} {{d-icon "anchor"}} {{/if}} {{/if}} - {{#if (or this.isUploading this.isProcessingUpload)}} + {{#if + (or this.composer.isUploading this.composer.isProcessingUpload) + }}
- {{#if this.isProcessingUpload}} + {{#if this.composer.isProcessingUpload}} {{loading-spinner size="small"}}{{i18n "upload_selector.processing" }} @@ -282,30 +291,39 @@ {{loading-spinner size="small"}}{{i18n "upload_selector.uploading" }} - {{this.uploadProgress}}% + {{this.composer.uploadProgress}}% {{/if}} - {{#if this.isCancellable}} + {{#if this.composer.isCancellable}} {{d-icon "times"}} {{/if}}
{{/if}} -
- {{#if this.model.draftStatus}} - - {{#if this.model.draftConflictUser}} - {{avatar this.model.draftConflictUser imageSize="small"}} +
+ {{#if this.composer.model.draftStatus}} + + {{#if this.composer.model.draftConflictUser}} + {{avatar + this.composer.model.draftConflictUser + imageSize="small" + }} {{d-icon "user-edit"}} {{else}} {{d-icon "exclamation-triangle"}} {{/if}} - {{#unless this.site.mobileView}} - {{this.model.draftStatus}} + {{#unless this.composer.site.mobileView}} + {{this.composer.model.draftStatus}} {{/unless}} {{/if}} @@ -314,27 +332,27 @@
- {{#if this.site.mobileView}} + {{#if this.composer.site.mobileView}} - {{#if this.allowUpload}} + {{#if this.composer.allowUpload}} - {{d-icon this.uploadIcon}} + {{d-icon this.composer.uploadIcon}} {{/if}} @@ -342,15 +360,15 @@ href class="btn btn-default no-text mobile-preview" title={{i18n "composer.show_preview"}} - {{on "click" this.togglePreview}} + {{on "click" this.composer.togglePreview}} aria-label={{i18n "preview"}} > {{d-icon "desktop"}} - {{#if this.showPreview}} + {{#if this.composer.showPreview}} {{/if}} @@ -371,11 +389,11 @@
{{else}}
- {{#if this.model.createdPost}} + {{#if this.composer.model.createdPost}} {{i18n "composer.saved"}} {{else}} @@ -385,19 +403,19 @@
- {{#if this.model.topic}} + {{#if this.composer.model.topic}} {{d-icon "share"}} - {{html-safe this.draftTitle}} + {{html-safe this.composer.draftTitle}} {{else}} {{i18n "composer.saved_draft"}} {{/if}}
{{/if}} {{/if}} diff --git a/app/assets/javascripts/discourse/app/components/composer-container.js b/app/assets/javascripts/discourse/app/components/composer-container.js new file mode 100644 index 0000000000..826c81d498 --- /dev/null +++ b/app/assets/javascripts/discourse/app/components/composer-container.js @@ -0,0 +1,6 @@ +import Component from "@glimmer/component"; +import { inject as service } from "@ember/service"; + +export default class ComposerContainer extends Component { + @service composer; +} diff --git a/app/assets/javascripts/discourse/app/controllers/composer.js b/app/assets/javascripts/discourse/app/controllers/composer.js new file mode 100644 index 0000000000..73e80522c5 --- /dev/null +++ b/app/assets/javascripts/discourse/app/controllers/composer.js @@ -0,0 +1,19 @@ +import Composer, { + addComposerSaveErrorCallback, + addPopupMenuOptionsCallback, + clearComposerSaveErrorCallback, + clearPopupMenuOptionsCallback, + toggleCheckDraftPopup, +} from "discourse/services/composer"; + +// TODO add deprecation + +export default Composer; + +export { + addComposerSaveErrorCallback, + addPopupMenuOptionsCallback, + clearComposerSaveErrorCallback, + clearPopupMenuOptionsCallback, + toggleCheckDraftPopup, +}; diff --git a/app/assets/javascripts/discourse/app/routes/application.js b/app/assets/javascripts/discourse/app/routes/application.js index e3b54b895a..d4f2dcb236 100644 --- a/app/assets/javascripts/discourse/app/routes/application.js +++ b/app/assets/javascripts/discourse/app/routes/application.js @@ -72,13 +72,6 @@ const ApplicationRoute = DiscourseRoute.extend(OpenComposer, { this.documentTitle.setTitle(tokens.join(" - ")); }, - postWasEnqueued(details) { - showModal("post-enqueued", { - model: details, - title: "review.approval.title", - }); - }, - composePrivateMessage(user, post) { const recipients = user ? user.get("username") : ""; const reply = post @@ -258,7 +251,6 @@ const ApplicationRoute = DiscourseRoute.extend(OpenComposer, { this.render("application"); this.render("user-card", { into: "application", outlet: "user-card" }); this.render("modal", { into: "application", outlet: "modal" }); - this.render("composer", { into: "application", outlet: "composer" }); }, handleShowLogin() { diff --git a/app/assets/javascripts/discourse/app/services/composer.js b/app/assets/javascripts/discourse/app/services/composer.js index 8a590575da..02fb94d0a3 100644 --- a/app/assets/javascripts/discourse/app/services/composer.js +++ b/app/assets/javascripts/discourse/app/services/composer.js @@ -1,5 +1,5 @@ import Composer, { SAVE_ICONS, SAVE_LABELS } from "discourse/models/composer"; -import Controller, { inject as controller } from "@ember/controller"; +import Controller from "@ember/controller"; import EmberObject, { action, computed } from "@ember/object"; import { alias, and, or, reads } from "@ember/object/computed"; import { @@ -96,7 +96,9 @@ export function addComposerSaveErrorCallback(callback) { export default class ComposerController extends Controller { @service router; @service dialog; - @controller("topic") topicController; + @service site; + @service store; + @service appEvents; checkedMessages = false; messageCount = null; @@ -121,6 +123,14 @@ export default class ComposerController extends Controller { @and("model.creatingTopic", "isStaffUser") canUnlistTopic; @or("replyingToWhisper", "model.whisper") isWhispering; + get topicController() { + return getOwner(this).lookup("controller:topic"); + } + + get capabilities() { + return getOwner(this).lookup("capabilities:main"); + } + @on("init") _setupPreview() { const val = this.site.mobileView @@ -574,7 +584,7 @@ export default class ComposerController extends Controller { @action onPopupMenuAction(menuAction) { - this.send(menuAction); + this[menuAction](); } @action @@ -590,7 +600,7 @@ export default class ComposerController extends Controller { @action cancelled() { - this.send("hitEsc"); + this.hitEsc(); } @action @@ -1043,7 +1053,7 @@ export default class ComposerController extends Controller { this.appEvents.trigger("composer:saved"); if (result.responseJson.action === "enqueued") { - this.send("postWasEnqueued", result.responseJson); + this.postWasEnqueued(result.responseJson); if (result.responseJson.pending_post) { let pendingPosts = this.get("topicController.model.pending_posts"); if (pendingPosts) { @@ -1145,6 +1155,14 @@ export default class ComposerController extends Controller { return promise; } + @action + postWasEnqueued(details) { + showModal("post-enqueued", { + model: details, + title: "review.approval.title", + }); + } + // Notify the composer messages controller that a reply has been typed. Some // messages only appear after typing. checkReplyLength() { diff --git a/app/assets/javascripts/discourse/app/templates/application.hbs b/app/assets/javascripts/discourse/app/templates/application.hbs index 8958f774b3..6e12530cbc 100644 --- a/app/assets/javascripts/discourse/app/templates/application.hbs +++ b/app/assets/javascripts/discourse/app/templates/application.hbs @@ -86,7 +86,7 @@ {{outlet "modal"}} - {{outlet "composer"}} + {{#if this.showFooterNav}}