I wonder if anyone else here has done this: In our realtime collaborative editor, we want almost all the text input to be shared immediately with collaborators. But we have a few exceptions, among which are keywords. We would like new keywords to appear as atomic commits - so only either the entire keyword is added to the document and shared with collaborators or it is not added at all.
To achieve this, I created a keywordInput plugin with a widget decoration which contains an input field. The widget is set to always stay at the end of the keywords block node and whenever enter, ";" or "," is pressed, a transaction is made to commit a new keyword to the document.
This all works quite well. However - making arrow caret movement between the input field and the prosemirror instance work seemlessly seems rather complex -- of course I can intercept transactions, see if they set the selection and whether the movement was crossing the input field and if this is the case select the input field instead, but I cannot relaly see if the crossing of the input field is due to the pressing of an arrow key or whether the user just coincidentally clicked right on the other side. Also, combined with checking for ArrowLeft and ArrowRight from the input field, it ends up being quite a bit of code.
So here I was wondering: Has anyone else tried this? And have you found a good way of doing this sort of operation?
Edit: I found that adding a handleKeyDown prop instead of trying to filter transactions for key movement is more helpful, as it allows me to only address cases in which there has been an actual key press event. What I still need to find out is a way to determine where the selection will be if the keypress takes place. In our above case, if the caret is at the beginning of the next element (body), it should be 3 higher than the position number of the input decoration. But in other cases it's less clear. Is there a way to find out the following in the same event/ trigger:
- where the caret will be after the selection change
- what kind of event lead is the cause of the seleciton change
It is totally Ok for the selection change to actually take place, so it would be good to get this information either before or after it has taken place.