简体   繁体   English

CKEditor 5 React自定义图像上传

[英]CKEditor 5 React custom image upload

I have spent all day trying to get CKEditor with React to work. 我整天都在努力使CKEditor和React一起工作。 Everything seems to be okay except the images. 除了图像,其他一切似乎都还不错。 I 一世

I have a way to upload the images to my server already (azure). 我已经有一种方法可以将图像上传到我的服务器了(天蓝色)。 ALL I NEED is to know how to connect it to the CKEditor with React! 我只需要知道如何使用React将其连接到CKEditor! I keep getting the error "Upload adapter is not defined." 我不断收到错误消息“未定义上传适配器”。

 <CKEditor editor={ ClassicEditor } data={this.state.body ? this.state.body : "<p>Body text...</p>"} onInit={ editor => { // You can store the "editor" and use when it is needed. console.log( 'Editor is ready to use!', editor ); } } onChange={ ( event, editor ) => { const data = editor.getData(); console.log( { event, editor, data,}, "DATA" ); } } // config={upload=this.uploadImage()} /> 

I'm guessing it has something to do with the config file? 我猜它与配置文件有关吗? I already have the function that uploads the file and returns the URL, I just don't know where to add that into the CKEditor in React. 我已经有上传文件并返回URL的功能,我只是不知道在React中将其添加到CKEditor中的位置。

That error means there is no upload adapter connected. 该错误意味着没有连接上载适配器。

First, you'll need to implement your own Custom Upload Adapter which handles uploading the images to the server. 首先,您需要实现自己的“ 自定义上传适配器” ,用于处理将图像上传到服务器。 There's a sample in this Stack Overflow response 此堆栈溢出响应中有一个示例

And then, you link the editor to your upload adapter in your onInit method. 然后,您可以使用onInit方法将编辑器链接到上传适配器。 Like so: 像这样:

<CKEditor
    editor={ClassicEditor}
    data={this.state.body ? this.state.body : "<p>Body text…</p>"}
    onInit={editor => {
       // Connect the upload adapter using code below 
       editor.plugins.get("FileRepository").createUploadAdapter = function(loader) {
          return new UploadAdapter(loader);
       };
       console.log("Editor is ready to use!", editor);
    }}
    onChange={(event, editor) => {
        const data = editor.getData();
        console.log({ event, editor, data }, "DATA");
    }}
/>

UploaderAdapter in the code is the name of your UploadAdapter class implementation. 代码中的UploaderAdapter是您的UploadAdapter类实现的名称。

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

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