index.vue 8.9 KB

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