美文网首页Web前端之路
HTML富文本编辑器Quill的使用

HTML富文本编辑器Quill的使用

作者: charles0427 | 来源:发表于2017-02-16 17:00 被阅读12729次

平台自动发送邮件的功能需要添加正文编辑,即需要提供在线编辑富文本,并将对应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

相关文章

网友评论

    本文标题:HTML富文本编辑器Quill的使用

    本文链接:https://www.haomeiwen.com/subject/rzdhwttx.html