Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: moves the public loop mixin into its own file #5730

Merged
merged 6 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 1 addition & 70 deletions blocks/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ goog.require('Blockly.FieldLabel');
goog.require('Blockly.FieldNumber');
goog.require('Blockly.FieldVariable');
goog.require('Blockly.Warning');
goog.require('Blockly.loopMixin');


/**
Expand Down Expand Up @@ -272,73 +273,3 @@ Blockly.Extensions.register('controls_for_tooltip',
Blockly.Extensions.register('controls_forEach_tooltip',
Blockly.Extensions.buildTooltipWithFieldText(
'%{BKY_CONTROLS_FOREACH_TOOLTIP}', 'VAR'));

/**
* This mixin adds a check to make sure the 'controls_flow_statements' block
* is contained in a loop. Otherwise a warning is added to the block.
* @mixin
* @augments Blockly.Block
* @package
* @readonly
*/
Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {
/**
* List of block types that are loops and thus do not need warnings.
* To add a new loop type add this to your code:
* Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.LOOP_TYPES.push('custom_loop');
*/
LOOP_TYPES: [
'controls_repeat',
'controls_repeat_ext',
'controls_forEach',
'controls_for',
'controls_whileUntil',
],

/**
* Is the given block enclosed (at any level) by a loop?
* @param {!Blockly.Block} block Current block.
* @return {Blockly.Block} The nearest surrounding loop, or null if none.
*/
getSurroundLoop: function(block) {
// Is the block nested in a loop?
do {
if (Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.LOOP_TYPES
.indexOf(block.type) !== -1) {
return block;
}
block = block.getSurroundParent();
} while (block);
return null;
},

/**
* Called whenever anything on the workspace changes.
* Add warning if this flow block is not nested inside a loop.
* @param {!Blockly.Events.Abstract} e Change event.
* @this {Blockly.Block}
*/
onchange: function(e) {
// Don't change state if:
// * It's at the start of a drag.
// * It's not a move event.
if (!this.workspace.isDragging || this.workspace.isDragging() ||
e.type !== Blockly.Events.BLOCK_MOVE) {
return;
}
const enabled = Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN
.getSurroundLoop(this);
this.setWarningText(enabled ? null :
Blockly.Msg['CONTROLS_FLOW_STATEMENTS_WARNING']);
if (!this.isInFlyout) {
const group = Blockly.Events.getGroup();
// Makes it so the move and the disable event get undone together.
Blockly.Events.setGroup(e.group);
this.setEnabled(enabled);
Blockly.Events.setGroup(group);
}
},
};

Blockly.Extensions.registerMixin('controls_flow_in_loop_check',
Blockly.Constants.Loops.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN);
2 changes: 2 additions & 0 deletions core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const dialog = goog.require('Blockly.dialog');
const fieldRegistry = goog.require('Blockly.fieldRegistry');
const geras = goog.require('Blockly.geras');
const internalConstants = goog.require('Blockly.internalConstants');
const loopMixin = goog.require('Blockly.loopMixin');
const minimalist = goog.require('Blockly.minimalist');
const registry = goog.require('Blockly.registry');
const svgMath = goog.require('Blockly.utils.svgMath');
Expand Down Expand Up @@ -682,6 +683,7 @@ exports.fieldRegistry = fieldRegistry;
exports.geras = geras;
exports.inject = inject;
exports.inputTypes = inputTypes;
exports.loopMixin = loopMixin;
exports.minimalist = minimalist;
exports.registry = registry;
exports.thrasos = thrasos;
Expand Down
83 changes: 83 additions & 0 deletions core/loop_mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview The mixin that holds logic for setting a warning on a block if
* it is not inside a loop.
*/
'use strict';

goog.module('Blockly.loopMixin');

const Extensions = goog.require('Blockly.Extensions');


