Merge pull request #581 from paulkaplan/feature/variable-persistence

Add variable creation and variable serialization
This commit is contained in:
Paul Kaplan 2017-05-30 09:26:37 -04:00 committed by GitHub
commit 51928bac65
3 changed files with 26 additions and 8 deletions

View file

@ -419,11 +419,7 @@ class Blocks {
* @return {string} String of XML representing this object's blocks.
*/
toXML () {
let xmlString = '<xml xmlns="http://www.w3.org/1999/xhtml">';
for (let i = 0; i < this._scripts.length; i++) {
xmlString += this.blockToXML(this._scripts[i]);
}
return `${xmlString}</xml>`;
return this._scripts.map(script => this.blockToXML(script)).join();
}
/**

View file

@ -15,6 +15,10 @@ class Variable {
this.value = value;
this.isCloud = isCloud;
}
toXML () {
return `<variable type="">${this.name}</variable>`;
}
}
module.exports = Variable;

View file

@ -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 = `<xml xmlns="http://www.w3.org/1999/xhtml">
<variables>
${variables.map(v => v.toXML()).join()}
</variables>
${this.editingTarget.blocks.toXML()}
</xml>`;
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;