mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-06-15 15:01:30 -04:00
Merge 3ac8451461
into bb9ac40252
This commit is contained in:
commit
fb7d1e8646
4 changed files with 49 additions and 4 deletions
|
@ -20,9 +20,10 @@ class Target extends EventEmitter {
|
|||
/**
|
||||
* @param {Runtime} runtime Reference to the runtime.
|
||||
* @param {?Blocks} blocks Blocks instance for the blocks owned by this target.
|
||||
* @param {?Object.<string, *>} comments Array of comments owned by this target.
|
||||
* @constructor
|
||||
*/
|
||||
constructor (runtime, blocks) {
|
||||
constructor (runtime, blocks, comments) {
|
||||
super();
|
||||
|
||||
if (!blocks) {
|
||||
|
@ -55,7 +56,7 @@ class Target extends EventEmitter {
|
|||
* Key is the comment id.
|
||||
* @type {Object.<string,*>}
|
||||
*/
|
||||
this.comments = {};
|
||||
this.comments = comments || {};
|
||||
/**
|
||||
* Dictionary of custom state for this target.
|
||||
* This can be used to store target-specific custom state for blocks which need it.
|
||||
|
|
|
@ -15,7 +15,7 @@ class RenderedTarget extends Target {
|
|||
* @constructor
|
||||
*/
|
||||
constructor (sprite, runtime) {
|
||||
super(runtime, sprite.blocks);
|
||||
super(runtime, sprite.blocks, sprite.comments);
|
||||
|
||||
/**
|
||||
* Reference to the sprite that this is a render of.
|
||||
|
|
|
@ -54,6 +54,12 @@ class Sprite {
|
|||
if (this.runtime && this.runtime.audioEngine) {
|
||||
this.soundBank = this.runtime.audioEngine.createBank();
|
||||
}
|
||||
/**
|
||||
* Dictionary of comments for this target.
|
||||
* Key is the comment id.
|
||||
* @type {Object.<string.*>}
|
||||
*/
|
||||
this.comments = {};
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,7 +151,12 @@ class Sprite {
|
|||
newSprite.blocks.createBlock(block);
|
||||
});
|
||||
|
||||
|
||||
// Logic to handle new sprite retaining the comments from the current sprite
|
||||
Object.keys(this.comments).forEach(commentId => {
|
||||
const newComment = this.comments[commentId];
|
||||
newSprite.comments[newComment.id] = newComment;
|
||||
});
|
||||
|
||||
const allNames = this.runtime.targets.map(t => t.sprite.name);
|
||||
newSprite.name = StringUtil.unusedName(this.name, allNames);
|
||||
|
||||
|
|
|
@ -45,6 +45,39 @@ test('blocks get new id on duplicate', t => {
|
|||
});
|
||||
});
|
||||
|
||||
test('comments are duplicated when duplicating target', t => {
|
||||
const r = new Runtime();
|
||||
const s = new Sprite(null, r);
|
||||
const rt = new RenderedTarget(s, r);
|
||||
rt.createComment('testCommentId', null, 'testcomment', 0, 0, 100, 100, false);
|
||||
return rt.duplicate().then(duplicate => {
|
||||
// ensure duplicated comment exists
|
||||
t.equal(Object.keys(duplicate.comments).length, 1);
|
||||
|
||||
// ensure comment was duplicated correctly
|
||||
const dupComment = duplicate.comments.testCommentId;
|
||||
t.equal(dupComment.id, 'testCommentId');
|
||||
t.equal(dupComment.text, 'testcomment');
|
||||
t.equal(dupComment.x, 0);
|
||||
t.equal(dupComment.y, 0);
|
||||
t.equal(dupComment.width, 100);
|
||||
t.equal(dupComment.height, 100);
|
||||
t.equal(dupComment.minimized, false);
|
||||
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('no comments are duplicated when duplicating target with no comments', t => {
|
||||
const r = new Runtime();
|
||||
const s = new Sprite(null, r);
|
||||
const rt = new RenderedTarget(s, r);
|
||||
return rt.duplicate().then(duplicate => {
|
||||
t.equal(Object.keys(duplicate.comments).length, 0);
|
||||
t.end();
|
||||
});
|
||||
});
|
||||
|
||||
test('direction', t => {
|
||||
const r = new Runtime();
|
||||
const s = new Sprite(null, r);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue