monaco-react
monaco-react copied to clipboard
[DiffEditor] Missing onChange property/functionality
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:
- Go to any playground/sandbox
- Change the default editor with DiffEditor
- Adds an onChange props with some console.log (following specs from Editor)
- Click on right-side panel and types some texts
- 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.
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
need onChange
props too
up
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
this is how it was implemented in my project https://github.com/retejs/rete-studio/blob/e23b692e3d6af1541f8d1eb5f3c9187cac2cca28/src/Lab.tsx#L79