给ckeditor编辑器添加自定义按钮

本文介绍如何在CKEditor中添加自定义按钮,并提供详细的步骤和代码示例,包括配置插件、定义按钮行为及在编辑器中显示。
1、首先下载并安装ckeditor。

2、自定义工具栏按钮:

我们可以自定义ckeditor工具栏要显示的按钮,工具栏按钮定义可以参考这里。

现在我们需要向工具栏添加一个自定义功能的按钮。ckeditor工具栏中的每个按钮都是作为插件定义在ckeditor\plugins\ 目录中。我们在ckeditor\plugins\中创建一个新文件夹linkbutton。在linkbutton文件夹内,我们创建一个plugin.js文件,它的代码如下:

(function(){
//Section 1 : 按下自定义按钮时执行的代码
var a= {
exec:function(editor){
alert("这是自定义按钮");
}
},
//Section 2 : 创建自定义按钮、绑定方法
b='linkbutton';
CKEDITOR.plugins.add(b,{
init:function(editor){
editor.addCommand(b,a);
editor.ui.addButton('linkbutton',{
label:'Link Button',
icon: this.path + 'logo_ckeditor.png',
command:b
});
}
});
})();
在上面的代码中我们指定自定义按钮的图标logo_ckeditor.png,logo_ckeditor.png放在linkbutton文件夹中。

接下来我们需要注册linkbutton插件。http://ajithmanmadhan.wordpress.com/2009/12/16/customizing-ckeditor-and-adding-a-new-toolbar-button/原文中的给出方法是在ckeditor.js中注册,这会使以后升级新版本遇到困难。提倡使用下面的方法在config.js中注册自定义插件:

CKEDITOR.editorConfig = function( config )
{
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.skin = 'office2003';
config.extraPlugins="linkbutton"
};
最后,在ckeditor中显示自定义按钮linkbutton,代码如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>自定义按钮</title>
<script type="text/javascript" src="ckeditor.js"></script>
<style type="text/css">
body{font-size:12px;line-height:1.8;font-family:'微软雅黑';}
</style>
</head>

<body>
<textarea id="ckeditor"></textarea>
<script type="text/javascript">
CKEDITOR.replace( 'ckeditor',{
toolbar :
[
[ 'Bold', 'Italic', '-', 'NumberedList', 'BulletedList', '-', 'Link', 'Unlink','-','linkbutton' ]
],
extraPlugins:"linkbutton" //注册linkbutton,也可在config.js中注册
});
</script>
</body>
</html>


注意:自定义插件的名称必须在任何地方都要保持一致。

来源:(http://blog.sina.com.cn/s/blog_5e6e077e0100hs9q.html) - ckeditor添加自定义按钮_myspace616_新浪博客
在 Vue3 中使用 CKEditor5 并自定义添加按钮,需要结合 CKEditor5 的插件机制以及 Vue3 的组件化特性来实现。以下是具体的方法和步骤: ### 安装 CKEditor5 首先确保安装了 CKEditor5 的构建版本,推荐使用 `@ckeditor/ckeditor5-vue` 包来集成到 Vue 项目中。 ```bash npm install --save @ckeditor/ckeditor5-build-classic npm install --save @ckeditor/ckeditor5-vue ``` ### 引入 CKEditor5 到 Vue3 组件中 在 Vue3 项目的组件中引入 CKEditor5,并注册为组件。 ```ts import { defineComponent } from 'vue' import ClassicEditor from '@ckeditor/ckeditor5-build-classic' import CKEditor from '@ckeditor/ckeditor5-vue' const App = defineComponent({ components: { ckeditor: CKEditor.component }, setup() { const editorData = '<p>初始内容</p>' return { editorData, editor: ClassicEditor } } }) ``` ### 创建自定义插件并添加按钮 CKEditor5 使用模块化插件系统,可以通过创建一个插件类来扩展编辑器功能,包括添加自定义按钮。 ```ts import Plugin from '@ckeditor/ckeditor5-core/src/plugin' import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview' export default class CustomButtonPlugin extends Plugin { public static get pluginName(): string { return 'CustomButtonPlugin' } public init(): void { const editor = this.editor const t = editor.t // 添加按钮 editor.ui.componentFactory.add('customButton', locale => { const buttonView = new ButtonView(locale) buttonView.set({ label: t('自定义按钮'), tooltip: true, icon: '<svg><path fill="currentColor" d="M10 20h4V4h-4v16z"/></svg>' // 可替换为图标路径 }) buttonView.on('execute', () => { editor.model.change(writer => { const insertPosition = editor.model.document.selection.getFirstPosition() writer.insertText('这是插入的文本', insertPosition) }) }) return buttonView }) } } ``` ### 在编辑器配置中启用插件并显示按钮 在初始化编辑器时加载自定义插件,并将按钮添加到工具栏。 ```ts import CustomButtonPlugin from './plugins/custom-button-plugin' ClassicEditor .create(document.querySelector('#editor'), { plugins: [CustomButtonPlugin, /* 其他插件 */], toolbar: { items: ['customButton', /* 其他按钮 */] } }) .catch(error => { console.error(error) }) ``` ### Vue3 组件模板中使用 CKEditor 在 Vue3 模板中使用 `<ckeditor>` 标签,并绑定数据。 ```html <template> <div id="app"> <ckeditor :editor="editor" v-model="editorData" tag-name="textarea"></ckeditor> </div> </template> ``` 通过以上步骤,可以在 Vue3 项目中成功集成 CKEditor5,并且自定义添加按钮[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值