Skip to content

Commit d6be3fe

Browse files
committed
feat: add a prop preventing the monaco created internally from being disposed
1 parent 9ccd395 commit d6be3fe

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/MonacoEditor.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ export interface MonacoEditorProps {
9494
* Default is opening a new editor in a popup
9595
*/
9696
onEditorOpenRequest?: (model: monaco.editor.ITextModel, options: IEditorOptions | undefined, source: monaco.editor.ICodeEditor, sideBySide?: boolean) => Promise<monaco.editor.ICodeEditor | null>
97+
98+
/**
99+
* Dispose
100+
*/
101+
disposeModels?: boolean
97102
}
98103

99104
function MonacoEditor ({
@@ -109,7 +114,8 @@ function MonacoEditor ({
109114
markers,
110115
saveViewState = defaultSaveViewState,
111116
restoreViewState = defaultRestoreViewState,
112-
onEditorOpenRequest
117+
onEditorOpenRequest,
118+
disposeModels = true
113119
}: MonacoEditorProps, ref: ForwardedRef<monaco.editor.IStandaloneCodeEditor>): ReactElement {
114120
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor>()
115121
const modelRef = useRef<monaco.editor.ITextModel>()
@@ -155,6 +161,9 @@ function MonacoEditor ({
155161
lastRestoreViewState(editorRef.current, model)
156162
}
157163
return () => {
164+
if (!disposeModels) {
165+
return
166+
}
158167
lastSaveViewState(editorRef.current!, model)
159168
if (existingModel == null) {
160169
// Only dispose if we are the one who created the model
@@ -166,7 +175,7 @@ function MonacoEditor ({
166175
editorRef.current?.setModel(null)
167176
}
168177
return undefined
169-
}, [monacoLanguage, modelUri, valueRef, lastSaveViewState, lastRestoreViewState])
178+
}, [monacoLanguage, modelUri, valueRef, lastSaveViewState, lastRestoreViewState, disposeModels])
170179

171180
// Create editor
172181
useEffect(() => {

0 commit comments

Comments
 (0)