Skip to content
Snippets Groups Projects
Commit f8251cc6 authored by Karishma Chadha's avatar Karishma Chadha
Browse files

Cached url should be shared across watermark and sprite-selector-item for...

Cached url should be shared across watermark and sprite-selector-item for performance, address other review feedback.
parent b5e97942
No related branches found
No related tags found
No related merge requests found
...@@ -46,15 +46,7 @@ class SpriteSelectorItem extends React.Component { ...@@ -46,15 +46,7 @@ class SpriteSelectorItem extends React.Component {
if (this.props.costumeURL) return this.props.costumeURL; if (this.props.costumeURL) return this.props.costumeURL;
if (!this.props.assetId) return null; if (!this.props.assetId) return null;
if (this.decodedAssetId === this.props.assetId) { return getCostumeUrl(this.props.assetId, this.props.vm);
// @todo consider caching more than one URL.
return this.cachedUrl;
}
this.decodedAssetId = this.props.assetId;
this.cachedUrl = getCostumeUrl(this.props.assetId, this.props.vm);
return this.cachedUrl;
} }
handleMouseUp () { handleMouseUp () {
this.initialOffset = null; this.initialOffset = null;
......
import bindAll from 'lodash.bindall'; import bindAll from 'lodash.bindall';
import omit from 'lodash.omit';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import React from 'react'; import React from 'react';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
...@@ -21,28 +22,15 @@ class Watermark extends React.Component { ...@@ -21,28 +22,15 @@ class Watermark extends React.Component {
getCostumeData () { getCostumeData () {
if (!this.props.assetId) return null; if (!this.props.assetId) return null;
if (this.decodedAssetId === this.props.assetId) { return getCostumeUrl(this.props.assetId, this.props.vm);
return this.cachedUrl;
}
this.decodedAssetId = this.props.assetId;
this.cachedUrl = getCostumeUrl(this.props.assetId, this.props.vm);
return this.cachedUrl;
} }
render () { render () {
const { const componentProps = omit(this.props, ['assetId', 'vm']);
/* eslint-disable no-unused-vars */
assetId,
vm,
/* eslint-enable no-unused-vars */
...props
} = this.props;
return ( return (
<WatermarkComponent <WatermarkComponent
costumeURL={this.getCostumeData()} costumeURL={this.getCostumeData()}
{...props} {...componentProps}
/> />
); );
} }
...@@ -73,11 +61,8 @@ const mapStateToProps = state => { ...@@ -73,11 +61,8 @@ const mapStateToProps = state => {
}; };
}; };
const mapDispatchToProps = () => ({});
const ConnectedComponent = connect( const ConnectedComponent = connect(
mapStateToProps, mapStateToProps
mapDispatchToProps
)(Watermark); )(Watermark);
export default ConnectedComponent; export default ConnectedComponent;
...@@ -3,27 +3,37 @@ import {SVGRenderer} from 'scratch-svg-renderer'; ...@@ -3,27 +3,37 @@ import {SVGRenderer} from 'scratch-svg-renderer';
// Contains 'font-family', but doesn't only contain 'font-family="none"' // Contains 'font-family', but doesn't only contain 'font-family="none"'
const HAS_FONT_REGEXP = 'font-family(?!="none")'; const HAS_FONT_REGEXP = 'font-family(?!="none")';
const getCostumeUrl = function (assetId, vm) { const getCostumeUrl = (function () {
let cachedAssetId;
const storage = vm.runtime.storage; let cachedUrl;
const asset = storage.get(assetId);
// If the SVG refers to fonts, they must be inlined in order to display correctly in the img tag. return function (assetId, vm) {
// Avoid parsing the SVG when possible, since it's expensive.
if (asset.assetType === storage.AssetType.ImageVector) { if (cachedAssetId === assetId) {
// If the asset ID has not changed, no need to re-parse return cachedUrl;
const svgRenderer = new SVGRenderer();
const svgString = vm.runtime.storage.get(assetId).decodeText();
if (svgString.match(HAS_FONT_REGEXP)) {
svgRenderer.loadString(svgString);
const svgText = svgRenderer.toString(true /* shouldInjectFonts */);
return `data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`;
} }
return vm.runtime.storage.get(assetId).encodeDataURI();
} cachedAssetId = assetId;
return vm.runtime.storage.get(assetId).encodeDataURI();
}; const storage = vm.runtime.storage;
const asset = storage.get(assetId);
// If the SVG refers to fonts, they must be inlined in order to display correctly in the img tag.
// Avoid parsing the SVG when possible, since it's expensive.
if (asset.assetType === storage.AssetType.ImageVector) {
const svgString = vm.runtime.storage.get(assetId).decodeText();
if (svgString.match(HAS_FONT_REGEXP)) {
const svgRenderer = new SVGRenderer();
svgRenderer.loadString(svgString);
const svgText = svgRenderer.toString(true /* shouldInjectFonts */);
cachedUrl = `data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`;
}
cachedUrl = vm.runtime.storage.get(assetId).encodeDataURI();
}
cachedUrl = vm.runtime.storage.get(assetId).encodeDataURI();
return cachedUrl;
};
}());
export { export {
getCostumeUrl as default, getCostumeUrl as default,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment