一款輕便的富文字編輯器---Quill
阿新 • • 發佈:2019-02-11
/* 編輯器操作條選項 */
var toolbarOptions = [
['bold', 'italic', 'underline', 'strike'], //開關按鈕
['blockquote', 'code-block'],
[{'header': 1}, {'header': 2}], //自定義按鈕值
[{'list': 'ordered'}, {'list': 'bullet'}],
[{'script' : 'sub'}, {'script': 'super'}], // 上標/下標
[{'indent': '-1'}, {'indent': '+1'}], // 減少縮排/縮排
[{'direction': 'rtl'}], // 文字方向
[{'size': ['small', false, 'large', 'huge']}], // 自定義下拉
[{'header': [1, 2, 3, 4, 5, 6, false]}],
[{'color': []}, {'background' : []}], //使用主題的預設下拉
[{'font': []}],
[{'align': []}],
['clean'], //移除格式化
['image'],
['video'],
['formula'] //需要載入cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.js
];
/* 例項化編輯器 */
var quill = new Quill('#editor', {
/*debug: 'info',*/
modules: {
//formula: true, //公式;需要載入cdnjs.cloudflare.com/ajax/libs/KaTeX/0.7.1/katex.min.js
//syntax: true, //高亮;需要載入cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js
/*toolbar: {
container:"#editor_header"
}*/ // 或者 toolbar :'#editor_header'
toolbar:toolbarOptions //指定編輯器操作條
},
theme: 'snow', //主題,有兩種,snow和bubble
placeholder:'請輸入',
readOnly: false
});
/* 傳入布林值,控制編輯器是否可用 */
quill.enable();
//quill.blur(); //失去焦點
//quill.focus(); //獲得焦點
/* 事件的繫結 */
quill.on('text-change', function(delta, oldDelta, source) {
console.log(delta);
console.log(oldDelta);
console.log(source);
console.log(quill.container.firstChild.innerHTML); //獲取當前富文字編輯器例項的內容(帶html標籤)
});
//quill.off('text-change', handler); //事件的解綁
/* 向編輯器中插值 */
quill.clipboard.dangerouslyPasteHTML(' <b>Hello World</b><p>new line</p>'); //向編輯器中插入html片段
quill.setText('Hello!'); //向編輯器中插入文字
/* 獲取編輯器中的值 */
console.log(quill.getContents());
/* 自定義按鈕 */
var myBtn = document.querySelector("#my_button");
myBtn.addEventListener("click",function(){
console.log('my-btn')
})