forked from chrisdone-archive/jquery-console
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjs-console.min.js
2 lines (2 loc) · 8.31 KB
/
js-console.min.js
1
2
/* Copyright 2019, more info: https://pat.sh/projects/js-console/ */
!function(){var e=!!~navigator.userAgent.indexOf(" AppleWebKit/"),n="js-console-",t=n+"cursor",o=n+"prompt",r=n+"prompt-box",s=n+"prompt-label",c=n+"message",a=c+"-value",i=c+"-success",l=c+"-error";window.makeConsole=function(n,u){var f={37:$,39:U,38:z,40:F,8:function(){Q(-1)&&(R(),re())},46:B,35:ee,36:_,13:function(){var e=E;if("function"==typeof u.commandValidate){var n=u.commandValidate(e);1==n||0==n?n&&X():q(n,l)}else X()},18:function(){},9:function(){"function"==typeof u.completeHandle?function(){if("function"==typeof u.completeHandle){var e=u.completeHandle(E),n=e.length;if(1===n)K.promptText(te(E,e[0]));else if(n>1&&u.cols){for(var t=E,o=0,r=0;r<n;r++)o=Math.max(o,e[r].length);o+=2;var s=Math.floor(u.cols/o),c="",i=0;for(r=0;r<n;r++){var l=e[r];c+=e[r];for(var f=l.length;f<o;f++)c+=" ";++i>=s&&(c+="\n",i=0)}q(c,a),K.promptText(t)}}}():"function"==typeof u.completeIssuer&&u.completeIssuer(E)}},d={65:_,69:ee,68:B,78:F,80:z,66:$,70:U,75:function(){for(;R();)re()},76:D,85:function(){K.promptText("")}};u.ctrlCodes&&Object.assign(d,u.ctrlCodes);var p={70:function(){for(;T<E.length&&!ne(E[T])&&U(););for(;T<E.length&&ne(E[T])&&U(););},66:function(){for(;T-1>=0&&!ne(E[T-1])&&$(););for(;T-1>=0&&ne(E[T-1])&&$(););},68:function(){for(;T<E.length&&!ne(E[T]);)R(),re();for(;T<E.length&&ne(E[T]);)R(),re()}},m={13:function(){var e="\n"+E.split("\n").slice(-1)[0].match(/^(\s*)/g)[0];E+=e,Q(e.length),re()}},v='<span class="'+t+'"> </span>',g=n,h=document.createElement("div");h.className="js-console-inner";var L,b,y=document.createElement("textarea");y.className="js-console-typer",["autocomplete","autocorrect","autocapitalize","spellcheck"].forEach(function(e){y.setAttribute(e,"off")}),y.className="js-console-typer";var C=u&&u.continuedPromptLabel?u.continuedPromptLabel:"> ",T=0,E="",w="",x="",j=void 0===u.fadeOnReset||u.fadeOnReset,A=u&&u.scrollPage?"pagescroll":"divscroll",H=[],k=0,M=0,I=!0,K={};function P(e,n,t,o){var r=function(){o||e.removeEventListener("animationend",r),t()};e.addEventListener("animationend",r),e.classList.add(n)}K.promptLabel=u&&u.promptLabel?u.promptLabel:"> ",g.append(h),h.append(y),y.classList.add("js-console-typer"),u.welcomeMessage&&J(u.welcomeMessage,"js-console-welcome"),O(),u.autofocus&&(h.classList.add("js-console-focus"),y.focus(),setTimeout(function(){h.classList.add("js-console-focus"),y.focus()},100)),K.inner=h,K.typer=y,K.scrollToBottom=W,K.report=G,K.showCompletion=oe,K.clearScreen=D,K.disableInput=Y,K.enableInput=Z,K.reset=function(){var e=void 0!==u.welcomeMessage,n=function(){for(var n=Array.from(h.children),t=0;t<n.length;++t){var o=n[t];"DIV"==o.tagName&&(e?e=!1:o.parentElement.removeChild(o))}};j?P(h.parentElement,"fadeout",function(){n(),O(),h.parentElement.classList.remove("fadeout"),h.parentElement.classList.add("fadein")}):(n(),O(),N())};var S,N=function(){h.classList.add("js-console-focus"),y.focus()};function O(){T=0,E="",k=0,Z(),(L=document.createElement("div")).classList.add(r);var e=document.createElement("span");e.classList.add(s);var n=K.continuedPrompt?C:K.promptLabel;e.innerText=n,L.append(e),e.innerHTML=e.innerHTML.replace(" "," "),(b=document.createElement("span")).classList.add(o),L.append(b),h.append(L),re()}function V(e){if(0!=H.length){(k+=e)<0?k=H.length:k>H.length&&(k=0);E=0==k?w:H[k-1],u.historyPreserveColumn?E.length<T+1?T=E.length:0==T&&(T=E.length):T=E.length,re()}}function z(){V(-1)}function F(){V(1)}function R(){return T<E.length&&(E=E.substring(0,T)+E.substring(T+1),w=E,!0)}function B(){R()&&re()}function D(){for(var e=[],n=0;n<h.children.length;++n)(h.children[n].classList.contains(r)||h.children[n].classList.contains(c))&&e.push(h.children[n]);for(n=0;n<e.length-1;++n)h.removeChild(e[n]);K.report(" "),K.focus()}function W(){"pagescroll"===A?window.scrollTo(0,document.body.scrollHeight):"divscroll"===A&&h.setAttribute("scrollTop",h.getAttribute("scrollHeight"))}function X(){if("function"==typeof u.commandHandle){Y(),t=E,H.push(t),w="";var e=E;K.continuedPrompt?x?x+="\n"+E:x=E:x=void 0,x&&(e=x);var n=u.commandHandle(e,function(e){q(e)});K.continuedPrompt&&!x&&(x=E),"boolean"==typeof n?n?q():q("Command failed.",l):"string"==typeof n?q(n,i):"object"==typeof n&&n.length?q(n):K.continuedPrompt&&q()}var t}function Y(){I=!1}function Z(){I=!0}function q(e,n){if(T=-1,re(),"string"==typeof e)J(e,n);else if(Array.isArray(e))for(var t in e){var o=e[t];J(o.msg,o.className)}else void 0!==e&&h.append(e);O()}function G(e,n){var t=E;L.remove(),q(e,n),K.promptText(t)}function J(e,n){var t,o,r=document.createElement("div");r.classList.add(c),n&&r.classList.add(n),o=e,(t=r).innerText=o,t.innerHTML=t.innerHTML.replace(/\t/g," ").replace(/\n/g,"<br/>"),r.classList.add("hide"),h.append(r),r.classList.remove("hide")}function Q(e){return T+e>=0&&T+e<=E.length&&(T+=e,!0)}function U(){return!!Q(1)&&(re(),!0)}function $(){return!!Q(-1)&&(re(),!0)}function _(){Q(-T)&&re()}function ee(){Q(E.length-T)&&re()}function ne(e){if("string"==typeof e){var n=e.charCodeAt();return n>="A".charCodeAt()&&n<="Z".charCodeAt()||n>="a".charCodeAt()&&n<="z".charCodeAt()||n>="0".charCodeAt()&&n<="9".charCodeAt()}return!1}function te(e,n){if(u.autocompleteSmart){if(u.autocompleteSmart){var t=e.split(" ");""==t[t.length-1]?K.promptText(e+n):K.promptText(t.slice(0,t.length-1).join(" ")+" "+n)}}else K.promptText(e+n)}function oe(e,n){var t=n.length;if(1===t)K.promptText(te(e,n[0]));else if(t>1&&u.cols){for(var o=e,r=0,s=0;s<t;s++)r=Math.max(r,n[s].length);r+=2;var c=Math.floor(u.cols/r),i="",l=0;for(s=0;s<t;s++){var f=n[s];i+=n[s];for(var d=f.length;d<r;d++)i+=" ";++l>=c&&(i+="\n",l=0)}q(i,a),K.promptText(o)}}function re(){var e=E,n="";if(T>0&&""==e)n=v;else if(T==E.length)n=se(e)+v;else{var o=e.substring(0,T),r=e.substring(T,T+1);r&&(r='<span class="'+t+'">'+se(r)+"</span>");var s=e.substring(T+1);n=se(o)+r+se(s)}b.innerHTML=n,W()}function se(e){return e.replace(/&/g,"&").replace(/</g,"<").replace(/</g,"<").replace(/ /g," ").replace(/\n/g,"<br />")}return K.focus=function(){N()},K.notice=function(e,n){var t=document.createElement("div");t.classList.add("notice");var o=document.createElement("div");o.innerText=e,t.appendChild(o),t.classList.add("invisible"),g.append(t);var r=!0;if("fadeout"==n)setTimeout(function(){P(t,"fadeout",function(){t.parentElement.removeChild(t)})},4e3);else if("prompt"==n){var s=document.createElement("br"),c=document.createElement("div");c.classList.add("action");var a=document.createElement("a");a.setAttribute("href","javascript:"),a.innerText="OK";var i=document.createElement("div");i.classList.add("clear"),c.appendChild(a),c.appendChild(i),t.append(s),t.append(c),r=!1,s.addEventListener("click",function(){t.parentElement.removeChild(t),h.classList.remove("halfvisible")})}return t.classList.remove("invisible"),P(t,"uncovertop",function(){console.log("finished uncovering"),r||h.classList.add("halfvisible"),h.classList.remove("uncovertop")}),t.classList.add("defaultcursor"),t},K.clickFocus=function(){return!window.getSelection().toString()&&(h.classList.add("js-console-focus"),h.classList.remove("js-console-nofocus"),e?(n=y,t=window.scrollX,o=window.scrollY,n.focus(),window.scrollTo(t,o)):(y.classList.add("fixposition"),y.focus()),W(),!1);var n,t,o},S=g,u.globalCapture&&(S=document),S.addEventListener("click",K.clickFocus),y.addEventListener("blur",function(){h.classList.remove("js-console-focus"),h.classList.add("js-console-nofocus")}),y.onpaste=function(e){y.value="",setTimeout(function(){y.consoleInsert(y.value),y.value=""},0)},y.onkeydown=function(e){M=0;var n=e.keyCode;if(e.ctrlKey&&67==n)return M=n,"function"==typeof u.cancelHandle&&u.cancelHandle(),!1;if(I){if(e.shiftKey&&n in m)return M=n,m[n](),!1;if(e.altKey&&n in p)return M=n,p[n](),!1;if(e.ctrlKey&&n in d)return M=n,d[n](),!1;if(n in f)return M=n,f[n](),!1}},y.onkeypress=function(n){var t=n.keyCode||n.which;if(function(e){return(e.keyCode==f.tab||192==e.keyCode)&&e.altKey}(n))return!1;if((n.ctrlKey||n.metaKey)&&"v"==String.fromCharCode(t).toLowerCase())return!0;if(I&&M!=t&&t>=32){if(M)return!1;(void 0===u.charInsertTrigger||"function"==typeof u.charInsertTrigger&&u.charInsertTrigger(t,E))&&y.consoleInsert(t)}return!e&&void 0},y.consoleInsert=function(e){var n="number"==typeof e?String.fromCharCode(e):e,t=E.substring(0,T),o=E.substring(T);E=t+n+o,Q(n.length),w=E,re()},K.promptText=function(e){return"string"==typeof e&&(T=(E=e).length,re()),E},K}}();