mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 15:02:52 -05:00
Merge pull request #581 from paulkaplan/feature/variable-persistence
Add variable creation and variable serialization
This commit is contained in:
commit
51928bac65
3 changed files with 26 additions and 8 deletions
|
@ -419,11 +419,7 @@ class Blocks {
|
||||||
* @return {string} String of XML representing this object's blocks.
|
* @return {string} String of XML representing this object's blocks.
|
||||||
*/
|
*/
|
||||||
toXML () {
|
toXML () {
|
||||||
let xmlString = '<xml xmlns="http://www.w3.org/1999/xhtml">';
|
return this._scripts.map(script => this.blockToXML(script)).join();
|
||||||
for (let i = 0; i < this._scripts.length; i++) {
|
|
||||||
xmlString += this.blockToXML(this._scripts[i]);
|
|
||||||
}
|
|
||||||
return `${xmlString}</xml>`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -15,6 +15,10 @@ class Variable {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.isCloud = isCloud;
|
this.isCloud = isCloud;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
toXML () {
|
||||||
|
return `<variable type="">${this.name}</variable>`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Variable;
|
module.exports = Variable;
|
||||||
|
|
|
@ -491,9 +491,18 @@ class VirtualMachine extends EventEmitter {
|
||||||
* of the current editing target's blocks.
|
* of the current editing target's blocks.
|
||||||
*/
|
*/
|
||||||
emitWorkspaceUpdate () {
|
emitWorkspaceUpdate () {
|
||||||
this.emit('workspaceUpdate', {
|
// @todo Include variables scoped to editing target also.
|
||||||
xml: this.editingTarget.blocks.toXML()
|
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) {
|
postSpriteInfo (data) {
|
||||||
this.editingTarget.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;
|
module.exports = VirtualMachine;
|
||||||
|
|
Loading…
Reference in a new issue