当前位置:   article > 正文

微信小程序使用editor富文本编辑器 以及回显 全屏弹窗的模式_微信小程序 editor 回显

微信小程序 editor 回显

 

  1. <!--富文本接收的位置-->
  2. <view class="white-box">
  3. <view class="title">
  4. <view class="yellow-fence"></view>
  5. <view class="v1">教研记录</view>
  6. </view>
  7. <view class="add-btn" bindtap="addRecords">添加新的教研记录</view>
  8. <view wx:for="{{records}}" class="rich-text-box" wx:key="index">
  9. <!-- <mp-html content="{{item}}" selectable="true" show-img-menu="true" /> -->
  10. <rich-text nodes="{{item}}" data-index="{{index}}" bindtap="echoRecordsData"></rich-text>
  11. </view>
  12. </view>
  13. <view class="white-box">
  14. <view class="title">
  15. <view class="yellow-fence"></view>
  16. <view class="v1">研究成果</view>
  17. </view>
  18. <view class="add-btn" bindtap="addAchievement">添加研究成果说明</view>
  19. <view wx:for="{{achievement}}" class="rich-text-box" wx:key="index">
  20. <!-- <mp-html content="{{item}}" selectable="true" show-img-menu="true" /> -->
  21. <rich-text nodes="{{item}}" data-index="{{index}}" bindtap="echoAchievementData"></rich-text>
  22. </view>
  23. </view>

