Merge pull request #1391 from kchadha/target-ordering

Target ordering
This commit is contained in:
kchadha authored and GitHub committed 2018-07-23 17:16:56 -04:00
commit dc612fb4a1
3 files changed
+37

No files matched your search

+22
View File
@@ -568,6 +568,12 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
}
}
}
if (object.hasOwnProperty('indexInLibrary')) {
// Temporarily store the 'indexInLibrary' property from the sb2 file
// so that we can correctly order sprites in the target pane.
// This will be deleted after we are done parsing and ordering the targets list.
target.targetPaneOrder = object.indexInLibrary;
}
target.isStage = topLevel;
@@ -652,6 +658,21 @@ const parseScratchObject = function (object, runtime, extensions, topLevel, zip)
);
};
const reorderParsedTargets = function (targets) {
// Reorder parsed targets based on the temporary targetPaneOrder property
// and then delete it.
targets.sort((a, b) => a.targetPaneOrder - b.targetPaneOrder);
// Delete the temporary target pane ordering since we shouldn't need it anymore.
targets.forEach(t => {
delete t.targetPaneOrder;
});
return targets;
};
/**
* Top-level handler. Parse provided JSON,
* and process the top-level object (the stage object).
@@ -667,6 +688,7 @@ const sb2import = function (json, runtime, optForceSprite, zip) {
extensionURLs: new Map()
};
return parseScratchObject(json, runtime, extensions, !optForceSprite, zip)
.then(reorderParsedTargets)
.then(targets => ({
targets,
extensions
BIN
View File
Binary file not shown.
+15
View File
@@ -87,3 +87,18 @@ test('whenclicked blocks imported separately', t => {
t.end();
});
});
test('Ordering', t => {
// This SB2 has 3 sprites that have been reordered in scratch 2
// so the order in the file is not the order specified by the indexInLibrary property.
const uri = path.resolve(__dirname, '../fixtures/ordering.sb2');
const json = extractProjectJson(uri);
const rt = new Runtime();
sb2.deserialize(json, rt).then(({targets}) => {
// Would fail with any other ordering.
t.equal(targets[1].sprite.name, 'First');
t.equal(targets[2].sprite.name, 'Second');
t.equal(targets[3].sprite.name, 'Third');
t.end();
});
});