1

In writing a generative test suite for code editors, I'm trying to programmatically call the event handlers that handle KeyboardEvent's in Monaco. Making a new KeyboardEvent({...}) and dispatching it on the textarea, or root editor element didn't work.

For example, how could we transition the editor through the following states:

  1. Paste "abc" => abc|
  2. Move Cursor by -1 => ab|c
  3. Delete once => a|c
  4. Type "x" => ax|c
den1k
  • 63
  • 1
  • 8

2 Answers2

3

Try

editor.trigger(monaco.KeyMod.CtrlCmd + monaco.KeyCode.KEY_P, 'type', { text: 'some text' });
Chariyski
  • 511
  • 5
  • 6
0

get a list of all supported actions using this code:

  editor.getSupportedActions().forEach((value) => {
        console.log(value);
    });

there is some example of trigger function in Monaco-editor that may be suitable for you:

editor.trigger('keyboard', 'type', {text: 'abc'})

editor.trigger('keyboard', 'deleteLeft', null);

editor.trigger('keyboard', 'deleteRight', null);

editor.trigger('keyboard', 'selectAll', null);

editor.trigger('keyboard', 'tab', null);

editor.trigger('keyboard', 'deleteLeft', null)

editor.trigger('keyboard', 'cursorEnd', null)

editor.trigger('keyboard', 'cursorHome', null)

editor.trigger('keyboard', 'cursorTop', null)

editor.trigger('keyboard', 'cursorBottom', null)

editor.trigger('keyboard', 'undo', null)

editor.trigger('keyboard', 'redo', null)

editor.trigger('source', 'editor.action.formatDocument', null)

editor.trigger('editor', 'gotoLine', {lineNumber: 10, column: 5})
MrBug
  • 63
  • 6