@@ -17,10 +17,10 @@ import './events/events_var_delete.js';
1717import './events/events_var_rename.js' ;
1818
1919import type { Block } from './block.js' ;
20- import * as dialog from './dialog .js' ;
20+ import * as deprecation from './utils/deprecation .js' ;
2121import * as eventUtils from './events/utils.js' ;
2222import * as registry from './registry.js' ;
23- import { Msg } from './msg .js' ;
23+ import * as Variables from './variables .js' ;
2424import { Names } from './names.js' ;
2525import * as idGenerator from './utils/idgenerator.js' ;
2626import { IVariableModel , IVariableState } from './interfaces/i_variable_model.js' ;
@@ -247,7 +247,6 @@ export class VariableMap
247247 this . variableMap . set ( type , variables ) ;
248248 }
249249 eventUtils . fire ( new ( eventUtils . get ( eventUtils . VAR_CREATE ) ) ( variable ) ) ;
250-
251250 return variable ;
252251 }
253252
@@ -269,90 +268,51 @@ export class VariableMap
269268
270269 /* Begin functions for variable deletion. */
271270 /**
272- * Delete a variable.
271+ * Delete a variable and all of its uses without confirmation .
273272 *
274273 * @param variable Variable to delete.
275274 */
276275 deleteVariable ( variable : IVariableModel < IVariableState > ) {
277- const variables = this . variableMap . get ( variable . getType ( ) ) ;
278- if ( ! variables || ! variables . has ( variable . getId ( ) ) ) return ;
279- variables . delete ( variable . getId ( ) ) ;
280- eventUtils . fire ( new ( eventUtils . get ( eventUtils . VAR_DELETE ) ) ( variable ) ) ;
281- if ( variables . size === 0 ) {
282- this . variableMap . delete ( variable . getType ( ) ) ;
276+ const uses = this . getVariableUsesById ( variable . getId ( ) ) ;
277+ const existingGroup = eventUtils . getGroup ( ) ;
278+ if ( ! existingGroup ) {
279+ eventUtils . setGroup ( true ) ;
280+ }
281+ try {
282+ for ( let i = 0 ; i < uses . length ; i ++ ) {
283+ uses [ i ] . dispose ( true ) ;
284+ }
285+ const variables = this . variableMap . get ( variable . getType ( ) ) ;
286+ if ( ! variables || ! variables . has ( variable . getId ( ) ) ) return ;
287+ variables . delete ( variable . getId ( ) ) ;
288+ eventUtils . fire ( new ( eventUtils . get ( eventUtils . VAR_DELETE ) ) ( variable ) ) ;
289+ if ( variables . size === 0 ) {
290+ this . variableMap . delete ( variable . getType ( ) ) ;
291+ }
292+ } finally {
293+ eventUtils . setGroup ( existingGroup ) ;
283294 }
284295 }
285296
286297 /**
287- * Delete a variables by the passed in ID and all of its uses from this
288- * workspace. May prompt the user for confirmation.
298+ * @deprecated v12 - Delete a variables by the passed in ID and all of its
299+ * uses from this workspace. May prompt the user for confirmation.
289300 *
290301 * @param id ID of variable to delete.
291302 */
292303 deleteVariableById ( id : string ) {
304+ deprecation . warn (
305+ 'VariableMap.deleteVariableById' ,
306+ 'v12' ,
307+ 'v13' ,
308+ 'Blockly.Variables.deleteVariable' ,
309+ ) ;
293310 const variable = this . getVariableById ( id ) ;
294311 if ( variable ) {
295- // Check whether this variable is a function parameter before deleting.
296- const variableName = variable . getName ( ) ;
297- const uses = this . getVariableUsesById ( id ) ;
298- for ( let i = 0 , block ; ( block = uses [ i ] ) ; i ++ ) {
299- if (
300- block . type === 'procedures_defnoreturn' ||
301- block . type === 'procedures_defreturn'
302- ) {
303- const procedureName = String ( block . getFieldValue ( 'NAME' ) ) ;
304- const deleteText = Msg [ 'CANNOT_DELETE_VARIABLE_PROCEDURE' ]
305- . replace ( '%1' , variableName )
306- . replace ( '%2' , procedureName ) ;
307- dialog . alert ( deleteText ) ;
308- return ;
309- }
310- }
311-
312- if ( uses . length > 1 ) {
313- // Confirm before deleting multiple blocks.
314- const confirmText = Msg [ 'DELETE_VARIABLE_CONFIRMATION' ]
315- . replace ( '%1' , String ( uses . length ) )
316- . replace ( '%2' , variableName ) ;
317- dialog . confirm ( confirmText , ( ok ) => {
318- if ( ok && variable ) {
319- this . deleteVariableInternal ( variable , uses ) ;
320- }
321- } ) ;
322- } else {
323- // No confirmation necessary for a single block.
324- this . deleteVariableInternal ( variable , uses ) ;
325- }
326- } else {
327- console . warn ( "Can't delete non-existent variable: " + id ) ;
312+ Variables . deleteVariable ( this . workspace , variable ) ;
328313 }
329314 }
330315
331- /**
332- * Deletes a variable and all of its uses from this workspace without asking
333- * the user for confirmation.
334- *
335- * @param variable Variable to delete.
336- * @param uses An array of uses of the variable.
337- * @internal
338- */
339- deleteVariableInternal (
340- variable : IVariableModel < IVariableState > ,
341- uses : Block [ ] ,
342- ) {
343- const existingGroup = eventUtils . getGroup ( ) ;
344- if ( ! existingGroup ) {
345- eventUtils . setGroup ( true ) ;
346- }
347- try {
348- for ( let i = 0 ; i < uses . length ; i ++ ) {
349- uses [ i ] . dispose ( true ) ;
350- }
351- this . deleteVariable ( variable ) ;
352- } finally {
353- eventUtils . setGroup ( existingGroup ) ;
354- }
355- }
356316 /* End functions for variable deletion. */
357317 /**
358318 * Find the variable by the given name and type and return it. Return null if
@@ -431,7 +391,7 @@ export class VariableMap
431391 getVariableTypes ( ws : Workspace | null ) : string [ ] {
432392 const variableTypes = new Set < string > ( this . variableMap . keys ( ) ) ;
433393 if ( ws && ws . getPotentialVariableMap ( ) ) {
434- for ( const key of ws . getPotentialVariableMap ( ) ! . variableMap . keys ( ) ) {
394+ for ( const key of ws . getPotentialVariableMap ( ) ! . getTypes ( ) ) {
435395 variableTypes . add ( key ) ;
436396 }
437397 }
@@ -470,26 +430,19 @@ export class VariableMap
470430 }
471431
472432 /**
473- * Find all the uses of a named variable.
433+ * @deprecated v12 - Find all the uses of a named variable.
474434 *
475435 * @param id ID of the variable to find.
476436 * @returns Array of block usages.
477437 */
478438 getVariableUsesById ( id : string ) : Block [ ] {
479- const uses = [ ] ;
480- const blocks = this . workspace . getAllBlocks ( false ) ;
481- // Iterate through every block and check the name.
482- for ( let i = 0 ; i < blocks . length ; i ++ ) {
483- const blockVariables = blocks [ i ] . getVarModels ( ) ;
484- if ( blockVariables ) {
485- for ( let j = 0 ; j < blockVariables . length ; j ++ ) {
486- if ( blockVariables [ j ] . getId ( ) === id ) {
487- uses . push ( blocks [ i ] ) ;
488- }
489- }
490- }
491- }
492- return uses ;
439+ deprecation . warn (
440+ 'VariableMap.getVariableUsesById' ,
441+ 'v12' ,
442+ 'v13' ,
443+ 'Blockly.Variables.getVariableUsesById' ,
444+ ) ;
445+ return Variables . getVariableUsesById ( this . workspace , id ) ;
493446 }
494447}
495448
0 commit comments