mirror of
https://github.com/paradoxxxzero/butterfly.git
synced 2025-01-27 02:08:53 +00:00
4 lines
No EOL
7.7 KiB
JavaScript
4 lines
No EOL
7.7 KiB
JavaScript
/*! butterfly 2015-04-17 */
|
||
|
||
(function(){var a,b,c,d,e,f,g,h,i,j,k,l=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};j=function(a){var b;return b=function(c){var d;return butterfly.element.classList.remove("alarm"),d="New activity on butterfly terminal ["+butterfly.title+"]",a?new Notification(d,{body:c.data,icon:"/static/images/favicon.png"}):alert(d+"\n"+c.data),butterfly.ws.removeEventListener("message",b)},butterfly.ws.addEventListener("message",b),butterfly.element.classList.add("alarm")},c=function(a){return a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),a.cancelBubble=!0,!1},addEventListener("keydown",function(a){return a.altKey&&65===a.keyCode?(Notification&&"default"===Notification.permission?Notification.requestPermission(function(){return j("granted"===Notification.permission)}):j("granted"===Notification.permission),c(a)):!0}),addEventListener("copy",d=function(a){var b,c,d,e,f,g,h;for(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;e>d;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;return butterfly.bell("pasted"),b=a.clipboardData.getData("text/plain"),b=b.replace(/\r\n/g,"\n").replace(/\n/g,"\r"),butterfly.send(b),a.preventDefault()}),i=null,c=function(a){return a.preventDefault&&a.preventDefault(),a.stopPropagation&&a.stopPropagation(),a.cancelBubble=!0,!1},h=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},g=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},a=function(){function a(){butterfly.element.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.start_line=this.start.node;!this.start_line.classList||l.call(this.start_line.classList,"line")<0;)this.start_line=this.start_line.parentNode;for(this.end_line=this.end.node,c=[];!this.end_line.classList||l.call(this.end_line.classList,"line")<0;)c.push(this.end_line=this.end_line.parentNode);return c},a.prototype.clear=function(){return this.selection.removeAllRanges()},a.prototype.destroy=function(){return butterfly.element.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=butterfly.children.indexOf(this.start_line)+a,b>=0&&b<butterfly.children.length){for(;!butterfly.children[b].textContent.match(/\S/);)if(b+=a,!(b>=0&&b<butterfly.children.length))return;return this.select_line(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.select_line=function(a){var b,c,d;return b=butterfly.children[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.shrink_right=function(){var a,b;return b=this.walk(this.end,/\s/,!0),a=this.walk(b,/\S/,!0),this.collapsed(this.start,a)?void 0:this.end=a},a.prototype.shrink_left=function(){var a,b;return a=this.walk(this.start,/\s/),b=this.walk(a,/\S/),this.collapsed(b,this.end)?void 0:this.start=b},a.prototype.expand_right=function(){var a;return a=this.walk(this.end,/\S/),this.end=this.walk(a,/\s/)},a.prototype.expand_left=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=e.textContent,d=a.offset,c)for(;e;){for(;d>0;)if(f[--d].match(b))return{node:e,offset:d+1};e=h(e),f=e.textContent,d=f.length}else for(;e;){for(;d<f.length;)if(f[d++].match(b))return{node:e,offset:d-1};e=g(e),f=e.textContent,d=0}return a},a}(),document.addEventListener("keydown",function(b){var d,e;if(d=b.keyCode,l.call([16,17,18,19],d)>=0)return!0;if(b.shiftKey&&13===b.keyCode&&!i&&!getSelection().isCollapsed)return butterfly.send(getSelection().toString()),getSelection().removeAllRanges(),c(b);if(i){if(i.reset(),!b.ctrlKey&&b.shiftKey&&37<=(e=b.keyCode)&&40>=e)return!0;if(b.shiftKey&&b.ctrlKey)38===b.keyCode?i.up():40===b.keyCode&&i.down();else if(39===b.keyCode)i.shrink_left();else if(38===b.keyCode)i.expand_left();else if(37===b.keyCode)i.shrink_right();else{if(40!==b.keyCode)return c(b);i.expand_right()}return null!=i&&i.apply(),c(b)}return!i&&b.ctrlKey&&b.shiftKey&&38===b.keyCode?(i=new a,i.select_line(butterfly.y-1),i.apply(),c(b)):!0}),document.addEventListener("keyup",function(a){var b,d;if(b=a.keyCode,l.call([16,17,18,19],b)>=0)return!0;if(i){if(13===a.keyCode)return butterfly.send(i.text()),i.destroy(),i=null,c(a);if(d=a.keyCode,l.call([37,38,39,40],d)<0)return i.destroy(),i=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("DOMContentLoaded",function(){}),/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)&&(e=!1,b=!1,f=!0,k=document.createElement("input"),k.type="password",k.style.position="fixed",k.style.top=0,k.style.left=0,k.style.border="none",k.style.outline="none",k.style.opacity=0,k.value="0",document.body.appendChild(k),k.addEventListener("blur",function(){return setTimeout(function(a){return function(){return a.focus()}}(this),10)}),addEventListener("click",function(){return k.focus()}),addEventListener("touchstart",function(a){return 2===a.touches.length?e=!0:3===a.touches.length?(e=!1,b=!0):4===a.touches.length?(e=!0,b=!0):void 0}),k.addEventListener("keydown",function(a){return butterfly.keyDown(a),!0}),k.addEventListener("input",function(a){var c;return c=this.value.length,0===c?(a.keyCode=8,butterfly.keyDown(a),this.value="0",!0):(a.keyCode=this.value.charAt(1).charCodeAt(0),!e&&!b||f?(butterfly.keyPress(a),f=!1,this.value="0",!0):(a.keyCode=this.value.charAt(1).charCodeAt(0),a.ctrlKey=e,a.altKey=b,a.keyCode>=97&&a.keyCode<=122&&(a.keyCode-=32),butterfly.keyDown(a),this.value="0",e=b=!1,!0))}))}).call(this);
|
||
//# sourceMappingURL=ext.min.js.map
|