index.vue 9.1 KB

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