monaco-react icon indicating copy to clipboard operation
monaco-react copied to clipboard

[DiffEditor] Missing onChange property/functionality

Open avalenti89 opened this issue 3 years ago • 5 comments

Monaco Editor allows the use of the right-side panel in DiffEditor to edit the code viewing changes in realtime.

Steps to reproduce the behavior:

  1. Go to any playground/sandbox
  2. Change the default editor with DiffEditor
  3. Adds an onChange props with some console.log (following specs from Editor)
  4. Click on right-side panel and types some texts
  5. No logs were write on console

Use this codesandbox as base to reproduce steps codesandbox.

Expected behavior I expext that "onChange" will be triggered as happens with Editor, and as happens with other monaco-editor wrapper libraries.

DiffEditor missing "onChange" type too, so typescript complaints about it.

avalenti89 avatar Aug 24 '21 09:08 avalenti89

Hello @avalenti89, you are right, there is no onChange for DiffEditor. I'll add it in the next version, but before that, you are able to add it manually; check this

suren-atoyan avatar Aug 25 '21 05:08 suren-atoyan

need onChange props too

cwtuan avatar May 02 '23 11:05 cwtuan

up

Ni55aN avatar Jun 18 '23 18:06 Ni55aN

You could use something like this,


  function handleEditorDidMount(editor, monaco) {
    console.log('handleEditorDidMount')
    // here is another way to get monaco instance
    // you can also store it in `useRef` for further usage
    monacoRef.current = monaco;
    const ed = editor.getModel().modified;
    ed.onDidChangeContent((event) => {
      onChange(ed.getValue());
    });
  }

Note that this can cause memory leak since too many listeners can be added with onDidChangeContent

eugeniol avatar Aug 25 '23 13:08 eugeniol

this is how it was implemented in my project https://github.com/retejs/rete-studio/blob/e23b692e3d6af1541f8d1eb5f3c9187cac2cca28/src/Lab.tsx#L79

Ni55aN avatar Aug 25 '23 15:08 Ni55aN