antosdk-apps/libwvnc/build/debug/main.js

1 line
6.0 KiB
JavaScript
Raw Normal View History

2020-12-29 20:36:35 +01:00
(function(){var e;e=class{constructor(e){var t,n,s;this.socket=void 0,this.ws=void 0,this.canvas=void 0,s="pkg://libwvnc/decoder.js".asFileHandle().getlink(),this.scale=1,e.ws&&(this.ws=e.ws),this.canvas=e.element,"string"==typeof this.canvas&&(this.canvas=document.getElementById(this.canvas)),e.libjpeg&&(t=e.libjpeg),this.decoder=new Worker(s),this.enableEvent=!0,n=this,this.mouseMask=0,this.decoder.postMessage({libjpeg:t}),this.decoder.onmessage=function(e){return n.process(e.data)}}init(){var e;return e=this,new Promise((function(t,n){return e.canvas?($(e.canvas).attr("tabindex","1"),e.initInputEvent(),t()):n("Canvas is not set")}))}initInputEvent(){var e,t,n,s;if(n=this,this.canvas&&(t=function(e){var t;return t=n.canvas.getBoundingClientRect(),{x:Math.floor((e.clientX-t.left)/n.scale),y:Math.floor((e.clientY-t.top)/n.scale)}},s=function(e){var s;if(n.enableEvent)return s=t(e),n.sendPointEvent(s.x,s.y,n.mouseMask)},n.canvas))return n.canvas.oncontextmenu=function(e){return e.preventDefault(),!1},n.canvas.onmousemove=function(e){return s(e)},n.canvas.onmousedown=function(e){var t;return t=1<<e.button,n.mouseMask=n.mouseMask|t,s(e)},n.canvas.onmouseup=function(e){var t;return t=1<<e.button,n.mouseMask=n.mouseMask&~t,s(e)},n.canvas.onkeydown=n.canvas.onkeyup=function(e){var t;switch(e.keyCode){case 8:t=65288;break;case 9:t=65417;break;case 13:t=65293;break;case 27:t=65307;break;case 46:t=65535;break;case 38:t=65362;break;case 40:t=65364;break;case 37:t=65361;break;case 39:t=65363;break;case 91:t=65511;break;case 93:t=65512;break;case 16:t=65505;break;case 17:t=65507;break;case 18:t=65513;break;case 20:t=65509;break;case 113:t=65471;break;case 112:t=65470;break;case 114:t=65472;break;case 115:t=65473;break;case 116:t=65474;break;case 117:t=65475;break;case 118:t=65476;break;case 119:t=65477;break;case 120:t=65478;break;case 121:t=65479;break;case 122:t=65480;break;case 123:t=65481;break;default:t=e.key.charCodeAt(0)}if(e.preventDefault(),t)return"keydown"===e.type?n.sendKeyEvent(t,1):"keyup"===e.type?n.sendKeyEvent(t,0):void 0},this.canvas.addEventListener("wheel",(function(e){var s;if(n.enableEvent)return s=t(e),e.preventDefault(),e.deltaY<0?(n.sendPointEvent(s.x,s.y,8),void n.sendPointEvent(s.x,s.y,0)):(n.sendPointEvent(s.x,s.y,16),n.sendPointEvent(s.x,s.y,0))})),this.canvas.onpaste=function(e){var t;if(n.enableEvent)return t=void 0,window.clipboardData&&window.clipboardData.getData?t=window.clipboardData.getData("Text"):e.clipboardData&&e.clipboardData.getData&&(t=e.clipboardData.getData("text/plain")),!!t&&(e.preventDefault(),n.sendTextAsClipboard(t))},e=e=>this.disconnect(!0),window.addEventListener("unload",e),window.addEventListener("beforeunload",e)}initCanvas(e,t,n){return this.depth=n,this.canvas.width=e,this.canvas.height=t,this.resolution={w:e,h:t,depth:this.depth},this.decoder.postMessage(this.resolution),this.canvas.style.cursor="none",this.setScale(this.scale)}process(e){var t,n,s;if(this.socket)return n=new Uint8Array(e.pixels),(s=(t=this.canvas.getContext("2d",{alpha:!1})).createImageData(e.w,e.h)).data.set(n),t.putImageData(s,e.x,e.y)}setScale(e){if(this.scale=e,this.canvas)return this.canvas.style.transformOrigin="0 0",this.canvas.style.transform="scale("+e+")"}connect(e,t){var n;if(n=this,this.socket&&this.socket.close(),this.ws)return this.socket=new WebSocket(this.ws),this.socket.binaryType="arraybuffer",this.socket.onopen=function(){return console.log("socket opened"),n.initConnection(e,t)},this.socket.onmessage=function(e){return n.consume(e)},this.socket.onclose=function(){return n.socket=null,n.canvas.style.cursor="auto",n.canvas&&n.resolution&&n.canvas.getContext("2d").clearRect(0,0,n.resolution.w,n.resolution.h),console.log("socket closed")}}disconnect(e){if(this.socket&&this.socket.close(),this.decoder&&e)return this.decoder.terminate()}initConnection(e,t){var n;return(n=new Uint8Array(e.length+3))[0]=32,n[1]=1,n[2]=50,t&&(t.bbp&&(n[0]=t.bbp),t.flag&&(n[1]=t.flag),t.quality&&(n[2]=t.quality)),n.set((new TextEncoder).encode(e),3),this.socket.send(this.buildCommand(1,n))}sendPointEvent(e,t,n){