Last active
July 24, 2016 23:29
-
-
Save tacone/fabce42b46991702b458 to your computer and use it in GitHub Desktop.
Bookmarklet to replace a textarea with a code editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript: function loadScripts(array, callback) { | |
var loader = function(src, handler) { | |
var script = document.createElement("script"); | |
script.src = src; | |
script.onload = script.onreadystatechange = function() { | |
script.onreadystatechange = script.onload = null; | |
handler(); | |
}; | |
var head = document.getElementsByTagName("head")[0]; | |
(head || document.body).appendChild(script); | |
}; | |
(function() { | |
if (array.length != 0) { | |
loader(array.shift(), arguments.callee); | |
} else { | |
callback && callback(); | |
} | |
})(); | |
}; | |
loadScripts(["http://code.jquery.com/jquery.js", "http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js"], function() { | |
$e = $('<div id="myeditor">'); | |
$e.css({ | |
position: 'absolute', | |
left: 0, | |
right: 0, | |
bottom: 0, | |
top: '200px', | |
widht: '100%', | |
'z-index': 11 | |
}); /* $e.height('500px').width('100%');*/ /*$('#x-form-el-modx-template-content')*/ | |
$('body').append($e); /* var textarea = $('#modx-template-content');*/ | |
var textarea = $('textarea:focus'); | |
if (!textarea.length) { | |
alert("please select a textarea"); | |
return; | |
}; | |
textarea.hide(); | |
var editor = ace.edit("myeditor"); | |
editor.setTheme("ace/theme/monokai"); | |
editor.getSession().setMode("ace/mode/php"); | |
editor.getSession().setValue(textarea.val()); | |
editor.getSession().on('change', function() { | |
textarea.val(editor.getSession().getValue()); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment