0
0
Fork 0
mirror of https://github.com/paradoxxxzero/butterfly.git synced 2025-04-17 18:22:38 +00:00
paradoxxxzero_butterfly/butterfly/static/ext.min.js
Peter Cai 5bbe456496
term: remove redundant events of inputHelper and redundant contentEditable
We do not need to listen for keydown and keypress for inputHelper because these events will propagate through the parent.
Listening them will be redundant and will cause some shortcut key combinations to stop working.

Since we now have a hidden `textarea`, there is no longer need to set anything to contentEditable
2018-06-03 17:51:16 +08:00

4 lines
No EOL
14 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*! butterfly 2018-06-03 */
(function(){var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y=[].indexOf||function(a){for(var b=0,c=this.length;b<c;b++)if(b in this&&this[b]===a)return b;return-1};g=function(a){var b,c,d,e;if(a.indexOf("")<0)return a;for(c=-1,d="",e="normal";c<a.length-1;)switch(b=a.charAt(++c),e){case"normal":if(""===b){e="escaped";break}d+=b;break;case"escaped":if("["===b){e="csi";break}if("]"===b){e="osc";break}"#()%*+-./".indexOf(b)>=0&&c++,e="normal";break;case"csi":if("?>!$\" '".indexOf(b)>=0)break;if("0"<=b&&b<="9")break;if(";"===b)break;e="normal";break;case"osc":""!==b&&""!==b||(""===b&&c++,e="normal")}return d},t=function(a,b){var c;return c=function(d){var e,f,h;if(e=g(d.data.slice(1)),null===b||b.test(e))return butterfly.body.classList.remove("alarm"),f="Butterfly ["+butterfly.title+"]",a?(h=new Notification(f,{body:e,icon:"/static/images/favicon.png"}),h.onclick=function(){return window.focus(),h.close()}):alert(f+"\n"+e),butterfly.ws.shell.removeEventListener("message",c)},butterfly.ws.shell.addEventListener("message",c),butterfly.body.classList.add("alarm")},f=function(a){return a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),a.cancelBubble=!0,!1},document.addEventListener("keydown",function(a){var b;if(!a.altKey||65!==a.keyCode)return!0;if(b=null,a.shiftKey){if(b=prompt("Ring alarm when encountering the following text: (can be a regexp)"),!b)return;b=new RegExp(b)}return Notification&&"default"===Notification.permission?Notification.requestPermission(function(){return t("granted"===Notification.permission,b)}):t("granted"===Notification.permission,b),f(a)}),addEventListener("copy",h=function(a){var b,c,d,e,f,g,h;for(document.getElementsByTagName("body")[0].contentEditable=!1,butterfly.bell("copied"),a.clipboardData.clearData(),h=getSelection().toString().replace(/\u00A0/g," ").replace(/\u2007/g," "),b="",g=h.split("\n"),d=0,e=g.length;d<e;d++)f=g[d],"⏎"===f.slice(-1)?(c="",f=f.slice(0,-1)):c="\n",b+=f.replace(/\s*$/,"")+c;return a.clipboardData.setData("text/plain",b.slice(0,-1)),a.preventDefault()}),addEventListener("paste",function(a){var b,c,d;return document.getElementsByTagName("body")[0].contentEditable=!1,butterfly.bell("pasted"),b=a.clipboardData.getData("text/plain"),b=b.replace(/\r\n/g,"\n").replace(/\n/g,"\r"),d=1024,c=function(){if(butterfly.send(b.substring(0,d)),b=b.substring(d),b.length)return setTimeout(c,25)},c(),a.preventDefault()}),addEventListener("beforeunload",function(a){if(!(butterfly.body.classList.contains("dead")||location.href.indexOf("session")>-1))return a.returnValue="This terminal is active and not in session. Are you sure you want to kill it?"}),Terminal.on("change",function(a){if(y.call(a.classList,"extended")>=0)return a.addEventListener("click",function(a){return function(){var b,c;return y.call(a.classList,"expanded")>=0?a.classList.remove("expanded"):(c=a.getBoundingClientRect().height,a.classList.add("expanded"),b=a.getBoundingClientRect().height,document.body.scrollTop+=b-c)}}(a))}),x=function(a,b){var c,d,e,f,g;for(f=a.childNodes,g=[],d=0,e=f.length;d<e;d++)c=f[d],b.call(c),g.push(x(c,b));return g},m=function(a){var b,c,d;return d=/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim,c=/(^|[^\/])(www\.[\S]+(\b|$))/gim,b=/[\w.]+@[a-zA-Z_-]+?(?:\.[a-zA-Z]{2,6})+/gim,a.replace(d,'<a href="$&">$&</a>').replace(c,'$1<a href="http://$2">$2</a>').replace(b,'<a href="mailto:$&">$&</a>')},u={"&":"&amp;","<":"&lt;",">":"&gt;"},j=function(a){return a.replace(/[&<>]/g,function(a){return u[a]||a})},Terminal.on("change",function(a){return x(a,function(){var a,b,c;if(3===this.nodeType&&(c=this.nodeValue,a=m(j(c)),a!==c))return b=document.createElement("span"),b.innerHTML=a,this.parentElement.replaceChild(b,this),!0})}),document.addEventListener("keydown",function(a){return!a.altKey||79!==a.keyCode||(open(location.origin),f(a))}),v=null,p=1e3,l=100,n=function(){var a,b,c,d,e,f;if(butterfly.term.childElementCount>p+butterfly.rows){for(a=document.getElementById("packed"),e=document.createDocumentFragment("fragment"),b=c=0,f=p;0<=f?c<=f:c>=f;b=0<=f?++c:--c)e.appendChild(butterfly.term.firstChild);return d=document.createElement("div"),d.classList.add("pack"),d.appendChild(e),a.appendChild(d),a.childElementCount>l&&a.firstChild.remove(),v=setTimeout(n)}},Terminal.on("refresh",function(){return v&&clearTimeout(v),n()}),Terminal.on("clear",function(){var a,b;return b=document.createElement("div"),b.id="packed",a=document.getElementById("packed"),butterfly.body.replaceChild(b,a)}),a=function(){function a(){this.el=document.getElementById("popup"),this.bound_click_maybe_close=this.click_maybe_close.bind(this),this.bound_key_maybe_close=this.key_maybe_close.bind(this)}return a.prototype.open=function(a){return this.el.innerHTML=a,this.el.classList.remove("hidden"),addEventListener("click",this.bound_click_maybe_close),addEventListener("keydown",this.bound_key_maybe_close)},a.prototype.close=function(){return removeEventListener("click",this.bound_click_maybe_close),removeEventListener("keydown",this.bound_key_maybe_close),this.el.classList.add("hidden"),this.el.innerHTML=""},a.prototype.click_maybe_close=function(a){var b;for(b=a.target;b.parentElement;){if(Array.prototype.slice.call(this.el.children).indexOf(b)>-1)return!0;b=b.parentElement}return this.close(),f(a)},a.prototype.key_maybe_close=function(a){return 27!==a.keyCode||(this.close(),f(a))},a}(),q=new a,s=null,f=function(a){return a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),a.cancelBubble=!0,!1},r=function(a){var b;for(b=a.previousSibling,b||(b=a.parentNode.previousSibling),b||(b=a.parentNode.parentNode.previousSibling);b.lastChild;)b=b.lastChild;return b},o=function(a){var b;for(b=a.nextSibling,b||(b=a.parentNode.nextSibling),b||(b=a.parentNode.parentNode.nextSibling);null!=b?b.firstChild:void 0;)b=b.firstChild;return b},b=function(){function a(){butterfly.body.classList.add("selection"),this.selection=getSelection()}return a.prototype.reset=function(){var a,b,c;for(this.selection=getSelection(),a=document.createRange(),a.setStart(this.selection.anchorNode,this.selection.anchorOffset),a.setEnd(this.selection.focusNode,this.selection.focusOffset),this.start={node:this.selection.anchorNode,offset:this.selection.anchorOffset},this.end={node:this.selection.focusNode,offset:this.selection.focusOffset},a.collapsed&&(b=[this.end,this.start],this.start=b[0],this.end=b[1]),this.startLine=this.start.node;!this.startLine.classList||y.call(this.startLine.classList,"line")<0;)this.startLine=this.startLine.parentNode;for(this.endLine=this.end.node,c=[];!this.endLine.classList||y.call(this.endLine.classList,"line")<0;)c.push(this.endLine=this.endLine.parentNode);return c},a.prototype.clear=function(){return this.selection.removeAllRanges()},a.prototype.destroy=function(){return butterfly.body.classList.remove("selection"),this.clear()},a.prototype.text=function(){return this.selection.toString().replace(/\u00A0/g," ").replace(/\u2007/g," ")},a.prototype.up=function(){return this.go(-1)},a.prototype.down=function(){return this.go(1)},a.prototype.go=function(a){var b;if(b=Array.prototype.indexOf.call(butterfly.term.childNodes,this.startLine)+a,0<=b&&b<butterfly.term.childElementCount){for(;!butterfly.term.childNodes[b].textContent.match(/\S/);)if(b+=a,!(0<=b&&b<butterfly.term.childElementCount))return;return this.selectLine(b)}},a.prototype.apply=function(){var a;return this.clear(),a=document.createRange(),a.setStart(this.start.node,this.start.offset),a.setEnd(this.end.node,this.end.offset),this.selection.addRange(a)},a.prototype.selectLine=function(a){var b,c,d;return b=butterfly.term.childNodes[a],d={node:b.firstChild,offset:0},c={node:b.lastChild,offset:b.lastChild.textContent.length},this.start=this.walk(d,/\S/),this.end=this.walk(c,/\S/,!0)},a.prototype.collapsed=function(a,b){var c;return c=document.createRange(),c.setStart(a.node,a.offset),c.setEnd(b.node,b.offset),c.collapsed},a.prototype.shrinkRight=function(){var a,b;if(b=this.walk(this.end,/\s/,!0),a=this.walk(b,/\S/,!0),!this.collapsed(this.start,a))return this.end=a},a.prototype.shrinkLeft=function(){var a,b;if(a=this.walk(this.start,/\s/),b=this.walk(a,/\S/),!this.collapsed(b,this.end))return this.start=b},a.prototype.expandRight=function(){var a;return a=this.walk(this.end,/\S/),this.end=this.walk(a,/\s/)},a.prototype.expandLeft=function(){var a;return a=this.walk(this.start,/\S/,!0),this.start=this.walk(a,/\s/,!0)},a.prototype.walk=function(a,b,c){var d,e,f;if(null==c&&(c=!1),e=a.node.firstChild?a.node.firstChild:a.node,f=null!=e?e.textContent:void 0,d=a.offset,c)for(;e;){for(;d>0;)if(f[--d].match(b))return{node:e,offset:d+1};e=r(e),f=null!=e?e.textContent:void 0,d=f.length}else for(;e;){for(;d<f.length;)if(f[d++].match(b))return{node:e,offset:d-1};e=o(e),f=null!=e?e.textContent:void 0,d=0}return a},a}(),document.addEventListener("keydown",function(a){var c,d,e;if(d=a.keyCode,y.call([16,17,18,19],d)>=0)return!0;if(a.shiftKey&&13===a.keyCode&&!s&&!getSelection().isCollapsed)return butterfly.send(getSelection().toString()),getSelection().removeAllRanges(),f(a);if(s){if(s.reset(),!a.ctrlKey&&a.shiftKey&&37<=(e=a.keyCode)&&e<=40)return!0;if(a.shiftKey&&a.ctrlKey)38===a.keyCode?s.up():40===a.keyCode&&s.down();else if(39===a.keyCode)s.shrinkLeft();else if(38===a.keyCode)s.expandLeft();else if(37===a.keyCode)s.shrinkRight();else{if(40!==a.keyCode)return f(a);s.expandRight()}return null!=s&&s.apply(),f(a)}return!(!s&&a.ctrlKey&&a.shiftKey&&38===a.keyCode)||(c=Math.max(butterfly.term.childElementCount-butterfly.rows,0),s=new b,s.selectLine(c+butterfly.y-1),s.apply(),f(a))}),document.addEventListener("keyup",function(a){var b,c;if(b=a.keyCode,y.call([16,17,18,19],b)>=0)return!0;if(s){if(13===a.keyCode)return butterfly.send(s.text()),s.destroy(),s=null,f(a);if(c=a.keyCode,y.call([37,38,39,40],c)<0)return s.destroy(),s=null,!0}return!0}),document.addEventListener("dblclick",function(a){var b,c,d,e,f;if(!(a.ctrlKey||a.altkey||(f=getSelection(),f.isCollapsed||f.toString().match(/\s/)))){for(e=document.createRange(),e.setStart(f.anchorNode,f.anchorOffset),e.setEnd(f.focusNode,f.focusOffset),e.collapsed&&(f.removeAllRanges(),d=document.createRange(),d.setStart(f.focusNode,f.focusOffset),d.setEnd(f.anchorNode,f.anchorOffset),f.addRange(d));!f.toString().match(/\s/)&&f.toString();)f.modify("extend","forward","character");for(f.modify("extend","backward","character"),b=f.anchorNode,c=f.anchorOffset,f.collapseToEnd(),f.extend(b,c);!f.toString().match(/\s/)&&f.toString();)f.modify("extend","backward","character");return f.modify("extend","forward","character")}}),document.addEventListener("keydown",function(a){var b;return!a.altKey||69!==a.keyCode||(b=new XMLHttpRequest,b.addEventListener("load",function(){var a,b,c,d,e,f;if(e=JSON.parse(this.responseText),c="<div>",c+="<h2>Session list</h2>",0===e.sessions.length)c+="No current session for user "+e.user;else{for(c+="<ul>",d=e.sessions,a=0,b=d.length;a<b;a++)f=d[a],c+='<li><a href="/session/'+f+'">'+f+"</a></li>";c+="</ul>"}return c+="</div>",q.open(c)}),b.open("GET","/sessions/list.json"),b.send(),f(a))}),c=function(a){var b;return document.getElementById("style").setAttribute("href",a),b=document.createElement("img"),b.onerror=function(){return setTimeout(function(){return"undefined"!=typeof butterfly&&null!==butterfly?butterfly.resize():void 0},250)},b.src=a},d="undefined"!=typeof localStorage&&null!==localStorage?localStorage.getItem("theme"):void 0,d&&c(d),this.set_theme=function(a){if(d=a,"undefined"!=typeof localStorage&&null!==localStorage&&localStorage.setItem("theme",a),a)return c(a)},document.addEventListener("keydown",function(a){var b,e;return!a.altKey||83!==a.keyCode||(a.shiftKey?(e=document.getElementById("style").getAttribute("href"),e=e.split("?")[0],c(e+"?"+(new Date).getTime()),f(a)):(b=new XMLHttpRequest,b.addEventListener("load",function(){var a,b,c,e,f,g,h,i,j,k,l,m;if(i=JSON.parse(this.responseText),a=i.builtin_themes,l=i.themes,b='<form>\n <h2>Pick a theme:</h2>\n <select id="theme_list">',h=function(a,c){return b+="<option ",d===a&&(b+="selected "),b+='value="'+a+'">',b+=c,b+="</option>"},h("/static/main.css","default"),l.length){for(b+='<optgroup label="Local themes">',c=0,f=l.length;c<f;c++)j=l[c],m="/theme/"+j+"/style.css",h(m,j);b+="</optgroup>"}for(b+='<optgroup label="Built-in themes">',e=0,g=a.length;e<g;e++)j=a[e],m="/theme/"+j+"/style.css",h(m,j.slice("built-in-".length));return b+="</optgroup>",b+=" </select>\n <label>You can create yours in "+i.dir+".</label>\n</form>",q.open(b),k=document.getElementById("theme_list"),k.addEventListener("change",function(){return set_theme(k.value)})}),b.open("GET","/themes/list.json"),b.send(),f(a)))}),/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)&&(i=!1,e=!1,k=!0,w=document.createElement("input"),w.type="password",w.style.position="fixed",w.style.top=0,w.style.left=0,w.style.border="none",w.style.outline="none",w.style.opacity=0,w.value="0",document.body.appendChild(w),w.addEventListener("blur",function(){return setTimeout(function(a){return function(){return a.focus()}}(this),10)}),addEventListener("click",function(){return w.focus()}),addEventListener("touchstart",function(a){return 2===a.touches.length?i=!0:3===a.touches.length?(i=!1,e=!0):4===a.touches.length?(i=!0,e=!0):void 0}),w.addEventListener("keydown",function(a){return butterfly.keyDown(a),!0}),w.addEventListener("input",function(a){var b;return b=this.value.length,0===b?(a.keyCode=8,butterfly.keyDown(a),this.value="0",!0):(a.keyCode=this.value.charAt(1).charCodeAt(0),!i&&!e||k?(butterfly.keyPress(a),k=!1,this.value="0",!0):(a.keyCode=this.value.charAt(1).charCodeAt(0),a.ctrlKey=i,a.altKey=e,a.keyCode>=97&&a.keyCode<=122&&(a.keyCode-=32),butterfly.keyDown(a),this.value="0",i=e=!1,!0))}))}).call(this);
//# sourceMappingURL=ext.min.js.map