微信小程序实战项目之富文本编辑器实现

 3030

本篇文章给大家带来了关于微信小程序的相关知识,其中主要介绍了关于富文本编辑器的实战示例,包括了创建发布页面、实现基本布局、实现编辑区操作栏的功能等内容,下面一起来看一下,希望对大家有帮助。


微信小程序实战项目之富文本编辑器实现


1. 实现效果

实现的效果如下图:


微信小程序实战项目之富文本编辑器实现


实现的功能点如下:

文本加粗、斜体、下划线,对齐方式

撤销、恢复、插入图片、删除功能。


2. 创建发布页面,实现基本布局

首先创建发布页面 article,在 app.json 中通过配置生成页面即可。

  1. "pages": [
  2.     "pages/article/article"
  3. ]

在 article.wxml 中,书写结构:

  1. <view>
  2.   <!-- 文章类型 -->
  3.   <view>
  4.     <picker bindchange="bindPickerChange" model:value="{{index}}" range="{{array}}">
  5.       <view class="picker">
  6.         文章类型:{{objectArray[index].name}}
  7.       </view>
  8.     </picker>
  9.   </view>
  10.   <!-- 文章标题 -->
  11.   <view>
  12.     <input name="title" class="title" placeholder="请输入文章标题" maxlength="18" model:value="{{title}}"></input>
  13.   </view>
  14.   <!-- 编辑区 -->
  15.   <view class="container">
  16.     <view class="page-body">
  17.       <view class='wrapper'>
  18.         <!-- 操作栏 -->
  19.         <view class='toolbar' bindtap="format">
  20.            <i class="iconfont icon-zitijiacu"></i>
  21.           <i class="iconfont icon-zitixieti"></i>
  22.           <i class="iconfont icon-zitixiahuaxian"></i>
  23.           <i class="iconfont icon-zuoduiqi"></i>
  24.           <i class="iconfont icon-juzhongduiqi"></i>
  25.           <i class="iconfont icon-youduiqi"></i>
  26.           <i class="iconfont icon-undo"></i>
  27.           <i class="iconfont icon-redo"></i> 
  28.           <i class="iconfont icon-charutupian"></i>
  29.           <i class="iconfont icon-shanchu"></i>
  30.  
  31.         </view>
  32.         <!-- 文章内容区,富文本编辑器 -->
  33.         <editor id="editor" class="ql-container" placeholder="{{placeholder}}"  showImgSize showImgToolbar showImgResize>
  34.         </editor>
  35.         <!-- 发布按钮 -->
  36.         <view class="button" bindtap="formSubmit">发布</view>
  37.       </view>
  38.     </view>
  39.   </view>
  40. </view>

在 article.wxss,书写基本的样式:

  1. page{
  2.     width: 740rpx;
  3.     margin: 0 auto;
  4.     background-color: #f9f9f9;
  5.  
  6. }
  7.  
  8. .title {
  9.   border: 1rpx solid #f2f2f2;
  10.   margin: 10rpx;
  11.   height: 70rpx;
  12.   line-height: 70rpx;
  13.   border-radius: 10rpx;
  14. }
  15.  
  16. .picker{
  17.   padding: 10rpx;
  18. }
  19.  
  20. .wrapper {
  21.   padding: 5px;
  22. }
  23.  
  24. .iconfont {
  25.   display: inline-block;
  26.   padding: 8px 8px;
  27.   width: 24px;
  28.   height: 24px;
  29.   cursor: pointer;
  30.   font-size: 20px;
  31. }
  32.  
  33. .toolbar {
  34.   box-sizing: border-box;
  35.   border-bottom: 0;
  36.   font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  37. }
  38.  
  39. .ql-container {
  40.   box-sizing: border-box;
  41.   padding: 12px 15px;
  42.   width: 100%;
  43.   min-height: 30vh;
  44.   height: auto;
  45.   background: #fff;
  46.   margin-top: 20px;
  47.   font-size: 16px;
  48.   line-height: 1.5;
  49.   border: 1rpx solid #f2f2f2;
  50.   border-radius: 15rpx;
  51. }
  52.  
  53. .button{
  54.   width: 360rpx;
  55.   height: 80rpx;
  56.   line-height: 80rpx;
  57.   text-align: center;
  58.   margin: auto;
  59.   margin-top: 50rpx;
  60.   border-radius: 8rpx;
  61.   font-size: 32rpx;
  62.   color: white;
  63.   background-color: #497749!important;
  64. }

