monacoCreator.js 6.9 KB

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