简体   繁体   English

如何使用 Reactjs 从 Ckeditor 添加图像

[英]How to add image from Ckeditor using Reactjs

I am working on Reactjs and using "nextjs" framework,Right now i am using Ckeditor,Editor is working except image section,So how can i insert/add image using ckeditor?我正在研究 Reactjs 并使用“nextjs”框架,现在我正在使用 Ckeditor,编辑器正在工作,除了图像部分,那么我如何使用 ckeditor 插入/添加图像? here is my current code,Following is "Ckeditor.js" file (inside component folder)这是我当前的代码,以下是“Ckeditor.js”文件(在组件文件夹内)

import React, { useEffect, useRef } from "react";export default function CKeditor({ onChange, editorLoaded, name, value }) {
    const editorRef = useRef();
    const { CKEditor, ClassicEditor } = editorRef.current || {};useEffect(() => {
    editorRef.current = {
         CKEditor: require("@ckeditor/ckeditor5-react").CKEditor, 
         ClassicEditor: require("@ckeditor/ckeditor5-build-classic")
        };
    }, []);
    return (
        <>
            {editorLoaded ? (
                <CKEditor
                    type=""
                    name={name}
                    editor={ClassicEditor}
                    data={value}
                    onChange={(event, editor) => {
                        const data = editor.getData();
                        onChange(data);
                    }}
                />
            ) : (
                <div>Editor loading</div>
            )}
        </>
    )
}

And here is my code in "Addblog.js" file这是我在“Addblog.js”文件中的代码

import { useEffect, useState } from "react";
import CKeditor from "../components/CKeditor";export default function index() {
  const [editorLoaded, setEditorLoaded] = useState(false);
  const [data, setData] = useState("");useEffect(() => {
    setEditorLoaded(true);
  }, []);
  return (
    <div>
      <CKeditor
        name="description"
        onChange={(data) => {
          setData(data);
        }}
        editorLoaded={editorLoaded}
      />       {JSON.stringify(data)}    </div>
  );
}

It might be due to the fact that you have not configured the component to allow image uploads.这可能是因为您没有配置组件以允许上传图片。 To allow image uploads in the CKEditor component, you will need to include a file upload adapter and configure the component to use it.要允许在 CKEditor 组件中上传图像,您需要包含一个文件上传适配器并配置组件以使用它。

https://ckeditor.com/docs/ckeditor5/latest/framework/guides/deep-dive/upload-adapter.html https://ckeditor.com/docs/ckeditor5/latest/framework/guides/deep-dive/upload-adapter.html

And then just import it into the component:然后将其导入到组件中:

import MyCustomUploadAdapterPlugin from './MyCustomUploadAdapterPlugin';

and configure like so:并像这样配置:

editorLoaded={editorLoaded}
config={{
  extraPlugins: [ MyCustomUploadAdapterPlugin ],
}}

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

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