File size: 11,184 Bytes
f2bee8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import PropTypes from 'prop-types';
import React from 'react';
import bindAll from 'lodash.bindall';
import { defineMessages, intlShape, injectIntl } from 'react-intl';
import VM from 'scratch-vm';

import AssetPanel from '../components/asset-panel/asset-panel.jsx';
import placeholderIcon from '../components/asset-panel/icon--files-placeholder.svg';

// import soundIconRtl from '../components/asset-panel/icon--sound-rtl.svg';
// import addSoundFromLibraryIcon from '../components/asset-panel/icon--add-sound-lib.svg';
// import addSoundFromRecordingIcon from '../components/asset-panel/icon--add-sound-record.svg';
// import fileUploadIcon from '../components/action-menu/icon--file-upload.svg';
// import surpriseIcon from '../components/action-menu/icon--surprise.svg';
// import searchIcon from '../components/action-menu/icon--search.svg';

import nordTheMan from '../components/asset-panel/nord.png';

// import RecordModal from './record-modal.jsx';
// import SoundEditor from './sound-editor.jsx';
// import SoundLibrary from './sound-library.jsx';

// import { getSoundLibrary } from '../lib/libraries/tw-async-libraries';
import { handleFileUpload, externalFileUpload } from '../lib/file-uploader.js';
import errorBoundaryHOC from '../lib/error-boundary-hoc.jsx';
// import DragConstants from '../lib/drag-constants';
import downloadBlob from '../lib/download-blob';

import { connect } from 'react-redux';

// import {
//     closeSoundLibrary,
//     openSoundLibrary,
//     openSoundRecorder
// } from '../reducers/modals';

// import {
//     activateTab,
//     COSTUMES_TAB_INDEX
// } from '../reducers/editor-tab';

import { setRestore } from '../reducers/restore-deletion';
import { showStandardAlert, closeAlertWithId } from '../reducers/alerts';

class FilesTab extends React.Component {
    constructor(props) {
        super(props);
        bindAll(this, [
            'handleSelectFile',
            'handleDeleteFile',
            'handleDuplicateFile',
            'handleDownloadFile',
            'handleNewFile',
            // 'handleSurpriseSound',
            'handleFileUploadClick',
            'handleExternalFileUpload',
            // 'handleDrop',
            'setFileInput'
        ]);
        this.state = { selectedFileIndex: 0 };
    }

    componentWillReceiveProps(nextProps) {
        // TODO: handle this
        // const {
        //     editingTarget,
        //     sprites,
        //     stage
        // } = nextProps;

        // const target = editingTarget && sprites[editingTarget] ? sprites[editingTarget] : stage;
        // if (!target || !target.sounds) {
        //     return;
        // }

        // // If switching editing targets, reset the sound index
        // if (this.props.editingTarget !== editingTarget) {
        //     this.setState({ selectedSoundIndex: 0 });
        // } else if (this.state.selectedSoundIndex > target.sounds.length - 1) {
        //     this.setState({ selectedSoundIndex: Math.max(target.sounds.length - 1, 0) });
        // }
    }

    handleSelectFile(fileIndex) {
        this.setState({ selectedFileIndex: fileIndex });
    }

    handleDeleteFile(fileIndex) {
        // TODO: deleteFile isnt a function
        const restoreFun = this.props.vm.deleteFile(fileIndex);
        if (fileIndex >= this.state.selectedFileIndex) {
            this.setState({ selectedFileIndex: Math.max(0, fileIndex - 1) });
        }
        this.props.dispatchUpdateRestore({ restoreFun, deletedItem: 'File' });
    }

    handleDownloadFile(fileIndex) {
        // TODO: vm.files doesnt exist
        const item = this.props.vm.files[fileIndex];
        const blob = new Blob([item.asset.data], { type: item.asset.assetType.contentType });
        downloadBlob(`${item.name}.${item.asset.dataFormat}`, blob);
    }

    handleDuplicateFile(fileIndex) {
        // TODO: duplicateFile isnt a function
        this.props.vm.duplicateFile(fileIndex).then(() => {
            this.setState({ selectedFileIndex: fileIndex + 1 });
        });
    }

    handleNewFile() {
        if (!this.props.vm.editingTarget) {
            return null;
        }
        // TODO: vm.files doesnt exist
        const files = this.props.vm.files ? this.props.vm.files : [];
        this.setState({ selectedFileIndex: Math.max(files.length - 1, 0) });
    }

    handleFileUploadClick() {
        this.fileInput.click();
    }

    handleExternalFileUpload(e) {
        const storage = this.props.vm.runtime.storage;
        const targetId = this.props.vm.editingTarget.id;
        this.props.onShowImporting();
        handleFileUpload(e.target, (buffer, fileType, fileName, fileIndex, fileCount) => {
            externalFileUpload(buffer, fileType, storage, newFile => {
                newFile.name = fileName;
                // TODO: addFile isnt a function
                this.props.vm.addFile(newFile, targetId).then(() => {
                    this.handleNewFile();
                    if (fileIndex === fileCount - 1) {
                        this.props.onCloseImporting();
                    }
                });
            }, this.props.onCloseImporting);
        }, this.props.onCloseImporting);
    }

    // handleDrop(dropInfo) {
    //     if (dropInfo.dragType === DragConstants.SOUND) {
    //         const sprite = this.props.vm.editingTarget.sprite;
    //         const activeSound = sprite.sounds[this.state.selectedSoundIndex];

    //         this.props.vm.reorderSound(this.props.vm.editingTarget.id,
    //             dropInfo.index, dropInfo.newIndex);

    //         this.setState({ selectedSoundIndex: sprite.sounds.indexOf(activeSound) });
    //     } else if (dropInfo.dragType === DragConstants.BACKPACK_COSTUME) {
    //         this.props.onActivateCostumesTab();
    //         this.props.vm.addCostume(dropInfo.payload.body, {
    //             name: dropInfo.payload.name
    //         });
    //     } else if (dropInfo.dragType === DragConstants.BACKPACK_SOUND) {
    //         this.props.vm.addSound({
    //             md5: dropInfo.payload.body,
    //             name: dropInfo.payload.name
    //         }).then(this.handleNewSound);
    //     }
    // }

    setFileInput(input) {
        this.fileInput = input;
    }

    render() {
        const {
            dispatchUpdateRestore, // eslint-disable-line no-unused-vars
            intl,
            isRtl,
            vm,
            // onNewSoundFromLibraryClick,
            // onNewSoundFromRecordingClick
        } = this.props;

        if (!vm.editingTarget) {
            return null;
        }

        const files = vm.files ? vm.files.map(file => (
            {
                url: isRtl ? fileIconRtl : fileIcon,
                name: file.name,
                details: file.size,
                dragPayload: file
            }
        )) : [];

        const messages = defineMessages({
            fileUploadExternal: {
                defaultMessage: 'Upload File',
                description: 'Button to upload file in the editor tab',
                id: 'pm.gui.filesTab.fileUploadExternal'
            },
            fileNew: {
                defaultMessage: 'New File',
                description: 'Button to create a new file in the editor tab',
                id: 'pm.gui.filesTab.fileNew'
            }
        });

        return (
            <AssetPanel
                buttons={[{
                    title: intl.formatMessage(messages.fileNew),
                    img: placeholderIcon,
                    onClick: this.handleFileUploadClick // TODO: should make a new TXT file instead
                }, {
                    title: intl.formatMessage(messages.fileUploadExternal),
                    img: placeholderIcon,
                    onClick: this.handleFileUploadClick,
                    fileAccept: '.txt, .json',
                    fileChange: this.handleExternalFileUpload,
                    fileInput: this.setFileInput,
                    fileMultiple: true
                }, {
                    title: intl.formatMessage(messages.fileNew),
                    img: placeholderIcon,
                    onClick: this.handleFileUploadClick // TODO: should make a new TXT file instead
                }]}
                // dragType={DragConstants.SOUND}
                isRtl={isRtl}
                items={files}
                selectedItemIndex={this.state.selectedFileIndex}
                onDeleteClick={this.handleDeleteFile}
                // onDrop={this.handleDrop}
                onDuplicateClick={this.handleDuplicateFile}
                onExportClick={this.handleDownloadFile}
                onItemClick={this.handleSelectFile}
            >
                <p>erm, you dont see anything here</p>
                <img
                    width="40"
                    height="40"
                    src={nordTheMan}
                    alt="Nord"
                ></img>
            </AssetPanel>
        );
    }
}

FilesTab.propTypes = {
    dispatchUpdateRestore: PropTypes.func,
    editingTarget: PropTypes.string,
    intl: intlShape,
    isRtl: PropTypes.bool,
    // onActivateCostumesTab: PropTypes.func.isRequired,
    onCloseImporting: PropTypes.func.isRequired,
    // onNewSoundFromLibraryClick: PropTypes.func.isRequired,
    // onNewSoundFromRecordingClick: PropTypes.func.isRequired,
    // onRequestCloseSoundLibrary: PropTypes.func.isRequired,
    onShowImporting: PropTypes.func.isRequired,
    // soundLibraryVisible: PropTypes.bool,
    // soundRecorderVisible: PropTypes.bool,
    // sprites: PropTypes.shape({
    //     id: PropTypes.shape({
    //         sounds: PropTypes.arrayOf(PropTypes.shape({
    //             name: PropTypes.string.isRequired
    //         }))
    //     })
    // }),
    // stage: PropTypes.shape({
    //     sounds: PropTypes.arrayOf(PropTypes.shape({
    //         name: PropTypes.string.isRequired
    //     }))
    // }),
    vm: PropTypes.instanceOf(VM).isRequired
};

const mapStateToProps = state => ({
    editingTarget: state.scratchGui.targets.editingTarget,
    isRtl: state.locales.isRtl,
    // sprites: state.scratchGui.targets.sprites,
    // stage: state.scratchGui.targets.stage,
    // soundLibraryVisible: state.scratchGui.modals.soundLibrary,
    // soundRecorderVisible: state.scratchGui.modals.soundRecorder
});

const mapDispatchToProps = dispatch => ({
    // onActivateCostumesTab: () => dispatch(activateTab(COSTUMES_TAB_INDEX)),
    // onNewSoundFromLibraryClick: e => {
    //     e.preventDefault();
    //     dispatch(openSoundLibrary());
    // },
    // onNewSoundFromRecordingClick: () => {
    //     dispatch(openSoundRecorder());
    // },
    // onRequestCloseSoundLibrary: () => {
    //     dispatch(closeSoundLibrary());
    // },
    dispatchUpdateRestore: restoreState => {
        dispatch(setRestore(restoreState));
    },
    onCloseImporting: () => dispatch(closeAlertWithId('importingAsset')),
    onShowImporting: () => dispatch(showStandardAlert('importingAsset'))
});

export default errorBoundaryHOC('Files Tab')(
    injectIntl(connect(
        mapStateToProps,
        mapDispatchToProps
    )(FilesTab))
);