index.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /// <reference path="../../out/monaco-editor/monaco.d.ts" />
  2. define(['require', './samples'], function (require, SAMPLES) {
  3. const domutils = require('vs/base/browser/dom');
  4. let model = monaco.editor.createModel('', 'plaintext');
  5. monaco.languages.typescript.typescriptDefaults.setInlayHintsOptions({
  6. includeInlayParameterNameHints: 'all',
  7. includeInlayParameterNameHintsWhenArgumentMatchesName: true,
  8. includeInlayFunctionParameterTypeHints: true,
  9. includeInlayVariableTypeHints: true,
  10. includeInlayPropertyDeclarationTypeHints: true,
  11. includeInlayFunctionLikeReturnTypeHints: true,
  12. includeInlayEnumMemberValueHints: true
  13. });
  14. var editor = monaco.editor.create(document.getElementById('container'), {
  15. model: model,
  16. glyphMargin: true,
  17. renderWhitespace: true
  18. });
  19. editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.F9, function (ctx, args) {
  20. alert('Command Running!!');
  21. console.log(ctx);
  22. });
  23. editor.addAction({
  24. id: 'my-unique-id',
  25. label: 'My Label!!!',
  26. keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.F10],
  27. contextMenuGroupId: 'navigation',
  28. contextMenuOrder: 2.5,
  29. run: function (ed) {
  30. console.log("i'm running => " + ed.getPosition());
  31. }
  32. });
  33. var currentSamplePromise = null;
  34. var samplesData = {};
  35. SAMPLES.sort(function (a, b) {
  36. return a.name.localeCompare(b.name);
  37. }).forEach(function (sample) {
  38. samplesData[sample.name] = function () {
  39. if (currentSamplePromise !== null) {
  40. currentSamplePromise.cancel();
  41. currentSamplePromise = null;
  42. }
  43. currentSamplePromise = sample.loadText().then(function (modelText) {
  44. currentSamplePromise = null;
  45. updateEditor(sample.mimeType, modelText, sample.name);
  46. });
  47. };
  48. });
  49. var examplesComboBox = new ComboBox('Template', samplesData);
  50. var modesData = {};
  51. monaco.languages.getLanguages().forEach(function (language) {
  52. modesData[language.id] = updateEditor.bind(this, language.id);
  53. });
  54. var modesComboBox = new ComboBox('Mode', modesData);
  55. var themesData = {};
  56. themesData['vs'] = function () {
  57. monaco.editor.setTheme('vs');
  58. };
  59. themesData['vs-dark'] = function () {
  60. monaco.editor.setTheme('vs-dark');
  61. };
  62. themesData['hc-black'] = function () {
  63. monaco.editor.setTheme('hc-black');
  64. };
  65. var themesComboBox = new ComboBox('Theme', themesData);
  66. // Do it in a timeout to simplify profiles
  67. window.setTimeout(function () {
  68. var START_SAMPLE = 'Y___DefaultJS';
  69. var url_matches = location.search.match(/sample=([^?&]+)/i);
  70. if (url_matches) {
  71. START_SAMPLE = url_matches[1];
  72. }
  73. if (location.hash) {
  74. START_SAMPLE = location.hash.replace(/^\#/, '');
  75. START_SAMPLE = decodeURIComponent(START_SAMPLE);
  76. }
  77. samplesData[START_SAMPLE]();
  78. examplesComboBox.set(START_SAMPLE);
  79. createOptions(editor);
  80. createToolbar(editor);
  81. }, 0);
  82. function updateEditor(mode, value, sampleName) {
  83. if (sampleName) {
  84. window.location.hash = sampleName;
  85. }
  86. if (typeof value !== 'undefined') {
  87. var oldModel = model;
  88. model = monaco.editor.createModel(value, mode);
  89. editor.setModel(model);
  90. if (oldModel) {
  91. oldModel.dispose();
  92. }
  93. } else {
  94. monaco.editor.setModelLanguage(model, mode);
  95. }
  96. modesComboBox.set(mode);
  97. }
  98. function createToolbar(editor) {
  99. var bar = document.getElementById('bar');
  100. bar.appendChild(examplesComboBox.domNode);
  101. bar.appendChild(modesComboBox.domNode);
  102. bar.appendChild(themesComboBox.domNode);
  103. bar.appendChild(
  104. createButton('Dispose all', function (e) {
  105. editor.dispose();
  106. editor = null;
  107. if (model) {
  108. model.dispose();
  109. model = null;
  110. }
  111. })
  112. );
  113. bar.appendChild(
  114. createButton('Remove Model', function (e) {
  115. editor.setModel(null);
  116. })
  117. );
  118. bar.appendChild(
  119. createButton('Dispose Model', function (e) {
  120. if (model) {
  121. model.dispose();
  122. model = null;
  123. }
  124. })
  125. );
  126. bar.appendChild(
  127. createButton(
  128. 'Ballistic scroll',
  129. (function () {
  130. var friction = 1000; // px per second
  131. var speed = 0; // px per second
  132. var isRunning = false;
  133. var lastTime;
  134. var r = 0;
  135. var scroll = function () {
  136. var currentTime = new Date().getTime();
  137. var ellapsedTimeS = (currentTime - lastTime) / 1000;
  138. lastTime = currentTime;
  139. speed = speed - friction * ellapsedTimeS;
  140. r = r + speed * ellapsedTimeS;
  141. editor.setScrollTop(r);
  142. if (speed >= 0) {
  143. domutils.scheduleAtNextAnimationFrame(scroll);
  144. } else {
  145. isRunning = false;
  146. }
  147. };
  148. return function (e) {
  149. speed += 2000;
  150. if (!isRunning) {
  151. isRunning = true;
  152. r = editor.getScrollTop();
  153. lastTime = new Date().getTime();
  154. domutils.runAtThisOrScheduleAtNextAnimationFrame(scroll);
  155. }
  156. };
  157. })()
  158. )
  159. );
  160. }
  161. function createButton(label, onClick) {
  162. var result = document.createElement('button');
  163. result.innerHTML = label;
  164. result.onclick = onClick;
  165. return result;
  166. }
  167. function createOptions(editor) {
  168. var options = document.getElementById('options');
  169. var lineNumbers;
  170. options.appendChild(
  171. createOptionToggle(
  172. editor,
  173. 'lineNumbers',
  174. function () {
  175. return lineNumbers === false ? false : true;
  176. },
  177. function (editor, newValue) {
  178. lineNumbers = newValue;
  179. editor.updateOptions({ lineNumbers: lineNumbers ? 'on' : 'off' });
  180. }
  181. )
  182. );
  183. var glyphMargin;
  184. options.appendChild(
  185. createOptionToggle(
  186. editor,
  187. 'glyphMargin',
  188. function () {
  189. return glyphMargin === false ? false : true;
  190. },
  191. function (editor, newValue) {
  192. glyphMargin = newValue;
  193. editor.updateOptions({ glyphMargin: glyphMargin });
  194. }
  195. )
  196. );
  197. var minimap;
  198. options.appendChild(
  199. createOptionToggle(
  200. editor,
  201. 'minimap',
  202. function () {
  203. return minimap === false ? false : true;
  204. },
  205. function (editor, newValue) {
  206. minimap = newValue;
  207. editor.updateOptions({ minimap: { enabled: minimap } });
  208. }
  209. )
  210. );
  211. var roundedSelection;
  212. options.appendChild(
  213. createOptionToggle(
  214. editor,
  215. 'roundedSelection',
  216. function () {
  217. return roundedSelection === false ? false : true;
  218. },
  219. function (editor, newValue) {
  220. roundedSelection = newValue;
  221. editor.updateOptions({ roundedSelection: roundedSelection });
  222. }
  223. )
  224. );
  225. var scrollBeyondLastLine;
  226. options.appendChild(
  227. createOptionToggle(
  228. editor,
  229. 'scrollBeyondLastLine',
  230. function () {
  231. return scrollBeyondLastLine === false ? false : true;
  232. },
  233. function (editor, newValue) {
  234. scrollBeyondLastLine = newValue;
  235. editor.updateOptions({ scrollBeyondLastLine: scrollBeyondLastLine });
  236. }
  237. )
  238. );
  239. var renderWhitespace;
  240. options.appendChild(
  241. createOptionToggle(
  242. editor,
  243. 'renderWhitespace',
  244. function () {
  245. return renderWhitespace === true ? true : false;
  246. },
  247. function (editor, newValue) {
  248. renderWhitespace = newValue;
  249. editor.updateOptions({ renderWhitespace: renderWhitespace });
  250. }
  251. )
  252. );
  253. var readOnly;
  254. options.appendChild(
  255. createOptionToggle(
  256. editor,
  257. 'readOnly',
  258. function () {
  259. return readOnly === true ? true : false;
  260. },
  261. function (editor, newValue) {
  262. readOnly = newValue;
  263. editor.updateOptions({ readOnly: readOnly });
  264. }
  265. )
  266. );
  267. var wordWrap;
  268. options.appendChild(
  269. createOptionToggle(
  270. editor,
  271. 'wordWrap',
  272. function () {
  273. return wordWrap === true ? true : false;
  274. },
  275. function (editor, newValue) {
  276. wordWrap = newValue;
  277. editor.updateOptions({ wordWrap: wordWrap ? 'on' : 'off' });
  278. }
  279. )
  280. );
  281. var folding;
  282. options.appendChild(
  283. createOptionToggle(
  284. editor,
  285. 'folding',
  286. function () {
  287. return folding === false ? false : true;
  288. },
  289. function (editor, newValue) {
  290. folding = newValue;
  291. editor.updateOptions({ folding: folding });
  292. }
  293. )
  294. );
  295. var bracketPairColorizationEnabled = false;
  296. options.appendChild(
  297. createOptionToggle(
  298. editor,
  299. 'bracketPairColorizationEnabled',
  300. function () {
  301. return bracketPairColorizationEnabled === false ? false : true;
  302. },
  303. function (editor, newValue) {
  304. bracketPairColorizationEnabled = newValue;
  305. editor.updateOptions({
  306. 'bracketPairColorization.enabled': bracketPairColorizationEnabled
  307. });
  308. }
  309. )
  310. );
  311. }
  312. function createOptionToggle(editor, labelText, stateReader, setState) {
  313. var domNode = document.createElement('div');
  314. domNode.className = 'option toggle';
  315. var input = document.createElement('input');
  316. input.type = 'checkbox';
  317. var label = document.createElement('label');
  318. label.appendChild(input);
  319. label.appendChild(document.createTextNode(labelText));
  320. domNode.appendChild(label);
  321. var renderState = function () {
  322. input.checked = stateReader();
  323. };
  324. renderState();
  325. editor.onDidChangeConfiguration(function () {
  326. renderState();
  327. });
  328. input.onchange = function () {
  329. setState(editor, !stateReader());
  330. };
  331. return domNode;
  332. }
  333. function ComboBox(label, externalOptions) {
  334. this.id = 'combobox-' + label.toLowerCase().replace(/\s/g, '-');
  335. this.domNode = document.createElement('div');
  336. this.domNode.setAttribute('style', 'display: inline; margin-right: 5px;');
  337. this.label = document.createElement('label');
  338. this.label.innerHTML = label;
  339. this.label.setAttribute('for', this.id);
  340. this.domNode.appendChild(this.label);
  341. this.comboBox = document.createElement('select');
  342. this.comboBox.setAttribute('id', this.id);
  343. this.comboBox.setAttribute('name', this.id);
  344. this.comboBox.onchange = function (e) {
  345. var target = e.target || e.srcElement;
  346. this.options[target.options[target.selectedIndex].value].callback();
  347. }.bind(this);
  348. this.domNode.appendChild(this.comboBox);
  349. this.options = [];
  350. for (var name in externalOptions) {
  351. if (externalOptions.hasOwnProperty(name)) {
  352. var optionElement = document.createElement('option');
  353. optionElement.value = name;
  354. optionElement.innerHTML = name;
  355. this.options[name] = {
  356. element: optionElement,
  357. callback: externalOptions[name]
  358. };
  359. this.comboBox.appendChild(optionElement);
  360. }
  361. }
  362. }
  363. ComboBox.prototype.set = function (name) {
  364. if (this.options[name]) {
  365. this.options[name].element.selected = true;
  366. }
  367. };
  368. });