关于富文本编辑器summernote的基本使用(四)
2016-10-27 23:09
375 查看
对summernote官方文档的翻译,有错误之处欢迎高手指正
Context:Context包含modules和editor’s 声明的容器
Renderer:Renderer是用来创建元素的方法
UI:UI是用来新建ui元素的渲染函数
initialize
通过
destroy
当
shouldInitialize
这个方法来决定模块是否会被初始化
下面是AutoLink 模块的完整代码
可以通过暴露的API来调用自定义模块中的方法
模块化
summernote通过模块化支持功能的扩展。这种模块体系的建立受益于spring框架的启发。关键术语
Module:模块Context:Context包含modules和editor’s 声明的容器
Renderer:Renderer是用来创建元素的方法
UI:UI是用来新建ui元素的渲染函数
Module
Module是用来扩展功能的组件,具有生命周期,也有辅助函数和依赖于生命周期的函数initialize
通过
$(‘..’).summernote()进行初始化的时候会调用这个函数,可以用来在editor中绑定事件和创建元素
this.initialize = function () { // create button var button = ui.button({ className: 'note-btn-bold', contents: '<i class="fa fa-bold">' click: function (e) { context.invoke('editor.bold'); // 调用editor中的bold方法 } }); // button.render()返回button生成的jquery对象 this.$button = button.render(); $toolbar.append(this.$button); }
destroy
当
$(‘..’).summernote(‘destroy’)的时候调用这个方法,移除initlize即初始化时创建的元素,并解绑事件
this.destroy = function () { this.$button.remove(); this.$button = null; }
shouldInitialize
这个方法来决定模块是否会被初始化
// AirPopover's shouldInitialize this.shouldInitialize = function () { return options.airMode && !list.isEmpty(options.popover.air); };
下面是AutoLink 模块的完整代码
// Module Name is AutoLink
// @param {Object} context - states of editor
var AutoLink = function (context) {
// you can get current editor's elements from layoutInfo
var layoutInfo = context.layoutInfo;
var $editor = layoutInfo.editor;
var $editable = layoutInfo.editable;
var $toolbar = layoutInfo.toolbar;
// ui is a set of renderers to build ui elements.
var ui = $.summernote.ui;
// this method will be called when editor is initialized by $('..').summernote();
// You can attach events and created elements on editor elements(eg, editable, ...).
this.initialize = function () {
// create button
var button = ui.button({
className: 'note-btn-bold',
contents: '<i class="fa fa-bold">'
click: function (e) {
// invoke bold method of a module named editor
context.invoke('editor.bold');
}
});
// generate jQuery element from button instance.
this.$button = button.render();
$toolbar.append(this.$button);
}
// this method will be called when editor is destroyed by $('..').summernote('destroy');
// You should detach events and remove elements on `initialize`.
this.destroy = function () { this.$button.remove(); this.$button = null; }
};
配置模块
可以通过option自定义模块$(".summernote").summernote({ modules: { myModule: MyModule } });
可以通过暴露的API来调用自定义模块中的方法
$(".summernote").summernote("myModule.method", 'hello');
Plugin
可以以插件形式来自定义模块// src/mymodule.js $.extend($.summernote.plugins, { myModule: function (context) { // define module ... } });
相关文章推荐
- 关于富文本编辑器summernote的基本使用(二)
- 关于富文本编辑器summernote的基本使用(三)
- 关于使用summernote编辑器提示内容无法汉化临时解决办法
- summernote富文本编辑器的基本使用
- summernote编辑器插件使用
- 关于Unity中Cg的基本语法和使用
- Android WebView系列文章1-关于WebView基本使用介绍
- C# Note18: 使用wpf制作about dialog(关于对话框)
- 关于Springmvc中include与Sitemesh装饰器的基本使用
- 基于Metronic的Bootstrap开发框架经验总结(17)-- 使用 summernote插件实现HTML文档的编辑和图片插入操作
- 关于EasyUI与富文本编辑器结合使用的问题(kindueditor与uueditor)
- 关于CEF基本使用
- sizeof关于数组、指针、基本数据类型的使用
- 关于PHP使用redis服务的一些基本使用心得
- Android关于libs,jniLibs库的基本使用说明及冲突解决
- h关于媒体查询基本使用方法
- 使用bootstrap中的summernote并使用七牛云上图片
- Odoo Website 替换 Summernote 为第三方富文本编辑器
- 关于Aazure 使用以前保留的vhd创建虚拟机的基本步骤
- 关于初次使用Verilog HDL语言需要懂的基本语法