我是Monaco编辑器的新手,我正在尝试使用虚拟键盘并通过虚拟键盘键入内容。你知道我该怎么做吗? 这是我目前正在做的
var position = editor.getPosition(); // Get current mouse position
var text = editor.getValue(position);
var splitedText=text.split("\n");
var lineContent = splitedText[position.lineNumber-1]; // Get selected line content
var textToInsert = character; // text to be inserted
splitedText[position.lineNumber-1] = [lineContent.slice(0, position.column-1), textToInsert, lineContent.slice(position.column-1)].join(''); // Append the text exactly at the selected position (position.column -1)
editor.setValue(splitedText.join("\n")); // Save the value back to the Editor
editor.setPosition(position+1);
第一个字符将被正确插入,但是当我触发下一个字符时,它将位于前一个字符的前面。有解决这个问题的想法吗?
谢谢
答案 0 :(得分:0)
我想出了答案
var line = editor.getPosition();
var range = new monaco.Range(line.lineNumber, line.column, line.lineNumber, line.column);
var id = { major: 1, minor: 1 };
var text = character ;
var op = {identifier: id, range: range, text: text, forceMoveMarkers: true};
editor.executeEdits("my-source", [op]);