简体   繁体   English

摩纳哥getValue()中的编辑器换行符

[英]Monaco Editor newline in getValue()

So, i'm using Monaco Editor as a big syntax-highlighted textarea which is submitted in a form with some JavaScript. 因此,我将Monaco编辑器用作具有语法高亮显示的大型文本区域,该文本区域以带有JavaScript的形式提交。 To allow this to happen, I have an <input type="text" id="content" name="content" style="display: none;"> tag, and the JavaScript is connected to a button which does this: 为了使这种情况发生,我有一个<input type="text" id="content" name="content" style="display: none;">标记,并且JavaScript连接到执行此操作的按钮:

function submitButtonClick(){
  document.getElementById("content").value = monacoeditor.getValue();
  document.getElementById("myForm").submit();
}

This works fine and does indeed submit the form, however the getValue() call appears to return the text in the monaco editor without the newlines. 这可以正常工作,并且确实可以提交表单,但是getValue()调用似乎可以在摩纳哥编辑器中返回不含换行符的文本。 Text which looks like this in Monaco: 在摩纳哥看起来像这样的文字:

#Hello World
*I am some text*

is returned like so: 这样返回:

#Hello World*I am some text*

How can I ensure that I get the contents of the editor, including all of the newlines? 如何确保获得编辑器的内容,包括所有换行符?

The problem is that HTML's <input type="text"> doesn't support line breaks. 问题是HTML的<input type="text">不支持换行符。 It is intended for single-line input (imagine usernames containing line breaks). 它用于单行输入(想象中的用户名包含换行符)。

For multi-line input there is the <textarea> tag, which is just a form element like any other. 对于多行输入,有一个<textarea>标记,它只是一个表单元素,与其他任何元素一样。 Your form would then look like this: 您的表格将如下所示:

<form id="myForm" action="..." method="POST">
    <textarea id="content" name="content" style="display: none;"></textarea>
</form>

You can then set the value of the textarea and submit the form as you did before: 然后,您可以像以前一样设置textarea的值并提交表单:

function submitButtonClick () {
    document.getElementById ("content").value = monacoeditor.getValue ();
    document.getElementById ("myForm").submit ();
}

您应该使用<input type="hidden" id="content" name="content" style="display: none;">

When pulling data out of Monaco, the newlines will be /n characters. 从摩纳哥提取数据时,换行符为/n字符。 In some situations, this will not work alone as a carriage return is also required. 在某些情况下,这不能单独使用,因为还需要回车。

String.replace("\n", "\r\n");

should fix this issue 应该解决这个问题

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

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