index.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div style="border: 1px solid #ccc; z-index: 999999">
  3. <Toolbar
  4. style="border-bottom: 1px solid #ccc"
  5. :editor="editor"
  6. :defaultConfig="toolbarConfig"
  7. :mode="mode"
  8. />
  9. <Editor
  10. style="height: 500px; overflow-y: hidden"
  11. v-model="currentValue"
  12. :defaultConfig="editorConfig"
  13. :mode="mode"
  14. @onCreated="onCreated"
  15. @onChange="onChange"
  16. @onDestroyed="onDestroyed"
  17. @customAlert="customAlert"
  18. />
  19. </div>
  20. </template>
  21. <script>
  22. import { getToken } from "@/utils/auth";
  23. import { Editor, Toolbar } from '@wangeditor/editor-for-vue'
  24. export default {
  25. components: { Editor, Toolbar },
  26. data() {
  27. return {
  28. editor: null,
  29. currentValue: '',
  30. toolbarConfig: {
  31. insertKeys:{
  32. index: 0,
  33. keys:['uploadAttachment']
  34. }
  35. },
  36. editorConfig: {
  37. hoverbarKeys: {
  38. attachment: {
  39. menuKeys: ['downloadAttachment'], // “下载附件”菜单
  40. },
  41. },
  42. placeholder: '请输入内容...',
  43. MENU_CONF:
  44. {
  45. uploadImage:{
  46. server: BASE_URL + "/v1/common/upload",
  47. fieldName: 'file',
  48. maxFileSize: this.fileSize * 1024 * 1024,
  49. headers:{
  50. Authorization: "Bearer " + getToken()
  51. },
  52. onBeforeUpload(file){
  53. return file
  54. },
  55. onProgress(progress){
  56. console.log('progress', progress)
  57. },
  58. onSuccess(file, res) {
  59. console.log(file.name + "上传成功")
  60. },
  61. onFailed(file, res) {
  62. console.log(file.name + "上传失败")
  63. },
  64. onError(file, err, res){
  65. console.log(file.name + "上传失败")
  66. },
  67. customInsert(res, insertFn) {
  68. insertFn(BASE_URL + res.url, res.fileName, '#')
  69. },
  70. },
  71. uploadVideo:{
  72. server: BASE_URL + "/v1/common/upload",
  73. fieldName: 'file',
  74. maxFileSize: this.fileSize * 1024 * 1024,
  75. headers:{
  76. Authorization: "Bearer " + getToken()
  77. },
  78. onBeforeUpload(file){
  79. return file
  80. },
  81. onProgress(progress){
  82. console.log('progress', progress)
  83. },
  84. onSuccess(file, res) {
  85. console.log(file.name + "上传成功")
  86. },
  87. onFailed(file, res) {
  88. console.log(file.name + "上传失败")
  89. },
  90. onError(file, err, res){
  91. console.log(file.name + "上传失败")
  92. },
  93. customInsert(res, insertFn) {
  94. insertFn(BASE_URL + res.url, res.fileName)
  95. },
  96. },
  97. uploadAttachment:{
  98. server: BASE_URL + "/v1/common/upload",
  99. fieldName: 'file',
  100. maxFileSize: this.fileSize * 1024 * 1024,
  101. headers:{
  102. Authorization: "Bearer " + getToken()
  103. },
  104. onBeforeUpload(file){
  105. return file
  106. },
  107. onProgress(progress){
  108. console.log('progress', progress)
  109. },
  110. onSuccess(file, res) {
  111. console.log(file.name + "上传成功")
  112. },
  113. onFailed(file, res) {
  114. console.log(file.name + "上传失败")
  115. },
  116. onError(file, err, res){
  117. console.log(file.name + "上传失败")
  118. },
  119. customInsert(res, file, insertFn) {
  120. insertFn(res.fileName, BASE_URL + res.url)
  121. },
  122. }
  123. }
  124. },
  125. mode: 'default', // or 'simple'
  126. }
  127. },
  128. // 接收父组件的方法
  129. props: {
  130. value: {
  131. type: String,
  132. default: ''
  133. },
  134. // 上传文件大小限制(MB)
  135. fileSize: {
  136. type: Number,
  137. default: 2048,
  138. },
  139. },
  140. watch: {
  141. value: {
  142. handler(val) {
  143. if (val !== this.currentValue) {
  144. this.currentValue = val === null ? "" : val;
  145. }
  146. },
  147. immediate: true,
  148. }
  149. },
  150. methods: {
  151. onCreated(editor) {
  152. this.editor = Object.seal(editor) // 一定要用 Object.seal() ,否则会报错
  153. },
  154. onChange(editor) {
  155. this.$emit("input", this.editor.getHtml());
  156. },
  157. onDestroyed(editor) {},
  158. customAlert(s, t){ //自定义弹框
  159. switch (t){
  160. case 'success':
  161. this.$message.success(s)
  162. break
  163. case 'info':
  164. this.$message.info(s)
  165. break
  166. case 'warning':
  167. this.$message.warning(s)
  168. break
  169. case 'error':
  170. this.$message.error(s)
  171. break
  172. default:
  173. this.$message.info(s)
  174. break
  175. }
  176. },
  177. },
  178. mounted() {
  179. },
  180. beforeDestroy() {
  181. const editor = this.editor
  182. if (editor == null) return
  183. editor.destroy() // 组件销毁时,及时销毁编辑器
  184. }
  185. }
  186. </script>
  187. <style src="@wangeditor/editor/dist/css/style.css"></style>