I'm trying to create a CodeMirror editor for some XML content with the fold gutter showing. I'm loading a bunch of codemirror library code (version 3.18):
<script src="js/lib/codemirror.js"></script>
<script src="js/lib/foldcode.js"></script>
<script src="js/lib/xml-fold.js"></script>
<script src="js/lib/foldgutter.js"></script>
<script src="js/lib/xml.js"></script>
<script src="js/lib/sparql.js"></script>
And then I create the editor object:
var showCodeMirrorResult = function( code, count, mode ) {
var editor = CodeMirror( $("#results").get(0), {
value: code,
mode: mode,
lineNumbers: true,
extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
} );
};
where code
is a string encoding an XML document, and mode
is application/xml
. This all works - I get a syntax-highlighted CodeMirror editor, I can use ctl-Q to fold and unfold XML nodes, all good. However, I can't get the gutter to show, indicating to the user that folding is something they can do in this editor. I'm expecting something like this - note the triangles in the LH gutter to indicate fold points. I can't get those to appear, no matter what I try.
This can be done by calling cm. swapDoc(doc: CodeMirror. Doc) . Save this answer.
Example: Gutters The view module provides functionality for adding gutters (vertical bars in front of the code) to your editor. The simplest use of gutters is to simply dump lineNumbers() into your configuration to get a line number gutter.
This could be used to, for example, replace a textarea with a real editor: var myCodeMirror = CodeMirror(function(elt) { myTextArea. parentNode. replaceChild(elt, myTextArea); }, {value: myTextArea.
Download CodeMirror files. Download jQuery file. Inside the codemirror project folder create subfolders and name them js, css and plugin. The js folder will hold all the javascript files.
Did you add CSS?
.CodeMirror-foldmarker {
font-family: arial;
}
.CodeMirror-foldgutter {
width: .7em;
}
.CodeMirror-foldgutter-open,
.CodeMirror-foldgutter-folded {
color: #555;
cursor: pointer;
}
.CodeMirror-foldgutter-open:after {
content: "\25BE";
}
.CodeMirror-foldgutter-folded:after {
content: "\25B8";
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With