monacoCreator.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * This JS file is for Monaco management
  3. */
  4. class MonacoCreator {
  5. constructor(parent) {
  6. this.parent = parent;
  7. this.jsEditor = null;
  8. this.monacoMode = "javascript";
  9. this.blockEditorChange = false;
  10. this.compilerTriggerTimeoutID = null;
  11. }
  12. // ACCESSORS
  13. get JsEditor() {
  14. return this.jsEditor;
  15. };
  16. getCode() {
  17. if(this.jsEditor) return this.jsEditor.getValue();
  18. else return "";
  19. };
  20. setCode(value) {
  21. this.jsEditor.setValue(value);
  22. };
  23. get MonacoMode() {
  24. return this.monacoMode;
  25. };
  26. set MonacoMode(mode) {
  27. if (this.monacoMode != "javascript"
  28. && this.monacoMode != "typescript")
  29. console.warn("Error while defining Monaco Mode");
  30. this.monacoMode = mode;
  31. };
  32. get BlockEditorChange() {
  33. return this.blockEditorChange;
  34. };
  35. set BlockEditorChange(value) {
  36. this.blockEditorChange = value;
  37. };
  38. // FUNCTIONS
  39. /**
  40. * Load the Monaco Node module.
  41. */
  42. loadMonaco() {
  43. var xhr = new XMLHttpRequest();
  44. xhr.open('GET', "babylon.d.txt", true);
  45. xhr.onreadystatechange = function () {
  46. if (xhr.readyState === 4) {
  47. if (xhr.status === 200) {
  48. require.config({ paths: { 'vs': 'node_modules/monaco-editor/min/vs' } });
  49. require(['vs/editor/editor.main'], function () {
  50. if (this.monacoMode === "javascript") {
  51. monaco.languages.typescript.javascriptDefaults.addExtraLib(xhr.responseText, 'babylon.d.ts');
  52. } else {
  53. monaco.languages.typescript.typescriptDefaults.addExtraLib(xhr.responseText, 'babylon.d.ts');
  54. }
  55. this.parent.main.run();
  56. }.bind(this));
  57. }
  58. }
  59. }.bind(this);
  60. xhr.send(null);
  61. };
  62. /**
  63. * Function to (re)create the editor
  64. */
  65. createMonacoEditor() {
  66. var oldCode = "";
  67. if (this.jsEditor) {
  68. oldCode = this.jsEditor.getValue();
  69. this.jsEditor.dispose();
  70. }
  71. var editorOptions = {
  72. value: "",
  73. language: this.monacoMode,
  74. lineNumbers: true,
  75. tabSize: "auto",
  76. insertSpaces: "auto",
  77. roundedSelection: true,
  78. automaticLayout: true,
  79. scrollBeyondLastLine: false,
  80. readOnly: false,
  81. theme: this.parent.settingsPG.vsTheme,
  82. contextmenu: false,
  83. folding: true,
  84. showFoldingControls: "always",
  85. renderIndentGuides: true,
  86. minimap: {
  87. enabled: true
  88. }
  89. };
  90. editorOptions.minimap.enabled = document.getElementById("minimapToggle1280").classList.contains('checked');
  91. this.jsEditor = monaco.editor.create(document.getElementById('jsEditor'), editorOptions);
  92. this.jsEditor.setValue(oldCode);
  93. this.jsEditor.onKeyUp(function () {
  94. this.parent.utils.markDirty();
  95. }.bind(this));
  96. };
  97. /**
  98. * Format the code in the editor
  99. */
  100. formatCode () {
  101. this.jsEditor.getAction('editor.action.formatDocument').run();
  102. };
  103. /**
  104. * Toggle the minimap
  105. */
  106. toggleMinimap () {
  107. var minimapToggle = document.getElementById("minimapToggle1280");
  108. if (minimapToggle.classList.contains('checked')) {
  109. this.jsEditor.updateOptions({ minimap: { enabled: false } });
  110. this.parent.utils.setToMultipleID("minimapToggle", "innerHTML", 'Minimap <i class="fa fa-square" aria-hidden="true"></i>');
  111. } else {
  112. this.jsEditor.updateOptions({ minimap: { enabled: true } });
  113. this.parent.utils.setToMultipleID("minimapToggle", "innerHTML", 'Minimap <i class="fa fa-check-square" aria-hidden="true"></i>');
  114. }
  115. minimapToggle.classList.toggle('checked');
  116. };
  117. /**
  118. * Get the code in the editor
  119. * @param {Function} callBack : Function that will be called after retrieving the code.
  120. */
  121. getRunCode(callBack) {
  122. if (this.parent.settingsPG.ScriptLanguage == "JS")
  123. callBack(this.jsEditor.getValue());
  124. else if (this.parent.settingsPG.ScriptLanguage == "TS") {
  125. this.triggerCompile(this.JsEditor.getValue(), function (result) {
  126. callBack(result + "var createScene = function() { return Playground.CreateScene(engine, engine.getRenderingCanvas()); }")
  127. });
  128. }
  129. };
  130. /**
  131. * Usefull function for TypeScript code
  132. * @param {*} codeValue
  133. * @param {*} callback
  134. */
  135. triggerCompile(codeValue, callback) {
  136. if (this.compilerTriggerTimeoutID !== null) {
  137. window.clearTimeout(this.compilerTriggerTimeoutID);
  138. }
  139. this.compilerTriggerTimeoutID = window.setTimeout(function () {
  140. try {
  141. var output = this.transpileModule(codeValue, {
  142. module: ts.ModuleKind.AMD,
  143. target: ts.ScriptTarget.ES5,
  144. noLib: true,
  145. noResolve: true,
  146. suppressOutputPathCheck: true
  147. });
  148. if (typeof output === "string") {
  149. callback(output);
  150. }
  151. }
  152. catch (e) {
  153. this.parent.utils.showError(e.message, e);
  154. }
  155. }.bind(this), 100);
  156. };
  157. /**
  158. * Usefull function for TypeScript code
  159. * @param {*} input
  160. * @param {*} options
  161. */
  162. transpileModule(input, options) {
  163. var inputFileName = options.jsx ? "module.tsx" : "module.ts";
  164. var sourceFile = ts.createSourceFile(inputFileName, input, options.target || ts.ScriptTarget.ES5);
  165. // Output
  166. var outputText;
  167. var program = ts.createProgram([inputFileName], options, {
  168. getSourceFile: function (fileName) { return fileName.indexOf("module") === 0 ? sourceFile : undefined; },
  169. writeFile: function (_name, text) { outputText = text; },
  170. getDefaultLibFileName: function () { return "lib.d.ts"; },
  171. useCaseSensitiveFileNames: function () { return false; },
  172. getCanonicalFileName: function (fileName) { return fileName; },
  173. getCurrentDirectory: function () { return ""; },
  174. getNewLine: function () { return "\r\n"; },
  175. fileExists: function (fileName) { return fileName === inputFileName; },
  176. readFile: function () { return ""; },
  177. directoryExists: function () { return true; },
  178. getDirectories: function () { return []; }
  179. });
  180. // Emit
  181. program.emit();
  182. if (outputText === undefined) {
  183. throw new Error("Output generation failed");
  184. }
  185. return outputText;
  186. }
  187. };