From d1c3cf8bb0964f7244f7c5dcf3247f5584827e0f Mon Sep 17 00:00:00 2001 From: Christopher Willis-Ford Date: Mon, 10 Dec 2018 14:49:05 -0800 Subject: [PATCH] Move image-from-storage code into new ScratchImage component --- src/components/library-item/library-item.jsx | 11 +-- src/components/library/library.jsx | 39 ++++++++-- .../scratch-image/scratch-image.jsx | 71 ++++++++++++++++++ src/containers/library-item.jsx | 74 +------------------ 4 files changed, 111 insertions(+), 84 deletions(-) create mode 100644 src/components/scratch-image/scratch-image.jsx diff --git a/src/components/library-item/library-item.jsx b/src/components/library-item/library-item.jsx index eb418d8d9..4289a5269 100644 --- a/src/components/library-item/library-item.jsx +++ b/src/components/library-item/library-item.jsx @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'; import React from 'react'; import Box from '../box/box.jsx'; +import ScratchImage from '../scratch-image/scratch-image.jsx'; import styles from './library-item.css'; import classNames from 'classnames'; @@ -34,9 +35,9 @@ class LibraryItemComponent extends React.PureComponent { /> ) : null} - {this.props.insetIconURL ? ( @@ -120,9 +121,9 @@ class LibraryItemComponent extends React.PureComponent { {/* Layers of wrapping is to prevent layout thrashing on animation */} - @@ -143,7 +144,7 @@ LibraryItemComponent.propTypes = { extensionId: PropTypes.string, featured: PropTypes.bool, hidden: PropTypes.bool, - iconURI: PropTypes.string, + iconSource: ScratchImage.propTypes.imageSource, insetIconURL: PropTypes.string, internetConnectionRequired: PropTypes.bool, name: PropTypes.oneOfType([ diff --git a/src/components/library/library.jsx b/src/components/library/library.jsx index b0a7a481c..42b342481 100644 --- a/src/components/library/library.jsx +++ b/src/components/library/library.jsx @@ -10,6 +10,7 @@ import Divider from '../divider/divider.jsx'; import Filter from '../filter/filter.jsx'; import TagButton from '../../containers/tag-button.jsx'; import analytics from '../../lib/analytics'; +import storage from '../../lib/storage'; import styles from './library.css'; @@ -29,6 +30,24 @@ const messages = defineMessages({ const ALL_TAG = {tag: 'all', intlLabel: messages.allTag}; const tagListPrefix = [ALL_TAG]; +/** + * Find the AssetType which corresponds to a particular file extension. For example, 'png' => AssetType.ImageBitmap. + * @param {string} fileExtension - the file extension to look up. + * @returns {AssetType} - the AssetType corresponding to the extension, if any. + */ +const getAssetTypeForFileExtension = function (fileExtension) { + const compareOptions = { + sensitivity: 'accent', + usage: 'search' + }; + for (const assetTypeId of Object.keys(storage.AssetType)) { + const assetType = storage.AssetType[assetTypeId]; + if (fileExtension.localeCompare(assetType.runtimeFormat, compareOptions) === 0) { + return assetType; + } + } +}; + class LibraryComponent extends React.Component { constructor (props) { super(props); @@ -172,8 +191,17 @@ class LibraryComponent extends React.Component { })} ref={this.setFilteredDataRef} > - {this.getFilteredData().map((dataItem, index) => ( - { + const iconSource = {}; + if (dataItem.rawURL) { + iconSource.uri = dataItem.rawURL; + } else { + // TODO: adjust libraries to be more storage-friendly; don't use split() here. + const [assetId, fileExtension] = dataItem.md5.split('.'); + iconSource.assetId = assetId; + iconSource.assetType = getAssetTypeForFileExtension(fileExtension); + } + return (