microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/qrCodeContentProvider.ts
34lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import * as qr from "qr-image"; |
| 5 | import { TextDocumentContentProvider, Uri, CancellationToken } from "vscode"; |
| 6 | |
| 7 | export class QRCodeContentProvider implements TextDocumentContentProvider { |
| 8 | |
| 9 | private cache: { [uri: string]: string } = {}; |
| 10 | |
| 11 | public provideTextDocumentContent(uri: Uri, token: CancellationToken): string { |
| 12 | |
| 13 | let stringUri = uri.toString(); |
| 14 | |
| 15 | if (!this.cache[stringUri]) { |
| 16 | const imageBuffer: NodeBuffer = qr.imageSync(stringUri); |
| 17 | this.cache[stringUri] = "data:image/png;base64," + imageBuffer.toString("base64"); |
| 18 | } |
| 19 | |
| 20 | return `<!DOCTYPE html> |
| 21 | <html> |
| 22 | <body> |
| 23 | <div style="text-align:center"> |
| 24 | <h3> |
| 25 | Expo is running. Open your Expo app at<br/> |
| 26 | <span style="text-decoration: underline">${stringUri}</span><br/> |
| 27 | or scan QR code below: |
| 28 | <h3> |
| 29 | <img src="${this.cache[stringUri]}" /> |
| 30 | </div> |
| 31 | </body> |
| 32 | </html>`; |
| 33 | } |
| 34 | } |
| 35 | |