从ckeditor5开始,默认的编译版本,只提供了固定的功能,若是须要使用更多的特性,好比文字颜色,对齐,高亮等,则须要经过源代码编译的方式添加进去,如下说明下如何从源代码方式编译自定义的ckeditor5。html
官网教程 中,提供了从github上拷贝源代码到本地开发的方式,可是因为国情,有几率下载失败,这时候能够借助gitee的 从其余仓库导入
功能,步骤以下webpack
从其余仓库导入
,仓库地址为 https://github.com/ckeditor/ckeditor5-build-classic.git
# https://gitee.com/somebugs/ckeditor5-build-classic 是我本身的gitee仓库地址 git clone -b stable https://gitee.com/somebugs/ckeditor5-build-classic # 安装依赖 npm i
下载下来的源代码,已经配置好了默认的功能,只须要对其进行增减便可。git
以添加字体
, 对齐
, 高亮
三个功能为例, 须要先安装对应的插件,github
npm i @ckeditor/ckeditor5-font @ckeditor/ckeditor5-alignment @ckeditor/ckeditor5-highlight -D
须要编辑 src/ckeditor.js
web
// src/ckeditor.js // Font 包含了字体,文字颜色,文字背景色三个功能 // Highlight 包含了字体高亮和背景高亮 import Font from '@ckeditor/ckeditor5-font/src/font'; import Alignment from '@ckeditor/ckeditor5-alignment/src/alignment'; import Highlight from '@ckeditor/ckeditor5-highlight/src/highlight'; ClassicEditor.builtinPlugins = [ ...// 原有的插件 Font, Alignment, Highlight, ]; ClassicEditor.defaultConfig = { toolbar: { items: [ ...// 原有配置 'alignment', '|', 'fontSize', 'fontFamily', 'fontColor', 'fontBackgroundColor', 'highlight', '|', ...// 原有配置 ] } }
而后执行如下命令,便可在build
目录下生成新的ckeditor.js文件shell
npm run build
注意:自定义方式构建出的ckeditor不支持官方的语言包。npm
模板默认内置的语言是英语,若是须要修改默认的语言为中文,则须要同时修改
webpack.config.js
和 /src/ckeditor.js
文件中的 language
配置:编辑器
language: 'zh-cn'
在打包的同时,会在 build
文件夹下面生成 translations
文件夹,里面自动翻译了很是多的语言(默认语言除外),在使用中若是须要使用其余语言,直接引入对应的语言文件便可。ide
默认会生成umd方式 的js文件,能够直接使用,也能够经过ES6模块方式导入使用。直接使用的变量名依然是 window.ClassicEditor
字体
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>CKEditor 5 – classic editor build – development sample</title> <style> body { max-width: 800px; margin: 20px auto; } </style> </head> <body> <h1>CKEditor 5 – classic editor build – development sample</h1> <div id="editor"> <h2>Sample</h2> <p>This is an instance of the <a href="https://ckeditor.com/docs/ckeditor5/latest/builds/guides/overview.html#classic-editor">classic editor build</a>.</p> <figure class="image"> <img src="../tests/manual/sample.jpg" alt="Autumn fields" /> </figure> <p>You can use this sample to validate whether your <a href="https://ckeditor.com/docs/ckeditor5/latest/builds/guides/development/custom-builds.html">custom build</a> works fine.</p> </div> <script src="../build/translations/en.js"></script> <script src="../build/translations/zh.js"></script> <script src="../build/ckeditor.js"></script> <script> ClassicEditor.create( document.querySelector( '#editor' ), { language: 'en' } ) .then( editor => { window.editor = editor; } ) .catch( err => { console.error( err.stack ); } ); </script> </body> </html>