简体   繁体   English

Monaco Editor 默认不支持 XML 语言吗?

[英]Doesn't Monaco Editor support XML language by default?

I am trying to use Monaco Editor for XML language, but XML string is not getting formatted while the editor instance is being rendered, whereas its working fine for JSON.我正在尝试将 Monaco Editor 用于 XML 语言,但在呈现编辑器实例时 XML 字符串未格式化,而它在 JSON 上工作正常。

I also don't find "Format Document" option in Context Menu of Editor(if it is opened with XML language).我也没有在编辑器的上下文菜单中找到“格式化文档”选项(如果它是用 XML 语言打开的)。 Does Monaco editor support XML formatting by default?摩纳哥编辑器默认支持XML格式吗?

It appears that Monaco Editor doesn't format xml by default.默认情况下,摩纳哥编辑器似乎不格式化 xml。 A colleague of mine - @thewahome , had to write code to format the xml.我的一位同事 - @thewahome不得不编写代码来格式化 xml。 If you're interested on how to format xml the following snippet will help.如果您对如何格式化 xml 感兴趣,以下代码段将有所帮助。

function formatXml(xml: any) {
  const PADDING = ' '.repeat(2);
  const reg = /(>)(<)(\/*)/g;
  let pad = 0;

  xml = xml.replace(reg, '$1\r\n$2$3');

  return xml.split('\r\n').map((node: any, index: number) => {
    let indent = 0;
    if (node.match(/.+<\/\w[^>]*>$/)) {
      indent = 0;
    } else if (node.match(/^<\/\w/) && pad > 0) {
      pad -= 1;
    } else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
      indent = 1;
    } else {
      indent = 0;
    }

    pad += indent;

    return PADDING.repeat(pad - indent) + node;
  }).join('\r\n');
}

The support for XML in Monaco is strange.摩纳哥对XML的支持很奇怪。 As far as I know, there is no XML formatter implemented.据我所知,没有实现 XML 格式化程序。 But don't use XML formatting on clientside .不要在客户端使用 XML 格式 It's a potential bottleneck in your app if you edit large texts.如果您编辑大文本,这可能是您应用程序的瓶颈。 Format XML only once on your backend and send it already formatted.仅在您的后端格式化一次 XML并发送已格式化的文件。 Monaco can be configured to follow XML indention as you type. Monaco 可以配置为在您键入时遵循 XML 缩进。

You can play around with this in Monaco editor playground .您可以在Monaco editor playground中使用它。 (just copy, paste, run): (只需复制、粘贴、运行):

monaco.editor.create(document.getElementById('container'), {
    value: "<group>\n<content>\n<value text=\"Press Ctrl + Shift + F\">\</value>\n</content>\n</group>",
    language: "xml",
    tabSize: 2,
    autoIndent: "full",
    detectIndentation: true,
    formatOnType: true,
    formatOnPaste: true,
});

monaco.languages.setLanguageConfiguration("xml", {
    "indentationRules": {
        "increaseIndentPattern": new RegExp("<(?!\\?|[^>]*\\/>)([-_.A-Za-z0-9]+)(?=\\s|>)\\b[^>]*>(?!.*<\\/\\1>)|<!--(?!.*-->)|\\{[^}\"']*$"),
        "decreaseIndentPattern": new RegExp("^\\s*(<\\/(?!html)[-_.A-Za-z0-9]+\\b[^>]*>|-->|})"),
    },
});

monaco.editor.addKeybindingRules([
    {
        // Reindent lines with Ctrl + Shift + F
        keybinding: monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.KeyF,
        command: "editor.action.reindentlines",
    },
]);

indentationRules are borrowed from vscode HTML extension configuration and simplified. indentationRules借鉴了vscode HTML 扩展配置,并进行了简化。 More information about it here .更多信息请点击此处

This configuration also enables the "Reindent lines" action in the context menu.此配置还在上下文菜单中启用“Reindent lines”操作。

暂无
暂无

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

相关问题 UIL默认不支持scheme(protocol)错误 - UIL doesn't support scheme(protocol) by default ERROR 当我将 XML 文档加载到 Monaco 编辑器时,如何通过鼠标单击或 select 获取 XML 信息? - when I load an XML document to the Monaco editor, how can I get XML information on a mouse click or select? 内容辅助在带有支持库的xml中不起作用 - Content Assist doesn't work in xml with support library solrException。 XML解析器不支持XInclude选项 - solrException. XML parser doesn't support XInclude option 由于语言(模式)失败,ng2-ace-editor 不显示颜色并折叠 - ng2-ace-editor doesn't show colour and folded due to language(mode) fail 如何在Oxygen XML编辑器中使用XSLT 2.0将XHTML转换为XML? XSLT 1.0的解决方案不起作用 - How to transform XHTML to XML with XSLT 2.0 in Oxygen XML editor? Solution for XSLT 1.0 doesn't work Eclipse Indigo XML编辑器无法打开带有其他扩展名的正确XML文件 - Eclipse Indigo XML editor doesn't open proper XML files with other extensions XML文件中的默认命名空间不适用于XSD,为什么? - Default namespace in XML file doesn't work with XSD, why? Android:默认XML编辑器不再打开[不支持的内容类型错误] - Android: Default XML Editor isn't opening anymore [Unsupported Content Type Error] 将XML文件打开到数据网格视图算法中,数据表不支持从xml进行模式推断 - Open XML File into data grid view algorithm, datatable doesn't support schema inference from xml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM