/
coloreditor.min.js
1 lines (1 loc) · 10.5 KB
/
coloreditor.min.js
1
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.ColorEditor=e():t.ColorEditor=e()}(this,function(){return function(t){function e(o){if(r[o])return r[o].exports;var i=r[o]={exports:{},id:o,loaded:!1};return t[o].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";var o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var o in r)Object.prototype.hasOwnProperty.call(r,o)&&(t[o]=r[o])}return t},i=r(1),a=r(2),s=r(3),n=r(5),l=r(6),c=r(4),u=React.createClass({displayName:"ColorEditor",propTypes:{colors:React.PropTypes.object,formats:React.PropTypes.object,importFromUrl:React.PropTypes.bool},getInitialState:function(){return{value:""}},getDefaultProps:function(){return{colors:c.colors,formats:c.formats,importFromUrl:!1}},getImporter:function(){return new l(this.props.colors,this.props.formats)},setValue:function(t){console.log("Importing: "+t);var e=this.getImporter().createHtml(t),r=this.getEditable();r.innerHTML="",e.forEach(function(t){r.appendChild(t)}),console.log("Imported: "+r.innerHTML),this.setState({value:t})},componentDidMount:function(){if(this.getEditable().addEventListener("paste",this.onPaste),this.props.importFromUrl){var t=l.getCodeFromUrl();t&&this.setValue(t)}},componentWillUnmount:function(){this.getEditable().removeEventListener("paste",this.onPaste),this.urlTask&&clearTimeout(this.urlTask)},onPaste:function(t){t.preventDefault();var e=t.clipboardData.getData("text/plain");if(c.formatRegex.test(e)){var r=document.createElement("DIV");this.getImporter().createHtml(e).forEach(function(t){r.appendChild(t)}),e=r.innerHTML,console.log("Inserting: "+e)}document.execCommand("insertHTML",!1,e)},onSelectFormat:function(t){switch(t){case"bold":this.execCommand("bold");break;case"italic":this.execCommand("italic");break;case"reset":this.execCommand("removeFormat");break;case"strikethrough":this.execCommand("strikeThrough");break;case"underline":this.execCommand("underline")}},onSelectColor:function(t){this.execCommand("foreColor","#"+t)},execCommand:function(t,e){document.execCommand(t,!1,e),this.getEditable().focus()},updateUrl:function(){clearTimeout(this.urlTask),this.props.importFromUrl&&(window.location.hash="/editor_message="+encodeURIComponent(this.state.value))},onInput:function(t){this.urlTask&&clearTimeout(this.urlTask),this.urlTask=setTimeout(this.updateUrl,350),this.setState({value:this.parseHtml()})},parseHtml:function(){return new n(this.props.colors,this.props.formats).parse(this.getEditable().childNodes)},getEditable:function(){return this.refs.editable},render:function(){var t=[];for(var e in this.props.colors){var r=this.props.colors[e];t.push(React.createElement("li",{key:e},React.createElement(i,{color:r,onClick:this.onSelectColor})))}for(var e in this.props.formats){var n=this.props.formats[e];t.push(React.createElement("li",{key:e},React.createElement(a,{format:n,onClick:this.onSelectFormat})))}return React.createElement("div",o({},this.props,{className:"coloreditor"}),React.createElement("div",{className:"coloreditor-buttons"},React.createElement("ul",null,t)),React.createElement("div",{className:"coloreditor-editable",onInput:this.onInput,autoCapitalize:"off",autoCorrect:"off",contentEditable:!0,spellCheck:!1,ref:"editable"}),React.createElement(s,{value:this.state.value}))}});t.exports=u},function(t,e){"use strict";var r=React.createClass({displayName:"ColorButton",propTypes:{color:React.PropTypes.string.isRequired,onClick:React.PropTypes.func},onClick:function(){this.props.onClick&&this.props.onClick(this.props.color)},render:function(){return React.createElement("button",{className:"coloreditor-colorbutton",onClick:this.onClick,style:{backgroundColor:"#"+this.props.color}})}});t.exports=r},function(t,e){"use strict";var r=React.createClass({displayName:"FormatButton",propTypes:{format:React.PropTypes.string.isRequired,onClick:React.PropTypes.func},onClick:function(){this.props.onClick&&this.props.onClick(this.props.format)},render:function(){return React.createElement("button",{className:"coloreditor-formatbutton",onClick:this.onClick},this.props.format[0])}});t.exports=r},function(t,e,r){"use strict";var o=r(4),i=React.createClass({displayName:"ResultPane",propTypes:{value:React.PropTypes.string.isRequired},getInitialState:function(){return{prefix:o.colorPrefixBukkit}},shouldComponentUpdate:function(t,e){return t.value!==this.props.value||e.prefix!==this.state.prefix},setPrefix:function(t){this.setState({prefix:t})},render:function(){var t=this.props.value;return t=t.replace(o.formatRegex,'<span class="highlight">'+this.state.prefix+"$2</span>"),t=t.replace(/\n/g,"<br />"),React.createElement("div",{className:"coloreditor-result"},React.createElement("div",{className:"coloreditor-code",dangerouslySetInnerHTML:{__html:t}}),React.createElement("div",{className:"coloreditor-prefix"},React.createElement("button",{className:this.state.prefix===o.colorPrefixVanilla&&"active",onClick:this.setPrefix.bind(this,o.colorPrefixVanilla)},"Vanilla ",o.colorPrefixVanilla),React.createElement("button",{className:this.state.prefix===o.colorPrefixBukkit&&"active",onClick:this.setPrefix.bind(this,o.colorPrefixBukkit)},"Bukkit ",o.colorPrefixBukkit)))}});t.exports=i},function(t,e){"use strict";var r={colors:{0:"000000",1:"0000AA",2:"00AA00",3:"00AAAA",4:"AA0000",5:"AA00AA",6:"FFAA00",7:"AAAAAA",8:"555555",9:"5555FF",a:"55FF55",b:"55FFFF",c:"FF5555",d:"FF55FF",e:"FFFF55",f:"FFFFFF"},formats:{l:"bold",n:"underline",o:"italic",m:"strikethrough",r:"reset"},defaultColor:"f",colorPrefixBukkit:"&",colorPrefixVanilla:"§",formatRegex:/([&|§])([0-9a-flmnor])/g};t.exports=r},function(t,e,r){"use strict";function o(t,e,r){this.colors=t,this.formats=e,this.prefix=r||"&",this.lastColor=i.defaultColor,this.lastFormats=[],this.lastDivBreak=!1,this.hasWrittenContent=!1}var i=r(4);o.prototype.parse=function(t){return this.parseNodeTree(t,i.defaultColor,[],0)},o.prototype.addLastFormat=function(t){this.addUniqueValue(this.lastFormats,t)},o.prototype.addUniqueValue=function(t,e){-1===t.indexOf(e)&&t.push(e)},o.prototype.removeValue=function(t,e){var r=t.indexOf(e);r>-1&&t.splice(r,1)},o.prototype.parseNodeTree=function(t,e,r,o){for(var i="",a=0;a<t.length;++a){var s=t[a],n=s.nodeType;if(3===n){if(this.lastColor!=e&&(i+=this.prefix+e,this.lastColor=e,this.lastFormats=[]),r.length>0){var l=!1;if(this.lastFormats.length>r.length)l=!0;else for(var c=0;c<this.lastFormats.length;++c)if(-1===r.indexOf(this.lastFormats[c])){l=!0;break}l&&(i+=this.prefix+e,this.lastFormats=[]),r.forEach(function(t){-1===this.lastFormats.indexOf(t)&&(i+=this.prefix+t,this.addLastFormat(t))}.bind(this))}else this.lastFormats.length>0&&(i+=this.prefix+e,this.lastFormats=[]);this.lastDivBreak=!1,this.hasWrittenContent=!0,i+=s.textContent.replace(/\n/,"")}else if(1===n){var u=e,p=r.slice();if(s.hasAttribute("color")&&(u=this.parseColor(s.getAttribute("color"))),s.hasAttribute("style")){s.style.color&&(u=this.parseColor(getComputedStyle(s).color));var h=s.style.fontWeight;"bold"==h?this.addUniqueValue(p,this.getFormatCode("bold")):("normal"==h||"initial"==h||"400"==h)&&this.removeValue(p,"bold");var f=s.style.fontStyle;"italic"==f?this.addUniqueValue(p,this.getFormatCode("italic")):("normal"==f||"initial"==f)&&this.removeValue(p,"italic");var d=s.style.textDecoration;if(d){var m=d.split(" ");this.removeValue(p,"underline"),this.removeValue(p,"strikethrough"),m.forEach(function(t){"underline"==t?this.addUniqueValue(p,this.getFormatCode("underline")):"line-through"==t&&this.addUniqueValue(p,this.getFormatCode("strikethrough"))}.bind(this))}}if("B"==s.tagName)this.addUniqueValue(p,this.getFormatCode("bold"));else if("I"==s.tagName)this.addUniqueValue(p,this.getFormatCode("italic"));else if("U"==s.tagName)this.addUniqueValue(p,this.getFormatCode("underline"));else if("STRIKE"==s.tagName)this.addUniqueValue(p,this.getFormatCode("strikethrough"));else if("BR"==s.tagName){i+="\n";continue}""!=i&&"DIV"==s.tagName&&!this.lastDivBreak&&this.hasWrittenContent&&(this.lastDivBreak=!0,i+="\n"),i+=this.parseNodeTree(s.childNodes,u,p,o+1)}}return i},o.prototype.parseColor=function(t){if(0===t.indexOf("#"))return this.getColorCode(t);var e=t.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i),r=this.rgbToHex(parseInt(e[1]),parseInt(e[2]),parseInt(e[3]));return this.getColorCode(r)},o.prototype.componentToHex=function(t){var e=t.toString(16);return 1==e.length?"0"+e:e},o.prototype.rgbToHex=function(t,e,r){return"#"+this.componentToHex(t)+this.componentToHex(e)+this.componentToHex(r)},o.prototype.getColorCode=function(t){for(var e in this.colors)if(this.colors[e]==t.replace("#","").toUpperCase())return e;return"?"},o.prototype.getFormatCode=function(t){for(var e in this.formats)if(this.formats[e]==t)return e;return"?"},t.exports=o},function(t,e,r){"use strict";function o(t,e){this.colors=t,this.formats=e}var i=r(4);o.prototype.createHtml=function(t){for(var e=t.split(/\n/),r=this.colors[i.defaultColor],o={},a=[],s=0;s<e.length;++s){var n=e[s],l=this.createLine(n,r,o);if(r=l[1],o=l[2],0==s)l[0].forEach(function(t){a.push(t)});else{var c=document.createElement("DIV");l[0].forEach(function(t){c.appendChild(t)}),a.push(c)}}return a},o.prototype.createLine=function(t,e,r){for(var o=!1,a="",s=e,n=r,l="code",c=[],u=0,p=0;p<t.length;++p){var h=t[p];if(o)o=!1;else{var f=!1;if(h==i.colorPrefixBukkit||h==i.colorPrefixVanilla){var d=t[p+1],m="";"r"==d&&(d=i.defaultColor);var v=s,g={};for(var C in n)g[C]=n[C];this.colors[d]?(m="color",s=this.colors[d]):this.formats[d]?(m="format",n[this.formats[d]]=!0):f=!0,f||("char"==l&&(c.push(this.createFormattedNode(u,a,v,g)),a="",++u),("r"==d||"color"==m)&&(n={}),l="code",o=!0)}else f=!0;f&&(l="char",a+=h)}}var x=!1;return 0===u&&""==a.trim()?c.push(document.createElement("BR")):(""!=a||0==u)&&c.push(this.createFormattedNode(u,a,s,n)),[c,s,n,x]},o.prototype.createFormattedNode=function(t,e,r,o){var i=document.createElement("FONT");i.setAttribute("color","#"+r);var a=i;for(var s in o){var n="";switch(s){case"bold":n="B";break;case"italic":n="I";break;case"underline":n="U";break;case"strikethrough":n="STRIKE"}var l=document.createElement(n);a.appendChild(l),a=l}return a.innerHTML=e,i},o.getUrlQuery=function(){var t=window.location.hash.substr(2).split("&");if(""==t)return{};for(var e={},r=0;r<t.length;++r){var o=t[r].split("=",2);1==o.length?e[o[0]]="":e[o[0]]=decodeURIComponent(o[1].replace(/\+/g," "))}return e},o.getCodeFromUrl=function(){var t=o.getUrlQuery().editor_message;return t?t:null},t.exports=o}])});