diff --git a/blocks/procedures.js b/blocks/procedures.js index ee4e0958e..49ce8a046 100644 --- a/blocks/procedures.js +++ b/blocks/procedures.js @@ -961,32 +961,6 @@ const procedureCallerGetDefMixin = function() { return model; }, - /** - * Creates a procedure definition block with the given name and params, - * and returns the procedure model associated with it. - * @param {string} name The name of the procedure to create. - * @param {string[]} params The names of the parameters to create. - * @return {IProcedureModel} The procedure model associated with the new - * procedure definition block. - */ - createDef_(name, params = []) { - const xy = this.getRelativeToSurfaceXY(); - const newName = Procedures.findLegalName(name, this); - this.renameProcedure(name, newName); - - const blockDef = { - 'type': this.defType_, - 'x': xy.x + config.snapRadius * (this.RTL ? -1 : 1), - 'y': xy.y + config.snapRadius * 2, - 'extraState': { - 'params': params.map((p) => ({'name': p})), - }, - 'fields': {'NAME': newName}, - }; - return serialization.blocks.append(blockDef, this.getTargetWorkspace_()) - .getProcedureModel(); - }, - /** * @return {Workspace} The main workspace (i.e. not the flyout workspace) * associated with this block. @@ -1293,7 +1267,6 @@ const procedureCallerOnChangeMixin = { // Events not generated by user. Skip handling. return; } - // TODO: Clean this up to call createDef_. if (event.type === Events.BLOCK_CREATE && (event.blockId === this.id || event.ids.indexOf(this.id) !== -1)) { // Look for the case where a procedure call was created (usually through @@ -1318,11 +1291,44 @@ const procedureCallerOnChangeMixin = { } }, + /** + * Returns true if the given def block matches the definition of this caller + * block. + * @param {Block} defBlock The definition block to check against. + * @return {boolean} Whether the def block matches or not. + */ defMatches_(defBlock) { return defBlock && defBlock.type === this.defType_ && JSON.stringify(defBlock.getVars()) === JSON.stringify(this.paramsFromSerializedState_); }, + + /** + * Creates a procedure definition block with the given name and params, + * and returns the procedure model associated with it. + * @param {string} name The name of the procedure to create. + * @param {string[]} params The names of the parameters to create. + * @return {IProcedureModel} The procedure model associated with the new + * procedure definition block. + */ + createDef_(name, params = []) { + const xy = this.getRelativeToSurfaceXY(); + const newName = Procedures.findLegalName(name, this); + this.renameProcedure(name, newName); + + const blockDef = { + 'type': this.defType_, + 'x': xy.x + config.snapRadius * (this.RTL ? -1 : 1), + 'y': xy.y + config.snapRadius * 2, + 'extraState': { + 'params': params.map((p) => ({'name': p})), + }, + 'fields': {'NAME': newName}, + }; + return serialization.blocks + .append(blockDef, this.getTargetWorkspace_(), {recordUndo: true}) + .getProcedureModel(); + }, }; Extensions.registerMixin( 'procedure_caller_onchange_mixin', procedureCallerOnChangeMixin);