|
|
/** layui-v2.5.6 MIT License By https://www.layui.com */
|
|
|
;layui.define(["layer","form"],function(t){"use strict";
|
|
|
var e=layui.$, // 引用 layui.$,即 jQuery
|
|
|
i=layui.layer, // 引用 layui.layer,弹出层
|
|
|
a=layui.form, // 引用 layui.form,表单组件
|
|
|
l=(layui.hint(),layui.device()), // 引用 layui.hint() 和 layui.device(),设备信息
|
|
|
n="layedit", // 编辑器名称
|
|
|
o="layui-show", // 显示样式
|
|
|
r="layui-disabled", // 禁用样式
|
|
|
c=function(){ // 构造器,初始化配置
|
|
|
var t=this;
|
|
|
t.index=0; // 编辑器索引
|
|
|
t.config={ // 编辑器默认配置
|
|
|
tool:["strong","italic","underline","del","|","left","center","right","|","link","unlink","face","image"], // 工具栏按钮
|
|
|
hideTool:[], // 隐藏的工具
|
|
|
height:280 // 编辑框高度
|
|
|
}
|
|
|
};
|
|
|
c.prototype.set=function(t){ // 配置方法
|
|
|
var i=this;
|
|
|
return e.extend(!0,i.config,t),i // 合并配置
|
|
|
},
|
|
|
c.prototype.on=function(t,e){ // 事件监听
|
|
|
return layui.onevent(n,t,e)
|
|
|
},
|
|
|
c.prototype.build=function(t,i){ // 构建编辑器
|
|
|
i=i||{};
|
|
|
var a=this,n=a.config,r="layui-layedit",c=e("string"==typeof t?"#"+t:t),u="LAY_layedit_"+ ++a.index,d=c.next("."+r),y=e.extend({},n,i),
|
|
|
f=function(){ // 工具栏显示设置
|
|
|
var t=[],e={};
|
|
|
return layui.each(y.hideTool,function(t,i){e[i]=!0}),
|
|
|
layui.each(y.tool,function(i,a){C[a]&&!e[a]&&t.push(C[a])}),t.join("")
|
|
|
}(),
|
|
|
m=e(['<div class="'+r+'">', // 编辑器HTML结构
|
|
|
'<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(""));
|
|
|
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)
|
|
|
},
|
|
|
c.prototype.getContent=function(t){ // 获取编辑框内容
|
|
|
var e=u(t);
|
|
|
if(e[0])return d(e[0].document.body.innerHTML)
|
|
|
},
|
|
|
c.prototype.getText=function(t){ // 获取纯文本
|
|
|
var i=u(t);
|
|
|
if(i[0])return e(i[0].document.body).text()
|
|
|
},
|
|
|
c.prototype.setContent=function(t,i,a){ // 设置编辑框内容
|
|
|
var l=u(t);
|
|
|
l[0]&&(a?e(l[0].document.body).append(i):e(l[0].document.body).html(i),layedit.sync(t))
|
|
|
},
|
|
|
c.prototype.sync=function(t){ // 同步内容
|
|
|
var i=u(t);
|
|
|
if(i[0]){var a=e("#"+i[1].attr("textarea"));
|
|
|
a.val(d(i[0].document.body.innerHTML))
|
|
|
}
|
|
|
},
|
|
|
c.prototype.getSelection=function(t){ // 获取当前选中的文本
|
|
|
var e=u(t);
|
|
|
if(e[0]){var i=m(e[0].document);
|
|
|
return document.selection?i.text:i.toString()
|
|
|
}
|
|
|
};
|
|
|
var s=function(t,i,a){ // 创建编辑器iframe并初始化
|
|
|
var l=this,n=t.find("iframe");
|
|
|
n.css({height:a.height}).on("load",function(){
|
|
|
var o=n.contents(),r=n.prop("contentWindow"),c=o.find("head"),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("")),
|
|
|
u=o.find("body");
|
|
|
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)
|
|
|
})
|
|
|
},
|
|
|
u=function(t){ // 获取iframe及其窗口对象
|
|
|
var i=e("#LAY_layedit_"+t),a=i.prop("contentWindow");
|
|
|
return[a,i]
|
|
|
},
|
|
|
d=function(t){ // 处理IE低版本兼容
|
|
|
return 8==l.ie&&(t=t.replace(/<.+>/g,function(t){return t.toLowerCase()})),t
|
|
|
},
|
|
|
y=function(t,a,n,o){ // 编辑器初始化和事件绑定
|
|
|
var r=t.document,c=e(r.body);
|
|
|
c.on("keydown",function(t){ // 监听按键事件
|
|
|
var e=t.keyCode;
|
|
|
if(13===e){var a=m(r),l=p(a),n=l.parentNode;if("pre"===n.tagName.toLowerCase()){if(t.shiftKey)return;return i.msg("请暂时用shift+enter"),!1}r.execCommand("formatBlock",!1,"<p>")
|
|
|
}},e(n).parents("form").on("submit",function(){var t=c.html();8==l.ie&&(t=t.replace(/<.+>/g,function(t){return t.toLowerCase()})),n.value=t}),
|
|
|
c.on("paste",function(e){r.execCommand("formatBlock",!1,"<p>"),setTimeout(function(){f.call(t,c),n.value=c.html()},100)})
|
|
|
},
|
|
|
f=function(t){ // 格式化HTML
|
|
|
var i=this;i.document;
|
|
|
t.find("*[style]").each(function(){var t=this.style.textAlign;this.removeAttribute("style"),e(this).css({"text-align":t||""})}),
|
|
|
t.find("table").addClass("layui-table"),
|
|
|
t.find("script,link").remove()
|
|
|
},
|