-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4a50ea
chore: moves the public loop mixin into its own file
alschmiedt 5f824c8
chore: fix spacing
alschmiedt 81e8fd7
chore: fix types and format
alschmiedt 2359f32
chore: update renamings file
alschmiedt f36c3e8
chore: fix comment and lint
alschmiedt c1500cc
chore: fix lint
alschmiedt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
*/ | ||
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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 beBlockly.loopMixin
.