I've been using CodeMirror
and trying to style the autocomplete pop up with CSS. It's been a challenge because the pop up disappears when I inspect styles and make changes.
I came across some code in show-hint.js
that looks like this:
if (options.closeOnUnfocus !== false) {
var closingOnBlur;
cm.on("blur", this.onBlur = function () { closingOnBlur = setTimeout(function () { completion.close(); }, 100); });
cm.on("focus", this.onFocus = function () { clearTimeout(closingOnBlur); });
}
Commenting out the above code prevents the pop up from disappearing when I click on other elements, which is what I wanted. But now I'm trying to figure out how to toggle this behavior on and off.
I wanted to set the closeOnUnfocus
option on my own, but I couldn't find a way to do it. Further research led me to an example on the CodeMirror website with the following code:
CodeMirror.commands.autocomplete = function(cm) {
CodeMirror.showHint(cm, CodeMirror.hint.anyword);
}
Looking deeper, show-hint.js
has a function named showHint
with the following signature:
CodeMirror.showHint = function (cm, getHints, options) {
if (cm.somethingSelected()) return;
if (getHints == null) {
if (options && options.async) return;
else getHints = CodeMirror.hint.auto;
}
if (cm.state.completionActive) cm.state.completionActive.close();
var completion = cm.state.completionActive = new Completion(cm, getHints, options || {});
CodeMirror.signal(cm, "startCompletion", cm);
if (completion.options.async)
getHints(cm, function (hints) { completion.showHints(hints); }, completion.options);
else
return completion.showHints(getHints(cm, completion.options));
};
I tried passing my option through this function by modifying the autocomplete command like this:
CodeMirror.commands.autocomplete = function (cm) {
CodeMirror.showHint(cm, CodeMirror.hint.anyword, {
closeOnUnfocus: false
});
}
However, this approach didn't work as expected - it seems that the options are not being passed through at all. Even when I log the options in show-hint.js
, they are ignored. I'm confused about how to pass options through successfully. Can anyone help me with this?