Looking at the demo here, I downloaded the active-line.js
file and included it in my HTML as shown in the demo but nothing happens. I included it like so:
<script src="/js/codemirror.js"></script>
<script src="/js/sql.js"></script>
<script src="/js/active-line.js"></script>
<link rel="stylesheet" type="text/css" href="/css/codemirror.css">
This is how I initialise CodeMirror:
<script>
CodeMirror.fromTextArea(document.getElementById("maple_code"), {
lineNumbers: true,
mode: "text/x-mysql"
});
</script>
Syntax highlighting and line numbers work as they should except the active line highlighting. Do I need to tweak anything in the options as well?
Looking at the demo here, I downloaded the active-line.js
file and included it in my HTML as shown in the demo but nothing happens. I included it like so:
<script src="/js/codemirror.js"></script>
<script src="/js/sql.js"></script>
<script src="/js/active-line.js"></script>
<link rel="stylesheet" type="text/css" href="/css/codemirror.css">
This is how I initialise CodeMirror:
<script>
CodeMirror.fromTextArea(document.getElementById("maple_code"), {
lineNumbers: true,
mode: "text/x-mysql"
});
</script>
Syntax highlighting and line numbers work as they should except the active line highlighting. Do I need to tweak anything in the options as well?
Share Improve this question asked May 14, 2018 at 19:25 MorganMorgan 9271 gold badge14 silver badges36 bronze badges2 Answers
Reset to default 5For anyone who builds their project with Webpack or other similar tools here is a full sequence of actions needed to highlight active lines:
Add the following import:
import 'codemirror/addon/selection/active-line';
On CodeMirror instance creation specify
styleActiveLine
option:const editor = CodeMirror.fromTextArea( textAreaElement, { styleActiveLine: { nonEmpty: true } /* add other options here if you need */ } )
From now CSS-class
.CodeMirror-activeline
will be automatically added to currently active line, at the same time.CodeMirror-activeline-background
class will be also added to its childdiv
that is responsible for background. The default background style should be applied automatically when you addimport 'codemirror/lib/codemirror.css'
.If not, you may still need to write a background style manually. For example:
.CodeMirror-activeline-background { background: #e8f2ff; }
Or, if you would like to highlight active lines only when CodeMirror element is focused:
.CodeMirror-focused .CodeMirror-activeline-background { background: #e8f2ff; }
If the default style was applied, but you would like to turn it off when CodeMirror element is not focused:
.CodeMirror:not(.CodeMirror-focused) .CodeMirror-activeline-background { background: unset; }
Enjoy :)
I figured out how to do this.
Initialise your CodeMirror
object like this:
var editor = CodeMirror.fromTextArea(document.getElementById("maple_code"), {
lineNumbers: true,
lineWrapping: true,
styleActiveLine: true,
styleActiveSelected: true,
mode: "text/x-mysql"
});
The line styleActiveLine: true
is what you basically need. styleActiveSelected: true
is optional and it makes it so active line styling persists when you select a line.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744279863a4566516.html
评论列表(0条)