在Codemirror中获取选定范围

use*_*328 1 javascript codemirror

如果我在我的代码镜像编辑器中选择了shift +上/下/左/右的范围,是否有办法获取所选文本的坐标?像开始/结束的行号一样,相对于行以及文档?的char位置。

bsp*_*oel 6

codemirror.getCursor(true)将使您开始选择。 codemirror.getCursor(false)将使您的选择结束。


小智 5

创建函数后,我将函数附加到编辑器实例

editor.getSelectedRange = function() {
  return { from: editor.getCursor(true), to: editor.getCursor(false) };
};
Run Code Online (Sandbox Code Playgroud)

匹配标准getSelection方法。如果要处理许多编辑器实例,则必须将其添加到每个实例中,这些实例只是单个对象。