Here I use editor.setValue() to render a string to the editor, the result in the render view was correct
But when I use editor.getValue() again, and print the result string length, I got an unexpected result.
Here is the code.
var value = `asfdasfsa\n\nasdfasdfasdfal;k l;'k \r\n \r\n \r\n k 'a\r\n skd \r\n a\r\n d \r\n \r\n sas\r\n dffa\r\n sd\r\n fsd\r\n f\r\n sfasfdasfasfdasdffasfaf\n\n fa ljjflkajl\n\n false\n\n\r\n\n A\r\n jpakfafafafaffaffaffafafewfwe\n `;
console.log(value.length); // 280
var editor = monaco.editor.create(document.getElementById('container'), {
value: value,
language: 'javascript',
lineNumbers: 'off',
roundedSelection: false,
scrollBeyondLastLine: false,
readOnly: false,
theme: 'vs-dark'
});
console.log(editor.getValue().length) // 290
This question has been bothering me for a long time, and I still haven't found the answer
Can somebody help answer this question ah, thank you very much~~
It looks like the difference between 280 and 290 is because Monaco is adding \r before any \n in your string that doesn't have a \r already (so it's effectively normalising all end of line characters in the string). If you change your last console.log to console.log(JSON.stringify(editor.getValue()), editor.getValue().length) you will see how the value has changed and some additional \r's have been added.
This comment suggests that Monaco takes the input string and splits it into lines (looking for \r\n or \r or \n). The value of editor.getValue() will return the individual lines, joined by the same line separator that was used when the editor was created, hence a different character count when compared to the input string.
If you paste the following code into the Monaco Playground, you will see that it adds additional \r's and therefore the string returned from the editor (editor.getValue()) is 2 characters longer than the value supplied (value):
var value = `'line 1'\n\'line 2'\r\r\n'line 3'`;
console.log(JSON.stringify(value), value.length); // "'line 1'\n'line 2'\r\r\n'line 3'" 28
var editor = monaco.editor.create(document.getElementById('container'), {
value: value,
language: 'javascript',
lineNumbers: 'off',
roundedSelection: false,
scrollBeyondLastLine: false,
readOnly: false,
theme: 'vs-dark'
});
console.log(JSON.stringify(editor.getValue()), editor.getValue().length) // "'line 1'\r\n'line 2'\r\n\r\n'line 3'" 30