index.vue 4.3 KB

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