index.vue 8.4 KB

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