First off, as always, thanks so much for this incredible library!
When a key event fires, I want to intercept it, do some work, and insert a bookmark at the cursor position instead. Here’s a reduced test program, for a texarea node containing " hello" (2 spaces followed by ‘hello’):
// initialize the editor and mark the contents with a node
var editor = CodeMirror.fromTextArea(document.getElementById("code"));
var node = document.createElement('span');
node.className = "marker";
editor.markText({line:0,ch:2},{line:0,ch:5}, {replacedWith: node});
editor.on("keydown", handleKeyDown);
// insert a bookmark node wherever the cursor is
function handleKeyDown(cm, e){
var node = document.createElement('span');
node.className = "bookmark";
cm.setBookmark(cm.getCursor(), {widget: node});
//e.preventDefault(); // <-- this is the weird line
}
The resulting CM instance shows two spaces, followed by my “marker” node, followed by the letters “lo”. I can place the cursor anywhere I wish and hit a key, and I’ll see the “bookmark” node appear, followed by the character I typed.
If, however, I uncomment the preventDefault() line, the behavior changes: obviously I don’t see the resulting character, but now the bookmark won’t show up if the cursor falls at the beginning of the “marked” node. Inserting a bookmark anywhere else, however, works as expected.
Is this a bug? If not, what are the rules that explain this behavior?
Note - may be related to https://github.com/codemirror/CodeMirror/issues/3556