index.js 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //@flow
  2. import { NativeModules, findNodeHandle } from "react-native";
  3. const { RNViewShot } = NativeModules;
  4. export const dirs = {
  5. // cross platform
  6. CacheDir: RNViewShot.CacheDir,
  7. DocumentDir: RNViewShot.DocumentDir,
  8. MainBundleDir: RNViewShot.MainBundleDir,
  9. MovieDir: RNViewShot.MovieDir,
  10. MusicDir: RNViewShot.MusicDir,
  11. PictureDir: RNViewShot.PictureDir,
  12. // only Android
  13. DCIMDir: RNViewShot.DCIMDir,
  14. DownloadDir: RNViewShot.DownloadDir,
  15. RingtoneDir: RNViewShot.RingtoneDir,
  16. SDCardDir: RNViewShot.SDCardDir
  17. };
  18. export function takeSnapshot(
  19. view: number | ReactElement<any>,
  20. options?: {
  21. width?: number,
  22. height?: number,
  23. path?: string,
  24. format?: "png" | "jpg" | "jpeg" | "webm",
  25. quality?: number,
  26. result?: "tmpfile" | "file" | "base64" | "data-uri",
  27. snapshotContentContainer?: boolean
  28. } = {}
  29. ): Promise<string> {
  30. if (options.result === "file") {
  31. console.warn(
  32. "react-native-view-shot: result='file' is deprecated, has been renamed to 'tmpfile' and no longer support any 'path' option. See README for more information"
  33. );
  34. } else if ("path" in options) {
  35. console.warn(
  36. "react-native-view-shot: path option is deprecated. See README for more information"
  37. );
  38. }
  39. if (typeof view !== "number") {
  40. const node = findNodeHandle(view);
  41. if (!node)
  42. return Promise.reject(
  43. new Error("findNodeHandle failed to resolve view=" + String(view))
  44. );
  45. view = node;
  46. }
  47. return RNViewShot.takeSnapshot(view, options);
  48. }
  49. export default { takeSnapshot, dirs };