suren-atoyan / monaco-react

Monaco Editor for React - use the monaco-editor in any React application without needing to use webpack (or rollup/parcel/etc) configuration files / plugins
https://monaco-react.surenatoyan.com/
MIT License
3.74k stars 265 forks source link

get path as 3rd parameter in onChange event #404

Open codesiddhant opened 2 years ago

codesiddhant commented 2 years ago

Is your feature request related to a problem? Please describe. i had modified the original source code manage to get path on every change event.

Describe the solution you'd like i want you to accept my pull request, we can get this feature directly from this repository

Describe alternatives you've considered previously. we have to use editor.onDidChangeModelContent function for get the path of recently changed events

Additional context its to have the path of change just onchange event it helps in various aspect. like we can access states in onchange method which is no possible in editor.onDidChangeModelContent image

suren-atoyan commented 2 years ago

Hello @codesiddhant 👋

Thanks for opening this issue. The idea of having the current model's path in onChange is really interesting.

But first of all, I want to emphasize the fact that we can get the current mode's path in onChange with the library's current version. Based on the fact that the editor will use only one model at the given time, we can do the following:

function App() {
  const editorRef = useRef(null);

  function handleEditorDidMount(editor) {
    editorRef.current = editor; 
  }

 function handleEditorChange(value) {
   const { path } = editorRef.current.getModel().uri;

   console.log(path, value);
 }

  return (
    <Editor
      height="90vh"
      defaultLanguage="javascript"
      defaultValue="// some comment"
      onMount={handleEditorDidMount}
      onChange={handleEditorChange}
    />
  );
}

Having path (or URI) as third argument in onChange makes sense, but the question is should I expose path, URI, or the entire model?