editor.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. $(document).ready(function () {
  2. var output = $('#edoutput');
  3. var outf = function (text) {
  4. output.text(output.text() + text);
  5. };
  6. var jsoutf = function (text) {
  7. window.js_output.setValue(text);
  8. }
  9. var keymap = {
  10. "Ctrl-Enter" : function (editor) {
  11. Sk.configure({output: outf, read: builtinRead});
  12. Sk.canvas = "mycanvas";
  13. if (editor.getValue().indexOf('turtle') > -1 ) {
  14. $('#mycanvas').show()
  15. }
  16. Sk.pre = "edoutput";
  17. (Sk.TurtleGraphics || (Sk.TurtleGraphics = {})).target = 'mycanvas';
  18. try {
  19. Sk.misceval.asyncToPromise(function() {
  20. return Sk.importMainWithBody("<stdin>",false,editor.getValue(),true);
  21. });
  22. } catch(e) {
  23. outf(e.toString() + "\n")
  24. }
  25. },
  26. "Shift-Enter": function (editor) {
  27. Sk.configure({output: outf, read: builtinRead});
  28. Sk.canvas = "mycanvas";
  29. Sk.pre = "edoutput";
  30. if (editor.getValue().indexOf('turtle') > -1 ) {
  31. $('#mycanvas').show()
  32. }
  33. try {
  34. Sk.misceval.asyncToPromise(function() {
  35. return Sk.importMainWithBody("<stdin>",false,editor.getValue(),true);
  36. });
  37. } catch(e) {
  38. outf(e.toString() + "\n")
  39. }
  40. }
  41. }
  42. var editor = CodeMirror.fromTextArea(document.getElementById('code'), {
  43. parserfile: ["parsepython.js"],
  44. autofocus: true,
  45. theme: "solarized dark",
  46. //path: "static/env/codemirror/js/",
  47. styleActiveLine: true,
  48. lineNumbers: true,
  49. textWrapping: false,
  50. indentUnit: 4,
  51. height: "160px",
  52. fontSize: "9pt",
  53. autoMatchParens: true,
  54. extraKeys: keymap,
  55. parserConfig: {'pythonVersion': 2, 'strictErrors': true}
  56. });
  57. var js_output = CodeMirror.fromTextArea(document.getElementById('codeoutput'), {
  58. parserfile: ["parsejavascript.js"],
  59. autofocus: false,
  60. theme: "solarized dark",
  61. //path: "static/env/codemirror/js/",
  62. lineNumbers: true,
  63. textWrapping: false,
  64. indentUnit: 4,
  65. height: "160px",
  66. fontSize: "9pt",
  67. autoMatchParens: true,
  68. extraKeys: keymap,
  69. });
  70. window.code_editor = editor;
  71. window.js_output = js_output;
  72. window.jsoutf = jsoutf;
  73. window.outf = outf;
  74. window.builtinRead = builtinRead;
  75. $("#skulpt_run").click(function (e) { keymap["Ctrl-Enter"](editor)} );
  76. $("#toggledocs").click(function (e) {
  77. $("#quickdocs").toggle();
  78. });
  79. var exampleCode = function (id, text) {
  80. $(id).click(function (e) {
  81. editor.setValue(text);
  82. editor.focus(); // so that F5 works, hmm
  83. });
  84. };
  85. exampleCode('#codeexample1', "print \"Hello, World!\" # natch");
  86. exampleCode('#codeexample2', "for i in range(5):\n print i\n");
  87. exampleCode('#codeexample3', "print [x*x for x in range(20) if x % 2 == 0]");
  88. exampleCode('#codeexample4', "print 45**123");
  89. exampleCode('#codeexample5', "print \"%s:%r:%d:%x\\n%#-+37.34o\" % (\n \"dog\",\n \"cat\",\n 23456,\n 999999999999L,\n 0123456702345670123456701234567L)");
  90. exampleCode('#codeexample6', "def genr(n):\n i = 0\n while i < n:\n yield i\n i += 1\n\nprint list(genr(12))\n");
  91. exampleCode('#codeexample7', "# obscure C3 MRO example from Python docs\nclass O(object): pass\nclass A(O): pass\nclass B(O): pass\nclass C(O): pass\nclass D(O): pass\nclass E(O): pass\nclass K1(A,B,C): pass\nclass K2(D,B,E): pass\nclass K3(D,A): pass\nclass Z(K1,K2,K3): pass\nprint Z.__mro__\n");
  92. exampleCode('#codeexample8', "import document\n\npre = document.getElementById('edoutput')\npre.innerHTML = '''\n<h1> Skulpt can also access DOM! </h1>\n''' \n");
  93. $('#clearoutput').click(function (e) {
  94. $('#edoutput').text('');
  95. $('#mycanvas').hide();
  96. });
  97. function builtinRead(x) {
  98. if (Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
  99. throw "File not found: '" + x + "'";
  100. return Sk.builtinFiles["files"][x];
  101. }
  102. editor.focus();
  103. });