mirror of
https://github.com/status-im/react-native.git
synced 2025-02-27 00:20:31 +00:00
Moved takeSnapshot method from UIManager to ReactNative
Reviewed By: spicyj Differential Revision: D4767428 fbshipit-source-id: 77c80c0135641ab46f9dce2763f27499a96373a0
This commit is contained in:
parent
30778b62ea
commit
848593c0f0
@ -28,8 +28,8 @@ var ReactNative = require('react-native');
|
|||||||
var {
|
var {
|
||||||
ActionSheetIOS,
|
ActionSheetIOS,
|
||||||
StyleSheet,
|
StyleSheet,
|
||||||
|
takeSnapshot,
|
||||||
Text,
|
Text,
|
||||||
UIManager,
|
|
||||||
View,
|
View,
|
||||||
} = ReactNative;
|
} = ReactNative;
|
||||||
|
|
||||||
@ -164,7 +164,7 @@ class ShareScreenshotExample extends React.Component {
|
|||||||
|
|
||||||
showShareActionSheet = () => {
|
showShareActionSheet = () => {
|
||||||
// Take the snapshot (returns a temp file uri)
|
// Take the snapshot (returns a temp file uri)
|
||||||
UIManager.takeSnapshot('window').then((uri) => {
|
takeSnapshot('window').then((uri) => {
|
||||||
// Share image data
|
// Share image data
|
||||||
ActionSheetIOS.showShareActionSheetWithOptions({
|
ActionSheetIOS.showShareActionSheetWithOptions({
|
||||||
url: uri,
|
url: uri,
|
||||||
|
@ -15,72 +15,24 @@ const NativeModules = require('NativeModules');
|
|||||||
const Platform = require('Platform');
|
const Platform = require('Platform');
|
||||||
|
|
||||||
const defineLazyObjectProperty = require('defineLazyObjectProperty');
|
const defineLazyObjectProperty = require('defineLazyObjectProperty');
|
||||||
const findNodeHandle = require('findNodeHandle');
|
|
||||||
const invariant = require('fbjs/lib/invariant');
|
const invariant = require('fbjs/lib/invariant');
|
||||||
|
|
||||||
import type React from 'react';
|
|
||||||
|
|
||||||
const { UIManager } = NativeModules;
|
const { UIManager } = NativeModules;
|
||||||
|
|
||||||
invariant(UIManager, 'UIManager is undefined. The native module config is probably incorrect.');
|
invariant(UIManager, 'UIManager is undefined. The native module config is probably incorrect.');
|
||||||
|
|
||||||
const _takeSnapshot = UIManager.takeSnapshot;
|
// In past versions of ReactNative users called UIManager.takeSnapshot()
|
||||||
|
// However takeSnapshot was moved to ReactNative in order to support flat
|
||||||
// findNodeHandle() returns a reference to a wrapper component with viewConfig.
|
// bundles and to avoid a cyclic dependency between UIManager and ReactNative.
|
||||||
// This wrapper is required for NativeMethodsMixin.setNativeProps, but most
|
// UIManager.takeSnapshot still exists though. In order to avoid confusion or
|
||||||
// callers want the native tag (number) and not the wrapper. For this purpose,
|
// accidental usage, mask the method with a deprecation warning.
|
||||||
// the ReactNative renderer decorates findNodeHandle() and extracts the tag.
|
UIManager.__takeSnapshot = UIManager.takeSnapshot;
|
||||||
// However UIManager can't require ReactNative without introducing a cycle, and
|
UIManager.takeSnapshot = function() {
|
||||||
// deferring the require causes a significant performance regression in Wilde
|
invariant(
|
||||||
// (along the lines of 17% regression in RN Bridge startup). So as a temporary
|
false,
|
||||||
// workaround, this wrapper method mimics what the native renderer does.
|
'UIManager.takeSnapshot should not be called directly. ' +
|
||||||
// TODO (bvaughn) Remove this and use findNodeHandle directly once stack is gone
|
'Use ReactNative.takeSnapshot instead.'
|
||||||
function findNodeHandleWrapper(componentOrHandle : any) : ?number {
|
);
|
||||||
const instance: any = findNodeHandle(componentOrHandle);
|
|
||||||
|
|
||||||
if (instance) {
|
|
||||||
return typeof instance._nativeTag === 'number'
|
|
||||||
? instance._nativeTag
|
|
||||||
: instance.getHostNode();
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Capture an image of the screen, window or an individual view. The image
|
|
||||||
* will be stored in a temporary file that will only exist for as long as the
|
|
||||||
* app is running.
|
|
||||||
*
|
|
||||||
* The `view` argument can be the literal string `window` if you want to
|
|
||||||
* capture the entire window, or it can be a reference to a specific
|
|
||||||
* React Native component.
|
|
||||||
*
|
|
||||||
* The `options` argument may include:
|
|
||||||
* - width/height (number) - the width and height of the image to capture.
|
|
||||||
* - format (string) - either 'png' or 'jpeg'. Defaults to 'png'.
|
|
||||||
* - quality (number) - the quality when using jpeg. 0.0 - 1.0 (default).
|
|
||||||
*
|
|
||||||
* Returns a Promise.
|
|
||||||
* @platform ios
|
|
||||||
*/
|
|
||||||
UIManager.takeSnapshot = async function(
|
|
||||||
view ?: 'window' | React.Element<any> | number,
|
|
||||||
options ?: {
|
|
||||||
width ?: number,
|
|
||||||
height ?: number,
|
|
||||||
format ?: 'png' | 'jpeg',
|
|
||||||
quality ?: number,
|
|
||||||
},
|
|
||||||
) {
|
|
||||||
if (!_takeSnapshot) {
|
|
||||||
console.warn('UIManager.takeSnapshot is not available on this platform');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (typeof view !== 'number' && view !== 'window') {
|
|
||||||
view = findNodeHandleWrapper(view) || 'window';
|
|
||||||
}
|
|
||||||
return _takeSnapshot(view, options);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,6 +27,7 @@ const deepFreezeAndThrowOnMutationInDev = require('deepFreezeAndThrowOnMutationI
|
|||||||
const emptyObject = require('fbjs/lib/emptyObject');
|
const emptyObject = require('fbjs/lib/emptyObject');
|
||||||
const findNodeHandle = require('findNodeHandle');
|
const findNodeHandle = require('findNodeHandle');
|
||||||
const invariant = require('fbjs/lib/invariant');
|
const invariant = require('fbjs/lib/invariant');
|
||||||
|
const takeSnapshot = require('takeSnapshot');
|
||||||
|
|
||||||
const {injectInternals} = require('ReactFiberDevToolsHook');
|
const {injectInternals} = require('ReactFiberDevToolsHook');
|
||||||
|
|
||||||
@ -402,6 +403,8 @@ const ReactNative = {
|
|||||||
return NativeRenderer.getPublicRootInstance(root);
|
return NativeRenderer.getPublicRootInstance(root);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
takeSnapshot,
|
||||||
|
|
||||||
unmountComponentAtNode(containerTag: number) {
|
unmountComponentAtNode(containerTag: number) {
|
||||||
const root = roots.get(containerTag);
|
const root = roots.get(containerTag);
|
||||||
if (root) {
|
if (root) {
|
||||||
|
@ -18,6 +18,7 @@ var ReactNativeStackInjection = require('ReactNativeStackInjection');
|
|||||||
var ReactUpdates = require('ReactUpdates');
|
var ReactUpdates = require('ReactUpdates');
|
||||||
|
|
||||||
var findNodeHandle = require('findNodeHandle');
|
var findNodeHandle = require('findNodeHandle');
|
||||||
|
var takeSnapshot = require('takeSnapshot');
|
||||||
|
|
||||||
ReactNativeInjection.inject();
|
ReactNativeInjection.inject();
|
||||||
ReactNativeStackInjection.inject();
|
ReactNativeStackInjection.inject();
|
||||||
@ -45,6 +46,9 @@ var ReactNative = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
render: render,
|
render: render,
|
||||||
|
|
||||||
|
takeSnapshot,
|
||||||
|
|
||||||
unmountComponentAtNode: ReactNativeMount.unmountComponentAtNode,
|
unmountComponentAtNode: ReactNativeMount.unmountComponentAtNode,
|
||||||
|
|
||||||
/* eslint-disable camelcase */
|
/* eslint-disable camelcase */
|
||||||
|
55
Libraries/Renderer/src/renderers/native/takeSnapshot.js
Normal file
55
Libraries/Renderer/src/renderers/native/takeSnapshot.js
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2015-present, Facebook, Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the BSD-style license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||||||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||||||
|
*
|
||||||
|
* @providesModule takeSnapshot
|
||||||
|
* @flow
|
||||||
|
*/
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var ReactNative = require('ReactNative');
|
||||||
|
var UIManager = require('UIManager');
|
||||||
|
|
||||||
|
import type {Element} from 'React';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Capture an image of the screen, window or an individual view. The image
|
||||||
|
* will be stored in a temporary file that will only exist for as long as the
|
||||||
|
* app is running.
|
||||||
|
*
|
||||||
|
* The `view` argument can be the literal string `window` if you want to
|
||||||
|
* capture the entire window, or it can be a reference to a specific
|
||||||
|
* React Native component.
|
||||||
|
*
|
||||||
|
* The `options` argument may include:
|
||||||
|
* - width/height (number) - the width and height of the image to capture.
|
||||||
|
* - format (string) - either 'png' or 'jpeg'. Defaults to 'png'.
|
||||||
|
* - quality (number) - the quality when using jpeg. 0.0 - 1.0 (default).
|
||||||
|
*
|
||||||
|
* Returns a Promise.
|
||||||
|
* @platform ios
|
||||||
|
*/
|
||||||
|
module.exports = async function takeSnapshot(
|
||||||
|
view ?: 'window' | Element<any> | number,
|
||||||
|
options ?: {
|
||||||
|
width ?: number,
|
||||||
|
height ?: number,
|
||||||
|
format ?: 'png' | 'jpeg',
|
||||||
|
quality ?: number,
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
if (
|
||||||
|
typeof view !== 'number' &&
|
||||||
|
view !== 'window'
|
||||||
|
) {
|
||||||
|
view = ReactNative.findNodeHandle(view) || 'window';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the hidden '__takeSnapshot' method; the main one throws an error to
|
||||||
|
// prevent accidental backwards-incompatible usage.
|
||||||
|
return UIManager.__takeSnapshot(view, options);
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user