从resizeObserver调用layout()不起作用,但从窗口调用。
在我的React应用程序中,给出编辑器
是当前在应用程序中打开的 Monaco.editor.create()创建的所有编辑器的数组,它是我有这样的:
const resizeObserver = useRef(new ResizeObserver(entries => {
console.log('entries = ', entries);
for(let entry of entries) {
console.log('target = ', entry.target);
const editor = getEditor(entry.target.id);
console.log('editor = ', editor);
editor.layout({} as monaco.editor.IDimension);
}
}));
它可以正确发射,但编辑器没有调整大小。但是,假设我只调整当前编辑器的大小:
window.onresize = () => {
const editor = editors[0];
editor.layout({} as monaco.editor.IDimension);
}
这很好。我想念什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在React应用程序中,当您在页面上只有一个可见编辑器时,仅使用一个编辑器。在看不到时,请勿将编辑器实例存储在某个地方进行操作。而是使用
componentDidmount
在(仅)编辑器实例中加载一个新模型,并将编辑器的状态保存在componentupdated
中。对于调整大小操作,请在编辑器组件中使用“调整大小”观察者。将其设置在其强制器中:
并处理该编辑器组件中的调整大小:
当组件被卸载时,请不要忘记清理:
In a React application, when you only have one visible editor on a page, only use one editor. Don't store editor instances somewhere for manipulations, when they are not visible. Instead use
componentDidMount
to load a new model in the (only) editor instance and save state of the editor incomponentUpdated
. Here's a code editor component, which does all that.For the resize operation use a resize observer in the editor component. Set it up in its constuctor:
and handle the resize in that editor component like this:
Don't forget to clean up when the component is unmounted: