diff --git a/core/block_events.js b/core/block_events.js index 8638a57e0..7c80dfca7 100644 --- a/core/block_events.js +++ b/core/block_events.js @@ -177,16 +177,16 @@ Blockly.Events.Change.prototype.run = function(forward) { } break; case 'comment': - block.setCommentText(value || null); + block.setCommentText(/** @type {string} */ (value) || null); break; case 'collapsed': - block.setCollapsed(value); + block.setCollapsed(!!value); break; case 'disabled': block.setEnabled(!value); break; case 'inline': - block.setInputsInline(value); + block.setInputsInline(!!value); break; case 'mutation': var oldMutation = ''; @@ -195,7 +195,7 @@ Blockly.Events.Change.prototype.run = function(forward) { oldMutation = oldMutationDom && Blockly.Xml.domToText(oldMutationDom); } if (block.domToMutation) { - var dom = Blockly.Xml.textToDom(value || ''); + var dom = Blockly.Xml.textToDom(/** @type {string} */ (value) || ''); block.domToMutation(dom); } Blockly.Events.fire(new Blockly.Events.Change( @@ -223,7 +223,7 @@ Blockly.Events.Create = function(block) { } else { this.xml = Blockly.Xml.blockToDom(block); } - this.ids = Blockly.Events.getDescendantIds_(block); + this.ids = Blockly.Events.getDescendantIds(block); }; Blockly.utils.object.inherits(Blockly.Events.Create, Blockly.Events.BlockBase); @@ -276,7 +276,7 @@ Blockly.Events.Create.prototype.run = function(forward) { for (var i = 0, id; id = this.ids[i]; i++) { var block = workspace.getBlockById(id); if (block) { - block.dispose(false, false); + block.dispose(false); } else if (id == this.blockId) { // Only complain about root-level block. console.warn("Can't uncreate non-existent block: " + id); @@ -305,7 +305,7 @@ Blockly.Events.Delete = function(block) { } else { this.oldXml = Blockly.Xml.blockToDom(block); } - this.ids = Blockly.Events.getDescendantIds_(block); + this.ids = Blockly.Events.getDescendantIds(block); }; Blockly.utils.object.inherits(Blockly.Events.Delete, Blockly.Events.BlockBase); @@ -352,7 +352,7 @@ Blockly.Events.Delete.prototype.run = function(forward) { for (var i = 0, id; id = this.ids[i]; i++) { var block = workspace.getBlockById(id); if (block) { - block.dispose(false, false); + block.dispose(false); } else if (id == this.blockId) { // Only complain about root-level block. console.warn("Can't delete non-existent block: " + id); @@ -448,7 +448,7 @@ Blockly.Events.Move.prototype.recordNew = function() { * @private */ Blockly.Events.Move.prototype.currentLocation_ = function() { - var workspace = Blockly.Workspace.getById(this.workspaceId); + var workspace = this.getEventWorkspace_(); var block = workspace.getBlockById(this.blockId); var location = {}; var parent = block.getParent(); diff --git a/core/events.js b/core/events.js index bcc5cb100..a693cae04 100644 --- a/core/events.js +++ b/core/events.js @@ -195,6 +195,9 @@ Blockly.Events.fireNow_ = function() { var queue = Blockly.Events.filter(Blockly.Events.FIRE_QUEUE_, true); Blockly.Events.FIRE_QUEUE_.length = 0; for (var i = 0, event; event = queue[i]; i++) { + if (!event.workspaceId) { + continue; + } var workspace = Blockly.Workspace.getById(event.workspaceId); if (workspace) { workspace.fireChangeListener(event); @@ -330,9 +333,9 @@ Blockly.Events.setGroup = function(state) { * Compute a list of the IDs of the specified block and all its descendants. * @param {!Blockly.Block} block The root block. * @return {!Array.} List of block IDs. - * @private + * @package */ -Blockly.Events.getDescendantIds_ = function(block) { +Blockly.Events.getDescendantIds = function(block) { var ids = []; var descendants = block.getDescendants(false); for (var i = 0, descendant; descendant = descendants[i]; i++) { @@ -373,13 +376,13 @@ Blockly.Events.fromJson = function(json, workspace) { event = new Blockly.Events.VarRename(null, ''); break; case Blockly.Events.UI: - event = new Blockly.Events.Ui(null); + event = new Blockly.Events.Ui(null, '', '', ''); break; case Blockly.Events.COMMENT_CREATE: event = new Blockly.Events.CommentCreate(null); break; case Blockly.Events.COMMENT_CHANGE: - event = new Blockly.Events.CommentChange(null); + event = new Blockly.Events.CommentChange(null, '', ''); break; case Blockly.Events.COMMENT_MOVE: event = new Blockly.Events.CommentMove(null); @@ -405,6 +408,9 @@ Blockly.Events.fromJson = function(json, workspace) { Blockly.Events.disableOrphans = function(event) { if (event.type == Blockly.Events.MOVE || event.type == Blockly.Events.CREATE) { + if (!event.workspaceId) { + return; + } var workspace = Blockly.Workspace.getById(event.workspaceId); var block = workspace.getBlockById(event.blockId); if (block) { diff --git a/core/events_abstract.js b/core/events_abstract.js index 5f9073d30..99b8e6982 100644 --- a/core/events_abstract.js +++ b/core/events_abstract.js @@ -44,7 +44,7 @@ Blockly.Events.Abstract = function() { * perspective, and should be undone together. * @type {string} */ - this.group = Blockly.Events.group_; + this.group = Blockly.Events.getGroup(); /** * Sets whether the event should be added to the undo stack. @@ -93,12 +93,14 @@ Blockly.Events.Abstract.prototype.run = function(_forward) { /** * Get workspace the event belongs to. - * @return {Blockly.Workspace} The workspace the event belongs to. + * @return {!Blockly.Workspace} The workspace the event belongs to. * @throws {Error} if workspace is null. * @protected */ Blockly.Events.Abstract.prototype.getEventWorkspace_ = function() { - var workspace = Blockly.Workspace.getById(this.workspaceId); + if (this.workspaceId) { + var workspace = Blockly.Workspace.getById(this.workspaceId); + } if (!workspace) { throw Error('Workspace is null. Event must have been generated from real' + ' Blockly events.'); diff --git a/core/inject.js b/core/inject.js index 6cd73c6b0..74c755fa2 100644 --- a/core/inject.js +++ b/core/inject.js @@ -365,7 +365,9 @@ Blockly.createMainWorkspace_ = function(svg, options, blockDragSurface, console.log('WARNING: Moved object in bounds but there was no' + ' event group. This may break undo.'); } - Blockly.Events.setGroup(oldGroup); + if (oldGroup !== null) { + Blockly.Events.setGroup(oldGroup); + } } } } diff --git a/core/ui_events.js b/core/ui_events.js index 080ec3fc4..cc8cabcfb 100644 --- a/core/ui_events.js +++ b/core/ui_events.js @@ -44,7 +44,7 @@ goog.require('Blockly.utils.object'); Blockly.Events.Ui = function(block, element, oldValue, newValue) { Blockly.Events.Ui.superClass_.constructor.call(this); this.blockId = block ? block.id : null; - this.workspaceId = block ? block.workspace.id : null; + this.workspaceId = block ? block.workspace.id : undefined; this.element = element; this.oldValue = oldValue; this.newValue = newValue; diff --git a/core/workspace_events.js b/core/workspace_events.js index 44b3a6af3..581366574 100644 --- a/core/workspace_events.js +++ b/core/workspace_events.js @@ -51,7 +51,7 @@ Blockly.Events.FinishedLoading = function(workspace) { * perspective, and should be undone together. * @type {string} */ - this.group = Blockly.Events.group_; + this.group = Blockly.Events.getGroup(); // Workspace events do not undo or redo. this.recordUndo = false; diff --git a/core/ws_comment_events.js b/core/ws_comment_events.js index be90436ff..7e62d5451 100644 --- a/core/ws_comment_events.js +++ b/core/ws_comment_events.js @@ -62,7 +62,7 @@ Blockly.Events.CommentBase = function(comment) { * perspective, and should be undone together. * @type {string} */ - this.group = Blockly.Events.group_; + this.group = Blockly.Events.getGroup(); /** * Sets whether the event should be added to the undo stack. @@ -315,7 +315,7 @@ Blockly.Events.CommentMove = function(comment) { /** * The location after the move, in workspace coordinates. - * @type {!Blockly.utils.Coordinate} + * @type {Blockly.utils.Coordinate} */ this.newCoordinate_ = null; };