解決方案:微信小程序editor富文本編輯器的使用,拿走不謝
優(yōu)采云 發(fā)布時(shí)間: 2022-10-16 10:17解決方案:微信小程序editor富文本編輯器的使用,拿走不謝
前言:對于產(chǎn)品的上傳,我們大部分都是在PC后臺,因為管理起來(lái)比較方便高效,但是也有一些客戶(hù)想在手機上實(shí)現簡(jiǎn)單的上傳,使用富文本編輯器,正好editor富文本編輯器自帶小程序表單組件,一起來(lái)學(xué)習吧。
如果對大家有幫助,請點(diǎn)贊轉發(fā)。
特征
文件地址:
整合后的頁(yè)面:
代碼使用
wxml:
wxss:
@import "../common/lib/weui.wxss";
@import "./assets/iconfont.wxss";
.container {
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.ql-container {
box-sizing: border-box;
width: 100%;
height: 100%;
font-size: 16px;
line-height: 1.5;
overflow: auto;
padding: 10px 10px 20px 10px;
border: 1px solid #ECECEC;
}
.ql-active {
color: #22C704;
}
.iconfont {
display: inline-block;
width: 30px;
height: 30px;
cursor: pointer;
font-size: 20px;
}
.toolbar {
box-sizing: border-box;
padding: 0 10px;
height: 50px;
width: 100%;
position: fixed;
left: 0;
right: 100%;
bottom: 0;
display: flex;
align-items: center;
justify-content: space-between;
border: 1px solid #ECECEC;
border-left: none;
border-right: none;
}
css引用的兩個(gè)外部鏈接在文章的末尾,我會(huì )給出下載地址。
js:
Page({
data: {
formats: {},
readOnly: false,
placeholder: '開(kāi)始輸入...',
editorHeight: 300,
keyboardHeight: 0,
isIOS: false
},
readOnlyChange() {
this.setData({
readOnly: !this.data.readOnly
})
},
onLoad() {
const platform = wx.getSystemInfoSync().platform
const isIOS = platform === 'ios'
this.setData({ isIOS})
const that = this
this.updatePosition(0)
let keyboardHeight = 0
wx.onKeyboardHeightChange(res => {
if (res.height === keyboardHeight) return
const duration = res.height > 0 ? res.duration * 1000 : 0
keyboardHeight = res.height
setTimeout(() => {
wx.pageScrollTo({
scrollTop: 0,
success() {
that.updatePosition(keyboardHeight)
that.editorCtx.scrollIntoView()
}
})
}, duration)
})
},
updatePosition(keyboardHeight) {
const toolbarHeight = 50
const { windowHeight, platform } = wx.getSystemInfoSync()
let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
this.setData({ editorHeight, keyboardHeight })
},
calNavigationBarAndStatusBar() {
const systemInfo = wx.getSystemInfoSync()
const { statusBarHeight, platform } = systemInfo
const isIOS = platform === 'ios'
const navigationBarHeight = isIOS ? 44 : 48
return statusBarHeight + navigationBarHeight
},
onEditorReady() {
const that = this
wx.createSelectorQuery().select('#editor').context(function (res) {
that.editorCtx = res.context
}).exec()
},
blur() {
this.editorCtx.blur()
},
format(e) {
let { name, value } = e.target.dataset
if (!name) return
// console.log('format', name, value)
this.editorCtx.format(name, value)
},
onStatusChange(e) {
const formats = e.detail
this.setData({ formats })
},
insertDivider() {
this.editorCtx.insertDivider({
success: function () {
console.log('insert divider success')
}
})
},
clear() {
this.editorCtx.clear({
success: function (res) {
console.log("clear success")
<p>
}
})
},
removeFormat() {
this.editorCtx.removeFormat()
},
insertDate() {
const date = new Date()
const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
this.editorCtx.insertText({
text: formatDate
})
},
insertImage() {
const that = this
that.blur();
wx.showLoading({
title: '加載中…',
})
setTimeout(function(){
wx.hideLoading();
wx.chooseImage({
count: 1,
success: function (res) {
that.editorCtx.insertImage({
src: res.tempFilePaths[0],
data: {
id: 'abcd',
role: 'god'
},
width: '80%',
success: function () {
console.log('insert image success')
}
})
}
})
},500);
}
})
</p>
整體目錄結構:
富文本編輯器無(wú)法在開(kāi)發(fā)者工具上演示,必須在手機上運行才能查看效果。
示例代碼:
如果需要代碼,可以通過(guò)運行示例代碼在開(kāi)發(fā)者工具中獲取。
富文本編輯器中方法api介紹
文件地址:
比如怎么上傳圖片,怎么設置編輯器的內容,怎么獲取編輯器的內容等等。其實(shí)重點(diǎn)是獲取編輯器的內容,然后傳到后臺數據庫.
獲取編輯器內容的代碼:
that.editorCtx.getContents({
success(res){
var description = res['html'];//詳情
wx.request({
url: HTTP_REQUEST_URL+'/api/user/product_create',
data: {
},
method: 'POST',
dataType:'json',
header: header,
success: function (res) {
var data = res['data'];
if(data['code']==200){
}else{
}
},
fail: function (res) {
},
});
}
})
總結:
小程序的富文本編輯器可以實(shí)現圖文上傳、排序等功能的基本操作。對于普通用戶(hù)來(lái)說(shuō),只要能輸入文字、上傳圖片進(jìn)行排版,就可以解決基本需求。
上一篇文章提到使用編輯器上傳圖片會(huì )出現樣式問(wèn)題
如果有朋友在使用過(guò)程中碰巧遇到這樣的問(wèn)題,可以看看我昨天發(fā)的文章。微信小程序內置編輯器編輯器上傳圖片wx.chooseImage樣式問(wèn)題
我是一名小程序軟件開(kāi)發(fā)人員。每天分享開(kāi)發(fā)過(guò)程中遇到的知識點(diǎn)。如果對你有幫助,請給我點(diǎn)個(gè)贊,然后去,非常感謝。
解決方案:WordPress修改管理員郵箱的方法有哪些
本文來(lái)自
要想把網(wǎng)站操作好,及時(shí)更新文章是必不可少的。文章需要在WordPress后臺編輯,需要有編輯器。那么有人會(huì )問(wèn)什么是WordPress文章 編輯器?
以下是推薦給大家的兩個(gè) WordPress 編輯器插件:
1.經(jīng)典編輯器
現在 WordPress 的默認編輯器是古騰堡編輯器,但還是有很多人使用經(jīng)典編輯器,因為它好用。接下來(lái),我將教你如何切換回經(jīng)典編輯器。
在網(wǎng)站的后臺打開(kāi)“安裝插件”就可以看到(如果看不到就在搜索框輸入:經(jīng)典編輯器)
如果沒(méi)有安裝,點(diǎn)擊安裝(本站已經(jīng)安裝了上圖),然后啟用就可以使用了。
2. TinyMCE Advanced(經(jīng)典編輯器增強版)
TinyMCE Advanced 是 WordPress 經(jīng)典編輯器的增強版,用戶(hù)數量相當龐大(100 萬(wàn)+)。本插件常用的功能都有,表格、粗體、斜體、下劃線(xiàn)、刪除線(xiàn)、上標、下標、插入代碼、清除格式、錨點(diǎn)、橫線(xiàn)、特殊字符等。對于一般人來(lái)說(shuō),是足夠的。
這兩個(gè)WordPress文章編輯器也是大家常用的,大家可以根據自己的喜好選擇。