简体   繁体   English

摩纳哥编辑器强制调整大小编辑器

[英]Monaco Editor force resize editor

I use the Monaco Editor .我使用摩纳哥编辑器

My full code is too long to be posted here but this is my settings:我的完整代码太长,无法在此处发布,但这是我的设置:

automaticLayout: true,
folding: false,
theme: 'vs-dark',
lineNumbers: 'off',
minimap: {
    enabled: false
}

Problem问题

  • When I enlarge my window, the Monaco Editor follows as intended.当我放大我的窗口时,摩纳哥编辑器会按预期显示。
  • When I reduce my Window, the Monaco Editor does not resize (height).当我缩小窗口时,摩纳哥编辑器不会调整大小(高度)。

Question问题

How can I force a resize that I can trigger from my script?如何强制调整可以从我的脚本触发的大小?

What I've tried, that did not work我试过的,没用

What did happend was that the console message was output on every window change, but not the Monaco height.发生的事情是每次窗口更改时都会输出控制台消息,而不是摩纳哥高度。

window.onresize = () => {
  console.log('Window resize');
  editor.layout();
});

Demo演示

I enlarge first and then reduce the height by making the developer console larger/smaller.我先放大,然后通过使开发人员控制台更大/更小来降低高度。

在此处输入图片说明

We have grappled with this too.我们也已经解决了这个问题。 The answer is to make the layout take an empty object rather than no parameter, so the resize function becomes答案是让布局带一个空对象而不是没有参数,所以resize函数就变成了

window.onresize = () => {
  console.log('Window resize');
  editor.layout({});
});

Note that typescript does not like this, as the object apparently should contain both width and height.请注意,打字稿不喜欢这样,因为对象显然应该同时包含宽度和高度。

window.onresize = () => {
  console.log('Window resize');
  editor.layout({} as monaco.editor.IDimension);
});

Fixes that issue.修复了这个问题。

I found min-height worked great:我发现 min-height 效果很好:

height:100%
width:100%
min-height:100%
min-width:100%

Can I suggest that you do post the full code using the Stack Overflow snippet/insert code button from the toolbar to give more context.我可以建议您使用工具栏中的 Stack Overflow 代码段/插入代码按钮发布完整代码以提供更多上下文。

I do something similar to help redraw & resize when the window is resized and use .layout() function without any problems.当调整窗口大小并使用 .layout() 函数时,我做了类似的事情来帮助重绘和调整大小,没有任何问题。

As 'editor' could be null or not be widely available in the scope of this window resize function, it would be good to see the code please由于“编辑器”可能为空或在此窗口大小调整功能的范围内无法广泛使用,因此最好查看代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM