[英]How do I format JSON code in Monaco Editor with API?
I am working with monaco editor aka the VS Code engine in a web project.我正在使用monaco 编辑器,也就是 Web 项目中的VS Code引擎。
I am using it to allow users to edit some JSON that has a JSON Schema set, to help give some auto-completion.我使用它来允许用户编辑一些具有 JSON 架构集的 JSON,以帮助提供一些自动完成功能。
When they save their changes and wish to re-edit their work, the JSON that I load back into the editor is converted to a string but this renders the code out on a single line and I would much prefer the JSON to be prettier as if the user right clicks and uses the Format Document command from the context menu or keyboard shortcut etc..当他们保存更改并希望重新编辑他们的工作时,我加载回编辑器的 JSON 被转换为字符串,但这会将代码呈现在一行中,我更希望 JSON 更漂亮,好像用户右键单击并使用上下文菜单或键盘快捷键等中的格式化文档命令。
So this所以这
{ "enable": true, "description": "Something" }
Would become this会变成这样
{
"enable": true,
"description:" "Something"
}
I have tried the following but it feels very hacky to use a timeout to wait/guess when the content has loaded我尝试了以下操作,但是在内容加载时使用超时等待/猜测感觉非常糟糕
require(['vs/editor/editor.main'], function() { // JSON object we want to edit const jsonCode = [{ "enabled": true, "description": "something" }]; const modelUri = monaco.Uri.parse("json://grid/settings.json"); const jsonModel = monaco.editor.createModel(JSON.stringify(jsonCode), "json", modelUri); const editor = monaco.editor.create(document.getElementById('container'), { model: jsonModel }); // TODO: YUK see if we can remove timeout, as waiting for the model/content to be set is weird // Must be some nice native event?! // ALSO ITS SUPER JARRING WITH FLASH OF CHANGE setTimeout(function() { editor.getAction('editor.action.formatDocument').run(); }, 100); });
<script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.19.3/min/vs/loader.js"></script> <script> require.config({ paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.19.3/min/vs' } }); </script> <div id="container" style="width:800px;height:600px;border:1px solid grey"></div>
Does anyone have a better idea or solution to this please?有没有人对此有更好的想法或解决方案?
require(['vs/editor/editor.main'], function() { // JSON object we want to edit const jsonCode = [{ "enabled": true, "description": "something" }]; const modelUri = monaco.Uri.parse("json://grid/settings.json"); const jsonModel = monaco.editor.createModel(JSON.stringify(jsonCode, null, '\\t'), "json", modelUri); const editor = monaco.editor.create(document.getElementById('container'), { model: jsonModel }); });
<script src="https://cdn.jsdelivr.net/npm/monaco-editor@0.19.3/min/vs/loader.js"></script> <script> require.config({ paths: { 'vs': 'https://cdn.jsdelivr.net/npm/monaco-editor@0.19.3/min/vs' } }); </script> <div id="container" style="width:800px;height:600px;border:1px solid grey"></div>
Thanks to https://stackoverflow.com/users/1378051/dalie for reminding me about the extra arguments to JSON.stringify感谢https://stackoverflow.com/users/1378051/dalie提醒我关于 JSON.stringify 的额外参数
Use the tab character for the space argument使用制表符作为空格参数
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
const jsonModel = monaco.editor.createModel(JSON.stringify(jsonCode, null, '\\t'), "json", modelUri);
I use this function to tidy my code:我使用这个函数来整理我的代码:
tidy() {
this.monacoEditor.trigger('anyString', 'editor.action.formatDocument')
}
where monacoEditor
is result of this.monacoEditor = monaco.editor.create(this.$el, { ...this.editorOptions })
其中
monacoEditor
是this.monacoEditor = monaco.editor.create(this.$el, { ...this.editorOptions })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.