这次项目中需要用到编辑器插件,于是上网查了一下。由于需要的编辑器功能比较简单,不需要太多复杂功能,所以选择了一款特别轻量的summernote插件,而且后台操作也很简单。
官网:http://summernote.org/
github地址:https://github.com/summernote...

先来看一下官网的截图

麻雀虽小五脏俱全。完全可以满足编辑器的需要。

按照官网链接下载下来的是

我们需要使用的是在dist文件夹内

其中font主要是编辑器内的图标显示,lang是各种语言,css则是样式。我们主要来看一下summernote.js。

summernote.js

定义

 $.fn.extend({summernote: function () {var type = $.type(list.head(arguments));var isExternalAPICalled = type === 'string';var hasInitOptions = type === 'object';var options = hasInitOptions ? list.head(arguments) : {};options = $.extend({}, $.summernote.options, options);options.langInfo = $.extend(true, {}, $.summernote.lang['en-US'], $.summernote.lang[options.lang]);this.each(function (idx, note) {var $note = $(note);if (!$note.data('summernote')) {var context = new Context($note, options);$note.data('summernote', context);$note.data('summernote').triggerEvent('init', context.layoutInfo);}});var $note = this.first();if ($note.length) {var context = $note.data('summernote');if (isExternalAPICalled) {return context.invoke.apply(context, list.from(arguments));} else if (options.focus) {context.invoke('editor.focus');}}return this;}});

这就是初始化summernote时执行的函数。

$.extend(object) 可以理解为JQuery 添加一个静态方法。
$.fn.extend(object) 可以理解为JQuery实例添加一个方法。

默认的options如下

