热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

React使用CKEditor全网最详细讲解(三)

React使用CKEditor全网最详细讲解三一、环境介绍二、依赖下载三、config-overrides.js配置四、编辑器配置五、自定义图片上传插件六、效果查看七、样式修改八、


React使用CKEditor全网最详细讲解三

  • 一、环境介绍
  • 二、依赖下载
  • 三、config-overrides.js配置
  • 四、编辑器配置
  • 五、自定义图片上传插件
  • 六、效果查看
  • 七、样式修改
  • 八、总结

上两篇文章讲解了CKEditor5的基本使用,本文主要讲解CKEditor的高级使用,包括自定义编辑栏插件、自定义上传图片、插件的详细配置。


一、环境介绍

博主用到的React框架是react-app-rewired+customize-cra配置,这一点和webpack有所不同,webpack的配置方式可以参照CKEditor5的官网配置,官网给出的配置是根据webpack来配置的,链接为:https://ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/frameworks/react.html,本文就react-app-rewired+customize-cra进行讲解


二、依赖下载

这里主要讲解CKEditor5插件依赖下载

yarn add @ckeditor/ckeditor5-adapter-ckfinder@ckeditor/ckeditor5-autoformat@ckeditor/ckeditor5-basic-styles@ckeditor/ckeditor5-block-quote@ckeditor/ckeditor5-build-classic@ckeditor/ckeditor5-ckfinder@ckeditor/ckeditor5-code-block@ckeditor/ckeditor5-dev-webpack-plugin@ckeditor/ckeditor5-easy-image@ckeditor/ckeditor5-editor-classic@ckeditor/ckeditor5-essentials@ckeditor/ckeditor5-heading@ckeditor/ckeditor5-highlight@ckeditor/ckeditor5-indent@ckeditor/ckeditor5-link@ckeditor/ckeditor5-list@ckeditor/ckeditor5-media-embed@ckeditor/ckeditor5-paragraph@ckeditor/ckeditor5-paste-from-office@ckeditor/ckeditor5-react@ckeditor/ckeditor5-table@ckeditor/ckeditor5-theme-lark

三、config-overrides.js配置

