¿Cómo hago un textarea un editor ACE?


Me gustaría poder convertir áreas de texto específicas en una página para ser editores ACE.

¿Alguien tiene algún consejo, por favor?

EDITAR:

Tengo el editor.el archivo html funciona con un área de texto, pero tan pronto como agrego un segundo, el segundo no se convierte en un editor.

EDITAR 2:

Decidí desechar la idea de tener varios, y en su lugar abrir uno en una nueva ventana. Mi nuevo problema es que cuando escondo() y muestro () el área de texto, la pantalla se muestra mal. Alguna idea?

Author: Paul, 2011-06-22

4 answers

Por lo que entendí la idea de Ace, no deberías hacer un textarea un editor de Ace en sí. Debe crear un div adicional y actualizar textarea usando .getSession () funciona en su lugar.

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());
});

O simplemente llame

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

Solo cuando envíe el formulario con el área de texto dada. No estoy seguro de si esta es la forma correcta de usar Ace, pero es la forma en que se usa en GitHub.

 145
Author: installero,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-10-22 13:54:56

Duncansmart tiene una solución bastante impresionante en su página de github, progresivo-as lo que demuestra una forma sencilla de conectar un editor ACE a su página.

Básicamente obtenemos todos los elementos <textarea> con el atributo data-editor y convertimos cada uno en un editor ACE. El ejemplo también establece algunas propiedades que debe personalizar a su gusto, y muestra cómo puede usar los atributos data para establecer propiedades por elemento, como mostrar y ocultar el canal con 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>
 22
Author: billynoah,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-29 06:03:16

Puede tener varios editores Ace. Simplemente dale a cada área de texto un ID y crea un editor Ace para ambos ID de la siguiente manera:

<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>
 8
Author: Breck,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-28 20:31:27

Para crear un editor simplemente haga:

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;
}

Deben estar posicionados y dimensionados explícitamente. Por show () y hide () Creo que te estás refiriendo a las funciones de jQuery. No estoy seguro exactamente cómo lo hacen, pero no puede modificar el espacio que ocupa en el DOM. Me escondo y muestro usando:

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

Si utiliza la propiedad css 'display' no funcionará.

Echa un vistazo a la wiki aquí para saber cómo agregar temas, modos, sucesivamente... https://github.com/ajaxorg/ace/wiki/Embedding---API

Nota: no tienen que ser textareas, pueden ser el elemento que quieras.

 0
Author: Bobby,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-09-19 23:20:41