截流自动化的商城平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

likeedit.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. layui.define(["layer", "form", "layedit"], function (t) {
  2. var e = layui.$, i = layui.layer, a = layui.form, l = (layui.hint(), layui.device()), n = "layedit",
  3. o = "layui-show", r = "layui-disabled", layedit=layui.layedit, c = function () {
  4. var t = this;
  5. t.index = 0, t.config = {
  6. tool: ["strong", "italic", "underline", "del", "|", "left", "center", "right", "|", "link", "unlink", "face", "image"],
  7. hideTool: [],
  8. height: 500
  9. }
  10. };
  11. c.prototype.set = function (t) {
  12. var i = this;
  13. return e.extend(!0, i.config, t), i
  14. }, c.prototype.on = function (t, e) {
  15. return layui.onevent(n, t, e)
  16. }, c.prototype.build = function (t, i) {
  17. i = i || {};
  18. var a = this, n = a.config, r = "layui-layedit", c = e("string" == typeof t ? "#" + t : t),
  19. u = "LAY_layedit_" + ++a.index, d = c.next("." + r), y = e.extend({}, n, i), f = function () {
  20. var t = [], e = {};
  21. return layui.each(y.hideTool, function (t, i) {
  22. e[i] = !0
  23. }), layui.each(y.tool, function (i, a) {
  24. C[a] && !e[a] && t.push(C[a])
  25. }), t.join("")
  26. }(),
  27. m = e(['<div class="' + r + '">', '<div class="layui-unselect layui-layedit-tool">' + f + "</div>", '<div class="layui-layedit-iframe">', '<iframe id="' + u + '" name="' + u + '" textarea="' + t + '" frameborder="0"></iframe>', "</div>", "</div>"].join(""));
  28. return l.ie && l.ie < 8 ? c.removeClass("layui-hide").addClass(o) : (d[0] && d.remove(), s.call(a, m, c[0], y), c.addClass("layui-hide").after(m), a.index)
  29. }, c.prototype.getContent = function (t) {
  30. var e = u(t);
  31. if (e[0]) {
  32. return d(e[0].document.body.innerHTML)
  33. }
  34. }, c.prototype.getText = function (t) {
  35. var i = u(t);
  36. if (i[0]) {
  37. return e(i[0].document.body).text()
  38. }
  39. }, c.prototype.setContent = function (t, i, a) {
  40. var l = u(t);
  41. l[0] && (a ? e(l[0].document.body).append(i) : e(l[0].document.body).html(i), layedit.sync(t))
  42. }, c.prototype.sync = function (t) {
  43. var i = u(t);
  44. if (i[0]) {
  45. var a = e("#" + i[1].attr("textarea"));
  46. a.val(d(i[0].document.body.innerHTML))
  47. }
  48. }, c.prototype.getSelection = function (t) {
  49. var e = u(t);
  50. if (e[0]) {
  51. var i = m(e[0].document);
  52. return document.selection ? i.text : i.toString()
  53. }
  54. };
  55. var s = function (t, i, a) {
  56. var l = this, n = t.find("iframe");
  57. n.css({height: a.height}).on("load", function () {
  58. var o = n.contents(), r = n.prop("contentWindow"), c = o.find("head"),
  59. s = e(["<style>", "*{margin: 0; padding: 0;}", "body{padding: 10px; line-height: 20px; overflow-x: hidden; word-wrap: break-word; font: 14px Helvetica Neue,Helvetica,PingFang SC,Microsoft YaHei,Tahoma,Arial,sans-serif; -webkit-box-sizing: border-box !important; -moz-box-sizing: border-box !important; box-sizing: border-box !important;}", "a{color:#01AAED; text-decoration:none;}a:hover{color:#c00}", "p{margin-bottom: 10px;}", "img{display: inline-block; border: none; vertical-align: middle;}", "pre{margin: 10px 0; padding: 10px; line-height: 20px; border: 1px solid #ddd; border-left-width: 6px; background-color: #F2F2F2; color: #333; font-family: Courier New; font-size: 12px;}", "</style>"].join("")),
  60. u = o.find("body");
  61. c.append(s), u.attr("contenteditable", "true").css({"min-height": a.height}).html(i.value || ""), y.apply(l, [r, n, i, a]), g.call(l, r, t, a)
  62. })
  63. }, u = function (t) {
  64. var i = e("#LAY_layedit_" + t), a = i.prop("contentWindow");
  65. return [a, i]
  66. }, d = function (t) {
  67. return 8 == l.ie && (t = t.replace(/<.+>/g, function (t) {
  68. return t.toLowerCase()
  69. })), t
  70. }, y = function (t, a, n, o) {
  71. var r = t.document, c = e(r.body);
  72. c.on("keydown", function (t) {
  73. var e = t.keyCode;
  74. if (13 === e) {
  75. var a = m(r), l = p(a), n = l.parentNode;
  76. if ("pre" === n.tagName.toLowerCase()) {
  77. if (t.shiftKey) {
  78. return
  79. }
  80. return i.msg("请暂时用shift+enter"), !1
  81. }
  82. r.execCommand("formatBlock", !1, "<p>")
  83. }
  84. }), e(n).parents("form").on("submit", function () {
  85. var t = c.html();
  86. 8 == l.ie && (t = t.replace(/<.+>/g, function (t) {
  87. return t.toLowerCase()
  88. })), n.value = t
  89. }), c.on("paste", function (e) {
  90. r.execCommand("formatBlock", !1, "<p>"), setTimeout(function () {
  91. f.call(t, c), n.value = c.html()
  92. }, 100)
  93. })
  94. }, f = function (t) {
  95. var i = this;
  96. i.document;
  97. t.find("*[style]").each(function () {
  98. var t = this.style.textAlign;
  99. this.removeAttribute("style"), e(this).css({"text-align": t || ""})
  100. }), t.find("table").addClass("layui-table"), t.find("script,link").remove()
  101. }, m = function (t) {
  102. return t.selection ? t.selection.createRange() : t.getSelection().getRangeAt(0)
  103. }, p = function (t) {
  104. return t.endContainer || t.parentElement().childNodes[0]
  105. }, v = function (t, i, a) {
  106. var l = this.document, n = document.createElement(t);
  107. for (var o in i) {
  108. n.setAttribute(o, i[o])
  109. }
  110. if (n.removeAttribute("text"), l.selection) {
  111. var r = a.text || i.text;
  112. if ("a" === t && !r) {
  113. return
  114. }
  115. r && (n.innerHTML = r), a.pasteHTML(e(n).prop("outerHTML")), a.select()
  116. } else {
  117. var r = a.toString() || i.text;
  118. if ("a" === t && !r) {
  119. return
  120. }
  121. // r && (n.innerHTML = r), a.deleteContents(), a.insertNode(n) //这句是原来的代码, 会删除节点
  122. r && (n.innerHTML = r), a.insertNode(n)
  123. }
  124. }, h = function (t, i) {
  125. var a = this.document, l = "layedit-tool-active", n = p(m(a)), o = function (e) {
  126. return t.find(".layedit-tool-" + e)
  127. };
  128. i && i[i.hasClass(l) ? "removeClass" : "addClass"](l), t.find(">i").removeClass(l), o("unlink").addClass(r), e(n).parents().each(function () {
  129. var t = this.tagName.toLowerCase(), e = this.style.textAlign;
  130. "b" !== t && "strong" !== t || o("b").addClass(l), "i" !== t && "em" !== t || o("i").addClass(l), "u" === t && o("u").addClass(l), "strike" === t && o("d").addClass(l), "p" === t && ("center" === e ? o("center").addClass(l) : "right" === e ? o("right").addClass(l) : o("left").addClass(l)), "a" === t && (o("link").addClass(l), o("unlink").removeClass(r))
  131. })
  132. }, g = function (t, a, l) {
  133. var n = t.document, o = e(n.body), c = {
  134. link: function (i) {
  135. var a = p(i), l = e(a).parent();
  136. b.call(o, {href: l.attr("href"), target: l.attr("target")}, function (e) {
  137. var a = l[0];
  138. "A" === a.tagName ? a.href = e.url : v.call(t, "a", {target: e.target, href: e.url, text: e.url}, i)
  139. })
  140. }, unlink: function (t) {
  141. n.execCommand("unlink")
  142. }, face: function (e) {
  143. x.call(this, function (i) {
  144. v.call(t, "img", {src: i.src, alt: i.alt}, e)
  145. })
  146. }, image: function (a) {
  147. var n = this;
  148. var uploadImage = l.uploadImage;
  149. var windows = layer.open({type: 2, title: "上传图片", content: uploadImage.url, area: ["90%", "90%"]});
  150. window.callback = function (uri) {
  151. uri.forEach(function (item) {
  152. v.call(t, "img", {src: item}, a)
  153. });
  154. };
  155. window.callbackSetUri = function (uri) {
  156. v.call(t, "img", {src: "/" + uri}, a);
  157. layer.close(windows)
  158. }
  159. }, code: function (e) {
  160. k.call(o, function (i) {
  161. v.call(t, "pre", {text: i.code, "lay-lang": i.lang}, e)
  162. })
  163. }, help: function () {
  164. i.open({
  165. type: 2,
  166. title: "帮助",
  167. area: ["600px", "380px"],
  168. shadeClose: !0,
  169. shade: 0.1,
  170. skin: "layui-layer-msg",
  171. content: ["http://www.layui.com/about/layedit/help.html", "no"]
  172. })
  173. }
  174. }, s = a.find(".layui-layedit-tool"), u = function () {
  175. var i = e(this), a = i.attr("layedit-event"), l = i.attr("lay-command");
  176. if (!i.hasClass(r)) {
  177. o.focus();
  178. var u = m(n);
  179. u.commonAncestorContainer;
  180. l ? (n.execCommand(l), /justifyLeft|justifyCenter|justifyRight/.test(l) && n.execCommand("formatBlock", !1, "<p>"), setTimeout(function () {
  181. o.focus()
  182. }, 10)) : c[a] && c[a].call(this, u), h.call(t, s, i)
  183. }
  184. }, d = /image/;
  185. s.find(">i").on("mousedown", function () {
  186. var t = e(this), i = t.attr("layedit-event");
  187. d.test(i) || u.call(this)
  188. }).on("click", function () {
  189. var t = e(this), i = t.attr("layedit-event");
  190. d.test(i) && u.call(this)
  191. }), o.on("click", function () {
  192. h.call(t, s), i.close(x.index)
  193. })
  194. }, b = function (t, e) {
  195. var l = this, n = i.open({
  196. type: 1,
  197. id: "LAY_layedit_link",
  198. area: "350px",
  199. shade: 0.05,
  200. shadeClose: !0,
  201. moveType: 1,
  202. title: "超链接",
  203. skin: "layui-layer-msg",
  204. content: ['<ul class="layui-form" style="margin: 15px;">', '<li class="layui-form-item">', '<label class="layui-form-label" style="width: 60px;">URL</label>', '<div class="layui-input-block" style="margin-left: 90px">', '<input name="url" lay-verify="url" value="' + (t.href || "") + '" autofocus="true" autocomplete="off" class="layui-input">', "</div>", "</li>", '<li class="layui-form-item">', '<label class="layui-form-label" style="width: 60px;">打开方式</label>', '<div class="layui-input-block" style="margin-left: 90px">', '<input type="radio" name="target" value="_self" class="layui-input" title="当前窗口"' + ("_self" !== t.target && t.target ? "" : "checked") + ">", '<input type="radio" name="target" value="_blank" class="layui-input" title="新窗口" ' + ("_blank" === t.target ? "checked" : "") + ">", "</div>", "</li>", '<li class="layui-form-item" style="text-align: center;">', '<button type="button" lay-submit lay-filter="layedit-link-yes" class="layui-btn"> 确定 </button>', '<button style="margin-left: 20px;" type="button" class="layui-btn layui-btn-primary"> 取消 </button>', "</li>", "</ul>"].join(""),
  205. success: function (t, n) {
  206. var o = "submit(layedit-link-yes)";
  207. a.render("radio"), t.find(".layui-btn-primary").on("click", function () {
  208. i.close(n), l.focus()
  209. }), a.on(o, function (t) {
  210. i.close(b.index), e && e(t.field)
  211. })
  212. }
  213. });
  214. b.index = n
  215. }, x = function (t) {
  216. var a = function () {
  217. var t = ["[微笑]", "[嘻嘻]", "[哈哈]", "[可爱]", "[可怜]", "[挖鼻]", "[吃惊]", "[害羞]", "[挤眼]", "[闭嘴]", "[鄙视]", "[爱你]", "[泪]", "[偷笑]", "[亲亲]", "[生病]", "[太开心]", "[白眼]", "[右哼哼]", "[左哼哼]", "[嘘]", "[衰]", "[委屈]", "[吐]", "[哈欠]", "[抱抱]", "[怒]", "[疑问]", "[馋嘴]", "[拜拜]", "[思考]", "[汗]", "[困]", "[睡]", "[钱]", "[失望]", "[酷]", "[色]", "[哼]", "[鼓掌]", "[晕]", "[悲伤]", "[抓狂]", "[黑线]", "[阴险]", "[怒骂]", "[互粉]", "[心]", "[伤心]", "[猪头]", "[熊猫]", "[兔子]", "[ok]", "[耶]", "[good]", "[NO]", "[赞]", "[来]", "[弱]", "[草泥马]", "[神马]", "[囧]", "[浮云]", "[给力]", "[围观]", "[威武]", "[奥特曼]", "[礼物]", "[钟]", "[话筒]", "[蜡烛]", "[蛋糕]"],
  218. e = {};
  219. return layui.each(t, function (t, i) {
  220. e[i] = layui.cache.dir + "images/face/" + t + ".gif"
  221. }), e
  222. }();
  223. return x.hide = x.hide || function (t) {
  224. "face" !== e(t.target).attr("layedit-event") && i.close(x.index)
  225. }, x.index = i.tips(function () {
  226. var t = [];
  227. return layui.each(a, function (e, i) {
  228. t.push('<li title="' + e + '"><img src="' + i + '" alt="' + e + '"></li>')
  229. }), '<ul class="layui-clear">' + t.join("") + "</ul>"
  230. }(), this, {
  231. tips: 1, time: 0, skin: "layui-box layui-util-face", maxWidth: 500, success: function (l, n) {
  232. l.css({marginTop: -4, marginLeft: -10}).find(".layui-clear>li").on("click", function () {
  233. t && t({src: a[this.title], alt: this.title}), i.close(n)
  234. }), e(document).off("click", x.hide).on("click", x.hide)
  235. }
  236. })
  237. }, k = function (t) {
  238. var e = this, l = i.open({
  239. type: 1,
  240. id: "LAY_layedit_code",
  241. area: "550px",
  242. shade: 0.05,
  243. shadeClose: !0,
  244. moveType: 1,
  245. title: "插入代码",
  246. skin: "layui-layer-msg",
  247. content: ['<ul class="layui-form layui-form-pane" style="margin: 15px;">', '<li class="layui-form-item">', '<label class="layui-form-label">请选择语言</label>', '<div class="layui-input-block">', '<select name="lang">', '<option value="JavaScript">JavaScript</option>', '<option value="HTML">HTML</option>', '<option value="CSS">CSS</option>', '<option value="Java">Java</option>', '<option value="PHP">PHP</option>', '<option value="C#">C#</option>', '<option value="Python">Python</option>', '<option value="Ruby">Ruby</option>', '<option value="Go">Go</option>', "</select>", "</div>", "</li>", '<li class="layui-form-item layui-form-text">', '<label class="layui-form-label">代码</label>', '<div class="layui-input-block">', '<textarea name="code" lay-verify="required" autofocus="true" class="layui-textarea" style="height: 200px;"></textarea>', "</div>", "</li>", '<li class="layui-form-item" style="text-align: center;">', '<button type="button" lay-submit lay-filter="layedit-code-yes" class="layui-btn"> 确定 </button>', '<button style="margin-left: 20px;" type="button" class="layui-btn layui-btn-primary"> 取消 </button>', "</li>", "</ul>"].join(""),
  248. success: function (l, n) {
  249. var o = "submit(layedit-code-yes)";
  250. a.render("select"), l.find(".layui-btn-primary").on("click", function () {
  251. i.close(n), e.focus()
  252. }), a.on(o, function (e) {
  253. i.close(k.index), t && t(e.field)
  254. })
  255. }
  256. });
  257. k.index = l
  258. }, C = {
  259. html: '<i class="layui-icon layedit-tool-html" title="HTML源代码" lay-command="html" layedit-event="html"">&#xe64b;</i><span class="layedit-tool-mid"></span>',
  260. strong: '<i class="layui-icon layedit-tool-b" title="加粗" lay-command="Bold" layedit-event="b"">&#xe62b;</i>',
  261. italic: '<i class="layui-icon layedit-tool-i" title="斜体" lay-command="italic" layedit-event="i"">&#xe644;</i>',
  262. underline: '<i class="layui-icon layedit-tool-u" title="下划线" lay-command="underline" layedit-event="u"">&#xe646;</i>',
  263. del: '<i class="layui-icon layedit-tool-d" title="删除线" lay-command="strikeThrough" layedit-event="d"">&#xe64f;</i>',
  264. "|": '<span class="layedit-tool-mid"></span>',
  265. left: '<i class="layui-icon layedit-tool-left" title="左对齐" lay-command="justifyLeft" layedit-event="left"">&#xe649;</i>',
  266. center: '<i class="layui-icon layedit-tool-center" title="居中对齐" lay-command="justifyCenter" layedit-event="center"">&#xe647;</i>',
  267. right: '<i class="layui-icon layedit-tool-right" title="右对齐" lay-command="justifyRight" layedit-event="right"">&#xe648;</i>',
  268. link: '<i class="layui-icon layedit-tool-link" title="插入链接" layedit-event="link"">&#xe64c;</i>',
  269. unlink: '<i class="layui-icon layedit-tool-unlink layui-disabled" title="清除链接" lay-command="unlink" layedit-event="unlink"">&#xe64d;</i>',
  270. face: '<i class="layui-icon layedit-tool-face" title="表情" layedit-event="face"">&#xe650;</i>',
  271. image: '<i class="layui-icon layedit-tool-image" title="图片" layedit-event="image">&#xe64a;</i>',
  272. code: '<i class="layui-icon layedit-tool-code" title="插入代码" layedit-event="code">&#xe64e;</i>',
  273. help: '<i class="layui-icon layedit-tool-help" title="帮助" layedit-event="help">&#xe607;</i>'
  274. }, w = new c;
  275. t("likeedit", w)
  276. });