index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <view class="container">
  3. <view class="page-body">
  4. <view class='wrapper'>
  5. <view class='toolbar' @tap="format">
  6. <view v-if="icons.zitijiacu" :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold"></view>
  7. <view v-if="icons.zitixieti" :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic"></view>
  8. <view v-if="icons.zitixiahuaxian" :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian" data-name="underline"></view>
  9. <view v-if="icons.zitishanchuxian" :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian" data-name="strike"></view>
  10. <view v-if="icons.juzhongduiqi" :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi" data-name="align" data-value="center"></view>
  11. <view v-if="icons.youduiqi" :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi" data-name="align" data-value="right"></view>
  12. <view v-if="icons.zuoyouduiqi" :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi" data-name="align" data-value="justify"></view>
  13. <view v-if="icons.clearedformat" class="iconfont icon-clearedformat" @tap="removeFormat"></view>
  14. <view v-if="icons.text_color" :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color" data-name="color" data-value="#0000ff"></view>
  15. <view v-if="icons.fontbgcolor" :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''" class="iconfont icon-fontbgcolor" data-name="backgroundColor" data-value="#00ff00"></view>
  16. <view v-if="icons.date" class="iconfont icon-date" @tap="insertDate"></view>
  17. <view v-if="icons.checklist" class="iconfont icon--checklist" data-name="list" data-value="check"></view>
  18. <view v-if="icons.youxupailie" :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie" data-name="list" data-value="ordered"></view>
  19. <view v-if="icons.wuxupailie" :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie" data-name="list" data-value="bullet"></view>
  20. <view v-if="icons.undo" class="iconfont icon-undo" @tap="undo"></view>
  21. <view v-if="icons.redo" class="iconfont icon-redo" @tap="redo"></view>
  22. <view v-if="icons.outdent" class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
  23. <view v-if="icons.indent" class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
  24. <view v-if="icons.fengexian" class="iconfont icon-fengexian" @tap="insertDivider"></view>
  25. <view v-if="icons.charutupian" class="iconfont icon-charutupian" @tap="insertImage"></view>
  26. <view v-if="icons.format_header" :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1" data-name="header" :data-value="1"></view>
  27. <view v-if="icons.zitixiabiao" :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao" data-name="script" data-value="sub"></view>
  28. <view v-if="icons.zitishangbiao" :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao" data-name="script" data-value="super"></view>
  29. <view v-if="icons.shanchu" class="iconfont icon-shanchu" @tap="clear"></view>
  30. <view v-if="icons.direction" :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl" data-name="direction" data-value="rtl"></view>
  31. </view>
  32. <view class="editor-wrapper">
  33. <editor id="editor" class="ql-container" placeholder="开始输入..."
  34. show-img-size show-img-toolbar show-img-resize
  35. @statuschange="onStatusChange" @ready="onEditorReady"
  36. @input="onInputChange"
  37. @blur="handleBlur">
  38. </editor>
  39. </view>
  40. </view>
  41. </view>
  42. <view v-if="maxLength > 0" class="word-count">
  43. <text>{{ currentWordCount }}</text>
  44. <text> / </text>
  45. <text>{{ maxLength }}</text>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. /*
  51. @blur 富文本失焦事件 会传出一个自定义事件 携带富文本
  52. richValue 富文本回显数据
  53. */
  54. export default {
  55. props:{
  56. maxLength: {
  57. type: Number,
  58. default: 0
  59. },
  60. richValue: {
  61. type: String,
  62. default: ''
  63. }
  64. },
  65. data() {
  66. return {
  67. icons: {
  68. zitijiacu: true,
  69. zitixieti: true,
  70. zitixiahuaxian: true,
  71. zitishanchuxian: false,
  72. juzhongduiqi: true,
  73. youduiqi: true,
  74. zuoyouduiqi: true,
  75. clearedformat: true,
  76. text_color: false,
  77. fontbgcolor: false,
  78. date: false,
  79. checklist: false,
  80. youxupailie: true,
  81. wuxupailie: true,
  82. undo: true,
  83. redo: true,
  84. outdent: true,
  85. indent: true,
  86. fengexian: true,
  87. charutupian: false,
  88. format_header: false,
  89. zitixiabiao: false,
  90. zitishangbiao: false,
  91. shanchu: true,
  92. direction: false,
  93. },
  94. prevHtml: null,
  95. currentWordCount: 0,
  96. readOnly: false,
  97. formats: {}
  98. }
  99. },
  100. methods: {
  101. handleBlur() {
  102. this.editorCtx.getContents({
  103. success: (res) => {
  104. this.$emit('blur',res.html)
  105. }
  106. })
  107. },
  108. onInputChange(e) {
  109. const text = e?.detail?.text || null
  110. const html = e?.detail?.html || null
  111. if (!text || !html) return
  112. if (this.maxLength && (text.length >= this.maxLength)) {
  113. uni.showToast({ icon: 'none', title: `最多可输入${this.maxLength}个字符` })
  114. setEditorContent(this.prevHtml)
  115. return
  116. }
  117. this.currentWordCount = text.length || 0
  118. this.prevHtml = html
  119. },
  120. setEditorContent(content) {
  121. this.editorCtx.setContents({
  122. html: content
  123. });
  124. },
  125. onEditorReady() {
  126. this.createSelectorQuery().select('#editor').context((res) => {
  127. this.editorCtx = res.context
  128. this.setEditorContent(this.richValue)
  129. }).exec()
  130. // #endif
  131. },
  132. undo() {
  133. this.editorCtx.undo()
  134. },
  135. redo() {
  136. this.editorCtx.redo()
  137. },
  138. format(e) {
  139. let {name,value} = e.target.dataset
  140. if (!name) return
  141. this.editorCtx.format(name, value)
  142. },
  143. onStatusChange(e) {
  144. const formats = e.detail
  145. this.formats = formats
  146. },
  147. insertDivider() {
  148. this.editorCtx.insertDivider({
  149. success: function() {
  150. console.log('insert divider success')
  151. }
  152. })
  153. },
  154. clear() {
  155. uni.showModal({
  156. title: '清空编辑器',
  157. content: '确定清空全部内容?',
  158. success: res => {
  159. if (res.confirm) {
  160. this.editorCtx.clear({
  161. success: function(res) {
  162. console.log("clear success")
  163. }
  164. })
  165. }
  166. }
  167. })
  168. },
  169. removeFormat() {
  170. this.editorCtx.removeFormat()
  171. },
  172. insertDate() {
  173. const date = new Date()
  174. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  175. this.editorCtx.insertText({
  176. text: formatDate
  177. })
  178. },
  179. insertImage() {
  180. // 上传图片方法 这里需要调用上传图片接口,拿到图片url并插入
  181. this.$utils.uploadImage(1, (res) => {
  182. this.editorCtx.insertImage({
  183. src: res,
  184. alt: '图像',
  185. success: (res) => {
  186. console.log('insert image success')
  187. }
  188. })
  189. });
  190. }
  191. }
  192. }
  193. </script>
  194. <style>
  195. @import "./editor-icon.css";
  196. .container {
  197. width: 100%;
  198. border: solid 1px #eee;
  199. }
  200. #editor {
  201. width: 100%;
  202. height: 300px;
  203. border: solid 1px #eee;
  204. word-break: break-all;
  205. }
  206. .wrapper {
  207. padding: 5px;
  208. }
  209. .iconfont {
  210. display: inline-block;
  211. padding: 8px 8px;
  212. /* width: 24px;
  213. height: 24px; */
  214. cursor: pointer;
  215. font-size: 16px;
  216. }
  217. .toolbar {
  218. box-sizing: border-box;
  219. border-bottom: 0;
  220. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  221. }
  222. .ql-container {
  223. box-sizing: border-box;
  224. padding: 10px;
  225. width: 100%;
  226. min-height: 30vh;
  227. height: auto;
  228. background: #fff;
  229. /* margin-top: 10px; */
  230. font-size: 14px;
  231. line-height: 1.5;
  232. }
  233. .ql-active {
  234. color: #06c;
  235. }
  236. .word-count {
  237. margin-top: 10px;
  238. font-size: 12px;
  239. color: #999;
  240. text-align: right;
  241. margin: 4px;
  242. }
  243. </style>