useTemplate.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import blockquotes from "./Blockquotes.js";
  2. const tabList = [
  3. {
  4. label: "标题",
  5. name: "titleList",
  6. },
  7. {
  8. label: "正文",
  9. name: "contentList",
  10. },
  11. // {
  12. // label:'图片',
  13. // name:'imageList'
  14. // },
  15. {
  16. label: "引导",
  17. name: "concernList",
  18. },
  19. // {
  20. // label:'场景',
  21. // name:'sceneList'
  22. // },
  23. {
  24. label: "布局",
  25. name: "layoutList",
  26. },
  27. {
  28. label: "分隔符",
  29. name: "separatorList",
  30. },
  31. // {
  32. // label:'模板',
  33. // name:'tempalteList'
  34. // }
  35. ];
  36. const useTemplate = (domId) => {
  37. let dom = document.getElementById(domId);
  38. let doc = document.getElementById(domId + "-iframe").contentDocument;
  39. let hhHeight = document.getElementById(`${domId}container`).offsetHeight;
  40. dom.style.position = "relative";
  41. let width = 375;
  42. dom.style.marginLeft = width + "px";
  43. let section = document.createElement("section");
  44. section.id = `${domId}Template`;
  45. section.className = "rich-text-template";
  46. section.style.cssText = `width:${width}px;position:absolute;top:0;left:-${
  47. width + 1
  48. }px;border:1px solid #a0a0a0;`;
  49. dom.appendChild(section);
  50. let html = `<header style="display:flex;justify-content: space-around;width:100%;height:36px;">`;
  51. for (let i = 0; i < tabList.length; i++) {
  52. let item = tabList[i];
  53. html += `<div data-type="${item.name}" id="${
  54. item.name === "titleList" ? domId + "checkTab" : ""
  55. }" class="tab-${domId}" style="background-color:${
  56. item.name === "titleList" ? "#ffffff" : "rgb(0,0,0,.2)"
  57. };flex-basis:20%;height:100%;line-height:36px;text-align:center;">${
  58. item.label
  59. }</div>`;
  60. }
  61. html += "</header>";
  62. html += `<div style="height:${hhHeight - 68}px;overflow:auto;padding:15px;">`;
  63. html += `<content id="${domId}tabContent" style="width:calc(100% - 40px);">`;
  64. for (let i = 0; i < blockquotes.titleList.length; i++) {
  65. let item = blockquotes.titleList[i];
  66. html += `<article class="${domId}templateArticle" style="margin-bottom:10px;">`;
  67. html += item;
  68. html += `</article>`;
  69. }
  70. html += "</content>";
  71. html += "</div>";
  72. document.getElementById(`${domId}Template`).innerHTML = html;
  73. let tabEleList = document.getElementsByClassName(`tab-${domId}`);
  74. let clickTemplate = () => {
  75. let temList = document.getElementsByClassName(`${domId}templateArticle`);
  76. for (let i = 0; i < temList.length; i++) {
  77. let item = temList[i];
  78. item.onclick = (e) => {
  79. //console.log("e", e.target.parentNode);
  80. let el = e.target;
  81. while (el.className.indexOf(`${domId}templateArticle`) === -1) {
  82. el = el.parentNode;
  83. }
  84. doc.body.focus();
  85. doc.execCommand("insertHTML", false, el.innerHTML + "<br/>");
  86. };
  87. }
  88. };
  89. clickTemplate();
  90. for (let i = 0; i < tabEleList.length; i++) {
  91. let item = tabEleList[i];
  92. item.onclick = (e) => {
  93. let el = e.target;
  94. let type = el.dataset.type;
  95. if (el.style.backgroundColor !== "#ffffff") {
  96. let checkDom = document.getElementById(`${domId}checkTab`);
  97. checkDom.style.backgroundColor = "rgb(0,0,0,.2)";
  98. checkDom.id = "";
  99. el.style.backgroundColor = "#ffffff";
  100. el.id = `${domId}checkTab`;
  101. let checkHtml = "";
  102. let checkList = blockquotes[type];
  103. for (let j = 0; j < checkList.length; j++) {
  104. checkHtml += `<article class="${domId}templateArticle" style="margin-bottom:10px;">`;
  105. checkHtml += checkList[j];
  106. checkHtml += `</article>`;
  107. }
  108. document.getElementById(`${domId}tabContent`).innerHTML = checkHtml;
  109. setTimeout(() => {
  110. clickTemplate();
  111. }, 30);
  112. }
  113. };
  114. }
  115. };
  116. export default useTemplate;