index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. watch: {
  106. richValue: {
  107. handler(newVal) {
  108. this.setEditorContent(newVal || '')
  109. },
  110. immediate: true,
  111. deep: true
  112. }
  113. },
  114. methods: {
  115. // isExceedValidate(alert = false, isReturn = false, title = '字数超出限制') {
  116. // const bool = Boolean(this.max-0 && (this.currentWordCount-0 > this.max-0))
  117. // if (alert && bool) uni.showToast({ title, icon: 'none', duration: 2000 })
  118. // },
  119. handleBlur() {
  120. this.editorCtx.getContents({
  121. success: (res) => {
  122. this.$emit('blur', res.html === '<p><br></p>' ? '' : res.html)
  123. }
  124. })
  125. },
  126. onInputChange(e) {
  127. clearTimeout(this.inputTimer)
  128. this.inputTimer = setTimeout(() => {
  129. const text = e?.detail?.text ? e.detail.text : null
  130. const html = e?.detail?.html ? e.detail.html : null
  131. if (!text || !html) return
  132. this.currentWordCount = text.length || 0
  133. if (this.max-0 && (text.length >= this.max-0)) {
  134. uni.showToast({ icon: 'none', title: `最多可输入${this.max}个字符` })
  135. }
  136. // this.$emit('change', html)
  137. }, 500)
  138. },
  139. setEditorContent(content) {
  140. this.editorCtx.setContents({
  141. html: content
  142. });
  143. this.editorCtx.getContents({
  144. success: (res) => {
  145. this.currentWordCount = res?.text?.length || 0
  146. }
  147. })
  148. },
  149. onEditorReady() {
  150. // #ifdef APP-PLUS || MP-WEIXIN || H5
  151. this.createSelectorQuery().select('#editor').context((res) => {
  152. this.editorCtx = res.context
  153. this.setEditorContent(this.richValue)
  154. }).exec()
  155. // #endif
  156. },
  157. undo() {
  158. this.editorCtx.undo()
  159. },
  160. redo() {
  161. this.editorCtx.redo()
  162. },
  163. format(e) {
  164. let {name,value} = e.target.dataset
  165. if (!name) return
  166. this.editorCtx.format(name, value)
  167. },
  168. onStatusChange(e) {
  169. const formats = e.detail
  170. this.formats = formats
  171. },
  172. insertDivider() {
  173. this.editorCtx.insertDivider({
  174. success: function() {
  175. console.log('insert divider success')
  176. }
  177. })
  178. },
  179. clear() {
  180. uni.showModal({
  181. title: '清空编辑器',
  182. content: '确定清空全部内容?',
  183. success: res => {
  184. if (res.confirm) {
  185. this.editorCtx.clear({
  186. success: function(res) {
  187. console.log("clear success")
  188. }
  189. })
  190. }
  191. }
  192. })
  193. },
  194. removeFormat() {
  195. this.editorCtx.removeFormat()
  196. },
  197. insertDate() {
  198. const date = new Date()
  199. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
  200. this.editorCtx.insertText({
  201. text: formatDate
  202. })
  203. },
  204. insertImage() {
  205. // 上传图片方法 这里需要调用上传图片接口,拿到图片url并插入
  206. this.$utils.uploadImage(1, (res) => {
  207. this.editorCtx.insertImage({
  208. src: res,
  209. alt: '图像',
  210. success: (res) => {
  211. console.log('insert image success')
  212. }
  213. })
  214. });
  215. }
  216. }
  217. }
  218. </script>
  219. <style>
  220. @import "./editor-icon.css";
  221. .container {
  222. width: 100%;
  223. border: solid 1px #eee;
  224. }
  225. #editor {
  226. width: 100%;
  227. height: 300px;
  228. border: solid 1px #eee;
  229. word-break: break-all;
  230. }
  231. .wrapper {
  232. padding: 5px;
  233. }
  234. .iconfont {
  235. display: inline-block;
  236. padding: 8px 8px;
  237. /* width: 24px;
  238. height: 24px; */
  239. cursor: pointer;
  240. font-size: 16px;
  241. }
  242. .toolbar {
  243. box-sizing: border-box;
  244. border-bottom: 0;
  245. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  246. }
  247. .ql-container {
  248. box-sizing: border-box;
  249. padding: 10px;
  250. width: 100%;
  251. min-height: 30vh;
  252. height: auto;
  253. background: #fff;
  254. /* margin-top: 10px; */
  255. font-size: 14px;
  256. line-height: 1.5;
  257. }
  258. .ql-active {
  259. color: #06c;
  260. }
  261. .warn { color: #fe574a; }
  262. .normal { color: #999; }
  263. .word-count {
  264. margin-top: 10px;
  265. font-size: 12px;
  266. text-align: right;
  267. margin: 4px;
  268. }
  269. </style>