index.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /// <reference path="../node_modules/monaco-editor-core/monaco.d.ts" />
  2. define(['require', './samples'], function(require, SAMPLES) {
  3. var domutils = require('vs/base/browser/dom');
  4. var model = monaco.editor.createModel('', 'plaintext');
  5. var editor = monaco.editor.create(document.getElementById('container'), {
  6. model: model,
  7. glyphMargin: true,
  8. renderWhitespace: true
  9. });
  10. editor.addCommand({
  11. ctrlCmd: true,
  12. key: 'F9'
  13. }, function(ctx, args) {
  14. alert('Command Running!!');
  15. console.log(ctx);
  16. });
  17. editor.addAction({
  18. id: 'my-unique-id',
  19. label: 'My Label!!!',
  20. keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.F10],
  21. contextMenuGroupId: 'navigation',
  22. contextMenuOrder: 2.5,
  23. run: function(ed) {
  24. console.log("i'm running => " + ed.getPosition());
  25. }
  26. });
  27. var currentSamplePromise = null;
  28. var samplesData = {};
  29. SAMPLES.sort(function(a,b) {
  30. return a.name.localeCompare(b.name);
  31. }).forEach(function(sample) {
  32. samplesData[sample.name] = function() {
  33. if (currentSamplePromise !== null) {
  34. currentSamplePromise.cancel();
  35. currentSamplePromise = null;
  36. }
  37. currentSamplePromise = sample.loadText().then(function(modelText) {
  38. currentSamplePromise = null;
  39. updateEditor(sample.mimeType, modelText, sample.name);
  40. });
  41. }
  42. });
  43. var examplesComboBox = new ComboBox('Template', samplesData);
  44. var modesData = {};
  45. monaco.languages.getLanguages().forEach(function(language) {
  46. modesData[language.id] = updateEditor.bind(this, language.id);
  47. });
  48. var modesComboBox = new ComboBox('Mode', modesData);
  49. var themesData = {};
  50. themesData['vs'] = function() { monaco.editor.setTheme('vs') };
  51. themesData['vs-dark'] = function() { monaco.editor.setTheme('vs-dark') };
  52. themesData['hc-black'] = function() { monaco.editor.setTheme('hc-black') };
  53. var themesComboBox = new ComboBox('Theme', themesData);
  54. // Do it in a timeout to simplify profiles
  55. window.setTimeout(function () {
  56. var START_SAMPLE = 'Y___DefaultJS';
  57. var url_matches = location.search.match(/sample=([^?&]+)/i);
  58. if (url_matches) {
  59. START_SAMPLE = url_matches[1];
  60. }
  61. if (location.hash) {
  62. START_SAMPLE = location.hash.replace(/^\#/, '');
  63. START_SAMPLE = decodeURIComponent(START_SAMPLE);
  64. }
  65. samplesData[START_SAMPLE]();
  66. examplesComboBox.set(START_SAMPLE);
  67. createOptions(editor);
  68. createToolbar(editor);
  69. }, 0);
  70. function updateEditor(mode, value, sampleName) {
  71. if (sampleName) {
  72. window.location.hash = sampleName;
  73. }
  74. if (typeof value !== 'undefined') {
  75. var oldModel = model;
  76. model = monaco.editor.createModel(value, mode);
  77. editor.setModel(model);
  78. if (oldModel) {
  79. oldModel.dispose();
  80. }
  81. } else {
  82. monaco.editor.setModelLanguage(model, mode);
  83. }
  84. modesComboBox.set(mode);
  85. }
  86. function createToolbar(editor) {
  87. var bar = document.getElementById('bar');
  88. bar.appendChild(examplesComboBox.domNode);
  89. bar.appendChild(modesComboBox.domNode);
  90. bar.appendChild(themesComboBox.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. }
  138. function createButton(label, onClick) {
  139. var result = document.createElement("button");
  140. result.innerHTML = label;
  141. result.onclick = onClick;
  142. return result;
  143. }
  144. function createOptions(editor) {
  145. var options = document.getElementById('options');
  146. options.appendChild(createOptionToggle(
  147. editor,
  148. 'lineNumbers',
  149. function(config) {
  150. return config.viewInfo.renderLineNumbers;
  151. },
  152. function(editor, newValue) {
  153. editor.updateOptions({ lineNumbers: newValue ? 'on': 'off' });
  154. }
  155. ));
  156. options.appendChild(createOptionToggle(
  157. editor,
  158. 'glyphMargin',
  159. function(config) {
  160. return config.viewInfo.glyphMargin;
  161. },
  162. function(editor, newValue) {
  163. editor.updateOptions({ glyphMargin: newValue });
  164. }
  165. ));
  166. options.appendChild(createOptionToggle(
  167. editor,
  168. 'minimap',
  169. function(config) {
  170. return config.viewInfo.minimap.enabled;
  171. },
  172. function(editor, newValue) {
  173. editor.updateOptions({ minimap: { enabled: newValue } });
  174. }
  175. ));
  176. options.appendChild(createOptionToggle(
  177. editor,
  178. 'roundedSelection',
  179. function(config) {
  180. return config.viewInfo.roundedSelection;
  181. },
  182. function(editor, newValue) {
  183. editor.updateOptions({ roundedSelection: newValue });
  184. }
  185. ));
  186. options.appendChild(createOptionToggle(
  187. editor,
  188. 'scrollBeyondLastLine',
  189. function(config) {
  190. return config.viewInfo.scrollBeyondLastLine;
  191. }, function(editor, newValue) {
  192. editor.updateOptions({ scrollBeyondLastLine: newValue });
  193. }
  194. ));
  195. options.appendChild(createOptionToggle(
  196. editor,
  197. 'renderWhitespace',
  198. function(config) {
  199. return config.viewInfo.renderWhitespace;
  200. }, function(editor, newValue) {
  201. editor.updateOptions({ renderWhitespace: newValue });
  202. }
  203. ));
  204. options.appendChild(createOptionToggle(
  205. editor,
  206. 'readOnly',
  207. function(config) {
  208. return config.readOnly;
  209. },
  210. function(editor, newValue) {
  211. editor.updateOptions({ readOnly: newValue });
  212. }
  213. ));
  214. options.appendChild(createOptionToggle(
  215. editor,
  216. 'wordWrap',
  217. function(config) {
  218. return config.wrappingInfo.isViewportWrapping;
  219. }, function(editor, newValue) {
  220. editor.updateOptions({ wordWrap: newValue ? 'on' : 'off' });
  221. }
  222. ));
  223. options.appendChild(createOptionToggle(
  224. editor,
  225. 'folding',
  226. function(config) {
  227. return config.contribInfo.folding;
  228. }, function(editor, newValue) {
  229. editor.updateOptions({ folding: newValue });
  230. }
  231. ));
  232. }
  233. function createOptionToggle(editor, labelText, stateReader, setState) {
  234. var domNode = document.createElement('div');
  235. domNode.className = 'option toggle';
  236. var input = document.createElement('input');
  237. input.type = 'checkbox';
  238. var label = document.createElement('label');
  239. label.appendChild(input);
  240. label.appendChild(document.createTextNode(labelText));
  241. domNode.appendChild(label);
  242. var renderState = function() {
  243. input.checked = stateReader(editor.getConfiguration());
  244. };
  245. renderState();
  246. editor.onDidChangeConfiguration(function() {
  247. renderState();
  248. });
  249. input.onchange = function() {
  250. setState(editor, !stateReader(editor.getConfiguration()));
  251. };
  252. return domNode;
  253. }
  254. function ComboBox(label, externalOptions) {
  255. this.id = 'combobox-' + label.toLowerCase().replace(/\s/g, '-');
  256. this.domNode = document.createElement('div');
  257. this.domNode.setAttribute('style', 'display: inline; margin-right: 5px;');
  258. this.label = document.createElement('label');
  259. this.label.innerHTML = label;
  260. this.label.setAttribute('for', this.id);
  261. this.domNode.appendChild(this.label);
  262. this.comboBox = document.createElement('select');
  263. this.comboBox.setAttribute('id', this.id);
  264. this.comboBox.setAttribute('name', this.id);
  265. this.comboBox.onchange =(function (e) {
  266. var target = e.target || e.srcElement;
  267. this.options[target.options[target.selectedIndex].value].callback();
  268. }).bind(this);
  269. this.domNode.appendChild(this.comboBox);
  270. this.options = [];
  271. for (var name in externalOptions) {
  272. if (externalOptions.hasOwnProperty(name)) {
  273. var optionElement = document.createElement('option');
  274. optionElement.value = name;
  275. optionElement.innerHTML = name;
  276. this.options[name] = {
  277. element: optionElement,
  278. callback: externalOptions[name]
  279. };
  280. this.comboBox.appendChild(optionElement);
  281. }
  282. }
  283. }
  284. ComboBox.prototype.set = function (name) {
  285. if (this.options[name]) {
  286. this.options[name].element.selected = true;
  287. }
  288. };
  289. });