wang-editor.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <div ref="editor" style="text-align:left"></div>
  3. </template>
  4. <script>
  5. import E from 'wangeditor';
  6. const menus = [
  7. 'head', // 标题
  8. 'bold', // 粗体
  9. 'fontSize', // 字号
  10. 'fontName', // 字体
  11. 'italic', // 斜体
  12. 'underline', // 下划线
  13. 'strikeThrough', // 删除线
  14. 'foreColor', // 文字颜色
  15. 'backColor', // 背景颜色
  16. 'link', // 插入链接
  17. 'list', // 列表
  18. 'justify', // 对齐方式
  19. 'quote', // 引用
  20. // 'emoticon', // 表情
  21. 'table', // 表格
  22. 'video', // 插入视频
  23. 'image',
  24. // 'code', // 插入代码
  25. 'undo', // 撤销
  26. 'redo', // 重复
  27. ];
  28. export default {
  29. name: 'wang-editor',
  30. model: {
  31. prop: 'value',
  32. event: 'change', // 默认为input时间,此处改为change
  33. },
  34. props: {
  35. value: { type: String, required: false, default: '' },
  36. },
  37. data() {
  38. return {
  39. editorContent: this.value,
  40. };
  41. },
  42. mounted() {
  43. var editor = new E(this.$refs.editor);
  44. editor.customConfig.onchange = html => {
  45. this.editorContent = html;
  46. this.$emit('change', html);
  47. };
  48. // 自定义菜单配置
  49. editor.customConfig.menus = menus;
  50. editor.customConfig.zIndex = 0;
  51. editor.customConfig.uploadImgServer = '/files/cms/images/upload';
  52. editor.customConfig.uploadImgMaxLength = 1;
  53. editor.customConfig.uploadImgHooks = {
  54. // 如果服务器端返回的不是 {errno:0, data: [...]} 这种格式,可使用该配置
  55. // (但是,服务器端返回的必须是一个 JSON 格式字符串!!!否则会报错)
  56. customInsert: function(insertImg, result, editor) {
  57. // 图片上传并返回结果,自定义插入图片的事件(而不是编辑器自动插入图片!!!)
  58. // insertImg 是插入图片的函数,editor 是编辑器对象,result 是服务器端返回的结果
  59. // 举例:假如上传图片成功后,服务器端返回的是 {url:'....'} 这种格式,即可这样插入图片:
  60. var url = result.uri;
  61. insertImg(url);
  62. // result 必须是一个 JSON 格式字符串!!!否则报错
  63. },
  64. };
  65. editor.create();
  66. editor.txt.html(this.value);
  67. },
  68. methods: {
  69. getContent: function() {
  70. return this.editorContent;
  71. },
  72. },
  73. };
  74. </script>