Add mutation events.

This commit is contained in:
Neil Fraser
2016-02-10 21:40:51 -08:00
parent cf257ea5e9
commit d310b96f84
2 changed files with 20 additions and 3 deletions

View File

@@ -95,7 +95,7 @@ Blockly.Events.fireNow_ = function() {
for (var i = 0, detail; detail = queue[i]; i++) {
console.log(detail);
var workspace = Blockly.Workspace.getById(detail.workspaceId);
if (workspace.rendered) {
if (workspace && workspace.rendered) {
// Create a custom event in a browser-compatible way.
if (typeof CustomEvent == 'function') {
// W3
@@ -124,11 +124,21 @@ Blockly.Events.filter_ = function(queueIn) {
if (event1.type == Blockly.Events.MOVE &&
event2.type == Blockly.Events.MOVE &&
event1.blockId == event2.blockId) {
// Merge move events.
event1.newParentId = event2.newParentId;
event1.newInputName = event2.newInputName;
event1.newCoordinate = event2.newCoordinate;
queue.splice(j, 1);
j--;
} else if (event1.type == Blockly.Events.CHANGE &&
event2.type == Blockly.Events.CHANGE &&
event1.blockId == event2.blockId &&
event1.element == event2.element &&
event1.name == event2.name) {
// Merge change events.
event1.newValue = event2.newValue;
queue.splice(j, 1);
j--;
}
}
}

View File

@@ -283,6 +283,7 @@ Blockly.Mutator.prototype.workspaceChanged_ = function() {
// When the mutator's workspace changes, update the source block.
if (this.rootBlock_.workspace == this.workspace_) {
var oldMutation = this.block_.mutationToDom();
// Switch off rendering while the source block is rebuilt.
var savedRendered = this.block_.rendered;
this.block_.rendered = false;
@@ -292,12 +293,18 @@ Blockly.Mutator.prototype.workspaceChanged_ = function() {
this.block_.rendered = savedRendered;
// Mutation may have added some elements that need initalizing.
this.block_.initSvg();
var newMutation = this.block_.mutationToDom();
if (Blockly.Xml.domToText(oldMutation) !=
Blockly.Xml.domToText(newMutation)) {
Blockly.Events.fire(new Blockly.Events.Change(
this.block_, 'mutation', null, oldMutation, newMutation));
goog.Timer.callOnce(
this.block_.bumpNeighbours_, Blockly.BUMP_DELAY, this.block_);
}
if (this.block_.rendered) {
this.block_.render();
}
this.resizeBubble_();
goog.Timer.callOnce(
this.block_.bumpNeighbours_, Blockly.BUMP_DELAY, this.block_);
}
};