index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. var lineNumbers;
  147. options.appendChild(createOptionToggle(
  148. editor,
  149. 'lineNumbers',
  150. function() {
  151. return (lineNumbers === false ? false : true);
  152. },
  153. function(editor, newValue) {
  154. lineNumbers = newValue;
  155. editor.updateOptions({ lineNumbers: lineNumbers ? 'on' : 'off' });
  156. }
  157. ));
  158. var glyphMargin;
  159. options.appendChild(createOptionToggle(
  160. editor,
  161. 'glyphMargin',
  162. function() {
  163. return (glyphMargin === false ? false : true);
  164. },
  165. function(editor, newValue) {
  166. glyphMargin = newValue;
  167. editor.updateOptions({ glyphMargin: glyphMargin });
  168. }
  169. ));
  170. var minimap;
  171. options.appendChild(createOptionToggle(
  172. editor,
  173. 'minimap',
  174. function() {
  175. return (minimap === false ? false : true);
  176. },
  177. function(editor, newValue) {
  178. minimap = newValue;
  179. editor.updateOptions({ minimap: { enabled: minimap } });
  180. }
  181. ));
  182. var roundedSelection;
  183. options.appendChild(createOptionToggle(
  184. editor,
  185. 'roundedSelection',
  186. function() {
  187. return (roundedSelection === false ? false : true);
  188. },
  189. function(editor, newValue) {
  190. roundedSelection = newValue;
  191. editor.updateOptions({ roundedSelection: roundedSelection });
  192. }
  193. ));
  194. var scrollBeyondLastLine;
  195. options.appendChild(createOptionToggle(
  196. editor,
  197. 'scrollBeyondLastLine',
  198. function() {
  199. return (scrollBeyondLastLine === false ? false : true);
  200. },
  201. function(editor, newValue) {
  202. scrollBeyondLastLine = newValue;
  203. editor.updateOptions({ scrollBeyondLastLine: scrollBeyondLastLine });
  204. }
  205. ));
  206. var renderWhitespace;
  207. options.appendChild(createOptionToggle(
  208. editor,
  209. 'renderWhitespace',
  210. function() {
  211. return (renderWhitespace === true ? true : false);
  212. },
  213. function(editor, newValue) {
  214. renderWhitespace = newValue;
  215. editor.updateOptions({ renderWhitespace: renderWhitespace });
  216. }
  217. ));
  218. var readOnly;
  219. options.appendChild(createOptionToggle(
  220. editor,
  221. 'readOnly',
  222. function() {
  223. return (readOnly === true ? true : false);
  224. },
  225. function(editor, newValue) {
  226. readOnly = newValue;
  227. editor.updateOptions({ readOnly: readOnly });
  228. }
  229. ));
  230. var wordWrap;
  231. options.appendChild(createOptionToggle(
  232. editor,
  233. 'wordWrap',
  234. function() {
  235. return (wordWrap === true ? true : false);
  236. },
  237. function(editor, newValue) {
  238. wordWrap = newValue;
  239. editor.updateOptions({ wordWrap: wordWrap ? 'on' : 'off' });
  240. }
  241. ));
  242. var folding;
  243. options.appendChild(createOptionToggle(
  244. editor,
  245. 'folding',
  246. function() {
  247. return (folding === false ? false : true);
  248. },
  249. function(editor, newValue) {
  250. folding = newValue;
  251. editor.updateOptions({ folding: folding });
  252. }
  253. ));
  254. }
  255. function createOptionToggle(editor, labelText, stateReader, setState) {
  256. var domNode = document.createElement('div');
  257. domNode.className = 'option toggle';
  258. var input = document.createElement('input');
  259. input.type = 'checkbox';
  260. var label = document.createElement('label');
  261. label.appendChild(input);
  262. label.appendChild(document.createTextNode(labelText));
  263. domNode.appendChild(label);
  264. var renderState = function() {
  265. input.checked = stateReader();
  266. };
  267. renderState();
  268. editor.onDidChangeConfiguration(function() {
  269. renderState();
  270. });
  271. input.onchange = function() {
  272. setState(editor, !stateReader());
  273. };
  274. return domNode;
  275. }
  276. function ComboBox(label, externalOptions) {
  277. this.id = 'combobox-' + label.toLowerCase().replace(/\s/g, '-');
  278. this.domNode = document.createElement('div');
  279. this.domNode.setAttribute('style', 'display: inline; margin-right: 5px;');
  280. this.label = document.createElement('label');
  281. this.label.innerHTML = label;
  282. this.label.setAttribute('for', this.id);
  283. this.domNode.appendChild(this.label);
  284. this.comboBox = document.createElement('select');
  285. this.comboBox.setAttribute('id', this.id);
  286. this.comboBox.setAttribute('name', this.id);
  287. this.comboBox.onchange =(function (e) {
  288. var target = e.target || e.srcElement;
  289. this.options[target.options[target.selectedIndex].value].callback();
  290. }).bind(this);
  291. this.domNode.appendChild(this.comboBox);
  292. this.options = [];
  293. for (var name in externalOptions) {
  294. if (externalOptions.hasOwnProperty(name)) {
  295. var optionElement = document.createElement('option');
  296. optionElement.value = name;
  297. optionElement.innerHTML = name;
  298. this.options[name] = {
  299. element: optionElement,
  300. callback: externalOptions[name]
  301. };
  302. this.comboBox.appendChild(optionElement);
  303. }
  304. }
  305. }
  306. ComboBox.prototype.set = function (name) {
  307. if (this.options[name]) {
  308. this.options[name].element.selected = true;
  309. }
  310. };
  311. });