options: {modules: {'editor': Editor,'clipboard': Clipboard,'dropzone': Dropzone,'codeview': Codeview,'statusbar': Statusbar,'fullscreen': Fullscreen,'handle': Handle,// FIXME: HintPopover must be front of autolink//  - Script error about range when Enter key is pressed on hint popover'hintPopover': HintPopover,'autoLink': AutoLink,'autoSync': AutoSync,'placeholder': Placeholder,'buttons': Buttons,'toolbar': Toolbar,'linkDialog': LinkDialog,'linkPopover': LinkPopover,'imageDialog': ImageDialog,'imagePopover': ImagePopover,'videoDialog': VideoDialog,'helpDialog': HelpDialog,'airPopover': AirPopover},buttons: {},lang: 'zh-CN',// toolbar工具栏默认toolbar: [['style', ['style']],['font', ['bold', 'underline', 'clear']],['fontname', ['fontname']],['color', ['color']],['para', ['ul', 'ol', 'paragraph']],['table', ['table']],['insert', ['link', 'picture', 'video']],['view', ['fullscreen', 'codeview', 'help']]],// popoverpopover: {image: [['imagesize', ['imageSize100', 'imageSize50', 'imageSize25']],['float', ['floatLeft', 'floatRight', 'floatNone']],['remove', ['removeMedia']]],link: [['link', ['linkDialogShow', 'unlink']]],air: [['color', ['color']],['font', ['bold', 'underline', 'clear']],['para', ['ul', 'paragraph']],['table', ['table']],['insert', ['link', 'picture']]]},// air mode: inline editorairMode: false,width: null,height: null,focus: false,tabSize: 4,styleWithSpan: false,shortcuts: true,textareaAutoSync: true,direction: null,styleTags: ['p', 'blockquote', 'pre', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6'],fontNames: ['Arial', 'Arial Black', 'Comic Sans MS', 'Courier New','Helvetica Neue', 'Helvetica', 'Impact', 'Lucida Grande','Tahoma', 'Times New Roman', 'Verdana'],fontSizes: ['8', '9', '10', '11', '12', '14', '18', '24', '36'],// pallete colors(n x n)colors: [['#000000', '#424242', '#636363', '#9C9C94', '#CEC6CE', '#EFEFEF', '#F7F7F7', '#FFFFFF'],['#FF0000', '#FF9C00', '#FFFF00', '#00FF00', '#00FFFF', '#0000FF', '#9C00FF', '#FF00FF'],['#F7C6CE', '#FFE7CE', '#FFEFC6', '#D6EFD6', '#CEDEE7', '#CEE7F7', '#D6D6E7', '#E7D6DE'],['#E79C9C', '#FFC69C', '#FFE79C', '#B5D6A5', '#A5C6CE', '#9CC6EF', '#B5A5D6', '#D6A5BD'],['#E76363', '#F7AD6B', '#FFD663', '#94BD7B', '#73A5AD', '#6BADDE', '#8C7BC6', '#C67BA5'],['#CE0000', '#E79439', '#EFC631', '#6BA54A', '#4A7B8C', '#3984C6', '#634AA5', '#A54A7B'],['#9C0000', '#B56308', '#BD9400', '#397B21', '#104A5A', '#085294', '#311873', '#731842'],['#630000', '#7B3900', '#846300', '#295218', '#083139', '#003163', '#21104A', '#4A1031']],lineHeights: ['1.0', '1.2', '1.4', '1.5', '1.6', '1.8', '2.0', '3.0'],tableClassName: 'table table-bordered',insertTableMaxSize: {col: 10,row: 10},dialogsInBody: false,dialogsFade: false,maximumImageFileSize: null,callbacks: {onInit: null,//初始化回调函数onFocus: null,//聚集onBlur: null,//失去焦点onEnter: null,//回车键的回调函数onKeyup: null,onKeydown: null,onSubmit: null,//提交时回调函数onImageUpload: null,//这就是上传图片的回调函数onImageUploadError: null//上传图片出错},codemirror: {mode: 'text/html',htmlMode: true,lineNumbers: true},keyMap: {pc: {'ENTER': 'insertParagraph','CTRL+Z': 'undo','CTRL+Y': 'redo','TAB': 'tab','SHIFT+TAB': 'untab','CTRL+B': 'bold','CTRL+I': 'italic','CTRL+U': 'underline','CTRL+SHIFT+S': 'strikethrough','CTRL+BACKSLASH': 'removeFormat','CTRL+SHIFT+L': 'justifyLeft','CTRL+SHIFT+E': 'justifyCenter','CTRL+SHIFT+R': 'justifyRight','CTRL+SHIFT+J': 'justifyFull','CTRL+SHIFT+NUM7': 'insertUnorderedList','CTRL+SHIFT+NUM8': 'insertOrderedList','CTRL+LEFTBRACKET': 'outdent','CTRL+RIGHTBRACKET': 'indent','CTRL+NUM0': 'formatPara','CTRL+NUM1': 'formatH1','CTRL+NUM2': 'formatH2','CTRL+NUM3': 'formatH3','CTRL+NUM4': 'formatH4','CTRL+NUM5': 'formatH5','CTRL+NUM6': 'formatH6','CTRL+ENTER': 'insertHorizontalRule','CTRL+K': 'linkDialog.show'},mac: {'ENTER': 'insertParagraph','CMD+Z': 'undo','CMD+SHIFT+Z': 'redo','TAB': 'tab','SHIFT+TAB': 'untab','CMD+B': 'bold','CMD+I': 'italic','CMD+U': 'underline','CMD+SHIFT+S': 'strikethrough','CMD+BACKSLASH': 'removeFormat','CMD+SHIFT+L': 'justifyLeft','CMD+SHIFT+E': 'justifyCenter','CMD+SHIFT+R': 'justifyRight','CMD+SHIFT+J': 'justifyFull','CMD+SHIFT+NUM7': 'insertUnorderedList','CMD+SHIFT+NUM8': 'insertOrderedList','CMD+LEFTBRACKET': 'outdent','CMD+RIGHTBRACKET': 'indent','CMD+NUM0': 'formatPara','CMD+NUM1': 'formatH1','CMD+NUM2': 'formatH2','CMD+NUM3': 'formatH3','CMD+NUM4': 'formatH4','CMD+NUM5': 'formatH5','CMD+NUM6': 'formatH6','CMD+ENTER': 'insertHorizontalRule','CMD+K': 'linkDialog.show'}},icons: {'align': 'icon-align','alignCenter': 'icon-align-center','alignJustify': 'icon-align-justify','alignLeft': 'icon-align-left','alignRight': 'icon-align-right','indent': 'icon-indent-right','outdent': 'icon-indent-left','arrowsAlt': 'icon-resize-full','bold': 'icon-bold','caret': 'icon-caret-down','circle': 'icon-circle','close': 'icon-close','code': 'icon-code','eraser': 'icon-eraser','font': 'icon-font','frame': 'icon-frame','italic': 'icon-italic','link': 'icon-link','unlink': 'icon-chain-broken','magic': 'icon-magic','menuCheck': 'icon-check','minus': 'icon-minus','orderedlist': 'icon-list-ol','pencil': 'icon-pencil','picture': 'icon-picture','question': 'icon-question','redo': 'icon-redo','square': 'icon-square','strikethrough': 'icon-strikethrough','subscript': 'icon-subscript','superscript': 'icon-superscript','table': 'icon-table','textHeight': 'icon-text-height','trash': 'icon-trash','underline': 'icon-underline','undo': 'icon-undo','unorderedlist': 'icon-list-ul','video': 'icon-facetime-video'}
}

关于编辑器需要的工具栏toolbar具体属性可查看官网summernote-toolbar属性

更改工具栏图标

由于项目中我是直接使用fontawesome,所以我没有再引入summernote.font,直接在options中的icons中修改。但比较麻烦,不知道有什么更好的方法,求指导。

关于图片上传、提交、按键等回调函数也是在options中,详见callbacks部分。

初始化一个编辑器很简单。只需要定义

<div class="summernote" id="myid"></div>$(function () {$('.summernote').summernote();//或者$('#myid').summernote();
});

设置placeholder:

$('.summernote').summernote({placeholder:'请输入文章内容',...});

设置toolbar,

$('.summernote').summernote({toolbar:[['style',['bold','italic','underline','clear']],['fontsize',['fontsize']],['para',['ul','ol','paragraph']],['color',['color']]],...});

更改图片上传的方式:

需要提及的是,summernote默认的图片上传方式为base64方式。若需要修改,看以下代码。
【一定要注意:onImageUpload方法修改时要放在callbacks内配置,否则没用】

$('#myid').summernote({callbacks:{onImageUpload: function(files, editor, $editable) {UploadFiles(files,insertImg);}},...
});function insertImg(){for(i in imageUrl){$('.summernote').summernote('editor.insertImage',imageUrl[i]);}
}function UploadFiles(files,func){
//这里files是因为我设置了可上传多张图片,所以需要依次添加到formData中var formData = new FormData();for(f in files){formData.append("file", files[f]);}$.ajax({data: formData,type: "POST",url: "/uploadMultipleFile",cache: false,contentType: false,processData: false,success: function(imageUrl) {func(imageUrl);},error: function() {console.log("uploadError");}})
}

我们项目的后台是用spring+springMVC实现的。后台图片上传代码如下:

@RequestMapping(value = "/uploadMultipleFile", method = RequestMethod.POST, produces = "application/json;charset=utf8")
@ResponseBody
public String uploadMultipleFileHandler(@RequestParam("file") MultipartFile[] files, HttpServletRequest request) throws IOException {return     UploadUtil.uploadImage(request.getServletContext().getRealPath("/"), files);
}//UploadUtil.java中uploadImage方法如下public static String uploadImage(String serverPath, MultipartFile[] files) {try {String uploadPath = serverPath + getImageRelativePath();String images = "{}";//如果不存在目录,创建一个目录isDirectory(uploadPath);if (files != null && files.length > 0) {for (int i = 0; i < files.length; i++) {MultipartFile file = files[i];//save fileif (!file.isEmpty()) {String savePath = getImageRelativePath() + file.getOriginalFilename();//数据库保存的图片路径images = JSONUtil.addProperty(images, String.valueOf(i), savePath);save(file, uploadPath);}}}return images;} catch (Exception e) {e.printStackTrace();return "{}";}
}

