简体   繁体   English

用React.lazy加载CKEditor5组件

[英]loading CKEditor5 components with React.lazy

In my project I want to use CKEditor5. 在我的项目中,我想使用CKEditor5。 Problem is, this version is not compatible with IE11 so my goal is to lazy load CKEditor5 components and when IE11 is detected, I dont want to simply load those components. 问题是,此版本与IE11不兼容,因此我的目标是延迟加载CKEditor5组件,并且当检测到IE11时,我不想简单地加载这些组件。

When component is imported like this import CKEditor from "@ckeditor/ckeditor5-react"; 当这样import CKEditor from "@ckeditor/ckeditor5-react";组件时, import CKEditor from "@ckeditor/ckeditor5-react"; it just importing class, however with React.lazy import component is wrapped with React.LazyExoticComponent . 它只是导入类,但是使用React.lazy导入组件被React.LazyExoticComponent包装。

I need to create instance of GFMDataProcessor according to documentation https://ckeditor.com/docs/ckeditor5/latest/features/markdown.html 我需要根据文档https://ckeditor.com/docs/ckeditor5/latest/features/markdown.html创建GFMDataProcessor实例

But with dynamic import I am not able to do that, since this line of code throws an error and I dont know what argument should I pass, since GFMDataProcessor is React.LazyExoticComponent and not GFMDataProcessor class. 但是对于动态导入,我无法执行此操作,因为这行代码会引发错误,并且我不知道应该传递什么参数,因为GFMDataProcessorReact.LazyExoticComponent而不是GFMDataProcessor类。

//Expected 1 arguments, but got 0  
const markdownPlugin = (editor) => { editor.data.processor = new GFMDataProcessor() }

Other problem is with my configuration for CKEditor, it has to be lazy loaded also and here is the same problem as above, ClassicEditor is again React.LazyExoticComponent instead of class and I have to pass to editor property imported component, not the wrapped one with React.LazyExoticComponent . 另一个问题是我对CKEditor的配置,它也必须被延迟加载,这与上面的问题相同, ClassicEditor还是React.LazyExoticComponent而不是类,我必须传递给editor属性导入的组件,而不是包装的React.LazyExoticComponent Is there some way how I can extract dynamically imported component from wrapped one or any other way how can this be solved? 有什么方法可以从包装的一个或任何其他方法中提取动态导入的组件,如何解决呢?

const ckeditorProps = {
        data: data,
        editor: ClassicEditor,
        onChange: (event, editor) => {
            const data2 = editor.getData();
            if (data2 !== data) {
                this.onChange(data2, this.state.selectedCultureCode, true);
            }
        },
        config: editorConfiguration
    }

Here are my dynamic imports 这是我的动态导入

const CKEditor = React.lazy(() => import("@ckeditor/ckeditor5-react"));
const ClassicEditor = React.lazy(() => import("@ckeditor/ckeditor5-build-classic"));
const GFMDataProcessor = React.lazy(() => import("@ckeditor/ckeditor5-markdown-gfm/src/gfmdataprocessor"));

Usage in DOM structure 在DOM结构中的用法

<React.Suspense fallback={<div>Loading...</div>}>
    <CKEditor {...ckeditorProps} />
</React.Suspense>

I don't know why you wrap anything you want to be fetched asynchronously into the React.lazy components. 我不知道为什么将要异步获取的所有内容包装到React.lazy组件中。 You should just fetch them normally when they're needed. 您只需要在需要时正常获取它们即可。 Maybe something like the following will work for you: 也许以下内容将为您工作:

let ClassicEditor, GFMDataProcessor;

const LazyCKEditor = React.lazy(() => {
    return Promise.all([
        import("@ckeditor/ckeditor5-build-classic"),
        import("@ckeditor/ckeditor5-react"),
        import("@ckeditor/ckeditor5-markdown-gfm/src/gfmdataprocessor")
    ]).then(([ _ClassicEditor, _CKEditor, _GFMDataProcessor ]) => {
        ClassicEditor = _ClassicEditor;
        GFMDataProcessor = _GFMDataProcessor;

        return _CKEditor;
    });
});

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

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