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

getContentHeight in @monaco-editor/react does not look correct #442

Open
MatrixFun opened this issue Jan 13, 2023 · 3 comments
Open

getContentHeight in @monaco-editor/react does not look correct #442

MatrixFun opened this issue Jan 13, 2023 · 3 comments

Comments

@MatrixFun
Copy link

MatrixFun commented Jan 13, 2023

I'm trying to use https://github.com/suren-atoyan/monaco-react. But I realize that its getContentHeight does not match the behavior described here.

Here is an example: https://stackblitz.com/edit/react-ts-en8wpm?file=EditorBasic.tsx,App.tsx,index.html. It starts with 500px, once we enter a newline, it becomes 518px, then another newline leads to 536px, etc. From what I understand, getContentHeight should not include the height of the editor.

import * as React from 'react';
import MonacoEditor from '@monaco-editor/react';

export default class EditorBasic extends React.Component<{}, {}> {
  editor: any;

  _handleEditorDidMount(editor, monaco) {
    this.editor = editor;
    this.editor.onDidContentSizeChange(this.handleResize.bind(this));
  }

  updateHeight = () => {
    console.log(
      'this.editor.getContentHeight()',
      this.editor.getContentHeight()
    );
  };

  handleResize() {
    if (this.editor && this.editor !== undefined) {
      this.updateHeight();
    }
  }

  render() {
    return (
      <MonacoEditor
        width="400px"
        height="500px"
        options={{ minimap: { enabled: false }, lineNumbers: 'off' }}
        defaultLanguage="javascript"
        defaultValue="content"
        onMount={this._handleEditorDidMount.bind(this)}
        theme="vs-dark"
      />
    );
  }
}

Does anyone know if it's a bug of @monaco-editor/react? Is there any other API to get the real content height?

(* Link in StackOverflow: https://stackoverflow.com/questions/75105858/getcontentheight-in-monaco-editor-react-does-not-look-correct *)

@danbtl
Copy link

danbtl commented Aug 8, 2023

@MatrixFun did you ever find a workaround for this?

@trm217
Copy link

trm217 commented Oct 3, 2023

same issue here, the value getContentHeight seems to increment whenever you enter a new line, no matter if you delete lines…

@fredgig
Copy link

fredgig commented Oct 31, 2024

I encountered the same issue. I needed to use getContentHeight in order to set the editor's height dynamically.

In my case the function returns the correct value when I set the editor's scrollBeyondLastLine option to false:

// ...

const minHeight = 100;

const [height, setHeight] = useState(minHeight);

const updateHeight = (editor: editor.IStandaloneCodeEditor) => setHeight(Math.max(minHeight, editor.getContentHeight()));

return (
  <Editor
    // ...
    options={{
      scrollBeyondLastLine: false,
      // ...
    }}
    height={height}
    onMount={(editor) => editor.onDidContentSizeChange(() => updateHeight(editor))}
    // ...
  />
);

// ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants