mempool/frontend/src/app/components/qrcode/qrcode.component.ts

68 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-05-06 00:52:25 +04:00
import { Component, Input, AfterViewInit, ViewChild, ElementRef, ChangeDetectionStrategy } from '@angular/core';
2021-12-08 20:24:33 +04:00
import * as QRCode from 'qrcode';
import { StateService } from 'src/app/services/state.service';
@Component({
selector: 'app-qrcode',
templateUrl: './qrcode.component.html',
2022-05-06 00:52:25 +04:00
styleUrls: ['./qrcode.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class QrcodeComponent implements AfterViewInit {
@Input() data: string;
2020-10-26 18:29:25 +07:00
@Input() size = 125;
2020-10-27 15:26:37 +07:00
@Input() imageUrl: string;
@ViewChild('canvas') canvas: ElementRef;
qrcodeObject: any;
constructor(
private stateService: StateService,
) { }
2022-05-06 00:52:25 +04:00
ngOnChanges() {
if (!this.canvas || !this.canvas.nativeElement) {
2022-05-06 00:52:25 +04:00
return;
}
this.render();
}
ngAfterViewInit() {
2022-05-06 00:52:25 +04:00
this.render();
}
render() {
if (!this.stateService.isBrowser) {
return;
}
2021-12-08 20:24:33 +04:00
const opts: QRCode.QRCodeRenderersOptions = {
errorCorrectionLevel: 'L',
margin: 0,
color: {
dark: '#000',
light: '#fff'
},
2020-10-26 18:29:25 +07:00
width: this.size,
};
if (!this.data) {
return;
}
const address = this.data;
if (
this.data.indexOf('bc1') === 0 ||
this.data.indexOf('tb1') === 0 ||
this.data.indexOf('bcrt1') === 0
) {
address.toUpperCase();
}
2020-07-14 14:38:52 +07:00
QRCode.toCanvas(this.canvas.nativeElement, address, opts, (error: any) => {
if (error) {
console.error(error);
}
});
}
}