设置编辑器中的值:

$('#myid').summernote('code',content);

需要注意的是,content是html代码,可能存在引号嵌套的问题导致报错,记得将引号进行转义。
后台处理-java代码:

content = content.replaceAll("'","\\'");
content = content.replaceAll(""", "\\"");

获取编辑器中的值:

var content = $('.summernote').summernote('code');

上传附件

这次项目需要使用附件,但发现summernote貌似没有附件功能,于是自己研究了一下代码,根据项目的需求,在link链接部分进行了修改。
效果如下:

首先,我们先看link按钮所绑定的事件。

context.memo('button.link', function () {return ui.button({contents: ui.icon(options.icons.link),tooltip: lang.link.link,click: context.createInvokeHandler('linkDialog.show')}).render();});

由上面的代码可以发现click事件为:linkDialog.show,那么我们再来看一下linkDialog

var LinkDialog = function (context) {...this.initialize = function () {//初始化...var body = '<div class="form-group">' +'<label>' + lang.link.textToDisplay + '</label>' +'<input class="note-link-text form-control" type="text" />' +'</div>' +'<div class="form-group">' +'<label>' + lang.link.attachment + '</label>' +'<input class="note-link-attachment form-control" type="file" />' +'</div>' +'<div class="form-group">' +'<label>' + lang.link.url + '</label>' +'<input class="note-link-url form-control" type="text" value="http://" />' +'</div>' +(!options.disableLinkTarget ?'<div class="checkbox">' +'<label>' + '<input type="checkbox" checked> ' + lang.link.openInNewWindow + '</label>' +'</div>' : '');var footer = '<button href="#" class="btn btn-primary note-link-btn disabled" disabled>' + lang.link.insert + '</button>';}
}

可以看到,点击链接按钮出现的弹框样式就在LinkDialoginitialize方法中的body,所以我在中间添加了一个input上传附件的部分。

'<div class="form-group">' +
'<label>' + lang.link.attachment + '</label>' +
'<input class="note-link-attachment form-control" type="file" />' +
'</div>' +

那么,我们需要在lang.link属性中,新增一个attachment附件属性。

除此之外,在中文的转换部分summernote-zh-CN.min.js中,添加linkattachment: '添加附件'

好了,那么我们接下来需要处理的问题是上传文件后的处理。

this.showLinkDialog = function (linkInfo) {return $.Deferred(function (deferred) {...//上传文件的输入框$linkAttachment = self.$dialog.find('.note-link-attachment'),ui.onDialogShown(self.$dialog, function () {...//对于输入框的事件绑定$linkAttachment.on('change', function() {UploadFiles($linkAttachment.val(),function(url){$linkUrl.val(url);//将上传后的URL赋值到linkUrl的输入框中});});}}
}

UploadFiles与上述修改上传图片的形式一样。

如果这篇文章对您有帮助,欢迎点赞。如果有疏漏,欢迎指正。
本文地址:http://lsxj615.com/2016/08/10...

summernote编辑器插件使用笔记相关推荐

  1. summernote java_summernote编辑器插件使用笔记

    这次项目中需要用到编辑器插件,于是上网查了一下.由于需要的编辑器功能比较简单,不需要太多复杂功能,所以选择了一款特别轻量的summernote插件,而且后台操作也很简单. 官网:http://summ ...

  2. 基于jquery的bootstrap在线文本编辑器插件Summernote

    Summernote是一个基于jquery的bootstrap超级简单WYSIWYG在线编辑器.Summernote非常的轻量级,大小只有30KB,支持Safari,Chrome,Firefox.Op ...

  3. HTML插件程序,10个最棒的jQuery和HTML所见即所得编辑器插件

    HTML5 所见即所得 WYSIWYG (What You See Is What You Get) 编辑器的需求量是非常大的.但是现在有太多这样的编辑器了,想要从其中找到一个合适的并不容易.今天这篇 ...

  4. mvc后台字符串转换html,在mvc中,使用summernote编辑器,我想发送数据的内容字符串和HTML到控制器...

    我是MVC的首发,所以有一些我不能做的. Summernote编辑器用于编写博客,Ajax用于发布.在这个编辑器里面写了一些字符后,smmernote编辑器和其他字符串数据中的这个html数据必须和a ...

  5. 编辑器插件不生效在html中,关于6.04版本HTML编辑器插件使用问题!

    今天将原来做的一个活字格工程文件当中的HTML编辑器插件更新为最新版本的时候出现一些问题,经过多方面排查终于发现问题所在(应该算Bug吧!),请官方大神们看是否有什么更好的解决方案.好了,废话不讲,直 ...

  6. jquery插件课程2 放大镜、多文件上传和在线编辑器插件如何使用

    jquery插件课程2 放大镜.多文件上传和在线编辑器插件如何使用 一.总结 一句话总结:插件使用真的还是比较简单的,引包,初始化,配置参数(json),配置数据(json),而后两步不是必须的.而且 ...

  7. php cms 插件开发,phpcms编辑器插件最新版——ckeditor4.5

    插件说明: 了解phpcms网站系统的站长都知道其编辑器是用的ckeditor网页编辑器插件.那么phpcms系统原带的ckeditor已是老版本,当前ckeditor官方最新版已经到4.5版. 本插 ...

  8. 第一次使用最新开发的在线编辑器讲课记录笔记

    2019独角兽企业重金招聘Python工程师标准>>> 第一次使用最新开发的在线编辑器讲课记录笔记,效果: 信息地址: http://www.phpkhbd.com/note/93 ...

  9. ubuntu18.04 Qt Creator 安装+ROS Qt Creator Plug-in 插件安装笔记

    ubuntu18.04 Qt Creator 安装+ROS Qt Creator Plug-in 插件安装笔记: 一.Qt Creator 5.10安装 官网下载地址: http://download ...

最新文章

  1. LeetCode - Palindrome Number
  2. Linux之xargs
  3. inxni扫地机器人_实用型助手,inxni以内扫地机器人 X332
  4. 0428专题:行内元素与块状元素
  5. ubuntu16.04编译boost for Android(boost 1.65)
  6. html表单不允许修改,[问题]如何在表单里定义(form)仅仅可读,不可修改的列?
  7. 华为鸿蒙系统学习笔记9-华为鸿蒙OS与LiteOS对比
  8. 斯坦福 CS183 Peter Thiel 创业课资料整理
  9. Lady Gaga ft.Beyonce- Telephone
  10. 《黑客大曝光:移动应用安全揭秘及防护措施》一3.5 小结
  11. python实现嵌套列表按指定位置元素排序、按子列表长度、子列表之和、绝对值和、最大/最小值排序
  12. DP:最大公共子序列
  13. codeblocks编译器报错Id returned 1 exit status
  14. 加拿大 oracle audio,来自加拿大的“简单”与“纯粹”——Verity Audio Otello座地式扬声器...
  15. 【最全】ISTQB- FL大纲(含重点)
  16. 【转】DSP是什么--DSP是神马东东??
  17. 使用Python打印爱心图案
  18. 【论文学习】Future Person Localization in First-Person Videos
  19. A leaf Variable that requires grad is being used in an in-place operation
  20. httpd离线安装(含依赖包下载地址)

热门文章

  1. ApexCharts使用示例 -- 多图表同时执行交互操作
  2. java checkbox数组_Java通过复选框控件数组实现添加多个复选框控件
  3. FlexSim2019版新功能——Emulation
  4. Vue学习之混入(Mixin)
  5. 透视:对企业内容管理的四个疑问
  6. 【Java从零到架构师第③季】【48】SpringBoot-Swagger
  7. Ocrking图片识别之Java实现本地验证码的识别
  8. 简约PPT模板素材哪里能免费下载?
  9. 表单全选取消全选案例
  10. Win10开机wsappx进程占用CPU资源过高,最全的解决方法总结。