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

Auto resizing with content size #103

Closed
akosyakov opened this issue Aug 10, 2016 · 16 comments
Closed

Auto resizing with content size #103

akosyakov opened this issue Aug 10, 2016 · 16 comments
Assignees
Labels
editor-core feature-request Request for new features or functionality

Comments

@akosyakov
Copy link

I need to show a complete model in the editor without vertical scrolling. I can achieve it by listening to content changes of a model and calling layout with a recomputed height as lineCount * lineHeight. I wonder is the more elegant way to achieve it?

@AndersMad
Copy link

you can editor.getScrollHeight(), then set the height of the container (used in the create call) and then editor.layout();

@alexdima
Copy link
Member

At this point, that is the only way. Would be nice to have an autoSize: true|false flag that would do that for you

@alexdima alexdima added the feature-request Request for new features or functionality label Aug 15, 2016
@akosyakov
Copy link
Author

I noticed the following issues with auto resizing, disabled scrolling and scrollBeyondLastLine as false:

  • focusing for last line is broken, one can focus only by clicking on the top of line, with one line it gets really annoying;
  • if a editor height is greater than a content height than clicking on the part below content does not focus on the content, would be nice if it focus on the last line.

@alexdima
Copy link
Member

@akosyakov I'm thinking the mouse hits the horizontal scrollbar. Would you expect the content to have an extra 7px or the height of the horizontal scrollbar to avoid this?

@akosyakov
Copy link
Author

@alexandrudima will try now

@akosyakov
Copy link
Author

@alexandrudima you are right, i've adjusted the formula to add horizontalScrollbarHeight and it looks better. I would expect that it happens automatically if autoSize set to true.

@akosyakov akosyakov changed the title resize on a content change auto resizing Aug 17, 2016
@akosyakov
Copy link
Author

also it would be nice to provide minHeightInLines, so in spite of a content is empty, an editor height can be for example 1.5*lineHeight

@akosyakov
Copy link
Author

akosyakov commented Aug 22, 2016

Another issue: Page scrolling does not work with auto resize and disabled scrolling.
Steps to reproduce: disable scrolling try to scroll a page down when a mouse is over an editor.

Filed a separate issue for it: #116

@jws305
Copy link

jws305 commented Feb 14, 2017

@akosyakov I got this working when I add content to the model however, when I delete lines, the container will never shrink. Were you able to work around that?

@akosyakov
Copy link
Author

@jws305 Yes, it works. Could it be that you did not hook it up at right places? We do autoresize whenever an editor configuration, an editor model or a model value changed.

The code that computes a height is here: https://github.com/R-Brain/jupyterlab/blob/master/src/monaco/editor.ts#L470. You can ignore boxSizing.

@jws305
Copy link

jws305 commented Feb 15, 2017

@akosyakov Thanks! That works much better than what I had come up with:

this.$element.height(0);
this.editor.layout();
let height = this.editor.getScrollHeight();
this.$element.height(height + 7);
this.editor.layout();

@alexdima alexdima modified the milestone: Backlog Jun 7, 2017
@khellinx
Copy link

@akosyakov That code doesn't seem to exist anymore.
Care to share the original code?

Tnx!

@khellinx
Copy link

Cool, thanks!
I tried to accomplish the exact same thing, but I couldn't find the line height in the layoutinfo.
Never thought to check the editor interface.

Thanks!

@blois
Copy link

blois commented Nov 15, 2018

Some additional comments:

I believe that the performant way is to calculate the height using @akosyakov's approach:

let lastResizedLineCount = 0;
model.onDidChangeContent(() => {
    let lineCount = model.getLineCount();
    // Do not invalidate layout if the line count hasn't changed, as this method
    // will be called for every keypress and layouts are too expensive.
    if (lineCount == lastResizedLineCount) {
        return;
    }
    lastResizedLineCount = lineCount;

    const configuration = editor.getConfiguration();
    const lineHeight = configuration.lineHeight;
    const contentHeight = lineHeight * lineCount;

    const horizontalScrollbarHeight = configuration.layoutInfo.horizontalScrollbarHeight;
    const height = contentHeight + horizontalScrollbarHeight;

    editorElement.style.height = `${height}px`;
    editor.layout({
      height,
      width: this.getElement().clientWidth,
    });
});

But this doesn't work with line-wrapping because the line count is dependent on the length of individual lines.

For that, I believe you have to use the line count from the ViewModel:

const lineCount = editor._modelData.viewModel.getLineCount()

Would really like to know if there's a better way which doesn't use the private _modelData member.

@alexdima alexdima modified the milestones: Backlog, On Deck Mar 1, 2019
@alexdima alexdima self-assigned this Mar 1, 2019
@alexdima alexdima changed the title auto resizing Auto resizing with growing content Dec 10, 2019
@alexdima alexdima changed the title Auto resizing with growing content Auto resizing with content size Dec 10, 2019
@alexdima
Copy link
Member

Let's track in #794 , which even though is a younger issue, has more upvotes.

@alexdima alexdima removed this from the On Deck milestone Dec 11, 2019
@vscodebot vscodebot bot locked and limited conversation to collaborators Jan 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
editor-core feature-request Request for new features or functionality
Projects
None yet
Development

No branches or pull requests

6 participants