From d3b8b780355f4a6d1faafe4bb4dbb1bd47d9af77 Mon Sep 17 00:00:00 2001 From: Monica Kozbial Date: Fri, 10 Jul 2020 13:23:08 -0700 Subject: [PATCH] Update click event handling for workspace and trashcan. (#4024) * Update click event handling for workspace and trashcan. * Fix typo * Change event name for trashcan flyout open --- core/gesture.js | 4 ++-- core/trashcan.js | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/core/gesture.js b/core/gesture.js index 190827de7..a5a4cd2ec 100644 --- a/core/gesture.js +++ b/core/gesture.js @@ -662,7 +662,7 @@ Blockly.Gesture.prototype.handleWsStart = function(e, ws) { * @private */ Blockly.Gesture.prototype.fireWorkspaceClick_ = function(ws) { - var clickEvent = new Blockly.Events.Ui(null, 'workspaceClick', null, null); + var clickEvent = new Blockly.Events.Ui(null, 'click', null, 'workspace'); clickEvent.workspaceId = ws.id; Blockly.Events.fire(clickEvent); }; @@ -752,7 +752,7 @@ Blockly.Gesture.prototype.doBlockClick_ = function() { } else { // Clicks events are on the start block, even if it was a shadow. Blockly.Events.fire( - new Blockly.Events.Ui(this.startBlock_, 'click', undefined, undefined)); + new Blockly.Events.Ui(this.startBlock_, 'click', undefined, 'block')); } this.bringBlockToFront_(); Blockly.Events.setGroup(false); diff --git a/core/trashcan.js b/core/trashcan.js index 107b9d602..fcb2acc0c 100644 --- a/core/trashcan.js +++ b/core/trashcan.js @@ -301,6 +301,8 @@ Blockly.Trashcan.prototype.createDom = function() { this.svgLid_.setAttributeNS(Blockly.utils.dom.XLINK_NS, 'xlink:href', this.workspace_.options.pathToMedia + Blockly.SPRITE.url); + Blockly.bindEventWithChecks_( + this.svgGroup_, 'mousedown', this, this.blockMouseDownWhenFull_); Blockly.bindEventWithChecks_(this.svgGroup_, 'mouseup', this, this.click); // bindEventWithChecks_ quashes events too aggressively. See: // https://groups.google.com/forum/#!topic/blockly/QF4yB9Wx00s @@ -345,6 +347,15 @@ Blockly.Trashcan.prototype.dispose = function() { clearTimeout(this.lidTask_); }; +/** + * Whether the trashcan has contents. + * @return {boolean} True if the trashcan has contents. + * @private + */ +Blockly.Trashcan.prototype.hasContents_ = function() { + return !!this.contents_.length; +}; + /** * Returns true if the trashcan contents-flyout is currently open. * @return {boolean} True if the trashcan contents-flyout is currently open. @@ -358,7 +369,7 @@ Blockly.Trashcan.prototype.contentsIsOpen = function() { * it will be closed. */ Blockly.Trashcan.prototype.emptyContents = function() { - if (!this.contents_.length) { + if (!this.hasContents_()) { return; } this.contents_.length = 0; @@ -501,7 +512,7 @@ Blockly.Trashcan.prototype.close = function() { * Inspect the contents of the trash. */ Blockly.Trashcan.prototype.click = function() { - if (!this.contents_.length) { + if (!this.hasContents_()) { return; } @@ -510,14 +521,38 @@ Blockly.Trashcan.prototype.click = function() { xml[i] = Blockly.Xml.textToDom(text); } this.flyout.show(xml); + + this.fireUiEvent_(); }; +/** + * Fires a ui event for trashcan flyout opening. + * @private + */ +Blockly.Trashcan.prototype.fireUiEvent_ = function() { + var uiEvent = new Blockly.Events.Ui(null, 'trashcanOpen', null, true); + uiEvent.workspaceId = this.workspace_.id; + Blockly.Events.fire(uiEvent); +}; + +/** + * Prevents a workspace scroll and click event if the trashcan has blocks. + * @param {!Event} e A mouse down event. + * @private + */ +Blockly.Trashcan.prototype.blockMouseDownWhenFull_ = function(e) { + if (this.hasContents_()) { + e.stopPropagation(); // Don't start a workspace scroll. + } +}; + + /** * Indicate that the trashcan can be clicked (by opening it) if it has blocks. * @private */ Blockly.Trashcan.prototype.mouseOver_ = function() { - if (this.contents_.length) { + if (this.hasContents_()) { this.setOpen(true); } };