code.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. 'use strict';
  2. // Depending on the URL argument, render as LTR or RTL.
  3. var rtl = (document.location.search == '?rtl');
  4. var block = null;
  5. function start() {
  6. Blockly.inject(document.getElementById('blocklyDiv'),
  7. {
  8. path: '../',
  9. toolbox: document.getElementById('toolbox')
  10. }
  11. );
  12. Blockly.addChangeListener(renderContent);
  13. Blockly.Blocks.CreateMainBlock();
  14. }
  15. function renderContent() {
  16. var content = document.getElementById('code');
  17. var code = Blockly.cake.workspaceToCode();
  18. content.textContent = code;
  19. if (typeof prettyPrintOne == 'function') {
  20. code = content.innerHTML;
  21. code = prettyPrintOne(code, 'c');
  22. content.innerHTML = code;
  23. }
  24. }
  25. /**
  26. * Discard all blocks from the workspace.
  27. */
  28. function discard() {
  29. var count = Blockly.mainWorkspace.getAllBlocks().length;
  30. if (count < 2 || window.confirm("刪除所有積木?")) {
  31. Blockly.mainWorkspace.clear();
  32. Blockly.Blocks.CreateMainBlock();
  33. window.location.hash = '';
  34. }
  35. }
  36. /**
  37. * Insert terminal into page.
  38. * https://github.com/jcubic/jquery.terminal
  39. */
  40. jQuery(function($, undefined) {
  41. // $('#terminal').terminal(function(command, term) {
  42. // if (command !== '') {
  43. // var result = window.eval(command);
  44. // if (result != undefined) {
  45. // term.echo(String(result));
  46. // }
  47. // }
  48. // }, {
  49. // greetings: 'Cake Console Terminal',
  50. // name: 'js_demo',
  51. // height: 0,
  52. // width: 0,
  53. // prompt: 'cake> '});
  54. });
  55. /**
  56. * Save current codes into a *.c file.
  57. * https://github.com/eligrey/FileSaver.js
  58. */
  59. function downloadCode() {
  60. var code = Blockly.cake.workspaceToCode();
  61. var codeArray = [];
  62. codeArray.push(code);
  63. // console.log(code);
  64. var codeBlob = new Blob(codeArray, {type: "text/plain;charset=utf-8"});
  65. // alert(document.getElementById('program_name').value)
  66. var filename = document.getElementById('program_name').value;
  67. // alert(filename);
  68. if (filename)
  69. filename = removeExtension(filename);
  70. else
  71. filename = "Example";
  72. var fileExt = ".c";
  73. saveAs(codeBlob, filename + fileExt);
  74. }
  75. // 刪除副檔名
  76. function removeExtension(filename){
  77. var lastDotPosition = filename.lastIndexOf(".");
  78. if (lastDotPosition === -1) return filename;
  79. else return filename.substr(0, lastDotPosition);
  80. }
  81. function saveXmlBlocks() {
  82. var filename = document.getElementById('program_name').value;
  83. var xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
  84. // alert(xml);
  85. if (filename)
  86. filename = removeExtension(filename);
  87. else
  88. filename = "Blockly";
  89. var fileExt = ".xml";
  90. var xml_text = Blockly.Xml.domToPrettyText(xml);
  91. //save the DOM
  92. var blob = new Blob([xml_text], {type: "text/plain;charset=utf-8"});
  93. if( blob.size != 0 )
  94. saveAs(blob, filename + fileExt);
  95. }
  96. function loadXmlBlocks(){
  97. // Init load event.
  98. var loadInput = document.getElementById('load');
  99. loadInput.addEventListener('change', loadXmlDom, false);
  100. loadInput.click();
  101. }
  102. function loadXmlDom(event) {
  103. var files = event.target.files;
  104. // Only allow uploading one file.
  105. if (files.length != 1) {
  106. return;
  107. }
  108. var a = document.getElementById("program_name");
  109. a.value = removeExtension(files[0].name) + ".c";
  110. // FileReader
  111. var reader = new FileReader();
  112. reader.onloadend = function(event) {
  113. var target = event.target;
  114. // 2 == FileReader.DONE
  115. if (target.readyState == 2) {
  116. try {
  117. var xml = Blockly.Xml.textToDom(target.result);
  118. } catch (e) {
  119. alert('剖析XML錯誤:\n' + e);
  120. return;
  121. }
  122. var count = Blockly.mainWorkspace.getAllBlocks().length;
  123. if (count) {
  124. Blockly.mainWorkspace.clear();
  125. }
  126. Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, xml);
  127. }
  128. // Reset value of input after loading because Chrome will not fire
  129. // a 'change' event if the same file is loaded again.
  130. document.getElementById('load').value = '';
  131. };
  132. reader.readAsText(files[0]);
  133. }