Tinymce group plugin
本文介绍一个tinymce插件,用来组合显示下拉的按钮。基于4.x,不兼容3.x。
以前
配置toolbar功能按钮
需要
toolbar1: "code undo redo fullscreen"
plugins: "code, fullscreen"
页面就能显示

配置 toolbar下拉的功能按钮集合,需要新写一个插件
插件核心源码:
editor.addButton('undoRedo', {
type: 'menubutton',
icon: 'mceIcon mce-i-undo',
menu: [
{
text: ed.getLang('Undo'),
icon: 'mceIcon mce-i-bold',
onclick: function() {
ed.execCommand('Undo');
}
},
{
text: ed.getLang('Redo'),
icon: 'mceIcon mce-i-redo',
onclick: function() {
ed.execCommand('Redo');
}
}
]
});
就能看到

现在
现在我们想要实现这类下拉功能菜单:

当然作为备选方案,可以使用使用上面提到的add menuButton的方式。不过这样的缺点是,我们如果需要5个下拉功能菜单,就不得不写五个插件,而都是重复的工作。
当一件事开始重复的时候,就一定有方法可以让其变的简单。
我们的解决办法是,通过一个插件,来配置多个下拉功能菜单。
talk is chip, show you the code:
使用配置:
toolbar1: 'undo redo | group group group group group | fullscreen',
group_set: [{
icon: 'alignleft',
buttons: 'alignleft,aligncenter,alignright',
title: 'Align center'
}, {
icon: 'bullist',
buttons: 'bullist,numlist',
title: 'advanced.bullist_desc'
}, {
icon: 'indent',
buttons: 'indent,outdent',
title: 'advanced.indent_desc'
}, {
icon: 'subscript',
buttons: 'superscript,subscript',
title: 'advanced.sup_desc'
},{
icon: 'image',
buttons: 'alitophotobank,image, aliphotobank',
title: 'advanced.sup_desc'
}];
效果:

