CKEditor4-Vue 使用小结
安装 及 引用
官网参考
npm install ckeditor4-vue
import Vue from 'vue';
import CKEditor from 'ckeditor4-vue';
Vue.use( CKEditor );
<template>
<div id="app">
<ckeditor v-model="editorData" :config="editorConfig"></ckeditor>
</div>
</template>
<script>
export default {
name: 'app',
data() {
return {
editorData: '<p>Content of the editor.</p>',
editorConfig: {
}
};
}
}
</script>
配置
官网参考
editorConfig: {
width: '50%',
height: 300,
editorplaceholder: 'hello',
resize_enabled: false,
toolbar: [
{
name: 'document',
items: ['Source', 'Print']
},
{
name: 'clipboard',
items: ['Undo', 'Redo']
},
{
name: 'styles',
items: ['Format', 'Font', 'FontSize']
},
{
name: 'colors',
items: ['TextColor', 'BGColor']
},
{
name: 'align',
items: ['JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock']
},
{
name: 'basicstyles',
items: ['Bold', 'Italic']
},
{
name: 'links',
items: ['Link', 'Unlink']
},
{
name: 'paragraph',
items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote']
},
{
name: 'insert',
items: ['Image', 'Table']
},
{
name: 'tools',
items: ['Maximize']
},
{
name: 'editing',
items: ['Scayt']
}
],
extraPlugins: 'print,format,font,colorbutton,justify,uploadimage,wordcount,notification',
format_tags: 'p;h1;h2;h3;h4;h5;h6;pre;address;div',
}
添加插件
注入插件时机
<ckeditor
ref="refCKEditor"
v-model="editorData"
:config="editorConfig"
@namespaceloaded="onNamespaceLoaded" />
onNamespaceLoaded(CKEDITOR) {
CKEDITOR.plugins.addExternal(
'timestamp',
'https://ckeditor.com/docs/ckeditor4/4.16.0/examples/assets/plugins/timestamp/',
'plugin.js'
);
}
添加 wordcount 插件遇到的问题
- 将插件放到 src 下某一目录 比如 /src/assets/ckeditor/wordcount
使用 CKEDITOR.plugins.addExternal 注入 如下
但是这种注入是无效的,有报错,使用相对路径也不行
路径如果使用cdn ,例如 'ckeditor.com/docs/ckedit…' 是可行的
CKEDITOR.plugins.addExternal(
'wordcount',
'/src/assets/ckeditor/wordcount/',
'plugin.js'
);
- 将插件放到 public 路径下 /public/ckeditor/plugins/wordcount/plugin.js 可行
引用时使用 /ckeditor/plugins/wordcount/plugin.js
CKEDITOR.plugins.addExternal('wordcount', '/ckeditor/plugins/wordcount/', 'plugin.js');
- 插件不放 public 方法
下载的插件放到 @/components/ckeditorPlugins/wordcount/plugin.js
需修改部分源码 参考
import wordcount from '@/components/ckeditorPlugins/wordcount/plugin.js';
onNamespaceLoaded(CKEDITOR) {
wordcount.init();
}
参考