1. 程式人生 > >富文字編輯器Quill的簡單React封裝

富文字編輯器Quill的簡單React封裝

最近要在React專案中實現富文字編輯器,我在基於不同外掛實現多種版本之後,體會到了Quill的強大,決定在專案中使用Quill實現富文字編輯器功能。

大多數富文字編輯器都有所見即所得的(指不能進行擴充套件)的問題,這意味著我們很難或者不可能對其進行自定義滿足一些特定的需求。Quill的強大之處在於通過將自身組織成模組,並提供強大的API來構建額外的模組解決擴充套件的問題,即所有能看到的,不能看到的功能統統都是一個獨立的模組,全部都是可以替換的。

雖然Quilljs是原生js的,但是可以輕鬆實現React版本的封裝。下邊是我在React中的簡單實現,供參考。

下載及匯入

直接npm install

[email protected]進行下載,在專案中

import Quill from 'quill';
import 'quill/dist/quill.snow.css';

html結構

在render()方法中只需要一個div標籤,給div一個id或者ref屬性,例如:

<div ref="textarea"></div>

初始化

在建構函式中對初始內容進行設定:

this.state = {
            'value':'<p>quill測試文字</p>',
        };
this.editor=null;
在componentDidMount中進行初始化:
const textbox = this.refs.textarea;
const options = {
            debug: 'warn',
            modules: {
                toolbar: true
            },
            placeholder: '請輸入文字...',
            readOnly: false,
            theme: 'snow'
        };
options為配置項,debug為在console中列印資訊的時期,可供選擇的有‘debug’,‘warn’,‘log’和‘info’;toolbar選為TRUE是指toolbar選用預設功能;readOnly指是否可編輯;theme有‘snow’和‘bubble’兩種選擇,分別是toolbar部分是否顯示。

下面是渲染文字編輯器,使用editor.clipboard.dangerouslyPasteHTML進行初始值賦值和繫結‘text-change’事件。

const editor =this.editor= new Quill(textbox,options);
const {value}=this.state;
if (value) editor.clipboard.dangerouslyPasteHTML(value);
editor.on('text-change', this.handleChange.bind(this));
在change處理函式中可以使用this.editor.root.innerHTML獲取文字編輯器的html內容。
handleChange () {
        let {value}=this.state;
        value = this.editor.root.innerHTML;
        this.setState({value});
    }
這就簡單實現了一個文字編輯器,效果如下,

theme指定為‘bubble’時,效果如下:


當然,我們可以配置toolbar,例如:

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', 'video'],
            ['clean']                                         // remove formatting button
        ];
相應的option為
modules: {
                toolbar: toolbarOptions
            },
展示效果為:

結束語


這樣就完成了一個React版本的簡單封裝。當然,quill的功能遠不止於此,還支援文字的樣式,多媒體檔案的上傳,響應鍵盤事件,操作歷史,公式支援等,如果外掛自帶的功能不足以實現你的需求,比如你要做一個動畫在選單欄上加一個圖示、選項或者什麼的。可以對整個選單欄進行定義和重寫,可參考http://www.qingpingshan.com/jb/javascript/202099.html。
quill的詳細配置和豐富的API在官網上有詳細介紹,可以自定義自己需要的富文字編輯器。
quill.js富文字編輯器的官網地址:https://quilljs.com/
quill.js富文字編輯器配置引數地址:https://quilljs.com/docs/configuration/
quill.js富文字編輯器API方法地址:https://quilljs.com/docs/api/

相關推薦

文字編輯Quill簡單React封裝

最近要在React專案中實現富文字編輯器,我在基於不同外掛實現多種版本之後,體會到了Quill的強大,決定在專案中使用Quill實現富文字編輯器功能。 大多數富文字編輯器都有所見即所得的(指不能進行擴充套件)的問題,這意味著我們很難或者不可能對其進行自定義滿足一些特定的需求

React中使用文字編輯Quill,支援貼上圖片

最近專案中需要用到富文字編輯器,並且客戶明確提出需要實現可以直接截圖貼上到文字框中。 由於我們的前端是用react寫的,於是就去參考了知乎的實現,發現知乎的富文字框是可以直接貼圖進去的,但由於看不到原始碼,只能瀏覽器除錯看了一下,發現他就是一個可編輯的div,

【js】kindeditor文字編輯簡單使用

首先去下載kindeditor的相關檔案 引入css以及js <link rel="stylesheet" href="js/kindeditor-4.1.10/themes/defaul

