SOLVED: Emitting selection set in transactions appended by a plugin

Hi all,

I am stuck on a simple pattern (I am sure I am missing something important).

I have a plugin that sets a selection using appendTransaction on certain circumstances. The way my app learns about any selection changes is through checking the selectionSet attribute in dispatchTransaction in the view and then emitting that to the outside. This works well for any changes but transactions that are appended by my plugin.

It seems (by design?) that transactions appended by plugins do not come through dispatchTransaction (probably because they are applied during the state update that usually happens within dispatchTransaction).

What would be the right pattern to emit my selection change? I have added an example for more clarity

Desired end result: I get a console log that the selection was set to 1 when I click anywhere into the text within the example. I am trying to only emit to the outside when dispatchTransaction is called to keep only one place where state is emitted.

Best Frederik

Why not use the arguments passed to appendTransaction?

I appreciate, but I am not sure what you are hinting at. I have no issues setting the selection within the plugin.

My conceptual problem is retrieving the decision from what the plugin did to the selection based on the previous transactions and state.

Of course, I could call a function within dispatchTransaction to get that decision but that seems less pure as if the plugin sets the state and then I just watch the transactions coming through (since dispatchTransaction is never called with the new transaction I’d have to check for every state whether the plugin would set a new selection).

Edit: Thank you, I just understood that you meant applyTransaction (on EditorState). Makes sense now. Thanks for this great library :heart:

I have updated the original example to also output the new transaction.

No, I did really mean appendTransaction—it gets an array of transactions that it hasn’t observed yet, which on the surface sounds like it should help with this. Though I guess if you need to keep additional state that might be hard to shoehorn into appendTransaction.

But anyway, glad you solved this.