2015-11-27 13:39:00 +00:00
|
|
|
/**
|
|
|
|
* 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 UIManager
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-05-09 15:20:08 +00:00
|
|
|
const NativeModules = require('NativeModules');
|
2016-09-23 18:12:54 +00:00
|
|
|
const Platform = require('Platform');
|
2016-05-09 15:20:08 +00:00
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
const defineLazyObjectProperty = require('defineLazyObjectProperty');
|
2016-11-04 12:40:26 +00:00
|
|
|
const findNodeHandle = require('findNodeHandle');
|
2016-08-05 18:19:11 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2015-11-27 13:39:00 +00:00
|
|
|
|
2016-10-14 17:40:15 +00:00
|
|
|
import type React from 'react';
|
|
|
|
|
2016-09-23 18:12:54 +00:00
|
|
|
const { UIManager } = NativeModules;
|
|
|
|
|
2016-08-05 18:19:11 +00:00
|
|
|
invariant(UIManager, 'UIManager is undefined. The native module config is probably incorrect.');
|
2016-02-23 10:26:11 +00:00
|
|
|
|
2016-08-17 17:22:51 +00:00
|
|
|
const _takeSnapshot = UIManager.takeSnapshot;
|
|
|
|
|
2017-03-20 19:58:08 +00:00
|
|
|
// findNodeHandle() returns a reference to a wrapper component with viewConfig.
|
|
|
|
// This wrapper is required for NativeMethodsMixin.setNativeProps, but most
|
|
|
|
// callers want the native tag (number) and not the wrapper. For this purpose,
|
|
|
|
// the ReactNative renderer decorates findNodeHandle() and extracts the tag.
|
|
|
|
// However UIManager can't require ReactNative without introducing a cycle, and
|
|
|
|
// deferring the require causes a significant performance regression in Wilde
|
|
|
|
// (along the lines of 17% regression in RN Bridge startup). So as a temporary
|
|
|
|
// workaround, this wrapper method mimics what the native renderer does.
|
|
|
|
// TODO (bvaughn) Remove this and use findNodeHandle directly once stack is gone
|
|
|
|
function findNodeHandleWrapper(componentOrHandle : any) : ?number {
|
|
|
|
const instance: any = findNodeHandle(componentOrHandle);
|
|
|
|
|
|
|
|
if (instance) {
|
|
|
|
return typeof instance._nativeTag === 'number'
|
|
|
|
? instance._nativeTag
|
|
|
|
: instance.getHostNode();
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-23 10:26:11 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2016-04-06 16:20:39 +00:00
|
|
|
*
|
2016-02-24 17:05:28 +00:00
|
|
|
* 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
|
2016-02-23 10:26:11 +00:00
|
|
|
* 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(
|
2016-10-16 11:11:59 +00:00
|
|
|
view ?: 'window' | React.Element<any> | number,
|
2016-02-23 10:26:11 +00:00
|
|
|
options ?: {
|
2016-08-05 18:19:11 +00:00
|
|
|
width ?: number,
|
|
|
|
height ?: number,
|
|
|
|
format ?: 'png' | 'jpeg',
|
|
|
|
quality ?: number,
|
2016-02-23 10:26:11 +00:00
|
|
|
},
|
|
|
|
) {
|
|
|
|
if (!_takeSnapshot) {
|
|
|
|
console.warn('UIManager.takeSnapshot is not available on this platform');
|
|
|
|
return;
|
|
|
|
}
|
2016-02-24 17:05:28 +00:00
|
|
|
if (typeof view !== 'number' && view !== 'window') {
|
2017-03-20 19:58:08 +00:00
|
|
|
view = findNodeHandleWrapper(view) || 'window';
|
2016-02-23 10:26:11 +00:00
|
|
|
}
|
|
|
|
return _takeSnapshot(view, options);
|
|
|
|
};
|
|
|
|
|
2016-05-09 15:20:08 +00:00
|
|
|
/**
|
|
|
|
* Copies the ViewManager constants and commands into UIManager. This is
|
|
|
|
* only needed for iOS, which puts the constants in the ViewManager
|
|
|
|
* namespace instead of UIManager, unlike Android.
|
|
|
|
*/
|
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
Object.keys(UIManager).forEach(viewName => {
|
|
|
|
const viewConfig = UIManager[viewName];
|
|
|
|
if (viewConfig.Manager) {
|
2016-09-23 18:12:54 +00:00
|
|
|
defineLazyObjectProperty(viewConfig, 'Constants', {
|
2016-05-09 15:20:08 +00:00
|
|
|
get: () => {
|
2016-09-23 18:12:54 +00:00
|
|
|
const viewManager = NativeModules[viewConfig.Manager];
|
2016-09-23 18:12:54 +00:00
|
|
|
const constants = {};
|
2016-05-09 15:20:08 +00:00
|
|
|
viewManager && Object.keys(viewManager).forEach(key => {
|
|
|
|
const value = viewManager[key];
|
|
|
|
if (typeof value !== 'function') {
|
|
|
|
constants[key] = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return constants;
|
|
|
|
},
|
|
|
|
});
|
2016-09-23 18:12:54 +00:00
|
|
|
defineLazyObjectProperty(viewConfig, 'Commands', {
|
2016-05-09 15:20:08 +00:00
|
|
|
get: () => {
|
2016-09-23 18:12:54 +00:00
|
|
|
const viewManager = NativeModules[viewConfig.Manager];
|
2016-09-23 18:12:54 +00:00
|
|
|
const commands = {};
|
2016-05-09 15:20:08 +00:00
|
|
|
let index = 0;
|
|
|
|
viewManager && Object.keys(viewManager).forEach(key => {
|
|
|
|
const value = viewManager[key];
|
|
|
|
if (typeof value === 'function') {
|
|
|
|
commands[key] = index++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return commands;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-10-07 12:31:30 +00:00
|
|
|
} else if (Platform.OS === 'android' && UIManager.AndroidLazyViewManagersEnabled) {
|
2017-01-20 23:53:42 +00:00
|
|
|
UIManager.ViewManagerNames.forEach(viewManagerName => {
|
|
|
|
defineLazyObjectProperty(UIManager, viewManagerName, {
|
|
|
|
get: () => NativeModules[viewManagerName.replace(/^(RCT|RK)/, '')],
|
|
|
|
});
|
|
|
|
});
|
2016-05-09 15:20:08 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:39:00 +00:00
|
|
|
module.exports = UIManager;
|