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

Bitmap upload -- mostly working, code needs cleanup.

parent f12718c2
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ import PropTypes from 'prop-types'; ...@@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
import React from 'react'; import React from 'react';
import bindAll from 'lodash.bindall'; import bindAll from 'lodash.bindall';
import {defineMessages, intlShape, injectIntl} from 'react-intl'; import {defineMessages, intlShape, injectIntl} from 'react-intl';
import {importBitmap} from 'scratch-svg-renderer';
import VM from 'scratch-vm'; import VM from 'scratch-vm';
import AssetPanel from '../components/asset-panel/asset-panel.jsx'; import AssetPanel from '../components/asset-panel/asset-panel.jsx';
...@@ -9,6 +10,7 @@ import PaintEditorWrapper from './paint-editor-wrapper.jsx'; ...@@ -9,6 +10,7 @@ import PaintEditorWrapper from './paint-editor-wrapper.jsx';
import CostumeLibrary from './costume-library.jsx'; import CostumeLibrary from './costume-library.jsx';
import BackdropLibrary from './backdrop-library.jsx'; import BackdropLibrary from './backdrop-library.jsx';
import {connect} from 'react-redux'; import {connect} from 'react-redux';
import log from '../lib/log.js';
import { import {
closeCostumeLibrary, closeCostumeLibrary,
...@@ -170,14 +172,15 @@ class CostumeTab extends React.Component { ...@@ -170,14 +172,15 @@ class CostumeTab extends React.Component {
const thisFileInput = e.target; const thisFileInput = e.target;
let thisFile = null; let thisFile = null;
const reader = new FileReader(); const reader = new FileReader();
const thisThing = this;
reader.onload = () => { reader.onload = () => {
// Reset the file input value now that we have everything we need // Reset the file input value now that we have everything we need
// so that the user can upload the same image multiple times // so that the user can upload the same image multiple times
// if they choose // if they choose
thisFileInput.value = null; thisFileInput.value = null;
// Cache the image in storage
const costumeBuffer = reader.result;
const storage = this.props.vm.runtime.storage; const storage = thisThing.props.vm.runtime.storage;
const fileType = thisFile.type; // check what the browser thinks this is const fileType = thisFile.type; // check what the browser thinks this is
// Only handling png and svg right now // Only handling png and svg right now
let costumeFormat = null; let costumeFormat = null;
...@@ -193,19 +196,31 @@ class CostumeTab extends React.Component { ...@@ -193,19 +196,31 @@ class CostumeTab extends React.Component {
assetType = storage.AssetType.ImageBitmap; assetType = storage.AssetType.ImageBitmap;
} }
const md5 = storage.builtinHelper.cache( const addCostumeFromBuffer = function (error, costumeBuffer) {
assetType, costumeFormat, new Uint8Array(costumeBuffer)); if (error) {
log.warn(`An error occurred while trying to extract image data: ${error}`);
return;
}
const md5Ext = `${md5}.${costumeFormat}`; const md5 = storage.builtinHelper.cache(
assetType, costumeFormat, costumeBuffer);
const vmCostume = { const md5Ext = `${md5}.${costumeFormat}`;
name: 'costume1',
dataFormat: costumeFormat,
md5: `${md5Ext}`
};
this.props.vm.addCostume(md5Ext, vmCostume); const vmCostume = {
name: 'costume1',
dataFormat: costumeFormat,
md5: `${md5Ext}`
};
thisThing.props.vm.addCostume(md5Ext, vmCostume);
};
if (costumeFormat === storage.DataFormat.SVG) {
addCostumeFromBuffer(null, reader.result);
} else {
importBitmap(reader.result, addCostumeFromBuffer);
}
}; };
if (thisFileInput.files) { if (thisFileInput.files) {
thisFile = thisFileInput.files[0]; thisFile = thisFileInput.files[0];
...@@ -272,7 +287,7 @@ class CostumeTab extends React.Component { ...@@ -272,7 +287,7 @@ class CostumeTab extends React.Component {
title: intl.formatMessage(addFileMessage), title: intl.formatMessage(addFileMessage),
img: fileUploadIcon, img: fileUploadIcon,
onClick: this.handleFileUploadClick, onClick: this.handleFileUploadClick,
fileAccept: '.svg,', // .png, .jpg, .jpeg coming soon fileAccept: '.svg, .png, .jpg, .jpeg', // coming soon
fileChange: this.handleCostumeUpload, fileChange: this.handleCostumeUpload,
fileInput: this.setFileInput fileInput: this.setFileInput
}, },
......
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