monacoCreator.js 6.4 KB

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