-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Can editor stretch to fit its content? .editor{height: auto} #794
Comments
A specific height is needed for container. Code could be hundreds of lines, and makes no sense to stretch editor by its content. |
@troy351 it is possible to get the size of the editor and then maybe decide to resize the container up to a certain limit, much like this github comment works ? |
|
@troy351 This will give incorrect results if you have collapsed content |
@David-Mulder Good catch. // disable scroll beyond last line
editor.updateOptions({ scrollBeyondLastLine: false });
const LINE_HEIGHT = 18;
const CONTAINER_GUTTER = 10;
const el = editor.domElement;
const codeContainer = el.getElementsByClassName('view-lines')[0];
let prevLineCount = 0;
editor.onDidChangeModelDecorations(() => {
// wait until dom rendered
setTimeout(() => {
const height =
codeContainer.childElementCount > prevLineCount
? codeContainer.offsetHeight // unfold
: codeContainer.childElementCount * LINE_HEIGHT + CONTAINER_GUTTER; // fold
prevLineCount = codeContainer.childElementCount;
el.style.height = height + 'px';
editor.layout();
}, 0);
}); BTW, a |
I added an extra check that if |
@abersnaze Could you provide the complete code sample? Thanks |
@troy351 this actually works with word wrap enabled as well, nice job |
In 0.19.3 the following seems to work without touching DOM to guess what the editor size is:
|
Starting with 0.20.x, there is At https://microsoft.github.io/monaco-editor/playground.html const width = 300;
const container = document.getElementById('container');
container.style.border = '1px solid #f00';
const editor = monaco.editor.create(container, {
value: "function hello() {\n\talert('Hello world!');\n}",
language: "javascript",
scrollBeyondLastLine: false,
wordWrap: 'on',
wrappingStrategy: 'advanced',
minimap: {
enabled: false
},
overviewRulerLanes: 0
});
let ignoreEvent = false;
const updateHeight = () => {
const contentHeight = Math.min(1000, editor.getContentHeight());
container.style.width = `${width}px`;
container.style.height = `${contentHeight}px`;
try {
ignoreEvent = true;
editor.layout({ width, height: contentHeight });
} finally {
ignoreEvent = false;
}
};
editor.onDidContentSizeChange(updateHeight);
updateHeight(); |
contentSizeChange event, adopt the new padding option to do away with the TopMargin hack Monaco 0.20 introduced a `getContentHeight` API that enables us to get the current editor height without resorting to hacks around line counts. This also handles cases such as code folding. See this issue for details: microsoft/monaco-editor#794 Also added more type checking in the editor instantiation, leveraged the new padding option to do away with the TopMargin work around.
@alexdima the solution doesn't seem to work for the diff editor. I get getContentHeight and onDidContentSizeChange are not defined for the diff editor. Any clues on how that could work? |
For diff editor, you can get the top of the last line of the two editors respectively and take the maximum value container.style.height = Math.max(
diffEditor.getOriginalEditor().getTopForLineNumber(diffEditor.getModel().original.getLineCount()),
diffEditor.getModifiedEditor().getTopForLineNumber(diffEditor.getModel().modified.getLineCount()),
) + 19 + 'px';
diffEditor.layout(); Or calculate from let lineCount = {
original: diffEditor.getModel().original.getLineCount(),
modified: diffEditor.getModel().modified.getLineCount(),
};
for (let change of diffEditor.getLineChanges()) {
if (!change.modifiedEndLineNumber) lineCount.modified += change.originalEndLineNumber - change.originalStartLineNumber + 1;
else if (!change.originalEndLineNumber) lineCount.original += change.modifiedEndLineNumber- change.modifiedStartLineNumber + 1;
else {
let diff = (change.originalEndLineNumber - change.originalStartLineNumber) - (change.modifiedEndLineNumber- change.modifiedStartLineNumber);
if (diff > 0) lineCount.modified += diff;
else lineCount.original -= diff;
}
}
container.style.height = Math.max(
lineCount.original,
lineCount.modified,
) * 19 + 'px';
diffEditor.layout(); Note that IDiffEditor doesn't provide |
This is perfect with renderSideBySide: false but something else seems necessary for renderSideBySide: true |
const originalLineCount = editorInstance.getModel()?.original.getLineCount() || 0;
let finalLineCount = originalLineCount;
for (let change of editorInstance.getLineChanges() || []) {
let diff = change.modifiedEndLineNumber - change.modifiedStartLineNumber + 1;
finalLineCount += Math.abs(diff);
}
editor.value.style.height = finalLineCount * 18 + "px"; Seems to work for me, though haven't tested with many different diffs yet |
It doesn't work. For example, one empty line in original and two empty lines in modified got finalLineCount=1, but should be 2. |
I realized that if we remove const width = 300;
const container = document.getElementById('container');
container.style.border = '1px solid #f00';
const editor = monaco.editor.create(container, {
value: "abc",
language: "javascript",
scrollBeyondLastLine: false,
wrappingStrategy: 'advanced',
minimap: {
enabled: false
},
overviewRulerLanes: 0
});
let ignoreEvent = false;
const updateHeight = () => {
console.log("updateHeight is triggered")
const contentHeight = Math.min(1000, editor.getContentHeight());
container.style.width = `${width}px`;
container.style.height = `${contentHeight}px`;
try {
ignoreEvent = true;
editor.layout({ width, height: contentHeight });
} finally {
ignoreEvent = false;
}
};
editor.onDidContentSizeChange(updateHeight);
updateHeight(); |
seems we can omit arguments for
editor.layout(); |
monaco-editor version: 0.10.1
div.editor{height: auto}
It seems like the monaco-eidtor's height depends on its container's height,
but its container's height is auto, I want the container(div.editor)'s height could be stretched by its content.
The text was updated successfully, but these errors were encountered: