WangEditor.vue 3.2 KB

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