Editor_v1.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <div id="toolbar"></div>
  3. <div ref="editorRef" style="border: 1px solid #ccc; height: 500px"></div>
  4. </template>
  5. <script setup>
  6. import { onMounted, ref, onUnmounted } from 'vue';
  7. import { createEditor, createToolbar, Boot } from '@wangeditor/editor';
  8. import '@wangeditor/editor/dist/css/style.css'; // 引入 css
  9. const editorRef = ref(null);
  10. let editor = null;
  11. let toolbarEditor = null;
  12. onMounted(() => {
  13. if (editorRef.value) {
  14. editor = createEditor({ selector: editorRef.value });
  15. toolbarEditor = createToolbar({
  16. editor,
  17. selector: '#toolbar' // 确保有一个id为toolbar的DOM元素
  18. });
  19. }
  20. console.log('editor', editor);
  21. console.log(Boot);
  22. });
  23. onUnmounted(() => {
  24. if (editor) {
  25. editor.destroy(); // 销毁编辑器实例
  26. }
  27. if (toolbarEditor) {
  28. toolbarEditor.destroy(); // 销毁工具栏实例(如果有的话)
  29. }
  30. });
  31. //自定义菜单
  32. class MyButtonMenu {
  33. // JS 语法
  34. constructor(title, iconSvg, tag, exec) {
  35. this.title = title; // 自定义菜单标题
  36. this.iconSvg = iconSvg; // 自定义菜单的svg
  37. this.tag = tag; //自定义菜单的类型
  38. this.exec = exec; //自定义菜单点击的方法
  39. }
  40. // 获取菜单执行时的 value ,用不到则返回空 字符串或 false
  41. getValue(editor) {
  42. // JS 语法
  43. return ' hello ';
  44. }
  45. // 菜单是否需要激活(如选中加粗文本,“加粗”菜单会激活),用不到则返回 false
  46. isActive(editor) {
  47. // JS 语法
  48. return false;
  49. }
  50. // 菜单是否需要禁用(如选中 H1 ,“引用”菜单被禁用),用不到则返回 false
  51. isDisabled(editor) {
  52. // JS 语法
  53. return false;
  54. }
  55. // 点击菜单时触发的函数
  56. // exec(editor, value) { // JS 语法
  57. // if (this.isDisabled(editor)) return
  58. // editor.insertText(value) // value 即 this.value(editor) 的返回值
  59. // }
  60. }
  61. </script>