如何让 coordsChar 到达 return 正确的位置?

How can I get coordsChar to return the correct location?

我正在尝试使用 coordsChar 获取编辑器中点击的位置,以便我可以使用它调用 getTokenAt 并检测点击了哪个词。

(我正在做的事情几乎与以下问题中描述的相同 [codemirror - detect and create links inside editor 并遵循那里的答案中给出的建议。)

不幸的是,无论我点击哪里,coordsChar 总是 returns 文档结尾的位置。我认为这与它总是说 "outside: true."

这一事实有关

如何让 coordsChar 到达 return 正确的位置?

这是我的...

    function onClick (e) {
         if(e.target.className === "cm-myStyle") { 
            //get coords of mouse event
            var x = e.windowX;
            var y = e.windowY;
            var coords = {x,y};

            var loc = editor.coordsChar(coords);

            //this always prints the same location (at the end of the doc)
            console.log(loc);
   }

更新 -- 让它工作。

新代码:

 function onClick (e) {
     if(e.target.className === "cm-myStyle") { 
        var x = e.pageX;
        var y = e.pageY;
        var coords = {left: x, top: y};

        var loc = editor.coordsChar(coords);
}

我意识到我需要在坐标对象中包括 left: 和 top: 。另外,将 windowX 和 windowY 更改为 pageX 和 pageY。