index.js 10 KB

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