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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <!DOCTYPE html>
  2. <!-- THIS IS A GENERATED FILE VIA `npm run simpleserver` -->
  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 `npm run simpleserver`
  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(
  46. monaco.KeyCode.Tab,
  47. function () {
  48. // services available in `ctx`
  49. alert('my command is executing!');
  50. },
  51. 'myCondition1 && myCondition2'
  52. );
  53. myCondition1.set(true);
  54. setTimeout(function () {
  55. alert('now enabling also myCondition2, try pressing Tab!');
  56. myCondition2.set(true);
  57. // you can use myCondition2.reset() to go back to the default
  58. }, 2000);
  59. /*----------------------------------------SAMPLE JS END*/
  60. });
  61. </script>
  62. </body>
  63. </html>