fix: undoing pasting procedure callers that create defs (#6750)

* fix: undoing pasting procedure callers that create defs

* chore: move createDefs_ to the mixin that calls it

This reverts commit 6f4eab2f0884fa108a9ba15ee2db7a3414517a23.
This commit is contained in:
Beka Westberg
2023-01-12 00:41:11 +00:00
committed by GitHub
parent 902c30948a
commit 9f73184803

View File

@@ -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);