Skip to content

Commit 7798aef

Browse files
committed
add backpack export item
1 parent bb59e39 commit 7798aef

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/components/backpack/backpack.jsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const Backpack = ({
5252
showMore,
5353
onToggle,
5454
onDelete,
55+
onExport,
5556
onRename,
5657
onMouseEnter,
5758
onMouseLeave,
@@ -124,6 +125,7 @@ const Backpack = ({
124125
name={intl.formatMessage(labelMap[item.type])}
125126
selected={false}
126127
onClick={noop}
128+
onExportButtonClick={item.type === 'script' ? null : onExport}
127129
onDeleteButtonClick={onDelete}
128130
// Currently, renaming sprites is not supported.
129131
onRenameButtonClick={item.type === 'sprite' ? null : onRename}
@@ -173,6 +175,7 @@ Backpack.propTypes = {
173175
intl: intlShape,
174176
loading: PropTypes.bool,
175177
onDelete: PropTypes.func,
178+
onExport: PropTypes.func,
176179
onRename: PropTypes.func,
177180
onMore: PropTypes.func,
178181
onMouseEnter: PropTypes.func,

src/containers/backpack.jsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import DropAreaHOC from '../lib/drop-area-hoc.jsx';
1919

2020
import {connect} from 'react-redux';
2121
import storage from '../lib/storage';
22+
import downloadBlob from '../lib/download-blob';
2223
import VM from 'scratch-vm';
2324

2425
const dragTypes = [DragConstants.COSTUME, DragConstants.SOUND, DragConstants.SPRITE];
@@ -39,6 +40,7 @@ class Backpack extends React.Component {
3940
'handleDrop',
4041
'handleToggle',
4142
'handleDelete',
43+
'handleExport',
4244
'handleRename',
4345
'getBackpackAssetURL',
4446
'getContents',
@@ -174,6 +176,24 @@ class Backpack extends React.Component {
174176
});
175177
});
176178
}
179+
handleExport (id) {
180+
const item = this.findItemById(id);
181+
if (!item) return;
182+
if (!item.bodyData) return;
183+
184+
const buffer = item.bodyData;
185+
const blob = new Blob([buffer], { type: item.mime });
186+
187+
let recommendedName = item.name;
188+
if (item.type === 'sprite') {
189+
recommendedName += '.pms';
190+
}
191+
if (item.type === 'script') {
192+
recommendedName += '.pmb';
193+
}
194+
195+
downloadBlob(recommendedName, blob);
196+
}
177197
findItemById (id) {
178198
return this.state.contents.find(i => i.id === id);
179199
}
@@ -270,6 +290,7 @@ class Backpack extends React.Component {
270290
loading={this.state.loading}
271291
showMore={this.state.moreToLoad}
272292
onDelete={this.handleDelete}
293+
onExport={this.handleExport}
273294
onRename={this.handleRename}
274295
onDrop={this.handleDrop}
275296
onMore={this.handleMore}

src/lib/make-toolbox-xml.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,21 @@ const control = function (isInitialSetup, isStage) {
687687
</block>
688688
${blockSeparator}
689689
<block type="control_all_at_once"/>
690+
<block type="control_run_as_sprite">
691+
<value name="RUN_AS_OPTION">
692+
<shadow type="control_run_as_sprite_menu"/>
693+
</value>
694+
</block>
695+
${blockSeparator}
696+
<block type="control_try_catch"/>
697+
<block type="control_throw_error">
698+
<value name="ERROR">
699+
<shadow type="text">
700+
<field name="TEXT">Hello!</field>
701+
</shadow>
702+
</value>
703+
</block>
704+
<block type="control_error"/>
690705
${blockSeparator}
691706
<block type="control_backToGreenFlag"></block>
692707
<block type="control_stop_sprite">
@@ -696,12 +711,6 @@ const control = function (isInitialSetup, isStage) {
696711
</block>
697712
<block type="control_stop"/>
698713
${blockSeparator}
699-
<block type="control_run_as_sprite">
700-
<value name="RUN_AS_OPTION">
701-
<shadow type="control_run_as_sprite_menu"/>
702-
</value>
703-
</block>
704-
${blockSeparator}
705714
${isStage ? `
706715
<block type="control_create_clone_of">
707716
<value name="CLONE_OPTION">

0 commit comments

Comments
 (0)