97

我希望能够将页面上的特定文本区域转换为 ACE 编辑器。

请问有人有什么指点吗?

编辑:

我有 editor.html 文件与一个文本区域一起工作,但是一旦我添加第二个,第二个就不会转换为编辑器。

编辑2:

我决定放弃拥有几个的想法,而是在新窗口中打开一个。我的新困境是,当我隐藏()和显示()文本区域时,显示出错了。有任何想法吗?

4

5 回答 5

164

据我了解 Ace 的想法,您不应该将textarea作为 Ace 编辑器本身。您应该创建一个额外的 div 并使用.getSession()函数更新 textarea 。

html

<textarea name="description"/>
<div id="description"/>

js

var editor = ace.edit("description");
var textarea = $('textarea[name="description"]').hide();
editor.getSession().setValue(textarea.val());
editor.getSession().on('change', function(){
  textarea.val(editor.getSession().getValue());
});

或者只是打电话

textarea.val(editor.getSession().getValue());

仅当您使用给定的 textarea 提交表单时。我不确定这是否是使用 Ace 的正确方式,但它是在GitHub 上使用的方式。

于 2011-11-02T11:43:57.043 回答
37

Duncansmart 在他的 github 页面上有一个非常棒的解决方案,progressive-ace,它演示了一种将 ACE 编辑器连接到您的页面的简单方法。

基本上,我们获取所有<textarea>具有该data-editor属性的元素并将每个元素转换为 ACE 编辑器。该示例还设置了一些您应该根据自己的喜好自定义的属性,并演示了如何使用data属性来设置每个元素的属性,例如使用 显示和隐藏排水沟data-gutter

// Hook up ACE editor to all textareas with data-editor attribute
$(function() {
  $('textarea[data-editor]').each(function() {
    var textarea = $(this);
    var mode = textarea.data('editor');
    var editDiv = $('<div>', {
      position: 'absolute',
      width: textarea.width(),
      height: textarea.height(),
      'class': textarea.attr('class')
    }).insertBefore(textarea);
    textarea.css('display', 'none');
    var editor = ace.edit(editDiv[0]);
    editor.renderer.setShowGutter(textarea.data('gutter'));
    editor.getSession().setValue(textarea.val());
    editor.getSession().setMode("ace/mode/" + mode);
    editor.setTheme("ace/theme/idle_fingers");

    // copy back to textarea on form submit...
    textarea.closest('form').submit(function() {
      textarea.val(editor.getSession().getValue());
    })
  });
});
textarea {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.9/ace.js"></script>
<textarea name="my-xml-editor" data-editor="xml" data-gutter="1" rows="15"></textarea>
<br>
<textarea name="my-markdown-editor" data-editor="markdown" data-gutter="0" rows="15"></textarea>

于 2013-10-22T09:01:13.740 回答
8

您可以拥有多个 Ace 编辑器。只需给每个 textarea 一个 ID 并为两个 IDS 创建一个 Ace 编辑器,如下所示:

<style>
#editor, #editor2 {
    position: absolute;
    width: 600px;
    height: 400px;
}
</style>
<div style="position:relative; height: 450px; " >
&nbsp;
<div id="editor">some text</div>
</div>
<div style="position:relative; height: 450px; " >
&nbsp;
<div id="editor2">some text</div>
</div>
<script src="ace.js" type="text/javascript" charset="utf-8"></script>
<script src="theme-twilight.js" type="text/javascript" charset="utf-8"></script>
<script src="mode-xml.js" type="text/javascript" charset="utf-8"></script>
<script>
window.onload = function() {
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/twilight");
    var XmlMode = require("ace/mode/xml").Mode;
    editor.getSession().setMode(new XmlMode());

    var editor2 = ace.edit("editor2");
    editor2.setTheme("ace/theme/twilight");
    editor2.getSession().setMode(new XmlMode());

};
</script>
于 2011-07-28T20:31:27.483 回答
1

要创建编辑器,只需执行以下操作:

HTML:

<textarea id="code1"></textarea>
<textarea id="code2"></textarea>

JS:

var editor1 = ace.edit('code1');
var editor2 = ace.edit('code2');
editor1.getSession().setValue("this text will be in the first editor");
editor2.getSession().setValue("and this in the second");

CSS:

#code1, code2 { 
  position: absolute;
  width: 400px;
  height: 50px;
}

它们必须明确定位和调整大小。通过 show() 和 hide() 我相信您指的是 jQuery 函数。我不确定他们到底是怎么做的,但它不能修改它在 DOM 中占用的空间。我隐藏和显示使用:

$('#code1').css('visibility', 'visible');
$('#code2').css('visibility', 'hidden');

如果您使用 css 属性“显示”,它将不起作用。

在此处查看 wiki,了解如何添加主题、模式等... https://github.com/ajaxorg/ace/wiki/Embedding---API

注意:它们不必是文本区域,它们可以是您想要的任何元素。

于 2011-09-19T23:20:41.957 回答
1

对于任何想要使用 CDN 中的 Ace 的最小工作示例的人:

<!DOCTYPE html>
<html lang="en">

<body style="margin:0">
  <div id="editor">function () { 
  console.log('this is a demo, try typing!')
}
  </div>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.1.01/ace.js" type="text/javascript" charset="utf-8"></script>
  <script>
    var editor = ace.edit("editor");
    editor.setTheme("ace/theme/monokai");
    editor.getSession().setMode("ace/mode/javascript");
    document.getElementById("editor").style.height = "120px";
  </script>
</body>

</html>

于 2019-04-20T02:52:36.427 回答