WangEditor.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div>
  3. <div id="editor" ref="myEditor"></div>
  4. <slot></slot>
  5. </div>
  6. </template>
  7. <script>
  8. import WangEditor from 'wangeditor'
  9. export default {
  10. name: 'ComponentWangeditor',
  11. data () {
  12. return {
  13. edit: ''
  14. }
  15. },
  16. props: {
  17. value: {
  18. type: String,
  19. default: ''
  20. },
  21. config: {
  22. type: Array,
  23. default: () => {
  24. return {}
  25. }
  26. },
  27. uploadConfig: {
  28. type: Array,
  29. default: () => {
  30. return {
  31. method: 'http', // 支持custom(objurl)和http(服务器)和base64
  32. url: '/'
  33. }
  34. }
  35. }
  36. },
  37. computed: {
  38. customConfig () {
  39. return {
  40. pasteFilterStyle: false, // 关闭掉粘贴样式的过滤
  41. pasteIgnoreImg: false, // 粘贴时不忽略图片
  42. ...this.config
  43. }
  44. }
  45. },
  46. watch: {
  47. },
  48. components: {
  49. },
  50. methods: {
  51. readBlobAsDataURL (blob, callback) {
  52. var a = new FileReader()
  53. a.onload = function (e) { callback(e.target.result) }
  54. a.readAsDataURL(blob)
  55. },
  56. initEditor () {
  57. var self = this
  58. this.editor = new WangEditor(this.$refs.myEditor)
  59. // 配置 onchange 事件
  60. this.editor.customConfig = this.customConfig
  61. this.editor.customConfig.uploadImgMaxLength = 5
  62. this.editor.change = function () { // 编辑区域内容变化时
  63. self.$emit('input', this.txt.html())
  64. self.$emit('onchange', this.txt.html(), this.txt)
  65. // editor.txt.html('.....') //设置编辑器内容
  66. // editor.txt.clear() //清空编辑器内容
  67. // editor.txt.append('<p>追加的内容</p>')//继续追加内容。
  68. // editor.txt.text() // 读取 text
  69. // editor.txt.getJSON() // 获取 JSON 格式的内容
  70. }
  71. this.editor.customConfig.customUploadImg = function (files, insert) {
  72. if (self.uploadConfig.method === 'custom') {
  73. files.forEach(file => {
  74. var fileUrl = URL.createObjectURL(file)
  75. insert(fileUrl)
  76. })
  77. }
  78. if (self.uploadConfig.method === 'base64') {
  79. files.forEach(file => {
  80. self.readBlobAsDataURL(file, function (dataurl) {
  81. insert(dataurl)
  82. })
  83. })
  84. }
  85. if (self.uploadConfig.method === 'http') {
  86. if (self.uploadConfig.callback) {
  87. self.uploadConfig.callback(files, insert)
  88. } else {
  89. var formData = new FormData()
  90. files.forEach(file => {
  91. formData.append('file', file)
  92. })
  93. self.$axios.post(self.uploadConfig.url, formData).then(({ data }) => {
  94. if (data.status === 'success') {
  95. insert(data.url)
  96. }
  97. })
  98. }
  99. }
  100. }
  101. this.editor.create() // 生成编辑器
  102. this.editor.txt.text(this.value) // 生成编辑器
  103. this.$emit('oninit', this.editor)
  104. }
  105. },
  106. beforeCreate () {
  107. },
  108. created () {
  109. },
  110. beforeMount () {
  111. },
  112. mounted () {
  113. this.initEditor()
  114. }
  115. }
  116. </script>
  117. <style >
  118. .w-e-toolbar{
  119. flex-wrap:wrap;
  120. }
  121. </style>