以上是富文本的值返回接收的地方 下面是富文本的html

  1. <!--富文本编辑器弹窗-->
  2. <view class="container" style="height:100vh;" wx:if="{{showEidter}}">
  3. <view class="save-box">
  4. <view class="v1" bindtap="cancelEidter">取消</view>
  5. <view class="v2" bindtap="saveEditer">确定</view>
  6. </view>
  7. <editor id="editor" class="ql-container" placeholder="{{placeholder}}" bindstatuschange="onStatusChange" bindready="onEditorReady" bindfocus="onFocus" bindinput="onBlur">
  8. </editor>
  9. <view class="toolbar" catchtouchend="format">
  10. <i class="iconfont icon-charutupian" catchtouchend="insertImage"></i>
  11. <i class="iconfont icon-format-header-2 {{formats.header === 2 ? 'ql-active' : ''}}" data-name="header" data-value="{{2}}"></i>
  12. <i class="iconfont icon-format-header-3 {{formats.header === 3 ? 'ql-active' : ''}}" data-name="header" data-value="{{3}}"></i>
  13. <i class="iconfont icon-zitijiacu {{formats.bold ? 'ql-active' : ''}}" data-name="bold"></i>
  14. <i class="iconfont icon-zitixieti {{formats.italic ? 'ql-active' : ''}}" data-name="italic"></i>
  15. <i class="iconfont icon-zitixiahuaxian {{formats.underline ? 'ql-active' : ''}}" data-name="underline"></i>
  16. <i class="iconfont icon--checklist" data-name="list" data-value="check"></i>
  17. <i class="iconfont icon-youxupailie {{formats.list === 'ordered' ? 'ql-active' : ''}}" data-name="list" data-value="ordered"></i>
  18. <i class="iconfont icon-wuxupailie {{formats.list === 'bullet' ? 'ql-active' : ''}}" data-name="list" data-value="bullet"></i>
  19. </view>
  20. </view>
  1. Page({
  2. data: {
  3. /*富文本-start*/
  4. formats: {},
  5. readOnly: false,
  6. placeholder: '开始输入...',
  7. isClickImg: false,
  8. records: [], //教研记录列表
  9. isRecordsEidt: false, //是否编辑的是教研记录
  10. recordsEidtIndex: null, //教研记录编辑的是哪一条
  11. achievement: [], //研究成果列表
  12. isAchievementEidt: false, //是否编辑的是研究成果
  13. achievementEidtIndex: null, //研究成果编辑的是哪一条
  14. showEidter: false,
  15. editData: '',
  16. /*富文本-end*/
  17. },
  18. onLoad(options) {},
  19. /*富文本编辑器-start*/
  20. //编辑教研记录列表
  21. echoRecordsData(e) {
  22. const that = this
  23. let oidx = e.currentTarget.dataset.index
  24. this.setData({
  25. isRecordsEidt: true,
  26. showEidter: true,
  27. recordsEidtIndex: oidx,
  28. isAchievementEidt: false, //是否编辑的是研究成果
  29. achievementEidtIndex: null, //研究成果编辑的是哪一条
  30. })
  31. //console.log(that.data.records[oidx])
  32. // 修改时,反显数据
  33. this.createSelectorQuery().select("#editor")
  34. .context((res) => {
  35. res.context.setContents({
  36. html: that.data.records[oidx],
  37. });
  38. })
  39. .exec();
  40. },
  41. echoAchievementData(e) {
  42. const that = this
  43. let oidx = e.currentTarget.dataset.index
  44. this.setData({
  45. isAchievementEidt: true,
  46. showEidter: true,
  47. achievementEidtIndex: oidx,
  48. isRecordsEidt: false, //是否编辑的是教研记录
  49. recordsEidtIndex: null, //教研记录编辑的是哪一条
  50. })
  51. //console.log(that.data.records[oidx])
  52. // 修改时,反显数据
  53. this.createSelectorQuery().select("#editor")
  54. .context((res) => {
  55. res.context.setContents({
  56. html: that.data.achievement[oidx],
  57. });
  58. })
  59. .exec();
  60. },
  61. //重置
  62. cancelEidter() {
  63. this.setData({
  64. isRecordsEidt: false,
  65. showEidter: false,
  66. recordsEidtIndex: null,
  67. isAchievementEidt: false,
  68. achievementEidtIndex: null,
  69. editData: ''
  70. })
  71. },
  72. saveEditer() {
  73. //如果是教研记录新增
  74. let recordsEidtIndex = this.data.recordsEidtIndex
  75. let achievementEidtIndex = this.data.achievementEidtIndex
  76. if (this.data.isRecordsEidt) {
  77. let odata = this.data.records
  78. if (recordsEidtIndex || recordsEidtIndex === 0) {
  79. //编辑
  80. odata[recordsEidtIndex] = this.data.editData
  81. } else {
  82. //新增
  83. odata.push(this.data.editData)
  84. }
  85. this.setData({
  86. records: odata
  87. })
  88. //console.log(this.data.records)
  89. this.cancelEidter()
  90. } else if (this.data.isAchievementEidt || achievementEidtIndex === 0) {
  91. let odata = this.data.achievement
  92. if (achievementEidtIndex) {
  93. //编辑
  94. odata[achievementEidtIndex] = this.data.editData
  95. } else {
  96. //新增
  97. odata.push(this.data.editData)
  98. }
  99. this.setData({
  100. achievement: odata
  101. })
  102. //console.log(this.data.records)
  103. this.cancelEidter()
  104. }
  105. },
  106. addRecords() {
  107. this.setData({
  108. isRecordsEidt: true,
  109. showEidter: true
  110. })
  111. },
  112. addAchievement() {
  113. this.setData({
  114. isAchievementEidt: true,
  115. showEidter: true
  116. })
  117. },
  118. readOnlyChange() {
  119. this.setData({
  120. readOnly: !this.data.readOnly
  121. })
  122. },
  123. onEditorReady() {
  124. const that = this
  125. wx.createSelectorQuery().select('#editor').context(function (res) {
  126. //console.log(res)
  127. if (res.context) {
  128. that.editorCtx = res.context
  129. }
  130. }).exec()
  131. },
  132. format(e) {
  133. let {
  134. name,
  135. value
  136. } = e.target.dataset
  137. if (!name) return
  138. // console.log('format', name, value)
  139. this.editorCtx.format(name, value)
  140. },
  141. onStatusChange(e) {
  142. const formats = e.detail
  143. this.setData({
  144. formats
  145. })
  146. },
  147. insertDivider() {
  148. this.editorCtx.insertDivider({
  149. success: function () {
  150. console.log('insert divider success')
  151. }
  152. })
  153. },
  154. clear() {
  155. this.editorCtx.clear({
  156. success: function (res) {
  157. console.log("clear success")
  158. }
  159. })
  160. },
  161. removeFormat() {
  162. this.editorCtx.removeFormat()
  163. },
  164. insertDate() {
  165. const date = new Date()
  166. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  167. this.editorCtx.insertText({
  168. text: formatDate
  169. })
  170. },
  171. insertImage() {
  172. this.setData({
  173. isClickImg: true
  174. })
  175. const that = this
  176. wx.chooseImage({
  177. count: 1,
  178. success: function (res) {
  179. that.editorCtx.insertImage({
  180. src: res.tempFilePaths[0],
  181. data: {
  182. id: 'abcd',
  183. role: 'god'
  184. },
  185. width: '80%',
  186. success: function () {
  187. console.log('insert image success')
  188. }
  189. })
  190. }
  191. })
  192. },
  193. onFocus() {
  194. this.setData({
  195. isClickImg: false
  196. })
  197. },
  198. onBlur(e) {
  199. //console.log(this.data.isClickImg)
  200. //防止上传的图片直接失焦 保存内容的问题
  201. if (!this.data.isClickImg) {
  202. //console.log(e.detail.html)
  203. this.setData({
  204. editData: e.detail.html
  205. })
  206. }
  207. //this.oneEditor.blur()
  208. },
  209. /*富文本编辑器-end*/
  210. })
  1. /*富文本编辑器-start*/
  2. .container {
  3. position: fixed;
  4. top: 0;
  5. right: 0;
  6. bottom: 0;
  7. left: 0;
  8. z-index: 999;
  9. display: flex;
  10. flex-direction: column;
  11. background: #fff;
  12. }
  13. .ql-container {
  14. flex: 1;
  15. padding: 10px 10px 50px 10px;
  16. box-sizing: border-box;
  17. font-size: 16px;
  18. line-height: 1.5;
  19. }
  20. .ql-active {
  21. color: #22C704;
  22. }
  23. .iconfont {
  24. display: inline-block;
  25. width: 30px;
  26. height: 30px;
  27. cursor: pointer;
  28. font-size: 20px;
  29. }
  30. .toolbar {
  31. position: fixed;
  32. left: 0;
  33. right: 100%;
  34. bottom: 0;
  35. padding: 0 10px;
  36. display: flex;
  37. align-items: center;
  38. justify-content: space-between;
  39. width: 100%;
  40. height: 50px;
  41. box-sizing: border-box;
  42. background: #fff;
  43. border-top: 1rpx solid #f2f2f2;
  44. }
  45. .save-box{ margin: 10rpx 10rpx 0 0; display: flex; justify-content: flex-end;}
  46. .save-box .v1,.save-box .v2{ font-size: 28rpx; width: 100rpx; height: 60rpx; text-align: center; line-height: 60rpx; border-radius: 12rpx;}
  47. .save-box .v1{ margin-right: 10rpx; background: #ddd; color: #fff;}
  48. .save-box .v2{ background: #ffb81c; color: #fff;}
  49. .rich-text-box{ margin-bottom: 20rpx;}
  50. /*富文本编辑器-end*/

记录一下使用方法 也可以参考一下怎么使用

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读