-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
534 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
order: 1 | ||
title: 演示 | ||
--- | ||
|
||
最简单的用法。 | ||
|
||
```ts | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-demo', | ||
template: `<qr value="https://ng-alain.com/"></qr>`, | ||
}) | ||
export class DemoComponent {} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
--- | ||
order: 2 | ||
title: 设计器 | ||
--- | ||
|
||
通过 `QRService` 操作更多参数;`change` 可以回调二维码 dataURL 值。 | ||
|
||
```ts | ||
import { Component } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'app-demo', | ||
template: ` | ||
<nz-row [nzGutter]="24"> | ||
<nz-col [nzSpan]="8" class="text-center"> | ||
<qr | ||
[value]="value" | ||
[background]="background" | ||
[backgroundAlpha]="backgroundAlpha" | ||
[foreground]="foreground" | ||
[foregroundAlpha]="foregroundAlpha" | ||
[level]="level" | ||
[mime]="mime" | ||
[padding]="padding" | ||
[size]="size" | ||
(change)="change($event)" | ||
style="border:1px solid #999"></qr> | ||
</nz-col> | ||
<nz-col [nzSpan]="16"> | ||
<form nz-form> | ||
<nz-form-item> | ||
<nz-form-label [nzSpan]="8">背景</nz-form-label> | ||
<nz-form-control [nzSpan]="16"> | ||
<nz-input-group> | ||
<div nz-col nzSpan="12"> | ||
<input nz-input type="color" [(ngModel)]="background" [ngModelOptions]="{standalone: true}"> | ||
</div> | ||
<div nz-col nzSpan="12"> | ||
<nz-input-number [(ngModel)]="backgroundAlpha" [nzMin]="0" [nzMax]="1" [nzStep]="0.1" [ngModelOptions]="{standalone: true}"></nz-input-number> | ||
</div> | ||
</nz-input-group> | ||
</nz-form-control> | ||
</nz-form-item> | ||
<nz-form-item> | ||
<nz-form-label [nzSpan]="8">前景</nz-form-label> | ||
<nz-form-control [nzSpan]="16"> | ||
<nz-input-group> | ||
<div nz-col nzSpan="12"> | ||
<input nz-input type="color" [(ngModel)]="foreground" [ngModelOptions]="{standalone: true}"> | ||
</div> | ||
<div nz-col nzSpan="12"> | ||
<nz-input-number [(ngModel)]="foregroundAlpha" [nzMin]="0" [nzMax]="1" [nzStep]="0.1" [ngModelOptions]="{standalone: true}"></nz-input-number> | ||
</div> | ||
</nz-input-group> | ||
</nz-form-control> | ||
</nz-form-item> | ||
<nz-form-item> | ||
<nz-form-label [nzSpan]="8">误差</nz-form-label> | ||
<nz-form-control [nzSpan]="16"> | ||
<nz-select [(ngModel)]="level" [ngModelOptions]="{standalone: true}"> | ||
<nz-option nzValue="L" nzLabel="L"></nz-option> | ||
<nz-option nzValue="M" nzLabel="M"></nz-option> | ||
<nz-option nzValue="Q" nzLabel="Q"></nz-option> | ||
<nz-option nzValue="H" nzLabel="H"></nz-option> | ||
</nz-select> | ||
</nz-form-control> | ||
</nz-form-item> | ||
<nz-form-item> | ||
<nz-form-label [nzSpan]="8">Mime</nz-form-label> | ||
<nz-form-control [nzSpan]="16"> | ||
<nz-select [(ngModel)]="mime" [ngModelOptions]="{standalone: true}"> | ||
<nz-option nzValue="image/png" nzLabel="image/png"></nz-option> | ||
<nz-option nzValue="image/jpeg" nzLabel="image/jpeg"></nz-option> | ||
<nz-option nzValue="image/gif" nzLabel="image/gif"></nz-option> | ||
</nz-select> | ||
</nz-form-control> | ||
</nz-form-item> | ||
<nz-form-item> | ||
<nz-form-label [nzSpan]="8">内边距</nz-form-label> | ||
<nz-form-control [nzSpan]="16"> | ||
<nz-input-number [(ngModel)]="padding" [ngModelOptions]="{standalone: true}" [nzMin]="0" [nzMax]="100"></nz-input-number>px | ||
</nz-form-control> | ||
</nz-form-item> | ||
<nz-form-item> | ||
<nz-form-label [nzSpan]="8">大小</nz-form-label> | ||
<nz-form-control [nzSpan]="16"> | ||
<nz-input-number [(ngModel)]="size" [ngModelOptions]="{standalone: true}" [nzMin]="100" [nzMax]="1000" [nzStep]="padding"></nz-input-number>px | ||
</nz-form-control> | ||
</nz-form-item> | ||
</form> | ||
</nz-col> | ||
</nz-row> | ||
`, | ||
}) | ||
export class DemoComponent { | ||
value = 'https://ng-alain.com/'; | ||
background = 'white'; | ||
backgroundAlpha = 1.0; | ||
foreground = 'black'; | ||
foregroundAlpha = 1.0; | ||
level = 'L'; | ||
mime = 'image/png'; | ||
padding = 10; | ||
size = 220; | ||
|
||
change(dataURL: string) { | ||
console.log(dataURL); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
--- | ||
title: qr | ||
subtitle: 二维码 | ||
cols: 1 | ||
module: AdQRModule | ||
config: AdQRConfig | ||
--- | ||
|
||
基于 [qrious](https://neocotic.com/qrious) 生成二维码。 | ||
|
||
## 注意 | ||
|
||
默认 `AdQRModule` 并没有强制依赖 `qrious`,因此还需要额外在 `.angular-cli.json` 的 `scripts` 节点引用它。 | ||
|
||
```ts | ||
"scripts": [ | ||
"../node_modules/qrious/dist/qrious.min.js" | ||
] | ||
``` | ||
|
||
## API | ||
|
||
参数 | 说明 | 类型 | 默认值 | ||
----|------|-----|------ | ||
background | 背景 | `string` | `white` | ||
backgroundAlpha | 背景透明级别,范围:`0-1` 之间 | `number` | `1.0` | ||
foreground | 前景 | `string` | `white` | ||
foregroundAlpha | 前景透明级别,范围:`0-1` 之间 | `number` | `1.0` | ||
level | 误差校正级别 | `L,M,Q,H` | `L` | ||
mime | 二维码输出图片MIME类型 | `string` | `image/png` | ||
padding | 内边距(单位:px) | `number` | `10` | ||
size | 大小(单位:px) | `number` | `220` | ||
change | 变更时回调,返回二维码dataURL值 | `EventEmitter<string>` | - |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './public_api'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { QRService } from './qr.service'; | ||
export { QRComponent } from './qr.component'; | ||
export { AdQRConfig } from './qr.config'; | ||
export { AdQRModule } from './qr.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { | ||
Component, | ||
Input, | ||
ChangeDetectionStrategy, | ||
OnChanges, | ||
ChangeDetectorRef, | ||
SimpleChanges, | ||
HostBinding, | ||
Output, | ||
EventEmitter, | ||
} from '@angular/core'; | ||
import { QRService } from './qr.service'; | ||
import { AdQRConfig } from './qr.config'; | ||
|
||
@Component({ | ||
selector: 'qr', | ||
template: ` | ||
<img src="{{dataURL}}"> | ||
`, | ||
preserveWhitespaces: false, | ||
host: { '[class.ad-qr]': 'true' }, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class QRComponent implements OnChanges { | ||
dataURL: string; | ||
// region: fields | ||
|
||
/** 背景 */ | ||
@Input() background: string; | ||
/** 背景透明级别,范围:`0-1` 之间 */ | ||
@Input() backgroundAlpha: number; | ||
/** 前景 */ | ||
@Input() foreground: string; | ||
/** 前景透明级别,范围:`0-1` 之间 */ | ||
@Input() foregroundAlpha: number; | ||
/** 误差校正级别 */ | ||
@Input() level: string; | ||
/** 二维码输出图片MIME类型 */ | ||
@Input() mime: string; | ||
/** 内边距(单位:px) */ | ||
@Input() padding: number; | ||
/** 大小(单位:px) */ | ||
@HostBinding('style.height.px') | ||
@HostBinding('style.width.px') | ||
@Input() size: number; | ||
/** 值 */ | ||
@Input() value: string; | ||
/** 变更时回调 */ | ||
@Output() change = new EventEmitter<string>(); | ||
|
||
// endregion | ||
|
||
constructor( | ||
cog: AdQRConfig, | ||
private srv: QRService, | ||
private cd: ChangeDetectorRef, | ||
) { | ||
Object.assign(this, cog); | ||
} | ||
|
||
ngOnChanges(changes: SimpleChanges): void { | ||
this.dataURL = this.srv.refresh({ | ||
background: this.background, | ||
backgroundAlpha: this.backgroundAlpha, | ||
foreground: this.foreground, | ||
foregroundAlpha: this.foregroundAlpha, | ||
level: this.level, | ||
mime: this.mime, | ||
padding: this.padding, | ||
size: this.size, | ||
value: this.value | ||
}); | ||
this.cd.detectChanges(); | ||
this.change.emit(this.dataURL); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export class AdQRConfig { | ||
/** 背景,默认:`white` */ | ||
background = 'white'; | ||
/** 背景透明级别,范围:`0-1` 之间,默认:`1.0` */ | ||
backgroundAlpha = 1.0; | ||
/** 前景,默认:`black` */ | ||
foreground = 'black'; | ||
/** 前景透明级别,范围:`0-1` 之间,默认:`1.0` */ | ||
foregroundAlpha = 1.0; | ||
/** 误差校正级别,默认:`L` */ | ||
level: 'L' | 'M' | 'Q' | 'H' = 'L'; | ||
/** 二维码输出图片MIME类型,默认:`image/png` */ | ||
mime = 'image/png'; | ||
/** 内边距(单位:px),默认:`10` */ | ||
padding = 10; | ||
/** 大小(单位:px),默认:`220` */ | ||
size = 220; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { NgModule, ModuleWithProviders } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
|
||
import { AdQRConfig } from './qr.config'; | ||
import { QRComponent } from './qr.component'; | ||
import { QRService } from './qr.service'; | ||
|
||
const COMPONENTS = [QRComponent]; | ||
|
||
@NgModule({ | ||
imports: [CommonModule], | ||
declarations: [...COMPONENTS], | ||
exports: [...COMPONENTS], | ||
}) | ||
export class AdQRModule { | ||
static forRoot(): ModuleWithProviders { | ||
return { ngModule: AdQRModule, providers: [AdQRConfig, QRService] }; | ||
} | ||
} |
Oops, something went wrong.