Created
August 14, 2014 16:34
-
-
Save mzhang28/0adfe72492752fee9e1f to your computer and use it in GitHub Desktop.
javascript part of skulpt script
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
function escapeRegExp(string) { | |
return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); | |
} | |
function replaceAll(string, find, replace) { | |
return string.replace(new RegExp(escapeRegExp(find), 'g'), replace); | |
} | |
var outf = function(text) { | |
var mypre = document.getElementById("output"); | |
mypre.innerHTML = mypre.innerHTML + text; | |
} | |
var builtinRead = function(x) { | |
if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined) | |
throw "File not found: '" + x + "'"; | |
return Sk.builtinFiles["files"][x]; | |
} | |
var runCode = function() { | |
$("#output").html(""); | |
var program = $("#editor").val(); | |
var output = document.getElementById("output"); | |
Sk.canvas = "canvas"; | |
Sk.pre = output; | |
Sk.configure({ | |
output: outf, | |
read: builtinRead | |
}); | |
try { | |
eval(Sk.importMainWithBody("<stdin>", false, program)); | |
} catch (e) { | |
$("#output").append("<div style='color:#F00;'>"+replaceAll(replaceAll(e.toString(),"<","<"),">",">")+"</div>"); | |
// alert(e.toString()); | |
} | |
$("#output-div").modal("show"); | |
}; | |
var ctrl = false; | |
$(document).keydown(function(e) { | |
if (e.keyCode == 17) { | |
ctrl = true; | |
} | |
}); | |
$(document).keyup(function(e) { | |
if (e.keyCode == 13 && ctrl) { | |
runCode(); | |
} else if (e.keyCode == 17) { | |
ctrl = false; | |
} | |
}); | |
$(document).ready(function() { | |
$("#editor").focus(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment