sample.js 888 B

123456789101112131415161718192021222324252627282930313233343536
  1. var jsCode = [
  2. '"use strict";',
  3. 'function Person(age) {',
  4. ' if (age) {',
  5. ' this.age = age;',
  6. ' }',
  7. '}',
  8. 'Person.prototype.getAge = function () {',
  9. ' return this.age;',
  10. '};'
  11. ].join('\n');
  12. var editor = monaco.editor.create(document.getElementById('container'), {
  13. value: jsCode,
  14. language: 'javascript'
  15. });
  16. var myCondition1 = editor.createContextKey(/*key name*/ 'myCondition1', /*default value*/ false);
  17. var myCondition2 = editor.createContextKey(/*key name*/ 'myCondition2', /*default value*/ false);
  18. editor.addCommand(
  19. monaco.KeyCode.Tab,
  20. function () {
  21. // services available in `ctx`
  22. alert('my command is executing!');
  23. },
  24. 'myCondition1 && myCondition2'
  25. );
  26. myCondition1.set(true);
  27. setTimeout(function () {
  28. alert('now enabling also myCondition2, try pressing Tab!');
  29. myCondition2.set(true);
  30. // you can use myCondition2.reset() to go back to the default
  31. }, 2000);