mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-24 06:52:40 -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.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,6 +15,10 @@ class Variable {
|
|||
this.value = value;
|
||||
this.isCloud = isCloud;
|
||||
}
|
||||
|
||||
toXML () {
|
||||
return `<variable type="">${this.name}</variable>`;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Variable;
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue