从resizeObserver调用layout()不起作用,但从窗口调用。

发布于 2025-02-09 12:31:26 字数 675 浏览 2 评论 0 原文

在我的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);
}

这很好。我想念什么?

In my react application, give editors is an array of all editors created with monaco.editor.create() that are currently opened in the application, let's say I have this:

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);
    }
  }));

it fires properly but the editor isn't resized. But let's say I'd resize only the current editor:

window.onresize = () => {
  const editor = editors[0];
  editor.layout({} as monaco.editor.IDimension);
}

this works fine. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春花秋月 2025-02-16 12:31:26

在React应用程序中,当您在页面上只有一个可见编辑器时,仅使用一个编辑器。在看不到时,请勿将编辑器实例存储在某个地方进行操作。而是使用 componentDidmount 在(仅)编辑器实例中加载一个新模型,并将编辑器的状态保存在 componentupdated 中。

对于调整大小操作,请在编辑器组件中使用“调整大小”观察者。将其设置在其强制器中:

        if (typeof ResizeObserver !== "undefined") {
            this.resizeObserver = new ResizeObserver(this.handleEditorResize);
        }

并处理该编辑器组件中的调整大小:

    private handleEditorResize = (entries: readonly ResizeObserverEntry[]): void => {
        if (entries.length > 0) {
            const rect = entries[0].contentRect;
            this.editor?.layout({ width: rect.width, height: rect.height });
        }
    };

当组件被卸载时,请不要忘记清理:

    public componentWillUnmount(): void {
        ...
        this.resizeObserver?.unobserve(this.hostRef.current as Element);
    }

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 in componentUpdated. 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:

        if (typeof ResizeObserver !== "undefined") {
            this.resizeObserver = new ResizeObserver(this.handleEditorResize);
        }

and handle the resize in that editor component like this:

    private handleEditorResize = (entries: readonly ResizeObserverEntry[]): void => {
        if (entries.length > 0) {
            const rect = entries[0].contentRect;
            this.editor?.layout({ width: rect.width, height: rect.height });
        }
    };

Don't forget to clean up when the component is unmounted:

    public componentWillUnmount(): void {
        ...
        this.resizeObserver?.unobserve(this.hostRef.current as Element);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文