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.
This commit is contained in:
Robin Ward 2019-10-11 14:54:30 -04:00
parent ee4369f972
commit c5d03c30ca
2 changed files with 23 additions and 7 deletions

View File

@ -721,7 +721,9 @@ export default Ember.Controller.extend({
})
.catch(error => {
composer.set("disableDrafts", false);
if (error) {
this.appEvents.one("composer:will-open", () => bootbox.alert(error));
}
});
if (

View File

@ -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,7 +837,7 @@ class PluginApi {
*
* Example:
*
* addComposerUploadHandler(["mp4", "mov"], (file, editor) => {
* api.addComposerUploadHandler(["mp4", "mov"], (file, editor) => {
* console.log("Handling upload for", file.name);
* })
*/
@ -845,6 +845,20 @@ class PluginApi {
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
*