富文本編輯器quill.js?開發(fā)之自定義插件示例詳解
quill
在 quill.js
中,擴展性最強大的功能就是插件
本文主要以一個圖片擴展的插件來介紹 quill
插件開發(fā)
在 quill.js
中他有著自己的名字: Modules
,而他也內(nèi)置了 5 種插件:
- TOOLBAR
- KEYBOARD
- HISTORY
- CLIPBOARD
- SYNTAX
分別是: 自定義工具欄、鍵盤事件控制、撤銷/重做功能配置、剪貼板配置、語法高亮
本文中的例子主要來源于 quill
最有名插件之一:https://github.com/kensnyder/quill-image-resize-module
modules 模板
先建立一個簡單的 modules
demo
創(chuàng)建文件 modules/index.ts
:
export default class QuillResize { private quill: any; constructor(quill, options = {}) { // 賦值, 備用 this.quill = quill; this.onCreate() } onCreate () { console.log('onCreate') // 在這里我們開始做一些事情 } }
App.tsx
引用 modules
:
import ImageResize from './modules/index' Quill.register('modules/resize', ImageResize); function App() { const modules = useMemo(() => ({ toolbar: { container: '#toolbar', }, resize: { // resize 就是上面注冊的名字 'modules/resize' foo: 1 // 這里傳一個自定義的參數(shù) } }), []); // ... return (<div className={'container'}> <CustomToolbar/> <ReactQuill ref={editorRef} theme="snow" value={value} modules={modules} onChange={setValue}/> </div>) }
這就是 modules
的雛形, 現(xiàn)在我們可以做任何想做的事了!
圖片交互
我們需要縮放圖片,那么就需要給圖片添加狀態(tài),即點擊圖片之后,用戶知道它是能夠交互的
添加監(jiān)聽事件,讓用戶知道高亮的是什么元素:
export default class QuillResize { constructor(quill, options = {}) { this.quill = quill; this.quill.root.addEventListener('mousedown', this.handleClick, false); } handleClick = (evt)=> { let show = false; let blot; const target = evt.target; if (target && target.tagName) { // 在 quill 編輯器中找到對應的點擊結(jié)構(gòu) blot = this.quill.constructor.find(target); if (blot) { // 如果有,再通過 judgeShow 函數(shù)判斷是否該高亮 show = this.judgeShow(blot, target); } } // 如果需要高亮, 則阻止默認動作(如 a 標簽等等) if (show) { evt.preventDefault(); return; } if (this.activeEle) { // 如果點擊在其他位置, 已經(jīng)聚焦圖片了, 那就取消高亮 this.hide(); } } }
handleClick
流程:
judgeShow
判斷是否應該聚焦圖片:
judgeShow = (blot, target) => { let res = false; if (!blot) return res; // 數(shù)據(jù)的一些判斷和補充 if (!target && blot.domNode) target = blot.domNode; // 參數(shù), 支持最小是 10px 的圖片, 可從外部傳入 const options = { limit: { minWidth: 10, }, } if (!options) return res; // 如果當前聚焦的是再次點擊的,則直接 return if (this.activeEle === target) return true; // 判斷大小的限制 const limit = options.limit || {}; if (!limit.minWidth || (limit.minWidth && target.offsetWidth >= limit.minWidth)) { res = true; // 當前聚焦和點擊的圖片不是同一個的時候, 隱藏原有的 if (this.activeEle) { this.hide(); } // 重新賦值 this.activeEle = target; this.blot = blot; // 調(diào)用 show 方法, 顯示聚焦樣式 this.show(); } return res; }
顯示圖片高亮聚焦樣式
show = ()=> { this.showOverlay(); // 顯示樣式 // this.initializeModules(); //初始化拖動事件/其他事件 這里暫不開放 // 如果有樣式, 則添加聚焦樣式 if (this.activeEle) this.activeEle.classList.add(this.activeClass); } showOverlay = () => { if (this.overlay) { this.hideOverlay(); } // 取消光標選中 this.quill.setSelection(null); // 阻止用戶選中 this.setUserSelect('none'); // 創(chuàng)建一個遮罩 this.overlay = document.createElement('div'); // 添加遮罩樣式 Object.assign(this.overlay.style, this.overlayStyle); // 插入到編輯器中 this.quill.root.parentNode.appendChild(this.overlay); this.hideProxy = () => { if (!this.activeEle) return; this.hide(); }; // 監(jiān)聽輸入事件, 發(fā)生變化則隱藏 this.quill.root.addEventListener('input', this.hideProxy, true); // 監(jiān)聽滾動事件, 遮罩要隨著滾動偏移 this.quill.root.addEventListener('scroll', this.updateOverlayPosition); // 樣式上添加具體的坐標 this.repositionElements(); };
圖片聚焦樣式:
滾動事件:
模塊擴展
在圖片聚焦時,添加額外功能,如圖片的縮放
新增文件 Resize.ts
class Resize { onCreate = () => { this.boxes = []; // 添加手柄 this.addBox('nwse-resize'); // top left this.addBox('nesw-resize'); // top right this.addBox('nwse-resize'); // bottom right this.addBox('nesw-resize'); // bottom left // 計算坐標 this.positionBoxes(); }; addBox = cursor => { const box = document.createElement('div'); Object.assign(box.style, this.handleStyles); box.style.width = `${this.handleStyles.width}px`; box.style.height = `${this.handleStyles.height}px`; // 監(jiān)聽動作, 拖動時觸發(fā)函數(shù) box.addEventListener('mousedown', this.handleMousedown, false); // 插入元素 this.overlay.appendChild(box); // 記錄到 boxes 中 this.boxes.push(box); }; }
按下手柄時觸發(fā)拖動事件
handleMousedown = evt => { // 修改狀態(tài) this.blot.handling && this.blot.handling(true); this.dragBox = evt.target; this.dragStartX = evt.clientX; this.dragStartY = evt.clientY; // 存儲坐標 this.preDragSize = { width: this.activeEle?.offsetWidth || 0, height: this.activeEle?.offsetHeight || 0, }; // 存儲原本尺寸 this.naturalSize = this.getNaturalSize(); const {width, height} = this.naturalSize; this.localRatio = height / width; this.editorMaxWidth = this.quill.container.clientWidth - 30; // 修改手柄的 cursor 屬性 this.setCursor(this.dragBox!.style.cursor); // 監(jiān)聽拖動和放開事件 // 根據(jù)拖動的距離, 計算圖片的尺寸,進行縮放 // 在 mouseup 中釋放監(jiān)聽事件 document.addEventListener('mousemove', this.handleDrag, false); document.addEventListener('mouseup', this.handleMouseup, false); };
大體流程:
拖動效果:
總結(jié)
當前例子只講述了點擊圖片之后的縮放功能,另外還有很多地方值得我們進行擴展,
如: 點擊圖片的預覽、展示圖片的尺寸、添加圖片的工具按鈕、擴展到視頻組件等等
不過即使是一個簡單的例子,也能管中窺豹,入門插件的開發(fā)
當然這這也只是 quill
富文本的開發(fā),在業(yè)界還有很多優(yōu)秀的富文本編輯器,他們都有著不同的實現(xiàn)和特殊功能,這些都值得我們繼續(xù)深入學習
以上就是富文本編輯器quill.js 開發(fā)之自定義插件示例詳解的詳細內(nèi)容,更多關(guān)于quill.js富文本編輯器插件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
解決vue更新路由router-view復用組件內(nèi)容不刷新的問題
今天小編就為大家分享一篇解決vue更新路由router-view復用組件內(nèi)容不刷新的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Vue+Koa2+mongoose寫一個像素繪板的實現(xiàn)方法
這篇文章主要介紹了Vue+Koa2+mongoose寫一個像素繪板的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09在Vue中使用Viser說明(基于AntV-G2可視化引擎)
這篇文章主要介紹了在Vue中使用Viser說明(基于AntV-G2可視化引擎),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10Element-ui table中過濾條件變更表格內(nèi)容的方法
下面小編就為大家分享一篇Element-ui table中過濾條件變更表格內(nèi)容的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03