为KindEditor 添加“一键去除空格功能”

环境说明:KindEditor  4.1.11

一、确定你在使用KindEditor时,引用的是kindEditor-all.js,找到任何一个已经存在的功能,例如,清除HTML代码,我在做的时候本来是想将这个功能加在清除HTML代码这一功能中,后又觉得不妥,就在清除HTML代码下面,新建一个方法,如下,这才进入正题:

 

 1 KindEditor.plugin('clearTab', function (K) {
 2     var self = this, name = 'clearTab';
 3     self.clickToolbar(name, function () {
 4         self.focus();
 5         var html = self.html();
 6         html = html.replace(/\ +/g, '');
 7         html = html.replace(/\ +/g, '');
 8         html = html.replace(/\ +/g, '');
 9         html = html.replace(/\ +/g, '');
10 
11         html = html.replace(/\s+([^<>]+)(?=<)/g, function (match) {
12             return html = match.replace(/\s+/g, '');
13         });
14         self.html(html);
15         self.cmd.selection(true);
16         self.addBookmark();
17     });
18 });

二、一键去除空格的主体功能好了,接下来就要在工具栏中展示了,很简单,找到items数组,在数组后追加“clearTab”,

三、此时你会发现工具栏中还是没有显示,接着找到‘KindEditor.lang’对象,新加一个

clearTab: '清除空格',  

四、接着你还发现没有显示出来,那是因为没有给样式,找到引用的default.css文件,新加样式,从样式可以看出来,你还需要个小图标来显示它,到这里就很随意啦,弄个你喜欢的图标就好

.ke-icon-clearTab{
    background-position: 0px -1250px;
    width: 16px;
    height: 16px;
}

五、完成,效果如下

posted @ 2019-01-14 13:44  小红帽子  阅读(480)  评论(0编辑  收藏  举报