@acarl005/codeflask
Advanced tools
Comparing version 2.0.8 to 2.0.9
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.CodeFlask=t()}(this,function(){"use strict";var e,t,n,o='"SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace',s="\n .codeflask {\n position: absolute;\n width: 100%;\n height: 100%;\n overflow: hidden;\n }\n\n .codeflask, .codeflask * {\n box-sizing: border-box;\n }\n\n .codeflask__pre {\n pointer-events: none;\n z-index: 3;\n overflow: hidden;\n }\n\n .codeflask__textarea {\n background: none;\n border: none;\n color: "+(e="caret-color",t="#000",("undefined"!=typeof CSS?CSS.supports(e,t):"undefined"!=typeof document&&(n=(n=e).split("-").filter(function(e){return!!e}).map(function(e){return e[0].toUpperCase()+e.substr(1)}).join(""))[0].toLowerCase()+n.substr(1)in document.body.style)?"#fff":"#ccc")+";\n z-index: 1;\n resize: none;\n font-family: "+o+";\n -webkit-appearance: pre;\n caret-color: #111;\n z-index: 2;\n width: 100%;\n height: 100%;\n }\n\n .codeflask--has-line-numbers .codeflask__textarea {\n width: calc(100% - 40px);\n }\n\n .codeflask__code {\n display: block;\n font-family: "+o+";\n overflow: hidden;\n }\n\n .codeflask__flatten {\n padding: 10px;\n font-size: 16px;\n line-height: 20px;\n white-space: pre;\n position: absolute;\n top: 0;\n left: 0;\n overflow: auto;\n margin: 0 !important;\n outline: none;\n text-align: left;\n }\n\n .codeflask--has-line-numbers .codeflask__flatten {\n width: calc(100% - 40px);\n left: 40px;\n }\n\n .codeflask__line-highlight {\n position: absolute;\n top: 10px;\n left: 0;\n width: 100%;\n height: 20px;\n background: rgba(0,0,0,0.1);\n z-index: 1;\n }\n\n .codeflask__lines {\n padding: 10px 4px;\n font-size: 12px;\n line-height: 20px;\n font-family: 'Cousine', monospace;\n position: absolute;\n left: 0;\n top: 0;\n width: 40px;\n min-height: 100%;\n text-align: right;\n color: #999;\n border-right: 1px solid #f8f8f8;\n background-color: #fafafa;\n z-index: 5;\n }\n\n .codeflask__lines__line {\n display: block;\n }\n\n .codeflask.codeflask--has-line-numbers {\n padding-left: 40px;\n }\n\n .codeflask.codeflask--has-line-numbers:before {\n content: '';\n position: absolute;\n left: 0;\n top: 0;\n width: 40px;\n height: 100%;\n background: #eee;\n z-index: 1;\n }\n";function i(e,t,n){var o=t||"codeflask-style",s=n||document.head;if(!e)return!1;if(document.getElementById(o))return!0;var i=document.createElement("style");return i.innerHTML=e,i.id=o,s.appendChild(i),!0}var a={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function r(e){return String(e).replace(/[&<>"'`=/]/g,function(e){return a[e]})}var l=function(e,t,n){if(!e)throw Error("CodeFlask expects 1st parameter to be an Element or a String selector");if(!t)throw Error("CodeFlask expects 2nd parameter to be the Prism peer dependency");if(!n)throw Error("CodeFlask expects 3rd parameter to be an object containing options");if(e.nodeType)this.editorRoot=e;else{var o=document.querySelector(e);o&&(this.editorRoot=o)}this.Prism=t,this.opts=n,this.startEditor()};return l.prototype.startEditor=function(){if(!i(s,null,this.opts.styleParent))throw Error("Failed to inject CodeFlask CSS.");this.createWrapper(),this.createTextarea(),this.createPre(),this.createCode(),this.runOptions(),this.listenTextarea(),this.populateDefault(),this.updateCode(this.code)},l.prototype.createWrapper=function(){this.code=this.editorRoot.innerHTML,this.editorRoot.innerHTML="",this.elWrapper=this.createElement("div",this.editorRoot),this.elWrapper.classList.add("codeflask")},l.prototype.createTextarea=function(){this.elTextarea=this.createElement("textarea",this.elWrapper),this.elTextarea.classList.add("codeflask__textarea","codeflask__flatten")},l.prototype.createPre=function(){this.elPre=this.createElement("pre",this.elWrapper),this.elPre.classList.add("codeflask__pre","codeflask__flatten")},l.prototype.createCode=function(){this.elCode=this.createElement("code",this.elPre),this.elCode.classList.add("codeflask__code","language-"+(this.opts.language||"html"))},l.prototype.createLineNumbers=function(){this.elLineNumbers=this.createElement("div",this.elWrapper),this.elLineNumbers.classList.add("codeflask__lines"),this.setLineNumber()},l.prototype.createElement=function(e,t){var n=document.createElement(e);return t.appendChild(n),n},l.prototype.runOptions=function(){this.opts.rtl=this.opts.rtl||!1,this.opts.tabSize=this.opts.tabSize||2,this.opts.enableAutocorrect=this.opts.enableAutocorrect||!1,this.opts.lineNumbers=this.opts.lineNumbers||!1,this.opts.defaultTheme=!1!==this.opts.defaultTheme,this.opts.areaId=this.opts.areaId||null,this.opts.ariaLabelledby=this.opts.ariaLabelledby||null,this.opts.readonly=this.opts.readonly||null,this.opts.customEventListeners=this.opts.customEventListeners||{},"boolean"!=typeof this.opts.handleTabs&&(this.opts.handleTabs=!0),"boolean"!=typeof this.opts.handleSelfClosingCharacters&&(this.opts.handleSelfClosingCharacters=!0),"boolean"!=typeof this.opts.handleNewLineIndentation&&(this.opts.handleNewLineIndentation=!0),!0===this.opts.rtl&&(this.elTextarea.setAttribute("dir","rtl"),this.elPre.setAttribute("dir","rtl")),!1===this.opts.enableAutocorrect&&(this.elTextarea.setAttribute("spellcheck","false"),this.elTextarea.setAttribute("autocapitalize","off"),this.elTextarea.setAttribute("autocomplete","off"),this.elTextarea.setAttribute("autocorrect","off")),this.opts.lineNumbers&&(this.elWrapper.classList.add("codeflask--has-line-numbers"),this.createLineNumbers()),this.opts.defaultTheme&&i("\n.codeflask {\n background: #fff;\n color: #4f559c;\n}\n\n.codeflask .token.punctuation {\n color: #4a4a4a;\n}\n\n.codeflask .token.keyword {\n color: #8500ff;\n}\n\n.codeflask .token.operator {\n color: #ff5598;\n}\n\n.codeflask .token.string {\n color: #41ad8f;\n}\n\n.codeflask .token.comment {\n color: #9badb7;\n}\n\n.codeflask .token.function {\n color: #8500ff;\n}\n\n.codeflask .token.boolean {\n color: #8500ff;\n}\n\n.codeflask .token.number {\n color: #8500ff;\n}\n\n.codeflask .token.selector {\n color: #8500ff;\n}\n\n.codeflask .token.property {\n color: #8500ff;\n}\n\n.codeflask .token.tag {\n color: #8500ff;\n}\n\n.codeflask .token.attr-value {\n color: #8500ff;\n}\n","theme-default",this.opts.styleParent),this.opts.areaId&&this.elTextarea.setAttribute("id",this.opts.areaId),this.opts.ariaLabelledby&&this.elTextarea.setAttribute("aria-labelledby",this.opts.ariaLabelledby),this.opts.readonly&&this.enableReadonlyMode()},l.prototype.updateLineNumbersCount=function(){for(var e="",t=1;t<=this.lineNumber;t++)e=e+'<span class="codeflask__lines__line">'+t+"</span>";this.elLineNumbers.innerHTML=e},l.prototype.listenTextarea=function(){var e=this,t=this.opts.customEventListeners;for(var n in t)t.hasOwnProperty(n)&&this.elTextarea.addEventListener(n,t[n]);this.elTextarea.addEventListener("input",function(t){e.code=t.target.value,e.elCode.innerHTML=r(t.target.value),e.highlight(),setTimeout(function(){e.runUpdate(),e.setLineNumber()},1)}),this.elTextarea.addEventListener("keydown",function(t){e.opts.readonly||(e.handleTabs(t),e.handleSelfClosingCharacters(t),e.handleNewLineIndentation(t))}),this.elTextarea.addEventListener("scroll",function(t){e.elPre.style.transform="translate3d(-"+t.target.scrollLeft+"px, -"+t.target.scrollTop+"px, 0)",e.elLineNumbers&&(e.elLineNumbers.style.transform="translate3d(0, -"+t.target.scrollTop+"px, 0)",e.elPre.style.width="calc(100% - 40px + "+t.target.scrollLeft+"px)")})},l.prototype.handleTabs=function(e){if(this.opts.handleTabs){if(9!==e.keyCode)return;e.preventDefault();var t=this.elTextarea,n=t.selectionDirection,o=t.selectionStart,s=t.selectionEnd,i=t.value,a=i.substr(0,o),r=i.substring(o,s),l=i.substring(s),c=" ".repeat(this.opts.tabSize);if(o!==s&&r.length>=c.length){var d=o-a.split("\n").pop().length,h=c.length,p=c.length;if(e.shiftKey)i.substr(d,c.length)===c?(h=-h,d>o?(r=r.substring(0,d)+r.substring(d+c.length),p=0):d===o?(h=0,p=0,r=r.substring(c.length)):(p=-p,a=a.substring(0,d)+a.substring(d+c.length))):(h=0,p=0),r=r.replace(new RegExp("\n"+c.split("").join("\\"),"g"),"\n");else a=a.substr(0,d)+c+a.substring(d,o),r=r.replace(/\n/g,"\n"+c);t.value=a+r+l,t.selectionStart=o+h,t.selectionEnd=o+r.length+p,t.selectionDirection=n}else t.value=a+c+l,t.selectionStart=o+c.length,t.selectionEnd=o+c.length;var u=t.value;this.updateCode(u),this.elTextarea.selectionEnd=s+this.opts.tabSize}},l.prototype.handleSelfClosingCharacters=function(e){if(this.opts.handleSelfClosingCharacters){var t=e.key;if(["(","[","{","<","'",'"'].includes(t)||[")","]","}",">","'",'"'].includes(t))switch(t){case"(":case")":this.closeCharacter(t);break;case"[":case"]":this.closeCharacter(t);break;case"{":case"}":this.closeCharacter(t);break;case"<":case">":case"'":case'"':this.closeCharacter(t)}}},l.prototype.setLineNumber=function(){this.lineNumber=this.code.split("\n").length,this.opts.lineNumbers&&this.updateLineNumbersCount()},l.prototype.handleNewLineIndentation=function(e){if(this.opts.handleNewLineIndentation&&13===e.keyCode){e.preventDefault();var t=this.elTextarea,n=t.selectionStart,o=t.selectionEnd,s=t.value,i=s.substr(0,n),a=s.substring(o),r=s.lastIndexOf("\n",n-1),l=r+s.slice(r+1).search(/[^ ]|$/),c=l>r?l-r:0,d=i+"\n"+" ".repeat(c)+a;t.value=d,t.selectionStart=n+c+1,t.selectionEnd=n+c+1,this.updateCode(t.value)}},l.prototype.closeCharacter=function(e){var t=this.elTextarea.selectionStart,n=this.elTextarea.selectionEnd;if(this.skipCloseChar(e)){var o=this.code.substr(n,1)===e,s=o?n+1:n,i=!o&&["'",'"'].includes(e)?e:"",a=""+this.code.substring(0,t)+i+this.code.substring(s);this.updateCode(a),this.elTextarea.selectionEnd=++this.elTextarea.selectionStart}else{var r=e;switch(e){case"(":r=String.fromCharCode(e.charCodeAt()+1);break;case"<":case"{":case"[":r=String.fromCharCode(e.charCodeAt()+2)}var l=this.code.substring(t,n),c=""+this.code.substring(0,t)+l+r+this.code.substring(n);this.updateCode(c)}this.elTextarea.selectionEnd=t},l.prototype.skipCloseChar=function(e){var t=this.elTextarea.selectionStart,n=this.elTextarea.selectionEnd,o=Math.abs(n-t)>0;return[")","}","]",">"].includes(e)||["'",'"'].includes(e)&&!o},l.prototype.updateCode=function(e){this.code=e,this.elTextarea.value=e,this.elCode.innerHTML=r(e),this.highlight(),this.setLineNumber(),setTimeout(this.runUpdate.bind(this),1)},l.prototype.updateLanguage=function(e){var t=this.opts.language;this.elCode.classList.remove("language-"+t),this.elCode.classList.add("language-"+e),this.opts.language=e,this.highlight()},l.prototype.addLanguage=function(e,t){this.Prism.languages[e]=t},l.prototype.populateDefault=function(){this.updateCode(this.code)},l.prototype.highlight=function(){this.Prism.highlightElement(this.elCode,!1)},l.prototype.highlightLines=function(e){this.elPre.setAttribute("data-line",e),this.highlight()},l.prototype.onUpdate=function(e){if(e&&"[object Function]"!=={}.toString.call(e))throw Error("CodeFlask expects callback of type Function");this.updateCallBack=e},l.prototype.getCode=function(){return this.code},l.prototype.runUpdate=function(){this.updateCallBack&&this.updateCallBack(this.code)},l.prototype.enableReadonlyMode=function(){this.elTextarea.setAttribute("readonly",!0)},l.prototype.disableReadonlyMode=function(){this.elTextarea.removeAttribute("readonly")},l}); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).CodeFlask=t()}(this,(function(){"use strict";var e="#fff";var t,n,o,s='"SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace',i="\n .codeflask {\n position: absolute;\n width: 100%;\n height: 100%;\n overflow: hidden;\n }\n\n .codeflask, .codeflask * {\n box-sizing: border-box;\n }\n\n .codeflask__pre {\n pointer-events: none;\n z-index: 3;\n overflow: hidden;\n }\n\n .codeflask__textarea {\n background: none;\n border: none;\n color: "+(t="caret-color",n="#000",("undefined"!=typeof CSS?CSS.supports(t,n):"undefined"!=typeof document&&(o=(o=t).split("-").filter((function(e){return!!e})).map((function(e){return e[0].toUpperCase()+e.substr(1)})).join(""))[0].toLowerCase()+o.substr(1)in document.body.style)?e:"#ccc")+";\n z-index: 1;\n resize: none;\n font-family: "+s+";\n -webkit-appearance: pre;\n caret-color: #111;\n z-index: 2;\n width: 100%;\n height: 100%;\n }\n\n .codeflask--has-line-numbers .codeflask__textarea {\n width: calc(100% - "+"40px);\n }\n\n .codeflask__code {\n display: block;\n font-family: "+s+";\n overflow: hidden;\n }\n\n .codeflask__flatten {\n padding: 10px;\n font-size: 16px;\n line-height: "+"20px;\n white-space: pre;\n position: absolute;\n top: 0;\n left: 0;\n overflow: auto;\n margin: 0 !important;\n outline: none;\n text-align: left;\n }\n\n .codeflask--has-line-numbers .codeflask__flatten {\n width: calc(100% - "+"40px);\n left: "+"40px;\n }\n\n .codeflask__line-highlight {\n position: absolute;\n top: 10px;\n left: 0;\n width: 100%;\n height: "+"20px;\n background: rgba(0,0,0,0.1);\n z-index: 1;\n }\n\n .codeflask__lines {\n padding: 10px 4px;\n font-size: 12px;\n line-height: "+"20px;\n font-family: 'Cousine', monospace;\n position: absolute;\n left: 0;\n top: 0;\n width: "+"40px;\n min-height: 100%;\n text-align: right;\n color: #999;\n border-right: 1px solid #f8f8f8;\n background-color: #fafafa;\n z-index: 5;\n }\n\n .codeflask__lines__line {\n display: block;\n }\n\n .codeflask.codeflask--has-line-numbers {\n padding-left: "+"40px;\n }\n\n .codeflask.codeflask--has-line-numbers:before {\n content: '';\n position: absolute;\n left: 0;\n top: 0;\n width: "+"40px;\n height: 100%;\n background: #eee;\n z-index: 1;\n }\n";function a(e,t,n){var o=t||"codeflask-style",s=n||document.head;if(!e)return!1;if(document.getElementById(o))return!0;var i=document.createElement("style");return i.innerHTML=e,i.id=o,s.appendChild(i),!0}var r={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function l(e){return String(e).replace(/[&<>"'`=/]/g,(function(e){return r[e]}))}var d={"(":")","[":"]","{":"}","<":">","'":"'",'"':'"'},h=function(e,t,n){if(!e)throw Error("CodeFlask expects 1st parameter to be an Element or a String selector");if(!t)throw Error("CodeFlask expects 2nd parameter to be the Prism peer dependency");if(!n)throw Error("CodeFlask expects 3rd parameter to be an object containing options");if(e.nodeType)this.editorRoot=e;else{var o=document.querySelector(e);o&&(this.editorRoot=o)}this.Prism=t,this.opts=n,this.startEditor()};return h.prototype.startEditor=function(){if(!a(i,null,this.opts.styleParent))throw Error("Failed to inject CodeFlask CSS.");this.createWrapper(),this.createTextarea(),this.createPre(),this.createCode(),this.runOptions(),this.listenTextarea(),this.populateDefault(),this.updateCode(this.code)},h.prototype.createWrapper=function(){this.code=this.editorRoot.innerHTML,this.editorRoot.innerHTML="",this.elWrapper=this.createElement("div",this.editorRoot),this.elWrapper.classList.add("codeflask")},h.prototype.createTextarea=function(){this.elTextarea=this.createElement("textarea",this.elWrapper),this.elTextarea.classList.add("codeflask__textarea","codeflask__flatten")},h.prototype.createPre=function(){this.elPre=this.createElement("pre",this.elWrapper),this.elPre.classList.add("codeflask__pre","codeflask__flatten")},h.prototype.createCode=function(){this.elCode=this.createElement("code",this.elPre),this.elCode.classList.add("codeflask__code","language-"+(this.opts.language||"html"))},h.prototype.createLineNumbers=function(){this.elLineNumbers=this.createElement("div",this.elWrapper),this.elLineNumbers.classList.add("codeflask__lines"),this.setLineNumber()},h.prototype.createElement=function(e,t){var n=document.createElement(e);return t.appendChild(n),n},h.prototype.runOptions=function(){this.opts.rtl=this.opts.rtl||!1,this.opts.tabSize=this.opts.tabSize||2,this.opts.enableAutocorrect=this.opts.enableAutocorrect||!1,this.opts.lineNumbers=this.opts.lineNumbers||!1,this.opts.defaultTheme=!1!==this.opts.defaultTheme,this.opts.areaId=this.opts.areaId||null,this.opts.ariaLabelledby=this.opts.ariaLabelledby||null,this.opts.readonly=this.opts.readonly||null,this.opts.customEventListeners=this.opts.customEventListeners||{},this.opts.selfClosingCharacters=this.opts.selfClosingCharacters||["(","[","{","<","'",'"'],"boolean"!=typeof this.opts.handleTabs&&(this.opts.handleTabs=!0),"boolean"!=typeof this.opts.handleNewLineIndentation&&(this.opts.handleNewLineIndentation=!0),!0===this.opts.rtl&&(this.elTextarea.setAttribute("dir","rtl"),this.elPre.setAttribute("dir","rtl")),!1===this.opts.enableAutocorrect&&(this.elTextarea.setAttribute("spellcheck","false"),this.elTextarea.setAttribute("autocapitalize","off"),this.elTextarea.setAttribute("autocomplete","off"),this.elTextarea.setAttribute("autocorrect","off")),this.opts.lineNumbers&&(this.elWrapper.classList.add("codeflask--has-line-numbers"),this.createLineNumbers()),this.opts.defaultTheme&&a("\n.codeflask {\n background: #fff;\n color: #4f559c;\n}\n\n.codeflask .token.punctuation {\n color: #4a4a4a;\n}\n\n.codeflask .token.keyword {\n color: #8500ff;\n}\n\n.codeflask .token.operator {\n color: #ff5598;\n}\n\n.codeflask .token.string {\n color: #41ad8f;\n}\n\n.codeflask .token.comment {\n color: #9badb7;\n}\n\n.codeflask .token.function {\n color: #8500ff;\n}\n\n.codeflask .token.boolean {\n color: #8500ff;\n}\n\n.codeflask .token.number {\n color: #8500ff;\n}\n\n.codeflask .token.selector {\n color: #8500ff;\n}\n\n.codeflask .token.property {\n color: #8500ff;\n}\n\n.codeflask .token.tag {\n color: #8500ff;\n}\n\n.codeflask .token.attr-value {\n color: #8500ff;\n}\n","theme-default",this.opts.styleParent),this.opts.areaId&&this.elTextarea.setAttribute("id",this.opts.areaId),this.opts.ariaLabelledby&&this.elTextarea.setAttribute("aria-labelledby",this.opts.ariaLabelledby),this.opts.readonly&&this.enableReadonlyMode()},h.prototype.updateLineNumbersCount=function(){for(var e="",t=1;t<=this.lineNumber;t++)e=e+'<span class="codeflask__lines__line">'+t+"</span>";this.elLineNumbers.innerHTML=e},h.prototype.listenTextarea=function(){var e=this,t=this.opts.customEventListeners;for(var n in t)t.hasOwnProperty(n)&&this.elTextarea.addEventListener(n,t[n]);this.elTextarea.addEventListener("input",(function(t){e.code=t.target.value,e.elCode.innerHTML=l(t.target.value),e.highlight(),setTimeout((function(){e.runUpdate(),e.setLineNumber()}),1)})),this.elTextarea.addEventListener("keydown",(function(t){e.opts.readonly||(e.handleTabs(t),e.handleSelfClosingCharacters(t),e.handleNewLineIndentation(t))})),this.elTextarea.addEventListener("scroll",(function(t){e.elPre.style.transform="translate3d(-"+t.target.scrollLeft+"px, -"+t.target.scrollTop+"px, 0)",e.elLineNumbers&&(e.elLineNumbers.style.transform="translate3d(0, -"+t.target.scrollTop+"px, 0)",e.elPre.style.width="calc(100% - 40px + "+t.target.scrollLeft+"px)")}))},h.prototype.handleTabs=function(e){if(this.opts.handleTabs){if(9!==e.keyCode)return;e.preventDefault();var t=this.elTextarea,n=t.selectionDirection,o=t.selectionStart,s=t.selectionEnd,i=t.value,a=i.substr(0,o),r=i.substring(o,s),l=i.substring(s),d=" ".repeat(this.opts.tabSize);if(o!==s&&r.length>=d.length){var h=o-a.split("\n").pop().length,c=d.length,p=d.length;if(e.shiftKey)i.substr(h,d.length)===d?(c=-c,h>o?(r=r.substring(0,h)+r.substring(h+d.length),p=0):h===o?(c=0,p=0,r=r.substring(d.length)):(p=-p,a=a.substring(0,h)+a.substring(h+d.length))):(c=0,p=0),r=r.replace(new RegExp("\n"+d.split("").join("\\"),"g"),"\n");else a=a.substr(0,h)+d+a.substring(h,o),r=r.replace(/\n/g,"\n"+d);t.value=a+r+l,t.selectionStart=o+c,t.selectionEnd=o+r.length+p,t.selectionDirection=n}else t.value=a+d+l,t.selectionStart=o+d.length,t.selectionEnd=o+d.length;var u=t.value;this.updateCode(u),this.elTextarea.selectionEnd=s+this.opts.tabSize}},h.prototype.handleSelfClosingCharacters=function(e){if(this.opts.selfClosingCharacters.length){var t=this.opts.selfClosingCharacters,n=this.opts.selfClosingCharacters.map((function(e){return d[e]}));(t.includes(e.key)||n.includes(e.key))&&this.closeCharacter(e.key)}},h.prototype.setLineNumber=function(){this.lineNumber=this.code.split("\n").length,this.opts.lineNumbers&&this.updateLineNumbersCount()},h.prototype.handleNewLineIndentation=function(e){if(this.opts.handleNewLineIndentation&&13===e.keyCode){e.preventDefault();var t=this.elTextarea,n=t.selectionStart,o=t.selectionEnd,s=t.value,i=s.substr(0,n),a=s.substring(o),r=s.lastIndexOf("\n",n-1),l=r+s.slice(r+1).search(/[^ ]|$/),d=l>r?l-r:0,h=i+"\n"+" ".repeat(d)+a;t.value=h,t.selectionStart=n+d+1,t.selectionEnd=n+d+1,this.updateCode(t.value)}},h.prototype.closeCharacter=function(e){var t=this.elTextarea.selectionStart,n=this.elTextarea.selectionEnd;if(this.skipCloseChar(e)){var o=this.code.substr(n,1)===e,s=o?n+1:n,i=!o&&["'",'"'].includes(e)?e:"",a=""+this.code.substring(0,t)+i+this.code.substring(s);this.updateCode(a),this.elTextarea.selectionEnd=++this.elTextarea.selectionStart}else{var r=e;switch(e){case"(":r=String.fromCharCode(e.charCodeAt()+1);break;case"<":case"{":case"[":r=String.fromCharCode(e.charCodeAt()+2)}var l=this.code.substring(t,n),d=""+this.code.substring(0,t)+l+r+this.code.substring(n);this.updateCode(d)}this.elTextarea.selectionEnd=t},h.prototype.skipCloseChar=function(e){var t=this.elTextarea.selectionStart,n=this.elTextarea.selectionEnd,o=Math.abs(n-t)>0;return[")","}","]",">"].includes(e)||["'",'"'].includes(e)&&!o},h.prototype.updateCode=function(e){this.code=e,this.elTextarea.value=e,this.elCode.innerHTML=l(e),this.highlight(),this.setLineNumber(),setTimeout(this.runUpdate.bind(this),1)},h.prototype.updateLanguage=function(e){var t=this.opts.language;this.elCode.classList.remove("language-"+t),this.elCode.classList.add("language-"+e),this.opts.language=e,this.highlight()},h.prototype.addLanguage=function(e,t){this.Prism.languages[e]=t},h.prototype.populateDefault=function(){this.updateCode(this.code)},h.prototype.highlight=function(){this.Prism.highlightElement(this.elCode,!1)},h.prototype.highlightLines=function(e){this.elPre.setAttribute("data-line",e),this.highlight()},h.prototype.onUpdate=function(e){if(e&&"[object Function]"!=={}.toString.call(e))throw Error("CodeFlask expects callback of type Function");this.updateCallBack=e},h.prototype.getCode=function(){return this.code},h.prototype.runUpdate=function(){this.updateCallBack&&this.updateCallBack(this.code)},h.prototype.enableReadonlyMode=function(){this.elTextarea.setAttribute("readonly",!0)},h.prototype.disableReadonlyMode=function(){this.elTextarea.removeAttribute("readonly")},h})); |
@@ -1,1 +0,1 @@ | ||
var BACKGROUND_COLOR="#fff",LINE_HEIGHT="20px",FONT_SIZE="16px",defaultCssTheme="\n.codeflask {\n background: "+BACKGROUND_COLOR+";\n color: #4f559c;\n}\n\n.codeflask .token.punctuation {\n color: #4a4a4a;\n}\n\n.codeflask .token.keyword {\n color: #8500ff;\n}\n\n.codeflask .token.operator {\n color: #ff5598;\n}\n\n.codeflask .token.string {\n color: #41ad8f;\n}\n\n.codeflask .token.comment {\n color: #9badb7;\n}\n\n.codeflask .token.function {\n color: #8500ff;\n}\n\n.codeflask .token.boolean {\n color: #8500ff;\n}\n\n.codeflask .token.number {\n color: #8500ff;\n}\n\n.codeflask .token.selector {\n color: #8500ff;\n}\n\n.codeflask .token.property {\n color: #8500ff;\n}\n\n.codeflask .token.tag {\n color: #8500ff;\n}\n\n.codeflask .token.attr-value {\n color: #8500ff;\n}\n";function cssSupports(e,t){return"undefined"!=typeof CSS?CSS.supports(e,t):"undefined"!=typeof document&&toCamelCase(e)in document.body.style}function toCamelCase(e){return(e=e.split("-").filter(function(e){return!!e}).map(function(e){return e[0].toUpperCase()+e.substr(1)}).join(""))[0].toLowerCase()+e.substr(1)}var FONT_FAMILY='"SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace',COLOR=cssSupports("caret-color","#000")?BACKGROUND_COLOR:"#ccc",LINE_NUMBER_WIDTH="40px",editorCss="\n .codeflask {\n position: absolute;\n width: 100%;\n height: 100%;\n overflow: hidden;\n }\n\n .codeflask, .codeflask * {\n box-sizing: border-box;\n }\n\n .codeflask__pre {\n pointer-events: none;\n z-index: 3;\n overflow: hidden;\n }\n\n .codeflask__textarea {\n background: none;\n border: none;\n color: "+COLOR+";\n z-index: 1;\n resize: none;\n font-family: "+FONT_FAMILY+";\n -webkit-appearance: pre;\n caret-color: #111;\n z-index: 2;\n width: 100%;\n height: 100%;\n }\n\n .codeflask--has-line-numbers .codeflask__textarea {\n width: calc(100% - "+LINE_NUMBER_WIDTH+");\n }\n\n .codeflask__code {\n display: block;\n font-family: "+FONT_FAMILY+";\n overflow: hidden;\n }\n\n .codeflask__flatten {\n padding: 10px;\n font-size: "+FONT_SIZE+";\n line-height: "+LINE_HEIGHT+";\n white-space: pre;\n position: absolute;\n top: 0;\n left: 0;\n overflow: auto;\n margin: 0 !important;\n outline: none;\n text-align: left;\n }\n\n .codeflask--has-line-numbers .codeflask__flatten {\n width: calc(100% - "+LINE_NUMBER_WIDTH+");\n left: "+LINE_NUMBER_WIDTH+";\n }\n\n .codeflask__line-highlight {\n position: absolute;\n top: 10px;\n left: 0;\n width: 100%;\n height: "+LINE_HEIGHT+";\n background: rgba(0,0,0,0.1);\n z-index: 1;\n }\n\n .codeflask__lines {\n padding: 10px 4px;\n font-size: 12px;\n line-height: "+LINE_HEIGHT+";\n font-family: 'Cousine', monospace;\n position: absolute;\n left: 0;\n top: 0;\n width: "+LINE_NUMBER_WIDTH+";\n min-height: 100%;\n text-align: right;\n color: #999;\n border-right: 1px solid #f8f8f8;\n background-color: #fafafa;\n z-index: 5;\n }\n\n .codeflask__lines__line {\n display: block;\n }\n\n .codeflask.codeflask--has-line-numbers {\n padding-left: "+LINE_NUMBER_WIDTH+";\n }\n\n .codeflask.codeflask--has-line-numbers:before {\n content: '';\n position: absolute;\n left: 0;\n top: 0;\n width: "+LINE_NUMBER_WIDTH+";\n height: 100%;\n background: #eee;\n z-index: 1;\n }\n";function injectCss(e,t,n){var s=t||"codeflask-style",o=n||document.head;if(!e)return!1;if(document.getElementById(s))return!0;var a=document.createElement("style");return a.innerHTML=e,a.id=s,o.appendChild(a),!0}var entityMap={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function escapeHtml(e){return String(e).replace(/[&<>"'`=/]/g,function(e){return entityMap[e]})}var CodeFlask=function(e,t,n){if(!e)throw Error("CodeFlask expects 1st parameter to be an Element or a String selector");if(!t)throw Error("CodeFlask expects 2nd parameter to be the Prism peer dependency");if(!n)throw Error("CodeFlask expects 3rd parameter to be an object containing options");if(e.nodeType)this.editorRoot=e;else{var s=document.querySelector(e);s&&(this.editorRoot=s)}this.Prism=t,this.opts=n,this.startEditor()};CodeFlask.prototype.startEditor=function(){if(!injectCss(editorCss,null,this.opts.styleParent))throw Error("Failed to inject CodeFlask CSS.");this.createWrapper(),this.createTextarea(),this.createPre(),this.createCode(),this.runOptions(),this.listenTextarea(),this.populateDefault(),this.updateCode(this.code)},CodeFlask.prototype.createWrapper=function(){this.code=this.editorRoot.innerHTML,this.editorRoot.innerHTML="",this.elWrapper=this.createElement("div",this.editorRoot),this.elWrapper.classList.add("codeflask")},CodeFlask.prototype.createTextarea=function(){this.elTextarea=this.createElement("textarea",this.elWrapper),this.elTextarea.classList.add("codeflask__textarea","codeflask__flatten")},CodeFlask.prototype.createPre=function(){this.elPre=this.createElement("pre",this.elWrapper),this.elPre.classList.add("codeflask__pre","codeflask__flatten")},CodeFlask.prototype.createCode=function(){this.elCode=this.createElement("code",this.elPre),this.elCode.classList.add("codeflask__code","language-"+(this.opts.language||"html"))},CodeFlask.prototype.createLineNumbers=function(){this.elLineNumbers=this.createElement("div",this.elWrapper),this.elLineNumbers.classList.add("codeflask__lines"),this.setLineNumber()},CodeFlask.prototype.createElement=function(e,t){var n=document.createElement(e);return t.appendChild(n),n},CodeFlask.prototype.runOptions=function(){this.opts.rtl=this.opts.rtl||!1,this.opts.tabSize=this.opts.tabSize||2,this.opts.enableAutocorrect=this.opts.enableAutocorrect||!1,this.opts.lineNumbers=this.opts.lineNumbers||!1,this.opts.defaultTheme=!1!==this.opts.defaultTheme,this.opts.areaId=this.opts.areaId||null,this.opts.ariaLabelledby=this.opts.ariaLabelledby||null,this.opts.readonly=this.opts.readonly||null,this.opts.customEventListeners=this.opts.customEventListeners||{},"boolean"!=typeof this.opts.handleTabs&&(this.opts.handleTabs=!0),"boolean"!=typeof this.opts.handleSelfClosingCharacters&&(this.opts.handleSelfClosingCharacters=!0),"boolean"!=typeof this.opts.handleNewLineIndentation&&(this.opts.handleNewLineIndentation=!0),!0===this.opts.rtl&&(this.elTextarea.setAttribute("dir","rtl"),this.elPre.setAttribute("dir","rtl")),!1===this.opts.enableAutocorrect&&(this.elTextarea.setAttribute("spellcheck","false"),this.elTextarea.setAttribute("autocapitalize","off"),this.elTextarea.setAttribute("autocomplete","off"),this.elTextarea.setAttribute("autocorrect","off")),this.opts.lineNumbers&&(this.elWrapper.classList.add("codeflask--has-line-numbers"),this.createLineNumbers()),this.opts.defaultTheme&&injectCss(defaultCssTheme,"theme-default",this.opts.styleParent),this.opts.areaId&&this.elTextarea.setAttribute("id",this.opts.areaId),this.opts.ariaLabelledby&&this.elTextarea.setAttribute("aria-labelledby",this.opts.ariaLabelledby),this.opts.readonly&&this.enableReadonlyMode()},CodeFlask.prototype.updateLineNumbersCount=function(){for(var e="",t=1;t<=this.lineNumber;t++)e=e+'<span class="codeflask__lines__line">'+t+"</span>";this.elLineNumbers.innerHTML=e},CodeFlask.prototype.listenTextarea=function(){var e=this,t=this.opts.customEventListeners;for(var n in t)t.hasOwnProperty(n)&&this.elTextarea.addEventListener(n,t[n]);this.elTextarea.addEventListener("input",function(t){e.code=t.target.value,e.elCode.innerHTML=escapeHtml(t.target.value),e.highlight(),setTimeout(function(){e.runUpdate(),e.setLineNumber()},1)}),this.elTextarea.addEventListener("keydown",function(t){e.opts.readonly||(e.handleTabs(t),e.handleSelfClosingCharacters(t),e.handleNewLineIndentation(t))}),this.elTextarea.addEventListener("scroll",function(t){e.elPre.style.transform="translate3d(-"+t.target.scrollLeft+"px, -"+t.target.scrollTop+"px, 0)",e.elLineNumbers&&(e.elLineNumbers.style.transform="translate3d(0, -"+t.target.scrollTop+"px, 0)",e.elPre.style.width="calc(100% - 40px + "+t.target.scrollLeft+"px)")})},CodeFlask.prototype.handleTabs=function(e){if(this.opts.handleTabs){if(9!==e.keyCode)return;e.preventDefault();var t=this.elTextarea,n=t.selectionDirection,s=t.selectionStart,o=t.selectionEnd,a=t.value,i=a.substr(0,s),r=a.substring(s,o),l=a.substring(o),d=" ".repeat(this.opts.tabSize);if(s!==o&&r.length>=d.length){var c=s-i.split("\n").pop().length,h=d.length,p=d.length;if(e.shiftKey)a.substr(c,d.length)===d?(h=-h,c>s?(r=r.substring(0,c)+r.substring(c+d.length),p=0):c===s?(h=0,p=0,r=r.substring(d.length)):(p=-p,i=i.substring(0,c)+i.substring(c+d.length))):(h=0,p=0),r=r.replace(new RegExp("\n"+d.split("").join("\\"),"g"),"\n");else i=i.substr(0,c)+d+i.substring(c,s),r=r.replace(/\n/g,"\n"+d);t.value=i+r+l,t.selectionStart=s+h,t.selectionEnd=s+r.length+p,t.selectionDirection=n}else t.value=i+d+l,t.selectionStart=s+d.length,t.selectionEnd=s+d.length;var u=t.value;this.updateCode(u),this.elTextarea.selectionEnd=o+this.opts.tabSize}},CodeFlask.prototype.handleSelfClosingCharacters=function(e){if(this.opts.handleSelfClosingCharacters){var t=e.key;if(["(","[","{","<","'",'"'].includes(t)||[")","]","}",">","'",'"'].includes(t))switch(t){case"(":case")":this.closeCharacter(t);break;case"[":case"]":this.closeCharacter(t);break;case"{":case"}":this.closeCharacter(t);break;case"<":case">":case"'":case'"':this.closeCharacter(t)}}},CodeFlask.prototype.setLineNumber=function(){this.lineNumber=this.code.split("\n").length,this.opts.lineNumbers&&this.updateLineNumbersCount()},CodeFlask.prototype.handleNewLineIndentation=function(e){if(this.opts.handleNewLineIndentation&&13===e.keyCode){e.preventDefault();var t=this.elTextarea,n=t.selectionStart,s=t.selectionEnd,o=t.value,a=o.substr(0,n),i=o.substring(s),r=o.lastIndexOf("\n",n-1),l=r+o.slice(r+1).search(/[^ ]|$/),d=l>r?l-r:0,c=a+"\n"+" ".repeat(d)+i;t.value=c,t.selectionStart=n+d+1,t.selectionEnd=n+d+1,this.updateCode(t.value)}},CodeFlask.prototype.closeCharacter=function(e){var t=this.elTextarea.selectionStart,n=this.elTextarea.selectionEnd;if(this.skipCloseChar(e)){var s=this.code.substr(n,1)===e,o=s?n+1:n,a=!s&&["'",'"'].includes(e)?e:"",i=""+this.code.substring(0,t)+a+this.code.substring(o);this.updateCode(i),this.elTextarea.selectionEnd=++this.elTextarea.selectionStart}else{var r=e;switch(e){case"(":r=String.fromCharCode(e.charCodeAt()+1);break;case"<":case"{":case"[":r=String.fromCharCode(e.charCodeAt()+2)}var l=this.code.substring(t,n),d=""+this.code.substring(0,t)+l+r+this.code.substring(n);this.updateCode(d)}this.elTextarea.selectionEnd=t},CodeFlask.prototype.skipCloseChar=function(e){var t=this.elTextarea.selectionStart,n=this.elTextarea.selectionEnd,s=Math.abs(n-t)>0;return[")","}","]",">"].includes(e)||["'",'"'].includes(e)&&!s},CodeFlask.prototype.updateCode=function(e){this.code=e,this.elTextarea.value=e,this.elCode.innerHTML=escapeHtml(e),this.highlight(),this.setLineNumber(),setTimeout(this.runUpdate.bind(this),1)},CodeFlask.prototype.updateLanguage=function(e){var t=this.opts.language;this.elCode.classList.remove("language-"+t),this.elCode.classList.add("language-"+e),this.opts.language=e,this.highlight()},CodeFlask.prototype.addLanguage=function(e,t){this.Prism.languages[e]=t},CodeFlask.prototype.populateDefault=function(){this.updateCode(this.code)},CodeFlask.prototype.highlight=function(){this.Prism.highlightElement(this.elCode,!1)},CodeFlask.prototype.highlightLines=function(e){this.elPre.setAttribute("data-line",e),this.highlight()},CodeFlask.prototype.onUpdate=function(e){if(e&&"[object Function]"!=={}.toString.call(e))throw Error("CodeFlask expects callback of type Function");this.updateCallBack=e},CodeFlask.prototype.getCode=function(){return this.code},CodeFlask.prototype.runUpdate=function(){this.updateCallBack&&this.updateCallBack(this.code)},CodeFlask.prototype.enableReadonlyMode=function(){this.elTextarea.setAttribute("readonly",!0)},CodeFlask.prototype.disableReadonlyMode=function(){this.elTextarea.removeAttribute("readonly")};export default CodeFlask; | ||
var e,t,n,o='"SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace',s="\n .codeflask {\n position: absolute;\n width: 100%;\n height: 100%;\n overflow: hidden;\n }\n\n .codeflask, .codeflask * {\n box-sizing: border-box;\n }\n\n .codeflask__pre {\n pointer-events: none;\n z-index: 3;\n overflow: hidden;\n }\n\n .codeflask__textarea {\n background: none;\n border: none;\n color: "+(e="caret-color",t="#000",("undefined"!=typeof CSS?CSS.supports(e,t):"undefined"!=typeof document&&(n=(n=e).split("-").filter((function(e){return!!e})).map((function(e){return e[0].toUpperCase()+e.substr(1)})).join(""))[0].toLowerCase()+n.substr(1)in document.body.style)?"#fff":"#ccc")+";\n z-index: 1;\n resize: none;\n font-family: "+o+";\n -webkit-appearance: pre;\n caret-color: #111;\n z-index: 2;\n width: 100%;\n height: 100%;\n }\n\n .codeflask--has-line-numbers .codeflask__textarea {\n width: calc(100% - 40px);\n }\n\n .codeflask__code {\n display: block;\n font-family: "+o+";\n overflow: hidden;\n }\n\n .codeflask__flatten {\n padding: 10px;\n font-size: 16px;\n line-height: 20px;\n white-space: pre;\n position: absolute;\n top: 0;\n left: 0;\n overflow: auto;\n margin: 0 !important;\n outline: none;\n text-align: left;\n }\n\n .codeflask--has-line-numbers .codeflask__flatten {\n width: calc(100% - 40px);\n left: 40px;\n }\n\n .codeflask__line-highlight {\n position: absolute;\n top: 10px;\n left: 0;\n width: 100%;\n height: 20px;\n background: rgba(0,0,0,0.1);\n z-index: 1;\n }\n\n .codeflask__lines {\n padding: 10px 4px;\n font-size: 12px;\n line-height: 20px;\n font-family: 'Cousine', monospace;\n position: absolute;\n left: 0;\n top: 0;\n width: 40px;\n min-height: 100%;\n text-align: right;\n color: #999;\n border-right: 1px solid #f8f8f8;\n background-color: #fafafa;\n z-index: 5;\n }\n\n .codeflask__lines__line {\n display: block;\n }\n\n .codeflask.codeflask--has-line-numbers {\n padding-left: 40px;\n }\n\n .codeflask.codeflask--has-line-numbers:before {\n content: '';\n position: absolute;\n left: 0;\n top: 0;\n width: 40px;\n height: 100%;\n background: #eee;\n z-index: 1;\n }\n";function i(e,t,n){var o=t||"codeflask-style",s=n||document.head;if(!e)return!1;if(document.getElementById(o))return!0;var i=document.createElement("style");return i.innerHTML=e,i.id=o,s.appendChild(i),!0}var a={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/","`":"`","=":"="};function r(e){return String(e).replace(/[&<>"'`=/]/g,(function(e){return a[e]}))}var l={"(":")","[":"]","{":"}","<":">","'":"'",'"':'"'},h=function(e,t,n){if(!e)throw Error("CodeFlask expects 1st parameter to be an Element or a String selector");if(!t)throw Error("CodeFlask expects 2nd parameter to be the Prism peer dependency");if(!n)throw Error("CodeFlask expects 3rd parameter to be an object containing options");if(e.nodeType)this.editorRoot=e;else{var o=document.querySelector(e);o&&(this.editorRoot=o)}this.Prism=t,this.opts=n,this.startEditor()};h.prototype.startEditor=function(){if(!i(s,null,this.opts.styleParent))throw Error("Failed to inject CodeFlask CSS.");this.createWrapper(),this.createTextarea(),this.createPre(),this.createCode(),this.runOptions(),this.listenTextarea(),this.populateDefault(),this.updateCode(this.code)},h.prototype.createWrapper=function(){this.code=this.editorRoot.innerHTML,this.editorRoot.innerHTML="",this.elWrapper=this.createElement("div",this.editorRoot),this.elWrapper.classList.add("codeflask")},h.prototype.createTextarea=function(){this.elTextarea=this.createElement("textarea",this.elWrapper),this.elTextarea.classList.add("codeflask__textarea","codeflask__flatten")},h.prototype.createPre=function(){this.elPre=this.createElement("pre",this.elWrapper),this.elPre.classList.add("codeflask__pre","codeflask__flatten")},h.prototype.createCode=function(){this.elCode=this.createElement("code",this.elPre),this.elCode.classList.add("codeflask__code","language-"+(this.opts.language||"html"))},h.prototype.createLineNumbers=function(){this.elLineNumbers=this.createElement("div",this.elWrapper),this.elLineNumbers.classList.add("codeflask__lines"),this.setLineNumber()},h.prototype.createElement=function(e,t){var n=document.createElement(e);return t.appendChild(n),n},h.prototype.runOptions=function(){this.opts.rtl=this.opts.rtl||!1,this.opts.tabSize=this.opts.tabSize||2,this.opts.enableAutocorrect=this.opts.enableAutocorrect||!1,this.opts.lineNumbers=this.opts.lineNumbers||!1,this.opts.defaultTheme=!1!==this.opts.defaultTheme,this.opts.areaId=this.opts.areaId||null,this.opts.ariaLabelledby=this.opts.ariaLabelledby||null,this.opts.readonly=this.opts.readonly||null,this.opts.customEventListeners=this.opts.customEventListeners||{},this.opts.selfClosingCharacters=this.opts.selfClosingCharacters||["(","[","{","<","'",'"'],"boolean"!=typeof this.opts.handleTabs&&(this.opts.handleTabs=!0),"boolean"!=typeof this.opts.handleNewLineIndentation&&(this.opts.handleNewLineIndentation=!0),!0===this.opts.rtl&&(this.elTextarea.setAttribute("dir","rtl"),this.elPre.setAttribute("dir","rtl")),!1===this.opts.enableAutocorrect&&(this.elTextarea.setAttribute("spellcheck","false"),this.elTextarea.setAttribute("autocapitalize","off"),this.elTextarea.setAttribute("autocomplete","off"),this.elTextarea.setAttribute("autocorrect","off")),this.opts.lineNumbers&&(this.elWrapper.classList.add("codeflask--has-line-numbers"),this.createLineNumbers()),this.opts.defaultTheme&&i("\n.codeflask {\n background: #fff;\n color: #4f559c;\n}\n\n.codeflask .token.punctuation {\n color: #4a4a4a;\n}\n\n.codeflask .token.keyword {\n color: #8500ff;\n}\n\n.codeflask .token.operator {\n color: #ff5598;\n}\n\n.codeflask .token.string {\n color: #41ad8f;\n}\n\n.codeflask .token.comment {\n color: #9badb7;\n}\n\n.codeflask .token.function {\n color: #8500ff;\n}\n\n.codeflask .token.boolean {\n color: #8500ff;\n}\n\n.codeflask .token.number {\n color: #8500ff;\n}\n\n.codeflask .token.selector {\n color: #8500ff;\n}\n\n.codeflask .token.property {\n color: #8500ff;\n}\n\n.codeflask .token.tag {\n color: #8500ff;\n}\n\n.codeflask .token.attr-value {\n color: #8500ff;\n}\n","theme-default",this.opts.styleParent),this.opts.areaId&&this.elTextarea.setAttribute("id",this.opts.areaId),this.opts.ariaLabelledby&&this.elTextarea.setAttribute("aria-labelledby",this.opts.ariaLabelledby),this.opts.readonly&&this.enableReadonlyMode()},h.prototype.updateLineNumbersCount=function(){for(var e="",t=1;t<=this.lineNumber;t++)e=e+'<span class="codeflask__lines__line">'+t+"</span>";this.elLineNumbers.innerHTML=e},h.prototype.listenTextarea=function(){var e=this,t=this.opts.customEventListeners;for(var n in t)t.hasOwnProperty(n)&&this.elTextarea.addEventListener(n,t[n]);this.elTextarea.addEventListener("input",(function(t){e.code=t.target.value,e.elCode.innerHTML=r(t.target.value),e.highlight(),setTimeout((function(){e.runUpdate(),e.setLineNumber()}),1)})),this.elTextarea.addEventListener("keydown",(function(t){e.opts.readonly||(e.handleTabs(t),e.handleSelfClosingCharacters(t),e.handleNewLineIndentation(t))})),this.elTextarea.addEventListener("scroll",(function(t){e.elPre.style.transform="translate3d(-"+t.target.scrollLeft+"px, -"+t.target.scrollTop+"px, 0)",e.elLineNumbers&&(e.elLineNumbers.style.transform="translate3d(0, -"+t.target.scrollTop+"px, 0)",e.elPre.style.width="calc(100% - 40px + "+t.target.scrollLeft+"px)")}))},h.prototype.handleTabs=function(e){if(this.opts.handleTabs){if(9!==e.keyCode)return;e.preventDefault();var t=this.elTextarea,n=t.selectionDirection,o=t.selectionStart,s=t.selectionEnd,i=t.value,a=i.substr(0,o),r=i.substring(o,s),l=i.substring(s),h=" ".repeat(this.opts.tabSize);if(o!==s&&r.length>=h.length){var d=o-a.split("\n").pop().length,c=h.length,p=h.length;if(e.shiftKey)i.substr(d,h.length)===h?(c=-c,d>o?(r=r.substring(0,d)+r.substring(d+h.length),p=0):d===o?(c=0,p=0,r=r.substring(h.length)):(p=-p,a=a.substring(0,d)+a.substring(d+h.length))):(c=0,p=0),r=r.replace(new RegExp("\n"+h.split("").join("\\"),"g"),"\n");else a=a.substr(0,d)+h+a.substring(d,o),r=r.replace(/\n/g,"\n"+h);t.value=a+r+l,t.selectionStart=o+c,t.selectionEnd=o+r.length+p,t.selectionDirection=n}else t.value=a+h+l,t.selectionStart=o+h.length,t.selectionEnd=o+h.length;var u=t.value;this.updateCode(u),this.elTextarea.selectionEnd=s+this.opts.tabSize}},h.prototype.handleSelfClosingCharacters=function(e){if(this.opts.selfClosingCharacters.length){var t=this.opts.selfClosingCharacters,n=this.opts.selfClosingCharacters.map((function(e){return l[e]}));(t.includes(e.key)||n.includes(e.key))&&this.closeCharacter(e.key)}},h.prototype.setLineNumber=function(){this.lineNumber=this.code.split("\n").length,this.opts.lineNumbers&&this.updateLineNumbersCount()},h.prototype.handleNewLineIndentation=function(e){if(this.opts.handleNewLineIndentation&&13===e.keyCode){e.preventDefault();var t=this.elTextarea,n=t.selectionStart,o=t.selectionEnd,s=t.value,i=s.substr(0,n),a=s.substring(o),r=s.lastIndexOf("\n",n-1),l=r+s.slice(r+1).search(/[^ ]|$/),h=l>r?l-r:0,d=i+"\n"+" ".repeat(h)+a;t.value=d,t.selectionStart=n+h+1,t.selectionEnd=n+h+1,this.updateCode(t.value)}},h.prototype.closeCharacter=function(e){var t=this.elTextarea.selectionStart,n=this.elTextarea.selectionEnd;if(this.skipCloseChar(e)){var o=this.code.substr(n,1)===e,s=o?n+1:n,i=!o&&["'",'"'].includes(e)?e:"",a=""+this.code.substring(0,t)+i+this.code.substring(s);this.updateCode(a),this.elTextarea.selectionEnd=++this.elTextarea.selectionStart}else{var r=e;switch(e){case"(":r=String.fromCharCode(e.charCodeAt()+1);break;case"<":case"{":case"[":r=String.fromCharCode(e.charCodeAt()+2)}var l=this.code.substring(t,n),h=""+this.code.substring(0,t)+l+r+this.code.substring(n);this.updateCode(h)}this.elTextarea.selectionEnd=t},h.prototype.skipCloseChar=function(e){var t=this.elTextarea.selectionStart,n=this.elTextarea.selectionEnd,o=Math.abs(n-t)>0;return[")","}","]",">"].includes(e)||["'",'"'].includes(e)&&!o},h.prototype.updateCode=function(e){this.code=e,this.elTextarea.value=e,this.elCode.innerHTML=r(e),this.highlight(),this.setLineNumber(),setTimeout(this.runUpdate.bind(this),1)},h.prototype.updateLanguage=function(e){var t=this.opts.language;this.elCode.classList.remove("language-"+t),this.elCode.classList.add("language-"+e),this.opts.language=e,this.highlight()},h.prototype.addLanguage=function(e,t){this.Prism.languages[e]=t},h.prototype.populateDefault=function(){this.updateCode(this.code)},h.prototype.highlight=function(){this.Prism.highlightElement(this.elCode,!1)},h.prototype.highlightLines=function(e){this.elPre.setAttribute("data-line",e),this.highlight()},h.prototype.onUpdate=function(e){if(e&&"[object Function]"!=={}.toString.call(e))throw Error("CodeFlask expects callback of type Function");this.updateCallBack=e},h.prototype.getCode=function(){return this.code},h.prototype.runUpdate=function(){this.updateCallBack&&this.updateCallBack(this.code)},h.prototype.enableReadonlyMode=function(){this.elTextarea.setAttribute("readonly",!0)},h.prototype.disableReadonlyMode=function(){this.elTextarea.removeAttribute("readonly")};export{h as default}; |
@@ -23,2 +23,3 @@ import * as prism from 'prismjs'; | ||
readonly?: boolean | ||
selfClosingCharacters: string[] | ||
customEventListeners?: EventListeners | ||
@@ -30,3 +31,3 @@ } | ||
updateCode(newCode: string): void | ||
updateCode(newCode: string): void | ||
updateLanguage(newLanguage: string): void | ||
@@ -33,0 +34,0 @@ addLanguage(name: string, options: LanguageDefinition): void |
{ | ||
"name": "@acarl005/codeflask", | ||
"version": "2.0.8", | ||
"version": "2.0.9", | ||
"description": "A micro code-editor for awesome web pages", | ||
@@ -30,2 +30,5 @@ "main": "build/codeflask.min.js", | ||
"devDependencies": { | ||
"@rollup/plugin-buble": "^0.21.3", | ||
"@rollup/plugin-commonjs": "^22.0.0", | ||
"@rollup/plugin-node-resolve": "^13.3.0", | ||
"@wdio/cli": "^6.1.15", | ||
@@ -40,7 +43,4 @@ "@wdio/local-runner": "^6.1.14", | ||
"mocha": "^5.1.1", | ||
"rollup": "^0.58.1", | ||
"rollup-plugin-buble": "^0.19.2", | ||
"rollup-plugin-commonjs": "^9.1.0", | ||
"rollup-plugin-node-resolve": "^3.0.3", | ||
"rollup-plugin-uglify": "^3.0.0", | ||
"rollup": "^2.72.1", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"serve": "^7.0.0", | ||
@@ -47,0 +47,0 @@ "wdio-chromedriver-service": "^6.0.3" |
@@ -13,2 +13,3 @@ # CodeFlask Mod | ||
1. Fix this issue: kazzkiq/CodeFlask#69 | ||
1. Make the self-closing characters configurable | ||
@@ -20,3 +21,4 @@ ```javascript | ||
const flask = new CodeFlask(editor, Prism, { | ||
language: "html", | ||
language: "js", | ||
selfClosingCharacters: ['(', '[', '{', "'", '"'], | ||
customEventListeners: { | ||
@@ -23,0 +25,0 @@ "keydown": e => { |
40
27745
118