const path = require('path')
const { styles } = require('@ckeditor/ckeditor5-dev-utils');
const CKEditorWebpackPlugin = require('@ckeditor/ckeditor5-dev-webpack-plugin');const {override,fixBabelImports,addLessLoader,addWebpackAlias,addWebpackModuleRule,addWebpackPlugin
} = require('customize-cra')module.exports = override(fixBabelImports('import', {libraryName: "antd", libraryDirectory: "lib",style: "css"}),addLessLoader({JavascriptEnabled: true,modifyVars: {"font-size-base": "12px","text-color": 'rgba(0, 0, 0, .85)',"btn-height-base": '24px',"input-height-base": '24px',"form-item-margin-bottom": '8px'}}),addWebpackAlias({'@': path.resolve(__dirname, 'src'),'components': path.resolve(__dirname, 'src/components'),'utils': path.resolve(__dirname, 'src/utils'),'views': path.resolve(__dirname, 'src/views'),'img': path.resolve(__dirname, 'src/img')}),addWebpackModuleRule({ test: /ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/, use: ['raw-loader'] }),addWebpackModuleRule({test: /ckeditor5-[^/\\]+[/\\]theme[/\\].+\.css$/,use: [{ loader: 'style-loader' },//{ loader: "css-loader" },{loader: 'postcss-loader',options: styles.getPostCssConfig({themeImporter: {themePath: require.resolve('@ckeditor/ckeditor5-theme-lark')},minify: true})}]}),addWebpackModuleRule({test: /\.html$/, use: ['html-loader']}),addWebpackPlugin(new CKEditorWebpackPlugin({//See https://ckeditor.com/docs/ckeditor5/latest/features/ui-language.htmllanguage: 'zh-cn'})))

四、编辑器配置

这里不能直接使用@ckeditor/ckeditor5-build-classic这个包了,因为这个包已经包含了很多插件,我们需要加入的代码编辑器插件与其冲突,所以只能用@ckeditor/ckeditor5-editor-classic/src/classiceditor,然后把插件一个个加载

import React from 'react'
import CKEditor from '@ckeditor/ckeditor5-react';
import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'
import Essentials from '@ckeditor/ckeditor5-essentials/src/essentials';
import UploadAdapter from '@ckeditor/ckeditor5-adapter-ckfinder/src/uploadadapter';
import Autoformat from '@ckeditor/ckeditor5-autoformat/src/autoformat';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
import ImageStyle from '@ckeditor/ckeditor5-image/src/imagestyle';
import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
import ImageUpload from '@ckeditor/ckeditor5-image/src/imageupload';
import Indent from '@ckeditor/ckeditor5-indent/src/indent';
import Link from '@ckeditor/ckeditor5-link/src/link';
import List from '@ckeditor/ckeditor5-list/src/list';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import PasteFromOffice from '@ckeditor/ckeditor5-paste-from-office/src/pastefromoffice';
import Table from '@ckeditor/ckeditor5-table/src/table';
import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar';
import CodeBlock from '@ckeditor/ckeditor5-code-block/src/codeblock'
import Highlight from '@ckeditor/ckeditor5-highlight/src/highlight';
import ImageResize from '@ckeditor/ckeditor5-image/src/imageresize'import MyCustomUploadAdapterPlugin from './MyUploadAdapter'//图片上传插件,下一个章节会讲解export default class BCKEditor extends React.Component {constructor(props) {super(props)this.state = {}}render() {return (

toolbar: ['imageTextAlternative', '|', 'imageStyle:alignLeft', 'imageStyle:full', 'imageStyle:alignRight','imageStyle:side'],styles: ['full','side','alignLeft','alignRight']}}}onInit={editor => {}}onChange={(event, editor) => {const data = editor.getData();if (this.props.onChange) {this.props.onChange(data)}}}onBlur={(event, editor) => {}}onFocus={(event, editor) => {}}/>
)}
}

自定义图片上传插件


五、自定义图片上传插件

这里用到的文件名为MyUploadAdapter.js,导出的方法为:MyCustomUploadAdapterPlugin,在上一个步骤的,extraPlugins已经配置

