DEV: Add a plugin API for registering a "beforeSave" on the composer

DEV: Add a plugin API for registering a “beforeSave” on the composer

This allows plugins to perform operations before saves occur, and perhaps reject the post.

diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6
index cf295cb..c5c68d0 100644
--- a/app/assets/javascripts/discourse/controllers/composer.js.es6
+++ b/app/assets/javascripts/discourse/controllers/composer.js.es6
@@ -721,7 +721,9 @@ export default Ember.Controller.extend({
       })
       .catch(error => {
         composer.set("disableDrafts", false);
-        this.appEvents.one("composer:will-open", () => bootbox.alert(error));
+        if (error) {
+          this.appEvents.one("composer:will-open", () => bootbox.alert(error));
+        }
       });
 
     if (
diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
index da8ba1c..246470a 100644
--- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6
+++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6
@@ -46,7 +46,7 @@ import { queryRegistry } from "discourse/widgets/widget";
 import Composer from "discourse/models/composer";
 
 // If you add any methods to the API ensure you bump up this number
-const PLUGIN_API_VERSION = "0.8.33";
+const PLUGIN_API_VERSION = "0.8.34";
 
 class PluginApi {
   constructor(version, container) {
@@ -791,7 +791,7 @@ class PluginApi {
    *
    * Example:
    *
-   * modifySelectKit("topic-footer-mobile-dropdown").appendContent(() => [{
+   * api.modifySelectKit("topic-footer-mobile-dropdown").appendContent(() => [{
    *   name: "discourse",
    *   id: 1
    * }])
@@ -807,7 +807,7 @@ class PluginApi {
    *
    * Example:
    *
-   * addGTMPageChangedCallback( gtmData => gtmData.locale = I18n.currentLocale() )
+   * api.addGTMPageChangedCallback( gtmData => gtmData.locale = I18n.currentLocale() )
    *
    */
   addGTMPageChangedCallback(fn) {
@@ -821,7 +821,7 @@ class PluginApi {
    * Example:
    *
    * // read /discourse/lib/sharing.js.es6 for options
-   * addSharingSource(options)
+   * api.addSharingSource(options)
    *
    */
   addSharingSource(options) {
@@ -837,8 +837,8 @@ class PluginApi {
    *
    * Example:
    *
-   * addComposerUploadHandler(["mp4", "mov"], (file, editor) => {
-   *    console.log("Handling upload for", file.name);
+   * api.addComposerUploadHandler(["mp4", "mov"], (file, editor) => {
+   *   console.log("Handling upload for", file.name);
    * })
    */
   addComposerUploadHandler(extensions, method) {
@@ -846,6 +846,20 @@ class PluginApi {
   }
 
   /**
+   * Registers a "beforeSave" function on the composer. This allows you to
+   * implement custom logic that will happen before the user makes a post.
+   *
+   * Example:
+   *
+   * api.composerBeforeSave(() => {
+   *   console.log("Before saving, do something!");
+   * })
+   */
+  composerBeforeSave(method) {
+    Composer.reopen({ beforeSave: method });
+  }
+
+  /**
    * Adds a field to draft serializer
    *
    * Example:

GitHub sha: c5d03c30

1 Like