Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: undoing and redoing parameter events #6721

Merged
merged 10 commits into from
Jan 6, 2023
52 changes: 29 additions & 23 deletions blocks/procedures.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,13 @@ const procedureDefMutator = {
if (opt_paramIds) {
container.setAttribute('name', this.getFieldValue('NAME'));
}
for (let i = 0; i < this.argumentVarModels_.length; i++) {

const params = this.getProcedureModel().getParameters();
for (let i = 0; i < params.length; i++) {
const parameter = xmlUtils.createElement('arg');
const argModel = this.argumentVarModels_[i];
parameter.setAttribute('name', argModel.name);
parameter.setAttribute('varid', argModel.getId());
const varModel = params[i].getVariableModel();
parameter.setAttribute('name', varModel.name);
parameter.setAttribute('varid', varModel.getId());
if (opt_paramIds && this.paramIds_) {
parameter.setAttribute('paramId', this.paramIds_[i]);
}
Expand All @@ -556,10 +558,10 @@ const procedureDefMutator = {
for (let i = 0; i < xmlElement.childNodes.length; i++) {
const node = xmlElement.childNodes[i];
if (node.nodeName.toLowerCase() !== 'arg') continue;
const varId = node.getAttribute('varid');
this.getProcedureModel().insertParameter(
new ObservableParameterModel(
this.workspace, node.getAttribute('name'),
node.getAttribute('varid')),
this.workspace, node.getAttribute('name'), undefined, varId),
i);
}

Expand Down Expand Up @@ -596,20 +598,20 @@ const procedureDefMutator = {
* parameters and statements.
*/
saveExtraState: function() {
if (!this.argumentVarModels_.length && this.hasStatements_) {
return null;
}
const params = this.getProcedureModel().getParameters();
if (!params.length && this.hasStatements_) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add undefined to the return type for this function

(nit because i'm not sure anything is currently checking/enforcing these types...?)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah whoops, thanks for the catch. This actually needs to return null, not undefined!


const state = Object.create(null);
if (this.argumentVarModels_.length) {
state['params'] = [];
for (let i = 0; i < this.argumentVarModels_.length; i++) {
state['params'].push({
// We don't need to serialize the name, but just in case we decide
// to separate params from variables.
'name': this.argumentVarModels_[i].name,
'id': this.argumentVarModels_[i].getId(),
});
}
if (params.length) {
state['params'] = params.map((p) => {
return {
'name': p.getName(),
'id': p.getVariableModel().getId(),
// Ideally this would be id, and the other would be varId,
// but backwards compatibility :/
'paramId': p.getId(),
};
});
}
if (!this.hasStatements_) {
state['hasStatements'] = false;
Expand All @@ -625,10 +627,9 @@ const procedureDefMutator = {
loadExtraState: function(state) {
if (state['params']) {
for (let i = 0; i < state['params'].length; i++) {
const param = state['params'][i];
const {name, id, paramId} = state['params'][i];
this.getProcedureModel().insertParameter(
new ObservableParameterModel(this.workspace, param.name, param.id),
i);
new ObservableParameterModel(this.workspace, name, paramId, id), i);
}
}

Expand Down Expand Up @@ -719,7 +720,7 @@ const procedureDefMutator = {
Procedures.mutateCallers(this);

const model = this.getProcedureModel();
const count = this.getProcedureModel().getParameters().length;
const count = model.getParameters().length;
for (let i = count - 1; i >= 0; i--) {
model.deleteParameter(i);
}
Expand Down Expand Up @@ -935,6 +936,7 @@ const validateProcedureParamMixin = {
this.createdVariables_.push(model);
}
}

return varName;
},

Expand Down Expand Up @@ -1210,6 +1212,10 @@ const procedureCallerUpdateShapeMixin = {
this.updateName_();
this.updateEnabled_();
this.updateParameters_();

// Temporarily maintained for code that relies on arguments_
this.arguments_ =
this.getProcedureModel().getParameters().map((p) => p.getName());
},

/**
Expand Down
5 changes: 3 additions & 2 deletions core/procedures/observable_parameter_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ export class ObservableParameterModel implements IParameterModel {
private procedureModel: IProcedureModel|null = null;

constructor(
private readonly workspace: Workspace, name: string, id?: string) {
private readonly workspace: Workspace, name: string, id?: string,
varId?: string) {
this.id = id ?? genUid();
this.variable = this.workspace.getVariable(name) ??
workspace.createVariable(name, '', id);
workspace.createVariable(name, '', varId);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions core/serialization/procedures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {ISerializer} from '../interfaces/i_serializer.js';
import {ObservableProcedureModel} from '../procedures/observable_procedure_model.js';
import {ObservableParameterModel} from '../procedures/observable_parameter_model.js';
import * as priorities from './priorities.js';
// import * as serializationRegistry from './registry.js';
import * as serializationRegistry from './registry.js';
import type {Workspace} from '../workspace.js';


Expand Down Expand Up @@ -171,4 +171,4 @@ export class ProcedureSerializer<ProcedureModel extends IProcedureModel,
export const observableProcedureSerializer =
new ProcedureSerializer(ObservableProcedureModel, ObservableParameterModel);

// serializationRegistry.register('procedures', observableProcedureSerializer);
serializationRegistry.register('procedures', observableProcedureSerializer);
Loading