headerClickFun.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. import htmlContent from "../htmlEditor/htmlContent.js";
  2. let backList = [],
  3. recoverList = [];
  4. const headerClickFun = {
  5. //点击事件,展开html代码
  6. showHtml: (e, domId) => {
  7. let list = e.target.parentNode.children;
  8. //将节点列表(NodeList)转化为数组
  9. let classList = Array.prototype.slice.call(e.target.classList, 0);
  10. let isSelected = true;
  11. if (classList.includes("rich-text-outicon-selected")) {
  12. //取消选中
  13. e.target.classList.remove("rich-text-outicon-selected");
  14. isSelected = false;
  15. } else {
  16. //选择
  17. e.target.classList.add("rich-text-outicon-selected");
  18. }
  19. for (let i = 0; i < list.length; i++) {
  20. if (isSelected && list[i].getAttribute("data-type") !== "showHtml") {
  21. list[i].style.opacity = 0.5;
  22. list[i].style.cursor = "default";
  23. } else if (list[i].getAttribute("data-type") !== "showHtml") {
  24. list[i].style.opacity = 1;
  25. list[i].style.cursor = "pointer";
  26. }
  27. }
  28. let iframe = document.getElementById(domId + "-iframe"),
  29. textarea = document.getElementById(domId + "-textarea");
  30. // textarea.onchange = null;
  31. if (isSelected) {
  32. let html = htmlContent.getHtml(domId);
  33. iframe.style.display = "none";
  34. textarea.style.display = "block";
  35. textarea.value = html;
  36. // textarea.onchange = (e) => {
  37. // htmlContent.setHtml(e.target.value);
  38. // };
  39. } else {
  40. iframe.style.display = "block";
  41. textarea.style.display = "none";
  42. htmlContent.setHtml(domId, textarea.value);
  43. textarea.value = "";
  44. }
  45. },
  46. //后退事件
  47. doBack: (e, domId) => {
  48. let doc = document.getElementById(domId + "-iframe").contentDocument;
  49. doc.body.focus();
  50. doc.execCommand("undo");
  51. return;
  52. let list = document.getElementById(domId + "-iframe").contentWindow
  53. .backDataList;
  54. let len = list.length;
  55. if (len === 1) {
  56. htmlContent.setHtml(domId, "");
  57. } else {
  58. htmlContent.setHtml(domId, list[len - 2]);
  59. document
  60. .getElementById(domId + "-iframe")
  61. .contentWindow.recoverDataList.unshift(list[len - 1]);
  62. document
  63. .getElementById(domId + "-iframe")
  64. .contentWindow.backDataList.splice(len - 1);
  65. }
  66. },
  67. //前进事件
  68. doRecover: (e, domId) => {
  69. let doc = document.getElementById(domId + "-iframe").contentDocument;
  70. doc.body.focus();
  71. doc.execCommand("redo");
  72. return;
  73. let list = document.getElementById(domId + "-iframe").contentWindow
  74. .recoverDataList;
  75. let len = list.length;
  76. if (len > 0) {
  77. htmlContent.setHtml(domId, list[0]);
  78. document
  79. .getElementById(domId + "-iframe")
  80. .contentWindow.backDataList.push(list[0]);
  81. document
  82. .getElementById(domId + "-iframe")
  83. .contentWindow.recoverDataList.splice(0, 1);
  84. }
  85. },
  86. //打印机事件
  87. doPrint: (e, domId) => {
  88. let features =
  89. "fullscreen=no,height=500, width=800, top=100, left=100, toolbar=no, menubar=no,scrollbars=no,resizable=no, location=no, status=no"; //设置新窗口的特性
  90. let newWindow = window.open("打印窗口", "_blank", features);
  91. let docStr = document.getElementById(domId + "-iframe").contentDocument.body
  92. .innerHTML; //htmlContent.getHtml(domId);
  93. newWindow.document.write(docStr);
  94. newWindow.document.close();
  95. newWindow.print();
  96. newWindow.close();
  97. },
  98. //剪切
  99. doCut: (e, domId) => {
  100. //let win = document.getElementById(domId + "-iframe").contentWindow;
  101. let doc = document.getElementById(domId + "-iframe").contentDocument;
  102. //document.selection.createRange().text; // IE9以下使用
  103. // let selectText = win.getSelection
  104. // ? win.getSelection().toString()
  105. // : doc.selection.createRange().text;
  106. doc.execCommand("Cut");
  107. },
  108. //复制
  109. doCopy: (e, domId) => {
  110. let doc = document.getElementById(domId + "-iframe").contentDocument;
  111. doc.execCommand("Copy");
  112. },
  113. doPaste: (e, domId) => {
  114. let doc = document.getElementById(domId + "-iframe").contentDocument;
  115. doc.body.focus();
  116. try {
  117. //readText
  118. navigator.clipboard
  119. .readText()
  120. .then(async (v) => {
  121. //read()
  122. // for (const clipboardItem of v) {
  123. // for (const type of clipboardItem.types) {
  124. // const blob = await clipboardItem.getType(type);
  125. // console.log(blob.text);
  126. // //console.log(URL.createObjectURL(blob));
  127. // }
  128. // }
  129. doc.execCommand("insertText", false, v);
  130. })
  131. .catch((v) => {
  132. console.log("doPaste获取剪贴板失败: ", v);
  133. });
  134. } catch (err) {
  135. console.error("Failed to read clipboard contents: ", err);
  136. }
  137. //doc.execCommand("Paste");
  138. },
  139. doPasteUnformat: (e, domId) => {
  140. let doc = document.getElementById(domId + "-iframe").contentDocument;
  141. doc.body.focus();
  142. try {
  143. navigator.clipboard
  144. .readText()
  145. .then((v) => {
  146. //console.log("doPaste获取剪贴板成功:", v);
  147. doc.execCommand("insertHtml", false, v);
  148. })
  149. .catch((v) => {
  150. console.log("doPaste获取剪贴板失败: ", v);
  151. });
  152. } catch (err) {
  153. console.error("Failed to read clipboard contents: ", err);
  154. }
  155. },
  156. //左对齐
  157. justifyLeft: (e, domId) => {
  158. let doc = document.getElementById(domId + "-iframe").contentDocument;
  159. doc.body.focus();
  160. doc.execCommand("justifyLeft");
  161. },
  162. justifyCenter: (e, domId) => {
  163. let doc = document.getElementById(domId + "-iframe").contentDocument;
  164. doc.body.focus();
  165. doc.execCommand("justifyCenter");
  166. },
  167. justifyRight: (e, domId) => {
  168. let doc = document.getElementById(domId + "-iframe").contentDocument;
  169. doc.body.focus();
  170. doc.execCommand("justifyRight");
  171. },
  172. justifyFull: (e, domId) => {
  173. let doc = document.getElementById(domId + "-iframe").contentDocument;
  174. doc.body.focus();
  175. doc.execCommand("justifyFull");
  176. },
  177. insertOrderedList: (e, domId) => {
  178. let doc = document.getElementById(domId + "-iframe").contentDocument;
  179. doc.body.focus();
  180. doc.execCommand("insertOrderedList");
  181. },
  182. insertUnorderedList: (e, domId) => {
  183. let doc = document.getElementById(domId + "-iframe").contentDocument;
  184. doc.body.focus();
  185. doc.execCommand("insertUnorderedList");
  186. },
  187. doIndent: (e, domId) => {
  188. let doc = document.getElementById(domId + "-iframe").contentDocument;
  189. doc.body.focus();
  190. doc.execCommand("indent");
  191. },
  192. doOutdent: (e, domId) => {
  193. let doc = document.getElementById(domId + "-iframe").contentDocument;
  194. doc.body.focus();
  195. doc.execCommand("outdent");
  196. },
  197. //下标
  198. doSubscript: (e, domId) => {
  199. let doc = document.getElementById(domId + "-iframe").contentDocument;
  200. doc.body.focus();
  201. doc.execCommand("subscript");
  202. },
  203. doSuperscript: (e, domId) => {
  204. let doc = document.getElementById(domId + "-iframe").contentDocument;
  205. doc.body.focus();
  206. doc.execCommand("superscript");
  207. },
  208. removeFormat: (e, domId) => {
  209. let doc = document.getElementById(domId + "-iframe").contentDocument;
  210. doc.body.focus();
  211. doc.execCommand("removeFormat");
  212. },
  213. selectAll: (e, domId) => {
  214. let doc = document.getElementById(domId + "-iframe").contentDocument;
  215. doc.body.focus();
  216. doc.execCommand("selectAll");
  217. },
  218. doHeading: (e, domId) => {
  219. e.target.style.position = "relative";
  220. let div = `<article id="${domId}clickRemove"
  221. style="width:300px;height:100px;overflow:auto;padding:4px;background:#fff;position:absolute;bottom:-4px;left:-4px;z-index:99999;border:1px solid #a0a0a0;box-shadow:1px 1px 3px #a0a0a0;">
  222. <div id="${domId}doHeadingH1" style="font-weight:bold;font-size:20px;height:28px;line-height:28px;">标题1</div>
  223. <div id="${domId}doHeadingH2" style="font-weight:bold;font-size:18px;height:26px;line-height:26px;">标题2</div>
  224. <div id="${domId}doHeadingH3" style="font-weight:bold;font-size:16px;height:24px;line-height:24px;">标题3</div>
  225. <div id="${domId}doHeadingH4" style="font-weight:bold;font-size:14px;">标题4</div>
  226. <div id="${domId}doHeadingH0" style="font-size:12px;">正文</div>
  227. </article>`;
  228. e.target.innerHTML = div;
  229. let doc = document.getElementById(domId + "-iframe").contentDocument;
  230. doc.onclick = (e) => {
  231. let self = document.getElementById(`${domId}clickRemove`);
  232. if (self) {
  233. self.parentElement.removeChild(self);
  234. }
  235. };
  236. document.body.onclick = (e) => {
  237. if (e.target.dataset.type !== "doHeading") {
  238. if (e.target.id === `${domId}doHeadingH1`) {
  239. doc.body.focus();
  240. doc.execCommand("formatBlock", false, "h1");
  241. } else if (e.target.id === `${domId}doHeadingH2`) {
  242. doc.body.focus();
  243. doc.execCommand("formatBlock", false, "h2");
  244. } else if (e.target.id === `${domId}doHeadingH3`) {
  245. doc.body.focus();
  246. doc.execCommand("formatBlock", false, "h3");
  247. } else if (e.target.id === `${domId}doHeadingH4`) {
  248. doc.body.focus();
  249. doc.execCommand("formatBlock", false, "h4");
  250. } else if (e.target.id === `${domId}doHeadingH0`) {
  251. doc.body.focus();
  252. doc.execCommand("formatBlock", false, "p");
  253. }
  254. let self = document.getElementById(`${domId}clickRemove`);
  255. if (self) {
  256. self.parentElement.removeChild(self);
  257. }
  258. }
  259. };
  260. },
  261. fontName: (e, domId) => {
  262. let mySelf = document.getElementById(`${domId}clickRemove`);
  263. if (mySelf) {
  264. mySelf.parentElement.removeChild(mySelf);
  265. }
  266. e.target.style.position = "relative";
  267. let div = `<article id="${domId}clickRemove"
  268. style="width:150px;height:100px;overflow:auto;padding:4px;background:#fff;position:absolute;bottom:-4px;left:-4px;z-index:99999;font-size:12px;border:1px solid #a0a0a0;box-shadow:1px 1px 3px #a0a0a0;">
  269. <div id="${domId}fontNameSimSun" style="font-family:SimSun;">宋体</div>
  270. <div id="${domId}fontNameNSimSun" style="font-family:NSimSun;">新宋体</div>
  271. <div id="${domId}fontNameSimHei" style="font-family:SimHei;">黑体</div>
  272. <div id="${domId}fontNameMicrosoft__YaHei" style="font-family:Microsoft YaHei;">微软雅黑</div>
  273. <div id="${domId}fontNameMicrosoft__JhengHei" style="font-family:Microsoft JhengHei;">微软正黑体</div>
  274. <div id="${domId}fontNamePMingLiU" style="font-family:PMingLiU;">新细明体</div>
  275. <div id="${domId}fontNameMingLiU" style="font-family:MingLiU;">细明体</div>
  276. <div id="${domId}fontNameDFKai-SB" style="font-family:DFKai-SB;">标楷体</div>
  277. <div id="${domId}fontNameFangSong" style="font-family:FangSong;">仿宋</div>
  278. <div id="${domId}fontNameKaiTi" style="font-family:KaiTi;">楷体</div>
  279. <div id="${domId}fontNameLiSu" style="font-family:LiSu;">隶书</div>
  280. <div id="${domId}fontNameYouYuan" style="font-family:YouYuan;">幼圆</div>
  281. <div id="${domId}fontNameFZShuTi" style="font-family:FZShuTi;">方正舒体</div>
  282. <div id="${domId}fontNameFZYaoti" style="font-family:FZYaoti;">方正姚体</div>
  283. <div id="${domId}fontNameSTCaiyun" style="font-family:STCaiyun;">华文彩云</div>
  284. <div id="${domId}fontNameSTHupo" style="font-family:STHupo;">华文琥珀</div>
  285. <div id="${domId}fontNameSTXinwei" style="font-family:STXinwei;">华文新魏</div>
  286. <div id="${domId}fontNameSTXingkai" style="font-family:STXingkai;">华文行楷</div>
  287. <div id="${domId}fontNameArial" style="font-family:Arial;">Arial</div>
  288. <div id="${domId}fontNameBrush__Script__MT" style="font-family:Brush Script MT;">Brush Script MT</div>
  289. <div id="${domId}fontNameCourier__New" style="font-family:Courier New;">Courier New</div>
  290. <div id="${domId}fontNameLucida__Handwriting" style="font-family:Lucida Handwriting;">Lucida Handwriting</div>
  291. <div id="${domId}fontNamePapyrus" style="font-family:Papyrus;">Papyrus</div>
  292. <div id="${domId}fontNameTimes__New__Roman" style="font-family:Times New Roman;">Times New Roman</div>
  293. </article>`;
  294. e.target.innerHTML = div;
  295. let doc = document.getElementById(domId + "-iframe").contentDocument;
  296. doc.onclick = (e) => {
  297. let self = document.getElementById(`${domId}clickRemove`);
  298. if (self) {
  299. self.parentElement.removeChild(self);
  300. }
  301. };
  302. document.body.onclick = (e) => {
  303. e.stopPropagation();
  304. if (e.target.dataset.type !== "fontName") {
  305. let id = e.target.id;
  306. let index = id.indexOf(`${domId}fontName`);
  307. if (index === 0) {
  308. let newId = id.replace(`${domId}fontName`, "");
  309. let filed = newId.replace("__", "");
  310. doc.body.focus();
  311. doc.execCommand("fontName", false, filed);
  312. }
  313. let self = document.getElementById(`${domId}clickRemove`);
  314. if (self) {
  315. self.parentElement.removeChild(self);
  316. }
  317. }
  318. };
  319. },
  320. fontSize: (e, domId) => {
  321. let mySelf = document.getElementById(`${domId}clickRemove`);
  322. if (mySelf) {
  323. mySelf.parentElement.removeChild(mySelf);
  324. }
  325. e.target.style.position = "relative";
  326. let div = `<article id="${domId}clickRemove"
  327. style="width:150px;height:98px;overflow:auto;padding:4px;background:#fff;position:absolute;bottom:0;left:0;z-index:99999;font-size:12px;border:1px solid #a0a0a0;box-shadow:1px 1px 3px #a0a0a0;">
  328. <div id="${domId}fontSize12" style="font-size:12px;">12px</div>
  329. <div id="${domId}fontSize14" style="font-size:14px;">14px</div>
  330. <div id="${domId}fontSize16" style="font-size:16px;">16px</div>
  331. <div id="${domId}fontSize18" style="font-size:18px;">18px</div>
  332. <div id="${domId}fontSize20" style="font-size:20px;">20px</div>
  333. <div id="${domId}fontSize24" style="font-size:24px;">24px</div>
  334. <div id="${domId}fontSize28" style="font-size:28px;">28px</div>
  335. <div id="${domId}fontSize32" style="font-size:32px;">32px</div>
  336. <div id="${domId}fontSize42" style="font-size:34px;">42px</div>
  337. <div id="${domId}fontSize60" style="font-size:36px;">60px</div>
  338. <div id="${domId}fontSize72" style="font-size:40px;">72px</div>
  339. </article>`;
  340. e.target.innerHTML = div;
  341. let doc = document.getElementById(domId + "-iframe").contentDocument;
  342. let win = document.getElementById(domId + "-iframe").contentWindow;
  343. doc.onclick = (e) => {
  344. let self = document.getElementById(`${domId}clickRemove`);
  345. if (self) {
  346. self.parentElement.removeChild(self);
  347. }
  348. };
  349. document.body.onclick = (e) => {
  350. e.stopPropagation();
  351. if (e.target.dataset.type !== "fontSize") {
  352. let id = e.target.id;
  353. let index = id.indexOf(`${domId}fontSize`);
  354. if (index === 0) {
  355. let newId = id.replace(`${domId}fontSize`, "");
  356. doc.body.focus();
  357. doc.execCommand("fontSize", false, 3);
  358. let spanList = doc.getElementsByTagName("span");
  359. let aList = doc.getElementsByTagName("a");
  360. //let uList = doc.getElementsByTagName("u");
  361. // let strikeList = doc.getElementsByTagName("strike");
  362. let list = [...spanList, ...aList];
  363. for (let i = 0; i < list.length; i++) {
  364. let size = list[i].style.fontSize;
  365. if (size === "medium") {
  366. list[i].style.fontSize = newId + "px";
  367. list[i].style.zIndex = "891314";
  368. list[i].id = Date.now();
  369. }
  370. }
  371. }
  372. let self = document.getElementById(`${domId}clickRemove`);
  373. if (self) {
  374. self.parentElement.removeChild(self);
  375. }
  376. }
  377. };
  378. },
  379. foreColor: (e, domId) => {
  380. let mySelf = document.getElementById(`${domId}clickRemove`);
  381. if (mySelf) {
  382. mySelf.parentElement.removeChild(mySelf);
  383. }
  384. e.target.style.position = "relative";
  385. let div = `<article id="${domId}clickRemove"
  386. style="width:170px;height:100px;overflow:auto;padding:4px;background:#fff;
  387. display:flex;flex-wrap:wrap;
  388. position:absolute;bottom:-4px;left:-4px;z-index:99999;font-size:12px;border:1px solid #a0a0a0;box-shadow:1px 1px 3px #a0a0a0;">
  389. <div id="${domId}foreColor333333" style="width:170px;height:20px;line-height:color:#333333;20px;text-align:center;">恢复默认</div>
  390. <div id="${domId}foreColorE53333" style="width:20px;height:20px;background:#E53333;margin:0 8px 8px 0;"></div>
  391. <div id="${domId}foreColorE56600" style="width:20px;height:20px;background:#E56600;margin:0 8px 8px 0;"></div>
  392. <div id="${domId}foreColorFF9900" style="width:20px;height:20px;background:#FF9900;margin:0 8px 8px 0;"></div>
  393. <div id="${domId}foreColor64451D" style="width:20px;height:20px;background:#64451D;margin:0 8px 8px 0;"></div>
  394. <div id="${domId}foreColorDFC5A4" style="width:20px;height:20px;background:#DFC5A4;margin:0 8px 8px 0;"></div>
  395. <div id="${domId}foreColorFFE500" style="width:20px;height:20px;background:#FFE500;margin:0 8px 8px 0;"></div>
  396. <div id="${domId}foreColor009900" style="width:20px;height:20px;background:#009900;margin:0 8px 8px 0;"></div>
  397. <div id="${domId}foreColor006600" style="width:20px;height:20px;background:#006600;margin:0 8px 8px 0;"></div>
  398. <div id="${domId}foreColor99BB00" style="width:20px;height:20px;background:#99BB00;margin:0 8px 8px 0;"></div>
  399. <div id="${domId}foreColorB8D100" style="width:20px;height:20px;background:#B8D100;margin:0 8px 8px 0;"></div>
  400. <div id="${domId}foreColor60D978" style="width:20px;height:20px;background:#60D978;margin:0 8px 8px 0;"></div>
  401. <div id="${domId}foreColor00D5FF" style="width:20px;height:20px;background:#00D5FF;margin:0 8px 8px 0;"></div>
  402. <div id="${domId}foreColor337FE5" style="width:20px;height:20px;background:#337FE5;margin:0 8px 8px 0;"></div>
  403. <div id="${domId}foreColor003399" style="width:20px;height:20px;background:#003399;margin:0 8px 8px 0;"></div>
  404. <div id="${domId}foreColor4C33E5" style="width:20px;height:20px;background:#4C33E5;margin:0 8px 8px 0;"></div>
  405. <div id="${domId}foreColor9933E5" style="width:20px;height:20px;background:#9933E5;margin:0 8px 8px 0;"></div>
  406. <div id="${domId}foreColorCC33E5" style="width:20px;height:20px;background:#CC33E5;margin:0 8px 8px 0;"></div>
  407. <div id="${domId}foreColorEE33EE" style="width:20px;height:20px;background:#EE33EE;margin:0 8px 8px 0;"></div>
  408. <div id="${domId}foreColorFFFFFF" style="width:20px;height:20px;background:#FFFFFF;margin:0 8px 8px 0;"></div>
  409. <div id="${domId}foreColorCCCCCC" style="width:20px;height:20px;background:#CCCCCC;margin:0 8px 8px 0;"></div>
  410. <div id="${domId}foreColor999999" style="width:20px;height:20px;background:#999999;margin:0 8px 8px 0;"></div>
  411. <div id="${domId}foreColor666666" style="width:20px;height:20px;background:#666666;margin:0 8px 8px 0;"></div>
  412. <div id="${domId}foreColor333333" style="width:20px;height:20px;background:#333333;margin:0 8px 8px 0;"></div>
  413. <div id="${domId}foreColor000000" style="width:20px;height:20px;background:#000000;margin:0 8px 8px 0;"></div>
  414. </article>`;
  415. e.target.innerHTML = div;
  416. let doc = document.getElementById(domId + "-iframe").contentDocument;
  417. let win = document.getElementById(domId + "-iframe").contentWindow;
  418. doc.onclick = (e) => {
  419. let self = document.getElementById(`${domId}clickRemove`);
  420. if (self) {
  421. self.parentElement.removeChild(self);
  422. }
  423. };
  424. document.body.onclick = (e) => {
  425. e.stopPropagation();
  426. if (e.target.dataset.type !== "foreColor") {
  427. let id = e.target.id;
  428. let index = id.indexOf(`${domId}foreColor`);
  429. if (index === 0) {
  430. let newId = id.replace(`${domId}foreColor`, "");
  431. let filed = newId.replace("__", "");
  432. doc.body.focus();
  433. doc.execCommand("foreColor", false, "#" + filed);
  434. }
  435. let self = document.getElementById(`${domId}clickRemove`);
  436. if (self) {
  437. self.parentElement.removeChild(self);
  438. }
  439. }
  440. };
  441. },
  442. hiliteColor: (e, domId) => {
  443. let mySelf = document.getElementById(`${domId}clickRemove`);
  444. if (mySelf) {
  445. mySelf.parentElement.removeChild(mySelf);
  446. }
  447. e.target.style.position = "relative";
  448. let div = `<article id="${domId}clickRemove"
  449. style="width:170px;height:100px;overflow:auto;padding:4px;background:#fff;
  450. display:flex;flex-wrap:wrap;
  451. position:absolute;bottom:-4px;left:-4px;z-index:99999;font-size:12px;border:1px solid #a0a0a0;box-shadow:1px 1px 3px #a0a0a0;">
  452. <div id="${domId}hiliteColortransparent" style="width:170px;height:20px;line-height:20px;text-align:center;">无颜色</div>
  453. <div id="${domId}hiliteColorE53333" style="width:20px;height:20px;background:#E53333;margin:0 8px 8px 0;"></div>
  454. <div id="${domId}hiliteColorE56600" style="width:20px;height:20px;background:#E56600;margin:0 8px 8px 0;"></div>
  455. <div id="${domId}hiliteColorFF9900" style="width:20px;height:20px;background:#FF9900;margin:0 8px 8px 0;"></div>
  456. <div id="${domId}hiliteColor64451D" style="width:20px;height:20px;background:#64451D;margin:0 8px 8px 0;"></div>
  457. <div id="${domId}hiliteColorDFC5A4" style="width:20px;height:20px;background:#DFC5A4;margin:0 8px 8px 0;"></div>
  458. <div id="${domId}hiliteColorFFE500" style="width:20px;height:20px;background:#FFE500;margin:0 8px 8px 0;"></div>
  459. <div id="${domId}hiliteColor009900" style="width:20px;height:20px;background:#009900;margin:0 8px 8px 0;"></div>
  460. <div id="${domId}hiliteColor006600" style="width:20px;height:20px;background:#006600;margin:0 8px 8px 0;"></div>
  461. <div id="${domId}hiliteColor99BB00" style="width:20px;height:20px;background:#99BB00;margin:0 8px 8px 0;"></div>
  462. <div id="${domId}hiliteColorB8D100" style="width:20px;height:20px;background:#B8D100;margin:0 8px 8px 0;"></div>
  463. <div id="${domId}hiliteColor60D978" style="width:20px;height:20px;background:#60D978;margin:0 8px 8px 0;"></div>
  464. <div id="${domId}hiliteColor00D5FF" style="width:20px;height:20px;background:#00D5FF;margin:0 8px 8px 0;"></div>
  465. <div id="${domId}hiliteColor337FE5" style="width:20px;height:20px;background:#337FE5;margin:0 8px 8px 0;"></div>
  466. <div id="${domId}hiliteColor003399" style="width:20px;height:20px;background:#003399;margin:0 8px 8px 0;"></div>
  467. <div id="${domId}hiliteColor4C33E5" style="width:20px;height:20px;background:#4C33E5;margin:0 8px 8px 0;"></div>
  468. <div id="${domId}hiliteColor9933E5" style="width:20px;height:20px;background:#9933E5;margin:0 8px 8px 0;"></div>
  469. <div id="${domId}hiliteColorCC33E5" style="width:20px;height:20px;background:#CC33E5;margin:0 8px 8px 0;"></div>
  470. <div id="${domId}hiliteColorEE33EE" style="width:20px;height:20px;background:#EE33EE;margin:0 8px 8px 0;"></div>
  471. <div id="${domId}hiliteColorFFFFFF" style="width:20px;height:20px;background:#FFFFFF;margin:0 8px 8px 0;"></div>
  472. <div id="${domId}hiliteColorCCCCCC" style="width:20px;height:20px;background:#CCCCCC;margin:0 8px 8px 0;"></div>
  473. <div id="${domId}hiliteColor999999" style="width:20px;height:20px;background:#999999;margin:0 8px 8px 0;"></div>
  474. <div id="${domId}hiliteColor666666" style="width:20px;height:20px;background:#666666;margin:0 8px 8px 0;"></div>
  475. <div id="${domId}hiliteColor333333" style="width:20px;height:20px;background:#333333;margin:0 8px 8px 0;"></div>
  476. <div id="${domId}hiliteColor000000" style="width:20px;height:20px;background:#000000;margin:0 8px 8px 0;"></div>
  477. </article>`;
  478. e.target.innerHTML = div;
  479. let doc = document.getElementById(domId + "-iframe").contentDocument;
  480. //let win = document.getElementById(domId + "-iframe").contentWindow;
  481. doc.onclick = (e) => {
  482. let self = document.getElementById(`${domId}clickRemove`);
  483. if (self) {
  484. self.parentElement.removeChild(self);
  485. }
  486. };
  487. document.body.onclick = (e) => {
  488. e.stopPropagation();
  489. if (e.target.dataset.type !== "hiliteColor") {
  490. let id = e.target.id;
  491. let index = id.indexOf(`${domId}hiliteColor`);
  492. if (index === 0) {
  493. let newId = id.replace(`${domId}hiliteColor`, "");
  494. let filed = newId.replace("__", "");
  495. doc.body.focus();
  496. if (filed == "transparent") {
  497. doc.execCommand("hiliteColor", false, "transparent");
  498. } else {
  499. doc.execCommand("hiliteColor", false, "#" + filed);
  500. }
  501. }
  502. let self = document.getElementById(`${domId}clickRemove`);
  503. if (self) {
  504. self.parentElement.removeChild(self);
  505. }
  506. }
  507. };
  508. },
  509. doBold: (e, domId) => {
  510. let doc = document.getElementById(domId + "-iframe").contentDocument;
  511. doc.body.focus();
  512. doc.execCommand("bold");
  513. },
  514. doItalic: (e, domId) => {
  515. let doc = document.getElementById(domId + "-iframe").contentDocument;
  516. doc.body.focus();
  517. doc.execCommand("italic");
  518. },
  519. doUnderline: (e, domId) => {
  520. let doc = document.getElementById(domId + "-iframe").contentDocument;
  521. doc.body.focus();
  522. // doc.execCommand("styleWithCSS", null, false);
  523. doc.execCommand("underline");
  524. // doc.execCommand("styleWithCSS", null, true);
  525. return;
  526. //为了doLineHeight,"891314"
  527. let aList = doc.getElementsByTagName("a");
  528. let spanList = doc.getElementsByTagName("span");
  529. let list = [...aList, ...spanList];
  530. for (let i = 0; i < list.length; i++) {
  531. let textDecorationLine = list[i].style.textDecorationLine;
  532. let zIndex = list[i].style.zIndex;
  533. if (textDecorationLine.indexOf("underline") > -1) {
  534. list[i].style.zIndex = "891314";
  535. } else if (zIndex === "891314") {
  536. //删除的下划线
  537. list[i].style.zIndex = "";
  538. }
  539. }
  540. },
  541. strikeThrough: (e, domId) => {
  542. let doc = document.getElementById(domId + "-iframe").contentDocument;
  543. doc.body.focus();
  544. // doc.execCommand("styleWithCSS", null, false);
  545. doc.execCommand("strikeThrough"); //del标签最好
  546. // doc.execCommand("styleWithCSS", null, true);
  547. return;
  548. for (let i = 0; i < list.length; i++) {
  549. let textDecorationLine = list[i].style.textDecorationLine;
  550. console.log("textDecorationLine", textDecorationLine);
  551. let delList = list[i].getElementsByTagName("del");
  552. let delText = "";
  553. let text = list[i].innerText;
  554. console.log(delList, text, delText);
  555. for (let j = 0; j < delList.length; j++) {
  556. delText += delList[j].innerText;
  557. }
  558. let html = list[i].innerHTML;
  559. if (textDecorationLine.indexOf("line-through") > -1) {
  560. list[i].style.textDecorationLine = "";
  561. html = html.replace(/<del>/g, "");
  562. html = html.replace(/<\/del>/g, "");
  563. if (text !== delText) {
  564. html = "<del>" + html + "</del>";
  565. }
  566. list[i].innerHTML = html;
  567. } else if (text === delText) {
  568. html = html.replace(/<del>/g, "");
  569. html = html.replace(/<\/del>/g, "");
  570. list[i].innerHTML = html;
  571. }
  572. }
  573. },
  574. createLink: (e, domId) => {
  575. let doc = document.getElementById(domId + "-iframe").contentDocument;
  576. let win = document.getElementById(domId + "-iframe").contentWindow;
  577. let html = null,
  578. initUrl = "",
  579. initType = "_blank";
  580. if (win.getSelection) {
  581. //一般浏览器
  582. let dom = win.getSelection().getRangeAt(0).cloneContents();
  583. let div1 = document.createElement("div");
  584. document.body.appendChild(div1);
  585. div1.appendChild(dom);
  586. let aaaPar = win.getSelection().anchorNode.parentElement;
  587. while (
  588. aaaPar.tagName !== "A" &&
  589. aaaPar.tagName !== "BODY" &&
  590. aaaPar.tagName !== "HTML"
  591. ) {
  592. aaaPar = aaaPar.parentElement;
  593. }
  594. let href = aaaPar.href;
  595. let type = aaaPar.target;
  596. if (href !== undefined) {
  597. initUrl = href;
  598. }
  599. if (type === "_self") {
  600. initType = "_self";
  601. }
  602. html = div1.innerHTML;
  603. document.body.removeChild(div1);
  604. } else if (doc.selection) {
  605. //IE浏览器、Opera
  606. html = doc.selection.createRange().htmlText;
  607. }
  608. if (html === undefined || html === "") {
  609. return;
  610. }
  611. let ele = `<article style="width:300px;margin:25% auto 0;padding:16px;background:#fff;border:1px solid #a0a0a0;border-radius:10px;">
  612. <section>
  613. <label for="${domId}linkUrl">URL</label>
  614. <input type="text" id="${domId}linkUrl" style="width:250px;" value="${initUrl}" />
  615. <div style="color:#666666;font-size:12px;">输入url;若设置指向锚点,请输入:#锚点名称</div>
  616. </section>
  617. <section style="width:150px;margin:10px 0 0;">
  618. <label for="${domId}linkType">打开类型</label>
  619. <select id="${domId}linkType" >
  620. <option value="_blank" >新窗口</option>
  621. <option value="_self" >当前窗口</option>
  622. </select>
  623. </section>
  624. <section style="width:150px;display:flex;margin:10px auto 0;justify-content:space-around;">
  625. <div id="${domId}linkUrlConfirm" style="width:50px;height:26px;line-height:26px;text-align:center;border:1px solid #a0a0a0;border-radius:10px;cursor:pointer;">确定</div>
  626. <div id="${domId}linkUrlCancel" style="width:50px;height:26px;line-height:26px;text-align:center;border:1px solid #a0a0a0;border-radius:10px;cursor:pointer;">取消</div>
  627. </section>
  628. </article>`;
  629. let div = document.createElement("div");
  630. div.id = `${domId}createLink`;
  631. div.style.cssText =
  632. "width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999;background:rgba(255,255,255,0.6);";
  633. document.body.appendChild(div);
  634. div.innerHTML = ele;
  635. document.getElementById(`${domId}linkType`).value = initType;
  636. document.getElementById(`${domId}linkUrlConfirm`).onclick = (e) => {
  637. let val = document.getElementById(`${domId}linkUrl`).value;
  638. if (val === "") {
  639. alert("请输入URL");
  640. return;
  641. }
  642. let type = document.getElementById(`${domId}linkType`).value;
  643. doc.body.focus();
  644. doc.execCommand("createLink", false, val);
  645. document.body.removeChild(div);
  646. let list = doc.getElementsByTagName("a");
  647. for (let i = 0; i < list.length; i++) {
  648. let target = list[i].target;
  649. if (target === "" && list[i].href !== "") {
  650. list[i].target = type;
  651. }
  652. }
  653. // let html2 = null;
  654. // if (win.getSelection) {
  655. // //一般浏览器
  656. // let dom2 = win.getSelection().getRangeAt(0).cloneContents(); //win.getSelection().anchorNode.parentElement
  657. // let div2 = document.createElement("div");
  658. // document.body.appendChild(div2);
  659. // div2.appendChild(dom2);
  660. // html2 = div2.innerHTML;
  661. // document.body.removeChild(div2);
  662. // } else if (doc.selection) {
  663. // //IE浏览器、Opera
  664. // html2 = doc.selection.createRange().htmlText;
  665. // }
  666. // if (html2 === undefined) {
  667. // return;
  668. // }
  669. // html2 = html2.replace(/<a/g, `<a target="_blank"`);
  670. // doc.execCommand("delete");
  671. // doc.execCommand("insertHTML", false, html2);
  672. };
  673. document.getElementById(`${domId}linkUrlCancel`).onclick = (e) => {
  674. document.body.removeChild(div);
  675. };
  676. },
  677. unlink: (e, domId) => {
  678. let doc = document.getElementById(domId + "-iframe").contentDocument;
  679. doc.body.focus();
  680. doc.execCommand("unlink");
  681. },
  682. anchorPoint: (e, domId) => {
  683. let doc = document.getElementById(domId + "-iframe").contentDocument;
  684. let win = document.getElementById(domId + "-iframe").contentWindow;
  685. let html = null,
  686. initUrl = "";
  687. if (win.getSelection) {
  688. //一般浏览器
  689. let dom = win.getSelection().getRangeAt(0).cloneContents();
  690. let div1 = document.createElement("div");
  691. document.body.appendChild(div1);
  692. div1.appendChild(dom);
  693. let aaaPar = win.getSelection().anchorNode.parentElement;
  694. while (
  695. aaaPar.tagName !== "A" &&
  696. aaaPar.tagName !== "BODY" &&
  697. aaaPar.tagName !== "HTML"
  698. ) {
  699. aaaPar = aaaPar.parentElement;
  700. }
  701. let name = aaaPar.name;
  702. if (name !== undefined) {
  703. initUrl = name;
  704. }
  705. html = div1.innerHTML;
  706. document.body.removeChild(div1);
  707. } else if (doc.selection) {
  708. //IE浏览器、Opera
  709. html = doc.selection.createRange().htmlText;
  710. }
  711. if (html === undefined || html === "") {
  712. return;
  713. }
  714. let ele = `<article style="width:300px;margin:25% auto 0;padding:16px;background:#fff;border:1px solid #a0a0a0;border-radius:10px;">
  715. <section>
  716. <label for="${domId}anchorPointName">锚点名称</label>
  717. <input type="text" id="${domId}anchorPointName" style="width:250px;" value="${initUrl}" />
  718. </section>
  719. <section style="width:150px;display:flex;margin:10px auto 0;justify-content:space-around;">
  720. <div id="${domId}anchorPointConfirm" style="width:50px;height:26px;line-height:26px;text-align:center;border:1px solid #a0a0a0;border-radius:10px;cursor:pointer;">确定</div>
  721. <div id="${domId}anchorPointCancel" style="width:50px;height:26px;line-height:26px;text-align:center;border:1px solid #a0a0a0;border-radius:10px;cursor:pointer;">取消</div>
  722. </section>
  723. </article>`;
  724. let div = document.createElement("div");
  725. div.id = `${domId}anchorPoint`;
  726. div.style.cssText =
  727. "width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999;background:rgba(255,255,255,0.6);";
  728. document.body.appendChild(div);
  729. div.innerHTML = ele;
  730. document.getElementById(`${domId}anchorPointConfirm`).onclick = (e) => {
  731. let val = document.getElementById(`${domId}anchorPointName`).value;
  732. if (val === "") {
  733. alert("请输入锚点名称");
  734. return;
  735. }
  736. doc.body.focus();
  737. doc.execCommand("createLink", false, domId + "javascript:;");
  738. // let dom = win.getSelection().getRangeAt(0).cloneContents();
  739. //range.extractContents().textContent获取的是选区中纯文本内容,range.deleteContents()删除选取内容,
  740. //range.insertNode(span)插入加有行距的内容
  741. // let dom2 = win.getSelection().anchorNode.parentElement;
  742. //win.getSelection().toString()
  743. let list = doc.getElementsByTagName("a");
  744. for (let i = 0; i < list.length; i++) {
  745. let href = list[i].href;
  746. if (href === domId + "javascript:;") {
  747. list[i].removeAttribute("href");
  748. list[i].name = val;
  749. //list[i].style.cssText = "border:1px solid #1989fa;border-radius:4px;";
  750. }
  751. }
  752. document.body.removeChild(div);
  753. };
  754. document.getElementById(`${domId}anchorPointCancel`).onclick = (e) => {
  755. document.body.removeChild(div);
  756. };
  757. },
  758. insertHorizontalRule: (e, domId) => {
  759. let doc = document.getElementById(domId + "-iframe").contentDocument;
  760. doc.body.focus();
  761. doc.execCommand("insertHorizontalRule");
  762. },
  763. insertHorizontalRuleDashed: (e, domId) => {
  764. let doc = document.getElementById(domId + "-iframe").contentDocument;
  765. doc.body.focus();
  766. let html = `<hr style="page-break-after:always;height:2px;border:1px dotted #AAA;" />`;
  767. doc.execCommand("insertHTML", false, html);
  768. },
  769. doLineHeight: (e, domId) => {
  770. let mySelf = document.getElementById(`${domId}clickRemove`);
  771. if (mySelf) {
  772. mySelf.parentElement.removeChild(mySelf);
  773. }
  774. let doc = document.getElementById(domId + "-iframe").contentDocument;
  775. let win = document.getElementById(domId + "-iframe").contentWindow;
  776. // 获取当前的选区
  777. let html = null;
  778. if (win.getSelection) {
  779. //一般浏览器
  780. let dom = win.getSelection().getRangeAt(0).cloneContents();
  781. let div1 = document.createElement("div");
  782. document.body.appendChild(div1);
  783. div1.appendChild(dom);
  784. html = div1.innerHTML;
  785. document.body.removeChild(div1);
  786. } else if (doc.selection) {
  787. //IE浏览器、Opera
  788. html = doc.selection.createRange().htmlText;
  789. }
  790. if (html === undefined || html === "") {
  791. return;
  792. }
  793. e.target.style.position = "relative";
  794. let div = `<article id="${domId}clickRemove"
  795. style="width:150px;height:98px;line-height:1.5;overflow:auto;padding:4px;background:#fff;position:absolute;bottom:0;left:0;z-index:99999;font-size:12px;border:1px solid #a0a0a0;box-shadow:1px 1px 3px #a0a0a0;">
  796. <div id="${domId}doLineHeight10" >单倍行距</div>
  797. <div id="${domId}doLineHeight12" >1.2倍行距</div>
  798. <div id="${domId}doLineHeight15" >1.5倍行距</div>
  799. <div id="${domId}doLineHeight18" >1.8倍行距</div>
  800. <div id="${domId}doLineHeight20" >2倍行距</div>
  801. </article>`;
  802. e.target.innerHTML = div;
  803. doc.onclick = (e) => {
  804. let self = document.getElementById(`${domId}clickRemove`);
  805. if (self) {
  806. self.parentElement.removeChild(self);
  807. }
  808. };
  809. document.body.onclick = (e) => {
  810. e.stopPropagation();
  811. if (e.target.dataset.type !== "doLineHeight") {
  812. let id = e.target.id;
  813. let index = id.indexOf(`${domId}doLineHeight`);
  814. if (index === 0) {
  815. let newId = id.replace(`${domId}doLineHeight`, "");
  816. let value = newId / 10;
  817. doc.body.focus();
  818. let spanList0 = doc.getElementsByTagName("span");
  819. let aList0 = doc.getElementsByTagName("a");
  820. let list0 = [...spanList0, ...aList0].filter((item) => {
  821. return item.style.zIndex === "891314";
  822. });
  823. let objId = {};
  824. for (let i = 0; i < list0.length; i++) {
  825. let id = list0[i].id;
  826. objId[id] = list0[i].style.fontSize;
  827. }
  828. doc.execCommand("fontSize", false, 3);
  829. let spanList = doc.getElementsByTagName("span");
  830. let aList = doc.getElementsByTagName("a");
  831. let list = [...spanList, ...aList];
  832. for (let i = 0; i < list.length; i++) {
  833. let size = list[i].style.fontSize;
  834. let zIndex = list[i].style.zIndex;
  835. if (size === "medium") {
  836. list[i].style.fontSize = "";
  837. list[i].style.lineHeight = value;
  838. }
  839. if (zIndex === "891314") {
  840. let id = list[i].id;
  841. list[i].style.fontSize = objId[id];
  842. }
  843. }
  844. }
  845. let self = document.getElementById(`${domId}clickRemove`);
  846. if (self) {
  847. self.parentElement.removeChild(self);
  848. }
  849. }
  850. };
  851. return;
  852. // let range = selection.getRangeAt(0);
  853. // // 选中区域的html
  854. // let outSpan = document.createElement("span");
  855. // outSpan.style.cssText = "line-height: " + value + ";display: inline-block;";
  856. // outSpan.appendChild(range.cloneContents());
  857. // // 设置指定行距
  858. // function setLineHeight(content, value) {
  859. // let reg = /line-height\s*:\s*(\d+(\.*\d+)?)/gi;
  860. // let c = content.replace(reg, "line-height: " + value);
  861. // return c;
  862. // }
  863. // let content = setLineHeight(outSpan.innerHTML, value);
  864. // // 选取中的纯文本
  865. // let txt = range.extractContents().textContent;
  866. // let length = txt.length;
  867. // if (length === 0) {
  868. // return;
  869. // }
  870. // // 删除被选择的内容
  871. // range.deleteContents();
  872. // // 创建新的元素
  873. // let span = document.createElement("span");
  874. // span.style.cssText = "line-height: " + value + ";display: inline-block;";
  875. // // 设置 span 内容
  876. // span.innerHTML = content;
  877. // // 在被选择的位置设置行距的元素
  878. // range.insertNode(span);
  879. },
  880. doTable: (e, domId) => {
  881. let doc = document.getElementById(domId + "-iframe").contentDocument;
  882. //let win = document.getElementById(domId + "-iframe").contentWindow;
  883. let ele = `<article style="width:480px;margin:25% auto 0;padding:16px;background:#fff;border:1px solid #a0a0a0;border-radius:10px;">
  884. <section>
  885. <label>单元格数&emsp;</label>
  886. <label for="${domId}doTableRow">行数</label>
  887. <input type="text" id="${domId}doTableRow" style="width:80px;" value="3" />
  888. <label for="${domId}doTableCol">列数</label>
  889. <input type="text" id="${domId}doTableCol" style="width:80px;" value="2" />
  890. </section>
  891. <section style="margin:10px 0 0;">
  892. <label>大小&emsp;&emsp;&emsp;</label>
  893. <label for="${domId}doTableWidth">宽度</label>
  894. <input type="text" id="${domId}doTableWidth" style="width:80px;" value="100" />
  895. <select id="${domId}doTableWidthType" >
  896. <option value="%" >%</option>
  897. <option value="px" >px</option>
  898. </select>
  899. <label for="${domId}doTableHeight">高度</label>
  900. <input type="text" id="${domId}doTableHeight" style="width:80px;" value="" />
  901. <select id="${domId}doTableHeightType" >
  902. <option value="%" >%</option>
  903. <option value="px" >px</option>
  904. </select>
  905. </section>
  906. <section style="margin:10px 0 0;">
  907. <label>边距间距&emsp;</label>
  908. <label for="${domId}doTablePadding">边距</label>
  909. <input type="text" id="${domId}doTablePadding" style="width:80px;" value="2" />
  910. <label for="${domId}doTableSpacing">间距</label>
  911. <input type="text" id="${domId}doTableSpacing" style="width:80px;" value="0" />
  912. </section>
  913. <section style="margin:10px 0 0;">
  914. <label>对齐方式&emsp;</label>
  915. <label for="${domId}doTableAlignType">边距</label>
  916. <select id="${domId}doTableAlignType" >
  917. <option value="" >默认</option>
  918. <option value="left" >左对齐</option>
  919. <option value="center" >居中</option>
  920. <option value="right" >右对齐</option>
  921. </select>
  922. <section style="margin:10px 0 0;" id="${domId}doTableBorder">
  923. <label>表格边框&emsp;</label>
  924. <label for="${domId}doTableBorderWidth">边框</label>
  925. <input type="text" id="${domId}doTableBorderWidth" style="width:80px;" value="1" />
  926. <label >颜色</label>
  927. <input type="radio" name="borderRadio" value="0" />无颜色
  928. <input type="radio" name="borderRadio" value="1" checked="true" />有颜色
  929. <input type="color" id="${domId}doTableBorderColor" style="width:80px;" value="#000000" />
  930. </section>
  931. <section style="margin:10px 0 0;" id="${domId}doTableBackground">
  932. <label >背景颜色&emsp;</label>
  933. <input type="radio" name="backRadio" value="0" checked="true" />无颜色
  934. <input type="radio" name="backRadio" value="1" />有颜色
  935. <input type="color" id="${domId}doTableBackgroundColor" style="width:80px;" value="#ffffff" />
  936. </section>
  937. <section style="width:150px;display:flex;margin:10px auto 0;justify-content:space-around;">
  938. <div id="${domId}doTableConfirm" style="width:50px;height:26px;line-height:26px;text-align:center;border:1px solid #a0a0a0;border-radius:10px;cursor:pointer;">确定</div>
  939. <div id="${domId}doTableCancel" style="width:50px;height:26px;line-height:26px;text-align:center;border:1px solid #a0a0a0;border-radius:10px;cursor:pointer;">取消</div>
  940. </section>
  941. </article>`;
  942. let div = document.createElement("div");
  943. div.id = `${domId}doTable`;
  944. div.style.cssText =
  945. "width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999;background:rgba(255,255,255,0.6);";
  946. document.body.appendChild(div);
  947. div.innerHTML = ele;
  948. document.getElementById(`${domId}doTableWidthType`).value = "%";
  949. document.getElementById(`${domId}doTableHeightType`).value = "%";
  950. document.getElementById(`${domId}doTableAlignType`).value = "";
  951. document.getElementById(`${domId}doTableConfirm`).onclick = (e) => {
  952. let rowNum = document.getElementById(`${domId}doTableRow`).value;
  953. let colNum = document.getElementById(`${domId}doTableCol`).value;
  954. let tWidth = document.getElementById(`${domId}doTableWidth`).value;
  955. let tWidthType = document.getElementById(
  956. `${domId}doTableWidthType`
  957. ).value;
  958. let tHeight = document.getElementById(`${domId}doTableHeight`).value;
  959. let tHeightType = document.getElementById(
  960. `${domId}doTableHeightType`
  961. ).value;
  962. let paddNum = document.getElementById(`${domId}doTablePadding`).value;
  963. let spacNum = document.getElementById(`${domId}doTableSpacing`).value;
  964. let alignType = document.getElementById(`${domId}doTableAlignType`).value;
  965. let borderNum = document.getElementById(
  966. `${domId}doTableBorderWidth`
  967. ).value;
  968. let borderColor = "";
  969. let checkVal1 = document
  970. .getElementById(`${domId}doTableBorder`)
  971. .querySelectorAll("input[name='borderRadio']:checked")[0].value;
  972. if (checkVal1 === "1") {
  973. borderColor = document.getElementById(
  974. `${domId}doTableBorderColor`
  975. ).value;
  976. }
  977. let backgroundColor = "";
  978. let checkVal2 = document
  979. .getElementById(`${domId}doTableBackground`)
  980. .querySelectorAll("input[name='backRadio']:checked")[0].value;
  981. if (checkVal2 === "1") {
  982. backgroundColor = document.getElementById(
  983. `${domId}doTableBackgroundColor`
  984. ).value;
  985. }
  986. if (!/^[1-9][0-9]{0,}$/.test(rowNum)) {
  987. alert("行数请输入大于0的整数");
  988. return;
  989. } else if (!/^[1-9][0-9]{0,}$/.test(colNum)) {
  990. alert("列数请输入大于0的整数");
  991. return;
  992. } else if (!/^[0-9]{0,}$/.test(tWidth) && tWidth !== "") {
  993. alert("宽度请输入数字");
  994. return;
  995. } else if (!/^[0-9]{0,}$/.test(tHeight) && tHeight !== "") {
  996. alert("高度请输入数字");
  997. return;
  998. } else if (!/^[0-9]{0,}$/.test(paddNum) && paddNum !== "") {
  999. alert("边距请输入数字");
  1000. return;
  1001. } else if (!/^[0-9]{0,}$/.test(spacNum) && spacNum !== "") {
  1002. alert("间距请输入数字");
  1003. return;
  1004. } else if (!/^[0-9]{0,}$/.test(borderNum) && borderNum !== "") {
  1005. alert("边框请输入数字");
  1006. return;
  1007. }
  1008. let html = `<table style="${
  1009. tWidth !== "" ? "width:" + tWidth + tWidthType + ";" : ""
  1010. }${tHeight !== "" ? "height:" + tHeight + tHeightType + ";" : ""}${
  1011. backgroundColor !== ""
  1012. ? "background-color:" + backgroundColor + ";"
  1013. : ""
  1014. }"
  1015. ${alignType !== "" ? 'align="' + alignType + '"' : ""}
  1016. ${paddNum !== "" ? 'cellpadding="' + paddNum + '"' : ""}
  1017. ${spacNum !== "" ? 'cellspacing="' + spacNum + '"' : ""}
  1018. ${borderNum !== "" ? 'border="' + borderNum + '"' : ""}
  1019. ${borderColor !== "" ? 'bordercolor="' + borderColor + '"' : ""}
  1020. >`;
  1021. html += "<tbody>";
  1022. for (let i = 0; i < rowNum; i++) {
  1023. html += "<tr>";
  1024. for (let col = 0; col < colNum; col++) {
  1025. html += "<td><br/></td>";
  1026. }
  1027. html += "</tr>";
  1028. }
  1029. html += "</tbody>";
  1030. html += "</table>";
  1031. html += "<br/>";
  1032. doc.body.focus();
  1033. doc.execCommand("insertHTML", false, html);
  1034. document.body.removeChild(div);
  1035. };
  1036. document.getElementById(`${domId}doTableCancel`).onclick = (e) => {
  1037. document.body.removeChild(div);
  1038. };
  1039. },
  1040. insertImage: (e, domId) => {
  1041. let doc = document.getElementById(domId + "-iframe").contentDocument;
  1042. //let win = document.getElementById(domId + "-iframe").contentWindow;
  1043. //accept="image/gif, image/jpeg"
  1044. let ele = `<article style="width:300px;margin:25% auto 0;padding:16px;background:#fff;border:1px solid #a0a0a0;border-radius:10px;">
  1045. <section>
  1046. <label for="${domId}insertImageFile">选择图片</label>
  1047. <input type="file" accept="image/*" placeholder="请上传图片" id="${domId}insertImageFile" style="width:250px;" />
  1048. </section>
  1049. <section style="width:150px;display:flex;margin:10px auto 0;justify-content:space-around;">
  1050. <div id="${domId}insertImageConfirm" style="width:50px;height:26px;line-height:26px;text-align:center;border:1px solid #a0a0a0;border-radius:10px;cursor:pointer;">确定</div>
  1051. <div id="${domId}insertImageCancel" style="width:50px;height:26px;line-height:26px;text-align:center;border:1px solid #a0a0a0;border-radius:10px;cursor:pointer;">取消</div>
  1052. </section>
  1053. </article>`;
  1054. let div = document.createElement("div");
  1055. div.id = `${domId}insertImage`;
  1056. div.style.cssText =
  1057. "width:100%;height:100%;position:fixed;top:0;left:0;z-index:999999;background:rgba(255,255,255,0.6);";
  1058. document.body.appendChild(div);
  1059. div.innerHTML = ele;
  1060. let imgBase64 = "";
  1061. document.getElementById(`${domId}insertImageFile`).onchange = (el) => {
  1062. if (el.currentTarget.files.length === 0) {
  1063. return;
  1064. }
  1065. let reader = new FileReader();
  1066. reader.onload = (e) => {
  1067. imgBase64 = reader.result;
  1068. };
  1069. reader.readAsDataURL(el.target.files[0]);
  1070. //获取图片大小,字节
  1071. let fileMsg = el.currentTarget.files[0];
  1072. let fileType = fileMsg.type;
  1073. if (fileType.indexOf("image/") !== 0) {
  1074. alert("请上传图片!");
  1075. document.getElementById(`${domId}insertImageFile`).value = ""; //文件
  1076. }
  1077. // let fileSize = fileMsg.size;//不能超过2668484,大约为2.6MB
  1078. // if (fileSize > 2668484) {
  1079. // alert("选择的图像文件不能超过2.6MB,请重新选择!");
  1080. // document.getElementById(`${domId}insertImageFile`).value=""; //文件
  1081. // }
  1082. };
  1083. document.getElementById(`${domId}insertImageConfirm`).onclick = (e) => {
  1084. let val = document.getElementById(`${domId}insertImageFile`).value;
  1085. if (val === "") {
  1086. alert("请上传图片");
  1087. return;
  1088. }
  1089. doc.body.focus();
  1090. doc.execCommand("insertImage", false, imgBase64);
  1091. document.body.removeChild(div);
  1092. let imgList = doc.getElementsByTagName("img");
  1093. for (let i = 0; i < imgList.length; i++) {
  1094. imgList[i].style.objectFit = "contain";
  1095. imgList[i].style.maxWidth = "100%";
  1096. }
  1097. };
  1098. document.getElementById(`${domId}insertImageCancel`).onclick = (e) => {
  1099. document.body.removeChild(div);
  1100. };
  1101. },
  1102. setType: (e, domId) => {
  1103. let doc = document.getElementById(domId + "-iframe").contentDocument;
  1104. doc.body.focus();
  1105. //doc.execCommand("enableInlineTableEditing");
  1106. },
  1107. };
  1108. export default headerClickFun;