zx-editor
Version:
ZxEditor is a HTML5 rich text editor
10 lines (9 loc) • 35.7 kB
JavaScript
/*!
* zx-editor v3.0.4-alpha
* https://github.com/capricorncd/zx-editor
* Copyright © 2018-present, capricorncd
* Released under the MIT License
* Released on: 2021-03-20 19:20:00
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t=t||self).ZxEditor=e()}(this,function(){"use strict";function h(t){return(h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}var d="undefined"==typeof document?{body:{},addEventListener:function(){},removeEventListener:function(){},activeElement:{blur:function(){},nodeName:""},querySelector:function(){return null},querySelectorAll:function(){return[]},getElementById:function(){return null},createEvent:function(){return{initEvent:function(){}}},createElement:function(){return{children:[],childNodes:[],style:{},setAttribute:function(){},getElementsByTagName:function(){return[]}}},location:{hash:""}}:document,u="undefined"==typeof window?{document:d,navigator:{userAgent:""},location:{},history:{},CustomEvent:function(){return this},addEventListener:function(){},removeEventListener:function(){},getComputedStyle:function(){return{getPropertyValue:function(){return""}}},Image:function(){},Date:function(){},screen:{},setTimeout:function(){},clearTimeout:function(){}}:window;function a(t){for(var e=[],n=0;n<t.length;n+=1)-1===e.indexOf(t[n])&&e.push(t[n]);return e}function n(t,e){var n=t.nodeName.toLowerCase();if("string"!=typeof e||0===m.strip(e).length)throw new TypeError("changeNodeName(oldNode, tagName) 'tagName' is ".concat(e,", should be a 'string'"));if(n===e.toLowerCase())return t;n=d.createElement(e);return 1!==t.nodeType?n.appendChild(t):(t.className&&(n.className=t.className),t.id&&(n.id=t.id),(e=t.getAttribute("style"))&&n.setAttribute("style",e),n.innerHTML=t.innerHTML),t.parentNode&&t.parentNode.replaceChild(n,t),n}function o(t){return d.createTextNode(t)}function s(t,e){if(!t&&"string"!=typeof t)throw new TypeError("Parameter error");var n=d.createElement(t);if(e&&"object"===h(e))for(var i in e)e.hasOwnProperty(i)&&n.setAttribute(i,e[i]);return n}function f(t){if(!t)return null;if("string"==typeof t)return o(t);var e=t.tag,n=t.attrs,t=t.child;if(!e&&!n&&!t)return null;var i,r=s(e||"div",n);return Array.isArray(t)&&t.length?t.forEach(function(t){(i=f(t))&&r.appendChild(i)}):t&&"string"==typeof t&&r.appendChild(o(t)),r}var i=[],e=/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,t=u.navigator.userAgent,r=u.navigator.platform;function l(t){t=parseInt(t);return isNaN(t)?0:t}function c(t){return t?t.toString().replace(e,""):""}function p(t){return c(t).replace(/\s+/g," ")}function g(t,e){var n=t&&"object"===h(t)&&!Array.isArray(t);return e?n&&"[object Object]"===t.toString():n}function v(t){t=("number"==typeof t?t:l(t)).toString(16);return t[1]?t:"0"+t}var m={isIPhone:/iphone/i.test(t)&&/iphone/.test(r),isIPhoneX:812===u.screen.height&&375===u.screen.width,supportBoxModel:function(){var t=d.getElementsByTagName("body")[0],e=d.createElement("div");t.appendChild(e),e.innerHTML="",e.style.width=e.style.paddingLeft="1px";var n=2===e.offsetWidth;return t.removeChild(e),n}(),changeNodeName:n,int:l,isElement:function(t){return g(t)&&t.nodeType},isObject:g,isWindow:function(t){return null!==t&&t===t.window},removeHtmlTags:function(t){return p(t=(t+"").replace(/<\/?.+?>/g,""))},rgbToHex:function(t){var e="";return/rgb.*?\((\d+)\D+?(\d+)\D+?(\d+)/.test(t)&&(e+=v(RegExp.$1),e+=v(RegExp.$2),e+=v(RegExp.$3)),e?"#"+e:t},slice:function(t,e){return i.slice.call(t,l(e))},strip:p,trim:c,toHump:function(t){return p(t).replace(/([-_\s]+\w)/g,function(t,e){return e[1].toUpperCase()})},unique:a};function y(t,e){var n,i=this;return t?t instanceof y?t:("string"==typeof t?"<"===t[0]&&">"===t[t.length-1]&&3<=t.length?((n=d.createElement("div")).innerHTML=t,n=m.slice(n.childNodes)):/^[#\w\d_,\s-]+$/.test(t)&&(e instanceof y&&(e=e[0]),e=m.isElement(e)&&1===e.nodeType?e:d,n=m.slice(e.querySelectorAll(m.strip(t)))):t.nodeType||t===u||t===d?n=[t]:Array.isArray(t)&&t.every(function(t){return m.isElement(t)})&&(n=t),n&&(n.forEach(function(t,e){i[e]=t}),this.length=n.length),this):this}y.prototype={constructor:y,length:0,children:function(t,e){"boolean"==typeof t&&(e=t,t=void 0);for(var n,i=[],r=0;r<this.length;r++)for(var o=this[r].childNodes,s=0;s<o.length;s++)n=o[s],t?1===n.nodeType&&b(n).is(t)&&i.push(n):!e&&1!==n.nodeType||i.push(n);return b(a(i))},closest:function(t){var e=this;return void 0===t?new b:(e.is(t)||(e=e.parents(t).eq(0)),e)},eq:function(t){var e=this.length,e=m.int(t)+(t<0?e:0);return b(this[e])},find:function(t){for(var e,n=[],i=0;i<this.length;i++)if(1===this[i].nodeType){e=this[i].querySelectorAll(t);for(var r=0;r<e.length;r++)n.push(e[r])}return b(n)},findParentFrom:function(t){t instanceof y&&(t=t[0]);var e,n=this[0];if(n===t)return null;for(;n.parentNode;){if((e=n.parentNode)===t)return b(n);n=e}return null},firstChild:function(){return this.children().eq(0)},lastChild:function(){return this.children().eq(-1)},next:function(t){if(0<this.length){if(t)return this[0].nextElementSibling&&b(this[0].nextElementSibling).is(t)?b([this[0].nextElementSibling]):b();if(this[0].nextElementSibling)return b([this[0].nextElementSibling])}return b()},parent:function(t){for(var e=[],n=0;n<this.length;n++)null===this[n].parentNode||t&&!b(this[n].parentNode).is(t)||e.push(this[n].parentNode);return b(a(e))},parents:function(t){for(var e=[],n=0;n<this.length;n++)for(var i=this[n].parentNode;i;)t&&!b(i).is(t)||e.push(i),i=i.parentNode;return b(a(e))},addClass:function(t){var n=this;return t&&t.split(" ").forEach(function(t){for(var e=0;e<n.length;e++)n[e]&&n[e].classList&&n[e].classList.add(t)}),this},removeClass:function(t){var n=this;return t.split(" ").forEach(function(t){for(var e=0;e<n.length;e++)n[e]&&n[e].classList&&n[e].classList.remove(t)}),this},hasClass:function(t){return!!this[0]&&this[0].classList.contains(t)},attr:function(t,e){if(1===arguments.length&&"string"==typeof t)return this[0]?this[0].getAttribute(t):void 0;for(var n=0;n<this.length;n++)if(2===arguments.length)this[n].setAttribute(t,e);else for(var i in t)this[n][i]=t[i],this[n].setAttribute(i,t[i]);return this},removeAttr:function(t){for(var e=0;e<this.length;e++)this[e].removeAttribute(t);return this},data:function(t,e){var n;if(void 0!==e){for(var i=0;i<this.length;i++)(n=this[i]).dataStorage||(n.dataStorage={}),n.dataStorage[t]=e;return this}if(n=this[0]){if(n.dataStorage)return n.dataStorage[t];var r=n.getAttribute("data-".concat(t));return/^\d+\.?\d*$/.test(r)?+r:r}},append:function(){for(var t,e=0;e<arguments.length;e++){t=e<0||arguments.length<=e?void 0:arguments[e];for(var n=0;n<this.length;n++)if("string"==typeof t){var i=d.createElement("div");for(i.innerHTML=t;i.firstChild;)this[n].appendChild(i.firstChild)}else if(t instanceof y)for(var r=0;r<t.length;r++)this[n].appendChild(t[r]);else this[n].appendChild(t)}return this},appendTo:function(t){return b(t).append(this),this},insertAfter:function(t){for(var e=b(t),n=0;n<this.length;n++)if(1===e.length)e[0].parentNode.insertBefore(this[n],e[0].nextSibling);else if(1<e.length)for(var i=0;i<e.length;i++)e[i].parentNode.insertBefore(this[n].cloneNode(!0),e[i].nextSibling)},insertBefore:function(t){for(var e=b(t),n=0;n<this.length;n++)if(1===e.length)e[0].parentNode.insertBefore(this[n],e[0]);else if(1<e.length)for(var i=0;i<e.length;i++)e[i].parentNode.insertBefore(this[n].cloneNode(!0),e[i])},prepend:function(t){for(var e,n=0;n<this.length;n++)if("string"==typeof t){var i=d.createElement("div");for(i.innerHTML=t,e=i.childNodes.length-1;0<=e;e--)this[n].insertBefore(i.childNodes[e],this[n].childNodes[0])}else if(t instanceof y)for(e=0;e<t.length;e++)this[n].insertBefore(t[e],this[n].childNodes[0]);else this[n].insertBefore(t,this[n].childNodes[0]);return this},prependTo:function(t){return b(t).prepend(this),this},replace:function(t){for(var e,n=b(t),i=0;i<this.length;i++)(e=this[i].parentNode)&&e.replaceChild(n[0].cloneNode(!0),this[i]);return this},remove:function(t){var e;if("number"!=typeof t)for(var n=0;n<this.length;n++)(e=this[n]).parentNode&&e.parentNode.removeChild(e);else(e=this[t])&&e.parentNode&&e.parentNode.removeChild(e);return this},is:function(t){var e,n,i=this[0];if(!i||void 0===t)return!1;if("string"==typeof t){if(i.matches)return i.matches(t);if(i.webkitMatchesSelector)return i.webkitMatchesSelector(t);if(i.msMatchesSelector)return i.msMatchesSelector(t);for(e=b(t),n=0;n<e.length;n++)if(e[n]===i)return!0;return!1}if(t===d)return i===d;if(t===u)return i===u;if(t.nodeType||t instanceof y){for(e=t.nodeType?[t]:t,n=0;n<e.length;n++)if(e[n]===i)return!0;return!1}return!1},isTextNode:function(){var t=this[0];return!(!t||!t.nodeType)&&3===t.nodeType},isEmpty:function(){return!m.strip(this.text())&&!this.find("img, video, audio")[0]},isFirstChildren:function(t){return this.children().eq(0).is(t)},isLastChildren:function(t){return this.children().eq(-1).is(t)},indexOf:function(t){t instanceof y&&(t=t[0]);for(var e=0;e<this.length;e++)if(this[e]===t)return e;return-1},every:function(t,e){if("function"!=typeof t)throw new TypeError("every(fn) fn is not a function");for(var n=this.length,i=2<=arguments.length?e:void 0,r=0;r<n;r++)if(t.call(i,b(this[r]),r))return!1;return!0},text:function(t){if(void 0===t)return this[0]?this[0].textContent.trim():"";for(var e=0;e<this.length;e++)this[e].textContent=t;return this},html:function(t){if(void 0===t)return this[0]?this[0].innerHTML:"";for(var e=0;e<this.length;e++)this[e].innerHTML=t;return this},nodeName:function(){return this[0]?this[0].nodeName.toLowerCase():null},nodeType:function(){return this[0]?this[0].nodeType:0},changeNodeName:function(t){for(var e=0;e<this.length;e++)this[e]=n(this[e],t);return this},hide:function(){for(var t=0;t<this.length;t++)this[t].style.display="none";return this},show:function(){for(var t=0;t<this.length;t++){var e=this[t];"none"===e.style.display&&(e.style.display=""),"none"===u.getComputedStyle(e,null).getPropertyValue("display")&&(e.style.display="block")}return this},on:function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];for(var i,r,o,s,a=e[0],h=e[1],l=e[2],c=0;c<this.length;c++)1!==(i=this[c]).nodeType&&i!==d&&i!==u||(r=a,o=h,s=l,(i=i).addEventListener?i.addEventListener(r,o,s):i.attachEvent?i.attachEvent(r,o):i["on".concat(r)]=o);return this},off:function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];for(var i,r,o,s,a=e[0],h=e[1],l=e[2],c=0;c<this.length;c++)1!==(i=this[c]).nodeType&&i!==d&&i!==u||(r=a,o=h,s=l,(i=i).removeEventListener?i.removeEventListener(r,o,s):i.detachEvent?i.detachEvent(r,o):i["on".concat(r)]=null);return this},trigger:function(){for(var t=arguments.length,e=new Array(t),n=0;n<t;n++)e[n]=arguments[n];for(var i=e[0].split(" "),r=e[1],o=0;o<i.length;o++)for(var s=i[o],a=0;a<this.length;a++){var h=this[a],l=void 0;try{l=new u.CustomEvent(s,{detail:r,bubbles:!0,cancelable:!0})}catch(t){(l=d.createEvent("Event")).initEvent(s,!0,!0),l.detail=r}h.dispatchEvent(l)}return this},css:function(t,e){var n;if(1===arguments.length){if("string"==typeof t)return this[0]?u.getComputedStyle(this[0],null).getPropertyValue(t):void 0;for(n=0;n<this.length;n+=1)for(var i in t)this[n].style[i]=t[i]}if(2===arguments.length&&"string"==typeof t)for(n=0;n<this.length;n+=1)this[n].style[t]=e;return this},styles:function(){return this[0]?u.getComputedStyle(this[0],null):{}},width:function(){return this[0]===u?u.innerWidth:0<this.length?parseFloat(this.css("width")):null},outerWidth:function(t){if(0<this.length){var e=this[0].offsetWidth;if(t){t=this.styles();return e+parseFloat(t.getPropertyValue("margin-right"))+parseFloat(t.getPropertyValue("margin-left"))}return e}return null},height:function(){return this[0]===u?u.innerHeight:0<this.length?parseFloat(this.css("height")):null},outerHeight:function(t){if(0<this.length){var e=this[0].offsetHeight;if(t){t=this.styles();return e+parseFloat(t.getPropertyValue("margin-top"))+parseFloat(t.getPropertyValue("margin-bottom"))}return e}return null},offset:function(){if(0<this.length){var t=this[0],e=t.getBoundingClientRect(),n=d.body,i=t.clientTop||n.clientTop||0,r=t.clientLeft||n.clientLeft||0,n=t===u?u.scrollY:t.scrollTop,t=t===u?u.scrollX:t.scrollLeft;return{top:e.top+n-i,left:e.left+t-r}}return null},each:function(t){if(!t)return this;for(var e=0;e<this.length;e++)if(!1===t.call(this[e],e,this[e]))return this;return this},scrollTop:function(t){if(this.length){var e="scrollTop"in this[0];return void 0===t?e?this[0].scrollTop:this[0].pageYOffset:this.each(e?function(){this.scrollTop=t}:function(){this.scrollTo(this.scrollX,t)})}}};var b=function(t,e){return new y(t,e)};function w(t){this.$parent=t,this.selection=null,this.range=null,this.currentNode=null,this.timer=null;t=t.lastChild();this.offset=t.text().length||0,t[0]&&this.setRange(t[0],this.offset)}w.prototype={constructor:w,init:function(){this.selection=window.getSelection();try{this.range=this.selection.getRangeAt(0)}catch(t){this.range=new Range}},setRange:function(t,e){var n,i=this;t instanceof y&&(t=t[0]),null===this.selection?this.init():this.selection.removeAllRanges(),(t=t||this.currentNode)&&(n=t.childNodes[t.childNodes.length-1]||t,/IMG|VIDEO|AUDIO/.test(n.nodeName)?(this.offset=1,n=n.parentNode):this.offset=void 0===e?Math.min(b(n).text().length,this.offset):e,this.range.setStart(n,this.offset),this.currentNode=t),this.range.collapse(!0),this.timer&&(clearTimeout(this.timer),this.timer=null),this.timer=setTimeout(function(){i.selection.addRange(i.range)},100)},getCurrentNode:function(t){try{this.range=this.selection.getRangeAt(0)}catch(t){this.range=new Range}this.currentNode=this.range.endContainer,this.offset=this.range.startOffset;var e=b(this.currentNode).findParentFrom(this.$parent)||this.$parent.lastChild();return t?e[0]:e}};function x(){}var $={head:null,className:"",headHeight:44,headLeftBtnClassName:"",headLeftBtnText:"",headTitle:"Expansion Panel",height:260,body:null,name:"expansion-panel",onHeadClick:x,onBodyClick:x,onBeforeShow:x,onShow:x,onBeforeHide:x,onHide:x};function N(t,e){var n=this;if(!e instanceof G)throw new TypeError("new ExpansionPanel() parameter error, arguments[1] is not a ZxEditor instance.");var i=this.options=Object.assign({},$,t);(this.editorInstance=e).expansionPanels.push(this),this.visible=!1,this.name=i.name,this.$body=b('<div class="body-wrapper" style="height:'.concat(i.height-i.headHeight,'px;"></div>')),this.$el=b('<div class="zx-editor-expansion-panel border-top"></div>'),e.$eventHandlers[this.name]={$target:this.$el,type:"click",handler:function(t){t.stopPropagation()}},this.$el.on("click",e.$eventHandlers[this.name].handler),0<i.headHeight&&(i.head?this.$head=b(i.head):(this.$head=b('<div class="head-wrapper border-bottom '.concat(i.headAlign,'" style="height:').concat(i.headHeight,"px;line-height:").concat(i.headHeight,'px;"><div class="l cur ').concat(i.headLeftBtnClassName,'">').concat(i.headLeftBtnText,"</div>").concat(i.headTitle||"","</div>")),t=this.$head.find(".l"),e.$eventHandlers[this.name+"HeadLeftBtn"]={$target:t,type:"click",handler:function(t){i.onHeadClick.call(n,t,"left-button")}},t.on("click",e.$eventHandlers[this.name+"HeadLeftBtn"].handler),t=b('<i class="switch" style="width:'.concat(i.headHeight,"px;height:").concat(i.headHeight,'px;"></i>')),this.$head.append(t),e.$eventHandlers[this.name+"HeadSwitch"]={$target:t,type:"click",handler:function(t){i.onHeadClick.call(n,t,"switch"),n.hide()}},t.on("click",e.$eventHandlers[this.name+"HeadSwitch"].handler)),this.$el.append(this.$head),this.$head.on("click",function(t){i.onHeadClick.call(n,t)}));try{this.$body.append(b(i.body))}catch(t){throw t}this.$body.on("click",function(t){i.onBodyClick.call(n,t)}),this.$el.append(this.$body),e.$editor.append(this.$el),this.init(this.options)}function C(t){if(this.$cursorNode.nodeName()!==t){var e,n,i=this.$cursorNode[0],r=i.childNodes,o=r.length;if("ul"===t){var s=d.createElement("ul");E(s,i);for(var a=0;a<o;a++)s.appendChild((e=r[a].cloneNode(!0),n=void 0,(n=d.createElement("li")).appendChild(e),n));return i.parentNode.replaceChild(s,i),void(this.$cursorNode=b(s))}if("UL"===i.nodeName){for(var h,l,c=0;c<o;c++)E(h=r[c].cloneNode(!0),i),l=this.changeNodeName(h,t),b(l).insertBefore(this.$cursorNode);return this.$cursorNode.remove(),void(this.$cursorNode=b(l))}this.$cursorNode.changeNodeName(t),this.cursor.setRange(this.$cursorNode)}}function E(t,e){var n=e.getAttribute("style"),i=e.id,e=e.className;n&&t.setAttribute("style",n),i&&(t.id=i),e&&(t.className=e)}N.prototype={constructor:N,init:function(t){t=t||this.options;var e=window.innerHeight;this.$el.css({height:"".concat(t.height,"px"),top:e+"px"})},show:function(){this.visible||(this.options.onBeforeShow.call(this),this.$el.removeClass("out").addClass("in"),this.visible=!0,this.editorInstance.emit("expansionPanelShow",this,this.editorInstance),this.options.onShow.call(this))},hide:function(){this.visible&&(this.options.onBeforeHide.call(this),this.$el.removeClass("in").addClass("out"),this.visible=!1,this.editorInstance.emit("expansionPanelHide",this,this.editorInstance),this.options.onHide.call(this))}};var H=34,T=["#333","#d0d0d0","#ff583d","#fdaa25","#44c67b","#14b2e0","#b065e2"];function S(t){var n=[];return t.forEach(function(t,e){/^#\w{3,6}$/.test(t)&&n.push({tag:"dd",attrs:{class:0===e?"active":"","data-color":7===(e=t.toLowerCase()).length?e:"#".concat(e[1]).concat(e[1]).concat(e[2]).concat(e[2]).concat(e[3]).concat(e[3])},child:[{tag:"i",attrs:{style:"background:".concat(t)}}]})}),n}function A(t){var e=this,i=this,n=[];n.push({tag:"dl",attrs:{class:"__style-wrapper border-bottom"},child:[{tag:"dd",attrs:{style:"font-weight: 800;","data-style":"fontWeight:800"},child:["B"]},{tag:"dd",attrs:{style:"font-style: italic;","data-style":"fontStyle:italic"},child:["I"]},{tag:"dd",attrs:{style:"text-decoration: line-through;","data-style":"textDecoration:line-through"},child:["abc"]},{tag:"dd",attrs:{style:"","data-style":"textAlign:left",class:"text-align--l"}},{tag:"dd",attrs:{style:"","data-style":"textAlign:center",class:"text-align--c"}},{tag:"dd",attrs:{style:"","data-style":"textAlign:right",class:"text-align--r"}}]});var r,o=Array.isArray(t.textStyleColors)?t.textStyleColors:T;o.length&&(r={tag:"dl",attrs:{class:"__color-wrapper border-bottom"},child:S(o)},n.push(r));n.push({tag:"dl",attrs:{class:"__tag-wrapper"},child:[{tag:"dd",attrs:{class:"__h2","data-tag":"h2"},child:["大标题",{tag:"i"}]},{tag:"dd",attrs:{class:"__h4","data-tag":"h4"},child:["小标题",{tag:"i"}]},{tag:"dd",attrs:{class:"__section active","data-tag":"section"},child:["正文",{tag:"i"}]},{tag:"dd",attrs:{class:"__blockquote","data-tag":"blockquote"},child:["引用",{tag:"i"}]},{tag:"dd",attrs:{class:"__ul","data-tag":"ul"},child:["无序列表",{tag:"i"}]}]});n=f({tag:"div",attrs:{class:"text-style-outer-wrapper"},child:n}),n=b(n);this.textStylePanel=new this.ExpansionPanel({headLeftBtnText:t.textStyleHeadLeftBtnText,headTitle:t.textStyleTitle,headAlign:t.textStyleHeadAlign,body:n,onHeadClick:function(t){"left-button"===t&&((t=i.$cursorNode[0]).className="",t.setAttribute("style",""),"SECTION"!==t.nodeName&&(i.$cursorNode=b(i.changeNodeName(t,"section"))),d(o[0]),c("section"),i.cursor.setRange(i.$cursorNode))}},this),n.find(".__style-wrapper").children().on("click",function(){var t=b(this).data("style").split(":"),e=t[0],n=i.$cursorNode[0];n.style[e]=n.style[e]===t[1]?"":t[1],i.cursor.setRange(),i.$content.trigger("input")});var s=n.find(".__color-wrapper"),a=s.children();a.on("click",function(){for(var t,e=0;e<a.length;e++)(t=b(a[e]))[0]!==this||t.hasClass("active")?t.hasClass("active")&&t.removeClass("active"):(t.addClass("active"),i.$cursorNode.css("color",t.data("color")));i.cursor.setRange(),i.$content.trigger("input")});var h=n.find(".__tag-wrapper"),l=h.children();function c(t){var e=h.find(".active");e.data("tag")!==t&&(e.removeClass("active"),h.find(".__".concat(t)).addClass("active"))}function d(t){var e,n=s.find(".active");if(n.data("color")!==t){n.removeClass("active");for(var i=0;i<a.length;i++)if((e=b(a[i])).data("color")===t){e.addClass("active");break}}}return l.on("click",function(){if(!b(this).hasClass("active")){for(var t,e=b(this).data("tag"),n=0;n<l.length;n++)(t=b(l[n]))[0]===this?(t.addClass("active"),C.call(i,e)):t.hasClass("active")&&t.removeClass("active");i.$content.trigger("input")}}),this.isIPhoneX&&h.css("margin-bottom",H+"px"),this.textStylePanel.resetActiveState=function(){var t=e.cursor.getCurrentNode();c(t.nodeName()),d(e.rgbToHex(t.css("color")))},{name:"text-style",className:"text-style-btn",events:{type:"click",handler:function(){e.textStylePanel.show()}}}}function B(){this.toolbar.addButton(A.call(this,this.options))}function k(){var n=this,i=this.options,t=b('<label class="toolbar-icon-pic" style="position:absolute;top:0;left:0;width:100%;height:100%;"></label>'),e=b('<input type="file" accept="image/*" style="display:none">');t.append(e);var r=e[0];this.$eventHandlers.selectPictureInputClick={$target:e,type:"click",handler:function(){r.value=""},capture:!1};var o=/^<\w+\b.*<\/\w+>$/.test(i.imageSectionTemp)?i.imageSectionTemp:'<section><img src="{url}"></section>',s=Object.assign({},n.options,{width:i.imageMaxWidth});this.$eventHandlers.selectPictureInputChange={$target:e,type:"change",handler:function(t){var e=r.files[0];n.emit("selectPictureInputChange",e,t,n),i.customPictureHandler||n.fileToBase64(e,s).then(function(t){t=b(o.replace("{url}",t.base64));t.find("img").attr({id:"zxEditor_img_"+ +new Date,alt:e.name}),n.insertElm(t)}).catch(function(t){n.emit("error",t,"fileToBase64")})},capture:!1},this.toolbar.addButton({name:"select-picture",el:t})}var P={name:"",className:"",el:null,innerHtml:"",style:"",events:null};function I(t,e){this.editorInstance=e,this.options=t,this.visible=t.toolbarBeenFixed,this.height=t.toolbarHeight,this.$el=b('<div class="zx-editor-toolbar-wrapper border-top '.concat(this.visible?"in":"out",'" style="height:').concat(this.height+(m.isIPhoneX?H:0),'px;"><dl class="inner-wrapper" style="height:').concat(this.height,'px;"></dl></div>')),e.$editor.append(this.$el),this.init(t)}function L(e){return new Promise(function(n){var i,t=e.clipboardData,r="";function o(t,e){++t===e&&n(m.strip(r))}t&&(i=t.items)&&0<i.length?function(){for(var t,e=i.length,n=0;n<e;n++)t=i[n],/^text\/plain/.test(t.type)?t.getAsString(function(t){r+=t,o(0,e)}):o(0,e)}():n("")})}function _(){var t,e,n=this.$content,i=b(u),r=this.options;function o(t){this.emit("click",t,this),this.$cursorNode=this.cursor.getCurrentNode(),this.checkPosition(),this.textStylePanel&&this.textStylePanel.visible&&this.textStylePanel.resetActiveState()}for(e in this.$eventHandlers.windowResize={$target:i,type:"resize",handler:function(t){this.emit("windowResize",t,this),this.toolbar.init(),this.expansionPanels.forEach(function(t){t.init()})}.bind(this)},this.$eventHandlers.contentPaste={$target:n,type:"paste",handler:function(t){var e=this;this.emit("paste",t,this),r.customPasteHandler||(t.preventDefault(),L(t).then(function(t){e.insertElm(m.removeHtmlTags(t))}))}.bind(this)},this.$eventHandlers.contentInput={$target:n,type:"input",handler:function(t){this.emit("input",t,this),this._checkEmpty(),this.checkPosition(),this.emit("change",t,this)}.bind(this)},this.$eventHandlers.contentFocus={$target:n,type:"focus",handler:function(t){this.emit("focus",t,this),this.options.toolbarBeenFixed||this.toolbar.show()}.bind(this)},this.$eventHandlers.contentBlur={$target:n,type:"blur",handler:function(t){this.emit("blur",t,this),this.$cursorNode=this.cursor.getCurrentNode(),this._checkChildSection(),this.options.toolbarBeenFixed||this.toolbar.hide()}.bind(this)},this.$eventHandlers.contentClick={$target:n,type:"click",handler:o.bind(this)},this.$eventHandlers.contentKeyup={$target:n,type:"keydown",handler:function(t){this.emit("keydown",t,this)}.bind(this)},this.$eventHandlers.contentKeyup={$target:n,type:"keyup",handler:function(t){this.emit("keyup",t,this),"Enter"!==t.key&&13!==t.keyCode||(this._checkChildSection(),o.call(this))}.bind(this)},this.$eventHandlers)(t=this.$eventHandlers[e]).$target.on(t.type,t.handler,t.capture)}function R(t,e){t&&"string"==typeof t&&e&&"function"==typeof e&&(this.customEvents[t]||(this.customEvents[t]=[]),this.customEvents[t].push(e))}function z(t){var e=this.customEvents[t];if(e)for(var n=Array.prototype.slice.call(arguments,1),i=0;i<e.length;i++)try{e[i].apply(null,n)}catch(t){this.emit("error",t,"emit")}}function M(t,e){var n;this.customEvents[t]&&("function"==typeof e&&this.customEvents[t]?0<=(n=this.customEvents[t].findIndex(function(t){return t===e}))&&this.customEvents[t].splice(n,1):(this.customEvents[t]=null,delete this.customEvents[t]))}function F(e,n,i,r){var o=d.createElement("img");o.src=e,o.setAttribute("alt",n.name),o.onload=function(){var t={element:o,base64:e,width:o.naturalWidth||o.width,height:o.naturalHeight||o.height,type:n.type,size:n.size,name:n.name,file:n};r(null,1<i.orientation?function(t,e){var n=d.createElement("canvas"),i=n.getContext("2d"),r=t.element,o=t.width,s=t.height;switch(n.width=o,n.height=s,e){case 6:n.width=s,n.height=o,i.rotate(Math.PI/2),i.drawImage(r,0,-s,o,s);break;case 3:i.rotate(Math.PI),i.drawImage(r,-o,-s,o,s);break;case 8:n.width=s,n.height=o,i.rotate(3*Math.PI/2),i.drawImage(r,-o,0,o,s);break;default:i.drawImage(r,0,0,o,s)}s=n.toDataURL(t.type);return r.src=s,Object.assign(t,{element:n,base64:s,width:n.width,height:n.height})}(t,i.orientation):t)},o.onerror=r}function O(t,e){var n=d.createElement("canvas");return n.width=e.cw,n.height=e.ch,n.getContext("2d").drawImage(t,e.sx,e.sy,e.sw,e.sh,0,0,n.width,n.height),n}function D(t,e){return parseInt(t/e*1e4)/1e4}var j={width:0,heigth:0,imageMaxSize:null,ignoreGif:!0,forceResize:!(I.prototype={constructor:I,init:function(t){t=t||this.options;t=u.innerHeight;this.$el.css({top:t+"px"}),this.visible&&this.editorInstance.emit("toolbarShow",this,this.editorInstance)},show:function(){this.visible||(this.$el.removeClass("out").addClass("in"),this.visible=!0,this.editorInstance.emit("toolbarShow",this,this.editorInstance))},hide:function(){this.visible&&(this.$el.removeClass("in").addClass("out"),this.visible=!1,this.editorInstance.emit("toolbarHide",this,this.editorInstance))},addButton:function(t,e){var n=this,i=Object.assign({},P,t);i.name||(i.name="toolbar-btn-"+ +new Date);var r=b('<dd class="icon-item" data-name="'.concat(i.name,'" style="').concat(i.style,'"></dd>'));i.el&&"object"===h(i.el)?r.append(b(i.el)):r.html(i.innerHtml),i.className&&r.addClass(i.className);t={};this.options.toolbarHeight&&(t.width=t.height=this.options.toolbarHeight+"px"),r.css(t);var o,s,a,t=this.$el.find(".inner-wrapper").children();"number"==typeof e&&e<t.length?r.insertBefore(t[e]):this.$el.find(".inner-wrapper").append(r),i.events&&(m.isObject(i.events)?(o=[]).push(i.events):Array.isArray(i.events)&&(o=i.events),a=this.editorInstance.$eventHandlers,o.forEach(function(t){if(!t||"string"!=typeof t.type||"function"!=typeof t.handler)throw new TypeError("Function addButton(opts), opts.events's parameter error.");s="toolbarBtnEvent_".concat(t.name),a[s]={$target:r,type:t.type,handler:t.handler.bind(n.editorInstance),capture:"boolean"==typeof t.capture&&t.capture},r.on(t.type,a[s].handler,a[s].capture)}))}})};function q(t){var e;if(!/^data:(\w+\/\w+);base64,(.+)/.test(t))throw new TypeError("base64ToBlobData(base64Data), base64Data non-base64 data!");e=RegExp.$1,t=RegExp.$2;for(var n=u.atob(t),i=new Uint8Array(n.length),r=0;r<n.length;r++)i[r]=n.charCodeAt(r);return new Blob([i],{type:e})}function U(r,t){return new Promise(function(e,n){if(!r||!r instanceof File)throw new TypeError("file is not a File object");var i=Object.assign({},j,t);/image\/.*/i.test(r.type)?void 0===u.EXIF?(i.orientation=0,W(r,i).then(e).catch(n)):u.EXIF.getData(r,function(){var t=u.EXIF.getAllTags(this)||{};i.orientation=t.Orientation,W(r,i).then(e).catch(n)}):n(new TypeError('"'.concat(r.name,'" is not Image File!')))})}function V(t){return u.URL.createObjectURL(t)}function W(r,o){return new Promise(function(n,i){var t=new FileReader;t.readAsDataURL(r),t.onload=function(){F(this.result,r,o,function(t,e){if(t)i(t);else{if("image/gif"===r.type&&o.ignoreGif)return e.data=r,e.url=V(r),e.raw=e,void n(e);e=function(t,e){var n=t.type||t.file.type,i=function(t,e,n){var i=m.int(n.width),r=m.int(n.height);if(!n.forceImageResize&&0<i&&t<i&&0<r&&e<r)return{sx:0,sy:0,sw:t,sh:e,scaling:1,cw:t,ch:e};var o,s=1,a=0,h=0,l=t,c=e,n=0;return 0<i&&0<r?(c=r,l=i,n=Math.floor(i*e/t),s=D(t,i),h=r<=n?(a=0,m.int((n-r)/2*s)):(s=D(e,r),o=Math.floor(r*t/e),n=r,a=m.int((o-i)/2*s),0)):0<i?(s=D(t,i),l=i,c=Math.floor(i*e/t)):0<r&&(s=D(e,r),l=Math.floor(r*t/e),c=r),{sx:a,sy:h,sw:m.int(l*s),sh:m.int(c*s),scaling:s,cw:l,ch:c}}(t.width,t.height,e),r=t.element,o=2,s=i.sw,a=i.sh,h=i.sx,l=i.sy;if(i.scaling>o)for(o=i.scaling;2<o;)r=O(r,{cw:i.cw*o,ch:i.ch*o,sx:h,sy:l,sw:s,sh:a}),s=r.width,a=r.height,h=l=0,--o;var c=(r=O(r,{cw:i.cw,ch:i.ch,sx:h,sy:l,sw:s,sh:a})).toDataURL(n),e=q(c);return{element:r,type:n,width:i.cw,height:i.ch,data:e,base64:c,size:e.size,url:V(e),raw:t}}(e,o);0<o.imageMaxSize&&e.size/1024>o.imageMaxSize?i(new RangeError('File size "'.concat(e.size/1024,'" out of range, limit is ').concat(o.imageMaxSize,"Kib"))):n(e)}})},t.onerror=i})}var X={editable:!0,fixed:!1,placeholder:"Enter...",placeholderColor:"",lineHeight:1.5,paragraphTailSpacing:"",cursorColor:"",textColor:"",cursorOffsetTop:30,customPasteHandler:!1,toolbarBeenFixed:!0,toolbarHeight:50,toolbarBtns:["select-picture","text-style"],customPictureHandler:!1,imageMaxWidth:720,imageMaxSize:20480,imageSectionTemp:'<section class="child-is-picture"><img src="{url}"></section>',ignoreGif:!0,forceImageResize:!1,textStyleColors:null,textStyleTitle:"Set Style",textStyleHeadLeftBtnText:"Clear style",textStyleHeadAlign:"center",mainColor:"",borderColor:""};function G(t,e){if(!this instanceof G)throw new Error("ZxEditor is a constructor and should be called with the `new` keyword");if(this.$wrapper=b(t),!this.$wrapper[0])throw new Error("Can't found '".concat(t,"' Node in document!"));this.version="3.0.4-alpha",this.$=b,this.ExpansionPanel=N,this.options=Object.assign(X,e),this.init(this.options)}return G.prototype={constructor:G,init:function(t){t=t||this.options,this.$eventHandlers={},this.customEvents={},function(t){for(var e in t.prototype.on=R,t.prototype.emit=z,t.prototype.off=M,t.prototype.getPasteText=L,m)m.hasOwnProperty(e)&&(t.prototype[e]=m[e]);t.prototype.blobToUrl=V,t.prototype.base64ToBlobData=q,t.prototype.fileToBase64=U,t.prototype.createElement=s,t.prototype.createVdom=f}(G),this.expansionPanels=[],function(t){var e=this;this.$content=b('<div class="zx-editor-content-wrapper is-empty" contenteditable="'.concat(t.editable,'"><section><br></section></div>')),this.setContentHeight(t),this.$editor=b('<div class="zx-editor"></div>'),this.$editor.append(this.$content),this.toolbar=new I(t,this),this.$wrapper.append(this.$editor),this.lineHeight=m.int(this.$content.styles().lineHeight),t.toolbarBtns.forEach(function(t){"select-picture"===t?e.plugin(k):"text-style"===t&&e.plugin(B)})}.call(this,t),function(t){var e,n,i,r=".zx-editor .m-color{color:".concat(t.mainColor," !important;}.zx-editor .text-style-outer-wrapper .__tag-wrapper dd i {border-color:").concat(t.mainColor," !important;}");(t.lineHeight||t.cursorColor||t.textColor)&&(e=t.lineHeight?"line-height:".concat(t.lineHeight,";"):"",n=t.cursorColor?"caret-color:".concat(t.cursorColor,";"):"",i=t.textColor?"color:".concat(t.textColor,";"):"",r+=".zx-editor .zx-editor-content-wrapper{".concat(e+n+i,"}")),t.paragraphTailSpacing&&(r+=".zx-editor .zx-editor-content-wrapper > *{margin-bottom:".concat(t.paragraphTailSpacing,";}")),(t.placeholder||t.placeholderColor)&&(n=t.placeholder?"content:'".concat(t.placeholder,"';"):"",i=t.placeholderColor?"color:".concat(t.placeholderColor,";"):"",r+=".zx-editor .zx-editor-content-wrapper.is-empty:before{".concat(n+i,"}")),t.borderColor&&(r+=".zx-editor .border-top:before, .zx-editor .border-bottom:after {border-color: ".concat(t.borderColor)),b("head").append('<style type="text/css">'.concat(r,"</style>"))}.call(this,t),this.cursor=new w(this.$content),this.$cursorNode=this.cursor.getCurrentNode(),_.call(this)},insertElm:function(t){if(t){var e,n=this.$cursorNode;if("string"==typeof t)var i,r,o=n.isEmpty()?(n.text(t),e=n,t.length):n.children().every(function(t){return t.isTextNode()})?(r=this.cursor.offset,i=(i=n.text()).substr(0,r)+t+i.substr(r),n.text(i),e=n,t.length+r):((r=b("<section>".concat(t,"</section>"))).insertAfter(n),e=r,t.length);else for(var s=b(t),a=0;a<s.length;a++){var h,l=(h=b(s[a])).nodeName();"section"!==l&&(1!==h.nodeType()||/video|img|audio/.test(l)?h=b("<section></section>").append(h):h.changeNodeName("section")),n.isEmpty()?n.next()[0]&&n.next().isEmpty()?n.replace(h):h.insertBefore(n):h.insertAfter(n),o=h.next()[0]?(e=h).isTextNode()?h.text().length:0:(h=b("<section><br></section>"),this.$content.append(h),e=h,0)}this._checkChildSection(),this.$content.trigger("input"),this.cursor.setRange(e,o)}},insertBlankLine:function(){var t=b("<section><br></section>");this.insertElm(t),this.cursor.setRange(t,0)},_checkEmpty:function(){var t=this.$content;t.isEmpty()?t.addClass("is-empty"):t.hasClass("is-empty")&&t.removeClass("is-empty")},_checkChildSection:function(){this.$cursorNode||(this.$cursorNode=this.cursor.getCurrentNode());for(var t,e,n,i=this.$cursorNode[0],r=this.$content[0],o=r.childNodes,s=0;s<o.length;s++)1===(n=o[s]).nodeType?/SECTION|H1|H2|H3|H4|BLOCKQUOTE|UL/.test(n.nodeName)||(t=n===i,n=m.changeNodeName(n,"section"),t&&(this.$cursorNode=b(n),this.cursor.setRange(n))):((e=b("<section></section>")).append(n.cloneNode()),r.replaceChild(e[0],n),this.$cursorNode=e,this.cursor.setRange(e))},remove:function(){this.setHtml()},setHtml:function(t){this.$content.html(t||"<section><br></section>"),this._checkChildSection(),this.cursor.setRange(this.$content.firstChild(),0),this.$content.trigger("input")},getHtml:function(){return this.$content.html()},getText:function(){return this.$content.text()},destroy:function(){var t,e,n,i;for(e in this.$eventHandlers)(t=this.$eventHandlers[e]).$target.off(t.type,t.handler,t.capture),delete this.$eventHandlers[e];for(n in this.customEvents)t=this.customEvents[n],this.off(n);for(i in this.cursor=null,this.toolbar=null,this.textStylePanel=null,this)this[i]instanceof N&&(this[i]=null);this.$editor.remove()},setContentHeight:function(t){var e=window.innerHeight,n={marginBottom:e+"px"};t.height?n.height="number"==typeof t.height?t.height+"px":t.height:n.minHeight=(m.int(t.minHeight)||e)+"px",this.$content.css(n)},getBase64Images:function(){for(var t,e,n=[],i=this.$content.find("img"),r=0;r<i.length;r++)e=(t=i[r]).src,/^data:.+?;base64,/.test(e)&&n.push({id:t.id,base64:e,blob:q(e)});return n},setImageSrc:function(t,e){t=this.$content.find("#"+t)[0];return!!t&&(t.src=e,t.removeAttribute("id"),!0)},plugin:function(t){"function"==typeof t&&t.call(this)},checkPosition:function(){var t=this.$cursorNode=this.cursor.getCurrentNode(),e=this.cursor.offset,n=t.text().length,i=t.height(),r=t.offset().top,t=0,n=n/(i/this.lineHeight),n=((i>this.lineHeight?Math.floor(e/n):1)-1)*this.lineHeight;this.options.fixed||(n>window.innerHeight/4&&(t=n),n=b(window).scrollTop(),b(window).scrollTop(n+r+t-this.options.cursorOffsetTop))}},G});
//# sourceMappingURL=zx-editor.min.js.map