微信小程序中富文本編輯器的實(shí)現(xiàn)
小程序也是有富文本編輯器這個(gè)控件的,別說(shuō),之前我還真沒(méi)注意。
官方文檔中給出的東西倒是不多,具體的代碼示例在下圖紅框中標(biāo)注的位置:

示例代碼大概是這個(gè)樣子:

通過(guò)官方的示例,我這邊大概了解了一下微信小程序editor的使用,我這里封裝了一個(gè)自定義組件:
效果如下圖所示:

功能展示大概就是這個(gè)樣子,我這里放一下我組件的全部代碼:
myEditor.js
// api 請(qǐng)求類
const API = require("../../request/api.js").report;
// 公共函數(shù)庫(kù)
const utils = require("../../utils/util.js");
// 加密字符
const constant = require("../../utils/constant.js");
// 雙語(yǔ)字典
const languageUtils = require("../../language/languageUtils");
// 獲取應(yīng)用實(shí)例
const app = getApp();
Component({
/**
* 組件的屬性列表
*/
properties: {
project_id: {
type: Number,
value: "",
},
//編輯器默認(rèn)提示語(yǔ)
placeholder: {
type: String,
value: "開始編輯吧...",
},
// 修改時(shí)顯示內(nèi)容
richTextContents: {
type: String,
value: "",
},
// 編輯的富文本的索引
index: {
type: Number,
value: 0,
},
},
/**
* 組件的初始數(shù)據(jù)
*/
data: {
// 用戶手機(jī)鍵盤得高度,大于0表示打開了鍵盤
},
/**
* 組件的方法列表
*/
methods: {
/**
* @name: 編輯器初始化完成時(shí)觸發(fā)
* @author: camellia
* @date: 20211220
*/
onEditorReady() {
let self = this;
this.triggerEvent("onEditorReady");
// 獲取編輯器實(shí)例
self
.createSelectorQuery()
.select("#editor")
.context((res) => {
self.editorCtx = res.context;
self.setContents(self.properties.richTextContents); //設(shè)置富文本內(nèi)容
})
.exec();
},
/**
* @name: 點(diǎn)擊工具欄格式化編輯文本
* @author: camellia
* @date: 20211220
*/
format(e) {
let self = this;
let { name, value } = e.target.dataset;
// 富文本編輯器格式化內(nèi)容方法
self.editorCtx.format(name, value);
},
/**
* @name: 工具欄選項(xiàng)選中,圖標(biāo)出現(xiàn)選中樣式
* @author: camellia
* @date: 20211220
*/
onStatusChange(e) {
let self = this;
self.setData({
formats: e.detail,
});
},
/**
* @name: 設(shè)置富文本內(nèi)容
* @author: camellia
* @date: 2021-12-23
* @param: rechtext string 富文本內(nèi)容
*/
setContents(rechtext)
{
this.editorCtx.setContents({
html: rechtext,
success: (res) => {
// 富文本內(nèi)容設(shè)置成功
// console.log("[setContents success]", res);
},
});
},
/**
* @name: 富文本編輯器輸入時(shí),獲取值
* @author: camellia
* @date: 20211220
*/
getEditorContent()
{
let self = this;
// 富文本編輯器獲取內(nèi)容方法
self.editorCtx.getContents({
success: (res) => {
let array = [];
array["html"] = res.html;
array["index"] = self.properties.index;
// 通過(guò)自定義事件把內(nèi)容傳到父組件
self.triggerEvent("getEditorValue", array);
},
});
},
},
});
myEditor.json
{
"component": true,
"usingComponents": {}
}
myEditor.wxss
@import "./icon/icon.wxss";
.ql-container{
padding: 12rpx;
border: 1rpx solid #707070;
}
/* 工具欄 */
.toolbar {
z-index: 999;
box-sizing: border-box;
padding: 0 20rpx;
height: 100rpx;
width: 100%;
display: flex;
align-items: center;
justify-content: space-between;
border: 2rpx solid #ECECEC;
border-left: none;
border-right: none;
background-color: #FFFFFF;
}
/* 工具欄點(diǎn)擊時(shí)出現(xiàn)選中樣式 */
.ql-active {
color: #22C704;
}
myEditor.wxml
<view class="toolbar" catchtouchend="format">
<i class="iconfont icon-charutupian" catchtouchend="insertImage"></i>
<i class="iconfont icon-format-header-2 {{formats.header === 2 ? 'ql-active' : ''}}" data-name="header" data-value="{{2}}"></i>
<i class="iconfont icon-format-header-3 {{formats.header === 3 ? 'ql-active' : ''}}" data-name="header" data-value="{{3}}"></i>
<i class="iconfont icon-zitijiacu {{formats.bold ? 'ql-active' : ''}}" data-name="bold"></i>
<i class="iconfont icon-zitixieti {{formats.italic ? 'ql-active' : ''}}" data-name="italic"></i>
<i class="iconfont icon-zitixiahuaxian {{formats.underline ? 'ql-active' : ''}}" data-name="underline"></i>
<i class="iconfont icon--checklist" data-name="list" data-value="check"></i>
<i class="iconfont icon-youxupailie {{formats.list === 'ordered' ? 'ql-active' : ''}}" data-name="list" data-value="ordered"></i>
<i class="iconfont icon-wuxupailie {{formats.list === 'bullet' ? 'ql-active' : ''}}" data-name="list" data-value="bullet"></i>
</view>
<editor style="height:auto; min-height:240rpx;"
id="editor"
class="ql-container"
bindinput="getEditorContent"
bindready="onEditorReady"
bindstatuschange="onStatusChange">
</editor>
以上就是微信小程序中富文本編輯器的實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于小程序富文本編輯器的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript中的偽數(shù)組用法及說(shuō)明
這篇文章主要介紹了JavaScript中的偽數(shù)組用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
JS實(shí)現(xiàn)向iframe中表單傳值的方法
這篇文章主要介紹了JS實(shí)現(xiàn)向iframe中表單傳值的方法,涉及js針對(duì)頁(yè)面元素及表單屬性操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-03-03
javascript 文章截取部分無(wú)損html顯示實(shí)現(xiàn)代碼
近在做一些內(nèi)容搜索的工作,搜索出來(lái)的內(nèi)容為html格式,列表部分需要顯示每項(xiàng)內(nèi)容的一部分。2010-05-05
node.js 一個(gè)簡(jiǎn)單的頁(yè)面輸出實(shí)現(xiàn)代碼
最近決定重拾node.js,用它來(lái)做一個(gè)合并JS文件的東西。由于忘得差不多了,先看能不能輸出一個(gè)頁(yè)面來(lái)再說(shuō)。以下是我的一些筆記,省得以后又忘凈光2012-03-03
javascript Blob對(duì)象實(shí)現(xiàn)文件下載
這篇文章主要為大家介紹了vue組件通信的幾種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12

