@samitouri / QOS-React / commits / 004c937cb0

Move takeSnapshot from UIManager to ReactNative renderer(s) (#9292)

* Move takeSnapshot from UIManager to ReactNative renderer(s) * Prettier

Brian Vaughn committed Mar 30, 2017 at 13:10 UTC 004c937cb05811910d9d9fc2226932a8f88f3855
5 files changed +71
flow/react-native-host-hooks.js
+9
@@ -58,6 +58,15 @@ declare module 'UIManager' {
58 viewName : string,
59 props : ?Object,
60 ) : void;
61 + declare function __takeSnapshot(
62 + view ?: 'window' | Element<any> | number,
63 + options ?: {
64 + width ?: number,
65 + height ?: number,
66 + format ?: 'png' | 'jpeg',
67 + quality ?: number,
68 + },
69 + ) : Promise<any>;
70 }
71 declare module 'View' {
72 declare var exports : typeof ReactComponent;
src/__mocks__/UIManager.js
+1
@@ -20,6 +20,7 @@ var RCTUIManager = {
20 updateView: jest.fn(),
21 removeSubviewsFromContainerWithID: jest.fn(),
22 replaceExistingNonRootView: jest.fn(),
23 + __takeSnapshot: jest.fn(),
24 customBubblingEventTypes: {
25 topBlur: {
26 phasedRegistrationNames: {
src/renderers/native/ReactNativeFiber.js
+3
@@ -27,6 +27,7 @@ const deepFreezeAndThrowOnMutationInDev = require('deepFreezeAndThrowOnMutationI
27 const emptyObject = require('fbjs/lib/emptyObject');
28 const findNodeHandle = require('findNodeHandle');
29 const invariant = require('fbjs/lib/invariant');
30 +const takeSnapshot = require('takeSnapshot');
31
32 const {injectInternals} = require('ReactFiberDevToolsHook');
33
@@ -402,6 +403,8 @@ const ReactNative = {
403 return NativeRenderer.getPublicRootInstance(root);
404 },
405
406 + takeSnapshot,
407 +
408 unmountComponentAtNode(containerTag: number) {
409 const root = roots.get(containerTag);
410 if (root) {
src/renderers/native/ReactNativeStack.js
+4
@@ -18,6 +18,7 @@ var ReactNativeStackInjection = require('ReactNativeStackInjection');
18 var ReactUpdates = require('ReactUpdates');
19
20 var findNodeHandle = require('findNodeHandle');
21 +var takeSnapshot = require('takeSnapshot');
22
23 ReactNativeInjection.inject();
24 ReactNativeStackInjection.inject();
@@ -45,6 +46,9 @@ var ReactNative = {
46 },
47
48 render: render,
49 +
50 + takeSnapshot,
51 +
52 unmountComponentAtNode: ReactNativeMount.unmountComponentAtNode,
53
54 /* eslint-disable camelcase */
src/renderers/native/takeSnapshot.js new
+54
@@ -0,0 +1,54 @@
1 +/**
2 + * Copyright (c) 2015-present, Facebook, Inc.
3 + * All rights reserved.
4 + *
5 + * This source code is licensed under the BSD-style license found in the
6 + * LICENSE file in the root directory of this source tree. An additional grant
7 + * of patent rights can be found in the PATENTS file in the same directory.
8 + *
9 + * @providesModule takeSnapshot
10 + * @flow
11 + */
12 +'use strict';
13 +
14 +var ReactNative = require('ReactNative');
15 +var UIManager = require('UIManager');
16 +
17 +import type {Element} from 'React';
18 +
19 +/**
20 + * Capture an image of the screen, window or an individual view. The image
21 + * will be stored in a temporary file that will only exist for as long as the
22 + * app is running.
23 + *
24 + * The `view` argument can be the literal string `window` if you want to
25 + * capture the entire window, or it can be a reference to a specific
26 + * React Native component.
27 + *
28 + * The `options` argument may include:
29 + * - width/height (number) - the width and height of the image to capture.
30 + * - format (string) - either 'png' or 'jpeg'. Defaults to 'png'.
31 + * - quality (number) - the quality when using jpeg. 0.0 - 1.0 (default).
32 + *
33 + * Returns a Promise.
34 + * @platform ios
35 + */
36 +function takeSnapshot(
37 + view?: 'window' | Element<any> | number,
38 + options?: {
39 + width?: number,
40 + height?: number,
41 + format?: 'png' | 'jpeg',
42 + quality?: number,
43 + },
44 +): Promise<any> {
45 + if (typeof view !== 'number' && view !== 'window') {
46 + view = ReactNative.findNodeHandle(view) || 'window';
47 + }
48 +
49 + // Call the hidden '__takeSnapshot' method; the main one throws an error to
50 + // prevent accidental backwards-incompatible usage.
51 + return UIManager.__takeSnapshot(view, options);
52 +}
53 +
54 +module.exports = takeSnapshot;