Skip to content

Instantly share code, notes, and snippets.

@acutesoftware
Forked from duncansmart/progressive-ace.htm
Created November 29, 2017 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acutesoftware/b40d5cdbf0523332d4deadf3d17efa44 to your computer and use it in GitHub Desktop.
Save acutesoftware/b40d5cdbf0523332d4deadf3d17efa44 to your computer and use it in GitHub Desktop.
Integrating ACE Editor in a progressive way
<textarea name="my-xml-editor" data-editor="xml" rows="15"></textarea>
...
<textarea name="my-markdown-editor" data-editor="markdown" rows="15"></textarea>
...
<script src="//d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js"></script>
<script>
// 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('visibility', 'hidden');
var editor = ace.edit(editDiv[0]);
editor.renderer.setShowGutter(false);
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());
})
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment