index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div class="AIeditor">
  3. <Toolbar
  4. style="border-bottom: 1px solid #ccc"
  5. :editor="editorRef"
  6. :defaultConfig="toolbarConfig"
  7. mode="default"
  8. />
  9. <el-row :gutter="20">
  10. <el-col :span="20">
  11. <div class="bgEditor">
  12. <div class="editor">
  13. <h1 contenteditable style="padding: 100px 100px 0;outline: none">无标题</h1>
  14. <Editor
  15. v-model="valueHtml"
  16. :defaultConfig="editorConfig"
  17. style="padding: 0 90px;"
  18. mode="default"
  19. @onCreated="handleCreated"
  20. />
  21. </div>
  22. <div class="tool">123</div>
  23. </div>
  24. </el-col>
  25. <el-col :span="4">1</el-col>
  26. </el-row>
  27. </div>
  28. </template>
  29. <script setup>
  30. import '@wangeditor/editor/dist/css/style.css'; // 引入 css
  31. import { onBeforeUnmount, ref, shallowRef, onMounted } from 'vue';
  32. import { Editor, Toolbar } from '@wangeditor/editor-for-vue';
  33. // 编辑器实例,必须用 shallowRef
  34. const editorRef = shallowRef();
  35. // 内容 HTML
  36. const valueHtml = ref('<p>hello</p>');
  37. onMounted(() => {});
  38. const toolbarConfig = {
  39. toolbarKeys: [
  40. 'redo',
  41. 'undo',
  42. 'bold',
  43. 'underline',
  44. 'italic',
  45. 'through',
  46. '|',
  47. 'clearStyle',
  48. 'color',
  49. 'bgColor',
  50. 'fontSize',
  51. 'fontFamily',
  52. '|',
  53. 'indent',
  54. 'delIndent',
  55. 'justifyLeft',
  56. 'justifyRight',
  57. 'justifyCenter',
  58. 'justifyJustify',
  59. '|',
  60. 'divider',
  61. 'headerSelect',
  62. 'bulletedList',
  63. 'numberedList',
  64. ],
  65. };
  66. const editorConfig = {
  67. placeholder: '请输入内容...',
  68. hoverbarKeys: {
  69. text: {
  70. // 如有 match 函数,则优先根据 match 判断,而忽略 element type
  71. match: editor => {
  72. // 可参考下文的源码
  73. console.log(editor.getConfig());
  74. },
  75. menuKeys: [], // 定义你想要的 menu keys
  76. },
  77. },
  78. };
  79. // 组件销毁时,也及时销毁编辑器
  80. onBeforeUnmount(() => {
  81. if (editorRef.value != null) editorRef.value.destroy();
  82. });
  83. const handleCreated = editor => {
  84. editorRef.value = editor; // 记录 editor 实例,重要!
  85. };
  86. </script>
  87. <style scoped>
  88. .AIeditor {
  89. }
  90. .bgEditor {
  91. position: relative;
  92. }
  93. .bgEdigtoTool {
  94. position: absolute;
  95. }
  96. .editor {
  97. width: 80%;
  98. box-shadow: 0 0 25px #eeeeee;
  99. height: 100vh;
  100. margin: 1em auto;
  101. }
  102. .tool{
  103. display: flex;
  104. }
  105. </style>