Quill富文本编辑器的使用

 1、快速起步

<!-- Include stylesheet --> 
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet"> 
<!-- Create the editor container --> 
<div id="editor"> <p>Hello World!</p> 
<p>Some initial <strong>bold</strong> text</p>
 <p><br></p> </div> 
<!-- Include the Quill library --> 
<script src="https://cdn.quilljs.com/1.3.6/quill.js"></script> 
<!-- Initialize Quill editor --> 
<script> 
var quill = new Quill('#editor', { theme: 'snow' }); 
</script>

2.自定义Toobar

方法一

 const 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': [] }],
        ['link', 'image'],
        ['clean']                                         // remove formatting button
    ];

var quill = new Quill('#editor-container', {
  modules: {
    toolbar: toolbarOptions,
  },
  placeholder: 'Compose an epic...',
  theme: 'snow'  // or 'bubble'
});

方法二

<!-- Create toolbar container -->
<div id="toolbar">
  <!-- Add font size dropdown -->
  <select class="ql-size">
    <option value="small"></option>
    <!-- Note a missing, thus falsy value, is used to reset to default -->
    <option selected></option>
    <option value="large"></option>
    <option value="huge"></option>
  </select>
  <!-- Add a bold button -->
  <button class="ql-bold"></button>
  <!-- Add subscript and superscript buttons -->
  <button class="ql-script" value="sub"></button>
  <button class="ql-script" value="super"></button>
</div>
<div id="editor"></div>

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

三。自定义图片上传

<span class="ql-formats"><button type="button" class="ql-link"></button></span>
<span class="ql-formats">
      <button type="button" onclick="imgClick()"  style="outline:none">
      <svg viewBox="0 0 18 18"> <rect class="ql-stroke" height="10" width="12" x="3" y="4"></rect> <circle class="ql-fill" cx="6" cy="7" r="1"></circle> <polyline class="ql-even ql-fill" points="5 12 5 11 7 9 8 10 11 7 13 9 13 12 5 12"></polyline> </svg>
      </button>
</span>
    function imgClick(){
        /*内存创建input file*/
        let input = document.createElement('input');
        input.type = 'file';
        input.name = 'uploadFile';
        input.accept = 'image/jpeg,image/png,image/jpg,image/gif';
        input.onchange = onFileChange;
        input.click();
    }
    function onFileChange(e) {
        let fileInput = e.target;
        if (fileInput.files.length === 0) {
            return
        }
        quill.focus();
        let data = new FormData;
        data.append('uploadFile', fileInput.files[0]);
        axios.post('/url', data)
            .then(res => {
                if (res.data) {
                    quill.focus();
                    var range = quill.getSelection(true);
                    quill.insertEmbed(range.index, 'image', res.data);
                    quill.setSelection(range.index + 1);
                }
            })
    } 

 五、获取富文本数据

let introHtml = this.quill.container.firstChild.innerHTML;
let text = quill.getText();

六、自动调节图片大小

<!--引入image-resize.min.js-->
<script src="/node_modules/quill-image-resize-module/image-resize.min.js"></script>
<script>
var quill = new Quill(editor, {
    modules: {
        //添加 调节图片 模块
        ImageResize: {
        }
    }
});
</script>

猜你喜欢

转载自blog.csdn.net/weixin_37380784/article/details/88605785