123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*!
  2. *
  3. * Copyright 2017 - acrazing
  4. *
  5. * @author acrazing joking.young@gmail.com
  6. * @since 2017-11-11 16:03:17
  7. * @version 1.0.0
  8. * @desc react-native-view-shot.d.ts
  9. */
  10. declare module 'react-native-view-shot' {
  11. import { Component, ReactInstance, RefObject } from 'react'
  12. import { ViewStyle } from 'react-native'
  13. export interface CaptureOptions {
  14. /**
  15. * (number): the width and height of the final image (resized from the View bound. don't provide it if you want
  16. * the original pixel size).
  17. */
  18. width?: number;
  19. /**
  20. * @see {CaptureOptions#width}
  21. */
  22. height?: number;
  23. /**
  24. * either png or jpg or webm (Android). Defaults to png. raw is a ARGB array of image pixels.
  25. */
  26. format?: 'jpg' | 'png' | 'webm' | 'raw';
  27. /**
  28. * the quality. 0.0 - 1.0 (default). (only available on lossy formats like jpg)
  29. */
  30. quality?: number;
  31. /**
  32. * the method you want to use to save the snapshot, one of:
  33. " - tmpfile" (default): save to a temporary file (that will only exist for as long as the app is running).
  34. " - base64": encode as base64 and returns the raw string. Use only with small images as this may result of
  35. * lags (the string is sent over the bridge). N.B. This is not a data uri, use data-uri instead.
  36. " - data-uri": same as base64 but also includes the Data URI scheme header.
  37. " - zip-base64: compress data with zip deflate algorithm and than convert to base64 and return as a raw string."
  38. */
  39. result?: 'tmpfile' | 'base64' | 'data-uri' | 'zip-base64';
  40. /**
  41. * if true and when view is a ScrollView, the "content container" height will be evaluated instead of the
  42. * container height.
  43. */
  44. snapshotContentContainer?: boolean;
  45. }
  46. export interface ViewShotProperties {
  47. options?: CaptureOptions;
  48. /**
  49. * - if not defined (default). the capture is not automatic and you need to use the ref and call capture()
  50. * yourself.
  51. * - "mount". Capture the view once at mount. (It is important to understand image loading won't be waited, in
  52. * such case you want to use "none" with viewShotRef.capture() after Image#onLoad.)
  53. * - "continuous" EXPERIMENTAL, this will capture A LOT of images continuously. For very specific use-cases.
  54. * - "update" EXPERIMENTAL, this will capture images each time React redraw (on did update). For very specific
  55. * use-cases.
  56. */
  57. captureMode?: 'mount' | 'continuous' | 'update';
  58. /**
  59. * when a captureMode is defined, this callback will be called with the capture result.
  60. * @param {string} uri
  61. */
  62. onCapture?(uri: string): void;
  63. /**
  64. * when a captureMode is defined, this callback will be called when a capture fails.
  65. * @param {Error} error
  66. */
  67. onCaptureFailure?(error: Error): void;
  68. /**
  69. * style prop as ViewStyle
  70. */
  71. style?: ViewStyle;
  72. }
  73. export default class ViewShot extends Component<ViewShotProperties> {
  74. capture?(): Promise<string>;
  75. }
  76. /**
  77. * lower level imperative API
  78. *
  79. * @param {number | React.ReactInstance | RefObject} viewRef
  80. * @param {"react-native-view-shot".CaptureOptions} options
  81. * @return {Promise<string>} Returns a Promise of the image URI.
  82. */
  83. export function captureRef<T>(viewRef: number | ReactInstance | RefObject<T>, options?: CaptureOptions): Promise<string>
  84. /**
  85. * This method release a previously captured uri. For tmpfile it will clean them out, for other result types it
  86. * just won't do anything.
  87. *
  88. * NB: the tmpfile captures are automatically cleaned out after the app closes, so you might not have to worry
  89. * about this unless advanced usecases. The ViewShot component will use it each time you capture more than once
  90. * (useful for continuous capture to not leak files).
  91. * @param {string} uri
  92. */
  93. export function releaseCapture(uri: string): void
  94. /**
  95. * This method will capture the contents of the currently displayed screen as a native hardware screenshot. It does
  96. * not require a ref input, as it does not work at the view level. This means that ScrollViews will not be captured
  97. * in their entirety - only the portions currently visible to the user.
  98. *
  99. * Returns a Promise of the image URI.
  100. *
  101. * @param {"react-native-view-shot".CaptureOptions} options
  102. * @return {Promise<string>}
  103. */
  104. export function captureScreen(options?: CaptureOptions): Promise<string>
  105. }