UNPKG

1.27 kBJavaScriptView Raw
1import { findDOMNode } from 'react-dom';
2import * as Creator from './Creator.web';
3/**
4 * Taking a snapshot of DOM is not part of native browser behavior.
5 * This is a hack to best emulate mobile functionality.
6 * This implementation is based on https://github.com/pbakaus/domvas by Paul Bakaus http://paulbakaus.com/
7 */
8export default async function captureRef(component, options = {}) {
9 const element = getElement(component || document.body);
10 const { format = 'png' } = options;
11 const finalFormat = format.toLowerCase();
12 switch (finalFormat) {
13 case 'jpg':
14 return Creator.createJPEGAsync(element, options);
15 case 'png':
16 return Creator.createPNGAsync(element, options);
17 case 'raw':
18 return Creator.createPixelDataAsync(element, options);
19 case 'svg':
20 return Creator.createSVGAsync(element, options);
21 case 'blob':
22 return Creator.createBlobAsync(element, options);
23 default:
24 throw new Error(`takeSnapshotAsync: Unsupported format: ${finalFormat}`);
25 }
26}
27const getElement = component => {
28 try {
29 return findDOMNode(component);
30 }
31 catch (e) {
32 return component;
33 }
34};
35//# sourceMappingURL=captureRef.web.js.map
\No newline at end of file