问题
I am trying to write an online editor and at the moment am trying to implement code folding. On the CodeMirror website they do a demo that uses the foldCode command that comes from the foldcode.js addon.
Here is my code.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Code Mirror Test</title>
<script type="text/javascript" src="codemirror-5.4/lib/codemirror.js"></script>
<link rel="stylesheet" href="codemirror-5.4/lib/codemirror.css">
<link rel="stylesheet" href="codemirror-5.4/theme/neat.css">
<script type="text/javascript" src="codemirror-5.4/mode/javascript/javascript.js"></script>
<link rel="stylesheet" href="src/Main.css">
<script type="type/javascript" src="codemirror-5.4/addon/fold/foldcode.js"></script>
<script type="type/javascript" src="codemirror-5.4/addon/fold/foldgutter.js"></script>
<link rel="stylesheet" href="codemirror-5.4/addon/fold/foldgutter.css">
</head>
<body>
<script>
window.onload = function() {
var editor = CodeMirror(document.body, {
mode: "javascript",
lineNumbers: true,
theme: "neat",
extraKeys: {"Ctrl-Q": function(cm) { cm.foldCode(cm.getCursor()); }},
foldgutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
});
}
</script>
</body>
</html>
IntelliJ is telling me that foldCode is an unresolved method. Im wondering if maybe I am importing the script wrong or may be foldCode was depreciated with the newest versions of CodeMirror?
回答1:
You don't seem to be loading any actual folding functions. For JavaScript, you probably want addon/fold/brace-fold.js
.
回答2:
Besides the other answers, add
editor.foldCode(CodeMirror.Pos(0, 0));
outside of your var editor
回答3:
change
foldgutter: true;
to
foldGutter: true;
来源:https://stackoverflow.com/questions/31279950/codemirror-foldcode-method-not-working