Skip to content

Commit

Permalink
feat: add Image.resolveAssetSource api
Browse files Browse the repository at this point in the history
  • Loading branch information
duanyuwen committed Jul 20, 2023
1 parent 3babcc4 commit abec2cd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ Prefetches a remote image for later use by downloading it. Once an image has bee
Performs cache interrogation. Returns a mapping from URL to cache status: "disk", "memory", "disk/memory". If a requested URL is not in the mapping, it means it's not in the cache.
{% endcall %}

{% call macro.prop('resolveAssetSource', '?(source: Source) => { uri: string, width?: number, height?: number }') %}
Resolves an asset reference into an object which has the properties uri, width, and height.
{% endcall %}

---

## Examples
Expand Down
18 changes: 17 additions & 1 deletion packages/react-native-web/src/exports/Image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow
*/

import type { ImageProps } from './types';
import type { ImageProps, Source } from './types';

import * as React from 'react';
import createElement from '../createElement';
Expand Down Expand Up @@ -164,6 +164,7 @@ interface ImageStatics {
success: (width: number, height: number) => void,
failure: () => void
) => void;
resolveAssetSource: (source: Source) => { uri: string, width?: number, height?: number };
prefetch: (uri: string) => Promise<void>;
queryCache: (
uris: Array<string>
Expand Down Expand Up @@ -359,6 +360,21 @@ const ImageWithStatics = (Image: React.AbstractComponent<
> &
ImageStatics);

ImageWithStatics.resolveAssetSource = function resolveAssetSource(
source
): { uri: string, width?: number, height?: number } {
const uri = resolveAssetUri(source) || '';
const dimensions = resolveAssetDimensions(source) || {};
let width, height;
if (typeof dimensions.width === 'number') {
width = dimensions.width;
}
if (typeof dimensions.height === 'number') {
height = dimensions.height;
}
return { uri, width, height };
};

ImageWithStatics.getSize = function (uri, success, failure) {
ImageLoader.getSize(uri, success, failure);
};
Expand Down

0 comments on commit abec2cd

Please sign in to comment.