import { uploadFile } from '@/api'
import { message } from 'antd'export default function MyCustomUploadAdapterPlugin( editor ) {editor.plugins.get( 'FileRepository' ).createUploadAdapter = ( loader ) => {return new MyUploadAdapter( loader );};
}class MyUploadAdapter {constructor(loader) {this.loader = loader;}upload() {return this.loader.file.then(file => new Promise(async (resolve, reject) => {var data = new FormData();data.append("files", file);let res = await uploadFile(data); //axios上传图片的Actionif (res) {let { errno } = res;if (errno === 0) {resolve({ default: res.data[0] });
//res.data[0]为图片的URL,格式为http://127.0.0.1/image/1.jpg} else { reject(message) }} else {message.error('图片上传失败')}}));}abort() {console.log('暂停上传')}}

axios中uploadFile 方法

export function uploadFile(params) {return request.post('/file/upload', params, fileHeaders)
}

到这里了还需要添加SpringBoot后台上传依赖,具体Action代码为

&#64;PostMapping(value &#61; "/file/upload")public Map<String, Object> fileUpload(&#64;RequestParam(value &#61; "files") MultipartFile file, Model model,
HttpServletRequest request) {Map<String, Object> map &#61; new HashMap<String, Object>();List<String> list &#61; new ArrayList<String>();if (file.isEmpty()) {System.out.println("文件为空");}String fileName &#61; file.getOriginalFilename(); // 文件名String suffixName &#61; fileName.substring(fileName.lastIndexOf(".")); // 后缀名fileName &#61; UUID.randomUUID() &#43; suffixName; // 新文件名File dest &#61; new File(ApplicationAutoConfiguration.filePath &#43; fileName);if (!dest.getParentFile().exists()) {dest.getParentFile().mkdirs();}try {file.transferTo(dest);} catch (IOException e) {e.printStackTrace();}String prefix &#61; Utils.getUrlPrefix(domainName, port);String filename &#61; prefix &#43; ApplicationAutoConfiguration.filePrefix &#43; "/" &#43; fileName;//filename为文件的全路径&#xff0c;格式为http://127.0.0.1/images/1.jpglist.add(filename);map.put("errno", 0);map.put("data", list);return map;}

六、效果查看

最终效果图


七、样式修改

CKEditor5的编辑器高度是自动的&#xff0c;需要自己设置指定的高度&#xff0c;找到CKEditor5的css样式来修改&#xff0c;如下

.myckeditor .ck-editor__main{height: 500px;
}.myckeditor .ck-content{height: 500px;
}

八、总结

CKEditor5是通过插件来进行菜单栏的配置和扩展&#xff0c;不能沿用原来的&#64;ckeditor/ckeditor5-build-classic&#xff0c;需要使用新的编辑器&#64;ckeditor/ckeditor5-editor-classic/src/classiceditor&#xff0c;并且把菜单的依赖插件一个个加载并且导入&#xff0c;比较重要的是webpack的配置以及自定义上传文件。今天的分析就到这里&#xff0c;遇到什么问题解决不了可以给我留言。


推荐阅读
  • RN即ReactNative基于React框架针对移动端的跨平台框架,在学习RN前建议最好熟悉下html,css,js,当然如果比较急,那就直接上手吧,毕竟用学习前面基础的时间,R ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 开发笔记:spring boot项目打成war包部署到服务器的步骤与注意事项
    本文介绍了将spring boot项目打成war包并部署到服务器的步骤与注意事项。通过本文的学习,读者可以了解到如何将spring boot项目打包成war包,并成功地部署到服务器上。 ... [详细]
  • Unity3D引擎的体系结构和功能详解
    本文详细介绍了Unity3D引擎的体系结构和功能。Unity3D是一个屡获殊荣的工具,用于创建交互式3D应用程序。它由游戏引擎和编辑器组成,支持C#、Boo和JavaScript脚本编程。该引擎涵盖了声音、图形、物理和网络功能等主题。Unity编辑器具有多语言脚本编辑器和预制装配系统等特点。本文还介绍了Unity的许可证情况。Unity基本功能有限的免费,适用于PC、MAC和Web开发。其他平台或完整的功能集需要购买许可证。 ... [详细]
  • java ssm框架_Java SSM框架的简单搭建
    1.添加依赖包,可以通过properties统一框架版本UTF-81.71.75.0.8.RELEASEjunitjunit4.11testjavax.servlet ... [详细]
  • Nginx使用AWStats日志分析的步骤及注意事项
    本文介绍了在Centos7操作系统上使用Nginx和AWStats进行日志分析的步骤和注意事项。通过AWStats可以统计网站的访问量、IP地址、操作系统、浏览器等信息,并提供精确到每月、每日、每小时的数据。在部署AWStats之前需要确认服务器上已经安装了Perl环境,并进行DNS解析。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • Webmin远程命令执行漏洞复现及防护方法
    本文介绍了Webmin远程命令执行漏洞CVE-2019-15107的漏洞详情和复现方法,同时提供了防护方法。漏洞存在于Webmin的找回密码页面中,攻击者无需权限即可注入命令并执行任意系统命令。文章还提供了相关参考链接和搭建靶场的步骤。此外,还指出了参考链接中的数据包不准确的问题,并解释了漏洞触发的条件。最后,给出了防护方法以避免受到该漏洞的攻击。 ... [详细]
  • 近来有一个需求,是需要在androidjava基础库中插入一些log信息,完成这个工作需要的前置条件有编译好的android源码具体android源码如何编译,这 ... [详细]
  • Forexperiencedcryptoinvestors,thereareseveralsectorsthatseemedpromisingbutdidn’tlive ... [详细]
author-avatar
Mr_XieZhiQ
无表面兄弟,不编程!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有