Passing data between plugins

Hello. I have a highlight plugin that highlights parts of the documents. Additionally, I have plugins that create various ranges from the document. Now I want to pass these ranges along with additional data to the highlight plugin to decorate the editor content. I am having some trouble wrapping my head around the data flow here. If I understand it correctly, when state changes, each plugins’ state apply method is fired. I think I am supposed to pass data to plugins by adding metadata to transactions, but I just have access to the plugin state inside the method, and it also does not make much sense to create a new transaction and update state while updating state already. Is there a preferred way of doing this?

If you make sure the plugin that produces the data appears before any plugins that consume it in your array of plugins as passed to EditorState.create, and you give your producing plugin a key that you export, the consuming plugins can use that key to read the plugin state of that other plugin from the new state that is passed to their apply method.

Thank you for the quick response, I will try to implement this way as soon as I have some time available.

You can also use setMeta within appendTransaction on the originating plugin and process the metadata in apply and/or appendTransaction on consuming plugins. We use this approach for several cases.

This has the advantage that plugin order does not matter.

It doesn’t work if you need to reprocess multiple times, as appendTransaction is not called for transactions produced in any appendTransaction handler, but this approach is usually sufficient all the same.

Managed to get it to work. Thank you both for your help.

It should be (just not for transactions created by that same handler).

It should be (just not for transactions created by that same handler)

Good to know! I don’t recall if we tried this setup or not.