Sorry I am new to using CodeMirror, and I’m running into what surely is an easy bug. I’m making an online collaborative IDE for a school project. Didn’t want to send entire documents and wanted to just do the updates, but the input event listener doesn’t catch the enter key. Using keydown I can get when the enter key was pressed, I have the index I want to put it but for some reason can’t get the replaceRange function to work, I keep getting “Uncaught (in promise) TypeError: editorView.replaceRange is not a function” in my web console of the receiving user. I’m assuming I’m missing something rather simple like not targeting the right object to call replaceRange on, or not having the correct index/position of where im adding the new line.
Below is the relevant code for this problem.
Javascript file
cmEditorElement.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
console.log("Enter key was pressed");
list = cmEditorElement.querySelector(".cm-content").childNodes;
console.log(list)
let count = 1;
Array.prototype.forEach.call(list, child => {
if (child.className == "cm-line") {
// Name is not active line, move on
count++
}
else {
// Name matched, call socket to add new line to all other users
console.log('New line on ' + count)
socket.emit('newLine', count)
}
});
}
})
socket.on('addingNewLine', (pos) => {
console.log('Received new line')
editorView.replaceRange("\n", pos)
})
Server File
socket.on('newLine', (pos) => {
console.log('Adding new line')
socket.broadcast.emit('addingNewLine', pos)
})
Thank you for any help!