Quill是一个很流行的富文本编辑器,github上star大约21k:

github:https://github.com/quilljs/quill/

官网: https://quilljs.com/

使用

<!-- 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>

下载:

npm install quill@1.3.6

vue中使用:

<template>
<div>
<div id="editor">
<p>Hello World!</p>
<p>Some initial <strong>bold</strong> text</p>
<p><br></p>
</div>
</div>
</template> <script>
import Quill from 'quill' export default {
name: "QuillEditor",
mounted () {
this.initQuill()
},
beforeDestroy () {
this.quill = null
delete this.quill
},
methods: {
initQuill () {
const quill = new Quill('#editor', {
theme: 'snow'
})
this.quill = quill
},
}
}
</script>

效果

创建Quill实例需要两个参数,container与options

Container

container可以是css选择器,也可以是DOM对象:

const editor = new Quill('#editor')

或者

const container = document.getElementById('editor');
const editor = new Quill(container);

Options

包括theme、formats、modules等

const options = {
debug: 'info',
modules: {
toolbar: '#toolbar'
},
placeholder: 'Compose an epic...',
readOnly: true,
theme: 'snow'
};
const editor = new Quill('#editor', options);

获取与显示编辑内容

富文本编辑器的主要作用是编辑文本、保存、显示等。

获取编辑完成的内容:

const html = document.querySelector('#editor').children[0].innerHTML
console.log(html)

内容:

<p>Hello World!</p><p>Some initial <strong>bold</strong> text</p><p><br></p>

获取内容后置于编辑器中显示:

const html = document.querySelector('#editor').children[0].innerHTML
this.quill.pasteHTML('<h3>add some title</h3>' + html)

显示:

Toolbar

编辑器上方一栏可以设置文本格式部分,即为modules中的toolbar,可以使用默认值,也可以定制。

使用数组:

const toolbarOptions = ['bold', 'italic', 'underline', 'strike'];

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

效果:

也可以分组:

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': [] }], ['clean'] // remove formatting button
]; const quill = new Quill('#editor', {
modules: {
toolbar: toolbarOptions
},
theme: 'snow'
});

效果

同一组会置于同一<span>中。

也可以使用css选择器,最全的toolbar

<div id="toolbar">
<span class="ql-formats">
<select class="ql-font"></select>
<select class="ql-size"></select>
</span>
<span class="ql-formats">
<button class="ql-bold"></button>
<button class="ql-italic"></button>
<button class="ql-underline"></button>
<button class="ql-strike"></button>
</span>
<span class="ql-formats">
<select class="ql-color"></select>
<select class="ql-background"></select>
</span>
<span class="ql-formats">
<button class="ql-script" value="sub"></button>
<button class="ql-script" value="super"></button>
</span>
<span class="ql-formats">
<button class="ql-header" value="1"></button>
<button class="ql-header" value="2"></button>
<button class="ql-blockquote"></button>
<button class="ql-code-block"></button>
</span>
<span class="ql-formats">
<button class="ql-list" value="ordered"></button>
<button class="ql-list" value="bullet"></button>
<button class="ql-indent" value="-1"></button>
<button class="ql-indent" value="+1"></button>
</span>
<span class="ql-formats">
<button class="ql-direction" value="rtl"></button>
<select class="ql-align"></select>
</span>
<span class="ql-formats">
<button class="ql-link"></button>
<button class="ql-image"></button>
<button class="ql-video"></button>
<button class="ql-formula"></button>
</span>
<span class="ql-formats">
<button class="ql-clean"></button>
</span>
</div>

  

<script>
const quill = new Quill('#editor', {
modules: {
toolbar: '#toolbar'
}
});
</script>

  效果:

toolbar里的control与Quill的format是对应的,可以用来添加或者移除format:

formats

我们可以添加定制的handler来改变默认行为:

const toolbarOptions = {
handlers: {
// handlers object will be merged with default handlers object
'link': function(value) {
if (value) {
var href = prompt('Enter the URL');
this.quill.format('link', href);
} else {
this.quill.format('link', false);
}
}
}
} const quill = new Quill('#editor', {
modules: {
toolbar: toolbarOptions
}
}); // Handlers can also be added post initialization
const toolbar = quill.getModule('toolbar');
toolbar.addHandler('image', showImageUI);

可以定制handler来进行图片视频上传。

