index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /// <reference path="../../release/monaco.d.ts" />
  2. 'use strict';
  3. var editor = null,
  4. diffEditor = null;
  5. $(document).ready(function () {
  6. require(['vs/editor/editor.main'], function () {
  7. var MODES = (function () {
  8. var modesIds = monaco.languages.getLanguages().map(function (lang) {
  9. return lang.id;
  10. });
  11. modesIds.sort();
  12. return modesIds.map(function (modeId) {
  13. return {
  14. modeId: modeId,
  15. sampleURL: 'index/samples/sample.' + modeId + '.txt'
  16. };
  17. });
  18. })();
  19. var startModeIndex = 0;
  20. for (var i = 0; i < MODES.length; i++) {
  21. var o = document.createElement('option');
  22. o.textContent = MODES[i].modeId;
  23. if (MODES[i].modeId === 'typescript') {
  24. startModeIndex = i;
  25. }
  26. $('.language-picker').append(o);
  27. }
  28. $('.language-picker')[0].selectedIndex = startModeIndex;
  29. loadSample(MODES[startModeIndex]);
  30. $('.language-picker').change(function () {
  31. loadSample(MODES[this.selectedIndex]);
  32. });
  33. $('.theme-picker').change(function () {
  34. changeTheme(this.selectedIndex);
  35. });
  36. loadDiffSample();
  37. $('#inline-diff-checkbox').change(function () {
  38. diffEditor.updateOptions({
  39. renderSideBySide: !$(this).is(':checked')
  40. });
  41. });
  42. });
  43. window.onresize = function () {
  44. if (editor) {
  45. editor.layout();
  46. }
  47. if (diffEditor) {
  48. diffEditor.layout();
  49. }
  50. };
  51. });
  52. var preloaded = {};
  53. (function () {
  54. var elements = Array.prototype.slice.call(document.querySelectorAll('pre[data-preload]'), 0);
  55. elements.forEach(function (el) {
  56. var path = el.getAttribute('data-preload');
  57. preloaded[path] = el.innerText || el.textContent;
  58. el.parentNode.removeChild(el);
  59. });
  60. })();
  61. function xhr(url, cb) {
  62. if (preloaded[url]) {
  63. return cb(null, preloaded[url]);
  64. }
  65. $.ajax({
  66. type: 'GET',
  67. url: url,
  68. dataType: 'text',
  69. error: function () {
  70. cb(this, null);
  71. }
  72. }).done(function (data) {
  73. cb(null, data);
  74. });
  75. }
  76. function loadSample(mode) {
  77. $('.loading.editor').show();
  78. xhr(mode.sampleURL, function (err, data) {
  79. if (err) {
  80. if (editor) {
  81. if (editor.getModel()) {
  82. editor.getModel().dispose();
  83. }
  84. editor.dispose();
  85. editor = null;
  86. }
  87. $('.loading.editor').fadeOut({ duration: 200 });
  88. $('#editor').empty();
  89. $('#editor').append(
  90. '<p class="alert alert-error">Failed to load ' + mode.modeId + ' sample</p>'
  91. );
  92. return;
  93. }
  94. if (!editor) {
  95. $('#editor').empty();
  96. editor = monaco.editor.create(document.getElementById('editor'), {
  97. model: null
  98. });
  99. }
  100. var oldModel = editor.getModel();
  101. var newModel = monaco.editor.createModel(data, mode.modeId);
  102. editor.setModel(newModel);
  103. if (oldModel) {
  104. oldModel.dispose();
  105. }
  106. $('.loading.editor').fadeOut({ duration: 300 });
  107. });
  108. }
  109. function loadDiffSample() {
  110. var onError = function () {
  111. $('.loading.diff-editor').fadeOut({ duration: 200 });
  112. $('#diff-editor').append('<p class="alert alert-error">Failed to load diff editor sample</p>');
  113. };
  114. $('.loading.diff-editor').show();
  115. var lhsData = null,
  116. rhsData = null,
  117. jsMode = null;
  118. xhr('index/samples/diff.lhs.txt', function (err, data) {
  119. if (err) {
  120. return onError();
  121. }
  122. lhsData = data;
  123. onProgress();
  124. });
  125. xhr('index/samples/diff.rhs.txt', function (err, data) {
  126. if (err) {
  127. return onError();
  128. }
  129. rhsData = data;
  130. onProgress();
  131. });
  132. function onProgress() {
  133. if (lhsData && rhsData) {
  134. diffEditor = monaco.editor.createDiffEditor(document.getElementById('diff-editor'), {
  135. enableSplitViewResizing: false
  136. });
  137. var lhsModel = monaco.editor.createModel(lhsData, 'text/javascript');
  138. var rhsModel = monaco.editor.createModel(rhsData, 'text/javascript');
  139. diffEditor.setModel({
  140. original: lhsModel,
  141. modified: rhsModel
  142. });
  143. $('.loading.diff-editor').fadeOut({ duration: 300 });
  144. }
  145. }
  146. }
  147. function changeTheme(theme) {
  148. var newTheme = theme === 1 ? 'vs-dark' : theme === 0 ? 'vs' : 'hc-black';
  149. monaco.editor.setTheme(newTheme);
  150. }