wang-editor.vue 2.4 KB

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