globocom/megadraft

Performing an operation upon deleting a block

makivlach opened this issue · 4 comments

Let's say you have a custom block in your editor and let's say it's some image that uploads to your server. The server then returns an URL with the public path to the said image. All of this functionality is being performed by a plugin.

image

Is there a recommended way to send a request to the server when removing the image from the editor? I suspect that I may have to listen to onChange callback and whenever new currentContent doesn't contain said block then perform some request to the server. Am I on the right path?

Hi, @vlachmilan ! First, sorry for the delay in responding. As for your question, there is no recommended way to do this implementation, even your suggestion works without problems. However, it entails a request to the server for each change in the editor, even if that change was not necessarily been saved by the user. One idea would be to just hide the deleted images and set a "deleted" attribute in the data field, for example. When receiving contentState on the server, it would be enough to scan it and remove the images that have this attribute.

@mogavin What a fantastic suggestion! I think that is the best approach to the issue - to let the server handle all the heavy lifting while saving the content. Thank you!

In the end, I have decided to create a custom function supplied with the package that can be called in editor's onChange which keeps track for deleted images while respecting undo/redo operations.

Upon saving content, I would send this custom state of deleted images together with the editor's content, and server then handles the rest.

Ah, great solution @vlachmilan ! Thank you for sharing with us!