简体   繁体   English

Ace编辑器不会格式化编辑器div中的数据

[英]Ace editor doesn't format the data inside the editor div

I have embedded some JSON data inside the editor div. 我在编辑器div中嵌入了一些JSON数据。

as here : http://jsfiddle.net/P3TwV/11/ 在这里: http//jsfiddle.net/P3TwV/11/

But as shown in the fiddle, the JSON is not being formatted. 但正如小提琴中所示,JSON没有被格式化。 It simply put the data in one line. 它只是将数据放在一行中。

i want the data, that i inputed in single line without any spaces, should get automatically formatted with proper indenting, according to the specified type as here JSON and all the folding and unfolding of the objects inside the editor should get enable. 我希望我在单行中输入没有任何空格的数据应该通过适当的缩进自动格式化,根据指定的类型,这里JSON和编辑器内对象的所有折叠和展开都应该启用。

How do i approach that? 我该如何处理?

Any answer will help me here. 任何答案都会对我有所帮助。 Thank you. 谢谢。

Ace doesn't support formatting the code, you can either use beautify.js or browsers built-in json formatter Ace不支持格式化代码,您可以使用beautify.js或浏览器内置的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
editor.session.setValue(val)

我使用了美化并使用了js_beautify函数并完成了工作。

As a user mentioned, you should go with beautify.js . 正如用户提到的,你应该使用beautify.js

I tried including the Ace Beautifier plugin, but the formatting was completely off. 我尝试包含Ace Beautifier插件,但格式化完全关闭。

// https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-beautify.js
var beautify = ace.require('ace/ext/beautify');

beautify.beautify(editor.getSession());

Here is an example of hooking JS Beautifier into your existing Ace Editor. 这是一个将JS Beautifier挂钩到现有Ace编辑器的示例。

 // Variables var editor = ace.edit('editor'); var txtAra = document.querySelector('textarea[name="editor"]'); var jsbOpts = { indent_size : 2 }; // Setup editor.setTheme("ace/theme/monokai"); editor.getSession().setMode("ace/mode/json"); syncEditor(); // Main Logic setTimeout(formatCode, 1000); // Format sample JSON after 1 second. // Functions function syncEditor() { editor.getSession().setValue(txtAra.value); } function commitChanges() { txtAra.value = editor.getSession().getValue(); } function formatCode() { var session = editor.getSession(); session.setValue(js_beautify(session.getValue(), jsbOpts)); } 
 .title { font-size: 1.67em; font-weight: bold; text-align: center; } #editor { height: 75vh; width: 100%; } textarea[name="editor"] { display: none; } .as-console-wrapper { display: none !important; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.6.8/beautify.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css" rel="stylesheet"/> <div> <div class="title">Ace Editor - JSON Format</div> <textarea name="editor">{"glossary": {"title": "example glossary","GlossDiv": {"title": "S","GlossList": {"GlossEntry": {"ID": "SGML","SortAs": "SGML","GlossTerm": "Standard Generalized Markup Language","Acronym": "SGML","Abbrev": "ISO 8879:1986","GlossDef": {"para": "A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso": ["GML", "XML"]},"GlossSee": "markup"}}}}}</textarea> <div id="editor"></div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM