From db54b64fdc780080b9d381bf7f81b9081b10ec68 Mon Sep 17 00:00:00 2001
From: DD <liudi08@gmail.com>
Date: Thu, 22 Mar 2018 10:52:26 -0400
Subject: [PATCH] Implement Pauls suggestion and remove promises

---
 src/containers/backdrop-library.jsx |  7 +-----
 src/containers/costume-library.jsx  |  5 +---
 src/containers/costume-tab.jsx      | 38 +++++++----------------------
 3 files changed, 11 insertions(+), 39 deletions(-)

diff --git a/src/containers/backdrop-library.jsx b/src/containers/backdrop-library.jsx
index 68f373ece..8f796225e 100644
--- a/src/containers/backdrop-library.jsx
+++ b/src/containers/backdrop-library.jsx
@@ -23,11 +23,7 @@ class BackdropLibrary extends React.Component {
             bitmapResolution: item.info.length > 2 ? item.info[2] : 1,
             skinId: null
         };
-        this.props.vm.addBackdrop(item.md5, vmBackdrop).then(() => {
-            if (this.props.onNewBackdrop) {
-                this.props.onNewBackdrop();
-            }
-        });
+        this.props.vm.addBackdrop(item.md5, vmBackdrop);
         analytics.event({
             category: 'library',
             action: 'Select Backdrop',
@@ -47,7 +43,6 @@ class BackdropLibrary extends React.Component {
 }
 
 BackdropLibrary.propTypes = {
-    onNewBackdrop: PropTypes.func.isRequired,
     onRequestClose: PropTypes.func,
     vm: PropTypes.instanceOf(VM).isRequired
 };
diff --git a/src/containers/costume-library.jsx b/src/containers/costume-library.jsx
index 2e631bd95..afdd56a5d 100644
--- a/src/containers/costume-library.jsx
+++ b/src/containers/costume-library.jsx
@@ -23,9 +23,7 @@ class CostumeLibrary extends React.PureComponent {
             bitmapResolution: item.info.length > 2 ? item.info[2] : 1,
             skinId: null
         };
-        this.props.vm.addCostume(item.md5, vmCostume).then(() => {
-            this.props.onNewCostume();
-        });
+        this.props.vm.addCostume(item.md5, vmCostume);
         analytics.event({
             category: 'library',
             action: 'Select Costume',
@@ -45,7 +43,6 @@ class CostumeLibrary extends React.PureComponent {
 }
 
 CostumeLibrary.propTypes = {
-    onNewCostume: PropTypes.func.isRequired,
     onRequestClose: PropTypes.func,
     vm: PropTypes.instanceOf(VM).isRequired
 };
diff --git a/src/containers/costume-tab.jsx b/src/containers/costume-tab.jsx
index 3ce07dd02..f0712949f 100644
--- a/src/containers/costume-tab.jsx
+++ b/src/containers/costume-tab.jsx
@@ -67,7 +67,6 @@ class CostumeTab extends React.Component {
             'handleSelectCostume',
             'handleDeleteCostume',
             'handleDuplicateCostume',
-            'handleNewCostume',
             'handleNewBlankCostume',
             'handleSurpriseCostume',
             'handleSurpriseBackdrop'
@@ -97,24 +96,20 @@ class CostumeTab extends React.Component {
         }
 
         if (this.props.editingTarget === editingTarget) {
-            // Switch to a newly added costume if there is one
+            // If costumes have been added or removed, change costumes to the editing target's
+            // current costume.
             const oldTarget = this.props.sprites[editingTarget] ?
                 this.props.sprites[editingTarget] : this.props.stage;
-            // @todo: Check that the costume is actually new by making sure it doesn't
-            // exist in the old costume list. This is blocked by
+            // @todo: Find and switch to the index of the costume that is new. This is blocked by
             // https://github.com/LLK/scratch-vm/issues/967
-            if (oldTarget.costumeCount < target.costumeCount) {
+            // Right now, you can land on the wrong costume if a costume changing script is running.
+            if (oldTarget.costumeCount !== target.costumeCount) {
                 this.setState({selectedCostumeIndex: target.currentCostume});
             }
         } else {
             // If switching editing targets, update the costume index
             this.setState({selectedCostumeIndex: target.currentCostume});
         }
-
-        // In case of deleted costumes
-        if (this.state.selectedCostumeIndex > target.costumes.length - 1) {
-            this.setState({selectedCostumeIndex: target.costumes.length - 1});
-        }
     }
     handleSelectCostume (costumeIndex) {
         this.props.vm.editingTarget.setCostume(costumeIndex);
@@ -124,14 +119,7 @@ class CostumeTab extends React.Component {
         this.props.vm.deleteCostume(costumeIndex);
     }
     handleDuplicateCostume (costumeIndex) {
-        this.props.vm.duplicateCostume(costumeIndex).then(() => {
-            this.setState({selectedCostumeIndex: costumeIndex + 1});
-        });
-    }
-    handleNewCostume () {
-        if (!this.props.vm.editingTarget) return;
-        const costumes = this.props.vm.editingTarget.getCostumes() || [];
-        this.setState({selectedCostumeIndex: Math.max(costumes.length - 1, 0)});
+        this.props.vm.duplicateCostume(costumeIndex);
     }
     handleNewBlankCostume () {
         const emptyItem = costumeLibraryContent.find(item => (
@@ -146,9 +134,7 @@ class CostumeTab extends React.Component {
             skinId: null
         };
 
-        this.props.vm.addCostume(emptyItem.md5, vmCostume).then(() => {
-            this.handleNewCostume();
-        });
+        this.props.vm.addCostume(emptyItem.md5, vmCostume);
     }
     handleSurpriseCostume () {
         const item = costumeLibraryContent[Math.floor(Math.random() * costumeLibraryContent.length)];
@@ -159,9 +145,7 @@ class CostumeTab extends React.Component {
             bitmapResolution: item.info.length > 2 ? item.info[2] : 1,
             skinId: null
         };
-        this.props.vm.addCostume(item.md5, vmCostume).then(() => {
-            this.handleNewCostume();
-        });
+        this.props.vm.addCostume(item.md5, vmCostume);
     }
     handleSurpriseBackdrop () {
         const item = backdropLibraryContent[Math.floor(Math.random() * backdropLibraryContent.length)];
@@ -172,9 +156,7 @@ class CostumeTab extends React.Component {
             bitmapResolution: item.info.length > 2 ? item.info[2] : 1,
             skinId: null
         };
-        this.props.vm.addCostume(item.md5, vmCostume).then(() => {
-            this.handleNewCostume();
-        });
+        this.props.vm.addCostume(item.md5, vmCostume);
     }
     render () {
         // For paint wrapper
@@ -249,14 +231,12 @@ class CostumeTab extends React.Component {
                 {costumeLibraryVisible ? (
                     <CostumeLibrary
                         vm={vm}
-                        onNewCostume={this.handleNewCostume}
                         onRequestClose={onRequestCloseCostumeLibrary}
                     />
                 ) : null}
                 {backdropLibraryVisible ? (
                     <BackdropLibrary
                         vm={vm}
-                        onNewBackdrop={this.handleNewCostume}
                         onRequestClose={onRequestCloseBackdropLibrary}
                     />
                 ) : null}
-- 
GitLab