Ace 编辑器设置值或插入类似 dom 的结构

Ace editor set value or insert like structure of dom

我不能像dom

的结构一样设置值或插入到ACE编辑器

我的代码是这样的

html

<div contenteditable="true" id="proseMirror">
    <p>insert any text1</p>
    <div>
         <p>insert any text2</p>
    </div>
</div>
<div id="editor"></div>

javascript

【build ace editor】
ace.require('ace/ext/language_tools');
const editor = ace.edit('proseMirror');
editor.$blockScrolling = Infinity;
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
editor.setTheme('ace/theme/vibrant_ink');
editor.getSession().setMode('ace/mode/html');
editor.setHighlightActiveLine(true);

【insert to ace editor】
const inputElement = $(this).parents('.textEditable').find('.proseMirror')[0];
editor.setValue(inputElement.innerHTML);

我想做成这样

  1. <p>insert any text1</p>
    
  2. <div>
    
  3.     <p>insert any text2</p>
    
  4. </div>
    

但会是这个

  1. <p>insert any text1</p><div><p>insert any text2</p></div>
    

您可以使用美化扩展

var beautify = ace.require("ace/ext/beautify");
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.session.setMode("ace/mode/javascript");
beautify.beautify(editor.session);
#editor { 
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
<style>
</style>
</head>
<body>
<div id="editor">function foo(items) { var x = "All this is syntax highlighted"; return x; }</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ace.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/mode-javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/theme-monokai.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12/ext-beautify.min.js"></script>
</body>
</html>