index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import iconBase64 from "./iframe/headerButton/iconBase64.js";
  2. import initHeaderIcon from "./iframe/headerButton/initHeaderIcon.js";
  3. //import UTILS from "./utils/utils.js";
  4. //import initListenerInput from "./iframe/htmlEditor/initListenerInput.js";
  5. import htmlContent from "./iframe/htmlEditor/htmlContent.js";
  6. import useTemplate from "./iframe/template/useTemplate.js";
  7. let RichTextTool = {};
  8. //初始化事件
  9. let initHeaderClick = (headerList) => {
  10. for (let i = 0; i < headerList.length; i++) {
  11. let item = headerList[i];
  12. document.getElementsByClassName("rich-text-outicon")[i].onclick =
  13. item.onClick;
  14. }
  15. };
  16. //初始化htmlDom
  17. let initHtmlDom = (headerList, width, height, domId) => {
  18. let arr = [];
  19. arr.push(`
  20. <section
  21. id="${domId}container"
  22. class="rich-text-container"
  23. style="width:${width}px;background:#fff;border:1px solid #ccc;overflow:hidden;"
  24. >`);
  25. arr.push(`
  26. <header id="${domId}headerClick"
  27. style="width:${width}px;display:flex;flex-wrap:wrap;"
  28. >`);
  29. for (let i = 0; i < headerList.length; i++) {
  30. let item = headerList[i];
  31. let img = iconBase64[`${item.type}Img`];
  32. arr.push(`
  33. <span class="rich-text-outicon" data-type="${item.type}" title="${item.name}"
  34. style="width:28px;height:28px;margin:5px;background-image:url(${img});background-repeat:no-repeat;background-size:100% 100%;cursor:pointer;opacity:1;"
  35. unselectable="on"></span>
  36. `);
  37. }
  38. arr.push("</header>");
  39. let iframe = `<iframe style="margin:0;padding:0;border:0;display:block;" id="${domId}-iframe" width="${
  40. width - 2
  41. }" height="${height}"></iframe>`;
  42. arr.push(iframe);
  43. let textarea = `<textarea id="${domId}-textarea" hidefocus="true" style="width:${
  44. width - 7
  45. }px;height:${height}px;display:none;resize:none;overflow:auto;"></textarea>`;
  46. arr.push(textarea);
  47. arr.push("</section>");
  48. return arr.join("\n");
  49. };
  50. //初始化Iframe
  51. let initIframe = (width, height, domId) => {
  52. let html = `
  53. <!DOCTYPE html>
  54. <html lang="en">
  55. <head>
  56. <title></title>
  57. <meta charset="UTF-8" />
  58. <meta name="viewport" content="width=device-width, initial-scale=1" />
  59. <style>
  60. html{
  61. margin:0;
  62. padding:0;
  63. }
  64. body{
  65. margin:0;
  66. padding:5px;
  67. border:1px solid #ccc;
  68. line-height:1.5;
  69. color:#333;
  70. }
  71. body,td{
  72. font-size:16px;
  73. }
  74. body,p,div{
  75. word-break:break-all;
  76. }
  77. p{
  78. margin:5px 0;
  79. }
  80. img{
  81. border:0;
  82. }
  83. table {
  84. border-collapse:collapse;
  85. }
  86. </style>
  87. </head>
  88. <body class="rich-text-content" contenteditable="true" style="min-height:${
  89. height - 12
  90. }px"></body></html>`;
  91. document.getElementById(domId + "-iframe").contentDocument.write(html);
  92. };
  93. //初始化头部head
  94. let initHead = () => {
  95. let head = document.getElementsByTagName("head")[0];
  96. let link = document.createElement("link");
  97. link.href = "/css/richIndex.css";
  98. link.setAttribute("rel", "stylesheet");
  99. link.setAttribute("media", "all");
  100. link.setAttribute("type", "text/css");
  101. head.appendChild(link);
  102. };
  103. const initRichText = (
  104. domId,
  105. isTemplate = true,
  106. defaultHeader = [],
  107. customHeader = []
  108. ) => {
  109. //支持浏览器
  110. //暴露图片传输
  111. //暴露获取,设置html;
  112. //暴露,自定义按钮事件
  113. initHead();
  114. let dom = document.getElementById(domId);
  115. let width = dom.offsetWidth || 385;
  116. let height = dom.offsetHeight || 450;
  117. let headerList = initHeaderIcon(domId, defaultHeader, customHeader);
  118. let html = initHtmlDom(headerList, width, height, domId);
  119. dom.innerHTML = html;
  120. initIframe(width, height, domId); //初始化dom
  121. initHeaderClick(headerList); //点击事件
  122. let doc = document.getElementById(domId + "-iframe").contentDocument;
  123. doc.body.focus();
  124. doc.execCommand("styleWithCSS", null, true);
  125. //想使用不同的段落分隔符
  126. //doc.execCommand("defaultParagraphSeparator", false, "br");
  127. //initListenerInput(domId); //监听输入
  128. //UTILS._getBase64();
  129. if (isTemplate && dom.style.position === "") {
  130. useTemplate(domId);
  131. }
  132. //暴露出去api
  133. RichTextTool = {
  134. getDocument(id) {
  135. return document.getElementById(domId + "-iframe").contentDocument;
  136. },
  137. getWindow(id) {
  138. return document.getElementById(domId + "-iframe").contentWindow;
  139. },
  140. getHtml(id) {
  141. return htmlContent.getHtml(id);
  142. },
  143. setHtml(id, val) {
  144. htmlContent.setHtml(id, val);
  145. },
  146. bindClick(id, item, fn) {
  147. // item = {
  148. // name: "一键排版",
  149. // type: "",//唯一类型id
  150. // img: "",//base64或者地址
  151. // };
  152. let span = document.createElement("span");
  153. span.id = `${id}${item.type}`;
  154. span.className = "rich-text-outicon";
  155. span.dataset.type = item.type;
  156. span.title = item.name;
  157. span.style.cssText = `width:28px;height:28px;margin:5px;background-image:url(${item.img});background-repeat:no-repeat;background-size:100% 100%;cursor:pointer;opacity:1;`;
  158. span.unselectable = "on";
  159. document.getElementById(`${id}headerClick`).appendChild(span);
  160. document.getElementById(`${id}${item.type}`).onclick = (e) => {
  161. fn(e);
  162. };
  163. },
  164. };
  165. };
  166. //module.exports.RichTextTool = RichTextTool;
  167. //module.exports.initRichText = initRichText;
  168. export { initRichText, RichTextTool };