Created
December 7, 2018 09:24
-
-
Save anil826/6c84d7ae7205847f58a6ac9895805a96 to your computer and use it in GitHub Desktop.
JS 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
// ==UserScript== | |
// @id jsAceGit | |
// @name ACEgist | |
// @author Anil (anil826) | |
// @version 0.1.4 | |
// @description ACE editor in your gists | |
// @match https://gist.github.com/gists/*/edit | |
// ==/UserScript== | |
(function (d, cb) { | |
// | |
// A script loader over here! | |
// | |
if (typeof $LAB === 'undefined') { | |
var s; | |
s = d.createElement('script'); | |
s.src = '/LAB.min.js'; | |
s.onload = function () { | |
var s; | |
s = d.createElement('script'); | |
s.textContent = '(' + cb.toString() + ')();'; | |
d.body.appendChild(s); | |
} | |
d.body.appendChild(s); | |
} else { | |
cb() | |
} | |
}(document, function () { | |
// | |
// Syntax highlighting modes | |
// | |
var modes = { | |
'js': { | |
url: '/mode-javascript.js', | |
module: 'ace/mode/javascript' | |
} | |
}; | |
$LAB | |
.script('https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js') | |
.script('/ace.js') | |
.script('/theme-twilight.js') | |
.wait(function () { | |
$('textarea').each(function (i, el) { | |
var $el, | |
$container, | |
editor, | |
ext; | |
$el = $(el); | |
// | |
// Principle: inject an DOM element (sized and positioned) and hide the textarea | |
// | |
$container = $('<div/>').css({ | |
position: 'relative', | |
width: $el.width(), | |
height: $el.height() | |
}).insertAfter(el); | |
$el.hide(); | |
// | |
// ACE magic | |
// | |
editor = ace.edit($container[0]); | |
editor.setTheme('ace/theme/twilight'); | |
// Keep hidden textarea in sync | |
editor.getSession().setValue($el.val()); | |
editor.getSession().on('change', function () { | |
$el.val(editor.getSession().getValue()); | |
}); | |
// Syntax highlighting depending on filename.<ext> | |
ext = 'js'; | |
modes[ext] && $LAB.script(modes[ext].url).wait(function () { | |
var Mode = require(modes[ext].module).Mode; | |
editor.getSession().setMode(new Mode()); | |
}); | |
}); | |
}) | |
; | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment