CodeMirror foldCode method not working

最后都变了- 提交于 2019-12-11 10:46:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!