ng-qrcode.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import * as i1 from '@angular/common';
  2. import { CommonModule } from '@angular/common';
  3. import * as i0 from '@angular/core';
  4. import { isDevMode, Directive, Input, Component, NgModule } from '@angular/core';
  5. import qrcode from 'qrcode';
  6. const validColorRegex = /^#(?:[0-9a-fA-F]{3,4}){1,2}$/;
  7. class QrCodeDirective {
  8. static { this.DEFAULT_ERROR_CORRECTION_LEVEL = "M"; }
  9. static { this.DEFAULT_CENTER_IMAGE_SIZE = 40; }
  10. constructor(viewContainerRef) {
  11. this.viewContainerRef = viewContainerRef;
  12. // eslint-disable-next-line @angular-eslint/no-input-rename
  13. this.errorCorrectionLevel = QrCodeDirective.DEFAULT_ERROR_CORRECTION_LEVEL;
  14. this.darkColor = "#000000FF";
  15. this.lightColor = "#FFFFFFFF";
  16. // eslint-disable-next-line @angular-eslint/no-input-rename
  17. this.margin = 16;
  18. }
  19. async ngOnChanges() {
  20. if (!this.value) {
  21. return;
  22. }
  23. if (this.version && this.version > 40) {
  24. console.warn("[qrCode] max version is 40, clamping");
  25. this.version = 40;
  26. }
  27. else if (this.version && this.version < 1) {
  28. console.warn("[qrCode] min version is 1, clamping");
  29. this.version = 1;
  30. }
  31. else if (this.version !== undefined && isNaN(this.version)) {
  32. console.warn("[qrCode] version should be set to a number, defaulting to auto");
  33. this.version = undefined;
  34. }
  35. const canvas = this.viewContainerRef.element.nativeElement;
  36. if (!canvas) {
  37. // native element not available on server side rendering
  38. return;
  39. }
  40. const context = canvas.getContext("2d");
  41. if (context) {
  42. context.clearRect(0, 0, context.canvas.width, context.canvas.height);
  43. }
  44. const errorCorrectionLevel = this.errorCorrectionLevel ?? QrCodeDirective.DEFAULT_ERROR_CORRECTION_LEVEL;
  45. const dark = validColorRegex.test(this.darkColor) ? this.darkColor : undefined;
  46. const light = validColorRegex.test(this.lightColor) ? this.lightColor : undefined;
  47. if (isDevMode()) {
  48. if (!dark && this.darkColor) {
  49. console.error("[ng-qrcode] darkColor set to invalid value, must be RGBA hex color string, eg: #3050A1FF");
  50. }
  51. if (!light && this.lightColor) {
  52. console.error("[ng-qrcode] lightColor set to invalid value, must be RGBA hex color string, eg: #3050A130");
  53. }
  54. }
  55. await qrcode
  56. .toCanvas(canvas, this.value, {
  57. version: this.version,
  58. errorCorrectionLevel,
  59. width: this.width,
  60. margin: this.margin,
  61. color: {
  62. dark,
  63. light,
  64. },
  65. });
  66. const centerImageSrc = this.centerImageSrc;
  67. const centerImageWidth = getIntOrDefault(this.centerImageWidth, QrCodeDirective.DEFAULT_CENTER_IMAGE_SIZE);
  68. const centerImageHeight = getIntOrDefault(this.centerImageHeight, QrCodeDirective.DEFAULT_CENTER_IMAGE_SIZE);
  69. if (centerImageSrc && context) {
  70. if (!this.centerImage) {
  71. this.centerImage = new Image(centerImageWidth, centerImageHeight);
  72. }
  73. const centerImage = this.centerImage;
  74. if (centerImageSrc !== this.centerImage.src) {
  75. centerImage.src = centerImageSrc;
  76. }
  77. if (centerImageWidth !== this.centerImage.width) {
  78. centerImage.width = centerImageWidth;
  79. }
  80. if (centerImageHeight !== this.centerImage.height) {
  81. centerImage.height = centerImageHeight;
  82. }
  83. const doDrawImage = () => {
  84. context.drawImage(centerImage, canvas.width / 2 - centerImageWidth / 2, canvas.height / 2 - centerImageHeight / 2, centerImageWidth, centerImageHeight);
  85. };
  86. centerImage.onload = doDrawImage;
  87. if (centerImage.complete) {
  88. doDrawImage();
  89. }
  90. }
  91. }
  92. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: QrCodeDirective, deps: [{ token: i0.ViewContainerRef }], target: i0.ɵɵFactoryTarget.Directive }); }
  93. static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "19.0.5", type: QrCodeDirective, isStandalone: true, selector: "canvas[qrCode]", inputs: { value: ["qrCode", "value"], version: ["qrCodeVersion", "version"], errorCorrectionLevel: ["qrCodeErrorCorrectionLevel", "errorCorrectionLevel"], width: "width", height: "height", darkColor: "darkColor", lightColor: "lightColor", centerImageSrc: ["qrCodeCenterImageSrc", "centerImageSrc"], centerImageWidth: ["qrCodeCenterImageWidth", "centerImageWidth"], centerImageHeight: ["qrCodeCenterImageHeight", "centerImageHeight"], margin: ["qrCodeMargin", "margin"] }, usesOnChanges: true, ngImport: i0 }); }
  94. }
  95. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: QrCodeDirective, decorators: [{
  96. type: Directive,
  97. args: [{
  98. // eslint-disable-next-line @angular-eslint/directive-selector
  99. selector: `canvas[qrCode]`,
  100. standalone: true,
  101. }]
  102. }], ctorParameters: () => [{ type: i0.ViewContainerRef }], propDecorators: { value: [{
  103. type: Input,
  104. args: ["qrCode"]
  105. }], version: [{
  106. type: Input,
  107. args: ["qrCodeVersion"]
  108. }], errorCorrectionLevel: [{
  109. type: Input,
  110. args: ["qrCodeErrorCorrectionLevel"]
  111. }], width: [{
  112. type: Input
  113. }], height: [{
  114. type: Input
  115. }], darkColor: [{
  116. type: Input
  117. }], lightColor: [{
  118. type: Input
  119. }], centerImageSrc: [{
  120. type: Input,
  121. args: ["qrCodeCenterImageSrc"]
  122. }], centerImageWidth: [{
  123. type: Input,
  124. args: ["qrCodeCenterImageWidth"]
  125. }], centerImageHeight: [{
  126. type: Input,
  127. args: ["qrCodeCenterImageHeight"]
  128. }], margin: [{
  129. type: Input,
  130. args: ["qrCodeMargin"]
  131. }] } });
  132. function getIntOrDefault(value, defaultValue) {
  133. if (value === undefined || value === "") {
  134. return defaultValue;
  135. }
  136. if (typeof value === "string") {
  137. return parseInt(value, 10);
  138. }
  139. return value;
  140. }
  141. class QrCodeComponent {
  142. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: QrCodeComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
  143. static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "19.0.5", type: QrCodeComponent, isStandalone: true, selector: "qr-code", inputs: { value: "value", size: "size", style: "style", styleClass: "styleClass", darkColor: "darkColor", lightColor: "lightColor", errorCorrectionLevel: "errorCorrectionLevel", centerImageSrc: "centerImageSrc", centerImageSize: "centerImageSize", margin: "margin" }, ngImport: i0, template: `
  144. @if (value) {
  145. <canvas
  146. [qrCode]="value"
  147. [qrCodeErrorCorrectionLevel]="errorCorrectionLevel"
  148. [qrCodeCenterImageSrc]="centerImageSrc"
  149. [qrCodeCenterImageWidth]="centerImageSize"
  150. [qrCodeCenterImageHeight]="centerImageSize"
  151. [qrCodeMargin]="margin"
  152. [width]="size"
  153. [height]="size"
  154. [class]="styleClass"
  155. [ngStyle]="style"
  156. [darkColor]="darkColor"
  157. [lightColor]="lightColor"
  158. >
  159. </canvas>
  160. }
  161. `, isInline: true, dependencies: [{ kind: "directive", type: QrCodeDirective, selector: "canvas[qrCode]", inputs: ["qrCode", "qrCodeVersion", "qrCodeErrorCorrectionLevel", "width", "height", "darkColor", "lightColor", "qrCodeCenterImageSrc", "qrCodeCenterImageWidth", "qrCodeCenterImageHeight", "qrCodeMargin"] }, { kind: "ngmodule", type: CommonModule }, { kind: "directive", type: i1.NgStyle, selector: "[ngStyle]", inputs: ["ngStyle"] }] }); }
  162. }
  163. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: QrCodeComponent, decorators: [{
  164. type: Component,
  165. args: [{ selector: "qr-code", template: `
  166. @if (value) {
  167. <canvas
  168. [qrCode]="value"
  169. [qrCodeErrorCorrectionLevel]="errorCorrectionLevel"
  170. [qrCodeCenterImageSrc]="centerImageSrc"
  171. [qrCodeCenterImageWidth]="centerImageSize"
  172. [qrCodeCenterImageHeight]="centerImageSize"
  173. [qrCodeMargin]="margin"
  174. [width]="size"
  175. [height]="size"
  176. [class]="styleClass"
  177. [ngStyle]="style"
  178. [darkColor]="darkColor"
  179. [lightColor]="lightColor"
  180. >
  181. </canvas>
  182. }
  183. `, standalone: true, imports: [QrCodeDirective, CommonModule] }]
  184. }], propDecorators: { value: [{
  185. type: Input
  186. }], size: [{
  187. type: Input
  188. }], style: [{
  189. type: Input
  190. }], styleClass: [{
  191. type: Input
  192. }], darkColor: [{
  193. type: Input
  194. }], lightColor: [{
  195. type: Input
  196. }], errorCorrectionLevel: [{
  197. type: Input
  198. }], centerImageSrc: [{
  199. type: Input
  200. }], centerImageSize: [{
  201. type: Input
  202. }], margin: [{
  203. type: Input
  204. }] } });
  205. /**
  206. * @deprecated prefer importing `QrCodeComponent` or `QrCodeDirective` directly
  207. */
  208. class QrCodeModule {
  209. static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: QrCodeModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
  210. static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "19.0.5", ngImport: i0, type: QrCodeModule, imports: [CommonModule,
  211. QrCodeComponent,
  212. QrCodeDirective], exports: [QrCodeComponent,
  213. QrCodeDirective] }); }
  214. static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: QrCodeModule, imports: [CommonModule,
  215. QrCodeComponent] }); }
  216. }
  217. i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.0.5", ngImport: i0, type: QrCodeModule, decorators: [{
  218. type: NgModule,
  219. args: [{
  220. declarations: [],
  221. imports: [
  222. CommonModule,
  223. QrCodeComponent,
  224. QrCodeDirective,
  225. ],
  226. exports: [
  227. QrCodeComponent,
  228. QrCodeDirective,
  229. ],
  230. }]
  231. }] });
  232. /*
  233. * Public API Surface of ng-qrcode
  234. */
  235. /**
  236. * Generated bundle index. Do not edit.
  237. */
  238. export { QrCodeComponent, QrCodeDirective, QrCodeModule };
  239. //# sourceMappingURL=ng-qrcode.mjs.map