Home>Article>WeChat Applet> WeChat Mini Program Practical Project Rich Text Editor Implementation
This article brings you relevant knowledge aboutWeChat Mini Program, which mainly introduces practical examples of rich text editor, including creating a publishing page, implementing basic layout, and implementing editing. Let’s take a look at the functions of the area operation bar and other contents. I hope it will be helpful to everyone.
[Related learning recommendations:小program learning tutorial]
The effect achieved As shown below:
The implemented function points are as follows:
First create a publishing pagearticleand generate it through configuration inapp.jsonpage.
"pages": [ "pages/article/article" ]
Inarticle.wxml, write the structure:
文章类型:{{objectArray[index].name}} 发布
Inarticle.wxss, write the basic style:
page{ width: 740rpx; margin: 0 auto; background-color: #f9f9f9; } .title { border: 1rpx solid #f2f2f2; margin: 10rpx; height: 70rpx; line-height: 70rpx; border-radius: 10rpx; } .picker{ padding: 10rpx; } .wrapper { padding: 5px; } .iconfont { display: inline-block; padding: 8px 8px; width: 24px; height: 24px; cursor: pointer; font-size: 20px; } .toolbar { box-sizing: border-box; border-bottom: 0; font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif; } .ql-container { box-sizing: border-box; padding: 12px 15px; width: 100%; min-height: 30vh; height: auto; background: #fff; margin-top: 20px; font-size: 16px; line-height: 1.5; border: 1rpx solid #f2f2f2; border-radius: 15rpx; } .button{ width: 360rpx; height: 80rpx; line-height: 80rpx; text-align: center; margin: auto; margin-top: 50rpx; border-radius: 8rpx; font-size: 32rpx; color: white; background-color: #497749!important; }
At this time we will find that the middle operation bar icon is not displayed. We need to introduce theiconfont.wxssfont icon in the header ofarticle.wxss. iconfont.wxss file acquisition address
@import "./assets/iconfont.wxss";
This article only implements the function of the operation bar and realizes rich text editing. For other article types, please implement it yourself. , it’s not difficult!
First, we need to get the rich text editor instanceEditorContext, get it throughwx.createSelectorQuery, we are on the pagePagefunction, create theonEditorReadyfunction to obtain the instance:
onEditorReady() { const that = this wx.createSelectorQuery().select('#editor').context(function (res) { that.editorCtx = res.context }).exec() }
Then bind this method to thebindreadyof the rich text editor On the attribute, it is triggered after the initialization of the rich text editor is completed, thereby obtaining the instance.
How do we modify the style of the text?
EditorContext.format(string name, string value)
.name
: CSS attribute;value
: value.By consulting the WeChat applet development documentation, we can see that to achieve the above functions, the values ofname
andvalue
we need are:
So how do we modify the text style by clicking the button?
name
andvalue
attributes to the icon5a8028ccc7a7e27417bff9f05adf5932
label and fill in the icon Corresponding toname
andvalue
in the above picture, if there is novalue
, just leave it blank.EditorContext.format
API.Pageformatin function Function:
format(e) { let { name, value } = e.target.dataset if (!name) return this.editorCtx.format(name, value) },
Problem: When we click on the icon, the text is changed style, but the style of the icon has not changed, and we cannot be prompted for the current style status of the text. So how to solve it?
After consulting the documentation related toeditorWeChat applet development, thebindstatuschangeattribute binding method will be displayed when you passContextThis method is triggered when changing the style in the editor, and will return the style that has been set in the selection.
Then we can add theformatsobject indatato store the style attributes after clicking. Then when the icon button is clicked, the set style is obtained and stored informatsthrough thebindstatuschangebinding method; when the template is rendered, in5a8028ccc7a7e27417bff9f05adf5932
On theclassattribute, add{{formats.align === 'right' ? 'ql-active' : ''}}
(such as text to the right), when If you click on this icon, then there is this attribute informats, then add our dynamic class nameql-activeto change the icon color.
具体实现
onStatusChange(e) { const formats = e.detail this.setData({ formats }) }
5a8028ccc7a7e27417bff9f05adf5932
标签上,添加{{formats.align === 'right' ? 'ql-active' : ''}}
.ql-active { color: #497749; }
首先在5a8028ccc7a7e27417bff9f05adf5932
标签上绑定相应的事件:
撤销 undo
调用EditorContext API即可
undo() { this.editorCtx.undo() }
恢复 redo
同理
redo() { this.editorCtx.redo() }
插入图片 insertImage
同理
insertImage() { const that = this wx.chooseImage({ count: 1, success: function (res) { wx.showLoading({ title: '正在上传图片', }) wx.cloud.uploadFile({ cloudPath: `news/upload/${time.formatTime(new Date)}/${Math.floor(Math.random() * 100000000)}.png`, // 上传至云端的路径 filePath: res.tempFilePaths[0], success: cover => { that.editorCtx.insertImage({ src: cover.fileID, data: { id: cover.fileID, role: 'god' }, success: function () { wx.hideLoading() } }) } }) } }) }
清空 clear
同理
clear() { this.editorCtx.clear({ success: function (res) { console.log("clear success") } }) }
【相关学习推荐:小程序学习教程】
The above is the detailed content of WeChat Mini Program Practical Project Rich Text Editor Implementation. For more information, please follow other related articles on the PHP Chinese website!