Firing an action when I click on a token

Hi, I would like to show a popup when the user clicks on a token that has been marked with an error. I’ve tried using RightClick and LeftClick as extraKeys but it appears that no matter where you click these events are fired with the position where it was before you clicked so you can’t tell which token the user clicked on.

Is there another way to move the cursor first and then fire an action when the user clicks on a token.

Moving the selection is part of the effect of a click, so indeed, handler (which define the effect) run before that can happen. You should be able to use the mouse event’s clientX and clientY properties with coordsChar to figure out where the click is happening.

For some reason coordsChar always gives me the last character on the line regardless of where I actually click on the line. However, a workaround is to put an onclick handler on a div surrounding the codemirror element and then use getCursor. Codemirror processes the click first and then the click bubbles up to the outer div at which point the cursor has moved to where I clicked and getCursor gives me the right answer.

Any idea why coordsChar always returns the end of the line.

I don’t think it does—this is the same code that’s run to, for example, implement the default click behavior, and it’s pretty well-tested.