ace editor format code

format xml

var Mode = require('ace/mode/xml').Mode;
editor.getSession().setMode(new Mode());

format json

var val = editor.session.getValue()
var o = JSON.parse(val) // may throw if json is malformed
val = JSON.stringify(o, null, 4) // 4 is the indent size,该方法来自https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js
editor.session.setValue(val)


你可能感兴趣的:(js)