codemirror焦点
CodeMirror is a WYSIWYG-like editor that allows for rich text editing on a small scale, oftentimes used to allow Markdown editing, much like ReviewBoard uses it for. One problem I've found, however, is that calling a CodeMirror instance's focus
method put the cursor at the beginning of the input, which is annoying when there is input in the field. In theory you'd always want to put the cursor at the end so that the user can continue adding to the text that's already there.
CodeMirror是一种类似于WYSIWYG的编辑器,它可以进行小规模的富文本编辑,通常用于允许Markdown编辑,就像ReviewBoard所使用的那样。 但是,我发现的一个问题是,调用CodeMirror实例的focus
方法会将光标放在输入的开头,当字段中有输入时,这很烦人。 理论上,您总是希望将光标放在末尾,以便用户可以继续添加到已经存在的文本中。
Here's a snippet that will set the cursor to the end of existing input:
这是一个将光标设置到现有输入末尾的代码段:
cmInstance.focus();
// Set the cursor at the end of existing content
cmInstance.setCursor(cmInstance.lineCount(), 0);
You would think that there would be a method which would accomplish this task, or even have focus
set the cursor to the end of input by default if the instance has existing text. Anyways, this is the code that will put the cursor at the end of your CodeMirror input instance!
您会认为,如果实例具有现有文本,则默认情况下将存在一种可以完成此任务的方法,甚至可以focus
设置为默认情况下将光标设置在输入的末尾。 无论如何,这是将光标置于CodeMirror输入实例末尾的代码!
codemirror焦点