plugin group实现
修改 toolbar 配置
toolbar1配置了5个group占位符,但是addButton的第一个参数name一定要和toolbar上的占位配置一致,所以第一步是修改 settings.toolbar上的group占位符。
// 修改 setting中toolbar上的group,group为group1,group2
modifySettingToolbar: function(ed) {
var index = 1;
var settings = ed.settings;
if(!settings.toolbar1 && !settings.toolbar2 && !settings.toolbar3) return false; var toolbar1 = settings.toolbar1.split(' '),
toolbar2 = settings.toolbar2.split(' '),
toolbar3 = settings.toolbar3.split(' '); [toolbar1, toolbar2, toolbar3].forEach(function(item) {
for (var i = 0; i < item.length; i++) {
if(item[i] == 'group') {
item[i] = 'group' + index;
index ++;
}
};
});
this.toolbarNum = index;
settings.toolbar1 = toolbar1.join(' ');
settings.toolbar2 = toolbar2.join(' ');
settings.toolbar3 = toolbar3.join(' ');
},
插件核心代码
//编辑器初始化后将初始化一个插件实例
init: function(ed, url) {
//在这个实例中我们保存一些编辑器的公用信息
this.ed = ed;
//保留配置信息
this.settings = ed.settings;
// 注意 4.x 没有3.x的 ed.onInit 方法
ed.on('init', function() {
this.createControl(ed);
}.bind(this));
},
// 创建 group button
createControl: function(ed) {
var _set = this.settings; this.modifySettingToolbar(ed);
for (var i = 1; i < this.toolbarNum; i++) { var _item = _set.group_set[i-1]; //获取多组信息
if(!_item) return false;
var _buttons = _item ? _item.buttons.split(',') : [],
subItem = []; for (var j = 0, l = _buttons.length; j < l; j++) { btn = _buttons[j] && _buttons[j].trim(); subItem.push({
//配置标题信息则需要考虑到语言和主题
text: ed.getLang(BUTTONS_MAP[btn] && BUTTONS_MAP[btn][0]),
//图标类自己创建的话则需要注意格式
icon: 'mceIcon mce-i-' + btn,
//执行的命令 闭包传入当前btn
onclick: (function(btn) {
var cmd = BUTTONS_MAP[btn] && BUTTONS_MAP[btn][1];
return function(e) {
ed.execCommand(cmd);
}
})(btn)
});
}
ed.addButton('group' + i, {
type: 'menubutton',
icon: _item.icon || '',
menu: subItem
});
}
return false;
},
注释已经很详细了,就不讲解代码了。
其中的BUTTONS_MAP是我配置的一个title&cmd的map:
// 目前 plugin group支持的一些功能map
var BUTTONS_MAP = {
bold : ['Bold', 'Bold'],
italic : ['Italic', 'Italic'],
underline : ['Underline', 'Underline'],
strikethrough : ['Strikethrough', 'Strikethrough'],
alignleft : ['Align left', 'JustifyLeft'],
aligncenter : ['Align center', 'JustifyCenter'],
alignright : ['Align right', 'JustifyRight'],
alignjustify : ['Alignment', 'JustifyFull'],
bullist : ['Bullet list', 'InsertUnorderedList'],
numlist : ['Numbered list', 'InsertOrderedList'],
outdent : ['Decrease indent', 'Outdent'],
indent : ['Increase indent', 'Indent'],
cut : ['Cut', 'Cut'],
copy : ['Copy', 'Copy'],
paste : ['Paste', 'Paste'],
undo : ['Undo', 'Undo'],
redo : ['Redo', 'Redo'],
link : ['Insert link', 'mceLink'],
unlink : ['Remove link', 'unlink'],
image : ['Insert image', 'mceImage'],
removeformat : ['Clear formatting', 'mceCleanup'],
help : ['help', 'mceHelp'],
code : ['Source code', 'mceCodeEditor'],
hr : ['Horizontal line', 'InsertHorizontalRule'],
superscript : ['Subscript', 'subscript'],
subscript : ['Superscript', 'superscript'],
newdocument : ['New document', 'mceNewDocument'],
blockquote : ['Blockquote', 'mceBlockQuote']
};
以上就是group的使用方式和源码解释,希望能帮到你。
源码托管在github: 点我下载
Tinymce group plugin的更多相关文章
- OpenStack Networking overview
原文地址:http://docs.openstack.org/newton/install-guide-ubuntu/neutron-concepts.html Networking service ...
- 可视化HTML编辑器
[荐] 可视化HTML编辑器 CKEditor CKEditor是新一代的FCKeditor,是一个重新开发的版本.CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛 ...
- 开发XMPP IM
Openfire 是一个用Java 实现的XMPP 服务器,客户端可以通过IQ的方式与其进行通信(其实就是XML),客户端和服务器之间的通信是依靠底层Smack 库提供的各种功能来完成的.其实利用插件 ...
- WordPress中添加自定义评论表情包的方法
先来看看效果: 现在由于WordPress版本更新,再加上WordPress主题也越来越多,而现在的主题一般都是禁用了WordPress自带的评论表情,其实自带 的评论表情也是很丑的,但是以前我们可以 ...
- 即时通讯软件openfire+spark+smack
所以我基本上分为三篇文章来介绍此类软件的开发: 第一篇是关于XMPP 协议是啥,IM 是啥以及一个比较有名的开源实现,该开源实现包括三个部分(Spark.Smack和Openfire): 第二篇讲如何 ...
- nuxt Window 或 Document未定义解决方案
概述 在用nuxt开发服务端渲染项目并引入第三方库的时候,经常会遇到window或document未定义的情况,原因是这个第三方库里面用到了window或者document,然后在服务端打包的时候,n ...
- sudoers权限管理
该/etc/sudoers文件的权限管理很完善,覆盖了linux中的各种命令,各种shell.编辑器等等,在此留作以后作为参考. # This file MUST be edited with the ...
- db2配置、db和dbm
----start DB2 可以在四个不同层面配置: 一:系统环境变量(System Environment Variable) 系统环境变量用来配置DB2 的使用环境: 查看:set | grep ...
- Linux访问权限控制及时间同步实践
1.编写脚本/root/bin/checkip.sh,每5分钟检查一次,如果发现通过ssh登录失败 次数超过10次,自动将此远程IP放入Tcp Wrapper的黑名单中予以禁止防问 方式一:脚本+定时 ...
随机推荐
- Mysql查询结果导出为Excel的几种方法
本文地址:http://www.cnblogs.com/qiaoyihang/p/6398673.html 具体原文找不到了,此篇是借鉴门户的一篇文章 方法一:查询语句直接输出语法格式: Exampl ...
- go——方法
方法是与对象实例绑定的特殊函数.方法是面向对象编程的基本概念,用于维护和展示对象的自身状态.对象是内敛的,每个实例都有各自不同的独立特征,以属性和方法来暴露对外通信接口.普通函数则专注于算法流程,通过 ...
- linux svn 命令
windows下的TortoiseSVN是资源管理器的一个插件,以覆盖图标表示文件状态,几乎所以命令都有图形界面支持,比较好用,这里就不多说.主要说说linux下svn的使用,因为linux下大部分的 ...
- Mac OS X下搭建Android开发环境(包括SDK和NDK)
资源准备: JDK Eclipse Android SDK Android NDK ADT CDT ANT 搭建Android SDK开发环境: 1.JDK安装,要求版本>1.5, Mac O ...
- html基本标签介绍及应用
<!-- html标签 特征: 1.空白折叠现象 2.对空格和换行不敏感 3.标签要严格封闭 p标签的嵌套 多注意!!!!!! html中: 1.行内标签(不换行) (1)在一行内显示 span ...
- Educational Codeforces Round 11A. Co-prime Array 数学
地址:http://codeforces.com/contest/660/problem/A 题目: A. Co-prime Array time limit per test 1 second me ...
- [转]《Python爬虫学习系列教程》
<Python爬虫学习系列教程>学习笔记 http://cuiqingcai.com/1052.html 大家好哈,我呢最近在学习Python爬虫,感觉非常有意思,真的让生活可以方便很多. ...
- powerdesign初级入门教程
首先我们需要创建一个测试数据库,为了简单,我们在这个数据库中只创建一个Student表和一个Major表.其表结构和关系如下所示. 看看怎样用PowerDesigner快速的创建出这个数据库吧. 1. ...
- Mspec
Machine.Specifications Machine.Specifications (MSpec) is a context/specification framework that remo ...
- pyqt4学习之一:搭建环境和入门
还在继续写Python小工具,想起之前用Tkinter被坑得半死,决定换个框架写UI,又想顺便了解一下qt,就学习一下pyqt4 搭建环境 win:现在安装包 http://www.riverbank ...