editorButtom2.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { DomEditor } from '@wangeditor/editor';
  2. let targetDom = null;
  3. let editors = null;
  4. export default class MyMenu {
  5. constructor() {
  6. // super(editor);
  7. this.title = '格式刷';
  8. this.iconSvg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 384 512"><path d="M352 0H32C14.33 0 0 14.33 0 32v224h384V32c0-17.67-14.33-32-32-32zM0 320c0 35.35 28.66 64 64 64h64v64c0 35.35 28.66 64 64 64s64-28.65 64-64v-64h64c35.34 0 64-28.65 64-64v-32H0v32zm192 104c13.25 0 24 10.74 24 24c0 13.25-10.75 24-24 24s-24-10.75-24-24c0-13.26 10.75-24 24-24z" fill="currentColor"></path></svg>';
  9. this.tag = 'button';
  10. this._active = false;
  11. const dom = document.getElementById('editor-container');
  12. const _this = this;
  13. dom.onclick = function () {
  14. if (!_this._active) return;
  15. // 深拷贝所有的json
  16. let html = JSON.stringify(editors.children);
  17. html = JSON.parse(html);
  18. console.log(html);
  19. // 当前获取的的值
  20. const doms = DomEditor.getSelectedElems(editors);
  21. if (doms[0]?.type == 'image' || doms[1]?.type == 'image') return false;
  22. if (!targetDom) {
  23. _this._active = false;
  24. return;
  25. }
  26. // 循环赋值
  27. for (let l = 0; l < html.length; l++) {
  28. for (let i = 0; i < html[l].children.length; i++) {
  29. let children = doms[0].children[0].text.replace(/\s+/g, '');
  30. if (children == '') children = doms[0].children[1];
  31. if (html[l].children[i].text == children.text) {
  32. for (const key in targetDom) {
  33. html[l].children[i][key] = targetDom[key];
  34. }
  35. }
  36. }
  37. }
  38. const paste = pasteDom(html);
  39. console.log(paste, 'paste');
  40. console.log(editors.getHtml());
  41. editors.setHtml(paste);
  42. };
  43. }
  44. tryChangeActive() {}
  45. getValue(editor) {
  46. return editor;
  47. }
  48. isActive(editor) {
  49. return false;
  50. }
  51. isDisabled(editor) {
  52. return false; // or true
  53. }
  54. exec(editor, value) {
  55. editors = editor;
  56. this._active = !this._active;
  57. if (!this._active) {
  58. editor.copyStyleList = null;
  59. } else {
  60. // 获取格式刷样式
  61. const node = DomEditor.getSelectedElems(editor);
  62. // if (!node) return;
  63. const containerEle = node[0];
  64. const copyStyleList = parseDom(containerEle);
  65. // // 保存格式刷样式
  66. editor.copyStyleList = copyStyleList;
  67. }
  68. }
  69. }
  70. // 复制选中dom的样式
  71. function parseDom(dom) {
  72. if (dom.children[0]?.type == 'image' || dom.children[1]?.type == 'image') return false;
  73. getTargetDom(dom);
  74. function getTargetDom(dom) {
  75. let children = dom.children[0];
  76. const item = children.text.replace(/\s+/g, '');
  77. if (item == '') children = dom.children[1];
  78. const childrens = { ...children };
  79. delete childrens.text;
  80. targetDom = childrens;
  81. }
  82. }
  83. // 循环制造标签
  84. function pasteDom(dom) {
  85. let list = '';
  86. for (let l = 0; l < dom.length; l++) {
  87. for (let i = 0; i < dom[l].children.length; i++) {
  88. let style = '';
  89. let text = '';
  90. if (dom[l].children[i].type == 'image') {
  91. for (const keyl in dom[l].children[i].style) {
  92. style += `${keyl}:${dom[l].children[i].style[keyl]};`;
  93. }
  94. } else {
  95. text = dom[l].children[i].text;
  96. delete dom[l].children[i].text;
  97. for (const key in dom[l].children[i]) {
  98. let keys = key;
  99. if (keys == 'fontSize') keys = 'font-size';
  100. style += `${keys}:${dom[l].children[i][key]};`;
  101. }
  102. }
  103. const labels = dom[l].children[i].type !== 'image' ? `<p><span style="${style}">${text}</span></p>` : `<p><img style="${style}" src="${dom[l].children[i]?.src}"></img></p>`;
  104. list += labels;
  105. }
  106. }
  107. return list;
  108. }