sample.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var editor = monaco.editor.create(document.getElementById("container"), {
  2. value: [
  3. '',
  4. 'class Example {',
  5. '\tprivate m:number;',
  6. '',
  7. '\tpublic met(): string {',
  8. '\t\treturn "Hello world!";',
  9. '\t}',
  10. '}'
  11. ].join('\n'),
  12. language: "typescript"
  13. });
  14. // Explanation:
  15. // Try right clicking on an identifier or keyword => the action will be enabled (due to `tokensAtPosition`)
  16. // Try right clicking on a string => the action will be disabled (due to `tokensAtPosition`)
  17. // Try right clicking on whitespace => the action will be disabled (due to `wordAtPosition`)
  18. // Press F1 (Alt-F1 in IE) => the action will appear and run if it is enabled
  19. // Press Ctrl-F10 => the action will run if it is enabled
  20. editor.addAction({
  21. // An unique identifier of the contributed action.
  22. id: 'my-unique-id',
  23. // A label of the action that will be presented to the user.
  24. label: 'My Label!!!',
  25. // An optional array of keybindings for the action.
  26. keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.F10],
  27. keybindingContext: null,
  28. // Control if the action should show up in the context menu and where.
  29. // Built-in groups:
  30. // 1_goto/* => e.g. 1_goto/1_peekDefinition
  31. // 2_change/* => e.g. 2_change/2_format
  32. // 3_edit/* => e.g. 3_edit/1_copy
  33. // 4_tools/* => e.g. 4_tools/1_commands
  34. // You can also create your own group.
  35. // Defaults to null (don't show in context menu).
  36. contextMenuGroupId: '2_change/2_bla',
  37. // Method that will be executed when the action is triggered.
  38. // @param editor The editor instance is passed in as a convinience
  39. run: function(ed) {
  40. alert("i'm running => " + ed.getPosition());
  41. return null;
  42. },
  43. // Optional enablement conditions. All members are optional
  44. enablement: {
  45. // The action is enabled only if text in the editor is focused (e.g. blinking cursor).
  46. // Warning: This condition will be disabled if the action is marked to be displayed in the context menu
  47. // Defaults to false.
  48. textFocus: true,
  49. // The action is enabled only if the editor or its widgets have focus (e.g. focus is in find widget).
  50. // Defaults to false.
  51. //widgetFocus: true,
  52. // The action is enabled only if the editor is not in read only mode.
  53. // Defaults to false.
  54. //writeableEditor: true,
  55. // The action is enabled only if the cursor position is over a word (i.e. not whitespace).
  56. // Defaults to false.
  57. wordAtPosition: true,
  58. // The action is enabled only if the cursor position is over tokens of a certain kind.
  59. // Defaults to no tokens required.
  60. tokensAtPosition: ['identifier', '', 'keyword'],
  61. }
  62. });