这时我们会发现中间的操作栏图标不显示,我们需要在 article.wxss 中头部引入 iconfont.wxss 字体图标。 iconfont.wxss 文件获取地址

  1. @import "./assets/iconfont.wxss";


3. 实现编辑区操作栏的功能

本文只实现操作栏的功能,实现富文本编辑,其他文章类型的选择,请自行实现,不难哦!


微信小程序实战项目之富文本编辑器实现


首先,我们需要获取富文本编辑器实例 EditorContext,通过 wx.createSelectorQuery 获取,我们在页面 Page 函数中,创建 onEditorReady 函数,用于获取该实例:

  1. onEditorReady() {
  2.     const that = this
  3.     wx.createSelectorQuery().select('#editor').context(function (res) {
  4.         that.editorCtx = res.context
  5.     }).exec()
  6. }

然后将该方法绑定到富文本编辑器的 bindready 属性上,随着富文本编辑器初始化完成后触发,从而获取实例。

  1. <editor id="editor"
  2. class="ql-container"
  3. placeholder="{{placeholder}}" 
  4. showImgSize 
  5. showImgToolbar 
  6. showImgResize 
  7. bindstatuschange="onStatusChange"
  8. read-only="{{readOnly}}"
  9. bindready="onEditorReady">

3.1. 实现文本加粗、斜体、文本下划线、左对齐、居中对齐、右对齐


微信小程序实战项目之富文本编辑器实现


我们如何修改文本的样式呢?

通过 EditorContext 实例提供的APIEditorContext.format(string name, string value),进行样式修改。

name:CSS属性;value:值。

通过查阅微信小程序开发文档可知,实现上述功能,我们需要的 name 和 value的值为:

微信小程序实战项目之富文本编辑器实现


那么我们如何通过点击按钮,来修改文本样式呢?

首先我们在图标 <i> 标签上绑定name 和 value 属性,填上图标所对应上图的 name 和 value,无 value 的不填即可。

然后在父标签上绑定事件 format,通过该事件函数,使用 EditorContext.format API 进行样式修改。

  1. <view class='toolbar' bindtap="format">
  2.   <i class="iconfont icon-zitijiacu  data-name="bold"></i>
  3.   <i class="iconfont icon-zitixieti data-name="italic"></i>
  4.   <i class="iconfont icon-zitixiahuaxian  data-name="underline"></i>
  5.   <i class="iconfont icon-zuoduiqi  data-name="align" data-value="left"></i>
  6.   <i class="iconfont icon-juzhongduiqi  data-name="align" data-value="center"></i>
  7.   <i class="iconfont icon-youduiqi data-name="align" data-value="right"></i>
  8. </view>

Page 函数中的 format 函数:

  1. format(e) {
  2.     let {
  3.         name,
  4.         value
  5.     } = e.target.dataset
  6.     if (!name) return
  7.     this.editorCtx.format(name, value)
  8. },

问题:当我们点击图标时,改变了文本样式,但是图标的样式没有改变,无法提示我们文本现在的样式状态,那该怎么解决呢?

这时候我们就需要动态改变字体图标的样式了,比如点击图标后,改变颜色。


通过查阅 editor 微信小程序开发相关文档后,bindstatuschange 属性绑定的方法,会在当你通过 Context 方法改变编辑器内样式时触发,会返回选区已设置的样式。

