Add toJson to events.

This commit is contained in:
Neil Fraser
2016-03-16 17:55:44 -07:00
parent 95fbc0bb49
commit 3bf04aedc6
2 changed files with 59 additions and 1 deletions

View File

@@ -220,6 +220,22 @@ Blockly.Events.Abstract = function(block) {
this.recordUndo = Blockly.Events.recordUndo;
};
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Blockly.Events.Abstract.prototype.toJson = function() {
var json = {
'type': this.type,
'blockId': this.blockId,
'workspaceId': this.workspaceId
};
if (this.group) {
json['group'] = this.group;
}
return json;
};
/**
* Does this event record any change of state?
* @return {boolean} True if something changed.
@@ -246,6 +262,16 @@ goog.inherits(Blockly.Events.Create, Blockly.Events.Abstract);
*/
Blockly.Events.Create.prototype.type = Blockly.Events.CREATE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Blockly.Events.Create.prototype.toJson = function() {
var json = Blockly.Events.Create.superClass_.toJson.call(this);
json['xml'] = Blockly.Xml.domToText(this.xml);
return json;
};
/**
* Run a creation event.
* @param {boolean} forward True if run forward, false if run backward (undo).
@@ -332,6 +358,20 @@ goog.inherits(Blockly.Events.Change, Blockly.Events.Abstract);
*/
Blockly.Events.Change.prototype.type = Blockly.Events.CHANGE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Blockly.Events.Change.prototype.toJson = function() {
var json = Blockly.Events.Change.superClass_.toJson.call(this);
json['element'] = this.element;
if (this.name) {
json['name'] = this.name;
}
json['newValue'] = this.newValue;
return json;
};
/**
* Does this event record any change of state?
* @return {boolean} True if something changed.
@@ -415,6 +455,25 @@ goog.inherits(Blockly.Events.Move, Blockly.Events.Abstract);
*/
Blockly.Events.Move.prototype.type = Blockly.Events.MOVE;
/**
* Encode the event as JSON.
* @return {!Object} JSON representation.
*/
Blockly.Events.Move.prototype.toJson = function() {
var json = Blockly.Events.Move.superClass_.toJson.call(this);
if (this.newParentId) {
json['newParentId'] = this.newParentId;
}
if (this.newInputName) {
json['newInputName'] = this.newInputName;
}
if (this.newCoordinate) {
json['newCoordinate'] = Math.round(this.newCoordinate.x) + ',' +
Math.round(this.newCoordinate.y);
}
return json;
};
/**
* Record the block's new location. Called after the move.
*/

View File

@@ -228,7 +228,6 @@ Blockly.Workspace.prototype.undo = function(redo) {
events = Blockly.Events.filter(events, redo);
Blockly.Events.recordUndo = false;
for (var i = 0, event; event = events[i]; i++) {
console.log(event);
event.run(redo);
}
Blockly.Events.recordUndo = true;