设置值后Codemirror自动格式化

新手上路,请多包涵

http://liveweave.com/UxEJ0s

我正在为我的应用程序使用 Codemirror。

我注意到,如果我选择所有文本并按 SHIFT+Tab,它会自动对齐我的代码,使其更易于阅读。

这是我的应用程序当前呈现的示例:

 <ul>
<li>
<font color="#f90000">
  Apples
</font>
</li>
<li>
<font color="#ff9a3d">
  Oranges
</font>
</li>
</ul>

这是我试图让它呈现的内容:

 <ul>
  <li>
    <font color="#f90000">
      Apples
    </font>
  </li>
  <li>
    <font color="#ff9a3d">
      Oranges
    </font>
  </li>
</ul>

编辑

有谁知道是否有办法在不在 Codemirror 中手动选择整个代码的情况下执行此操作?

为什么?我的应用程序后台运行了 Codemirror,所有添加的代码都是动态添加的,但是当我保存最终代码时,它看起来像上面那样。

原文由 Michael Schwartz 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 3.2k
2 个回答

autoFormatRange 已从codemirror中删除,所以我们应该使用另一种方式,注册我们自己的扩展:

1.生成js

转到 js 生成器(这是一种使用插件和自定义扩展获取缩小 js 的简单方法)。 http://codemirror.net/doc/compress.html

版本 3 的更新链接: http://codemirror.net/3/doc/compress.html

2. 选择需要的选项

粘贴自定义扩展代码并按“ _压缩_”按钮。

 CodeMirror.defineExtension("autoFormatRange", function (from, to) {
    var cm = this;
    var outer = cm.getMode(), text = cm.getRange(from, to).split("\n");
    var state = CodeMirror.copyState(outer, cm.getTokenAt(from).state);
    var tabSize = cm.getOption("tabSize");

    var out = "", lines = 0, atSol = from.ch == 0;
    function newline() {
        out += "\n";
        atSol = true;
        ++lines;
    }

    for (var i = 0; i < text.length; ++i) {
        var stream = new CodeMirror.StringStream(text[i], tabSize);
        while (!stream.eol()) {
            var inner = CodeMirror.innerMode(outer, state);
            var style = outer.token(stream, state), cur = stream.current();
            stream.start = stream.pos;
            if (!atSol || /\S/.test(cur)) {
                out += cur;
                atSol = false;
            }
            if (!atSol && inner.mode.newlineAfterToken &&
                inner.mode.newlineAfterToken(style, cur, stream.string.slice(stream.pos) || text[i+1] || "", inner.state))
                newline();
        }
        if (!stream.pos && outer.blankLine) outer.blankLine(state);
        if (!atSol) newline();
    }

    cm.operation(function () {
        cm.replaceRange(out, from, to);
        for (var cur = from.line + 1, end = from.line + lines; cur <= end; ++cur)
            cm.indentLine(cur, "smart");
    });
});

// Applies automatic mode-aware indentation to the specified range
CodeMirror.defineExtension("autoIndentRange", function (from, to) {
    var cmInstance = this;
    this.operation(function () {
        for (var i = from.line; i <= to.line; i++) {
            cmInstance.indentLine(i, "smart");
        }
    });
});

3.使用生成的.js如下

网址:

 <textarea id=code><?=$value?></textarea>

<link rel="stylesheet" href="/codemirror/codemirror.css">
<script src="/codemirror/codemirror-compressed.js"></script>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
    lineNumbers: true,
    mode: "text/html"
});
var totalLines = editor.lineCount();
editor.autoFormatRange({line:0, ch:0}, {line:totalLines});
</script>

代码 在这里 找到

原文由 shukshin.ivan 发布,翻译遵循 CC BY-SA 3.0 许可协议

自从 Codemirror 删除了对 autoFormatRange() 的支持后,不值得用它来格式化文本。我使用 js-beautify 代替。

 var beautify_js = require('js-beautify').js_beautify
var beautify_html = require('js-beautify').html

var formattedJSON = beautify_js(jsonText, { indent_size: 2 });
var formattedXML = beautify_html(xmlText, { indent_size: 2 });

原文由 RajV 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题