富文本编辑器Quill(一)简单介绍的更多相关文章

  1. 现代富文本编辑器Quill的内容渲染机制

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...

  2. 现代富文本编辑器Quill的模块化机制

    DevUI是一支兼具设计视角和工程视角的团队,服务于华为云DevCloud平台和华为内部数个中后台系统,服务于设计师和前端工程师.官方网站:devui.designNg组件库:ng-devui(欢迎S ...

  3. 富文本编辑器...quill 的使用放...

    移动端 quill 时候用的 是 div 而不是 textarea.... 引入 dom <link href="//cdn.quilljs.com/1.3.6/quill.snow. ...

  4. wpf 富文本编辑器richtextbox的简单用法

    最近弄得一个小软件,需要用到富文本编辑器,richtextbox,一开始以为是和文本框一样的用法,但是实践起来碰壁之后才知道并不简单. richtextbox 类似于Word,是一个可编辑的控件.结构 ...

  5. 富文本编辑器Quill(二)上传图片与视频

    image与video在Quill formats中属于Embeds,要在富文本中插入图片或者视频需要使用insertEmbed api. insertEmbed insertEmbed(index: ...

  6. 富文本编辑器Quill的使用

    我们经常需要使用富文本编辑器从后台管理系统上传文字,图片等用于前台页面的显示,Quill在后台传值的时候需要传两个参数,一个用于后台管理系统编辑器的显示,一个用前台页面的显示,具体代码如下截图: 另Q ...

  7. UEditor富文本编辑器简单使用

    UEditor富文本编辑器简单使用 一.下载地址:https://ueditor.baidu.com/website/ 官网中并没有 python 版本的 UEditor 富文本编辑器,本文简单介绍 ...

  8. react-quill 富文本编辑器

    适合react的一款轻量级富文本编辑器 1.http://blog.csdn.net/xiaoxiao23333/article/details/62055128 (推荐一款Markdown富文本编辑 ...

  9. vue-quill-editor 富文本编辑器插件介绍

    Iblog项目中博文的文本编辑器采用了vue-quill-editor插件,本文将简单介绍其使用方法. 引入配置 安装模块 npm install vue-quill-editor --save in ...

随机推荐

  1. JAVA 第五周学习总结

    20175303 2018-2019-2 <Java程序设计>第五周学习总结 教材学习内容总结 •使用关键字interface来定义一个接口,定义接口分包含接口声明和接口体. •接口体中包 ...

  2. numpy(二)

    通用函数: np.add  加 np.subtract  减 np.multiply  乘 np.divide  除 np.floor_divide  地板乘除法,取商 np.power  指数运算  ...

  3. 初识springboot

    一.springboot简介: 1.简化spring应用开发框架 2.把spring所有技术整合在了一起 3.J2EE开发的一站式解决方案 我曾经学习springMVC时候,那许许多多的配置文件的配置 ...

  4. mysql 查询 最大值,最小值,第二大,第三大 一共四个值

    最大值:select max(num) from table 第二大值:select max(num) from tablewhere num not in(select max(num) from ...

  5. 接口测试工具-Jmeter使用笔记(八:模拟OAuth2.0协议简化模式的请求)

    背景 博主的主要工作是测试API,目前已经用Jmeter+Jenkins实现了项目中的接口自动化测试流程.但是马上要接手的项目,API应用的是OAuth2.0协议授权,并且采用的是简化模式(impli ...

  6. python多进程web爬虫-提升性能利器

    背景介绍: 小爬我最近给部门开发了一系列OA的爬虫工具,从selenium前端模拟进化到纯requests后台post请求爬取,效率逐步提升.刚开始能维持在0.5秒/笔.可惜当数据超过2000笔后,爬 ...

  7. linux c tcp p2p

    江湖上一直都有这位哥哥的传说,也有很多人说自己就他的真身! 但是... 今天分享一下TCP连接的P2P demo,江湖的规矩也要与时俱进... ———————————————————————————— ...

  8. 从零开始一起学习SLAM | 掌握g2o顶点编程套路

    点"计算机视觉life"关注,置顶更快接收消息! ## 小白:师兄,上一次将的g2o框架<从零开始一起学习SLAM | 理解图优化,一步步带你看懂g2o代码>真的很清晰 ...

  9. VS 2017 激活码

    最近逐渐抛弃了 VS2015  更新迭代到2017版本安装流程不必说激活码双手奉上 Enterprise: NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Professional: KB ...

  10. FB面经Prepare: Bipartite a graph

    input friends relations{{1,2}, {2,3}, {3,4}} 把人分成两拨,每拨人互相不认识, 所以应该是group1{1,3}, group2{2,4} 这道题应该是ho ...