简体   繁体   English

是否可以将textarea设为ACE编辑器?

[英]Is it possible to make a textarea an ACE editor?

I saw a similar topics here , but I couldn't find an answer. 我在这里看到了类似的话题,但找不到答案。 I am trying to connect Ace editor to textarea , but unsuccessfully. 我正在尝试将Ace编辑器连接到textarea ,但未成功。 Then I found that " ace works only on divs ." 然后我发现“ ace仅适用于divs”

I prefer to connect editor to textarea, not to div. 我更喜欢将编辑器连接到textarea,而不是div。 So, is it possible to connect Ace to textarea? 因此,可以将Ace连接到textarea吗?

Thanks in advance! 提前致谢!

This depends on what you want to do with the textarea after replacing it, but is easy to do with several lines of js 这取决于替换后要对textarea进行的处理,但是用几行js即可轻松完成

// create new ace instance
var editor = ace.edit(document.createElement("div"))
editor.session.setValue(textArea.value)
// set size
editor.container.style.height = textArea.clientHeight + "px";
editor.container.style.width = textArea.clientWidth + "px";
// replace textarea with the editor
textArea.parentNode.replaceChild(editor.container, textArea)
// trigger redraw of the editor
editor.resize()

and to replace editor with textarea 并用textarea替换编辑器

var value = editor.getValue()
var start = editor.session.doc.positionToIndex(editor.selection.getRange().start)
var end   = editor.session.doc.positionToIndex(editor.selection.getRange().end)
textArea.value = value
textArea.setSelectionRange(start, end)
editor.container.parentNode.replaceChild(textArea, editor.container)
editor.destroy()

you also can try to use textarea extension from ace https://github.com/ajaxorg/ace/blob/master/lib/ace/ext/textarea.js 您还可以尝试使用ace https://github.com/ajaxorg/ace/blob/master/lib/ace/ext/textarea.js中的 textarea扩展名

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

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