Skip to content

Commit 3f205d5

Browse files
committed
extraction of common logic into a single function
1 parent 4b29d1f commit 3f205d5

File tree

2 files changed

+59
-53
lines changed

2 files changed

+59
-53
lines changed

blocks/EnsembleBlocks.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

js/blocks/EnsembleBlocks.js

Lines changed: 58 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,39 @@ function _blockFindTurtle(activity, turtle, blk, receivedArg) {
6060
}
6161

6262
function setupEnsembleBlocks(activity) {
63-
class TurtleHeapBlock extends LeftBlock {
64-
constructor() {
65-
if (_THIS_IS_MUSIC_BLOCKS_) {
66-
super("turtleheap", _("mouse index heap"));
67-
this.setHelpString([
68-
_("The Mouse index heap block returns a value in the heap at a specified location for a specified mouse."),
69-
"documentation",
70-
""
71-
]);
63+
// Extract common block setup logic
64+
function setupBlock(blockClass) {
65+
new blockClass().setup(activity);
66+
}
7267

73-
this.formBlock({
74-
args: 2,
75-
defaults: [_("Mr. Mouse"), 1],
76-
argTypes: ["anyin", "numberin"],
77-
argLabels: [_("mouse name"), _("index")]
78-
});
79-
} else {
80-
super("turtleheap", _("turtle index heap"));
81-
this.setHelpString([
82-
_("The Turtle index heap block returns a value in the heap at a specified location for a specified turtle."),
83-
"documentation",
84-
""
85-
]);
68+
// Extract common form block logic
69+
function createFormBlock(blockInstance, options) {
70+
blockInstance.formBlock(options);
71+
}
8672

87-
this.formBlock({
88-
args: 2,
89-
//.TRANS: Yertle is the name of a turtle.
90-
defaults: [_("Yertle"), 1],
91-
argTypes: ["anyin", "numberin"],
92-
argLabels: [_("turtle name"), _("index")]
93-
});
94-
}
73+
// Extract common help string logic
74+
function setHelpString(blockInstance, helpText) {
75+
blockInstance.setHelpString(helpText);
76+
}
9577

78+
// Refactor TurtleHeapBlock
79+
class TurtleHeapBlock extends LeftBlock {
80+
constructor() {
81+
super("turtleheap", _THIS_IS_MUSIC_BLOCKS_ ? _("mouse index heap") : _("turtle index heap"));
9682
this.setPalette("ensemble", activity);
83+
84+
const helpText = _THIS_IS_MUSIC_BLOCKS_
85+
? [_("The Mouse index heap block returns a value in the heap at a specified location for a specified mouse."), "documentation", ""]
86+
: [_("The Turtle index heap block returns a value in the heap at a specified location for a specified turtle."), "documentation", ""];
87+
setHelpString(this, helpText);
88+
89+
const formOptions = {
90+
args: 2,
91+
defaults: [_THIS_IS_MUSIC_BLOCKS_ ? _("Mr. Mouse") : _("Yertle"), 1],
92+
argTypes: ["anyin", "numberin"],
93+
argLabels: [_THIS_IS_MUSIC_BLOCKS_ ? _("mouse name") : _("turtle name"), _("index")]
94+
};
95+
createFormBlock(this, formOptions);
9796
}
9897

9998
arg(logo, turtle, blk, receivedArg) {
@@ -1064,6 +1063,7 @@ function setupEnsembleBlocks(activity) {
10641063
.replace(/fill_color/g, fillColor)
10651064
.replace(/stroke_color/g, strokeColor);
10661065

1066+
// eslint-disable-next-line no-undef
10671067
tur.doTurtleShell(55, "data:image/svg+xml;base64," + window.btoa(base64Encode(artwork)));
10681068

10691069
// Restore the heading.
@@ -1305,26 +1305,31 @@ function setupEnsembleBlocks(activity) {
13051305
}
13061306
}
13071307

1308-
new TurtleHeapBlock().setup(activity);
1309-
new StopTurtleBlock().setup(activity);
1310-
new StartTurtleBlock().setup(activity);
1311-
new TurtleColorBlock().setup(activity);
1312-
new TurtleHeadingBlock().setup(activity);
1313-
new SetXYTurtleBlock().setup(activity);
1314-
new SetTurtleBlock().setup(activity);
1315-
new YTurtleBlock().setup(activity);
1316-
new XTurtleBlock().setup(activity);
1317-
new TurtleElapsedNotesBlock().setup(activity);
1318-
new TurtlePitchBlock().setup(activity);
1319-
new TurtleNoteBlock().setup(activity);
1320-
new TurtleNote2Block().setup(activity);
1321-
new TurtleSyncBlock().setup(activity);
1322-
new NthTurtleNameBlock().setup(activity);
1323-
new NumberOfTurtlesBlock().setup(activity);
1324-
new FoundTurtleBlock().setup(activity);
1325-
new NewTurtleBlock().setup(activity);
1326-
new TurtleNameBlock().setup(activity);
1327-
new SetTurtleColorBlock().setup(activity);
1328-
new SetTurtleNameBlock().setup(activity);
1329-
new SetTurtleName2Block().setup(activity);
1330-
}
1308+
// initialize all blocks
1309+
const blockClasses = [
1310+
TurtleHeapBlock,
1311+
StopTurtleBlock,
1312+
StartTurtleBlock,
1313+
TurtleColorBlock,
1314+
TurtleHeadingBlock,
1315+
SetXYTurtleBlock,
1316+
SetTurtleBlock,
1317+
YTurtleBlock,
1318+
XTurtleBlock,
1319+
TurtleElapsedNotesBlock,
1320+
TurtlePitchBlock,
1321+
TurtleNoteBlock,
1322+
TurtleNote2Block,
1323+
TurtleSyncBlock,
1324+
NthTurtleNameBlock,
1325+
NumberOfTurtlesBlock,
1326+
FoundTurtleBlock,
1327+
NewTurtleBlock,
1328+
TurtleNameBlock,
1329+
SetTurtleColorBlock,
1330+
SetTurtleNameBlock,
1331+
SetTurtleName2Block
1332+
];
1333+
1334+
blockClasses.forEach(setupBlock);
1335+
}

0 commit comments

Comments
 (0)