1
0

monarch.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /// <reference path="../../release/monaco.d.ts" />
  2. 'use strict';
  3. /*-----------------------------------------
  4. General helpers
  5. ------------------------------------------*/
  6. function clearInnerText(elem) {
  7. elem.innerHTML = '';
  8. }
  9. function getInnerText(elem) {
  10. var text = elem.innerText;
  11. if (!text) text = elem.textContent;
  12. return text;
  13. }
  14. function escapeToHTML(text) {
  15. return text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  16. }
  17. function appendInnerText(elem, txt) {
  18. txt = escapeToHTML(txt);
  19. elem.innerHTML += txt;
  20. }
  21. function setInnerText(elem, txt) {
  22. clearInnerText(elem);
  23. appendInnerText(elem, txt);
  24. }
  25. function getTextFromId(id) {
  26. var elem = document.getElementById(id);
  27. if (elem == null) return null;
  28. return getInnerText(elem);
  29. }
  30. /* -----------------------------------------
  31. The main loader for the workbench UI
  32. ------------------------------------------*/
  33. var outputPane = document.getElementById('monarchConsole');
  34. var isDirty = false;
  35. window.onbeforeunload = function (ev) {
  36. if (isDirty) {
  37. return 'If you leave this page any unsaved work will be lost.';
  38. }
  39. };
  40. function createLangModel(languageId, text) {
  41. monaco.languages.register({ id: languageId });
  42. var langModel = monaco.editor.createModel(text, 'javascript');
  43. var update = function () {
  44. var def = null;
  45. try {
  46. def = eval('(function(){ ' + langModel.getValue() + '; })()');
  47. } catch (err) {
  48. setInnerText(outputPane, err + '\n');
  49. return;
  50. }
  51. monaco.languages.setMonarchTokensProvider(languageId, def);
  52. setInnerText(outputPane, 'up-to-date\n');
  53. };
  54. langModel.onDidChangeContent(function () {
  55. isDirty = true;
  56. update();
  57. });
  58. update();
  59. return langModel;
  60. }
  61. function readSamples(sampleSelect) {
  62. var samples = {};
  63. for (var i = 0; i < sampleSelect.options.length; i++) {
  64. var id = sampleSelect.options[i].value;
  65. if (!id || sampleSelect.options[i].disabled) {
  66. continue;
  67. }
  68. var languageId = 'monarch-language-' + id;
  69. var sampleText = getTextFromId(id + '-sample');
  70. samples[id] = {
  71. languageId: languageId,
  72. langModel: createLangModel(languageId, getTextFromId(id)),
  73. langViewState: null,
  74. sampleModel: monaco.editor.createModel(sampleText, languageId),
  75. sampleViewState: null
  76. };
  77. }
  78. return samples;
  79. }
  80. require(['vs/editor/editor.main'], function () {
  81. var sampleSelect = document.getElementById('sampleselect');
  82. var langPane = document.getElementById('langPane');
  83. var editorPane = document.getElementById('editor');
  84. // Adjust height of editors
  85. var screenHeight = window.innerHeight;
  86. if (screenHeight) {
  87. var paneHeight = 0.76 * screenHeight;
  88. langPane.style.height = paneHeight + 'px';
  89. editorPane.style.height = paneHeight - 112 + 'px'; // 100px + margin 10px + borders 2px
  90. }
  91. var SAMPLES = readSamples(sampleSelect);
  92. var CURRENT_SAMPLE = null;
  93. var langEditor = monaco.editor.create(langPane, {
  94. model: null,
  95. scrollBeyondLastLine: false
  96. });
  97. var sampleEditor = monaco.editor.create(editorPane, {
  98. model: null,
  99. scrollBeyondLastLine: false
  100. });
  101. var select = document.getElementById('themeselect');
  102. var currentTheme = 'vs';
  103. select.onchange = function () {
  104. currentTheme = select.options[select.selectedIndex].value;
  105. monaco.editor.setTheme(currentTheme);
  106. };
  107. // on resize
  108. function refreshLayout() {
  109. langEditor.layout();
  110. sampleEditor.layout();
  111. }
  112. window.onresize = refreshLayout;
  113. // Switch to another sample
  114. function setEditorState(name) {
  115. if (!name || CURRENT_SAMPLE === name || !SAMPLES[name]) {
  116. return;
  117. }
  118. // Save previous sample's view state
  119. if (CURRENT_SAMPLE) {
  120. SAMPLES[CURRENT_SAMPLE].langViewState = langEditor.saveViewState();
  121. SAMPLES[CURRENT_SAMPLE].sampleViewState = sampleEditor.saveViewState();
  122. }
  123. CURRENT_SAMPLE = name;
  124. // Apply new sample
  125. langEditor.setModel(SAMPLES[CURRENT_SAMPLE].langModel);
  126. if (SAMPLES[CURRENT_SAMPLE].langViewState) {
  127. langEditor.restoreViewState(SAMPLES[CURRENT_SAMPLE].langViewState);
  128. }
  129. sampleEditor.setModel(SAMPLES[CURRENT_SAMPLE].sampleModel);
  130. if (SAMPLES[CURRENT_SAMPLE].sampleViewState) {
  131. sampleEditor.restoreViewState(SAMPLES[CURRENT_SAMPLE].sampleViewState);
  132. }
  133. }
  134. // Refresh the sample text
  135. function refreshSample() {
  136. var name = sampleSelect.options[sampleSelect.selectedIndex].value;
  137. setEditorState(name);
  138. }
  139. sampleSelect.onchange = refreshSample;
  140. refreshSample(); // initialize initial text
  141. }); // require