Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear existing models, or is there a way to re-Init monaco editor

I'm using ngx-monaco-editor to implement a code editor in a modal. Because I need to support multiple tabs, I need to prepare a map for models to remember the models with Uris. The modal can also be reopened after closing it.

Question: Once I reopen the modal and choose a file previously opened, it will throw error:

Cannot add model because it already exists

I'm not sure why ngx-monaco-editor does not destroy the models after the component is destroyed.

Is there a way that I can manually clear those models before closing the modal.

like image 601
huan feng Avatar asked Jun 19 '20 07:06

huan feng


People also ask

How do I dispose of Monaco editor?

If you have a hash that references the models, you can just call dispose on the model instance via the hash: hashOfModels[uri]. dispose(); Because the models only represent the state of the text file, you also have to stash the editor state (see editor.

How do I use editor in react in Monaco?

The easiest way to use the react-monaco-editor with create-react-app is to use the react-app-rewired project. For setting it up, the following steps are required: Install react-app-rewired : npm install -D react-app-rewired. Replace react-scripts by react-app-rewired in the scripts section of your packages.


1 Answers

Below approach is the way i find so far:

monaco.editor.getModels().forEach(model => model.dispose());

Getting all the models and calling its dispose method in component destroy method.

like image 53
huan feng Avatar answered Oct 26 '22 14:10

huan feng