DEV: Expose Composer.serializeOnUpdate in client side plugin api.

Also expose `Composer.serializeOnCreate` as well.
This commit is contained in:
Guo Xiang Tan 2020-05-14 14:27:55 +08:00
parent fdea969f14
commit a1a798ac7d
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
2 changed files with 53 additions and 4 deletions

View File

@ -55,7 +55,7 @@ import { on } from "@ember/object/evented";
import KeyboardShortcuts from "discourse/lib/keyboard-shortcuts";
// If you add any methods to the API ensure you bump up this number
const PLUGIN_API_VERSION = "0.8.43";
const PLUGIN_API_VERSION = "0.9.0";
class PluginApi {
constructor(version, container) {
@ -956,6 +956,36 @@ class PluginApi {
Composer.serializeToDraft(fieldName, property);
}
/**
* Adds a field to composer create serializer
*
* Example:
*
* api.serializeOnCreate('key_set_in_model', 'field_name_in_payload');
*
* to keep both of them same
* api.serializeOnCreate('field_name');
*
*/
serializeOnCreate(fieldName, property) {
Composer.serializeOnCreate(fieldName, property);
}
/**
* Adds a field to composer update serializer
*
* Example:
*
* api.serializeOnUpdate('key_set_in_model', 'field_name_in_payload');
*
* to keep both of them same
* api.serializeOnUpdate('field_name');
*
*/
serializeOnUpdate(fieldName, property) {
Composer.serializeOnUpdate(fieldName, property);
}
/**
* Registers a criteria that can be used as default topic order on category
* pages.

View File

@ -61,6 +61,11 @@ const CLOSED = "closed",
no_bump: "noBump",
draft_key: "draftKey"
},
_update_serializer = {
raw: "reply",
topic_id: "topic.id",
raw_old: "rawOld"
},
_edit_topic_serializer = {
title: "topic.title",
categoryId: "topic.category.id",
@ -851,6 +856,11 @@ const Composer = RestModel.extend({
});
},
@discourseComputed("editConflict", "originalText")
rawOld(editConflict, originalText) {
return editConflict ? null : originalText;
},
editPost(opts) {
const post = this.post;
const oldCooked = post.cooked;
@ -885,14 +895,12 @@ const Composer = RestModel.extend({
}
const props = {
topic_id: this.topic.id,
raw: this.reply,
raw_old: this.editConflict ? null : this.originalText,
edit_reason: opts.editReason,
image_sizes: opts.imageSizes,
cooked: this.getCookedHtml()
};
this.serialize(_update_serializer, props);
this.set("composeState", SAVING);
const rollback = throwAjaxError(error => {
@ -1225,6 +1233,17 @@ Composer.reopenClass({
return Object.keys(_create_serializer);
},
serializeOnUpdate(fieldName, property) {
if (!property) {
property = fieldName;
}
_update_serializer[fieldName] = property;
},
serializedFieldsForUpdate() {
return Object.keys(_update_serializer);
},
serializeToDraft(fieldName, property) {
if (!property) {
property = fieldName;