Skip to content

Commit

Permalink
Support AssetRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoo committed Jul 10, 2017
1 parent f1c0672 commit e0d2c8c
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/components/Image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import applyNativeMethods from '../../modules/applyNativeMethods';
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import createDOMElement from '../../modules/createDOMElement';
import { getAssetByID } from 'AssetRegistry';
import ImageLoader from '../../modules/ImageLoader';
import ImageResizeMode from './ImageResizeMode';
import ImageStylePropTypes from './ImageStylePropTypes';
Expand All @@ -39,7 +40,8 @@ const ImageSourcePropType = oneOfType([
uri: string.isRequired,
width: number
}),
string
number,
string,
]);

const getImageState = (uri, shouldDisplaySource) => {
Expand All @@ -50,12 +52,22 @@ const resolveAssetDimensions = source => {
if (typeof source === 'object') {
const { height, width } = source;
return { height, width };
} else if (typeof source === 'number') {
const { height, width } = getAssetByID(source);
return { height, width };
}
};

const svgDataUriPattern = /^data:image\/svg\+xml;/;
const resolveAssetSource = source => {
const uri = typeof source === 'object' ? source.uri : source || '';
let uri = typeof source === 'object' ? source.uri : source || '';
if (typeof source === 'number') {
// Get the actual URI from the packager
const asset = getAssetByID(source);
const scale = asset.scales[0];
const scaleSuffix = scale !== 1 ? `@${scale}x` : '';
uri = asset ? `${asset.httpServerLocation}/${asset.name}${scaleSuffix}.${asset.type}` : '';
}
// SVG data may contain characters (e.g., #, ") that need to be escaped
if (svgDataUriPattern.test(uri)) {
const parts = uri.split('<svg');
Expand Down

3 comments on commit e0d2c8c

@necolas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious what you were doing that led you to add AssetRegistry :)

@fmoo
Copy link
Owner Author

@fmoo fmoo commented on e0d2c8c Jul 11, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I've been experimenting with using metro-bundler, which uses AssetRegistry to keep track of assets in a couple places, instead of webpack.

I have a local fork of it as that uses the haste-style imports instead of the ones I linked above though; I was having trouble injecting module subdirectories via extraNodeModules. I could probably just punch a hole in my packager blacklist to let react-native's through instead though.

@necolas
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Haul uses it too. It's on the to-do list (see necolas#237 ...although perhaps a Babel plugin would be better to share across bundlers)

Please sign in to comment.