mirror of
https://github.com/google/blockly.git
synced 2026-01-04 15:40:08 +01:00
fix: improve prompting when deleting variables (#8529)
* 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
This commit is contained in:
@@ -165,11 +165,12 @@ const deleteOptionCallbackFactory = function (
|
|||||||
block: VariableBlock,
|
block: VariableBlock,
|
||||||
): () => void {
|
): () => void {
|
||||||
return function () {
|
return function () {
|
||||||
const workspace = block.workspace;
|
|
||||||
const variableField = block.getField('VAR') as FieldVariable;
|
const variableField = block.getField('VAR') as FieldVariable;
|
||||||
const variable = variableField.getVariable()!;
|
const variable = variableField.getVariable();
|
||||||
workspace.deleteVariableById(variable.getId());
|
if (variable) {
|
||||||
(workspace as WorkspaceSvg).refreshToolboxSelection();
|
Variables.deleteVariable(variable.getWorkspace(), variable, block);
|
||||||
|
}
|
||||||
|
(block.workspace as WorkspaceSvg).refreshToolboxSelection();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -176,11 +176,12 @@ const renameOptionCallbackFactory = function (block: VariableBlock) {
|
|||||||
*/
|
*/
|
||||||
const deleteOptionCallbackFactory = function (block: VariableBlock) {
|
const deleteOptionCallbackFactory = function (block: VariableBlock) {
|
||||||
return function () {
|
return function () {
|
||||||
const workspace = block.workspace;
|
|
||||||
const variableField = block.getField('VAR') as FieldVariable;
|
const variableField = block.getField('VAR') as FieldVariable;
|
||||||
const variable = variableField.getVariable()!;
|
const variable = variableField.getVariable();
|
||||||
workspace.deleteVariableById(variable.getId());
|
if (variable) {
|
||||||
(workspace as WorkspaceSvg).refreshToolboxSelection();
|
Variables.deleteVariable(variable.getWorkspace(), variable, block);
|
||||||
|
}
|
||||||
|
(block.workspace as WorkspaceSvg).refreshToolboxSelection();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import * as fieldRegistry from './field_registry.js';
|
|||||||
import * as internalConstants from './internal_constants.js';
|
import * as internalConstants from './internal_constants.js';
|
||||||
import type {Menu} from './menu.js';
|
import type {Menu} from './menu.js';
|
||||||
import type {MenuItem} from './menuitem.js';
|
import type {MenuItem} from './menuitem.js';
|
||||||
|
import {WorkspaceSvg} from './workspace_svg.js';
|
||||||
import {Msg} from './msg.js';
|
import {Msg} from './msg.js';
|
||||||
import * as parsing from './utils/parsing.js';
|
import * as parsing from './utils/parsing.js';
|
||||||
import {Size} from './utils/size.js';
|
import {Size} from './utils/size.js';
|
||||||
@@ -514,7 +515,11 @@ export class FieldVariable extends FieldDropdown {
|
|||||||
return;
|
return;
|
||||||
} else if (id === internalConstants.DELETE_VARIABLE_ID && this.variable) {
|
} else if (id === internalConstants.DELETE_VARIABLE_ID && this.variable) {
|
||||||
// Delete 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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ export class FlyoutButton implements IASTNodeLocationSvg {
|
|||||||
fontWeight,
|
fontWeight,
|
||||||
fontFamily,
|
fontFamily,
|
||||||
);
|
);
|
||||||
this.height = fontMetrics.height;
|
this.height = this.height || fontMetrics.height;
|
||||||
|
|
||||||
if (!this.isFlyoutLabel) {
|
if (!this.isFlyoutLabel) {
|
||||||
this.width += 2 * FlyoutButton.TEXT_MARGIN_X;
|
this.width += 2 * FlyoutButton.TEXT_MARGIN_X;
|
||||||
|
|||||||
@@ -714,15 +714,20 @@ export function getVariableUsesById(workspace: Workspace, id: string): Block[] {
|
|||||||
*
|
*
|
||||||
* @param workspace The workspace from which to delete the variable.
|
* @param workspace The workspace from which to delete the variable.
|
||||||
* @param variable The variable to delete.
|
* @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(
|
export function deleteVariable(
|
||||||
workspace: Workspace,
|
workspace: Workspace,
|
||||||
variable: IVariableModel<IVariableState>,
|
variable: IVariableModel<IVariableState>,
|
||||||
|
triggeringBlock?: Block,
|
||||||
) {
|
) {
|
||||||
// Check whether this variable is a function parameter before deleting.
|
// Check whether this variable is a function parameter before deleting.
|
||||||
const variableName = variable.getName();
|
const variableName = variable.getName();
|
||||||
const uses = getVariableUsesById(workspace, variable.getId());
|
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 (
|
if (
|
||||||
block.type === 'procedures_defnoreturn' ||
|
block.type === 'procedures_defnoreturn' ||
|
||||||
block.type === 'procedures_defreturn'
|
block.type === 'procedures_defreturn'
|
||||||
@@ -734,12 +739,15 @@ export function deleteVariable(
|
|||||||
dialog.alert(deleteText);
|
dialog.alert(deleteText);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (block === triggeringBlock) {
|
||||||
|
uses.splice(i, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uses.length > 1) {
|
if ((triggeringBlock && uses.length) || uses.length > 1) {
|
||||||
// Confirm before deleting multiple blocks.
|
// Confirm before deleting multiple blocks.
|
||||||
const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
|
const confirmText = Msg['DELETE_VARIABLE_CONFIRMATION']
|
||||||
.replace('%1', String(uses.length))
|
.replace('%1', String(uses.length + (triggeringBlock ? 1 : 0)))
|
||||||
.replace('%2', variableName);
|
.replace('%2', variableName);
|
||||||
dialog.confirm(confirmText, (ok) => {
|
dialog.confirm(confirmText, (ok) => {
|
||||||
if (ok && variable) {
|
if (ok && variable) {
|
||||||
@@ -747,7 +755,9 @@ export function deleteVariable(
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} 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);
|
workspace.getVariableMap().deleteVariable(variable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user