wangEditor文字編輯簡單使用

第二步:專案中引入js   第三步:jsp頁面中使用 <%@ page language="java" contentType="text/html; charset=UTF-8"

一款輕便的文字編輯---Quill

/* 編輯器操作條選項 */ var toolbarOptions = [ ['bold', 'italic', 'underline', 'strike'], //開關按鈕 ['blockqu

現代文字編輯Quill的模組化機制

    DevUI是一支兼具設計視角和工程視角的團隊,服務於華為雲DevCloud平臺和華為內部數箇中後臺系統,服務於設計師和前端工程師。官方網站:devui.designNg元件庫:ng-devui(歡迎Star) 引言 本文基於DevUI的富文字編輯器開發實踐和Quill原始碼寫成。

現代文字編輯Quill的內容渲染機制

DevUI是一支兼具設計視角和工程視角的團隊,服務於華為雲DevCloud平臺和華為內部數箇中後臺系統,服務於設計師和前端工程師。官方網站:devui.designNg元件庫:ng-devui(歡迎Star) 引言 在 Web 開發領域,富文字編輯器( Rich Text Editor )是一個使用場景非常

quill——簡單文字編輯

先介紹一下一般網頁如何實現 quill 富文字編輯器 引入 quill.js 檔案 <script src="https://cdn.quilljs.com/1.3.3/quill.j

Vue2 封裝Quill 文字編輯元件 Vue-Quill-Editor

1、安裝   npm install vue-quill-editor --save 2、使用 引入vue-quill-editor import { quillEditor } from 'vue-quill-editor' <!-- --><quil

vue專案中文字編輯vue-quill-editor的使用

前端開發過程中,會遇到在頁面上加入富文字編輯器,在vue專案中開發遇到這一需求的時候,我們可以使用富文字編輯器vue-quill-editor,話不多說,先上一張效果圖: 1)安裝 vue-quill-editor 依賴 npm install vue

angularjs中簡單使用kindeditor文字編輯

如何在angularjs中快速使用kindeditor富文字編輯器 ? 先引入相關的css樣式和js檔案: <!-- 富文字編輯器 --> <link rel="stylesheet" href="../plugins/kindedit

【Vue】quill-editor文字編輯元件的運用與修改配置使圖片上傳到伺服器

前言:Vue的生態已經越來越繁榮,越來越多有趣好用的元件加入的生態中了。quill-editor富文字編輯器就是很好用的元件之一。 一、quill-editor的安裝與使用 ①、安裝 npm install vue-quill-editor --save ②、

Django之文字編輯的使用,超級簡單~

前端顯示: Step1: pip3 install django-tinymce Step2: <script src="/static/tiny_mce/tiny_m

百度文字編輯UEditor使用簡單示例

HTML程式碼: <form id="f_edit" method="post"> <input name="id" type="hidden" value="${activit

適用於React文字編輯 -- react-umedito 圖片上傳本地伺服器解決方案

react-umeditor,這是liuhong1happy同學將百度富文字編輯器用react封裝的一個元件。 安裝:npm install react-umeditor –save 使用:reac

react 0.14中使用百度文字編輯

2017年12月01日 15:59:33 lz101281 閱讀數:1293 標籤: ie 8 語言

ueditor文字編輯java使用及最簡單的配置(ssh)

之前用過ueditor,結果這兩天要用又忘了怎麼用了,寫個文件記錄下來 1. UEditor簡介 UEditor是由百度web前端研發部開發所見即所得富文字web編輯器,具有輕量,可定製,注重使用者體驗等特點,開源基於MIT協議,允許自由使

在Vue專案使用quill-editor帶樣式編輯(更改插入圖片和視訊) 運用vue-quilt-editor編寫文字編輯 自定義圖片路徑 獲取後臺返回路徑

一、首先在main.js  引入 vue-quilt-editorimport VueQuillEditor from 'vue-quill-editor'import 'quill/dist/quill.core.css'import 'quill/dist/quill.s

wangEditor文字編輯+react+antd的使用

1、github上發現富文字編輯器: 2、結合react+antd的具體使用: 案例使用場景:MyModal為彈窗,彈窗顯示 編輯名稱及描述。描述使用wangeditor富文字編輯器實現。 MyModal.js import { Form,

Vue +Element UI +vue-quill-editor 文字編輯及插入圖片自定義

1.安裝npm install vue-quill-editor --save2.在main.js中引入import VueQuillEditor from 'vue-quill-editor' im