I also struggled with keeping the cursor (unblinking) when focus is lost.
Looking in devtools, the cursor is blinked by setting/unsetting style="visibility: hidden;"
on the cursors wrapping div - that is the div with class="CodeMirror-cursors"
, this style then overrides any css rule visibility settings(ie. div.CodeMirror-cursors { visibility: visible; }
)
I solved this by adding ‘!important’ like this:
// If newState is provided add/remove theClass accordingly, otherwise toggle theClass
function toggleClass(elem, theClass, newState) {
var matchRegExp = new RegExp('(?:^|\\s)' + theClass + '(?!\\S)', 'g');
var add=(arguments.length>2 ? newState : (elem.className.match(matchRegExp) == null));
elem.className=elem.className.replace(matchRegExp, ''); // clear all
if (add) elem.className += ' ' + theClass;
}
// toggle class on all cursors
function cmToggleCursorsClass(cm, theClass, newState) {
toggleClass(cm.getWrapperElement().getElementsByClassName('CodeMirror-cursors')[0], theClass, newState);
}
// keep an unblinking cursor on when blurring
cm.on('blur', cm => { cmToggleCursorsClass(cm, 'CodeMirror-cursorsVisible', true); });
cm.on('focus', cm => { cmToggleCursorsClass(cm, 'CodeMirror-cursorsVisible', false); });
// css
div.CodeMirror-cursorsVisible {
visibility: visible !important;
}
Hope this helps!
PS.
to change a cursor inner style you can do:
// toggle class on all individual cursors
function cmToggleCursorsInnerClass(cm, theClass, newState) {
let cursors=Array.from(cm.getWrapperElement().getElementsByClassName('CodeMirror-cursor'));
cursors.forEach(c => { toggleClass(c, theClass, newState); });
}
// blink the cursor as thick red once
function cmBlinkCursorRedOnce(cm) {
cmToggleCursorsInnerClass(cm, 'redCursor', true);
setTimeout(() => { cmToggleCursorsInnerClass(cm, 'redCursor', false); }, 500);
}
//css
div.redCursor {
visibility: visible !important;
border-color: red;
border-left-width: 2px;
}
I call cmBlinkCursorRedOnce when a search match is not found