monacoCreator.js 5.8 KB

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