Skip to content

Commit 7e03614

Browse files
author
=
committed
fix: perfectly handle action block renaming, state tracking, and palette refresh
1 parent b2af124 commit 7e03614

2 files changed

Lines changed: 97 additions & 73 deletions

File tree

js/block.js

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3846,7 +3846,7 @@ class Block {
38463846
*/
38473847
_changeLabel() {
38483848
const that = this;
3849-
this.originalValue = this.value;
3849+
this._capturedInitialValue = this.value;
38503850
const x = this.container.x;
38513851
const y = this.container.y;
38523852

@@ -4695,15 +4695,16 @@ class Block {
46954695

46964696
this._labelLock = true;
46974697

4698-
const oldValue = this.originalValue !== undefined ? this.originalValue : this.value;
4698+
const hasInitialValue = typeof this._capturedInitialValue !== "undefined";
4699+
const oldValue = hasInitialValue ? this._capturedInitialValue : this.value;
46994700

47004701
if (closeInput) {
47014702
this.label.style.display = "none";
47024703
if (this.labelattr !== null) {
47034704
this.labelattr.style.display = "none";
47044705
}
47054706
docById("wheelDiv").style.display = "none";
4706-
this.originalValue = undefined;
4707+
delete this._capturedInitialValue;
47074708
}
47084709

47094710
// The pie menu may be visible too, so hide it.
@@ -4732,12 +4733,13 @@ class Block {
47324733
if (oldValue === newValue) {
47334734
// Nothing to do in this case.
47344735
this._labelLock = false;
4735-
if (
4736-
this.name !== "text" ||
4737-
c === null ||
4738-
(this.blocks.blockList[c].name !== "storein" &&
4739-
this.blocks.blockList[c].name !== "action")
4740-
) {
4736+
4737+
const isText = this.name === "text";
4738+
const parentBlock = c !== null ? this.blocks.blockList[c] : null;
4739+
const parentName = parentBlock ? parentBlock.name : "";
4740+
const requiresUpdate = isText && (parentName === "storein" || parentName === "action");
4741+
4742+
if (!requiresUpdate) {
47414743
return;
47424744
}
47434745
}
@@ -4748,22 +4750,25 @@ class Block {
47484750
let uniqueValue;
47494751
switch (cblock.name) {
47504752
case "action":
4751-
if (oldValue !== newValue) {
4752-
this.blocks.palettes.removeActionPrototype(oldValue);
4753-
}
4753+
{
4754+
const isNameChanged = oldValue !== newValue;
4755+
if (isNameChanged) {
4756+
this.blocks.palettes.removeActionPrototype(oldValue);
4757+
}
47544758

4755-
// Ensure new name is unique.
4756-
uniqueValue = this.blocks.findUniqueActionName(newValue, c);
4757-
if (uniqueValue !== newValue) {
4758-
newValue = uniqueValue;
4759-
this.value = newValue;
4760-
let label = this.value.toString();
4761-
if (getTextWidth(label, "bold 20pt Sans") > TEXTWIDTH) {
4762-
label = label.substr(0, STRINGLEN) + "...";
4759+
// Ensure new name is unique.
4760+
const validatedName = this.blocks.findUniqueActionName(newValue, c);
4761+
if (validatedName !== newValue) {
4762+
newValue = validatedName;
4763+
this.value = newValue;
4764+
let label = this.value.toString();
4765+
if (getTextWidth(label, "bold 20pt Sans") > TEXTWIDTH) {
4766+
label = label.substr(0, STRINGLEN) + "...";
4767+
}
4768+
this.text.text = label;
4769+
this.label.value = newValue;
4770+
this.updateCache();
47634771
}
4764-
this.text.text = label;
4765-
this.label.value = newValue;
4766-
this.updateCache();
47674772
}
47684773
break;
47694774
case "pitch":
@@ -4914,53 +4919,66 @@ class Block {
49144919
const cblock = this.blocks.blockList[c];
49154920
switch (cblock.name) {
49164921
case "action":
4917-
if (oldValue !== newValue && closeInput) {
4918-
this.blocks.renameDos(oldValue, newValue);
4922+
{
4923+
const isNameChanged = oldValue !== newValue;
4924+
if (isNameChanged && closeInput) {
4925+
this.blocks.renameDos(oldValue, newValue);
4926+
4927+
const metadata = this.blocks.actionMetadata(c);
4928+
const isDefaultAction =
4929+
oldValue === _("action") || oldValue === "action";
4930+
4931+
if (isDefaultAction) {
4932+
this.blocks.newNameddoBlock(
4933+
newValue,
4934+
metadata.hasReturn,
4935+
metadata.hasArgs
4936+
);
4937+
this.blocks.setActionProtoVisibility(false);
4938+
}
49194939

4920-
const metadata = this.blocks.actionMetadata(c);
4921-
if (oldValue === _("action") || oldValue === "action") {
49224940
this.blocks.newNameddoBlock(
49234941
newValue,
49244942
metadata.hasReturn,
49254943
metadata.hasArgs
49264944
);
4927-
this.blocks.setActionProtoVisibility(false);
4928-
}
4945+
const blockPalette = this.blocks.palettes.dict["action"];
4946+
for (let blk = 0; blk < blockPalette.protoList.length; blk++) {
4947+
const block = blockPalette.protoList[blk];
4948+
if (oldValue === _("action") || oldValue === "action") {
4949+
if (block.name === "nameddo" && block.defaults.length === 0) {
4950+
block.hidden = true;
4951+
}
4952+
} else {
4953+
if (
4954+
block.name === "nameddo" &&
4955+
block.defaults[0] === oldValue
4956+
) {
4957+
blockPalette.remove(block, oldValue);
4958+
}
4959+
}
4960+
}
49294961

4930-
this.blocks.newNameddoBlock(newValue, metadata.hasReturn, metadata.hasArgs);
4931-
const blockPalette = this.blocks.palettes.dict["action"];
4932-
for (let blk = 0; blk < blockPalette.protoList.length; blk++) {
4933-
const block = blockPalette.protoList[blk];
49344962
if (oldValue === _("action") || oldValue === "action") {
4935-
if (block.name === "nameddo" && block.defaults.length === 0) {
4936-
block.hidden = true;
4937-
}
4938-
} else {
4939-
if (block.name === "nameddo" && block.defaults[0] === oldValue) {
4940-
blockPalette.remove(block, oldValue);
4941-
}
4963+
this.blocks.newNameddoBlock(
4964+
newValue,
4965+
metadata.hasReturn,
4966+
metadata.hasArgs
4967+
);
4968+
this.blocks.setActionProtoVisibility(false);
49424969
}
4970+
this.blocks.renameNameddos(oldValue, newValue);
4971+
this.blocks.palettes.hide();
4972+
this.blocks.palettes.updatePalettes("action");
4973+
this.blocks.palettes.show();
4974+
this.activity.refreshCanvas();
49434975
}
4944-
4945-
if (oldValue === _("action") || oldValue === "action") {
4946-
this.blocks.newNameddoBlock(
4947-
newValue,
4948-
metadata.hasReturn,
4949-
metadata.hasArgs
4950-
);
4951-
this.blocks.setActionProtoVisibility(false);
4976+
// Force-open the action palette so the newly created
4977+
// action block is immediately visible to the user.
4978+
if (closeInput) {
4979+
this.blocks.palettes.updatePalettes("action");
4980+
this.blocks.palettes.showPalette("action");
49524981
}
4953-
this.blocks.renameNameddos(oldValue, newValue);
4954-
this.blocks.palettes.hide();
4955-
this.blocks.palettes.updatePalettes("action");
4956-
this.blocks.palettes.show();
4957-
this.activity.refreshCanvas();
4958-
}
4959-
// Force-open the action palette so the newly created
4960-
// action block is immediately visible to the user.
4961-
if (closeInput) {
4962-
this.blocks.palettes.updatePalettes("action");
4963-
this.blocks.palettes.showPalette("action");
49644982
}
49654983
break;
49664984
case "storein":

js/blocks.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4550,23 +4550,29 @@ class Blocks {
45504550
this.blockList[blk].name
45514551
)
45524552
) {
4553-
const block = this.blockList[blk];
4554-
const currentName =
4555-
block.privateData ||
4556-
block.overrideName ||
4557-
(block.protoblock && block.protoblock.defaults[0]);
4558-
if (currentName === oldName) {
4559-
block.privateData = newName;
4560-
let label = newName;
4561-
if (getTextWidth(label, "bold 20pt Sans") > TEXTWIDTH) {
4562-
label = label.substr(0, STRINGLEN) + "...";
4553+
const targetBlock = this.blockList[blk];
4554+
4555+
let activeName = targetBlock.privateData || targetBlock.overrideName;
4556+
if (!activeName && targetBlock.protoblock && targetBlock.protoblock.defaults) {
4557+
activeName = targetBlock.protoblock.defaults[0];
4558+
}
4559+
4560+
if (activeName === oldName) {
4561+
targetBlock.privateData = newName;
4562+
4563+
let displayLabel = newName;
4564+
const isTooWide = getTextWidth(displayLabel, "bold 20pt Sans") > TEXTWIDTH;
4565+
if (isTooWide) {
4566+
displayLabel = displayLabel.substring(0, STRINGLEN) + "...";
45634567
}
45644568

4565-
block.overrideName = label;
4566-
if (block.protoblock && block.protoblock.defaults) {
4567-
block.protoblock.defaults[0] = newName;
4569+
targetBlock.overrideName = displayLabel;
4570+
4571+
if (targetBlock.protoblock && targetBlock.protoblock.defaults) {
4572+
targetBlock.protoblock.defaults[0] = newName;
45684573
}
4569-
block.regenerateArtwork();
4574+
4575+
targetBlock.regenerateArtwork();
45704576
}
45714577
}
45724578
}

0 commit comments

Comments
 (0)