按照一般官网的集成方案安装CKEditor5,需要vue的版本在3.x及以上才能使用,但是,由于博主目前使用的是vue2.6.10,所以这里介绍一种源码集成方案。注意,使用源码集成手脚架(vue/cli)必须要3.x及以上版本。
一、安装ckeditor基础框架、一些基础功能和主题:
npm install --save @ckeditor/ckeditor5-vue2 @ckeditor/ckeditor5-dev-webpack-plugin @ckeditor/ckeditor5-dev-utils postcss-loader@3 [email protected]
npm install --save @ckeditor/ckeditor5-editor-classic @ckeditor/ckeditor5-essentials @ckeditor/ckeditor5-basic-styles @ckeditor/ckeditor5-link @ckeditor/ckeditor5-paragraph @ckeditor/ckeditor5-theme-lark @ckeditor/ckeditor5-heading
const path = require('path');
const CKEditorWebpackPlugin = require('@ckeditor/ckeditor5-dev-webpack-plugin');
const {styles} = require('@ckeditor/ckeditor5-dev-utils');
module.exports = {
configureWebpack: {
plugins: [
new CKEditorWebpackPlugin({
language: 'en',
translationsOutputFile: /app/
})
]
},
transpileDependencies: [
/ckeditor5-[^/\\]+[/\\]src[/\\].+\.js$/,
],
chainWebpack: config => {
const svgRule = config.module.rule('svg');
svgRule.exclude.add(path.join(__dirname, 'node_modules', '@ckeditor'));
config.module
.rule('cke-svg')
.test(/ckeditor5-[^/\\]+[/\\]theme[/\\]icons[/\\][^/\\]+\.svg$/)
.use('raw-loader')
.loader('raw-loader');
config.module
.rule('cke-css')
.test(/ckeditor5-[^/\\]+[/\\].+\.css$/)
.use('postcss-loader')
.loader('postcss-loader')
.tap(() => {
return styles.getPostCssConfig({
themeImporter: {
themePath: require.resolve('@ckeditor/ckeditor5-theme-lark'),
},
minify: true
});
});
},
};
三、在需要使用ckeditor5的vue文件中进行引入使用:
四、自定义拓展功能:
功能列表有许多CKEditor5提供的功能,点击名字就能查到详细的功能和对应的demo,以Font功能为例子(classic中没有的功能):
①首先,找到font功能,点击进入
②进入后往下滑可以看到demo,点击进入
③进入后即可看到如何使用:
首先,安装:
npm install --save @ckeditor/ckeditor5-font
接着,在vue文件中引入:
import FontColor from '@ckeditor/ckeditor5-font/src/fontcolor'
import FontBackgroundColor from '@ckeditor/ckeditor5-font/src/fontbackgroundcolor'
import FontFamily from '@ckeditor/ckeditor5-font/src/fontfamily'
import FontSize from '@ckeditor/ckeditor5-font/src/fontsize'
并在plugins,toolbar中配置上,有一些可以自定义的配置:
plugins: [ FontColor, FontBackgroundColor, FontFamily, FontSize, .. ],
toolbar: [ 'fontFamily', 'fontSize', 'fontColor', 'fontBackgroundColor', ... ]
效果图:
五、实现大部分功能的自定义ckeditor5完整示例:
ckeditor5虽然可以自定义实现非常多的功能,但是在实现一些功能时,有未能实现预期效果的情况,比如:
①把ckeditor放在某个组件中时,工具栏宽度不够放下所有的工具栏项目,默认的它会显示“Show more items” (⋮) 的按钮,此时,将shouldNotGroupWhenFull设置为true,工具栏将停止对项目进行分组,并让它们在没有足够空间在单行中显示时换行到下一行。默认情况下为false,此设置启用项目分组。
②通过链接的方式添加图片可以实现,但上传本地图片,需要使用Base64 图片上传适配器。(网上很多文章或者官网使用ckfinder、ckeditor5-easy-image、ckeditor5-image,本人尝试过后都未能实现预期效果)
③表格或图片样式被过滤,即:在ckeditor中画的表格,提取出来在另一个地方显示,却没有表格样式(表格线、颜色等),只有按照表格顺序显示的一堆内容。解决这个问题需要在显示的地方添加编辑器的style样式:(其中,addTemplateContentShow表示用来显示ckeditor中编辑的内容的容器,而addTemplateContent表示ckeditor中编辑的内容)
this.addTemplateContentShow = `
` + this.addTemplateContent + `
`
基于以上,博主实现了绝大部分功能的自定义ckeditor示例如下:
效果图:
import CKEditor from '@ckeditor/ckeditor5-vue2'
import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor'
import EssentialsPlugin from '@ckeditor/ckeditor5-essentials/src/essentials'
import BoldPlugin from '@ckeditor/ckeditor5-basic-styles/src/bold'
import ItalicPlugin from '@ckeditor/ckeditor5-basic-styles/src/italic'
import Underline from '@ckeditor/ckeditor5-basic-styles/src/underline'
import Strikethrough from '@ckeditor/ckeditor5-basic-styles/src/strikethrough'
import Code from '@ckeditor/ckeditor5-basic-styles/src/code'
import Link from '@ckeditor/ckeditor5-link/src/link'
import ParagraphPlugin from '@ckeditor/ckeditor5-paragraph/src/paragraph'
import Heading from '@ckeditor/ckeditor5-heading/src/heading'
import FontColor from '@ckeditor/ckeditor5-font/src/fontcolor'
import FontBackgroundColor from '@ckeditor/ckeditor5-font/src/fontbackgroundcolor'
import FontFamily from '@ckeditor/ckeditor5-font/src/fontfamily'
import FontSize from '@ckeditor/ckeditor5-font/src/fontsize'
import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment'
import Table from '@ckeditor/ckeditor5-table/src/table'
import TableToolbar from '@ckeditor/ckeditor5-table/src/tabletoolbar'
import TableCaption from '@ckeditor/ckeditor5-table/src/tablecaption'
import TableProperties from '@ckeditor/ckeditor5-table/src/tableproperties'
import TableCellProperties from '@ckeditor/ckeditor5-table/src/tablecellproperties'
import ListStyle from '@ckeditor/ckeditor5-list/src/liststyle'
import HorizontalLine from '@ckeditor/ckeditor5-horizontal-line/src/horizontalline'
import BlockQuote from '@ckeditor/ckeditor5-block-quote/src/blockquote'
import Indent from '@ckeditor/ckeditor5-indent/src/indent'
import IndentBlock from '@ckeditor/ckeditor5-indent/src/indentblock'
import EasyImage from '@ckeditor/ckeditor5-easy-image/src/easyimage'
import Image from '@ckeditor/ckeditor5-image/src/image'
import ImageResize from '@ckeditor/ckeditor5-image/src/imageresize'
import CloudServicesPlugin from '@ckeditor/ckeditor5-cloud-services/src/cloudservices'
import ImageInsert from '@ckeditor/ckeditor5-image/src/imageinsert'
import SpecialCharacters from '@ckeditor/ckeditor5-special-characters/src/specialcharacters'
import SpecialCharactersEssentials from '@ckeditor/ckeditor5-special-characters/src/specialcharactersessentials'
import Base64UploadAdapter from '@ckeditor/ckeditor5-upload/src/adapters/base64uploadadapter'
components: {
ckeditor: CKEditor.component
}
data () {
return {
editor: ClassicEditor,
editorConfig: {
placeholder: 'Type something',
plugins: [
EssentialsPlugin,
BoldPlugin,
ItalicPlugin,
Underline,
Strikethrough,
Code,
Link,
ParagraphPlugin,
Heading,
FontColor,
FontBackgroundColor,
FontFamily,
FontSize,
Alignment,
Table,
TableToolbar,
TableCaption,
TableProperties,
TableCellProperties,
ListStyle,
HorizontalLine,
BlockQuote,
EasyImage,
Image,
ImageResize,
CloudServicesPlugin,
ImageInsert,
Indent,
IndentBlock,
SpecialCharacters,
SpecialCharactersEssentials,
Base64UploadAdapter
],
toolbar: {
shouldNotGroupWhenFull: true,
items: [
'heading',
'|',
'bold',
'italic',
'underline',
'strikethrough',
'code',
'link',
'|',
'numberedList',
'bulletedList',
'|',
'fontFamily',
'fontSize',
'fontColor',
'fontBackgroundColor',
'|',
'outdent',
'indent',
'|',
'blockQuote',
'alignment',
'insertImage',
'insertTable',
'horizontalLine',
'specialCharacters',
'|',
'undo',
'redo'
]
},
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
{ model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
{ model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
{ model: 'heading3', view: 'h3', title: 'Heading 3', class: 'ck-heading_heading3' }
]
},
fontFamily: {
options: [
'default',
'Ubuntu, Arial, sans-serif',
'Ubuntu Mono, Courier New, Courier, monospace'
]
}[添加链接描述](https://ckeditor.com/docs/ckeditor5/latest/features/toolbar/toolbar.html),
fontSize: {
options: [
10,
12,
'default',
16,
18,
20
],
supportAllValues: true
},
alignment: {
options: ['left', 'center', 'right']
},
table: {
contentToolbar: ['tableColumn', 'tableRow', 'mergeTableCells', 'toggleTableCaption', 'tableProperties', 'tableCellProperties']
}
},
addTemplateContent: ''
}
}
有更多的疑问,可以查询:CKEditor5文档