feat: procedure def blocks respond to models (#6673)

* feat: make procedure def blocks respond to model updates

* chore: format

* chore: unskip tests
This commit is contained in:
Beka Westberg
2023-01-05 23:11:37 +00:00
committed by GitHub
parent 5d20b62339
commit faf5a08c90
2 changed files with 43 additions and 12 deletions

View File

@@ -312,8 +312,8 @@ const procedureDefGetDefMixin = function() {
},
};
mixin.model = new ObservableProcedureModel(
this.workspace, this.getFieldValue('NAME'));
mixin.model =
new ObservableProcedureModel(this.workspace, this.getFieldValue('NAME'));
this.workspace.getProcedureMap().add(mixin.model);
this.mixin(mixin, true);
@@ -391,6 +391,34 @@ const procedureDefVarMixin = function() {
Extensions.register('procedure_def_var_mixin', procedureDefVarMixin);
const procedureDefUpdateShapeMixin = {
/**
* Updates the block to reflect the state of the procedure model.
*/
doProcedureUpdate: function() {
this.setFieldValue(this.model.getName(), 'NAME');
this.setEnabled(this.model.getEnabled());
this.updateParameters_();
},
/**
* Updates the parameters field to reflect the parameters in the procedure
* model.
*/
updateParameters_: function() {
const params = this.model.getParameters().map((p) => p.getName());
const paramString = params.length ?
`${Msg['PROCEDURES_BEFORE_PARAMS']} ${params.join(', ')}` :
'';
// The field is deterministic based on other events, no need to fire.
Events.disable();
try {
this.setFieldValue(paramString, 'PARAMS');
} finally {
Events.enable();
}
},
/**
* Add or remove the statement block from this function definition.
* @param {boolean} hasStatements True if a statement block is needed.
@@ -624,10 +652,9 @@ const procedureDefMutator = {
};
connDef = connDef['block']['next'];
}
const containerBlock =
serialization.blocks.append(
containerBlockDef, workspace, {recordUndo: false});
const containerBlock = serialization.blocks.append(
containerBlockDef, workspace, {recordUndo: false});
if (this.type === 'procedures_defreturn') {
containerBlock.setFieldValue(this.hasStatements_, 'STATEMENTS');
@@ -743,7 +770,8 @@ Extensions.registerMixin(
const procedureDefOnChangeMixin = {
onchange: function(e) {
if (e.type === Events.BLOCK_CHANGE && e.element == 'disabled') {
if (e.type === Events.BLOCK_CHANGE && e.blockId === this.id &&
e.element == 'disabled') {
this.model.setEnabled(!e.newValue);
}
},

View File

@@ -28,7 +28,7 @@ suite('Procedures', function() {
});
suite('updating data models', function() {
test.skip(
test(
'renaming a procedure def block updates the procedure model',
function() {
const defBlock = createProcDefBlock(this.workspace);
@@ -223,7 +223,7 @@ suite('Procedures', function() {
});
});
suite.skip('responding to data model updates', function() {
suite('responding to data model updates', function() {
suite('def blocks', function() {
test('renaming the procedure data model updates blocks', function() {
const defBlock = createProcDefBlock(this.workspace);
@@ -244,7 +244,7 @@ suite('Procedures', function() {
procModel.setEnabled(false);
chai.assert.isFalse(
defBlock.getEnabled(),
defBlock.isEnabled(),
'Expected the procedure block to be disabled');
});
@@ -329,7 +329,7 @@ suite('Procedures', function() {
});
});
suite('caller blocks', function() {
suite.skip('caller blocks', function() {
test('renaming the procedure data model updates blocks', function() {
const defBlock = createProcDefBlock(this.workspace);
const callBlock = createProcCallBlock(this.workspace);
@@ -947,7 +947,7 @@ suite('Procedures', function() {
});
});
suite('Multiple block serialization', function() {
suite.skip('Multiple block serialization', function() {
function assertDefAndCallBlocks(workspace, noReturnNames, returnNames, hasCallers) {
const allProcedures = Blockly.Procedures.allProcedures(workspace);
const defNoReturnBlocks = allProcedures[0];
@@ -1670,6 +1670,7 @@ suite('Procedures', function() {
},
{
title: 'With vars definition',
skip: true,
xml:
'<block type="' + testSuite.defType + '">\n' +
' <mutation>\n' +
@@ -1695,6 +1696,7 @@ suite('Procedures', function() {
},
{
title: 'With pre-created vars definition',
skip: true,
xml:
'<block type="' + testSuite.defType + '">\n' +
' <mutation>\n' +
@@ -1718,6 +1720,7 @@ suite('Procedures', function() {
},
{
title: 'With pre-created typed vars definition',
skip: true,
xml:
'<block type="' + testSuite.defType + '">\n' +
' <mutation>\n' +