mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-01-09 14:32:07 -05:00
Deal with comments attached to undefined blocks
Also fix an issue where you couldn't save/load projects that had multiple comments linked to a single block.
This commit is contained in:
parent
e7e300caeb
commit
9f517bd483
3 changed files with 47 additions and 18 deletions
|
@ -134,12 +134,11 @@ const parseBlockList = function (blockList, addBroadcastMsg, getVariableId, exte
|
||||||
const parsedBlockAndComments = parseBlock(block, addBroadcastMsg, getVariableId,
|
const parsedBlockAndComments = parseBlock(block, addBroadcastMsg, getVariableId,
|
||||||
extensions, comments, commentIndex);
|
extensions, comments, commentIndex);
|
||||||
|
|
||||||
if (parsedBlockAndComments) { // Could fail due to unrecognized op-codes
|
|
||||||
const parsedBlock = parsedBlockAndComments[0];
|
const parsedBlock = parsedBlockAndComments[0];
|
||||||
// Update commentIndex
|
// Update commentIndex
|
||||||
commentIndex = parsedBlockAndComments[1];
|
commentIndex = parsedBlockAndComments[1];
|
||||||
|
|
||||||
if (typeof parsedBlock === 'undefined') continue;
|
if (!parsedBlock) continue;
|
||||||
if (previousBlock) {
|
if (previousBlock) {
|
||||||
parsedBlock.parent = previousBlock.id;
|
parsedBlock.parent = previousBlock.id;
|
||||||
previousBlock.next = parsedBlock.id;
|
previousBlock.next = parsedBlock.id;
|
||||||
|
@ -147,7 +146,6 @@ const parseBlockList = function (blockList, addBroadcastMsg, getVariableId, exte
|
||||||
previousBlock = parsedBlock;
|
previousBlock = parsedBlock;
|
||||||
resultingList.push(parsedBlock);
|
resultingList.push(parsedBlock);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return [resultingList, commentIndex];
|
return [resultingList, commentIndex];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -709,9 +707,19 @@ const specMapBlock = function (block) {
|
||||||
* and second item is the updated comment index (after this block and its children are parsed)
|
* and second item is the updated comment index (after this block and its children are parsed)
|
||||||
*/
|
*/
|
||||||
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions, comments, commentIndex) {
|
const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extensions, comments, commentIndex) {
|
||||||
|
const commentsForParsedBlock = (comments && typeof commentIndex === 'number' && !isNaN(commentIndex)) ?
|
||||||
|
comments[commentIndex] : null;
|
||||||
const blockMetadata = specMapBlock(sb2block);
|
const blockMetadata = specMapBlock(sb2block);
|
||||||
if (!blockMetadata) {
|
if (!blockMetadata) {
|
||||||
return;
|
// No block opcode found, exclude this block, increment the comment id and
|
||||||
|
// send all linked comments back to zero/zero to prevent serialization issues.
|
||||||
|
if (commentsForParsedBlock) {
|
||||||
|
commentsForParsedBlock.forEach(comment => {
|
||||||
|
comment.blockId = null;
|
||||||
|
comment.x = comment.y = 0;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return [null, commentIndex + 1];
|
||||||
}
|
}
|
||||||
const oldOpcode = sb2block[0];
|
const oldOpcode = sb2block[0];
|
||||||
|
|
||||||
|
@ -734,15 +742,18 @@ const parseBlock = function (sb2block, addBroadcastMsg, getVariableId, extension
|
||||||
};
|
};
|
||||||
|
|
||||||
// Attach any comments to this block..
|
// Attach any comments to this block..
|
||||||
const commentsForParsedBlock = (comments && typeof commentIndex === 'number' && !isNaN(commentIndex)) ?
|
|
||||||
comments[commentIndex] : null;
|
|
||||||
if (commentsForParsedBlock) {
|
if (commentsForParsedBlock) {
|
||||||
// TODO currently only attaching the last comment to the block if there are multiple...
|
// Attach only the last comment to the block, make all others workspace comments
|
||||||
// not sure what to do here.. concatenate all the messages in all the comments and only
|
|
||||||
// keep one around?
|
|
||||||
activeBlock.comment = commentsForParsedBlock[commentsForParsedBlock.length - 1].id;
|
activeBlock.comment = commentsForParsedBlock[commentsForParsedBlock.length - 1].id;
|
||||||
commentsForParsedBlock.forEach(comment => {
|
commentsForParsedBlock.forEach(comment => {
|
||||||
|
if (comment.id === activeBlock.comment) {
|
||||||
comment.blockId = activeBlock.id;
|
comment.blockId = activeBlock.id;
|
||||||
|
} else {
|
||||||
|
// All other comments don't get a block ID and are sent back to zero.
|
||||||
|
// This is important, because if they have `null` x/y, serialization breaks.
|
||||||
|
comment.blockId = null;
|
||||||
|
comment.x = comment.y = 0;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
commentIndex++;
|
commentIndex++;
|
||||||
|
|
BIN
test/fixtures/unknown-opcode.sb2
vendored
BIN
test/fixtures/unknown-opcode.sb2
vendored
Binary file not shown.
|
@ -31,6 +31,24 @@ test('unknown opcode', t => {
|
||||||
t.equal(blocks.getBlock(topBlockId).opcode, 'sound_play');
|
t.equal(blocks.getBlock(topBlockId).opcode, 'sound_play');
|
||||||
t.equal(blocks.getBlock(secondBlockId).opcode, 'sound_play');
|
t.equal(blocks.getBlock(secondBlockId).opcode, 'sound_play');
|
||||||
t.equal(thirdBlockId, null);
|
t.equal(thirdBlockId, null);
|
||||||
|
|
||||||
|
const target = vm.runtime.targets[0];
|
||||||
|
const topCommentId = blocks.getBlock(topBlockId).comment;
|
||||||
|
const secondCommentId = blocks.getBlock(secondBlockId).comment;
|
||||||
|
|
||||||
|
t.equal(target.comments[topCommentId].text, 'pop1 comment');
|
||||||
|
t.equal(target.comments[secondCommentId].text, 'pop2 comment');
|
||||||
|
|
||||||
|
// The comment previously attached to the undefined block should become
|
||||||
|
// a workspace comment, at 0/0, with the same text as it had.
|
||||||
|
const undefinedCommentId = Object.keys(target.comments).filter(id =>
|
||||||
|
id !== topCommentId && id !== secondCommentId)[0];
|
||||||
|
const undefinedComment = target.comments[undefinedCommentId];
|
||||||
|
t.equal(undefinedComment.blockId, null);
|
||||||
|
t.equal(undefinedComment.text, 'undefined comment');
|
||||||
|
t.equal(undefinedComment.x, 0);
|
||||||
|
t.equal(undefinedComment.y, 0);
|
||||||
|
|
||||||
t.end();
|
t.end();
|
||||||
process.nextTick(process.exit);
|
process.nextTick(process.exit);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue