Set Cursor when Clicking on Decoration.replace()


I have a View Plugin that uses Decoration.replace() to replace parts of the document with a <span>. When I use the keyboard to move the cursor inside the replaced range, the original text in that range is shown (as intended). I would like to be able to move the cursor to the beginning of the range when clicking on that <span> . What would be the best way to archive that?

Naively, I would just register an on-click listener and then move the cursor. But I guess there is a better way…

By the way, Codemirror Next feels great and it’s amazing that I was able to come up with a working LaTex-Formula-Plugin in a matter of two hours - without ever using Codemirror Next before.

A handler on the span element would work, but I think an editor-wide handler that checks if is in such a span and uses posAtDOM to find the span’s start position when it is would be a bit nicer.

1 Like

Hi, I’m back! Glad to see so much progress with CM6 since last year (and sorry for never thanking you).

I have a follow-up question: I’m currently using regex within the visibleRanges to find the ranges in the document I want to replace. This works quite well, but in my case, the same thing is already done by the language parser (way more advanced, of course). So is there some easy way to access this kind of information? Even a hint in the right direction would be much much appreciated!

You can access the parser’s output with syntaxTree.

Thank you so much! Super helpful!