Skip to content

Commit 35898ae

Browse files
Refactor fragile argument-like block type checks into helper method (#5521)
* Refactor argument-like block type checks into helper method * prettier formatted
1 parent 4dd5e05 commit 35898ae

File tree

2 files changed

+48
-41
lines changed

2 files changed

+48
-41
lines changed

js/block.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ const COLLAPSIBLES = [
126126
*/
127127
const NOHIT = ["hidden", "hiddennoflow"];
128128

129+
/**
130+
* List of blocks that behave like argument blocks even though they are not
131+
* strictly classified as arg/value blocks.
132+
* @type {string[]}
133+
*/
134+
const ARG_LIKE_BLOCKS = ["doArg", "calcArg", "namedcalcArg", "makeblock"];
135+
129136
/**
130137
* List of special input types.
131138
* @type {string[]}
@@ -797,10 +804,14 @@ class Block {
797804
* @returns {void}
798805
*/
799806
const _postProcess = that => {
800-
that.collapseButtonBitmap.scaleX = that.collapseButtonBitmap.scaleY = that.collapseButtonBitmap.scale =
801-
scale / 3;
802-
that.expandButtonBitmap.scaleX = that.expandButtonBitmap.scaleY = that.expandButtonBitmap.scale =
803-
scale / 3;
807+
that.collapseButtonBitmap.scaleX =
808+
that.collapseButtonBitmap.scaleY =
809+
that.collapseButtonBitmap.scale =
810+
scale / 3;
811+
that.expandButtonBitmap.scaleX =
812+
that.expandButtonBitmap.scaleY =
813+
that.expandButtonBitmap.scale =
814+
scale / 3;
804815
that.updateCache();
805816
that._calculateBlockHitArea();
806817
};
@@ -1510,8 +1521,10 @@ class Block {
15101521
const image = new Image();
15111522
image.onload = () => {
15121523
that.collapseButtonBitmap = new createjs.Bitmap(image);
1513-
that.collapseButtonBitmap.scaleX = that.collapseButtonBitmap.scaleY = that.collapseButtonBitmap.scale =
1514-
that.protoblock.scale / 3;
1524+
that.collapseButtonBitmap.scaleX =
1525+
that.collapseButtonBitmap.scaleY =
1526+
that.collapseButtonBitmap.scale =
1527+
that.protoblock.scale / 3;
15151528
that.container.addChild(that.collapseButtonBitmap);
15161529
that.collapseButtonBitmap.x = 2 * that.protoblock.scale;
15171530
if (that.isInlineCollapsible()) {
@@ -1538,8 +1551,10 @@ class Block {
15381551
const image = new Image();
15391552
image.onload = () => {
15401553
that.expandButtonBitmap = new createjs.Bitmap(image);
1541-
that.expandButtonBitmap.scaleX = that.expandButtonBitmap.scaleY = that.expandButtonBitmap.scale =
1542-
that.protoblock.scale / 3;
1554+
that.expandButtonBitmap.scaleX =
1555+
that.expandButtonBitmap.scaleY =
1556+
that.expandButtonBitmap.scale =
1557+
that.protoblock.scale / 3;
15431558

15441559
that.container.addChild(that.expandButtonBitmap);
15451560
that.expandButtonBitmap.visible = that.collapsed;
@@ -1930,6 +1945,17 @@ class Block {
19301945
return this.protoblock.style === "value" || this.protoblock.style === "arg";
19311946
}
19321947

1948+
/**
1949+
* Checks if the block behaves like an argument block.
1950+
* Some blocks (e.g., doArg, calcArg, namedcalcArg, makeblock) are not styled
1951+
* strictly as arg/value blocks but are treated as argument blocks in
1952+
* certain contexts.
1953+
* @returns {boolean} - True if the block is argument-like, false otherwise.
1954+
*/
1955+
isArgumentLikeBlock() {
1956+
return this.isArgBlock() || ARG_LIKE_BLOCKS.includes(this.name);
1957+
}
1958+
19331959
/**
19341960
* Checks if the block is a two-argument block.
19351961
* @returns {boolean} - True if the block is a two-argument block, false otherwise.
@@ -2747,11 +2773,15 @@ class Block {
27472773
*/
27482774
_positionMedia(bitmap, width, height, blockScale) {
27492775
if (width > height) {
2750-
bitmap.scaleX = bitmap.scaleY = bitmap.scale =
2751-
((MEDIASAFEAREA[2] / width) * blockScale) / 2;
2776+
bitmap.scaleX =
2777+
bitmap.scaleY =
2778+
bitmap.scale =
2779+
((MEDIASAFEAREA[2] / width) * blockScale) / 2;
27522780
} else {
2753-
bitmap.scaleX = bitmap.scaleY = bitmap.scale =
2754-
((MEDIASAFEAREA[3] / height) * blockScale) / 2;
2781+
bitmap.scaleX =
2782+
bitmap.scaleY =
2783+
bitmap.scale =
2784+
((MEDIASAFEAREA[3] / height) * blockScale) / 2;
27552785
}
27562786
bitmap.x = ((MEDIASAFEAREA[0] - 10) * blockScale) / 2;
27572787
bitmap.y = (MEDIASAFEAREA[1] * blockScale) / 2;
@@ -4182,7 +4212,6 @@ class Block {
41824212
return new Date().getTime() - this._piemenuExitTime > 200;
41834213
}
41844214

4185-
41864215
/**
41874216
* Checks and reinitializes widget windows if their labels are changed.
41884217
* @param {boolean} closeInput - Flag indicating whether to close input.

js/blocks.js

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ class Blocks {
638638

639639
/** Which connection do we start with? */
640640
let ci;
641-
if (["doArg", "calcArg", "makeblock"].includes(myBlock.name)) {
641+
if (myBlock.isArgumentLikeBlock()) {
642642
ci = 2;
643643
} else {
644644
ci = 1;
@@ -1838,12 +1838,7 @@ class Blocks {
18381838
if (connection == null) {
18391839
if (this.blockList[newBlock].isArgClamp()) {
18401840
/** If it is an arg clamp, we may have to adjust the slot size. */
1841-
if (
1842-
["doArg", "calcArg", "makeblock"].indexOf(
1843-
this.blockList[newBlock].name
1844-
) !== -1 &&
1845-
newConnection === 1
1846-
) {
1841+
if (this.blockList[newBlock].isArgumentLikeBlock() && newConnection === 1) {
18471842
/** pass */
18481843
} else if (
18491844
["doArg", "nameddoArg"].includes(this.blockList[newBlock].name) &&
@@ -1858,11 +1853,7 @@ class Blocks {
18581853
const slotList = this.blockList[newBlock].argClampSlots;
18591854
let si = newConnection - 1;
18601855
/** Which slot is this block in? */
1861-
if (
1862-
["doArg", "calcArg", "makeblock"].includes(
1863-
this.blockList[newBlock].name
1864-
)
1865-
) {
1856+
if (this.blockList[newBlock].isArgumentLikeBlock()) {
18661857
si = newConnection - 2;
18671858
}
18681859

@@ -1890,12 +1881,7 @@ class Blocks {
18901881
*/
18911882
insertAfterDefault = false;
18921883
if (this.blockList[newBlock].isArgClamp()) {
1893-
if (
1894-
["doArg", "calcArg", "makeblock"].indexOf(
1895-
this.blockList[newBlock].name
1896-
) !== -1 &&
1897-
newConnection === 1
1898-
) {
1884+
if (this.blockList[newBlock].isArgumentLikeBlock() && newConnection === 1) {
18991885
/**
19001886
* If it is the action name then treat it like
19011887
* a standard replacement.
@@ -1929,11 +1915,7 @@ class Blocks {
19291915
/** Which slot is this block in? */
19301916
const ci = this.blockList[newBlock].connections.indexOf(connection);
19311917
let si = ci - 1;
1932-
if (
1933-
["doArg", "calcArg", "makeblock"].includes(
1934-
this.blockList[newBlock].name
1935-
)
1936-
) {
1918+
if (this.blockList[newBlock].isArgumentLikeBlock()) {
19371919
si = ci - 2;
19381920
}
19391921

@@ -2184,11 +2166,7 @@ class Blocks {
21842166

21852167
/** If it is an arg block, where is it coming from? */
21862168
/** FIXME: improve mechanism for testing block types. */
2187-
if (
2188-
(myBlock.isArgBlock() ||
2189-
["calcArg", "namedcalcArg", "makeblock"].includes(myBlock.name)) &&
2190-
newBlock != null
2191-
) {
2169+
if (myBlock.isArgumentLikeBlock() && newBlock != null) {
21922170
/** We care about twoarg blocks with connections to the first arg; */
21932171
if (this.blockList[newBlock].isTwoArgBlock()) {
21942172
if (this.blockList[newBlock].connections[1] === thisBlock) {

0 commit comments

Comments
 (0)