From e2eadbf603ead8d3ffb9e9f5b6989eca4f66d798 Mon Sep 17 00:00:00 2001 From: Karishma Chadha Date: Thu, 14 Jun 2018 14:11:20 -0400 Subject: [PATCH] Comment save and load. --- src/serialization/sb3.js | 44 ++++++++++++++++++++++++++++++++++ src/sprites/rendered-target.js | 1 + 2 files changed, 45 insertions(+) diff --git a/src/serialization/sb3.js b/src/serialization/sb3.js index cfd661af1..95d5fc311 100644 --- a/src/serialization/sb3.js +++ b/src/serialization/sb3.js @@ -8,6 +8,7 @@ const vmPackage = require('../../package.json'); const Blocks = require('../engine/blocks'); const Sprite = require('../sprites/sprite'); const Variable = require('../engine/variable'); +const Comment = require('../engine/comment'); const StageLayering = require('../engine/stage-layering'); const log = require('../util/log'); const uid = require('../util/uid'); @@ -207,6 +208,9 @@ const serializeBlock = function (block) { if (block.mutation) { obj.mutation = block.mutation; } + if (block.comment) { + obj.comment = block.comment; + } return obj; }; @@ -384,6 +388,27 @@ const serializeVariables = function (variables) { return obj; }; +const serializeComments = function (comments) { + const obj = Object.create(null); + for (const commentId in comments) { + if (!comments.hasOwnProperty(commentId)) continue; + const comment = comments[commentId]; + + const serializedComment = Object.create(null); + serializedComment.id = comment.id; + serializedComment.blockId = comment.blockId; + serializedComment.x = comment.x; + serializedComment.y = comment.y; + serializedComment.width = comment.width; + serializedComment.height = comment.height; + serializedComment.minimized = comment.minimized; + serializedComment.text = comment.text; + + obj[commentId] = serializedComment; + } + return obj; +}; + /** * Serialize the given target. Only serialize properties that are necessary * for saving and loading this target. @@ -399,6 +424,7 @@ const serializeTarget = function (target) { obj.lists = vars.lists; obj.broadcasts = vars.broadcasts; obj.blocks = serializeBlocks(target.blocks); + obj.comments = serializeComments(target.comments); obj.currentCostume = target.currentCostume; obj.costumes = target.costumes.map(serializeCostume); obj.sounds = target.sounds.map(serializeSound); @@ -834,6 +860,24 @@ const parseScratchObject = function (object, runtime, extensions, zip) { target.variables[newBroadcast.id] = newBroadcast; } } + if (object.hasOwnProperty('comments')) { + for (const commentId in object.comments) { + const comment = object.comments[commentId]; + const newComment = new Comment( + comment.id, + comment.text, + comment.x, + comment.y, + comment.width, + comment.height, + comment.minimized + ); + if (comment.blockId) { + newComment.blockId = comment.blockId; + } + target.comments[newComment.id] = newComment; + } + } if (object.hasOwnProperty('x')) { target.x = object.x; } diff --git a/src/sprites/rendered-target.js b/src/sprites/rendered-target.js index 37c1b5615..632374384 100644 --- a/src/sprites/rendered-target.js +++ b/src/sprites/rendered-target.js @@ -1103,6 +1103,7 @@ class RenderedTarget extends Target { costumeCount: costumes.length, visible: this.visible, rotationStyle: this.rotationStyle, + comments: this.comments, blocks: this.blocks._blocks, variables: this.variables, costumes: costumes,