在保存草稿时包含 Composer 自定义字段

Recently, I started looking into a bug with the Events plugin where the events data shown on the composer is lost if the post is saved to drafts.

I tried to fix the issue and soon found out that there’s no API exposed by Discourse which allows us to do that.

I think this is an important feature from a UX standpoint with many potential use cases other than what I’m faced with.

For that, I am making a PR, which fixes this issue by introducing a new method in the Composer Model called serializeToDraft. This method will add those custom fields while saving the post to draft. Also, those fields will be set to the composer model when the draft is reopened.

@angus is helping me out on this PR by reviewing the code and suggesting the important improvements to be made.

I would like to know the thoughts of the Discourse team on this feature.

I agree we should have a clean mechanism for plugins to add and retrieve draft info associated with a post.

Me and @angus have put together a PR to address this here.

There are some things I dont understand: why is serializeToDraft part of the plugin api while serializeOnCreate and serializeToTopic are not? under what circumstance is serializeToDraft useful without the other two and vice versa? shouldnt there be a wrapper that does the serialization on both post/topic and also the draft?

Yes, agreed. You can go ahead and make a PR for those ones too.

我遇到了另一个问题:saveDraft() 方法该如何处理?似乎需要为通过 serializeToDraft() 添加的字段设置一个观察者,类似于这个:

否则,如果仅自定义草稿字段发生变化,草稿将不会被发送到端点。
因此,saveDraft() 也应该成为 API 的一部分,或者实际上应该以某种方式将其抽象化。
我还在 composermodel 中发现了这个 dataChanged 观察者。我认为很奇怪,我们竟然有两个观察者在监视同一件事。该观察者内部的逻辑很可能也需要针对自定义草稿字段触发。我也想知道这两个观察者中哪一个会先运行,以及会产生什么影响。