小程序使用editor组件富文本编辑

1.效果图
在这里插入图片描述

2.提交打印
在这里插入图片描述
3.上代码
文件目录
在这里插入图片描述
assets文件

链接: https://pan.baidu.com/s/1CVyQgbQAVIUR6ZcSktcVLg 提取码: 5ush 复制这段内容后打开百度网盘手机App,操作更方便哦

4.i标签要去找哦,目前只写了一部分

<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>
<view class="contaner" >
	<editor id="editor" class="ql-container" value='{{dataArry}}' placeholder="{{placeholder}}"
		bindstatuschange="onStatusChange" bindblur='goplay' bindready="onEditorReady">
	</editor>
</view>

<view bindtap="sublimt" class="submit">
	提交
</view>
Page({
  data: {
    formats: {},
    readOnly: false,
    placeholder: '开始输入...',
    dataArry: "<p>dadsa</p><p>dsad</p><img src='https://sucai.suoluomei.cn/sucai_zs/images/2020/11/03/2739b646610de30a4b60ad705b1df64c.jpg'><img src='https://sucai.suoluomei.cn/sucai_zs/images/2020/11/03/2739b646610de30a4b60ad705b1df64c.jpg'><img src='https://sucai.suoluomei.cn/sucai_zs/images/2020/11/03/2739b646610de30a4b60ad705b1df64c.jpg'>"
  },
  readOnlyChange() {
    this.setData({
      readOnly: !that.data.readOnly
    })
  },
  onLoad() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context;
      that.editorCtx.setContents({
        html: that.data.dataArry,
        success: function () {}
      })
    }).exec();
  },

  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
    this.editorCtx.format(name, value)

  },
  insertImage() {
    const that = this
    wx.chooseImage({
      count: 9,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        that.getpublish(res.tempFilePaths, 0)
      }
    })
  },
  // 递归循环下载图片
  getpublish(list, i) {
    wx.showLoading({
      title: '正在上传第' + (i + 1) + '张',
    })
    var that = this
    wx.uploadFile({
      url: "https://api110.herbplantist.com/sucai/public/index.php/post/post/uploadFile",
      filePath: list[i],
      name: 'file',
      formData: {
        key: 'Gn1xVdBiTClSSHKZifg0pTQSU5XGagWO',
        is_https: 1,
      },
      success(res) {
        var data = JSON.parse(res.data)
        console.log(data)
        if (data.status == 200) {
          that.editorCtx.insertImage({
            src: data.info.url,
            success: function () {}
          })
        }
        if (i + 1 == list.length) {
          wx.showToast({
            title: '上传成功',
          });
        }
        wx.hideLoading()
        if (++i < list.length) {
          that.getpublish(list, i);
        }
      },
    })
  },
  goplay(e) {
    this.setData({
      dataArry: e.detail.html
    })
  },
  sublimt() {
    let that = this
    wx.showModal({
      title: '温馨提示',
      content: '确定提交?',
      success(res) {
        if (res.confirm) {
          wx.showToast({
            title: '提交成功,打开调试看打印',
          })
          console.log(that.data.dataArry)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})
@import "./assets/iconfont.wxss";

.toolbar {
  width: 100%;
  display: flex;
  flex-flow: row wrap;
  background: #fff;
  border-bottom: 1rpx solid #ccc;
  padding: 20rpx;
  box-sizing: border-box;
  position: sticky;
  top: 0;
  z-index: 99;
}
.ql-active {
  color: #22C704;
}

.iconfont {
  display: flex;
  align-items: center;
  text-align: center;
  width:30px;
  height: 30rpx;
}

.contaner {
  width: 100%;
}
.ql-container {
  box-sizing: border-box;
  width: 100%;
  height:auto;
  font-size: 16px;
  line-height: 1.5;
  overflow: auto;
  padding: 10px 10px 20px 10px;
  border: 1px solid #ECECEC;
}
.submit {
  width: 100rpx;
  height: 100rpx;
  line-height: 100rpx;
  color: #fff;
  background-color: red;
  position: fixed;
  bottom: 74rpx;
  right: 35rpx;
  text-align: center;
  border-radius: 50%;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HqL丶1024

创作不易,谢谢打赏!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值