Skip to content

Commit

Permalink
fix: improve prompting when deleting variables (#8529)
Browse files Browse the repository at this point in the history
* fix: improve variable deletion behaviors.

* fix: don't prompt about deletion of only 1 variable block when triggered programmatically.

* fix: include the triggering block in the count of referencing blocks

* fix: only count the triggering block as a referencing block if it's not in the flyout
  • Loading branch information
gonfunko authored Aug 19, 2024
1 parent 64fd9ad commit 14d119b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 14 deletions.
9 changes: 5 additions & 4 deletions blocks/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,12 @@ const deleteOptionCallbackFactory = function (
block: VariableBlock,
): () => void {
return function () {
const workspace = block.workspace;
const variableField = block.getField('VAR') as FieldVariable;
const variable = variableField.getVariable()!;
workspace.deleteVariableById(variable.getId());
(workspace as WorkspaceSvg).refreshToolboxSelection();
const variable = variableField.getVariable();
if (variable) {
Variables.deleteVariable(variable.getWorkspace(), variable, block);
}
(block.workspace as WorkspaceSvg).refreshToolboxSelection();
};
};

Expand Down
9 changes: 5 additions & 4 deletions blocks/variables_dynamic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ const renameOptionCallbackFactory = function (block: VariableBlock) {
*/
const deleteOptionCallbackFactory = function (block: VariableBlock) {
return function () {
const workspace = block.workspace;
const variableField = block.getField('VAR') as FieldVariable;
const variable = variableField.getVariable()!;
workspace.deleteVariableById(variable.getId());
(workspace as WorkspaceSvg).refreshToolboxSelection();
const variable = variableField.getVariable();
if (variable) {
Variables.deleteVariable(variable.getWorkspace(), variable, block);
}
(block.workspace as WorkspaceSvg).refreshToolboxSelection();
};
};

Expand Down
7 changes: 6 additions & 1 deletion core/field_variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as fieldRegistry from './field_registry.js';
import * as internalConstants from './internal_constants.js';
import type {Menu} from './menu.js';
import type {MenuItem} from './menuitem.js';
import {WorkspaceSvg} from './workspace_svg.js';
import {Msg} from './msg.js';
import * as parsing from './utils/parsing.js';
import {Size} from './utils/size.js';
Expand Down Expand Up @@ -514,7 +515,11 @@ export class FieldVariable extends FieldDropdown {
return;
} else if (id === internalConstants.DELETE_VARIABLE_ID && this.variable) {
// Delete variable.
this.sourceBlock_.workspace.deleteVariableById(this.variable.getId());
const workspace = this.variable.getWorkspace();
Variables.deleteVariable(workspace, this.variable, this.sourceBlock_);
if (workspace instanceof WorkspaceSvg) {
workspace.refreshToolboxSelection();
}
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/flyout_button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class FlyoutButton implements IASTNodeLocationSvg {
fontWeight,
fontFamily,
);
this.height = fontMetrics.height;
this.height = this.height || fontMetrics.height;

if (!this.isFlyoutLabel) {
this.width += 2 * FlyoutButton.TEXT_MARGIN_X;
Expand Down
18 changes: 14 additions & 4 deletions core/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -714,15 +714,20 @@ export function getVariableUsesById(workspace: Workspace, id: string): Block[] {
*
* @param workspace The workspace from which to delete the variable.
* @param variable The variable to delete.
* @param triggeringBlock The block from which this deletion was triggered, if
* any. Used to exclude it from checking and warning about blocks
* referencing the variable being deleted.
*/
export function deleteVariable(
workspace: Workspace,
variable: IVariableModel<IVariableState>,
triggeringBlock?: Block,
) {
// Check whether this variable is a function parameter before deleting.
const variableName = variable.getName();
const uses = getVariableUsesById(workspace, variable.getId());
for (let i = 0, block; (block = uses[i]); i++) {
for (let i = uses.length - 1; i >= 0; i--) {
const block = uses[i];
if (
block.type === 'procedures_defnoreturn' ||
block.type === 'procedures_defreturn'
Expand All @@ -734,20 +739,25 @@ export function deleteVariable(
dialog.alert(deleteText);
return;
}
if (block === triggeringBlock) {
uses.splice(i, 1);
}
}

if (uses.length > 1) {
if ((triggeringBlock && uses.length) || uses.length > 1) {
// Confirm before deleting multiple blocks.
const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
.replace('%1', String(uses.length))
.replace('%1', String(uses.length + (triggeringBlock ? 1 : 0)))
.replace('%2', variableName);
dialog.confirm(confirmText, (ok) => {
if (ok && variable) {
workspace.getVariableMap().deleteVariable(variable);
}
});
} else {
// No confirmation necessary for a single block.
// No confirmation necessary when the block that triggered the deletion is
// the only block referencing this variable or if only one block referencing
// this variable exists and the deletion was triggered programmatically.
workspace.getVariableMap().deleteVariable(variable);
}
}
Expand Down

0 comments on commit 14d119b

Please sign in to comment.