contracts-js
Version:
A contract library for JavaScript
99 lines (87 loc) • 2.81 kB
JavaScript
requirejs.config({
shim: {
'underscore': {
exports: '_'
}
}
});
require(["./sweet", "./syntax"], function(sweet, syn) {
var storage_code = 'editor_code';
var storage_mode = 'editor_mode';
var starting_code = $("#editor").text();
var compileWithSourcemap = $("body").attr("data-sourcemap") === "true";
var editor = CodeMirror.fromTextArea($('#editor')[0], {
lineNumbers: true,
smartIndent: false,
indentWithTabs: true,
tabSize: 4,
autofocus: true,
theme: 'solarized dark'
});
var currentStep = 1;
if (window.location.hash) {
editor.setValue(decodeURI(window.location.hash.slice(1)));
} else {
editor.setValue(localStorage[storage_code] ? localStorage[storage_code] : starting_code);
}
if(localStorage[storage_mode]) {
editor.setOption("keyMap", localStorage[storage_mode]);
}
var output = CodeMirror.fromTextArea($('#output')[0], {
lineNumbers: true,
theme: 'solarized dark',
readOnly: true
});
$('#btn-vim').click(function() {
editor.setOption('keyMap', 'vim');
editor.focus();
localStorage[storage_mode] = "vim";
});
$('#btn-emacs').click(function() {
editor.setOption('keyMap', 'emacs');
editor.focus();
localStorage[storage_mode] = "emacs";
});
$('#btn-step').click(function() {
var unparsedString = syn.prettyPrint(
sweet.expand(editor.getValue(),
undefined,
currentStep++),
$("#ck-hygiene").prop("checked"));
$("#lab-step").text(currentStep);
output.setValue(unparsedString);
});
var updateTimeout;
editor.on("change", function(e) {
clearTimeout(updateTimeout);
updateTimeout = setTimeout(updateExpand, 200);
});
function updateExpand() {
var code = editor.getValue();
var expanded, compiled, res;
window.location = "editor.html#" + encodeURI(code);
localStorage[storage_code] = code;
try {
if (compileWithSourcemap) {
res = sweet.compile(code, {
sourceMap: true,
filename: "test.js",
readableNames: true
});
} else {
res = sweet.compile(code, {
sourceMap: false,
readableNames: true
});
}
compiled = res.code;
output.setValue(compiled);
$('#errors').text('');
$('#errors').hide();
} catch (e) {
$('#errors').text(e);
$('#errors').show();
}
}
updateExpand();
});