From 6970e4528b018478ddf79cddd44f7b6322fd4ecb Mon Sep 17 00:00:00 2001 From: Stefan Hayden <113928239+StefanDBTLabs@users.noreply.github.com> Date: Fri, 28 Apr 2023 01:41:31 -0400 Subject: [PATCH] DiffEditor fix: set correct model before value update (#479) * DiffEditor fix: set correct model before value update * Revert "DiffEditor fix: set correct model before value update" This reverts commit ca00f9bc46846d8997159197c20b6c7888b1fe6e. * Updated code provided by suren-atoyan https://github.com/suren-atoyan/monaco-react/pull/479#issuecomment-1518988140 --- src/DiffEditor/DiffEditor.tsx | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/DiffEditor/DiffEditor.tsx b/src/DiffEditor/DiffEditor.tsx index a1e2abc..7d27264 100644 --- a/src/DiffEditor/DiffEditor.tsx +++ b/src/DiffEditor/DiffEditor.tsx @@ -165,6 +165,46 @@ function DiffEditor({ !isMonacoMounting && !isEditorReady && createEditor(); }, [isMonacoMounting, isEditorReady, createEditor]); + useUpdate( + () => { + if (editorRef.current && monacoRef.current) { + const originalEditor = editorRef.current.getOriginalEditor(); + const model = getOrCreateModel( + monacoRef.current, + original || '', + originalLanguage || language || 'text', + originalModelPath || '', + ); + + if (model !== originalEditor.getModel()) { + originalEditor.setModel(model); + } + } + }, + [originalModelPath], + isEditorReady, + ); + + useUpdate( + () => { + if (editorRef.current && monacoRef.current) { + const modifiedEditor = editorRef.current.getModifiedEditor(); + const model = getOrCreateModel( + monacoRef.current, + modified || '', + modifiedLanguage || language || 'text', + modifiedModelPath || '', + ); + + if (model !== modifiedEditor.getModel()) { + modifiedEditor.setModel(model); + } + } + }, + [modifiedModelPath], + isEditorReady, + ); + function disposeEditor() { const models = editorRef.current?.getModel();