I’m attempting to implement a context menu (triggered by a right-click) for a codemirror 6 editor in a desktop app (electron), and I assume a tooltip is appropriate for this. The problem I have is knowing when to close the menu if the user clicks elsewhere in the document (not on the context menu) or uses the keyboard (left/right arrows for example). Actions such as these should result in a change to the document selection. However, if I use a change of selection as the trigger to close the menu, I can identify a left mouse click as a user event of “select.pointer” (on the resulting transaction), but I cannot distinguish between a right click and the keyboard because both result in a user event of “select”. The contextmenu handler dispatches a transaction to open the menu at the correct right-click coordinates, but this arrives before the transaction for the selection change. Therefore, I need to identify a selection change caused by a right click and ignore it for the purpose of keeping the menu at the new right click location open, otherwise the menu is opened then immediately closed. Any ideas? If there is no way to identify a selection change being the result of a right click, is it possible to change the order of the selection change transaction and my contextmenu transaction, so that the selection change transaction is handled first? I suppose a possible alternative is to close on the “click” event and particular keystrokes, but this feels messy.
I think you don’t want this to be a two-transaction events (in general, it’s desirable to have only one transaction per user action, to avoid this kind of nonsense). Is there any reason not to use a mousedown handler returns true (when it handles the click) to prevent further handling by the editor?
I’ll investigate this approach.
This suggestion has worked nicely. I had originally returned true from the “contextmenu” handler thinking that might prevent the second transaction, but when it didn’t work I forgot about trying other events, even after noticing in the codemirror code the association between mousedown and selections.