2

I have a React application in which I'm trying to manually call undo/redo from code on an instance of a Monaco editor.

I want to do this so I can perform undo/redo when the editor is not in focus.

I've searched the API and did not find a way, how can I do this?

2 Answers 2

6

You can call IEditor.trigger, and use undo or redo handlers.

Example, where editor implements IEditor:

// Undo
editor.trigger("myapp", "undo");

// Redo
editor.trigger("myapp", "redo");

Source: https://github.com/microsoft/monaco-editor/issues/451#issuecomment-305445971

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Unfortunately, I don't have access to the project anymore so I can't check whether this works. I'll mark it as the answer because it seems pretty straightforward :)
For some reason it logs a console error: editorRef.current.trigger(...) is not a function. But then it does the action. Setting editorRef.current = editor in onMount. Maybe all this calls for a separate SO question, but I wouldn't say it bothers me that much.
0

Also, you can use the editor's model. It has everything you need:

const model = editor.getModel();
model.canUndo();
model.canRedo();
model.undo();
model.redo();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.