index.vue 9.2 KB

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