forked from Fixels/Eleditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eleditor.min.js
4 lines (4 loc) · 18 KB
/
Eleditor.min.js
1
2
3
4
(function(k){var c=false;var d="Eleditor";var a=["INPUT","IMG","TEXTAREA"];var f={insertText:"插文字",insertImage:"插图片",insertLink:"插链接",insertHr:"水平线",editText:"改文字","delete":"删除",undo:"撤销",cancel:"取消",};var i="";k[d]=function(){};if(typeof jQuery==="undefined"&&typeof Zepto==="undefined"){return console.warn("|--Eleditor 请引入jQuery或者Zepto!模块化环境请把依赖全局安装")}else{if(typeof g==="undefined"){var g=typeof jQuery!="undefined"?jQuery:Zepto}}var h=function(p){var o=document.scripts;o=o[o.length-1].src.substring(0,o[o.length-1].src.lastIndexOf("/")+1);p(o);return o}(function(o){var p='<link rel="stylesheet" href="'+o+"layout/base.css"+(c?"?v="+(+new Date()):"")+'">';g("head").append(p)});var l=function(q,o){for(var p in o){if(o[p]==q){return true}}return false};var n=function(o){var p=o.replace(/\ +/g,"");p=p.replace(/[ ]/g,"");p=p.replace(/[\r\n]/g,"");return p.replace(/(^\s*)|(\s*$)/g,"")};var b=function(){var o=Math.max.apply(null,g.map(g("body *"),function(q){var p=g(q);if(p.css("position")!="static"){return parseInt(p.css("z-index"))||1}}));return(o+"").indexOf("Infinity")>=0?1:o+1};var j=function(){return d+""+ +new Date};var e=function(){var p=arguments[0],o=arguments[1];g.each(p.find("iframe,audio,video"),function(){var r=g(this),q=this.tagName.toLocaleLowerCase();if(!r.parent().hasClass("Eleditor-"+q+"-area")){g(this).wrap('<div class="Eleditor-'+q+'-area"></div>')}});if(n(p.text())==""&&p.find("img,iframe,video").length===0){p.append(o)}if(p.find("*").length===0){p.html('<p class="Eleditor-placeholder">'+p.html()+"</p>")}};var m=function(u,t){var q='<div class="Eleditor-wrap" style="z-index:'+b()+'" id="'+t+'"> <div class="Eleditor-controller"> <ul>';for(var s=0;s<u.length;s++){var v=u[s],r=typeof v==="object"?v.id:v,p=(typeof v==="object"&&v.tag)?v.tag.toLocaleLowerCase():null,o=typeof v==="object"?v.name:f[v];q+='<li event="'+r+'" '+(p?'bind-tags="'+p+'"':"")+' class="Eleditor-'+r+'">'+o+"</li>"}q+=' </ul> </div> <div class="Eleditor-loading"><p></p></div> <div class="Eleditor-textEditor"> <div class="Eleditor-textStyle"> <div class="Eleditor-textStyle-item"><div class="Eleditor-textStyle-bold"></div></div> <div class="Eleditor-textStyle-item"><div class="Eleditor-textStyle-color"><span></span></div></div> <div class="Eleditor-textStyle-item"><div class="Eleditor-textStyle-linedecoration"></div></div> <div class="Eleditor-textStyle-item"><div class="Eleditor-textStyle-bgColor"></div></div> <div class="Eleditor-textStyle-item"><div class="Eleditor-textStyle-fontSize"></div></div> <div class="Eleditor-textStyle-item"><div class="Eleditor-textStyle-lineHeight"></div></div> <div class="Eleditor-textStyle-item"> <div class="Eleditor-textStyle-align Eleditor-textStyle-alignLeft" align="left"></div> </div> <div class="Eleditor-textStyle-item"> <div class="Eleditor-textStyle-align Eleditor-textStyle-alignCenter" align="center"> </div> </div> <div class="Eleditor-textStyle-item"> <div class="Eleditor-textStyle-align Eleditor-textStyle-alignRight" align="right"> </div> </div> <div class="Eleditor-textStyle-item Eleditor-textStyle-item-upImg"> <div class="Eleditor-textStyle-upImg"></div> </div> </div> <div class="Eleditor-textEditor-colors"> <div class="Eleditor-textEditor-modulePane"><span></span></div> <ul> <li><span style="background-color:#232323;"></span></li> <li><span style="background-color:#2196F3;"></span></li> <li><span style="background-color:#795548;"></span></li> <li><span style="background-color:#00BCD4;"></span></li> <li><span style="background-color:#4CAF50;"></span></li> <li><span style="background-color:#E666E5;"></span></li> <li><span style="background-color:#FF9800;"></span></li> <li><span style="background-color:#FF5722;"></span></li> <li><span style="background-color:#ff2a1a;"></span></li> <li><span style="background-color:#FFEB3B;"></span></li> <li><span style="background-color:#ffffff;border: 1px solid #ccc;"></span></li> <li> <span class="Eleditor-inheritValue" style="background-color:transparent; border: 1px solid #dedede;"> </span> </li> </ul> </div> <div class="Eleditor-textEditor-fontsizes"> <div class="Eleditor-textEditor-modulePane"><span>字体大小</span></div> <ul> <li class="Eleditor-inheritValue">默认</li> <li>14px</li> <li>16px</li> <li>20px</li> <li>28px</li> <li>35px</li> </ul> </div> <div class="Eleditor-textEditor-lineheight"> <div class="Eleditor-textEditor-modulePane"><span>行高</span></div> <ul> <li class="Eleditor-inheritValue">默认</li> <li>20px</li> <li>25px</li> <li>30px</li> <li>35px</li> <li>40px</li> </ul> </div> <div class="Eleditor-textEditor-linedecorations"> <div class="Eleditor-textEditor-modulePane"><span>文本修饰</span></div> <ul> <li class="Eleditor-inheritValue">无</li> <li style="text-decoration: overline">上划线修饰</li> <li style="text-decoration: line-through">删除线修饰</li> <li style="text-decoration: underline">下划线修饰</li> </ul> </div> <div class="Eleditor-textEditor-formats"> <div class="Eleditor-textEditor-format"></div> <div class="Eleditor-textEditor-clean"></div> </div> <div class="Eleditor-inputarea"> <input placeholder="请输入超链接" type="text" /> <div placeholder="点击输入内容" class="textarea" contenteditable="true"></div> </div> <div class="Eleditor-method"> <button class="Eleditor-commit">提交</button> <button class="Eleditor-cancel">取消</button> </div> </div> <div class="Eleditor-delete-layer"> <div class="Eleditor-delete-tip">进入批量删除模式,点击段落进行删除</div> <div class="Eleditor-delete-revoke"></div> <div class="Eleditor-delete-clear"></div> <div class="Eleditor-delete-back">返回继续编辑</div> </div> </div>';
return q};k[d]=function(){console.log("|--Eleditor Initing");var Q=arguments[0];Q.upload=Q.upload||{};Q.mounted=Q.mounted||function(){};Q.changer=Q.changer||function(){};Q.toolbars=Q.toolbars||[],_editorUid=j(),_historys=[],_placeHolder=Q.placeHolder||'<p class="Eleditor-placeholder">点击此处编辑内容</p>',_uploadRole=null;if(Q.toolbars.length===0){Q.toolbars=["insertText","editText","insertImage","insertLink","insertHr","delete","undo","cancel",]}if(Q.el instanceof jQuery){var Z=Q.el}else{var Z=g(Q.el);if(Z.length===0){return console.warn("|--Eleditor "+Q.el+"元素不存在,请在DOMContentLoaded后初始化Eleditor")}else{if(Z.length!=1){var Z=g(Z[0])}}}if(Z.attr("Eleditor-Inited")==="true"){return console.warn("|--Eleditor "+Q.el+"已经绑定了Eleditor")}Z.attr({"Eleditor-Inited":"true","Eleditor-Uid":_editorUid});e(Z,_placeHolder);var M=null;var x=g(k);var p=g("html,body");var O=g(m(Q.toolbars,_editorUid));var W=O.find(".Eleditor-controller");var y=O.find(".Eleditor-loading");var C=O.find(".Eleditor-textEditor");var D=C.find(".Eleditor-inputarea .textarea");var R=W.find(".Eleditor-insertImage");var E=C.find(".Eleditor-textStyle-item-upImg");var V=C.find(".Eleditor-inputarea input");var v=C.find(".Eleditor-textEditor-colors");var o=C.find(".Eleditor-textEditor-fontsizes");var L=O.find(".Eleditor-undo");var I=C.find(".Eleditor-textEditor-lineheight");var r=C.find(".Eleditor-textEditor-linedecorations");var w=O.find(".Eleditor-delete-layer");Z.addClass("Eleditor-area");Z.after(O);console.log("|--Eleditor Mounted To",Z);var J=null,P=null;var B=function(ab){J=ab;ab.addClass("Eleditor-active");var aa=ab.offset().top+ab.outerHeight();g.each(W.find("li"),function(ad,af){var ae=g(af),ac=ae.attr("bind-tags");if(ac){ac=ac.toLocaleLowerCase().split(",");if(l(J[0].tagName.toLocaleLowerCase(),ac)){ae.show()}else{ae.hide()}}});W.show();N();if(typeof p.animate==="function"){p.stop().animate({scrollTop:(aa-150)+"px"},500)}else{p.scrollTop((aa-150)+"px")}P&&P.refresh()};var N=function(){if(J){W.css({top:J.offset().top+J.outerHeight(),width:Z.width()-5})}},t=function(){Z.find(".Eleditor-active").removeClass("Eleditor-active");W.hide();J=null};var F=function(){K();Z.addClass("Eleditor-deleteMode");w.show()},K=function(){if(_historys.length>0){O.find(".Eleditor-delete-revoke").removeClass("Eleditor-delete-revoke-disabled")}else{O.find(".Eleditor-delete-revoke").addClass("Eleditor-delete-revoke-disabled")}},q=function(){Z.removeClass("Eleditor-deleteMode");w.hide()};var s=function(){W.hide();O.addClass("Eleditor-mask");M=x.scrollTop();p.addClass("Eleditor-scrollLocked")},G=function(){O.removeClass("Eleditor-mask");p.removeClass("Eleditor-scrollLocked");x.scrollTop(M)};var z=function(){s();y.show();y.html("<p>"+arguments[0]+"</p>")},Y=function(){G();y.hide()};var A=function(){_historys.push(Z.html());X();K();return true},H=function(){if(_historys.length===0){return}Z.html(_historys.pop());Q.changer();X();K();t()},X=function(){if(_historys.length==0){L.hide()}else{L.show()}};var u=function(){C.attr("role","edit").show();D.html(J.hasClass("Eleditor-placeholder")?"":J.html());D.attr("style",J.attr("style"));if(J.css("font-weight")=="bold"){C.find(".Eleditor-textStyle-bold").addClass("Eleditor-active")}if(l(J.css("text-decoration"),["overline","line-through","underline"])){C.find(".Eleditor-textStyle-linedecoration").addClass("Eleditor-active")}if(J[0].tagName=="A"){C.attr("type","link");V.val(J.attr("href"))}else{C.attr("type","word")}var aa=J.css("text-align");if(l(aa,["left","center","right"])){C.find(".Eleditor-textStyle-align[align="+aa+"]").addClass("Eleditor-active")}else{C.find(".Eleditor-textStyle-align").removeClass("Eleditor-active")}C.find(".Eleditor-textStyle-color span").css("background-color",J.css("color"))};if(typeof WebUploader!="undefined"&&typeof Q.uploader!="function"){P=WebUploader.create({auto:true,server:Q.upload.server||"/upload",pick:R,resize:false,headers:Q.upload.headers||{},duplicate:true,fileSingleSizeLimit:Q.upload.fileSizeLimit?Q.upload.fileSizeLimit*1024*1024:undefined,accept:Object.assign({title:"Images",extensions:"gif,jpg,jpeg,bmp,png,webp",mimeTypes:"image/gif,image/jpg,image/jpeg,image/bmp,image/png,image/webp"},Q.upload.accept||{}),fileVal:Q.upload.formName,});P.on("uploadStart",function(aa,ab){z('上传图片中<span id="uploadProgress">1</span>%')});P.on("uploadProgress",function(aa,ab){g("#uploadProgress").html(parseFloat((ab*100).toFixed(2)))});P.on("error",function(){if(arguments[0]=="Q_TYPE_DENIED"){k.alert("请上传图片格式文件")}if(arguments[0]=="F_EXCEED_SIZE"){k.alert("文件大小不能超过"+(arguments[1]/1048576)+"M")}});P.on("uploadComplete",function(){Y()});P.on("uploadSuccess",function(aa,ac){var ab=g("#rt_"+aa.source.ruid).parents(".Eleditor-textStyle-item-upImg").length>0;if(ac.status==1){if(ab){D.append('<img src="'+ac.url+'">')}else{A();J.after(g('<img src="'+ac.url+'">'));if(J.hasClass("Eleditor-placeholder")){J.remove()}}Q.changer()}else{k.alert("上传失败:["+ac.msg+"]")}if(!ab){t()}});P.addButton({id:E});E.addClass("Eleditor-webUploader-btn")}else{if(typeof Q.uploader=="function"){var S=function(){var ab=g(this).hasClass("Eleditor-textStyle-item-upImg");
try{Q.uploader.call(this).then(function(ac){if(ab){D.append('<img src="'+ac+'">')}else{A();J.after(g('<img src="'+ac+'">'));if(J.hasClass("Eleditor-placeholder")){J.remove()}t()}Q.changer()},function(ac){k.alert("上传失败:["+ac+"]")})}catch(aa){console.warn(aa);console.warn("|--Eleditor 请检查uploader函数返回是否是标准Promise对象!")}};R.click(S);E.click(S)}else{E.addClass("Eleditor-textStyle-item-upImg-disabled")}}var T={insertText:function(){s();C.attr({"role":"insert","type":"word"}).show()},insertLink:function(){s();C.attr({"role":"insert","type":"link"}).show()},insertImage:function(){if(typeof WebUploader==="undefined"&&typeof Q.uploader!="function"){window.alert("上传参数未定义.")}},insertHr:function(){A();var aa=g('<div class="horizontal-line" style="padding: 10px 0;border-bottom: 1px solid #aaa;margin-bottom: 20px;"></div>');J.after(aa);Q.changer();t()},editText:function(){if(l(J[0].tagName,a)){return this.insertText()}s();u()},"delete":function(){if(Z.find("*").length==1){A();Q.changer();J.remove()}else{F()}t();e(Z,_placeHolder)},undo:function(){H()},cancel:function(){t()}};for(var U=0;U<Q.toolbars.length;U++){if(typeof Q.toolbars[U]==="object"){T[Q.toolbars[U].id]=Q.toolbars[U].handle}}x.on("resize",function(){N()});W.on("click","ul li",function(){var ab=g(this),aa=ab.attr("event");if(typeof T[aa]==="function"){if(typeof f[aa]!="undefined"){T[aa]()}else{T[aa](J,ab)!==false&&T.cancel()}}});O.on("click",".Eleditor-delete-revoke",function(){H()});w.on("click",".Eleditor-delete-clear",function(){if(!confirm("确定清空内容吗?")){return}var aa=Z.clone();aa.find(".Eleditor-placeholder").remove();if(n(aa.text())!=""){A();Z.html("");Q.changer();e(Z,_placeHolder)}});w.on("click",".Eleditor-delete-back",function(){q()});C.on("click",".Eleditor-textStyle-bold",function(){D.css("font-weight",g(this).hasClass("Eleditor-active")?"normal":"bold");g(this).toggleClass("Eleditor-active")});C.on("click",".Eleditor-textStyle-linedecoration",function(){r.show();g(this).addClass("Eleditor-active")});C.on("click",".Eleditor-textStyle-color,.Eleditor-textStyle-bgColor",function(){var aa=g(this);var ab=aa.hasClass("Eleditor-textStyle-bgColor")?"bgcolor":"color";v.find(".Eleditor-textEditor-modulePane span").html(ab=="bgcolor"?"文字背景颜色":"文字颜色");v.attr("role",ab).show();g(this).addClass("Eleditor-active")});C.on("click",".Eleditor-textStyle-fontSize",function(){o.show();g(this).addClass("Eleditor-active")});C.on("click",".Eleditor-textStyle-lineHeight",function(){I.show();g(this).addClass("Eleditor-active")});r.on("click","ul li",function(){if(!g(this).hasClass("Eleditor-inheritValue")){D.css("text-decoration",g(this).css("text-decoration"))}else{D.css("text-decoration","inherit");C.find(".Eleditor-textStyle-linedecoration").removeClass("Eleditor-active")}r.hide()});I.on("click","ul li",function(){if(!g(this).hasClass("Eleditor-inheritValue")){D.css("line-height",g(this).html())}else{D.css("line-height","inherit");C.find(".Eleditor-textStyle-lineHeight").removeClass("Eleditor-active")}I.hide()});o.on("click","ul li",function(){if(!g(this).hasClass("Eleditor-inheritValue")){D.css("font-size",g(this).html())}else{D.css("font-size","inherit");C.find(".Eleditor-textStyle-fontSize").removeClass("Eleditor-active")}o.hide()});C.on("click",".Eleditor-textStyle-align",function(){var aa=g(this).attr("align");D.css({"text-align":aa,"display":"block"});C.find(".Eleditor-textStyle-align.Eleditor-active").removeClass("Eleditor-active");g(this).addClass("Eleditor-active")});C.on("click",".Eleditor-textEditor-format",function(){var ab=D,aa="style width height border bgcolor align color";ab.removeAttr(aa);g.each(ab.find("*"),function(ae,ac){var ad=ab.find(ac);if(l(ac.tagName.toLocaleLowerCase(),["script","style"])){ad.remove()}else{ad.removeAttr(aa)}});D.html(ab.html());C.find(".Eleditor-active").removeClass("Eleditor-active")});C.on("click",".Eleditor-textEditor-clean",function(){confirm("确定清空内容(不可恢复)?")&&D.html("")});C.on("click",".Eleditor-cancel,.Eleditor-commit",function(){arguments[0].preventDefault();if(g(this).hasClass("Eleditor-commit")){var aa=D.attr("style")||"";var ad=D.html();var ab=C.attr("role")=="edit";var af=J.hasClass("Eleditor-placeholder");if(!ad){return alert("请输入内容文字")}A();if(ab||(af&&C.attr("type")!="link")){if(C.attr("type")=="link"){J.attr("href",V.val())}J.attr("style",aa).removeClass("Eleditor-placeholder").html(ad)}else{var ac="";if(C.attr("type")=="link"){var ae=V.val();ac='<a style="'+aa+'" href="'+ae+'">'+ad+"</a>"}else{ac='<p style="'+aa+'">'+ad+"</p>"}J.after(g(ac));if(af){J.remove()}}Q.changer();N()}C.find(".Eleditor-active").removeClass("Eleditor-active");C.find(".Eleditor-textStyle-color span").removeAttr("style");D.removeAttr("style").html("");V.val("");G();C.hide();t()});v.on("click","ul li span",function(){var aa=g(this).css("background-color");if(v.attr("role")=="color"){if(!g(this).hasClass("Eleditor-inheritValue")){D.css("color",aa);C.find(".Eleditor-textStyle-color span").css("background-color",aa)}else{D.css("color","inherit");C.find(".Eleditor-textStyle-color").removeClass("Eleditor-active").find("span").removeAttr("style")
}}else{if(!g(this).hasClass("Eleditor-inheritValue")){D.css("background-color",aa)}else{D.css("background-color","inherit");C.find(".Eleditor-textStyle-bgColor").removeClass("Eleditor-active")}}v.hide()});Z.on("click","*",function(aa){var ab=g(this);if(Z.hasClass("Eleditor-deleteMode")){if(!ab.hasClass("Eleditor-placeholder")){A();if(typeof ab.fadeOut==="function"){ab.fadeOut("fast",function(){ab.remove();e(Z,_placeHolder)})}else{ab.remove();e(Z,_placeHolder)}Q.changer()}}else{if(!ab.hasClass("Eleditor-active")){t();q();B(ab)}}return aa.preventDefault()==0});Q.mounted();return{clear:function(){Q.changer();Z.html("");e(Z,_placeHolder)},revoke:function(){H()},append:function(){t();var ab=arguments[0];if(!ab){return}var aa=g(ab);if(typeof ab=="string"&&aa.length==0){aa=g("<p>"+ab+"</p>")}Z.find(".Eleditor-placeholder").remove();A();return Z.append(aa)},trigger:function(){var aa=arguments[0];if(aa=="insertText"){J=Z.find(">:last");T.insertText()}else{if(aa=="insertLink"){J=Z.find(">:last");T.insertLink()}}},saveState:function(){return A()},getEditNode:function(){return J},getContent:function(){var aa=Z.clone();aa.find(".Eleditor-placeholder").remove();return aa.html()},getContentText:function(){var aa=Z.clone();aa.find(".Eleditor-placeholder").remove();return n(aa.text())},hideEditorControllerLayer:t,destory:function(){Z.removeAttr("Eleditor-Inited Eleditor-Uid");Z.removeClass("Eleditor-area");Z.find(".Eleditor-placeholder").remove();Z.off().find(".Eleditor-active").removeClass("Eleditor-active");O.find("*").off();O.remove();console.log("|--Eleditor "+_editorUid+" destoryed")}}}})(window);