Skip to content
Snippets Groups Projects
Unverified Commit 55ae011b authored by DD Liu's avatar DD Liu Committed by GitHub
Browse files

fixRepeatDecode (#3174)

parent 75a84747
No related branches found
No related tags found
No related merge requests found
...@@ -32,7 +32,17 @@ class SpriteSelectorItem extends React.Component { ...@@ -32,7 +32,17 @@ class SpriteSelectorItem extends React.Component {
]); ]);
this.svgRenderer = new SVGRenderer(); this.svgRenderer = new SVGRenderer();
// Asset ID of the SVG currently in SVGRenderer // Asset ID of the SVG currently in SVGRenderer
this.svgRendererAssetId = null; this.decodedAssetId = null;
}
shouldComponentUpdate (nextProps) {
// Ignore dragPayload due to https://github.com/LLK/scratch-gui/issues/3172.
// This function should be removed once the issue is fixed.
for (const property in nextProps) {
if (property !== 'dragPayload' && this.props[property] !== nextProps[property]) {
return true;
}
}
return false;
} }
getCostumeUrl () { getCostumeUrl () {
if (this.props.costumeURL) return this.props.costumeURL; if (this.props.costumeURL) return this.props.costumeURL;
...@@ -44,18 +54,20 @@ class SpriteSelectorItem extends React.Component { ...@@ -44,18 +54,20 @@ class SpriteSelectorItem extends React.Component {
// Avoid parsing the SVG when possible, since it's expensive. // Avoid parsing the SVG when possible, since it's expensive.
if (asset.assetType === storage.AssetType.ImageVector) { if (asset.assetType === storage.AssetType.ImageVector) {
// If the asset ID has not changed, no need to re-parse // If the asset ID has not changed, no need to re-parse
if (this.svgRendererAssetId === this.props.assetId) { if (this.decodedAssetId === this.props.assetId) {
// @todo consider caching more than one URL.
return this.cachedUrl; return this.cachedUrl;
} }
this.decodedAssetId = this.props.assetId;
const svgString = this.props.vm.runtime.storage.get(this.props.assetId).decodeText(); const svgString = this.props.vm.runtime.storage.get(this.props.assetId).decodeText();
if (svgString.match(HAS_FONT_REGEXP)) { if (svgString.match(HAS_FONT_REGEXP)) {
this.svgRendererAssetId = this.props.assetId;
this.svgRenderer.loadString(svgString); this.svgRenderer.loadString(svgString);
const svgText = this.svgRenderer.toString(true /* shouldInjectFonts */); const svgText = this.svgRenderer.toString(true /* shouldInjectFonts */);
this.cachedUrl = `data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`; this.cachedUrl = `data:image/svg+xml;utf8,${encodeURIComponent(svgText)}`;
return this.cachedUrl; } else {
this.cachedUrl = this.props.vm.runtime.storage.get(this.props.assetId).encodeDataURI();
} }
return this.cachedUrl;
} }
return this.props.vm.runtime.storage.get(this.props.assetId).encodeDataURI(); return this.props.vm.runtime.storage.get(this.props.assetId).encodeDataURI();
} }
......
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