diff --git a/src/DiffEditor/DiffEditor.tsx b/src/DiffEditor/DiffEditor.tsx index 6ac0ffc..1ef471c 100644 --- a/src/DiffEditor/DiffEditor.tsx +++ b/src/DiffEditor/DiffEditor.tsx @@ -194,21 +194,31 @@ function DiffEditor({ }, [isEditorReady]); useEffect(() => { - !isMonacoMounting && !isEditorReady && createEditor(); + if (!isMonacoMounting && !isEditorReady) { + createEditor(); + } }, [isMonacoMounting, isEditorReady, createEditor]); function disposeEditor(): void { - const models = editorRef.current?.getModel(); + const editor = editorRef.current; + const models = editor?.getModel(); + + // oxlint-disable-next-line unicorn/no-null -- Monaco detaches diff models with null. + editor?.setModel(null); + editor?.dispose(); - if (!keepCurrentOriginalModel) { + if (!keepCurrentOriginalModel && !models?.original.isDisposed()) { models?.original?.dispose(); } - if (!keepCurrentModifiedModel) { + if (!keepCurrentModifiedModel && !models?.modified.isDisposed()) { models?.modified?.dispose(); } - editorRef.current?.dispose(); + // oxlint-disable-next-line unicorn/no-null -- React refs use null after unmount. + editorRef.current = null; + preventCreation.current = false; + setIsEditorReady(false); } return (