-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add qrcode web component version
- Loading branch information
1 parent
ef469ba
commit 21e28c5
Showing
21 changed files
with
925 additions
and
108 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "@keystonehq/animated-qr-base", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"homepage": "https://github.com/KeystoneHQ/keystone-airgaped-base#readme", | ||
"scripts": { | ||
"dev": "pnpm run cleanup && tsc -w", | ||
"build": "pnpm run cleanup && tsc", | ||
"cleanup": "rimraf ./dist", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"rimraf": "^5.0.7" | ||
}, | ||
"dependencies": { | ||
"@ngraveio/bc-ur": "^1.1.13" | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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 * from "./constant"; | ||
export * from "./types"; | ||
export * from "./webcamUtils"; | ||
export * from "./getAnimatedScan"; |
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
File renamed without changes.
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,14 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src", "node_modules/*"], | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"baseUrl": "./", | ||
"outDir": "./dist", | ||
"experimentalDecorators": true, | ||
"useDefineForClassFields": false, | ||
"paths": { | ||
"*": ["src/*", "node_modules/*"] | ||
} | ||
}, | ||
} |
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,28 @@ | ||
{ | ||
"name": "@keystonehq/animated-qr-lit", | ||
"version": "0.0.1", | ||
"description": "", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"homepage": "https://github.com/KeystoneHQ/keystone-airgaped-base#readme", | ||
"scripts": { | ||
"dev": "pnpm run cleanup && tsc -w", | ||
"build": "pnpm run cleanup && tsc", | ||
"cleanup": "rimraf ./dist", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"rimraf": "^5.0.7" | ||
}, | ||
"dependencies": { | ||
"@keystonehq/animated-qr-base": "^0.0.1", | ||
"@ngraveio/bc-ur": "^1.1.13", | ||
"@zxing/browser": "^0.1.1", | ||
"@zxing/library": "^0.19.1", | ||
"lit": "^3.1.4", | ||
"qrcode": "^1.5.3" | ||
} | ||
} |
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,82 @@ | ||
import { LitElement, html, css } from "lit"; | ||
import { property } from "lit/decorators/property.js"; | ||
import { customElement } from "lit/decorators/custom-element.js"; | ||
import QRCode from "qrcode"; | ||
import { UR, UREncoder } from "@ngraveio/bc-ur"; | ||
|
||
const MAX_FRAGMENT_LENGTH = 400; | ||
const DEFAULT_INTERVAL = 100; | ||
const DEFAULT_QR_SIZE = 180; | ||
|
||
@customElement("animated-qrcode-lit") | ||
export class AnimatedQRCodeLite extends LitElement { | ||
private timer: NodeJS.Timeout; | ||
private urEncoder: UREncoder; | ||
|
||
@property({ type: String }) | ||
cbor: string; | ||
|
||
@property({ type: String }) | ||
type: string; | ||
|
||
@property({ type: Number }) | ||
capacity: number = MAX_FRAGMENT_LENGTH; | ||
|
||
@property({ type: Number }) | ||
interval: number = DEFAULT_INTERVAL; | ||
|
||
@property({ type: Number }) | ||
size: number = DEFAULT_QR_SIZE; | ||
|
||
static styles = css` | ||
:host { | ||
display: block; | ||
background-color: white; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
`; | ||
|
||
generateQRCode() { | ||
if (this.timer) { | ||
clearInterval(this.timer); | ||
} | ||
this.urEncoder = new UREncoder( | ||
new UR(Buffer.from(this.cbor, "hex"), this.type), | ||
this.capacity | ||
); | ||
this.timer = setInterval(() => { | ||
this.updateQrcode(this.urEncoder.nextPart().toUpperCase()); | ||
}, this.interval); | ||
} | ||
|
||
async updateQrcode(data: string) { | ||
const img = this.shadowRoot.querySelector("img"); | ||
img.src = await QRCode.toDataURL(data, { | ||
margin: 0, | ||
}); | ||
} | ||
|
||
connectedCallback() { | ||
super.connectedCallback(); | ||
this.generateQRCode(); | ||
} | ||
|
||
disconnectedCallback(): void { | ||
super.disconnectedCallback(); | ||
clearInterval(this.timer); | ||
} | ||
|
||
updated(nextProps) { | ||
this.generateQRCode(); | ||
if (nextProps.has("size")) { | ||
this.style.width = `${this.size}px`; | ||
this.style.height = `${this.size}px`; | ||
} | ||
} | ||
|
||
render() { | ||
return html`<img style="width: ${this.size - 10}px; height: auto;" />`; | ||
} | ||
} |
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,68 @@ | ||
import { BarcodeFormat, DecodeHintType } from "@zxing/library"; | ||
import { BrowserQRCodeReader, IScannerControls } from "@zxing/browser"; | ||
import { ScannerProps, getAnimatedScan } from "@keystonehq/animated-qr-base"; | ||
|
||
const codeReader = () => { | ||
const hint = new Map(); | ||
hint.set(DecodeHintType.POSSIBLE_FORMATS, [BarcodeFormat.QR_CODE]); | ||
return new BrowserQRCodeReader(hint, { | ||
delayBetweenScanAttempts: 50, | ||
delayBetweenScanSuccess: 100, | ||
}); | ||
}; | ||
|
||
interface SetupProps extends Omit<ScannerProps, "options"> { | ||
video: HTMLVideoElementScanPreview<IScannerControls>; | ||
} | ||
|
||
export const setupScanner = ({ | ||
video, | ||
purpose, | ||
urTypes, | ||
handleScan, | ||
handleError, | ||
videoLoaded, | ||
onProgress, | ||
}: SetupProps) => { | ||
const { handleScanSuccess, handleScanFailure } = getAnimatedScan({ | ||
purpose, | ||
urTypes, | ||
handleScan, | ||
handleError, | ||
onProgress, | ||
}); | ||
const canplayListener = () => { | ||
videoLoaded && videoLoaded(true); | ||
}; | ||
video.addEventListener("canplay", canplayListener); | ||
const pendingScanRequest = | ||
video?.pendingScanRequest ?? Promise.resolve(undefined); | ||
const scanRequest = pendingScanRequest | ||
.then(() => | ||
codeReader().decodeFromVideoDevice(undefined, video, (result, error) => { | ||
if (result) { | ||
handleScanSuccess(result.getText()); | ||
} | ||
if (error) { | ||
handleScanFailure(error.message); | ||
} | ||
}) | ||
) | ||
.catch((error) => { | ||
console.error(error); | ||
return undefined; | ||
}); | ||
|
||
if (video) { | ||
video.pendingScanRequest = scanRequest; | ||
} | ||
|
||
return () => { | ||
video.removeEventListener("canplay", canplayListener); | ||
scanRequest.then((controls) => { | ||
if (controls) { | ||
controls.stop(); | ||
} | ||
}); | ||
}; | ||
}; |
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,2 @@ | ||
export * from "./AnimatedQRCode"; | ||
export * from "./AnimatedQRScanner"; |
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,14 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src", "node_modules/*", "types/**/*"], | ||
"compilerOptions": { | ||
"rootDir": "./src", | ||
"baseUrl": "./", | ||
"outDir": "./dist", | ||
"experimentalDecorators": true, | ||
"useDefineForClassFields": false, | ||
"paths": { | ||
"*": ["src/*", "node_modules/*"] | ||
} | ||
}, | ||
} |
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,13 @@ | ||
type Nullable<T> = T | null | undefined; | ||
|
||
type HTMLVideoElementScanPreview<T> = HTMLVideoElement & { | ||
pendingScanRequest?: Promise<T | undefined>; | ||
}; | ||
|
||
interface AnimatedQRCodeProps { | ||
cbor: string; | ||
type: string; | ||
size?: number; | ||
capacity?: number; | ||
interval?: number; | ||
} |
Oops, something went wrong.