interacting-with-the-editor-adding-a-command-to-an-editor-instance.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <!DOCTYPE html>
  2. <!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->
  3. <html>
  4. <head>
  5. <base href="..">
  6. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  7. </head>
  8. <body>
  9. <style>
  10. /*----------------------------------------SAMPLE CSS START*/
  11. /*----------------------------------------SAMPLE CSS END*/
  12. </style>
  13. <a class="loading-opts" href="playground.generated/index.html">[&lt;&lt; BACK]</a> <br/>
  14. THIS IS A GENERATED FILE VIA gulp generate-test-samples
  15. <div id="bar" style="margin-bottom: 6px;"></div>
  16. <div style="clear:both"></div>
  17. <div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">
  18. <!-- ----------------------------------------SAMPLE HTML START-->
  19. <div id="container" style="height:100%;"></div>
  20. <!-- ----------------------------------------SAMPLE HTML END-->
  21. </div>
  22. <div style="clear:both"></div>
  23. <script src="../metadata.js"></script>
  24. <script src="dev-setup.js"></script>
  25. <script>
  26. loadEditor(function() {
  27. /*----------------------------------------SAMPLE JS START*/
  28. var jsCode = [
  29. '"use strict";',
  30. 'function Person(age) {',
  31. ' if (age) {',
  32. ' this.age = age;',
  33. ' }',
  34. '}',
  35. 'Person.prototype.getAge = function () {',
  36. ' return this.age;',
  37. '};'
  38. ].join('\n');
  39. var editor = monaco.editor.create(document.getElementById("container"), {
  40. value: jsCode,
  41. language: "javascript"
  42. });
  43. var myCondition1 = editor.createContextKey(/*key name*/'myCondition1', /*default value*/false);
  44. var myCondition2 = editor.createContextKey(/*key name*/'myCondition2', /*default value*/false);
  45. editor.addCommand(monaco.KeyCode.Tab, function() {
  46. // services available in `ctx`
  47. alert('my command is executing!');
  48. }, 'myCondition1 && myCondition2')
  49. myCondition1.set(true);
  50. setTimeout(function() {
  51. alert('now enabling also myCondition2, try pressing Tab!');
  52. myCondition2.set(true);
  53. // you can use myCondition2.reset() to go back to the default
  54. }, 2000);
  55. /*----------------------------------------SAMPLE JS END*/
  56. });
  57. </script>
  58. </body>
  59. </html>