当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
40 Star 115 Fork 24

Try / Eleditor
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Eleditor.min.js 17.89 KB
一键复制 编辑 原始数据 按行查看 历史
Try 提交于 2018-03-17 15:12 . 更新 Eleditor.min.js
/**
* Version: 1.8
* Title: Eleditor 移动端富文本编辑器
* Site: https://eleditor.fixel.cn
* Author: Try
*/
(function(l){var c=false;var i="1.7";var d="Eleditor";var a=["INPUT","IMG","TEXTAREA"];var g={insertText:"插文字",insertImage:"插图片",insertLink:"插链接",insertHr:"水平线",editText:"改文字","delete":"删除",undo:"撤销",cancel:"取消",};var j="";l[d]=function(){};if(typeof jQuery==="undefined"&&typeof Zepto==="undefined"){return console.warn("|--Eleditor 请引入jQuery或者Zepto!模块化环境请把依赖全局安装")}else{if(typeof h==="undefined"){var h=typeof jQuery!="undefined"?jQuery:Zepto}}var f=function(q){var p=document.scripts;p=p[p.length-1].src.substring(0,p[p.length-1].src.lastIndexOf("/")+1);q(p);return p}(function(p){var q='<link rel="stylesheet" href="'+p+"layout/base.css"+(c?"?v="+(+new Date()):"")+'">';h("head").append(q)});var m=function(r,p){for(var q in p){if(p[q]==r){return true}}return false};var o=function(p){var q=p.replace(/\ +/g,"");q=p.replace(/[ ]/g,"");q=p.replace(/[\r\n]/g,"");return q.replace(/(^\s*)|(\s*$)/g,"")};var b=function(){var p=Math.max.apply(null,h.map(h("body *"),function(r){var q=h(r);if(q.css("position")!="static"){return parseInt(q.css("z-index"))||1}}));return(p+"").indexOf("Infinity")>=0?1:p+1};var k=function(){return d+""+ +new Date};var e=function(){var q=arguments[0],p=arguments[1];h.each(q.find("iframe,audio,video"),function(){var s=h(this),r=this.tagName.toLocaleLowerCase();if(!s.parent().hasClass("Eleditor-"+r+"-area")){h(this).wrap('<div class="Eleditor-'+r+'-area"></div>')}});if(o(q.text())==""&&q.find("img,iframe,video").length===0){q.append(p)}if(q.find("*").length===0){q.html('<p class="Eleditor-placeholder">'+q.html()+"</p>")}};var n=function(v,u){var r='<div class="Eleditor-wrap" style="z-index:'+b()+'" id="'+u+'"> <div class="Eleditor-controller"> <ul>';for(var t=0;t<v.length;t++){var w=v[t],s=typeof w==="object"?w.id:w,q=(typeof w==="object"&&w.tag)?w.tag.toLocaleLowerCase():null,p=typeof w==="object"?w.name:g[w];r+='<li event="'+s+'" '+(q?'bind-tags="'+q+'"':"")+' class="Eleditor-'+s+'">'+p+"</li>"}r+=' </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 r};l[d]=function(){console.log("|--Eleditor Initing");var R=arguments[0];R.upload=R.upload||{};R.toolbars=R.toolbars||[],_editorUid=k(),_historys=[],_placeHolder=R.placeHolder||'<p class="Eleditor-placeholder">点击此处编辑内容</p>';if(R.toolbars.length===0){R.toolbars=["insertText","editText","insertImage","insertLink","insertHr","delete","undo","cancel",]}if(R.el instanceof jQuery){var aa=R.el}else{var aa=h(R.el);if(aa.length===0){return console.warn("|--Eleditor "+R.el+"元素不存在,请在DOMContentLoaded后初始化Eleditor")}else{if(aa.length!=1){var aa=h(aa[0])}}}if(aa.attr("Eleditor-Inited")==="true"){return console.warn("|--Eleditor "+R.el+"已经绑定了Eleditor")}aa.attr({"Eleditor-Inited":"true","Eleditor-Uid":_editorUid});e(aa,_placeHolder);var N=null;var y=h(l);var q=h("html,body");var P=h(n(R.toolbars,_editorUid));var X=P.find(".Eleditor-controller");var z=P.find(".Eleditor-loading");var D=P.find(".Eleditor-textEditor");var E=D.find(".Eleditor-inputarea .textarea");var S=X.find(".Eleditor-insertImage");var F=D.find(".Eleditor-textStyle-item-upImg");var W=D.find(".Eleditor-inputarea input");var w=D.find(".Eleditor-textEditor-colors");var p=D.find(".Eleditor-textEditor-fontsizes");var M=P.find(".Eleditor-undo");var J=D.find(".Eleditor-textEditor-lineheight");var s=D.find(".Eleditor-textEditor-linedecorations");var x=P.find(".Eleditor-delete-layer");aa.addClass("Eleditor-area");aa.after(P);console.log("|--Eleditor Mounted To",aa);var K=null,Q=null;var C=function(ac){K=ac;ac.addClass("Eleditor-active");var ab=ac.offset().top+ac.outerHeight();h.each(X.find("li"),function(ae,ag){var af=h(ag),ad=af.attr("bind-tags");if(ad){ad=ad.toLocaleLowerCase().split(",");if(m(K[0].tagName.toLocaleLowerCase(),ad)){af.show()}else{af.hide()}}});X.show();O();if(typeof q.animate==="function"){q.stop().animate({scrollTop:(ab-150)+"px"},500)}else{q.scrollTop((ab-150)+"px")}Q&&Q.refresh()};var O=function(){if(K){X.css({top:K.offset().top+K.outerHeight(),width:aa.width()-5})}},u=function(){aa.find(".Eleditor-active").removeClass("Eleditor-active");X.hide();K=null};var G=function(){L();aa.addClass("Eleditor-deleteMode");x.show()},L=function(){if(_historys.length>0){P.find(".Eleditor-delete-revoke").removeClass("Eleditor-delete-revoke-disabled")}else{P.find(".Eleditor-delete-revoke").addClass("Eleditor-delete-revoke-disabled")}},r=function(){aa.removeClass("Eleditor-deleteMode");x.hide()};var t=function(){X.hide();P.addClass("Eleditor-mask");N=y.scrollTop();q.addClass("Eleditor-scrollLocked")},H=function(){P.removeClass("Eleditor-mask");q.removeClass("Eleditor-scrollLocked");y.scrollTop(N)};var A=function(){t();z.show();z.html("<p>"+arguments[0]+"</p>")},Z=function(){H();z.hide()};var B=function(){_historys.push(aa.html());Y();L();return true},I=function(){if(_historys.length===0){return}aa.html(_historys.pop());Y();L();u()},Y=function(){if(_historys.length==0){M.hide()}else{M.show()}};var v=function(){D.attr("role","edit").show();E.html(K.hasClass("Eleditor-placeholder")?"":K.html());E.attr("style",K.attr("style"));if(K.css("font-weight")=="bold"){D.find(".Eleditor-textStyle-bold").addClass("Eleditor-active")}if(m(K.css("text-decoration"),["overline","line-through","underline"])){D.find(".Eleditor-textStyle-linedecoration").addClass("Eleditor-active")}if(K[0].tagName=="A"){D.attr("type","link");W.val(K.attr("href"))}else{D.attr("type","word")}var ab=K.css("text-align");if(m(ab,["left","center","right"])){D.find(".Eleditor-textStyle-align[align="+ab+"]").addClass("Eleditor-active")}else{D.find(".Eleditor-textStyle-align").removeClass("Eleditor-active")}D.find(".Eleditor-textStyle-color span").css("background-color",K.css("color"))};if(typeof WebUploader!="undefined"&&typeof R.uploader!="function"){Q=WebUploader.create({auto:true,server:R.upload.server||"/upload",pick:S,duplicate:true,resize:false,fileSingleSizeLimit:R.upload.fileSizeLimit?R.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"},R.upload.accept||{}),fileVal:R.upload.formName,});Q.on("uploadStart",function(ab,ac){u();A('上传图片中<span id="uploadProgress">1</span>%')});Q.on("uploadProgress",function(ab,ac){h("#uploadProgress").html(parseFloat((ac*100).toFixed(2)))});Q.on("error",function(){if(arguments[0]=="Q_TYPE_DENIED"){l.alert("请上传图片格式文件")}if(arguments[0]=="F_EXCEED_SIZE"){l.alert("文件大小不能超过"+(arguments[1]/1048576)+"M")}});Q.on("uploadComplete",function(){Z()});Q.on("uploadSuccess",function(ab,ad){var ac=ab.source._refer[0].className.indexOf("Eleditor-textStyle-item-upImg")>=0;if(ad.status==1){if(ac){E.append('<img src="'+ad.url+'">')}else{B();K.after(h('<img src="'+ad.url+'">'));if(K.hasClass("Eleditor-placeholder")){K.remove()}}}else{l.alert("上传失败:["+ad.msg+"]")}});Q.addButton({id:F});F.addClass("Eleditor-webUploader-btn")}else{if(typeof R.uploader=="function"){var T=function(){var ac=h(this).hasClass("Eleditor-textStyle-item-upImg");try{R.uploader.call(this).then(function(ad){if(ac){E.append('<img src="'+ad+'">')
}else{B();K.after(h('<img src="'+ad+'">'));if(K.hasClass("Eleditor-placeholder")){K.remove()}}},function(ad){l.alert("上传失败:["+ad+"]")})}catch(ab){console.warn(ab);console.warn("|--Eleditor 请检查uploader函数返回是否是标准Promise对象!")}};S.click(T);F.click(T)}else{F.addClass("Eleditor-textStyle-item-upImg-disabled")}}var U={insertText:function(){t();D.attr({"role":"insert","type":"word"}).show()},insertLink:function(){t();D.attr({"role":"insert","type":"link"}).show()},insertImage:function(){if(typeof WebUploader==="undefined"&&typeof R.uploader!="function"){window.alert("上传参数未定义.")}},insertHr:function(){B();var ab=h('<div class="horizontal-line" style="padding: 10px 0;border-bottom: 1px solid #aaa;margin-bottom: 20px;"></div>');K.after(ab);u()},editText:function(){if(m(K[0].tagName,a)){return this.insertText()}t();v()},"delete":function(){if(aa.find("*").length==1){B();K.remove()}else{G()}u();e(aa,_placeHolder)},undo:function(){I()},cancel:function(){u()}};for(var V=0;V<R.toolbars.length;V++){if(typeof R.toolbars[V]==="object"){U[R.toolbars[V].id]=R.toolbars[V].handle}}y.on("resize",function(){O()});X.on("click","ul li",function(){var ac=h(this),ab=ac.attr("event");if(typeof U[ab]==="function"){if(typeof g[ab]!="undefined"){U[ab]()}else{U[ab](K,ac)!==false&&U.cancel()}}});P.on("click",".Eleditor-delete-revoke",function(){I()});x.on("click",".Eleditor-delete-clear",function(){if(!confirm("确定清空内容吗?")){return}var ab=aa.clone();ab.find(".Eleditor-placeholder").remove();if(o(ab.text())!=""){B();aa.html("");e(aa,_placeHolder)}});x.on("click",".Eleditor-delete-back",function(){r()});D.on("click",".Eleditor-textStyle-bold",function(){E.css("font-weight",h(this).hasClass("Eleditor-active")?"normal":"bold");h(this).toggleClass("Eleditor-active")});D.on("click",".Eleditor-textStyle-linedecoration",function(){s.show();h(this).addClass("Eleditor-active")});D.on("click",".Eleditor-textStyle-color,.Eleditor-textStyle-bgColor",function(){var ab=h(this);var ac=ab.hasClass("Eleditor-textStyle-bgColor")?"bgcolor":"color";w.find(".Eleditor-textEditor-modulePane span").html(ac=="bgcolor"?"文字背景颜色":"文字颜色");w.attr("role",ac).show();h(this).addClass("Eleditor-active")});D.on("click",".Eleditor-textStyle-fontSize",function(){p.show();h(this).addClass("Eleditor-active")});D.on("click",".Eleditor-textStyle-lineHeight",function(){J.show();h(this).addClass("Eleditor-active")});s.on("click","ul li",function(){if(!h(this).hasClass("Eleditor-inheritValue")){E.css("text-decoration",h(this).css("text-decoration"))}else{E.css("text-decoration","inherit");D.find(".Eleditor-textStyle-linedecoration").removeClass("Eleditor-active")}s.hide()});J.on("click","ul li",function(){if(!h(this).hasClass("Eleditor-inheritValue")){E.css("line-height",h(this).html())}else{E.css("line-height","inherit");D.find(".Eleditor-textStyle-lineHeight").removeClass("Eleditor-active")}J.hide()});p.on("click","ul li",function(){if(!h(this).hasClass("Eleditor-inheritValue")){E.css("font-size",h(this).html())}else{E.css("font-size","inherit");D.find(".Eleditor-textStyle-fontSize").removeClass("Eleditor-active")}p.hide()});D.on("click",".Eleditor-textStyle-align",function(){var ab=h(this).attr("align");E.css({"text-align":ab,"display":"block"});D.find(".Eleditor-textStyle-align.Eleditor-active").removeClass("Eleditor-active");h(this).addClass("Eleditor-active")});D.on("click",".Eleditor-textEditor-format",function(){var ac=E,ab="style width height border bgcolor align color";ac.removeAttr(ab);h.each(ac.find("*"),function(af,ad){var ae=ac.find(ad);if(m(ad.tagName.toLocaleLowerCase(),["script","style"])){ae.remove()}else{ae.removeAttr(ab)}});E.html(ac.html());D.find(".Eleditor-active").removeClass("Eleditor-active")});D.on("click",".Eleditor-textEditor-clean",function(){confirm("确定清空内容(不可恢复)?")&&E.html("")});D.on("click",".Eleditor-cancel,.Eleditor-commit",function(){arguments[0].preventDefault();if(h(this).hasClass("Eleditor-commit")){var ab=E.attr("style")||"";var ae=E.html();var ac=D.attr("role")=="edit";var ag=K.hasClass("Eleditor-placeholder");if(!ae){return alert("请输入内容文字")}B();if(ac||(ag&&D.attr("type")!="link")){if(D.attr("type")=="link"){K.attr("href",W.val())}K.attr("style",ab).removeClass("Eleditor-placeholder").html(ae)}else{var ad="";if(D.attr("type")=="link"){var af=W.val();ad='<a style="'+ab+'" href="'+af+'">'+ae+"</a>"}else{ad='<p style="'+ab+'">'+ae+"</p>"}K.after(h(ad));if(ag){K.remove()}}O()}D.find(".Eleditor-active").removeClass("Eleditor-active");D.find(".Eleditor-textStyle-color span").removeAttr("style");E.removeAttr("style").html("");W.val("");H();D.hide();u()});w.on("click","ul li span",function(){var ab=h(this).css("background-color");if(w.attr("role")=="color"){if(!h(this).hasClass("Eleditor-inheritValue")){E.css("color",ab);D.find(".Eleditor-textStyle-color span").css("background-color",ab)}else{E.css("color","inherit");D.find(".Eleditor-textStyle-color").removeClass("Eleditor-active").find("span").removeAttr("style")}}else{if(!h(this).hasClass("Eleditor-inheritValue")){E.css("background-color",ab)
}else{E.css("background-color","inherit");D.find(".Eleditor-textStyle-bgColor").removeClass("Eleditor-active")}}w.hide()});aa.on("click","*",function(ab){var ac=h(this);if(aa.hasClass("Eleditor-deleteMode")){if(!ac.hasClass("Eleditor-placeholder")){B();if(typeof ac.fadeOut==="function"){ac.fadeOut("fast",function(){ac.remove();e(aa,_placeHolder)})}else{ac.remove();e(aa,_placeHolder)}}}else{if(!ac.hasClass("Eleditor-active")){u();r();C(ac)}}return ab.preventDefault()==0});return{clear:function(){aa.html("");e(aa,_placeHolder)},revoke:function(){I()},append:function(){u();var ac=arguments[0];if(!ac){return}var ab=h(ac);if(typeof ac=="string"&&ab.length==0){ab=h("<p>"+ac+"</p>")}if(o(ab.text())==""){return}aa.find(".Eleditor-placeholder").remove();B();return aa.append(ab)},trigger:function(){var ab=arguments[0];if(ab=="insertText"){K=aa.find(">:last");U.insertText()}else{if(ab=="insertLink"){K=aa.find(">:last");U.insertLink()}}u()},saveState:function(){return B()},getEditNode:function(){return K},getContent:function(){var ab=aa.clone();ab.find(".Eleditor-placeholder").remove();return o(ab.html())},getContentText:function(){var ab=aa.clone();ab.find(".Eleditor-placeholder").remove();return o(ab.text())},destory:function(){aa.removeAttr("Eleditor-Inited Eleditor-Uid");aa.removeClass("Eleditor-area");aa.find(".Eleditor-placeholder").remove();aa.off().find(".Eleditor-active").removeClass("Eleditor-active");P.find("*").off();P.remove();console.log("|--Eleditor "+_editorUid+" destoryed")}}}})(window);
JavaScript
1
https://gitee.com/hihwp/eleditor.git
git@gitee.com:hihwp/eleditor.git
hihwp
eleditor
Eleditor
master

搜索帮助