React使用富文本CKEditor 5,上传图片并可设置大小

上传图片

基础使用(标题、粗体、斜体、超链接、缩进段落、有序无序、上传图片)

官网查看:https://ckeditor.com/docs/ckeditor5/latest/installation/integrations/react.html

安装依赖

shell 复制代码
npm install --save @ckeditor/ckeditor5-react @ckeditor/ckeditor5-build-classic

使用

ts 复制代码
import { CKEditor } from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';




<CKEditor
   editor={ ClassicEditor }
   data="<p>Hello from CKEditor&nbsp;5!</p>"
   onReady={ 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 } );
   } }
   onBlur={ ( event, editor ) => {
       console.log( 'Blur.', editor );
   } }
   onFocus={ ( event, editor ) => {
       console.log( 'Focus.', editor );
   } }
    extraPlugins={[(editor) => {
          	editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
    return new MyUploadAdapter( loader );
};

/>

上传图片 并可设置大小

方法:使用online builder

选中需要的插件,一直next step ,然后下载

把图片相关的插件选中,add

默认选择,next step

解压后

shell 复制代码
npm i #安装依赖

npm run build #打包

把打包后文件build/cheditor.js放进react项目的src/components/Editor/ckeditor.js

react项目中使用

ts 复制代码
import { PageContainer } from '@ant-design/pro-components';
import { Access, useAccess } from '@umijs/max';
import { Button } from 'antd';
import { CKEditor } from '@ckeditor/ckeditor5-react'
import ClassicEditor from '@/components/Editor/ckeditor';
const AccessPage: React.FC = () => {
  const access = useAccess();
  return (
    <PageContainer
      ghost
      header={{
        title: '权限示例',
      }}
    >
      <Access accessible={access.canSeeAdmin}>
        <Button>只有 Admin 可以看到这个按钮</Button>


      </Access>

      <div style={{ marginTop: 40}}>
        <CKEditor
          editor={ClassicEditor}
          config={{
            toolbar: {
              items: [
                'heading',
                '|',
                'bold',
                'italic',
                'link', //链接
                'bulletedList', //无序排序
                'numberedList', //有序排序

                'outdent',
                'indent',
                '|',
                'imageUpload',
                'blockQuote',
                'insertTable',
                'mediaEmbed',
                'undo',
                'redo'
              ]
            },
            image: { 
              toolbar: [
                'imageStyle:block',
                'imageStyle:side',
                '|',
                'toggleImageCaption',
                'imageTextAlternative',
                '|',
                'linkImage'
              ]
            }
          }}
          extraPlugins={[(editor) => {
          	editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {
    return new MyUploadAdapter( loader );
};

          }]}
        />
      </div>
    </PageContainer>
  );
};

export default AccessPage;

图片上传adapter

上传图片逻辑

MyUploadAdapter.js

cpp 复制代码
class MyUploadAdapter {
    constructor( loader ) {
        // The file loader instance to use during the upload.
        this.loader = loader;
    }

    // Starts the upload process.
    upload() {
        // Return a promise that will be resolved when the file is uploaded.
        return loader.file
            .then( file => {
				//上传图片逻辑,必须是异步的,否则在富文本框无法正常渲染图片
					return {
					
						default: ''//图片链接
					}

			} );
    }

    // Aborts the upload process.
    abort() {
    }
}
cpp 复制代码
{
    default: 'http://example.com/images/image--default-size.png'
}

图片上传upload adapter:https://ckeditor.com/docs/ckeditor5/latest/framework/deep-dive/upload-adapter.html

详细查看官网更多内容:https://ckeditor.com/ckeditor-5/

相关推荐
夏幻灵9 小时前
HTML5里最常用的十大标签
前端·html·html5
Mr Xu_9 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js
未来龙皇小蓝9 小时前
RBAC前端架构-01:项目初始化
前端·架构
程序员agions9 小时前
2026年,微前端终于“死“了
前端·状态模式
万岳科技系统开发9 小时前
食堂采购系统源码库存扣减算法与并发控制实现详解
java·前端·数据库·算法
程序员猫哥_9 小时前
HTML 生成网页工具推荐:从手写代码到 AI 自动生成网页的进化路径
前端·人工智能·html
龙飞059 小时前
Systemd -systemctl - journalctl 速查表:服务管理 + 日志排障
linux·运维·前端·chrome·systemctl·journalctl
我爱加班、、9 小时前
Websocket能携带token过去后端吗
前端·后端·websocket
AAA阿giao9 小时前
从零拆解一个 React + TypeScript 的 TodoList:模块化、数据流与工程实践
前端·react.js·ui·typescript·前端框架
杨超越luckly10 小时前
HTML应用指南:利用GET请求获取中国500强企业名单,揭秘企业增长、分化与转型的新常态
前端·数据库·html·可视化·中国500强