monacoCreator.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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(typings) {
  43. var xhr = new XMLHttpRequest();
  44. xhr.open('GET', typings || "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. var typescript = monaco.languages.typescript;
  54. typescript.typescriptDefaults.setCompilerOptions({
  55. module: typescript.ModuleKind.AMD,
  56. target: typescript.ScriptTarget.ES5,
  57. noLib: true,
  58. noResolve: true,
  59. suppressOutputPathCheck: true,
  60. allowNonTsExtensions: true // required to prevent Uncaught Error: Could not find file: 'inmemory://model/1'.
  61. });
  62. typescript.typescriptDefaults.addExtraLib(xhr.responseText, 'babylon.d.ts');
  63. }
  64. this.parent.main.run();
  65. }.bind(this));
  66. }
  67. }
  68. }.bind(this);
  69. xhr.send(null);
  70. };
  71. /**
  72. * Function to (re)create the editor
  73. */
  74. createMonacoEditor() {
  75. var oldCode = "";
  76. if (this.jsEditor) {
  77. oldCode = this.jsEditor.getValue();
  78. this.jsEditor.dispose();
  79. }
  80. var editorOptions = {
  81. value: "",
  82. language: this.monacoMode,
  83. lineNumbers: true,
  84. tabSize: "auto",
  85. insertSpaces: "auto",
  86. roundedSelection: true,
  87. automaticLayout: true,
  88. scrollBeyondLastLine: false,
  89. readOnly: false,
  90. theme: this.parent.settingsPG.vsTheme,
  91. contextmenu: false,
  92. folding: true,
  93. showFoldingControls: "always",
  94. renderIndentGuides: true,
  95. minimap: {
  96. enabled: true
  97. }
  98. };
  99. editorOptions.minimap.enabled = document.getElementById("minimapToggle1280").classList.contains('checked');
  100. this.jsEditor = monaco.editor.create(document.getElementById('jsEditor'), editorOptions);
  101. this.jsEditor.setValue(oldCode);
  102. this.jsEditor.onKeyUp(function () {
  103. this.parent.utils.markDirty();
  104. }.bind(this));
  105. };
  106. /**
  107. * Format the code in the editor
  108. */
  109. formatCode () {
  110. this.jsEditor.getAction('editor.action.formatDocument').run();
  111. };
  112. /**
  113. * Toggle the minimap
  114. */
  115. toggleMinimap () {
  116. var minimapToggle = document.getElementById("minimapToggle1280");
  117. if (minimapToggle.classList.contains('checked')) {
  118. this.jsEditor.updateOptions({ minimap: { enabled: false } });
  119. this.parent.utils.setToMultipleID("minimapToggle", "innerHTML", 'Minimap <i class="fa fa-square" aria-hidden="true"></i>');
  120. } else {
  121. this.jsEditor.updateOptions({ minimap: { enabled: true } });
  122. this.parent.utils.setToMultipleID("minimapToggle", "innerHTML", 'Minimap <i class="fa fa-check-square" aria-hidden="true"></i>');
  123. }
  124. minimapToggle.classList.toggle('checked');
  125. };
  126. /**
  127. * Get the code in the editor
  128. * @param {Function} callBack : Function that will be called after retrieving the code.
  129. */
  130. getRunCode(callBack) {
  131. var parent = this.parent;
  132. if (parent.settingsPG.ScriptLanguage == "JS")
  133. callBack(this.jsEditor.getValue());
  134. else if (parent.settingsPG.ScriptLanguage == "TS") {
  135. var uri = this.jsEditor.getModel().uri;
  136. monaco.languages.typescript.getTypeScriptWorker()
  137. .then(function(worker) {
  138. worker(uri)
  139. .then(function(languageService) {
  140. var uriStr = uri.toString();
  141. Promise.all([languageService.getSyntacticDiagnostics(uriStr), languageService.getSemanticDiagnostics(uriStr)])
  142. .then(function(diagnostics) {
  143. diagnostics.forEach(function(diagset) {
  144. if (diagset.length) {
  145. parent.utils.showError(diagset[0].messageText);
  146. }
  147. });
  148. });
  149. languageService.getEmitOutput(uriStr)
  150. .then(function(result) {
  151. var output = result.outputFiles[0].text;
  152. var stub = "var createScene = function() { return Playground.CreateScene(engine, engine.getRenderingCanvas()); }";
  153. callBack(output + stub);
  154. });
  155. });
  156. });
  157. }
  158. };
  159. };