-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
1 lines (1 loc) · 2.81 KB
/
index.js
File metadata and controls
1 lines (1 loc) · 2.81 KB
1
class PrintPlugin{constructor(a="58mm"){if(this.a=new TextEncoder,this.b=new Uint8Array([27,97,1]),this.c=new Uint8Array([27,97,2]),this.d=new Uint8Array([27,97,0]),this.e=new Uint8Array([27,69,1]),this.f=new Uint8Array([27,69,0]),this.g=new Uint8Array([27,45,1]),this.h=new Uint8Array([27,45,0]),this.i=new Uint8Array([29,33,17]),this.j=new Uint8Array([29,33,0]),this.k=null,"58mm"!==a&&"80mm"!==a)throw new Error('Invalid paper size. Only "58mm" and "80mm" are supported.');this.l=a}m(a){this.l=a}async n(){return navigator.bluetooth.getAvailability()}async o(a){a&&(await a.writeValue(this.d.buffer),await a.writeValue(this.j.buffer),await a.writeValue(this.f.buffer),await a.writeValue(this.h.buffer))}async writeLineBreak({count:a=1}={}){if(this.k)for(let b=0;b<a;b++)await this.k.writeValue(new Uint8Array([10]).buffer)}async writeDashLine(){this.k&&(await this.k.writeValue(this.a.encode("-".repeat("58mm"===this.l?32:42))),await this.writeLineBreak())}async writeTextWith2Column(a,b,{bold:c=!1,underline:d=!1,align:e="left",size:f="normal"}={}){this.k&&(c&&await this.k.writeValue(this.e.buffer),d&&await this.k.writeValue(this.g.buffer),"center"===e?await this.k.writeValue(this.b.buffer):"right"===e&&await this.k.writeValue(this.c.buffer),"double"===f&&await this.k.writeValue(this.i.buffer),await this.k.writeValue(this.a.encode(this.p(a,b))),await this.o(this.k),await this.writeLineBreak())}async writeText(a,{bold:b=!1,underline:c=!1,align:d="left",size:e="normal"}={}){this.k&&(b&&await this.k.writeValue(this.e.buffer),c&&await this.k.writeValue(this.g.buffer),"center"===d?await this.k.writeValue(this.b.buffer):"right"===d?await this.k.writeValue(this.c.buffer):await this.k.writeValue(this.d.buffer),"double"===e&&await this.k.writeValue(this.i.buffer),await this.k.writeValue(this.a.encode(a)),await this.o(this.k),await this.writeLineBreak())}async connectToPrint({onReady:a,onFailed:b}){try{if(await this.n())if(null==this.k)navigator.bluetooth.requestDevice({filters:[{acceptAllDevices:!0,services:["000018f0-0000-1000-8000-00805f9b34fb"]}]}).then(c=>(document.getElementById("status").textContent="Got device: "+c.name,c.gatt.connect())).then(c=>(document.getElementById("status").textContent+="\nConnected to GATT Server",c.getPrimaryService("000018f0-0000-1000-8000-00805f9b34fb"))).then(a=>a.getCharacteristics()).then(c=>{document.getElementById("status").textContent+="\nGot characteristics";let d=null;c.forEach(a=>{console.log("Characteristic UUID: "+a.uuid),a.properties.write&&(d=a,this.k=d)}),d?a(this):b("No writable characteristic found.")}).catch(c=>{console.error(c),document.getElementById("status").textContent="Error: "+c,b(c.message)});else a(this);else b("Perangkat Anda tidak mendukung untuk melakukan print dengan Bluetooth")}catch(c){b(c.message)}}p(a,b){const c=("58mm"===this.l?32:40)-a.length-b.length;return a+" ".repeat(c)+b}}