那么我们可以在 data 中,添加 formats 对象,存储点击后的样式属性。然后在点击图标按钮时,通过 bindstatuschange 绑定的方法,得到已设置的样式存储到 formats 中;在模板渲染时,在<i> 的 class 属性上,添加 {{formats.align === 'right' ? 'ql-active' : ''}}(如文本向右),当你点击了这个图标,那么 formats 中就有这个属性了,那么就添加我们的动态类名 ql-active 改变图标颜色。


具体实现

对 editor 标签属性 bindstatuschange 绑定方法 onStatusChange

  1. <editor id="editor"
  2. class="ql-container"
  3. placeholder="{{placeholder}}" 
  4. showImgSize showImgToolbar showImgResize  
  5. bindstatuschange="onStatusChange"
  6. read-only="{{readOnly}}"
  7. bindready="onEditorReady">
  1. onStatusChange(e) {
  2.     const formats = e.detail
  3.     this.setData({
  4.         formats
  5.     })
  6. }

在图标 <i> 标签上,添加{{formats.align === 'right' ? 'ql-active' : ''}}

  1. <i class="iconfont icon-zitijiacu {{formats.bold ? 'ql-active' : ''}}" data-name="bold"></i>
  2. <i class="iconfont icon-zitixieti {{formats.italic ? 'ql-active' : ''}}" data-name="italic"></i>
  3. <i class="iconfont icon-zitixiahuaxian {{formats.underline ? 'ql-active' : ''}}" data-name="underline"></i>
  4. <i class="iconfont icon-zuoduiqi {{formats.align === 'left' ? 'ql-active' : ''}}" data-name="align" data-value="left"></i>
  5. <i class="iconfont icon-juzhongduiqi {{formats.align === 'center' ? 'ql-active' : ''}}" data-name="align" data-value="center"></i>
  6. <i class="iconfont icon-youduiqi {{formats.align === 'right' ? 'ql-active' : ''}}" data-name="align" data-value="right"></i>

在 article.wxss 添加 ql-active 类

  1. .ql-active {
  2.   color: #497749;
  3. }


3.2. 实现撤销、恢复、插入图片、删除操作


微信小程序实战项目之富文本编辑器实现


首先在 <i> 标签上绑定相应的事件:

  1. <i class="iconfont icon-undo" bindtap="undo"></i>
  2. <i class="iconfont icon-redo" bindtap="redo"></i> 
  3. <i class="iconfont icon-charutupian" bindtap="insertImage"></i>
  4. <i class="iconfont icon-shanchu" bindtap="clear"></i>

撤销 undo

调用 EditorContext API 即可

  1. undo() {
  2.     this.editorCtx.undo()
  3. }

恢复 redo

同理

  1. redo() {
  2.     this.editorCtx.redo()
  3. }

插入图片 insertImage

同理

  1. insertImage() {
  2.     const that = this
  3.     wx.chooseImage({
  4.         count: 1,
  5.         success: function (res) {
  6.             wx.showLoading({
  7.                 title: '正在上传图片',
  8.             })
  9.             wx.cloud.uploadFile({
  10.                 cloudPath: `news/upload/${time.formatTime(new Date)}/${Math.floor(Math.random() * 100000000)}.png`, // 上传至云端的路径
  11.                 filePath: res.tempFilePaths[0], 
  12.                 success: cover => {
  13.                     that.editorCtx.insertImage({
  14.                         src: cover.fileID,
  15.                         data: {
  16.                             id: cover.fileID,
  17.                             role: 'god'
  18.                         },
  19.                         success: function () {
  20.                             wx.hideLoading()
  21.                         }
  22.                     })
  23.                 }
  24.             })
  25.         }
  26.     })
  27. }

清空 clear

同理

  1. clear() {
  2.     this.editorCtx.clear({
  3.         success: function (res) {
  4.             console.log("clear success")
  5.         }
  6.     })
  7. }


本文网址:https://www.zztuku.com/detail-13167.html
站长图库 - 微信小程序实战项目之富文本编辑器实现
申明:本文转载于《脚本之家》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐

    Photoshop绘制非常细腻的红苹果