From 77cc01a38ad322e40b0adbce0a5b05a0a015fe76 Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Thu, 25 May 2017 11:44:49 -0400 Subject: [PATCH] Add variable creation and variable serialization --- src/engine/blocks.js | 6 +----- src/engine/variable.js | 4 ++++ src/virtual-machine.js | 24 +++++++++++++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/engine/blocks.js b/src/engine/blocks.js index ebe113f89..13baf953b 100644 --- a/src/engine/blocks.js +++ b/src/engine/blocks.js @@ -427,11 +427,7 @@ class Blocks { * @return {string} String of XML representing this object's blocks. */ toXML () { - let xmlString = ''; - for (let i = 0; i < this._scripts.length; i++) { - xmlString += this.blockToXML(this._scripts[i]); - } - return `${xmlString}`; + return this._scripts.map(script => this.blockToXML(script)).join(); } /** diff --git a/src/engine/variable.js b/src/engine/variable.js index 2be2c0f89..69cb915c8 100644 --- a/src/engine/variable.js +++ b/src/engine/variable.js @@ -15,6 +15,10 @@ class Variable { this.value = value; this.isCloud = isCloud; } + + toXML () { + return `${this.name}`; + } } module.exports = Variable; diff --git a/src/virtual-machine.js b/src/virtual-machine.js index fef841a6a..19a4a8dd4 100644 --- a/src/virtual-machine.js +++ b/src/virtual-machine.js @@ -491,9 +491,18 @@ class VirtualMachine extends EventEmitter { * of the current editing target's blocks. */ emitWorkspaceUpdate () { - this.emit('workspaceUpdate', { - xml: this.editingTarget.blocks.toXML() - }); + // @todo Include variables scoped to editing target also. + const variableMap = this.runtime.getTargetForStage().variables; + const variables = Object.keys(variableMap).map(k => variableMap[k]); + + const xmlString = ` + + ${variables.map(v => v.toXML()).join()} + + ${this.editingTarget.blocks.toXML()} + `; + + this.emit('workspaceUpdate', {xml: xmlString}); } /** @@ -538,6 +547,15 @@ class VirtualMachine extends EventEmitter { postSpriteInfo (data) { this.editingTarget.postSpriteInfo(data); } + + /** + * Create a variable by name. + * @todo this only creates global variables by putting them on the stage + * @param {string} name The name of the variable + */ + createVariable (name) { + this.runtime.getTargetForStage().lookupOrCreateVariable(name); + } } module.exports = VirtualMachine;