简体   繁体   English

如何使用 API 在 Monaco Editor 中格式化 JSON 代码?

[英]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"
}

Current attempt当前尝试

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 的额外参数

Answer回答

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 })其中monacoEditorthis.monacoEditor = monaco.editor.create(this.$el, { ...this.editorOptions })

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

相关问题 如何将文本插入摩纳哥编辑器? - How do I insert text into a Monaco Editor? 如何将 javascript 自动完成添加到摩纳哥编辑器的 markdown 模式? - How do I add javascript autocomplete to markdown mode of monaco editor? 如何使用 Playwright 在摩纳哥编辑器中插入代码? - How to insert code in monaco editor using Playwright? 如何使用量角器在monaco编辑器中插入代码? - How to insert code in monaco editor using the protractor? 如何在Monaco Editor中获得JQuery代码完成? - How to get JQuery Code Completion in Monaco Editor? 我如何在我的反应构建中为摩纳哥编辑器的 loader.js 及其对本地主机的依赖项提供服务 - How do I serve Monaco editor's loader.js and its dependencies on localhost in my react build 如何在 React 中获取嵌入式 Monaco 编辑器的行数? (包括包装) - How do I get the line count of an embedded Monaco editor in React? (including wrapping) 您如何在 Monaco 编辑器中处理来自 IViewZone 的输入事件? - How do you process input events from an IViewZone in the Monaco Editor? 您如何从 monaco 编辑器中检索主题? - How do you retrieve a theme from the monaco editor? 如何将monaco编辑器从只读设置为可写? - How do you set monaco-editor from readonly to writeable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM