Skip to content

Commit 7be2a97

Browse files
authored
mutator.js -- auto close mutators on unfocus
1 parent 6dcd539 commit 7be2a97

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

core/mutator.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ Blockly.Mutator.prototype.createEditor_ = function() {
125125
[Workspace]
126126
</svg>
127127
*/
128-
this.svgDialog_ = Blockly.utils.createSvgElement('svg',
129-
{'x': Blockly.Bubble.BORDER_WIDTH, 'y': Blockly.Bubble.BORDER_WIDTH},
130-
null);
128+
this.svgDialog_ = Blockly.utils.createSvgElement(
129+
'svg',
130+
{ class: 'mutator-svg', 'x': Blockly.Bubble.BORDER_WIDTH, 'y': Blockly.Bubble.BORDER_WIDTH },
131+
null
132+
);
131133
// Convert the list of names into a list of XML objects for the flyout.
132134
if (this.quarkNames_.length) {
133135
var quarkXml = goog.dom.createDom('xml');
@@ -164,6 +166,16 @@ Blockly.Mutator.prototype.createEditor_ = function() {
164166
background.insertBefore(flyoutSvg, this.workspace_.svgBlockCanvas_);
165167
this.svgDialog_.appendChild(background);
166168

169+
this.unfocusCheck = (clickEvent) => {
170+
/* close the editor if we lose focus */
171+
const focused = clickEvent.target.closest('.blocklyMutatorBackground')
172+
|| clickEvent.target.closest('.blocklyDraggable')
173+
|| clickEvent.target.closest('.mutator-svg');
174+
175+
if (!focused) this.setVisible(false);
176+
};
177+
document.addEventListener('click', this.unfocusCheck);
178+
167179
return this.svgDialog_;
168180
};
169181

@@ -249,7 +261,7 @@ Blockly.Mutator.prototype.setVisible = function(visible) {
249261
this.bubble_ = new Blockly.Bubble(
250262
/** @type {!Blockly.WorkspaceSvg} */ (this.block_.workspace),
251263
this.createEditor_(), this.block_.svgPath_, this.iconXY_, null, null);
252-
this.bubble_.setColour("#00000055");
264+
this.bubble_.setColour('#00000055');
253265
var tree = this.workspace_.options.languageTree;
254266
if (tree) {
255267
this.workspace_.flyout_.init(this.workspace_);
@@ -265,7 +277,7 @@ Blockly.Mutator.prototype.setVisible = function(visible) {
265277
this.rootBlock_.setMovable(false);
266278
this.rootBlock_.setDeletable(false);
267279
this.rootBlock_.setOutputShape(Blockly.OUTPUT_SHAPE_SQUARE);
268-
this.rootBlock_.setOutput(true, "normal");
280+
this.rootBlock_.setOutput(true, 'normal');
269281
this.rootBlock_.setPreviousStatement(false);
270282
this.rootBlock_.output_ = true;
271283

@@ -295,6 +307,7 @@ Blockly.Mutator.prototype.setVisible = function(visible) {
295307
this.updateColour();
296308
} else {
297309
// Dispose of the bubble.
310+
document.removeEventListener('click', this.unfocusCheck);
298311
this.svgDialog_ = null;
299312
this.workspace_.dispose();
300313
this.workspace_ = null;

0 commit comments

Comments
 (0)