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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With