From c5d03c30ca179ab893508d0aba819a3550111efb Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 11 Oct 2019 14:54:30 -0400 Subject: [PATCH] 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. --- .../discourse/controllers/composer.js.es6 | 4 ++- .../discourse/lib/plugin-api.js.es6 | 26 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6 index cf295cb8707..c5c68d095ce 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 da8ba1cedf6..246470af8cf 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,14 +837,28 @@ 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) { addComposerUploadHandler(extensions, method); } + /** + * 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 *