/**
* This mixin adds a check to make sure the 'controls_flow_statements' block
* is contained in a loop. Otherwise a warning is added to the block.
* @mixin
* @augments Blockly.Block
* @readonly
*/
const CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = {
/**
* List of block types that are loops and thus do not need warnings.
* To add a new loop type add this to your code:
* Blockly.LoopMixin.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.LOOP_TYPES.push('custom_loop');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update comment to refer to the new location, and add to renamings file as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And add a comment telling them to talk to us if they want to use this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok added a comment asking them to comment on this issue if they need to use this. And I updated Blockly.LoopMixin to be Blockly.loopMixin.

*/
LOOP_TYPES: [
'controls_repeat',
'controls_repeat_ext',
'controls_forEach',
'controls_for',
'controls_whileUntil',
],

/**
* Is the given block enclosed (at any level) by a loop?
* @param {!Blockly.Block} block Current block.
* @return {Blockly.Block} The nearest surrounding loop, or null if none.
*/
getSurroundLoop: function(block) {
// Is the block nested in a loop?
do {
if (this.LOOP_TYPES.indexOf(block.type) !== -1) {
return block;
}
block = block.getSurroundParent();
} while (block);
return null;
},

/**
* Called whenever anything on the workspace changes.
* Add warning if this flow block is not nested inside a loop.
* @param {!Blockly.Events.Abstract} e Change event.
* @this {Blockly.Block}
*/
onchange: function(e) {
// Don't change state if:
// * It's at the start of a drag.
// * It's not a move event.
if (!this.workspace.isDragging || this.workspace.isDragging() ||
e.type !== Blockly.Events.BLOCK_MOVE) {
return;
}
const enabled = this.getSurroundLoop(this);
this.setWarningText(enabled ? null :
Blockly.Msg['CONTROLS_FLOW_STATEMENTS_WARNING']);
if (!this.isInFlyout) {
const group = Blockly.Events.getGroup();
// Makes it so the move and the disable event get undone together.
Blockly.Events.setGroup(e.group);
this.setEnabled(enabled);
Blockly.Events.setGroup(group);
}
},
};
exports.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = CONTROL_FLOW_IN_LOOP_CHECK_MIXIN;

Extensions.registerMixin('controls_flow_in_loop_check', CONTROL_FLOW_IN_LOOP_CHECK_MIXIN);
1 change: 1 addition & 0 deletions externs/block-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ goog.provide('Blockly.FieldTextInput');
goog.provide('Blockly.FieldVariable');
goog.provide('Blockly.Mutator');
goog.provide('Blockly.Warning');
goog.provide('Blockly.loopMixin');

var Blockly;
1 change: 1 addition & 0 deletions externs/generator-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
goog.provide('Blockly');
goog.provide('Blockly.Generator');
goog.provide('Blockly.inputTypes');
goog.provide('Blockly.loopMixin');
goog.provide('Blockly.utils.global');
goog.provide('Blockly.utils.object');
goog.provide('Blockly.utils.string');
Expand Down
2 changes: 1 addition & 1 deletion generators/dart/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Blockly.Dart['controls_flow_statements'] = function(block) {
xfix += Blockly.Dart.injectId(Blockly.Dart.STATEMENT_SUFFIX, block);
}
if (Blockly.Dart.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
3 changes: 2 additions & 1 deletion generators/javascript/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
goog.provide('Blockly.JavaScript.loops');

goog.require('Blockly.JavaScript');
goog.require('Blockly.loopMixin');


Blockly.JavaScript['controls_repeat_ext'] = function(block) {
Expand Down Expand Up @@ -164,7 +165,7 @@ Blockly.JavaScript['controls_flow_statements'] = function(block) {
block);
}
if (Blockly.JavaScript.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
2 changes: 1 addition & 1 deletion generators/lua/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ Blockly.Lua['controls_flow_statements'] = function(block) {
xfix += Blockly.Lua.injectId(Blockly.Lua.STATEMENT_SUFFIX, block);
}
if (Blockly.Lua.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
2 changes: 1 addition & 1 deletion generators/php/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Blockly.PHP['controls_flow_statements'] = function(block) {
xfix += Blockly.PHP.injectId(Blockly.PHP.STATEMENT_SUFFIX, block);
}
if (Blockly.PHP.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
2 changes: 1 addition & 1 deletion generators/python/loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Blockly.Python['controls_flow_statements'] = function(block) {
xfix += Blockly.Python.injectId(Blockly.Python.STATEMENT_SUFFIX, block);
}
if (Blockly.Python.STATEMENT_PREFIX) {
const loop = Blockly.Constants.Loops
const loop = Blockly.loopMixin
.CONTROL_FLOW_IN_LOOP_CHECK_MIXIN.getSurroundLoop(block);
if (loop && !loop.suppressPrefixSuffix) {
// Inject loop's statement prefix here since the regular one at the end
Expand Down
Loading