Editor.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <div style="border: 1px solid #ccc;">
  3. <Toolbar
  4. style="border-bottom: 1px solid #ccc"
  5. :editor="editor"
  6. :defaultConfig="toolbarConfig"
  7. :mode="mode"
  8. />
  9. <Editor
  10. :style="[{'height': height?height:'500px'},{'overflow-y': 'hidden'}]"
  11. v-model="html"
  12. :defaultConfig="editorConfig"
  13. :mode="mode"
  14. @onCreated="onCreated"
  15. />
  16. </div>
  17. </template>
  18. <script>
  19. import Vue from 'vue'
  20. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  21. export default Vue.extend({
  22. components: { Editor, Toolbar },
  23. data() {
  24. return {
  25. editor: null,
  26. html: '',
  27. toolbarConfig: {
  28. isFullScreen:false
  29. },
  30. editorConfig: {
  31. placeholder: this.$t('请输入内容...'),
  32. MENU_CONF:{
  33. uploadImage: {
  34. customUpload:this.update
  35. },
  36. uploadVideo: {
  37. customUpload:this.update
  38. }
  39. }
  40. },
  41. mode: 'default', // or 'simple'
  42. }
  43. },
  44. props: ['id','content','disabled','height'],
  45. watch: {
  46. disabled (val) {
  47. val ? this.editor.disable() : this.editor.enable()
  48. }
  49. },
  50. methods: {
  51. onCreated(editor) {
  52. this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
  53. },
  54. //自定义上传
  55. update(file,insertFn) {
  56. let filename = file.name.substr(0,file.name.indexOf('.'))
  57. let ext = file.name.substr(file.name.indexOf('.') + 1)
  58. //资源上传请求配置
  59. let uploadRequest = {
  60. "classname": "system.attachment.huawei.OBS",
  61. "method": "getFileName",
  62. "content": {
  63. "filename": filename,
  64. "filetype": ext,
  65. "parentid": JSON.parse(sessionStorage.getItem('folderid')).appfolderid
  66. }
  67. }
  68. //申请上传地址
  69. this.$api.requested(uploadRequest).then( res => {
  70. let url = res.data.uploadurl
  71. let obsfilename = res.data.serialfilename
  72. let config = {
  73. headers:{'Content-Type':'application/octet-stream'}
  74. }
  75. //上传华为云
  76. this.$upload.hw_upload(url, file, config).then(res => {
  77. if(res.statusText == 'OK') {
  78. let param = {
  79. "classname": "system.attachment.huawei.OBS",
  80. "method": "uploadSuccess",
  81. "content": {
  82. "serialfilename": obsfilename
  83. }
  84. }
  85. //上传成功反馈
  86. this.$api.requested(param).then( res => {
  87. let result = res.data.attachmentids
  88. let param = {
  89. "classname": "system.attachment.Attachment",
  90. "method": "createFileLink",
  91. "content": {
  92. "ownertable": "SAT_SHAREMATERIAL",
  93. "ownerid": this.id,
  94. "usetype": "richtext",
  95. "attachmentids": result
  96. }
  97. }
  98. this.$api.requested(param).then(res => {
  99. if (res.msg != '成功') {
  100. this.$notify({
  101. title: this.$t('提示'),
  102. message: this.$t('上传失败'),
  103. type: 'error'
  104. })
  105. } else {
  106. //插入富文本
  107. insertFn(res.data[0].url,filename,res.data[0].url)
  108. }
  109. })
  110. })
  111. }
  112. })
  113. })
  114. }
  115. },
  116. mounted() {
  117. // 模拟 ajax 请求,异步渲染编辑器
  118. setTimeout(() => {
  119. this.html = this.content
  120. this.disabled ? this.editor.disable() : this.editor.enable()
  121. }, 1000)
  122. },
  123. beforeDestroy() {
  124. const editor = this.editor
  125. if (editor == null) return
  126. editor.destroy() // 组件销毁时,及时销毁编辑器
  127. }
  128. })
  129. </script>
  130. <style src="@wangeditor/editor/dist/css/style.css"></style>