index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /// <reference path="../node_modules/monaco-editor-core/monaco.d.ts" />
  2. define(['./samples'], function(SAMPLES) {
  3. var WRAPPING_COLUMN = 300;
  4. var model = monaco.editor.createModel('', 'plaintext');
  5. var editor = monaco.editor.create(document.getElementById('container'), {
  6. model: model,
  7. readOnly: false,
  8. glyphMargin: true,
  9. wrappingColumn: WRAPPING_COLUMN,
  10. outlineMarkers: false,
  11. renderWhitespace: true,
  12. // scrollbar: {
  13. // verticalHasArrows: true,
  14. // horizontalHasArrows: true
  15. // }
  16. });
  17. editor.addCommand({
  18. ctrlCmd: true,
  19. key: 'F9'
  20. }, function(ctx, args) {
  21. alert('Command Running!!');
  22. console.log(ctx);
  23. });
  24. editor.addAction({
  25. id: 'my-unique-id',
  26. label: 'My Label!!!',
  27. keybindings: [
  28. {
  29. ctrlCmd: true,
  30. key: 'F10'
  31. }
  32. ],
  33. enablement: {
  34. textFocus: true,
  35. wordAtPosition: true,
  36. tokensAtPosition: ['identifier', '', 'keyword'],
  37. },
  38. contextMenuGroupId: '2_change/2_bla',
  39. run: function(ed) {
  40. console.log("i'm running => " + ed.getPosition());
  41. }
  42. });
  43. var currentSamplePromise = null;
  44. var samplesData = {};
  45. SAMPLES.sort(function(a,b) {
  46. return a.name.localeCompare(b.name);
  47. }).forEach(function(sample) {
  48. samplesData[sample.name] = function() {
  49. if (currentSamplePromise !== null) {
  50. currentSamplePromise.cancel();
  51. currentSamplePromise = null;
  52. }
  53. currentSamplePromise = sample.loadText().then(function(modelText) {
  54. currentSamplePromise = null;
  55. updateEditor(sample.mimeType, modelText, sample.name);
  56. });
  57. }
  58. });
  59. var examplesComboBox = new ComboBox('Template', samplesData);
  60. var modesData = {};
  61. monaco.languages.getLanguages().forEach(function(language) {
  62. modesData[language.id] = updateEditor.bind(this, language.id);
  63. });
  64. var modesComboBox = new ComboBox ('Mode', modesData);
  65. // Do it in a timeout to simplify profiles
  66. window.setTimeout(function () {
  67. var START_SAMPLE = 'Y___DefaultJS';
  68. var url_matches = location.search.match(/sample=([^?&]+)/i);
  69. if (url_matches) {
  70. START_SAMPLE = url_matches[1];
  71. }
  72. if (location.hash) {
  73. START_SAMPLE = location.hash.replace(/^\#/, '');
  74. }
  75. samplesData[START_SAMPLE]();
  76. examplesComboBox.set(START_SAMPLE);
  77. createOptions(editor);
  78. createToolbar(editor);
  79. }, 0);
  80. function updateEditor(mode, value, sampleName) {
  81. if (sampleName) {
  82. window.location.hash = sampleName;
  83. }
  84. if (typeof value !== 'undefined') {
  85. var oldModel = model;
  86. model = monaco.editor.createModel(value, mode);
  87. editor.setModel(model);
  88. if (oldModel) {
  89. oldModel.dispose();
  90. }
  91. } else {
  92. monaco.editor.setModelLanguage(model, mode);
  93. }
  94. modesComboBox.set(mode);
  95. }
  96. function createToolbar(editor) {
  97. var bar = document.getElementById('bar');
  98. bar.appendChild(examplesComboBox.domNode);
  99. bar.appendChild(modesComboBox.domNode);
  100. bar.appendChild(createButton("Dispose all", function (e) {
  101. editor.dispose();
  102. editor = null;
  103. if (model) {
  104. model.dispose();
  105. model = null;
  106. }
  107. }));
  108. bar.appendChild(createButton("Remove Model", function(e) {
  109. editor.setModel(null);
  110. }));
  111. bar.appendChild(createButton("Dispose Model", function(e) {
  112. if (model) {
  113. model.dispose();
  114. model = null;
  115. }
  116. }));
  117. bar.appendChild(createButton('Ballistic scroll', (function() {
  118. var friction = 1000; // px per second
  119. var speed = 0; // px per second
  120. var isRunning = false;
  121. var lastTime;
  122. var r = 0;
  123. var scroll = function() {
  124. var currentTime = new Date().getTime();
  125. var ellapsedTimeS = (currentTime - lastTime) / 1000;
  126. lastTime = currentTime;
  127. speed = speed - friction * ellapsedTimeS;
  128. r = r + speed * ellapsedTimeS;
  129. editor.setScrollTop(r);
  130. if (speed >= 0) {
  131. domutils.scheduleAtNextAnimationFrame(scroll);
  132. } else {
  133. isRunning = false;
  134. }
  135. }
  136. return function (e) {
  137. speed += 2000;
  138. if (!isRunning) {
  139. isRunning = true;
  140. r = editor.getScrollTop();
  141. lastTime = new Date().getTime();
  142. domutils.runAtThisOrScheduleAtNextAnimationFrame(scroll);
  143. }
  144. };
  145. })()));
  146. bar.appendChild(createButton("Colorize", function(e) {
  147. var out = document.getElementById('colorizeOutput');
  148. monaco.editor.colorize(editor.getModel().getValue(), editor.getModel().getMode().getId(), { tabSize: 4 }).then(function(r) {
  149. out.innerHTML = r;
  150. });
  151. }));
  152. }
  153. function createButton(label, onClick) {
  154. var result = document.createElement("button");
  155. result.innerHTML = label;
  156. result.onclick = onClick;
  157. return result;
  158. }
  159. function createOptions(editor) {
  160. var options = document.getElementById('options');
  161. options.appendChild(createOptionToggle(editor, 'lineNumbers', function(config) {
  162. return config.viewInfo.lineNumbers;
  163. }, function(editor, newValue) {
  164. editor.updateOptions({ lineNumbers: newValue });
  165. }));
  166. options.appendChild(createOptionToggle(editor, 'glyphMargin', function(config) {
  167. return config.viewInfo.glyphMargin;
  168. }, function(editor, newValue) {
  169. editor.updateOptions({ glyphMargin: newValue });
  170. }));
  171. options.appendChild(createOptionToggle(editor, 'roundedSelection', function(config) {
  172. return config.viewInfo.roundedSelection;
  173. }, function(editor, newValue) {
  174. editor.updateOptions({ roundedSelection: newValue });
  175. }));
  176. options.appendChild(createOptionToggle(editor, 'dark', function(config) {
  177. return config.viewInfo.theme === 'vs-dark';
  178. }, function(editor, newValue) {
  179. editor.updateOptions({ theme: newValue ? 'vs-dark' : 'vs' });
  180. }));
  181. options.appendChild(createOptionToggle(editor, 'readOnly', function(config) {
  182. return config.readOnly;
  183. }, function(editor, newValue) {
  184. editor.updateOptions({ readOnly: newValue });
  185. }));
  186. options.appendChild(createOptionToggle(editor, 'hideCursorInOverviewRuler', function(config) {
  187. return config.viewInfo.hideCursorInOverviewRuler;
  188. }, function(editor, newValue) {
  189. editor.updateOptions({ hideCursorInOverviewRuler: newValue });
  190. }));
  191. options.appendChild(createOptionToggle(editor, 'scrollBeyondLastLine', function(config) {
  192. return config.viewInfo.scrollBeyondLastLine;
  193. }, function(editor, newValue) {
  194. editor.updateOptions({ scrollBeyondLastLine: newValue });
  195. }));
  196. options.appendChild(createOptionToggle(editor, 'wordWrap', function(config) {
  197. return config.wrappingInfo.isViewportWrapping;
  198. }, function(editor, newValue) {
  199. editor.updateOptions({ wrappingColumn: newValue ? 0 : WRAPPING_COLUMN });
  200. }));
  201. options.appendChild(createOptionToggle(editor, 'quickSuggestions', function(config) {
  202. return config.contribInfo.quickSuggestions;
  203. }, function(editor, newValue) {
  204. editor.updateOptions({ quickSuggestions: newValue });
  205. }));
  206. options.appendChild(createOptionToggle(editor, 'iconsInSuggestions', function(config) {
  207. return config.contribInfo.iconsInSuggestions;
  208. }, function(editor, newValue) {
  209. editor.updateOptions({ iconsInSuggestions: newValue });
  210. }));
  211. options.appendChild(createOptionToggle(editor, 'autoClosingBrackets', function(config) {
  212. return config.autoClosingBrackets;
  213. }, function(editor, newValue) {
  214. editor.updateOptions({ autoClosingBrackets: newValue });
  215. }));
  216. options.appendChild(createOptionToggle(editor, 'formatOnType', function(config) {
  217. return config.contribInfo.formatOnType;
  218. }, function(editor, newValue) {
  219. editor.updateOptions({ formatOnType: newValue });
  220. }));
  221. options.appendChild(createOptionToggle(editor, 'suggestOnTriggerCharacters', function(config) {
  222. return config.contribInfo.suggestOnTriggerCharacters;
  223. }, function(editor, newValue) {
  224. editor.updateOptions({ suggestOnTriggerCharacters: newValue });
  225. }));
  226. options.appendChild(createOptionToggle(editor, 'acceptSuggestionOnEnter', function(config) {
  227. return config.contribInfo.acceptSuggestionOnEnter;
  228. }, function(editor, newValue) {
  229. editor.updateOptions({ acceptSuggestionOnEnter: newValue });
  230. }));
  231. options.appendChild(createOptionToggle(editor, 'selectionHighlight', function(config) {
  232. return config.contribInfo.selectionHighlight;
  233. }, function(editor, newValue) {
  234. editor.updateOptions({ selectionHighlight: newValue });
  235. }));
  236. options.appendChild(createOptionToggle(editor, 'folding', function(config) {
  237. return config.contribInfo.folding;
  238. }, function(editor, newValue) {
  239. editor.updateOptions({ folding: newValue });
  240. }));
  241. options.appendChild(createOptionToggle(editor, 'renderWhitespace', function(config) {
  242. return config.viewInfo.renderWhitespace;
  243. }, function(editor, newValue) {
  244. editor.updateOptions({ renderWhitespace: newValue });
  245. }));
  246. }
  247. function createOptionToggle(editor, labelText, stateReader, setState) {
  248. var domNode = document.createElement('div');
  249. domNode.className = 'option toggle';
  250. var input = document.createElement('input');
  251. input.type = 'checkbox';
  252. var label = document.createElement('label');
  253. label.appendChild(input);
  254. label.appendChild(document.createTextNode(labelText));
  255. domNode.appendChild(label);
  256. var renderState = function() {
  257. input.checked = stateReader(editor.getConfiguration());
  258. };
  259. renderState();
  260. editor.onDidChangeConfiguration(function() {
  261. renderState();
  262. });
  263. input.onchange = function() {
  264. setState(editor, !stateReader(editor.getConfiguration()));
  265. };
  266. return domNode;
  267. }
  268. function ComboBox(label, externalOptions) {
  269. this.id = 'combobox-' + label.toLowerCase().replace(/\s/g, '-');
  270. this.domNode = document.createElement('div');
  271. this.domNode.setAttribute('style', 'display: inline; margin-right: 5px;');
  272. this.label = document.createElement('label');
  273. this.label.innerHTML = label;
  274. this.label.setAttribute('for', this.id);
  275. this.domNode.appendChild(this.label);
  276. this.comboBox = document.createElement('select');
  277. this.comboBox.setAttribute('id', this.id);
  278. this.comboBox.setAttribute('name', this.id);
  279. this.comboBox.onchange =(function (e) {
  280. var target = e.target || e.srcElement;
  281. this.options[target.options[target.selectedIndex].value].callback();
  282. }).bind(this);
  283. this.domNode.appendChild(this.comboBox);
  284. this.options = [];
  285. for (var name in externalOptions) {
  286. if (externalOptions.hasOwnProperty(name)) {
  287. var optionElement = document.createElement('option');
  288. optionElement.value = name;
  289. optionElement.innerHTML = name;
  290. this.options[name] = {
  291. element: optionElement,
  292. callback: externalOptions[name]
  293. };
  294. this.comboBox.appendChild(optionElement);
  295. }
  296. }
  297. }
  298. ComboBox.prototype.set = function (name) {
  299. if (this.options[name]) {
  300. this.options[name].element.selected = true;
  301. }
  302. };
  303. });