Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DiffEditor fix: set correct model before value update #479

Merged

Conversation

StefanDBTLabs
Copy link
Contributor

When trying to keep the models in the DiffEditor the code sometimes updated the incorrect model with the incorrect value because it assumes the correct model is loaded. This update checks and sets the correct model for the passed modelpath before allowing updates to be made.

This addresses the issue described in #478

@suren-atoyan
Copy link
Owner

@StefanDBTLabs thanks for opening PR 🙌 I'll keep you up to date once I check it.

@suren-atoyan
Copy link
Owner

hey @StefanDBTLabs 👋

I checked your report and I can confirm that there is an issue. At the same time, the issue isn't related to keepOriginalModelPath or keepModifiedModelPath. These properties basically tell the component to keep the models after unmounting. In your case, there is no unmount, you just update the same DiffEditor component. The issue is in tracking path (originalModelPath and modifiedModelPath). Whenever the user provides a new path, we need to create (or get if exists) a new model with the provided path. That part is missing in DiffEditor implementation. Here you can see how it's implemented in the Editor component. So, basically, you need two more useUpdates with originalModelPath and modifiedModelPath instead of checking them whenever modified or original properties are changed - the worst thing here is that in the controlled mode you will do these model-related checks on each keypress; that's not good, these are two unrelated side effects and should be handled with independent useEffects (useUpdates in this particular case).

Revert your changes and add these two useUpdates before disposeEditor:

  // ...

  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(...)

Let me know if this makes sense and thanks a lot for your effort 🙂

@StefanDBTLabs
Copy link
Contributor Author

thanks for helping to debug this issue.

@suren-atoyan suren-atoyan merged commit 6970e45 into suren-atoyan:master Apr 28, 2023
@suren-atoyan
Copy link
Owner

merged 🥳 I'll let you know once I publish a new version

@CaptainArni
Copy link

Is there any estimate on when the new version will be published?

@suren-atoyan
Copy link
Owner

@StefanDBTLabs @CaptainArni it's published 🎉 check v4.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants