HTML富文本编辑器Quill的使用

平台自动发送邮件的功能需要添加正文编辑,即需要提供在线编辑富文本,并将对应html传回后台。
富文本编辑器使用quill.js
quill的风格较为简洁美观,提供了带toolbar的snow和不带toolbar的bubble两种主题

Demo

两种主题

snow

Quill引用

  1. npm下载: npm install quill@1.2.0; 或者release直接下载源文件
  2. 引用文件存放在/dist目录下,复制到项目中
  3. html中添加
<!-- Include stylesheet -->
<link href="https://cdn.quilljs.com/1.2.0/quill.snow.css" rel="stylesheet">

<!-- Create the editor container -->
<div id="editor">
</div>

<!-- Include the Quill library -->
<script src="https://cdn.quilljs.com/1.2.0/quill.js"></script>

<!-- Initialize Quill editor -->
<script>
  var quill = new Quill('#editor', {
    theme: 'snow'
  });
</script>

ToolBar

toolbar的自定义:

var toolbarOptions = [
  ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
  ['blockquote', 'code-block'],

  [{ 'header': 1 }, { 'header': 2 }],               // custom button values
  [{ 'list': 'ordered'}, { 'list': 'bullet' }],
  [{ 'script': 'sub'}, { 'script': 'super' }],      // superscript/subscript
  [{ 'indent': '-1'}, { 'indent': '+1' }],          // outdent/indent
  [{ 'direction': 'rtl' }],                         // text direction

  [{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
  [{ 'header': [1, 2, 3, 4, 5, 6, false] }],

  [{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
  [{ 'font': [] }],
  [{ 'align': [] }],

  ['clean']                                         // remove formatting button
];

var quill = new Quill('#editor', {
  modules: {
    toolbar: toolbarOptions
  },
  theme: 'snow'
});

文本对应Html获取

quill取消了getHtml()的API,getContents()返回的是Delta对象,一种JSON数组,getText()返回文本域里对应字符串。
因平台需将用户编辑的格式传回后台生成邮件的正文,而邮件的正文是Html格式,通过查issue找到获取Html的方法:quill.container.firstChild.innerHTML

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,989评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,198评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • iOS开发之UITableView全面解析详细整理:UITableView优化技巧iOS 保持界面流畅的技巧
    sellse阅读 179评论 0 0
  • 【周日轻松话题】0416你最想去的旅游目的地是哪里? 大连 高二那会有大连理工大学的老师来我们学校宣传,我第一次...
    一只正在成长的狮子阅读 214评论 0 1