From 02aada1e3dad712c77ece3f90c565e6337f2fb3a Mon Sep 17 00:00:00 2001 From: Rachel Fenichel Date: Thu, 3 May 2018 17:36:08 -0700 Subject: [PATCH] Make ui events work when block is null; test --- core/ui_events.js | 13 ++++++----- tests/jsunit/event_test.js | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/core/ui_events.js b/core/ui_events.js index caa6debba..bd09f7575 100644 --- a/core/ui_events.js +++ b/core/ui_events.js @@ -34,6 +34,10 @@ goog.require('goog.math.Coordinate'); /** * Class for a UI event. + * UI events are events that don't need to be sent over the wire for multi-user + * editing to work (e.g. scrolling the workspace, zooming, opening toolbox + * categories). + * UI events do not undo or redo. * @param {Blockly.Block} block The affected block. * @param {string} element One of 'selected', 'comment', 'mutator', etc. * @param {*} oldValue Previous value of element. @@ -42,16 +46,13 @@ goog.require('goog.math.Coordinate'); * @constructor */ Blockly.Events.Ui = function(block, element, oldValue, newValue) { - if (!block) { - return; // Blank event to be populated by fromJson. - } - Blockly.Events.Ui.superClass_.constructor.call(this); - this.blockId = block.id; - this.workspaceId = block.workspace.id; + this.blockId = block ? block.id : null; + this.workspaceId = block? block.workspace.id : null; this.element = element; this.oldValue = oldValue; this.newValue = newValue; + // UI events do not undo or redo. this.recordUndo = false; }; goog.inherits(Blockly.Events.Ui, Blockly.Events.Abstract); diff --git a/tests/jsunit/event_test.js b/tests/jsunit/event_test.js index afdfd215f..40020490a 100644 --- a/tests/jsunit/event_test.js +++ b/tests/jsunit/event_test.js @@ -312,6 +312,52 @@ function test_blockMove_constructoroldParentId() { } } +function test_uiEvent_constructor_null() { + try { + Blockly.Events.setGroup('testGroup'); + var event = new Blockly.Events.Ui(null, 'foo', 'bar', 'baz'); + checkExactEventValues(event, + { + 'blockId': null, + 'workspaceId': null, + 'type': 'ui', + 'oldValue': 'bar', + 'newValue': 'baz', + 'element': 'foo', + 'recordUndo': false, + 'group': 'testGroup' + } + ); + } finally { + Blockly.Events.setGroup(false); + } +} + +function test_uiEvent_constructor_block() { + eventTest_setUpWithMockBlocks(); + setUpMockMethod(mockControl_, Blockly.utils, 'genUid', null, ['1']); + try { + var block1 = createSimpleTestBlock(workspace); + Blockly.Events.setGroup('testGroup'); + var event = new Blockly.Events.Ui(block1, 'foo', 'bar', 'baz'); + checkExactEventValues(event, + { + 'blockId': '1', + 'workspaceId': workspace.id, + 'type': 'ui', + 'oldValue': 'bar', + 'newValue': 'baz', + 'element': 'foo', + 'recordUndo': false, + 'group': 'testGroup' + } + ); + } finally { + Blockly.Events.setGroup(false); + eventTest_tearDownWithMockBlocks(); + } +} + function test_varCreate_constructor() { eventTest_setUp(); try {