editoritem.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template lang="html">
  2. <div class="editor">
  3. <div ref="toolbar" class="toolbar">
  4. </div>
  5. <div ref="editor" class="text">
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import E from 'wangeditor'
  11. import { createNamespacedHelpers } from 'vuex'
  12. const { mapActions } = createNamespacedHelpers('files')
  13. const token = sessionStorage.getItem('token')
  14. export default {
  15. name: 'editoritem',
  16. data () {
  17. return {
  18. // uploadPath,
  19. editor: null,
  20. info_: null
  21. }
  22. },
  23. model: {
  24. prop: 'value',
  25. event: 'change'
  26. },
  27. props: {
  28. value: {
  29. type: String,
  30. default: ''
  31. },
  32. isClear: {
  33. type: Boolean,
  34. default: false
  35. }
  36. },
  37. watch: {
  38. isClear (val) {
  39. // 触发清除文本域内容
  40. if (val) {
  41. this.editor.txt.clear()
  42. this.info_ = null
  43. }
  44. },
  45. value: function (value) {
  46. if (value !== this.editor.txt.html()) {
  47. this.editor.txt.html(this.value)
  48. }
  49. }
  50. // value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  51. },
  52. mounted () {
  53. this.seteditor()
  54. this.editor.txt.html(this.value)
  55. },
  56. methods: {
  57. ...mapActions(['filesupload']),
  58. seteditor () {
  59. const _this = this
  60. // http://192.168.2.125:8080/admin/storage/create
  61. this.editor = new E(this.$refs.toolbar, this.$refs.editor)
  62. this.editor.config.uploadImgParams = { type: 'resource' }
  63. this.editor.config.uploadImgShowBase64 = false // base 64 存储图片
  64. this.editor.config.uploadImgServer = '/api/files/upload'// 配置服务器端地址
  65. this.editor.config.uploadImgHeaders = { Authorization: `Bearer ${token}`, a: 100 }// 自定义 header
  66. this.editor.config.uploadFileName = 'file' // 后端接受上传文件的参数名
  67. this.editor.config.uploadImgMaxSize = 2 * 1024 * 1024 // 将图片大小限制为 2M
  68. this.editor.config.uploadImgMaxLength = 6 // 限制一次最多上传 3 张图片
  69. this.editor.config.uploadImgTimeout = 3 * 60 * 1000 // 设置超时时间
  70. this.editor.config.showLinkImg = false
  71. // 配置菜单
  72. this.editor.config.menus = [
  73. 'head', // 标题
  74. 'bold', // 粗体
  75. 'fontSize', // 字号
  76. 'fontName', // 字体
  77. 'italic', // 斜体
  78. 'underline', // 下划线
  79. 'strikeThrough', // 删除线
  80. 'foreColor', // 文字颜色
  81. 'backColor', // 背景颜色
  82. 'link', // 插入链接
  83. 'list', // 列表
  84. 'justify', // 对齐方式
  85. 'quote', // 引用
  86. 'emoticon', // 表情
  87. 'image', // 插入图片
  88. 'table', // 表格
  89. 'video', // 插入视频
  90. 'code', // 插入代码
  91. 'undo', // 撤销
  92. 'redo', // 重复
  93. 'fullscreen' // 全屏
  94. ]
  95. this.editor.config.customUploadImg = async function (resultFiles, insertImgFn) {
  96. // resultFiles 是 input 中选中的文件列表
  97. var data = new FormData()
  98. data.append('type', 'resource')
  99. data.append('file', resultFiles[0])
  100. const res = await _this.filesupload(data)
  101. // insertImgFn 是获取图片 url 后,插入到编辑器的方法
  102. const url = res.data.path
  103. // 上传图片,返回结果,将图片插入到编辑器中
  104. insertImgFn(url)
  105. }
  106. this.editor.config.uploadImgHooks = {
  107. fail: (xhr, editor, result) => {
  108. // 插入图片失败回调
  109. console.log('插入图片失败回调')
  110. console.log(xhr, editor, result)
  111. },
  112. success: (xhr, editor, result) => {
  113. // 图片上传成功回调
  114. console.log('图片上传成功回调')
  115. console.log(xhr, editor, result)
  116. },
  117. timeout: (xhr, editor) => {
  118. // 网络超时的回调
  119. },
  120. error: (xhr, editor) => {
  121. // 图片上传错误的回调\
  122. _this.$message.error('上传失败')
  123. },
  124. customInsert: (insertImg, result, editor) => {
  125. console.log('图片上传成功,插入图片的回调')
  126. console.log(result, editor)
  127. // 图片上传成功,插入图片的回调
  128. // result为上传图片成功的时候返回的数据,这里我打印了一下发现后台返回的是data:[{url:"路径的形式"},...]
  129. // console.log(result.data[0].url)
  130. // insertImg()为插入图片的函数
  131. // 循环插入图片
  132. // for (let i = 0; i < 1; i++) {
  133. // console.log(result)
  134. const url = result.data.path
  135. insertImg(url)
  136. // }
  137. }
  138. }
  139. this.editor.config.onchange = (html) => {
  140. this.info_ = html // 绑定当前逐渐地值
  141. this.$emit('change', this.info_) // 将内容同步到父组件中
  142. }
  143. // 创建富文本编辑器
  144. this.editor.create()
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="css">
  150. .editor {
  151. width: 100%;
  152. margin: 0 auto;
  153. position: relative;
  154. z-index: 0;
  155. }
  156. .toolbar {
  157. border: 1px solid #ccc;
  158. }
  159. .text {
  160. border: 1px solid #ccc;
  161. min-height: 300px;
  162. }
  163. </style>