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

How to make editor autoresizable #82

Open
3xau1o opened this issue Jun 11, 2020 · 21 comments
Open

How to make editor autoresizable #82

3xau1o opened this issue Jun 11, 2020 · 21 comments
Labels
monaco-editor The issue is related to the `monaco-editor` package, it's not related to the wrapper

Comments

@3xau1o
Copy link

3xau1o commented Jun 11, 2020

In the IEditorConstructionOptions has a property called automaticLayout, that enables autoresize
image
as commented here
https://stackoverflow.com/questions/49923334/how-to-make-monaco-editor-auto-fit-content-like-codemirror-auto-resize-mode

how can i pass this boolean property in react component? and will this override the default sizes passed in the monaco-react component?

@suren-atoyan
Copy link
Owner

It's already true. Check it here. But in any case, you can pass options property to the component with certain options. Check it here.

@3xau1o
Copy link
Author

3xau1o commented Jun 11, 2020

I see, thanks for your answer, i'll check what wrong with my code that the editor doesn't autoresize to fit code height

@suren-atoyan
Copy link
Owner

@saulpalv please let me know if you'll find something that we can close the issue. Thank you!

@KalebMatthews
Copy link

The editor will not resize after its initialization because the child elements are setting a static width based on the init.

Component:

<ControlledEditor
            width={"100%"}
            height={height}
            language={language}
            theme="dark"
            value={code}
            options={{
                automaticLayout: true,
            }}
            onChange={onChange}
        />

Rendered Html of monaco-react parent sections:
Screen Shot 2020-06-26 at 1 32 44 PM

Cut off monaco editor display:
Screen Shot 2020-06-26 at 1 34 36 PM

Please let me know if more information is needed.

@3xau1o
Copy link
Author

3xau1o commented Jun 26, 2020

@KalebMatthews thank you for your response, it makes sense now, i'll close the issue as found no solution on monaco, i changed to codemirror wich provides this feature out of the box

@3xau1o 3xau1o closed this as completed Jun 26, 2020
@KalebMatthews
Copy link

@saulpalv Can we keep this open for a chance that it might be resolved in this repo?

@3xau1o 3xau1o reopened this Jun 26, 2020
@suren-atoyan
Copy link
Owner

@saulpalv @KalebMatthews guys it is completely autosizeble. Please, create a reproducible snippet, so I can understand your particular case. For example, check here; move splitter to see how it's autosizeble.

Plus, we already support className and wrapperClassName props, so, you can modify styles.

@3xau1o
Copy link
Author

3xau1o commented Jun 28, 2020

@suren-atoyan hello suren, in your provided example the editor does indeed auto resize, but only the code view, not the monaco container, in other words it's resizing internally/virtually, that's why it has a vertical slider, i seek for autoresize for both the code view and the editor container so all the code is visible and without the slider, this behaviour option is built-in (or default) in editors like codemirror editor -> example and ace editor ->xample, try to add new lines on those examples, you'll get what i mean. Maybe monaco does not have an built in option to enable this behaviour, the container will retain it's fixed size once provided as @KalebMatthews commented .

@suren-atoyan
Copy link
Owner

@KalebMatthews, @saulpalv now I understand what you meant and also found the problem.
Look at this example. We did such kind of thing manually while ago.
But what is interesting is that it works only in old versions on monaco. In the 16th line, you can see that I use the previous version of monaco, you can change 0.19.0 to 0.20.0 (please update the page after that) to see how auto-resize is broken.
I found some issues in the original library. So, they have some problems with automaticLayout option in the 0.20.0 version.

Now, I suggest keeping it open until the 0.21.0 release. After, I'll update the default version of monaco in this library, and if everything is okay, we will close it.

Thank you for your support guys.

@3xau1o
Copy link
Author

3xau1o commented Jun 29, 2020

@suren-atoyan i get the idea with your example and yes it's broken on monaco 0.20.0 as issued :
Auto width editor Not Working in v0.20.0 #1878
automaticLayout strange behavior #1954
automaticLayout broken on master? #1855

shure a solution will come in 0.21.0

@suren-atoyan
Copy link
Owner

suren-atoyan commented Jul 1, 2020

okay, we are on the same line. So, let's leave it open for a while, at least until the 0.21.0 version arrives, after that, I'll update the package and we will see what we have.

@MatthewCaseres
Copy link

MatthewCaseres commented Oct 2, 2020

There is a way to do this, This solution comes from an issue in monaco microsoft/monaco-editor#794.
https://codesandbox.io/s/empty-dust-h6z4y?file=/src/App.js

@arbel03
Copy link

arbel03 commented Oct 12, 2020

I tried to update the package to use monaco-editor 0.21.2 and the issue still persists
It doesn't seem to be the source :\

@Dreamacro
Copy link
Contributor

https://codesandbox.io/s/monaco-editor-react-forked-im1gc

Working at Editor but not at DiffEditor.

@suren-atoyan suren-atoyan added the monaco-editor The issue is related to the `monaco-editor` package, it's not related to the wrapper label Jan 14, 2021
@dio
Copy link

dio commented Feb 7, 2022

Sorry probably this is unrelated, but since it is "hardcoded" to true, can we override it to false and use a custom resize detector (microsoft/monaco-editor#28 (comment))? Ref: microsoft/monaco-editor#28 (comment).

@brycefranzen
Copy link

brycefranzen commented May 3, 2022

@Dreamacro

Working at Editor but not at DiffEditor.

I was able to get it to work on "DiffEditor" like so:

diffEditor.onDidUpdateDiff(() => {
  const originalHeight = originalEditor.getContentHeight();
  const modifiedHeight = modifiedEditor.getContentHeight();
  setHeight(
    Math.max(Math.min(600, Math.max(originalHeight, modifiedHeight)), 200)
  );
  diffEditor.layout();
});

This has a min-height of 200 and a max height of 600. You can play with the values of min/max if you'd like accordingly.

@Dreamacro
Copy link
Contributor

@brycefranzen

In the new version, automaticLayout works well.

<DiffEditor language="json" options={{ automaticLayout: true }} />

@brycefranzen
Copy link

@Dreamacro
automatic layout seems to be working for horizontal resizing, but does nothing for vertically resizing the editor to fit the content height. To auto-resize vertically, you have to calculate the height from the content and then set that height on a wrapping element as the editor auto-grows to the size of its parent.

@Jaryt
Copy link

Jaryt commented Jun 14, 2022

The automaticLayout option doesn't seem to fix it for my solution. You can view my source here: https://github.com/CircleCI-Public/visual-config-editor/blob/main/src/components/panes/EditorPane.tsx

I am using the latest version of the monacoEditor. It seems like I might have to create a resize hook for this, but of course it'd be preferable for the monaco editor can handle its sizing itself.

Some notes: The component will grow and shrink vertically perfectly fine. It will also grow horizontally as expected. The width attribute on the div of class monaco-editor does not shrink.
image

Please let me know if there is anything I'm doing wrong here! :)

@nojaf
Copy link

nojaf commented Apr 18, 2023

@Jaryt were you ever able to solve this problem? I think I'm running into this problem as well.
image

@alswl
Copy link

alswl commented May 12, 2024

@brycefranzen

In the new version, automaticLayout works well.

<DiffEditor language="json" options={{ automaticLayout: true }} />

You are my life saver.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
monaco-editor The issue is related to the `monaco-editor` package, it's not related to the wrapper
Projects
None yet
Development

No branches or pull requests